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 "v3d_context.h"
31 /* The OQ/semaphore packets are the same across V3D versions. */
32 #define V3D_VERSION 33
33 #include "broadcom/cle/v3dx_pack.h"
34 #include "broadcom/common/v3d_macros.h"
35 #include "util/hash_table.h"
36 #include "util/ralloc.h"
38 #include "broadcom/clif/clif_dump.h"
41 remove_from_ht(struct hash_table
*ht
, void *key
)
43 struct hash_entry
*entry
= _mesa_hash_table_search(ht
, key
);
44 _mesa_hash_table_remove(ht
, entry
);
48 vc5_job_free(struct vc5_context
*vc5
, struct vc5_job
*job
)
50 struct set_entry
*entry
;
52 set_foreach(job
->bos
, entry
) {
53 struct vc5_bo
*bo
= (struct vc5_bo
*)entry
->key
;
54 vc5_bo_unreference(&bo
);
57 remove_from_ht(vc5
->jobs
, &job
->key
);
59 if (job
->write_prscs
) {
60 struct set_entry
*entry
;
62 set_foreach(job
->write_prscs
, entry
) {
63 const struct pipe_resource
*prsc
= entry
->key
;
65 remove_from_ht(vc5
->write_jobs
, (void *)prsc
);
69 for (int i
= 0; i
< VC5_MAX_DRAW_BUFFERS
; i
++) {
71 remove_from_ht(vc5
->write_jobs
, job
->cbufs
[i
]->texture
);
72 pipe_surface_reference(&job
->cbufs
[i
], NULL
);
76 remove_from_ht(vc5
->write_jobs
, job
->zsbuf
->texture
);
77 pipe_surface_reference(&job
->zsbuf
, NULL
);
83 vc5_destroy_cl(&job
->bcl
);
84 vc5_destroy_cl(&job
->rcl
);
85 vc5_destroy_cl(&job
->indirect
);
86 vc5_bo_unreference(&job
->tile_alloc
);
87 vc5_bo_unreference(&job
->tile_state
);
92 static struct vc5_job
*
93 vc5_job_create(struct vc5_context
*vc5
)
95 struct vc5_job
*job
= rzalloc(vc5
, struct vc5_job
);
99 vc5_init_cl(job
, &job
->bcl
);
100 vc5_init_cl(job
, &job
->rcl
);
101 vc5_init_cl(job
, &job
->indirect
);
103 job
->draw_min_x
= ~0;
104 job
->draw_min_y
= ~0;
108 job
->bos
= _mesa_set_create(job
,
110 _mesa_key_pointer_equal
);
115 vc5_job_add_bo(struct vc5_job
*job
, struct vc5_bo
*bo
)
120 if (_mesa_set_search(job
->bos
, bo
))
123 vc5_bo_reference(bo
);
124 _mesa_set_add(job
->bos
, bo
);
125 job
->referenced_size
+= bo
->size
;
127 uint32_t *bo_handles
= (void *)(uintptr_t)job
->submit
.bo_handles
;
129 if (job
->submit
.bo_handle_count
>= job
->bo_handles_size
) {
130 job
->bo_handles_size
= MAX2(4, job
->bo_handles_size
* 2);
131 bo_handles
= reralloc(job
, bo_handles
,
132 uint32_t, job
->bo_handles_size
);
133 job
->submit
.bo_handles
= (uintptr_t)(void *)bo_handles
;
135 bo_handles
[job
->submit
.bo_handle_count
++] = bo
->handle
;
139 vc5_job_add_write_resource(struct vc5_job
*job
, struct pipe_resource
*prsc
)
141 struct vc5_context
*vc5
= job
->vc5
;
143 if (!job
->write_prscs
) {
144 job
->write_prscs
= _mesa_set_create(job
,
146 _mesa_key_pointer_equal
);
149 _mesa_set_add(job
->write_prscs
, prsc
);
150 _mesa_hash_table_insert(vc5
->write_jobs
, prsc
, job
);
154 vc5_flush_jobs_writing_resource(struct vc5_context
*vc5
,
155 struct pipe_resource
*prsc
)
157 struct hash_entry
*entry
= _mesa_hash_table_search(vc5
->write_jobs
,
160 struct vc5_job
*job
= entry
->data
;
161 vc5_job_submit(vc5
, job
);
166 vc5_flush_jobs_reading_resource(struct vc5_context
*vc5
,
167 struct pipe_resource
*prsc
)
169 struct vc5_resource
*rsc
= vc5_resource(prsc
);
171 vc5_flush_jobs_writing_resource(vc5
, prsc
);
173 struct hash_entry
*entry
;
174 hash_table_foreach(vc5
->jobs
, entry
) {
175 struct vc5_job
*job
= entry
->data
;
177 if (_mesa_set_search(job
->bos
, rsc
->bo
)) {
178 vc5_job_submit(vc5
, job
);
179 /* Reminder: vc5->jobs is safe to keep iterating even
180 * after deletion of an entry.
188 vc5_job_set_tile_buffer_size(struct vc5_job
*job
)
190 static const uint8_t tile_sizes
[] = {
197 int tile_size_index
= 0;
199 tile_size_index
+= 2;
201 if (job
->cbufs
[3] || job
->cbufs
[2])
202 tile_size_index
+= 2;
203 else if (job
->cbufs
[1])
206 int max_bpp
= RENDER_TARGET_MAXIMUM_32BPP
;
207 for (int i
= 0; i
< VC5_MAX_DRAW_BUFFERS
; i
++) {
209 struct vc5_surface
*surf
= vc5_surface(job
->cbufs
[i
]);
210 max_bpp
= MAX2(max_bpp
, surf
->internal_bpp
);
213 job
->internal_bpp
= max_bpp
;
214 STATIC_ASSERT(RENDER_TARGET_MAXIMUM_32BPP
== 0);
215 tile_size_index
+= max_bpp
;
217 assert(tile_size_index
< ARRAY_SIZE(tile_sizes
));
218 job
->tile_width
= tile_sizes
[tile_size_index
* 2 + 0];
219 job
->tile_height
= tile_sizes
[tile_size_index
* 2 + 1];
223 * Returns a vc5_job struture for tracking V3D rendering to a particular FBO.
225 * If we've already started rendering to this FBO, then return old same job,
226 * otherwise make a new one. If we're beginning rendering to an FBO, make
227 * sure that any previous reads of the FBO (or writes to its color/Z surfaces)
231 vc5_get_job(struct vc5_context
*vc5
,
232 struct pipe_surface
**cbufs
, struct pipe_surface
*zsbuf
)
234 /* Return the existing job for this FBO if we have one */
235 struct vc5_job_key local_key
= {
244 struct hash_entry
*entry
= _mesa_hash_table_search(vc5
->jobs
,
249 /* Creating a new job. Make sure that any previous jobs reading or
250 * writing these buffers are flushed.
252 struct vc5_job
*job
= vc5_job_create(vc5
);
254 for (int i
= 0; i
< VC5_MAX_DRAW_BUFFERS
; i
++) {
256 vc5_flush_jobs_reading_resource(vc5
, cbufs
[i
]->texture
);
257 pipe_surface_reference(&job
->cbufs
[i
], cbufs
[i
]);
259 if (cbufs
[i
]->texture
->nr_samples
> 1)
264 vc5_flush_jobs_reading_resource(vc5
, zsbuf
->texture
);
265 pipe_surface_reference(&job
->zsbuf
, zsbuf
);
266 if (zsbuf
->texture
->nr_samples
> 1)
270 vc5_job_set_tile_buffer_size(job
);
272 for (int i
= 0; i
< VC5_MAX_DRAW_BUFFERS
; i
++) {
274 _mesa_hash_table_insert(vc5
->write_jobs
,
275 cbufs
[i
]->texture
, job
);
278 _mesa_hash_table_insert(vc5
->write_jobs
, zsbuf
->texture
, job
);
280 memcpy(&job
->key
, &local_key
, sizeof(local_key
));
281 _mesa_hash_table_insert(vc5
->jobs
, &job
->key
, job
);
287 vc5_get_job_for_fbo(struct vc5_context
*vc5
)
292 struct pipe_surface
**cbufs
= vc5
->framebuffer
.cbufs
;
293 struct pipe_surface
*zsbuf
= vc5
->framebuffer
.zsbuf
;
294 struct vc5_job
*job
= vc5_get_job(vc5
, cbufs
, zsbuf
);
296 /* The dirty flags are tracking what's been updated while vc5->job has
297 * been bound, so set them all to ~0 when switching between jobs. We
298 * also need to reset all state at the start of rendering.
302 /* If we're binding to uninitialized buffers, no need to load their
303 * contents before drawing.
305 for (int i
= 0; i
< 4; i
++) {
307 struct vc5_resource
*rsc
= vc5_resource(cbufs
[i
]->texture
);
309 job
->cleared
|= PIPE_CLEAR_COLOR0
<< i
;
314 struct vc5_resource
*rsc
= vc5_resource(zsbuf
->texture
);
316 job
->cleared
|= PIPE_CLEAR_DEPTH
| PIPE_CLEAR_STENCIL
;
319 job
->draw_tiles_x
= DIV_ROUND_UP(vc5
->framebuffer
.width
,
321 job
->draw_tiles_y
= DIV_ROUND_UP(vc5
->framebuffer
.height
,
330 vc5_clif_dump_lookup(void *data
, uint32_t addr
, void **vaddr
)
332 struct vc5_job
*job
= data
;
333 struct set_entry
*entry
;
335 set_foreach(job
->bos
, entry
) {
336 struct vc5_bo
*bo
= (void *)entry
->key
;
338 if (addr
>= bo
->offset
&&
339 addr
< bo
->offset
+ bo
->size
) {
341 *vaddr
= bo
->map
+ addr
- bo
->offset
;
350 vc5_clif_dump(struct vc5_context
*vc5
, struct vc5_job
*job
)
352 if (!(V3D_DEBUG
& V3D_DEBUG_CL
))
355 struct clif_dump
*clif
= clif_dump_init(&vc5
->screen
->devinfo
,
356 stderr
, vc5_clif_dump_lookup
,
359 fprintf(stderr
, "BCL: 0x%08x..0x%08x\n",
360 job
->submit
.bcl_start
, job
->submit
.bcl_end
);
362 clif_dump_add_cl(clif
, job
->submit
.bcl_start
, job
->submit
.bcl_end
);
364 fprintf(stderr
, "RCL: 0x%08x..0x%08x\n",
365 job
->submit
.rcl_start
, job
->submit
.rcl_end
);
366 clif_dump_add_cl(clif
, job
->submit
.rcl_start
, job
->submit
.rcl_end
);
370 * Submits the job to the kernel and then reinitializes it.
373 vc5_job_submit(struct vc5_context
*vc5
, struct vc5_job
*job
)
375 MAYBE_UNUSED
struct vc5_screen
*screen
= vc5
->screen
;
377 if (!job
->needs_flush
)
380 if (vc5
->screen
->devinfo
.ver
>= 41)
385 if (cl_offset(&job
->bcl
) > 0) {
386 if (screen
->devinfo
.ver
>= 41)
387 v3d41_bcl_epilogue(vc5
, job
);
389 v3d33_bcl_epilogue(vc5
, job
);
392 job
->submit
.out_sync
= vc5
->out_sync
;
393 job
->submit
.bcl_end
= job
->bcl
.bo
->offset
+ cl_offset(&job
->bcl
);
394 job
->submit
.rcl_end
= job
->rcl
.bo
->offset
+ cl_offset(&job
->rcl
);
396 /* On V3D 4.1, the tile alloc/state setup moved to register writes
397 * instead of binner packets.
399 if (screen
->devinfo
.ver
>= 41) {
400 vc5_job_add_bo(job
, job
->tile_alloc
);
401 job
->submit
.qma
= job
->tile_alloc
->offset
;
402 job
->submit
.qms
= job
->tile_alloc
->size
;
404 vc5_job_add_bo(job
, job
->tile_state
);
405 job
->submit
.qts
= job
->tile_state
->offset
;
408 vc5_clif_dump(vc5
, job
);
410 if (!(V3D_DEBUG
& V3D_DEBUG_NORAST
)) {
413 #ifndef USE_V3D_SIMULATOR
414 ret
= drmIoctl(vc5
->fd
, DRM_IOCTL_V3D_SUBMIT_CL
, &job
->submit
);
416 ret
= vc5_simulator_flush(vc5
, &job
->submit
, job
);
418 static bool warned
= false;
419 if (ret
&& !warned
) {
420 fprintf(stderr
, "Draw call returned %s. "
421 "Expect corruption.\n", strerror(errno
));
427 vc5_job_free(vc5
, job
);
431 vc5_job_compare(const void *a
, const void *b
)
433 return memcmp(a
, b
, sizeof(struct vc5_job_key
)) == 0;
437 vc5_job_hash(const void *key
)
439 return _mesa_hash_data(key
, sizeof(struct vc5_job_key
));
443 vc5_job_init(struct vc5_context
*vc5
)
445 vc5
->jobs
= _mesa_hash_table_create(vc5
,
448 vc5
->write_jobs
= _mesa_hash_table_create(vc5
,
450 _mesa_key_pointer_equal
);