Path: blob/21.2-virgl/src/gallium/drivers/crocus/crocus_batch.h
4570 views
/*1* Copyright © 2017 Intel Corporation2*3* Permission is hereby granted, free of charge, to any person obtaining a4* copy of this software and associated documentation files (the "Software"),5* to deal in the Software without restriction, including without limitation6* the rights to use, copy, modify, merge, publish, distribute, sublicense,7* and/or sell copies of the Software, and to permit persons to whom the8* Software is furnished to do so, subject to the following conditions:9*10* The above copyright notice and this permission notice (including the next11* paragraph) shall be included in all copies or substantial portions of the12* Software.13*14* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR15* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,16* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL17* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER18* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING19* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS20* IN THE SOFTWARE.21*/2223#ifndef CROCUS_BATCH_DOT_H24#define CROCUS_BATCH_DOT_H2526#include <stdbool.h>27#include <stdint.h>28#include <string.h>2930#include "util/u_dynarray.h"3132#include "common/intel_decoder.h"33#include "drm-uapi/i915_drm.h"3435#include "crocus_fence.h"36#include "crocus_fine_fence.h"3738#include "crocus_bufmgr.h"39/* The kernel assumes batchbuffers are smaller than 256kB. */40#define MAX_BATCH_SIZE (256 * 1024)4142/* 3DSTATE_BINDING_TABLE_POINTERS has a U16 offset from Surface State Base43* Address, which means that we can't put binding tables beyond 64kB. This44* effectively limits the maximum statebuffer size to 64kB.45*/46#define MAX_STATE_SIZE (64 * 1024)4748/* Our target batch size - flush approximately at this point. */49#define BATCH_SZ (20 * 1024)50#define STATE_SZ (16 * 1024)5152enum crocus_batch_name {53CROCUS_BATCH_RENDER,54CROCUS_BATCH_COMPUTE,55};5657#define CROCUS_BATCH_COUNT 25859struct crocus_address {60struct crocus_bo *bo;61int32_t offset;62uint32_t reloc_flags;63};6465struct crocus_reloc_list {66struct drm_i915_gem_relocation_entry *relocs;67int reloc_count;68int reloc_array_size;69};7071struct crocus_growing_bo {72struct crocus_bo *bo;73void *map;74void *map_next;75struct crocus_bo *partial_bo;76void *partial_bo_map;77unsigned partial_bytes;78struct crocus_reloc_list relocs;79unsigned used;80};8182struct crocus_batch {83struct crocus_context *ice;84struct crocus_screen *screen;85struct pipe_debug_callback *dbg;86struct pipe_device_reset_callback *reset;8788/** What batch is this? (e.g. CROCUS_BATCH_RENDER/COMPUTE) */89enum crocus_batch_name name;9091/** buffers: command, state */92struct crocus_growing_bo command, state;9394/** Size of the primary batch if we've moved on to a secondary. */95unsigned primary_batch_size;9697bool state_base_address_emitted;98uint8_t pipe_controls_since_last_cs_stall;99100uint32_t hw_ctx_id;101102uint32_t valid_reloc_flags;103104bool use_shadow_copy;105bool no_wrap;106107/** The validation list */108struct drm_i915_gem_exec_object2 *validation_list;109struct crocus_bo **exec_bos;110int exec_count;111int exec_array_size;112113/** Whether INTEL_BLACKHOLE_RENDER is enabled in the batch (aka first114* instruction is a MI_BATCH_BUFFER_END).115*/116bool noop_enabled;117118/**119* A list of crocus_syncobjs associated with this batch.120*121* The first list entry will always be a signalling sync-point, indicating122* that this batch has completed. The others are likely to be sync-points123* to wait on before executing the batch.124*/125struct util_dynarray syncobjs;126127/** A list of drm_i915_exec_fences to have execbuf signal or wait on */128struct util_dynarray exec_fences;129130/** The amount of aperture space (in bytes) used by all exec_bos */131int aperture_space;132133struct {134/** Uploader to use for sequence numbers */135struct u_upload_mgr *uploader;136137/** GPU buffer and CPU map where our seqno's will be written. */138struct crocus_state_ref ref;139uint32_t *map;140141/** The sequence number to write the next time we add a fence. */142uint32_t next;143} fine_fences;144145/** A seqno (and syncobj) for the last batch that was submitted. */146struct crocus_fine_fence *last_fence;147148/** List of other batches which we might need to flush to use a BO */149struct crocus_batch *other_batches[CROCUS_BATCH_COUNT - 1];150151struct {152/**153* Set of struct brw_bo * that have been rendered to within this154* batchbuffer and would need flushing before being used from another155* cache domain that isn't coherent with it (i.e. the sampler).156*/157struct hash_table *render;158159/**160* Set of struct brw_bo * that have been used as a depth buffer within161* this batchbuffer and would need flushing before being used from162* another cache domain that isn't coherent with it (i.e. the sampler).163*/164struct set *depth;165} cache;166167struct intel_batch_decode_ctx decoder;168struct hash_table_u64 *state_sizes;169170/** Have we emitted any draw calls to this batch? */171bool contains_draw;172173/** Batch contains fence signal operation. */174bool contains_fence_signal;175};176177static inline bool178batch_has_fine_fence(struct crocus_batch *batch)179{180return !!batch->fine_fences.uploader;181}182183#define BATCH_HAS_FINE_FENCES(batch) (!!(batch)->fine_fences.uploader)184void crocus_init_batch(struct crocus_context *ctx,185enum crocus_batch_name name,186int priority);187void crocus_batch_free(struct crocus_batch *batch);188void crocus_batch_maybe_flush(struct crocus_batch *batch, unsigned estimate);189190void _crocus_batch_flush(struct crocus_batch *batch, const char *file, int line);191#define crocus_batch_flush(batch) _crocus_batch_flush((batch), __FILE__, __LINE__)192193bool crocus_batch_references(struct crocus_batch *batch, struct crocus_bo *bo);194195bool crocus_batch_prepare_noop(struct crocus_batch *batch, bool noop_enable);196197#define RELOC_WRITE EXEC_OBJECT_WRITE198#define RELOC_NEEDS_GGTT EXEC_OBJECT_NEEDS_GTT199/* Inverted meaning, but using the same bit...emit_reloc will flip it. */200#define RELOC_32BIT EXEC_OBJECT_SUPPORTS_48B_ADDRESS201202void crocus_use_pinned_bo(struct crocus_batch *batch, struct crocus_bo *bo,203bool writable);204uint64_t crocus_command_reloc(struct crocus_batch *batch, uint32_t batch_offset,205struct crocus_bo *target, uint32_t target_offset,206unsigned int reloc_flags);207uint64_t crocus_state_reloc(struct crocus_batch *batch, uint32_t batch_offset,208struct crocus_bo *target, uint32_t target_offset,209unsigned int reloc_flags);210211enum pipe_reset_status crocus_batch_check_for_reset(struct crocus_batch *batch);212213void crocus_grow_buffer(struct crocus_batch *batch, bool grow_state,214unsigned used, unsigned new_size);215216static inline unsigned217crocus_batch_bytes_used(struct crocus_batch *batch)218{219return batch->command.map_next - batch->command.map;220}221222/**223* Ensure the current command buffer has \param size bytes of space224* remaining. If not, this creates a secondary batch buffer and emits225* a jump from the primary batch to the start of the secondary.226*227* Most callers want crocus_get_command_space() instead.228*/229static inline void230crocus_require_command_space(struct crocus_batch *batch, unsigned size)231{232const unsigned required_bytes = crocus_batch_bytes_used(batch) + size;233unsigned used = crocus_batch_bytes_used(batch);234if (required_bytes >= BATCH_SZ && !batch->no_wrap) {235crocus_batch_flush(batch);236} else if (used + size >= batch->command.bo->size) {237const unsigned new_size =238MIN2(batch->command.bo->size + batch->command.bo->size / 2,239MAX_BATCH_SIZE);240241crocus_grow_buffer(batch, false, used, new_size);242batch->command.map_next = (void *)batch->command.map + used;243assert(crocus_batch_bytes_used(batch) + size < batch->command.bo->size);244}245}246247/**248* Allocate space in the current command buffer, and return a pointer249* to the mapped area so the caller can write commands there.250*251* This should be called whenever emitting commands.252*/253static inline void *254crocus_get_command_space(struct crocus_batch *batch, unsigned bytes)255{256crocus_require_command_space(batch, bytes);257void *map = batch->command.map_next;258batch->command.map_next += bytes;259return map;260}261262/**263* Helper to emit GPU commands - allocates space, copies them there.264*/265static inline void266crocus_batch_emit(struct crocus_batch *batch, const void *data, unsigned size)267{268void *map = crocus_get_command_space(batch, size);269memcpy(map, data, size);270}271272/**273* Get a pointer to the batch's signalling syncobj. Does not refcount.274*/275static inline struct crocus_syncobj *276crocus_batch_get_signal_syncobj(struct crocus_batch *batch)277{278/* The signalling syncobj is the first one in the list. */279struct crocus_syncobj *syncobj =280((struct crocus_syncobj **)util_dynarray_begin(&batch->syncobjs))[0];281return syncobj;282}283284/**285* Take a reference to the batch's signalling syncobj.286*287* Callers can use this to wait for the the current batch under construction288* to complete (after flushing it).289*/290static inline void291crocus_batch_reference_signal_syncobj(struct crocus_batch *batch,292struct crocus_syncobj **out_syncobj)293{294struct crocus_syncobj *syncobj = crocus_batch_get_signal_syncobj(batch);295crocus_syncobj_reference(batch->screen, out_syncobj, syncobj);296}297298/**299* Record the size of a piece of state for use in INTEL_DEBUG=bat printing.300*/301static inline void302crocus_record_state_size(struct hash_table_u64 *ht, uint32_t offset_from_base,303uint32_t size)304{305if (ht) {306_mesa_hash_table_u64_insert(ht, offset_from_base,307(void *)(uintptr_t)size);308}309}310311static inline bool312crocus_ptr_in_state_buffer(struct crocus_batch *batch, void *p)313{314return (char *)p >= (char *)batch->state.map &&315(char *)p < (char *)batch->state.map + batch->state.bo->size;316}317318static inline void319crocus_require_statebuffer_space(struct crocus_batch *batch, int size)320{321if (batch->state.used + size >= STATE_SZ)322crocus_batch_flush(batch);323}324#endif325326327