1bf215546Sopenharmony_ci/*
2bf215546Sopenharmony_ci * Copyright (C) 2018 Rob Clark <robclark@freedesktop.org>
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 FROM,
20bf215546Sopenharmony_ci * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21bf215546Sopenharmony_ci * SOFTWARE.
22bf215546Sopenharmony_ci *
23bf215546Sopenharmony_ci * Authors:
24bf215546Sopenharmony_ci *    Rob Clark <robclark@freedesktop.org>
25bf215546Sopenharmony_ci */
26bf215546Sopenharmony_ci
27bf215546Sopenharmony_ci#ifndef FREEDRENO_RINGBUFFER_SP_H_
28bf215546Sopenharmony_ci#define FREEDRENO_RINGBUFFER_SP_H_
29bf215546Sopenharmony_ci
30bf215546Sopenharmony_ci#include <assert.h>
31bf215546Sopenharmony_ci#include <inttypes.h>
32bf215546Sopenharmony_ci#include <pthread.h>
33bf215546Sopenharmony_ci
34bf215546Sopenharmony_ci#include "util/hash_table.h"
35bf215546Sopenharmony_ci#include "util/os_file.h"
36bf215546Sopenharmony_ci#include "util/slab.h"
37bf215546Sopenharmony_ci
38bf215546Sopenharmony_ci#include "freedreno_priv.h"
39bf215546Sopenharmony_ci#include "freedreno_ringbuffer.h"
40bf215546Sopenharmony_ci
41bf215546Sopenharmony_ci/* A "softpin" implementation of submit/ringbuffer, which lowers CPU overhead
42bf215546Sopenharmony_ci * by avoiding the additional tracking necessary to build cmds/relocs tables
43bf215546Sopenharmony_ci * (but still builds a bos table)
44bf215546Sopenharmony_ci */
45bf215546Sopenharmony_ci
46bf215546Sopenharmony_citypedef int (*flush_submit_list_fn)(struct list_head *submit_list);
47bf215546Sopenharmony_ci
48bf215546Sopenharmony_cistruct fd_submit_sp {
49bf215546Sopenharmony_ci   struct fd_submit base;
50bf215546Sopenharmony_ci
51bf215546Sopenharmony_ci   DECLARE_ARRAY(struct fd_bo *, bos);
52bf215546Sopenharmony_ci
53bf215546Sopenharmony_ci   /* maps fd_bo to idx in bos table: */
54bf215546Sopenharmony_ci   struct hash_table *bo_table;
55bf215546Sopenharmony_ci
56bf215546Sopenharmony_ci   struct slab_child_pool ring_pool;
57bf215546Sopenharmony_ci
58bf215546Sopenharmony_ci   /* Allow for sub-allocation of stateobj ring buffers (ie. sharing
59bf215546Sopenharmony_ci    * the same underlying bo)..
60bf215546Sopenharmony_ci    *
61bf215546Sopenharmony_ci    * We also rely on previous stateobj having been fully constructed
62bf215546Sopenharmony_ci    * so we can reclaim extra space at it's end.
63bf215546Sopenharmony_ci    */
64bf215546Sopenharmony_ci   struct fd_ringbuffer *suballoc_ring;
65bf215546Sopenharmony_ci
66bf215546Sopenharmony_ci   /* Flush args, potentially attached to the last submit in the list
67bf215546Sopenharmony_ci    * of submits to merge:
68bf215546Sopenharmony_ci    */
69bf215546Sopenharmony_ci   int in_fence_fd;
70bf215546Sopenharmony_ci   struct fd_submit_fence *out_fence;
71bf215546Sopenharmony_ci
72bf215546Sopenharmony_ci   /* State for enqueued submits:
73bf215546Sopenharmony_ci    */
74bf215546Sopenharmony_ci   struct list_head submit_list;   /* includes this submit as last element */
75bf215546Sopenharmony_ci
76bf215546Sopenharmony_ci   /* Used in case out_fence==NULL: */
77bf215546Sopenharmony_ci   struct util_queue_fence fence;
78bf215546Sopenharmony_ci
79bf215546Sopenharmony_ci   /* Used by retire_queue, if used by backend: */
80bf215546Sopenharmony_ci   int out_fence_fd;
81bf215546Sopenharmony_ci   struct util_queue_fence retire_fence;
82bf215546Sopenharmony_ci
83bf215546Sopenharmony_ci   flush_submit_list_fn flush_submit_list;
84bf215546Sopenharmony_ci};
85bf215546Sopenharmony_ciFD_DEFINE_CAST(fd_submit, fd_submit_sp);
86bf215546Sopenharmony_ci
87bf215546Sopenharmony_ci/* for FD_RINGBUFFER_GROWABLE rb's, tracks the 'finalized' cmdstream buffers
88bf215546Sopenharmony_ci * and sizes.  Ie. a finalized buffer can have no more commands appended to
89bf215546Sopenharmony_ci * it.
90bf215546Sopenharmony_ci */
91bf215546Sopenharmony_cistruct fd_cmd_sp {
92bf215546Sopenharmony_ci   struct fd_bo *ring_bo;
93bf215546Sopenharmony_ci   unsigned size;
94bf215546Sopenharmony_ci};
95bf215546Sopenharmony_ci
96bf215546Sopenharmony_cistruct fd_ringbuffer_sp {
97bf215546Sopenharmony_ci   struct fd_ringbuffer base;
98bf215546Sopenharmony_ci
99bf215546Sopenharmony_ci   /* for FD_RINGBUFFER_STREAMING rb's which are sub-allocated */
100bf215546Sopenharmony_ci   unsigned offset;
101bf215546Sopenharmony_ci
102bf215546Sopenharmony_ci   union {
103bf215546Sopenharmony_ci      /* for _FD_RINGBUFFER_OBJECT case, the array of BOs referenced from
104bf215546Sopenharmony_ci       * this one
105bf215546Sopenharmony_ci       */
106bf215546Sopenharmony_ci      struct {
107bf215546Sopenharmony_ci         struct fd_pipe *pipe;
108bf215546Sopenharmony_ci         DECLARE_ARRAY(struct fd_bo *, reloc_bos);
109bf215546Sopenharmony_ci      };
110bf215546Sopenharmony_ci      /* for other cases: */
111bf215546Sopenharmony_ci      struct {
112bf215546Sopenharmony_ci         struct fd_submit *submit;
113bf215546Sopenharmony_ci         DECLARE_ARRAY(struct fd_cmd_sp, cmds);
114bf215546Sopenharmony_ci      };
115bf215546Sopenharmony_ci   } u;
116bf215546Sopenharmony_ci
117bf215546Sopenharmony_ci   struct fd_bo *ring_bo;
118bf215546Sopenharmony_ci};
119bf215546Sopenharmony_ciFD_DEFINE_CAST(fd_ringbuffer, fd_ringbuffer_sp);
120bf215546Sopenharmony_ci
121bf215546Sopenharmony_civoid fd_pipe_sp_flush(struct fd_pipe *pipe, uint32_t fence);
122bf215546Sopenharmony_ciuint32_t fd_submit_append_bo(struct fd_submit_sp *submit, struct fd_bo *bo);
123bf215546Sopenharmony_cistruct fd_submit *fd_submit_sp_new(struct fd_pipe *pipe,
124bf215546Sopenharmony_ci                                   flush_submit_list_fn flush_submit_list);
125bf215546Sopenharmony_civoid fd_pipe_sp_ringpool_init(struct fd_pipe *pipe);
126bf215546Sopenharmony_civoid fd_pipe_sp_ringpool_fini(struct fd_pipe *pipe);
127bf215546Sopenharmony_cistruct fd_ringbuffer *fd_ringbuffer_sp_new_object(struct fd_pipe *pipe, uint32_t size);
128bf215546Sopenharmony_ci
129bf215546Sopenharmony_ci#endif /* FREEDRENO_RINGBUFFER_SP_H_ */
130