1bf215546Sopenharmony_ci/*
2bf215546Sopenharmony_ci * Copyright © 2017 Intel Corporation
3bf215546Sopenharmony_ci *
4bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a
5bf215546Sopenharmony_ci * copy of this software and associated documentation files (the "Software"),
6bf215546Sopenharmony_ci * to deal in the Software without restriction, including without limitation
7bf215546Sopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8bf215546Sopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the
9bf215546Sopenharmony_ci * Software is furnished to do so, subject to the following conditions:
10bf215546Sopenharmony_ci *
11bf215546Sopenharmony_ci * The above copyright notice and this permission notice (including the next
12bf215546Sopenharmony_ci * paragraph) shall be included in all copies or substantial portions of the
13bf215546Sopenharmony_ci * Software.
14bf215546Sopenharmony_ci *
15bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16bf215546Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17bf215546Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18bf215546Sopenharmony_ci * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19bf215546Sopenharmony_ci * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20bf215546Sopenharmony_ci * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21bf215546Sopenharmony_ci * IN THE SOFTWARE.
22bf215546Sopenharmony_ci */
23bf215546Sopenharmony_ci
24bf215546Sopenharmony_ci#ifndef CROCUS_BATCH_DOT_H
25bf215546Sopenharmony_ci#define CROCUS_BATCH_DOT_H
26bf215546Sopenharmony_ci
27bf215546Sopenharmony_ci#include <stdbool.h>
28bf215546Sopenharmony_ci#include <stdint.h>
29bf215546Sopenharmony_ci#include <string.h>
30bf215546Sopenharmony_ci
31bf215546Sopenharmony_ci#include "util/u_dynarray.h"
32bf215546Sopenharmony_ci
33bf215546Sopenharmony_ci#include "common/intel_decoder.h"
34bf215546Sopenharmony_ci#include "drm-uapi/i915_drm.h"
35bf215546Sopenharmony_ci
36bf215546Sopenharmony_ci#include "crocus_fence.h"
37bf215546Sopenharmony_ci#include "crocus_fine_fence.h"
38bf215546Sopenharmony_ci
39bf215546Sopenharmony_ci#include "crocus_bufmgr.h"
40bf215546Sopenharmony_ci/* The kernel assumes batchbuffers are smaller than 256kB. */
41bf215546Sopenharmony_ci#define MAX_BATCH_SIZE (256 * 1024)
42bf215546Sopenharmony_ci
43bf215546Sopenharmony_ci/* 3DSTATE_BINDING_TABLE_POINTERS has a U16 offset from Surface State Base
44bf215546Sopenharmony_ci * Address, which means that we can't put binding tables beyond 64kB.  This
45bf215546Sopenharmony_ci * effectively limits the maximum statebuffer size to 64kB.
46bf215546Sopenharmony_ci */
47bf215546Sopenharmony_ci#define MAX_STATE_SIZE (64 * 1024)
48bf215546Sopenharmony_ci
49bf215546Sopenharmony_ci/* Our target batch size - flush approximately at this point. */
50bf215546Sopenharmony_ci#define BATCH_SZ (20 * 1024)
51bf215546Sopenharmony_ci#define STATE_SZ (16 * 1024)
52bf215546Sopenharmony_ci
53bf215546Sopenharmony_cienum crocus_batch_name {
54bf215546Sopenharmony_ci   CROCUS_BATCH_RENDER,
55bf215546Sopenharmony_ci   CROCUS_BATCH_COMPUTE,
56bf215546Sopenharmony_ci};
57bf215546Sopenharmony_ci
58bf215546Sopenharmony_ci#define CROCUS_BATCH_COUNT 2
59bf215546Sopenharmony_ci
60bf215546Sopenharmony_cistruct crocus_address {
61bf215546Sopenharmony_ci   struct crocus_bo *bo;
62bf215546Sopenharmony_ci   int32_t offset;
63bf215546Sopenharmony_ci   uint32_t reloc_flags;
64bf215546Sopenharmony_ci};
65bf215546Sopenharmony_ci
66bf215546Sopenharmony_cistruct crocus_reloc_list {
67bf215546Sopenharmony_ci   struct drm_i915_gem_relocation_entry *relocs;
68bf215546Sopenharmony_ci   int reloc_count;
69bf215546Sopenharmony_ci   int reloc_array_size;
70bf215546Sopenharmony_ci};
71bf215546Sopenharmony_ci
72bf215546Sopenharmony_cistruct crocus_growing_bo {
73bf215546Sopenharmony_ci   struct crocus_bo *bo;
74bf215546Sopenharmony_ci   void *map;
75bf215546Sopenharmony_ci   void *map_next;
76bf215546Sopenharmony_ci   struct crocus_bo *partial_bo;
77bf215546Sopenharmony_ci   void *partial_bo_map;
78bf215546Sopenharmony_ci   unsigned partial_bytes;
79bf215546Sopenharmony_ci   struct crocus_reloc_list relocs;
80bf215546Sopenharmony_ci   unsigned used;
81bf215546Sopenharmony_ci};
82bf215546Sopenharmony_ci
83bf215546Sopenharmony_cistruct crocus_batch {
84bf215546Sopenharmony_ci   struct crocus_context *ice;
85bf215546Sopenharmony_ci   struct crocus_screen *screen;
86bf215546Sopenharmony_ci   struct util_debug_callback *dbg;
87bf215546Sopenharmony_ci   struct pipe_device_reset_callback *reset;
88bf215546Sopenharmony_ci
89bf215546Sopenharmony_ci   /** What batch is this? (e.g. CROCUS_BATCH_RENDER/COMPUTE) */
90bf215546Sopenharmony_ci   enum crocus_batch_name name;
91bf215546Sopenharmony_ci
92bf215546Sopenharmony_ci   /** buffers: command, state */
93bf215546Sopenharmony_ci   struct crocus_growing_bo command, state;
94bf215546Sopenharmony_ci
95bf215546Sopenharmony_ci   /** Size of the primary batch if we've moved on to a secondary. */
96bf215546Sopenharmony_ci   unsigned primary_batch_size;
97bf215546Sopenharmony_ci
98bf215546Sopenharmony_ci   bool state_base_address_emitted;
99bf215546Sopenharmony_ci   uint8_t pipe_controls_since_last_cs_stall;
100bf215546Sopenharmony_ci
101bf215546Sopenharmony_ci   uint32_t hw_ctx_id;
102bf215546Sopenharmony_ci
103bf215546Sopenharmony_ci   uint32_t valid_reloc_flags;
104bf215546Sopenharmony_ci
105bf215546Sopenharmony_ci   bool use_shadow_copy;
106bf215546Sopenharmony_ci   bool no_wrap;
107bf215546Sopenharmony_ci
108bf215546Sopenharmony_ci   /** The validation list */
109bf215546Sopenharmony_ci   struct drm_i915_gem_exec_object2 *validation_list;
110bf215546Sopenharmony_ci   struct crocus_bo **exec_bos;
111bf215546Sopenharmony_ci   int exec_count;
112bf215546Sopenharmony_ci   int exec_array_size;
113bf215546Sopenharmony_ci
114bf215546Sopenharmony_ci   /** Whether INTEL_BLACKHOLE_RENDER is enabled in the batch (aka first
115bf215546Sopenharmony_ci    * instruction is a MI_BATCH_BUFFER_END).
116bf215546Sopenharmony_ci    */
117bf215546Sopenharmony_ci   bool noop_enabled;
118bf215546Sopenharmony_ci
119bf215546Sopenharmony_ci   /**
120bf215546Sopenharmony_ci    * A list of crocus_syncobjs associated with this batch.
121bf215546Sopenharmony_ci    *
122bf215546Sopenharmony_ci    * The first list entry will always be a signalling sync-point, indicating
123bf215546Sopenharmony_ci    * that this batch has completed.  The others are likely to be sync-points
124bf215546Sopenharmony_ci    * to wait on before executing the batch.
125bf215546Sopenharmony_ci    */
126bf215546Sopenharmony_ci   struct util_dynarray syncobjs;
127bf215546Sopenharmony_ci
128bf215546Sopenharmony_ci   /** A list of drm_i915_exec_fences to have execbuf signal or wait on */
129bf215546Sopenharmony_ci   struct util_dynarray exec_fences;
130bf215546Sopenharmony_ci
131bf215546Sopenharmony_ci   /** The amount of aperture space (in bytes) used by all exec_bos */
132bf215546Sopenharmony_ci   int aperture_space;
133bf215546Sopenharmony_ci
134bf215546Sopenharmony_ci   struct {
135bf215546Sopenharmony_ci      /** Uploader to use for sequence numbers */
136bf215546Sopenharmony_ci      struct u_upload_mgr *uploader;
137bf215546Sopenharmony_ci
138bf215546Sopenharmony_ci      /** GPU buffer and CPU map where our seqno's will be written. */
139bf215546Sopenharmony_ci      struct crocus_state_ref ref;
140bf215546Sopenharmony_ci      uint32_t *map;
141bf215546Sopenharmony_ci
142bf215546Sopenharmony_ci      /** The sequence number to write the next time we add a fence. */
143bf215546Sopenharmony_ci      uint32_t next;
144bf215546Sopenharmony_ci   } fine_fences;
145bf215546Sopenharmony_ci
146bf215546Sopenharmony_ci   /** A seqno (and syncobj) for the last batch that was submitted. */
147bf215546Sopenharmony_ci   struct crocus_fine_fence *last_fence;
148bf215546Sopenharmony_ci
149bf215546Sopenharmony_ci   /** List of other batches which we might need to flush to use a BO */
150bf215546Sopenharmony_ci   struct crocus_batch *other_batches[CROCUS_BATCH_COUNT - 1];
151bf215546Sopenharmony_ci
152bf215546Sopenharmony_ci   struct {
153bf215546Sopenharmony_ci      /**
154bf215546Sopenharmony_ci       * Set of struct brw_bo * that have been rendered to within this
155bf215546Sopenharmony_ci       * batchbuffer and would need flushing before being used from another
156bf215546Sopenharmony_ci       * cache domain that isn't coherent with it (i.e. the sampler).
157bf215546Sopenharmony_ci       */
158bf215546Sopenharmony_ci      struct hash_table *render;
159bf215546Sopenharmony_ci
160bf215546Sopenharmony_ci      /**
161bf215546Sopenharmony_ci       * Set of struct brw_bo * that have been used as a depth buffer within
162bf215546Sopenharmony_ci       * this batchbuffer and would need flushing before being used from
163bf215546Sopenharmony_ci       * another cache domain that isn't coherent with it (i.e. the sampler).
164bf215546Sopenharmony_ci       */
165bf215546Sopenharmony_ci      struct set *depth;
166bf215546Sopenharmony_ci   } cache;
167bf215546Sopenharmony_ci
168bf215546Sopenharmony_ci   struct intel_batch_decode_ctx decoder;
169bf215546Sopenharmony_ci   struct hash_table_u64 *state_sizes;
170bf215546Sopenharmony_ci
171bf215546Sopenharmony_ci   /** Have we emitted any draw calls to this batch? */
172bf215546Sopenharmony_ci   bool contains_draw;
173bf215546Sopenharmony_ci
174bf215546Sopenharmony_ci   /** Batch contains fence signal operation. */
175bf215546Sopenharmony_ci   bool contains_fence_signal;
176bf215546Sopenharmony_ci};
177bf215546Sopenharmony_ci
178bf215546Sopenharmony_cistatic inline bool
179bf215546Sopenharmony_cibatch_has_fine_fence(struct crocus_batch *batch)
180bf215546Sopenharmony_ci{
181bf215546Sopenharmony_ci   return !!batch->fine_fences.uploader;
182bf215546Sopenharmony_ci}
183bf215546Sopenharmony_ci
184bf215546Sopenharmony_ci#define BATCH_HAS_FINE_FENCES(batch) (!!(batch)->fine_fences.uploader)
185bf215546Sopenharmony_civoid crocus_init_batch(struct crocus_context *ctx,
186bf215546Sopenharmony_ci                       enum crocus_batch_name name,
187bf215546Sopenharmony_ci                       int priority);
188bf215546Sopenharmony_civoid crocus_batch_free(struct crocus_batch *batch);
189bf215546Sopenharmony_civoid crocus_batch_maybe_flush(struct crocus_batch *batch, unsigned estimate);
190bf215546Sopenharmony_ci
191bf215546Sopenharmony_civoid _crocus_batch_flush(struct crocus_batch *batch, const char *file, int line);
192bf215546Sopenharmony_ci#define crocus_batch_flush(batch) _crocus_batch_flush((batch), __FILE__, __LINE__)
193bf215546Sopenharmony_ci
194bf215546Sopenharmony_cibool crocus_batch_references(struct crocus_batch *batch, struct crocus_bo *bo);
195bf215546Sopenharmony_ci
196bf215546Sopenharmony_cibool crocus_batch_prepare_noop(struct crocus_batch *batch, bool noop_enable);
197bf215546Sopenharmony_ci
198bf215546Sopenharmony_ci#define RELOC_WRITE EXEC_OBJECT_WRITE
199bf215546Sopenharmony_ci#define RELOC_NEEDS_GGTT EXEC_OBJECT_NEEDS_GTT
200bf215546Sopenharmony_ci/* Inverted meaning, but using the same bit...emit_reloc will flip it. */
201bf215546Sopenharmony_ci#define RELOC_32BIT EXEC_OBJECT_SUPPORTS_48B_ADDRESS
202bf215546Sopenharmony_ci
203bf215546Sopenharmony_civoid crocus_use_pinned_bo(struct crocus_batch *batch, struct crocus_bo *bo,
204bf215546Sopenharmony_ci                          bool writable);
205bf215546Sopenharmony_ciuint64_t crocus_command_reloc(struct crocus_batch *batch, uint32_t batch_offset,
206bf215546Sopenharmony_ci                              struct crocus_bo *target, uint32_t target_offset,
207bf215546Sopenharmony_ci                              unsigned int reloc_flags);
208bf215546Sopenharmony_ciuint64_t crocus_state_reloc(struct crocus_batch *batch, uint32_t batch_offset,
209bf215546Sopenharmony_ci                            struct crocus_bo *target, uint32_t target_offset,
210bf215546Sopenharmony_ci                            unsigned int reloc_flags);
211bf215546Sopenharmony_ci
212bf215546Sopenharmony_cienum pipe_reset_status crocus_batch_check_for_reset(struct crocus_batch *batch);
213bf215546Sopenharmony_ci
214bf215546Sopenharmony_civoid crocus_grow_buffer(struct crocus_batch *batch, bool grow_state,
215bf215546Sopenharmony_ci                        unsigned used, unsigned new_size);
216bf215546Sopenharmony_ci
217bf215546Sopenharmony_cistatic inline unsigned
218bf215546Sopenharmony_cicrocus_batch_bytes_used(struct crocus_batch *batch)
219bf215546Sopenharmony_ci{
220bf215546Sopenharmony_ci   return batch->command.map_next - batch->command.map;
221bf215546Sopenharmony_ci}
222bf215546Sopenharmony_ci
223bf215546Sopenharmony_ci/**
224bf215546Sopenharmony_ci * Ensure the current command buffer has \param size bytes of space
225bf215546Sopenharmony_ci * remaining.  If not, this creates a secondary batch buffer and emits
226bf215546Sopenharmony_ci * a jump from the primary batch to the start of the secondary.
227bf215546Sopenharmony_ci *
228bf215546Sopenharmony_ci * Most callers want crocus_get_command_space() instead.
229bf215546Sopenharmony_ci */
230bf215546Sopenharmony_cistatic inline void
231bf215546Sopenharmony_cicrocus_require_command_space(struct crocus_batch *batch, unsigned size)
232bf215546Sopenharmony_ci{
233bf215546Sopenharmony_ci   const unsigned required_bytes = crocus_batch_bytes_used(batch) + size;
234bf215546Sopenharmony_ci   unsigned used = crocus_batch_bytes_used(batch);
235bf215546Sopenharmony_ci   if (required_bytes >= BATCH_SZ && !batch->no_wrap) {
236bf215546Sopenharmony_ci      crocus_batch_flush(batch);
237bf215546Sopenharmony_ci   } else if (used + size >= batch->command.bo->size) {
238bf215546Sopenharmony_ci      const unsigned new_size =
239bf215546Sopenharmony_ci         MIN2(batch->command.bo->size + batch->command.bo->size / 2,
240bf215546Sopenharmony_ci              MAX_BATCH_SIZE);
241bf215546Sopenharmony_ci
242bf215546Sopenharmony_ci      crocus_grow_buffer(batch, false, used, new_size);
243bf215546Sopenharmony_ci      batch->command.map_next = (void *)batch->command.map + used;
244bf215546Sopenharmony_ci      assert(crocus_batch_bytes_used(batch) + size < batch->command.bo->size);
245bf215546Sopenharmony_ci   }
246bf215546Sopenharmony_ci}
247bf215546Sopenharmony_ci
248bf215546Sopenharmony_ci/**
249bf215546Sopenharmony_ci * Allocate space in the current command buffer, and return a pointer
250bf215546Sopenharmony_ci * to the mapped area so the caller can write commands there.
251bf215546Sopenharmony_ci *
252bf215546Sopenharmony_ci * This should be called whenever emitting commands.
253bf215546Sopenharmony_ci */
254bf215546Sopenharmony_cistatic inline void *
255bf215546Sopenharmony_cicrocus_get_command_space(struct crocus_batch *batch, unsigned bytes)
256bf215546Sopenharmony_ci{
257bf215546Sopenharmony_ci   crocus_require_command_space(batch, bytes);
258bf215546Sopenharmony_ci   void *map = batch->command.map_next;
259bf215546Sopenharmony_ci   batch->command.map_next += bytes;
260bf215546Sopenharmony_ci   return map;
261bf215546Sopenharmony_ci}
262bf215546Sopenharmony_ci
263bf215546Sopenharmony_ci/**
264bf215546Sopenharmony_ci * Helper to emit GPU commands - allocates space, copies them there.
265bf215546Sopenharmony_ci */
266bf215546Sopenharmony_cistatic inline void
267bf215546Sopenharmony_cicrocus_batch_emit(struct crocus_batch *batch, const void *data, unsigned size)
268bf215546Sopenharmony_ci{
269bf215546Sopenharmony_ci   void *map = crocus_get_command_space(batch, size);
270bf215546Sopenharmony_ci   memcpy(map, data, size);
271bf215546Sopenharmony_ci}
272bf215546Sopenharmony_ci
273bf215546Sopenharmony_ci/**
274bf215546Sopenharmony_ci * Get a pointer to the batch's signalling syncobj.  Does not refcount.
275bf215546Sopenharmony_ci */
276bf215546Sopenharmony_cistatic inline struct crocus_syncobj *
277bf215546Sopenharmony_cicrocus_batch_get_signal_syncobj(struct crocus_batch *batch)
278bf215546Sopenharmony_ci{
279bf215546Sopenharmony_ci   /* The signalling syncobj is the first one in the list. */
280bf215546Sopenharmony_ci   struct crocus_syncobj *syncobj =
281bf215546Sopenharmony_ci      ((struct crocus_syncobj **)util_dynarray_begin(&batch->syncobjs))[0];
282bf215546Sopenharmony_ci   return syncobj;
283bf215546Sopenharmony_ci}
284bf215546Sopenharmony_ci
285bf215546Sopenharmony_ci/**
286bf215546Sopenharmony_ci * Take a reference to the batch's signalling syncobj.
287bf215546Sopenharmony_ci *
288bf215546Sopenharmony_ci * Callers can use this to wait for the the current batch under construction
289bf215546Sopenharmony_ci * to complete (after flushing it).
290bf215546Sopenharmony_ci */
291bf215546Sopenharmony_cistatic inline void
292bf215546Sopenharmony_cicrocus_batch_reference_signal_syncobj(struct crocus_batch *batch,
293bf215546Sopenharmony_ci                                      struct crocus_syncobj **out_syncobj)
294bf215546Sopenharmony_ci{
295bf215546Sopenharmony_ci   struct crocus_syncobj *syncobj = crocus_batch_get_signal_syncobj(batch);
296bf215546Sopenharmony_ci   crocus_syncobj_reference(batch->screen, out_syncobj, syncobj);
297bf215546Sopenharmony_ci}
298bf215546Sopenharmony_ci
299bf215546Sopenharmony_ci/**
300bf215546Sopenharmony_ci * Record the size of a piece of state for use in INTEL_DEBUG=bat printing.
301bf215546Sopenharmony_ci */
302bf215546Sopenharmony_cistatic inline void
303bf215546Sopenharmony_cicrocus_record_state_size(struct hash_table_u64 *ht, uint32_t offset_from_base,
304bf215546Sopenharmony_ci                         uint32_t size)
305bf215546Sopenharmony_ci{
306bf215546Sopenharmony_ci   if (ht) {
307bf215546Sopenharmony_ci      _mesa_hash_table_u64_insert(ht, offset_from_base,
308bf215546Sopenharmony_ci                                  (void *)(uintptr_t)size);
309bf215546Sopenharmony_ci   }
310bf215546Sopenharmony_ci}
311bf215546Sopenharmony_ci
312bf215546Sopenharmony_cistatic inline bool
313bf215546Sopenharmony_cicrocus_ptr_in_state_buffer(struct crocus_batch *batch, void *p)
314bf215546Sopenharmony_ci{
315bf215546Sopenharmony_ci   return (char *)p >= (char *)batch->state.map &&
316bf215546Sopenharmony_ci          (char *)p < (char *)batch->state.map + batch->state.bo->size;
317bf215546Sopenharmony_ci}
318bf215546Sopenharmony_ci
319bf215546Sopenharmony_cistatic inline void
320bf215546Sopenharmony_cicrocus_require_statebuffer_space(struct crocus_batch *batch, int size)
321bf215546Sopenharmony_ci{
322bf215546Sopenharmony_ci   if (batch->state.used + size >= STATE_SZ)
323bf215546Sopenharmony_ci      crocus_batch_flush(batch);
324bf215546Sopenharmony_ci}
325bf215546Sopenharmony_ci#endif
326