2 * Copyright © 2014-2017 Broadcom
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
26 * Functions for submitting VC5 render jobs to the kernel.
30 #include "vc5_context.h"
31 #include "util/hash_table.h"
32 #include "util/ralloc.h"
34 #include "broadcom/clif/clif_dump.h"
35 #include "broadcom/cle/v3d_packet_v33_pack.h"
38 remove_from_ht(struct hash_table
*ht
, void *key
)
40 struct hash_entry
*entry
= _mesa_hash_table_search(ht
, key
);
41 _mesa_hash_table_remove(ht
, entry
);
45 vc5_job_free(struct vc5_context
*vc5
, struct vc5_job
*job
)
47 struct set_entry
*entry
;
49 set_foreach(job
->bos
, entry
) {
50 struct vc5_bo
*bo
= (struct vc5_bo
*)entry
->key
;
51 vc5_bo_unreference(&bo
);
54 remove_from_ht(vc5
->jobs
, &job
->key
);
56 for (int i
= 0; i
< VC5_MAX_DRAW_BUFFERS
; i
++) {
58 remove_from_ht(vc5
->write_jobs
, job
->cbufs
[i
]->texture
);
59 pipe_surface_reference(&job
->cbufs
[i
], NULL
);
63 remove_from_ht(vc5
->write_jobs
, job
->zsbuf
->texture
);
64 pipe_surface_reference(&job
->zsbuf
, NULL
);
70 vc5_destroy_cl(&job
->bcl
);
71 vc5_destroy_cl(&job
->rcl
);
72 vc5_destroy_cl(&job
->indirect
);
73 vc5_bo_unreference(&job
->tile_alloc
);
78 static struct vc5_job
*
79 vc5_job_create(struct vc5_context
*vc5
)
81 struct vc5_job
*job
= rzalloc(vc5
, struct vc5_job
);
85 vc5_init_cl(job
, &job
->bcl
);
86 vc5_init_cl(job
, &job
->rcl
);
87 vc5_init_cl(job
, &job
->indirect
);
94 job
->bos
= _mesa_set_create(job
,
96 _mesa_key_pointer_equal
);
101 vc5_job_add_bo(struct vc5_job
*job
, struct vc5_bo
*bo
)
106 if (_mesa_set_search(job
->bos
, bo
))
109 vc5_bo_reference(bo
);
110 _mesa_set_add(job
->bos
, bo
);
112 uint32_t *bo_handles
= (void *)(uintptr_t)job
->submit
.bo_handles
;
114 if (job
->submit
.bo_handle_count
>= job
->bo_handles_size
) {
115 job
->bo_handles_size
= MAX2(4, job
->bo_handles_size
* 2);
116 bo_handles
= reralloc(job
, bo_handles
,
117 uint32_t, job
->bo_handles_size
);
118 job
->submit
.bo_handles
= (uintptr_t)(void *)bo_handles
;
120 bo_handles
[job
->submit
.bo_handle_count
++] = bo
->handle
;
124 vc5_flush_jobs_writing_resource(struct vc5_context
*vc5
,
125 struct pipe_resource
*prsc
)
127 struct hash_entry
*entry
= _mesa_hash_table_search(vc5
->write_jobs
,
130 struct vc5_job
*job
= entry
->data
;
131 vc5_job_submit(vc5
, job
);
136 vc5_flush_jobs_reading_resource(struct vc5_context
*vc5
,
137 struct pipe_resource
*prsc
)
139 struct vc5_resource
*rsc
= vc5_resource(prsc
);
141 vc5_flush_jobs_writing_resource(vc5
, prsc
);
143 struct hash_entry
*entry
;
144 hash_table_foreach(vc5
->jobs
, entry
) {
145 struct vc5_job
*job
= entry
->data
;
147 if (_mesa_set_search(job
->bos
, rsc
->bo
)) {
148 vc5_job_submit(vc5
, job
);
149 /* Reminder: vc5->jobs is safe to keep iterating even
150 * after deletion of an entry.
158 vc5_job_set_tile_buffer_size(struct vc5_job
*job
)
160 static const uint8_t tile_sizes
[] = {
167 int tile_size_index
= 0;
169 tile_size_index
+= 2;
172 tile_size_index
+= 2;
173 else if (job
->cbufs
[2])
176 int max_bpp
= RENDER_TARGET_MAXIMUM_32BPP
;
177 for (int i
= 0; i
< VC5_MAX_DRAW_BUFFERS
; i
++) {
179 struct vc5_surface
*surf
= vc5_surface(job
->cbufs
[i
]);
180 max_bpp
= MAX2(max_bpp
, surf
->internal_bpp
);
183 job
->internal_bpp
= max_bpp
;
184 STATIC_ASSERT(RENDER_TARGET_MAXIMUM_32BPP
== 0);
185 tile_size_index
+= max_bpp
;
187 assert(tile_size_index
< ARRAY_SIZE(tile_sizes
));
188 job
->tile_width
= tile_sizes
[tile_size_index
* 2 + 0];
189 job
->tile_height
= tile_sizes
[tile_size_index
* 2 + 1];
193 * Returns a vc5_job struture for tracking V3D rendering to a particular FBO.
195 * If we've already started rendering to this FBO, then return old same job,
196 * otherwise make a new one. If we're beginning rendering to an FBO, make
197 * sure that any previous reads of the FBO (or writes to its color/Z surfaces)
201 vc5_get_job(struct vc5_context
*vc5
,
202 struct pipe_surface
**cbufs
, struct pipe_surface
*zsbuf
)
204 /* Return the existing job for this FBO if we have one */
205 struct vc5_job_key local_key
= {
214 struct hash_entry
*entry
= _mesa_hash_table_search(vc5
->jobs
,
219 /* Creating a new job. Make sure that any previous jobs reading or
220 * writing these buffers are flushed.
222 struct vc5_job
*job
= vc5_job_create(vc5
);
224 for (int i
= 0; i
< VC5_MAX_DRAW_BUFFERS
; i
++) {
226 vc5_flush_jobs_reading_resource(vc5
, cbufs
[i
]->texture
);
227 pipe_surface_reference(&job
->cbufs
[i
], cbufs
[i
]);
229 if (cbufs
[i
]->texture
->nr_samples
> 1)
234 vc5_flush_jobs_reading_resource(vc5
, zsbuf
->texture
);
235 pipe_surface_reference(&job
->zsbuf
, zsbuf
);
236 if (zsbuf
->texture
->nr_samples
> 1)
240 vc5_job_set_tile_buffer_size(job
);
242 for (int i
= 0; i
< VC5_MAX_DRAW_BUFFERS
; i
++) {
244 _mesa_hash_table_insert(vc5
->write_jobs
,
245 cbufs
[i
]->texture
, job
);
248 _mesa_hash_table_insert(vc5
->write_jobs
, zsbuf
->texture
, job
);
250 memcpy(&job
->key
, &local_key
, sizeof(local_key
));
251 _mesa_hash_table_insert(vc5
->jobs
, &job
->key
, job
);
257 vc5_get_job_for_fbo(struct vc5_context
*vc5
)
262 struct pipe_surface
**cbufs
= vc5
->framebuffer
.cbufs
;
263 struct pipe_surface
*zsbuf
= vc5
->framebuffer
.zsbuf
;
264 struct vc5_job
*job
= vc5_get_job(vc5
, cbufs
, zsbuf
);
266 /* The dirty flags are tracking what's been updated while vc5->job has
267 * been bound, so set them all to ~0 when switching between jobs. We
268 * also need to reset all state at the start of rendering.
272 /* If we're binding to uninitialized buffers, no need to load their
273 * contents before drawing.
275 for (int i
= 0; i
< 4; i
++) {
277 struct vc5_resource
*rsc
= vc5_resource(cbufs
[i
]->texture
);
279 job
->cleared
|= PIPE_CLEAR_COLOR0
<< i
;
284 struct vc5_resource
*rsc
= vc5_resource(zsbuf
->texture
);
286 job
->cleared
|= PIPE_CLEAR_DEPTH
| PIPE_CLEAR_STENCIL
;
289 job
->draw_tiles_x
= DIV_ROUND_UP(vc5
->framebuffer
.width
,
291 job
->draw_tiles_y
= DIV_ROUND_UP(vc5
->framebuffer
.height
,
300 vc5_clif_dump_lookup(void *data
, uint32_t addr
, void **vaddr
)
302 struct vc5_job
*job
= data
;
303 struct set_entry
*entry
;
305 set_foreach(job
->bos
, entry
) {
306 struct vc5_bo
*bo
= (void *)entry
->key
;
308 if (addr
>= bo
->offset
&&
309 addr
< bo
->offset
+ bo
->size
) {
311 *vaddr
= bo
->map
+ addr
- bo
->offset
;
320 vc5_clif_dump(struct vc5_context
*vc5
, struct vc5_job
*job
)
322 if (!(V3D_DEBUG
& V3D_DEBUG_CL
))
325 struct clif_dump
*clif
= clif_dump_init(&vc5
->screen
->devinfo
,
326 stderr
, vc5_clif_dump_lookup
,
329 fprintf(stderr
, "BCL: 0x%08x..0x%08x\n",
330 job
->submit
.bcl_start
, job
->submit
.bcl_end
);
332 clif_dump_add_cl(clif
, job
->submit
.bcl_start
, job
->submit
.bcl_end
);
334 fprintf(stderr
, "RCL: 0x%08x..0x%08x\n",
335 job
->submit
.rcl_start
, job
->submit
.rcl_end
);
336 clif_dump_add_cl(clif
, job
->submit
.rcl_start
, job
->submit
.rcl_end
);
340 * Submits the job to the kernel and then reinitializes it.
343 vc5_job_submit(struct vc5_context
*vc5
, struct vc5_job
*job
)
345 if (!job
->needs_flush
)
348 /* The RCL setup would choke if the draw bounds cause no drawing, so
349 * just drop the drawing if that's the case.
351 if (job
->draw_max_x
<= job
->draw_min_x
||
352 job
->draw_max_y
<= job
->draw_min_y
) {
358 if (cl_offset(&job
->bcl
) > 0) {
359 vc5_cl_ensure_space_with_branch(&job
->bcl
, 2);
361 /* Increment the semaphore indicating that binning is done and
362 * unblocking the render thread. Note that this doesn't act
363 * until the FLUSH completes.
365 cl_emit(&job
->bcl
, INCREMENT_SEMAPHORE
, incr
);
367 /* The FLUSH caps all of our bin lists with a
370 cl_emit(&job
->bcl
, FLUSH
, flush
);
373 job
->submit
.bcl_end
= job
->bcl
.bo
->offset
+ cl_offset(&job
->bcl
);
374 job
->submit
.rcl_end
= job
->rcl
.bo
->offset
+ cl_offset(&job
->rcl
);
376 vc5_clif_dump(vc5
, job
);
378 if (!(V3D_DEBUG
& V3D_DEBUG_NORAST
)) {
381 #ifndef USE_VC5_SIMULATOR
382 ret
= drmIoctl(vc5
->fd
, DRM_IOCTL_VC5_SUBMIT_CL
, &job
->submit
);
384 ret
= vc5_simulator_flush(vc5
, &job
->submit
, job
);
386 static bool warned
= false;
387 if (ret
&& !warned
) {
388 fprintf(stderr
, "Draw call returned %s. "
389 "Expect corruption.\n", strerror(errno
));
394 if (vc5
->last_emit_seqno
- vc5
->screen
->finished_seqno
> 5) {
395 if (!vc5_wait_seqno(vc5
->screen
,
396 vc5
->last_emit_seqno
- 5,
397 PIPE_TIMEOUT_INFINITE
,
399 fprintf(stderr
, "Job throttling failed\n");
404 vc5_job_free(vc5
, job
);
408 vc5_job_compare(const void *a
, const void *b
)
410 return memcmp(a
, b
, sizeof(struct vc5_job_key
)) == 0;
414 vc5_job_hash(const void *key
)
416 return _mesa_hash_data(key
, sizeof(struct vc5_job_key
));
420 vc5_job_init(struct vc5_context
*vc5
)
422 vc5
->jobs
= _mesa_hash_table_create(vc5
,
425 vc5
->write_jobs
= _mesa_hash_table_create(vc5
,
427 _mesa_key_pointer_equal
);