1b8e80941Smrg/*
2b8e80941Smrg * Copyright (C) 2012-2018 Rob Clark <robclark@freedesktop.org>
3b8e80941Smrg *
4b8e80941Smrg * Permission is hereby granted, free of charge, to any person obtaining a
5b8e80941Smrg * copy of this software and associated documentation files (the "Software"),
6b8e80941Smrg * to deal in the Software without restriction, including without limitation
7b8e80941Smrg * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8b8e80941Smrg * and/or sell copies of the Software, and to permit persons to whom the
9b8e80941Smrg * Software is furnished to do so, subject to the following conditions:
10b8e80941Smrg *
11b8e80941Smrg * The above copyright notice and this permission notice (including the next
12b8e80941Smrg * paragraph) shall be included in all copies or substantial portions of the
13b8e80941Smrg * Software.
14b8e80941Smrg *
15b8e80941Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16b8e80941Smrg * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17b8e80941Smrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18b8e80941Smrg * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19b8e80941Smrg * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20b8e80941Smrg * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21b8e80941Smrg * SOFTWARE.
22b8e80941Smrg *
23b8e80941Smrg * Authors:
24b8e80941Smrg *    Rob Clark <robclark@freedesktop.org>
25b8e80941Smrg */
26b8e80941Smrg
27b8e80941Smrg#ifndef FREEDRENO_RINGBUFFER_H_
28b8e80941Smrg#define FREEDRENO_RINGBUFFER_H_
29b8e80941Smrg
30b8e80941Smrg#include "util/u_debug.h"
31b8e80941Smrg
32b8e80941Smrg#include "freedreno_drmif.h"
33b8e80941Smrg
34b8e80941Smrgstruct fd_submit;
35b8e80941Smrgstruct fd_ringbuffer;
36b8e80941Smrg
37b8e80941Smrgenum fd_ringbuffer_flags {
38b8e80941Smrg
39b8e80941Smrg	/* Primary ringbuffer for a submit, ie. an IB1 level rb
40b8e80941Smrg	 * which kernel must setup RB->IB1 CP_INDIRECT_BRANCH
41b8e80941Smrg	 * packets.
42b8e80941Smrg	 */
43b8e80941Smrg	FD_RINGBUFFER_PRIMARY = 0x1,
44b8e80941Smrg
45b8e80941Smrg	/* Hint that the stateobj will be used for streaming state
46b8e80941Smrg	 * that is used once or a few times and then discarded.
47b8e80941Smrg	 *
48b8e80941Smrg	 * For sub-allocation, non streaming stateobj's should be
49b8e80941Smrg	 * sub-allocated from a page size buffer, so one long lived
50b8e80941Smrg	 * state obj doesn't prevent other pages from being freed.
51b8e80941Smrg	 * (Ie. it would be no worse than allocating a page sized
52b8e80941Smrg	 * bo for each small non-streaming stateobj).
53b8e80941Smrg	 *
54b8e80941Smrg	 * But streaming stateobj's could be sub-allocated from a
55b8e80941Smrg	 * larger buffer to reduce the alloc/del overhead.
56b8e80941Smrg	 */
57b8e80941Smrg	FD_RINGBUFFER_STREAMING = 0x2,
58b8e80941Smrg
59b8e80941Smrg	/* Indicates that "growable" cmdstream can be used,
60b8e80941Smrg	 * consisting of multiple physical cmdstream buffers
61b8e80941Smrg	 */
62b8e80941Smrg	FD_RINGBUFFER_GROWABLE = 0x4,
63b8e80941Smrg
64b8e80941Smrg	/* Internal use only: */
65b8e80941Smrg	_FD_RINGBUFFER_OBJECT = 0x8,
66b8e80941Smrg};
67b8e80941Smrg
68b8e80941Smrg/* A submit object manages/tracks all the state buildup for a "submit"
69b8e80941Smrg * ioctl to the kernel.  Additionally, with the exception of long-lived
70b8e80941Smrg * non-STREAMING stateobj rb's, rb's are allocated from the submit.
71b8e80941Smrg */
72b8e80941Smrgstruct fd_submit * fd_submit_new(struct fd_pipe *pipe);
73b8e80941Smrg
74b8e80941Smrg/* NOTE: all ringbuffer's create from the submit should be unref'd
75b8e80941Smrg * before destroying the submit.
76b8e80941Smrg */
77b8e80941Smrgvoid fd_submit_del(struct fd_submit *submit);
78b8e80941Smrg
79b8e80941Smrg/* Allocate a new rb from the submit. */
80b8e80941Smrgstruct fd_ringbuffer * fd_submit_new_ringbuffer(struct fd_submit *submit,
81b8e80941Smrg		uint32_t size, enum fd_ringbuffer_flags flags);
82b8e80941Smrg
83b8e80941Smrg/* in_fence_fd: -1 for no in-fence, else fence fd
84b8e80941Smrg * out_fence_fd: NULL for no output-fence requested, else ptr to return out-fence
85b8e80941Smrg */
86b8e80941Smrgint fd_submit_flush(struct fd_submit *submit,
87b8e80941Smrg		int in_fence_fd, int *out_fence_fd,
88b8e80941Smrg		uint32_t *out_fence);
89b8e80941Smrg
90b8e80941Smrgstruct fd_ringbuffer_funcs;
91b8e80941Smrg
92b8e80941Smrg/* the ringbuffer object is not opaque so that OUT_RING() type stuff
93b8e80941Smrg * can be inlined.  Note that users should not make assumptions about
94b8e80941Smrg * the size of this struct.
95b8e80941Smrg */
96b8e80941Smrgstruct fd_ringbuffer {
97b8e80941Smrg	uint32_t *cur, *end, *start;
98b8e80941Smrg	const struct fd_ringbuffer_funcs *funcs;
99b8e80941Smrg
100b8e80941Smrg// size or end coudl probably go away
101b8e80941Smrg	int size;
102b8e80941Smrg	int32_t refcnt;
103b8e80941Smrg	enum fd_ringbuffer_flags flags;
104b8e80941Smrg};
105b8e80941Smrg
106b8e80941Smrg/* Allocate a new long-lived state object, not associated with
107b8e80941Smrg * a submit:
108b8e80941Smrg */
109b8e80941Smrgstruct fd_ringbuffer * fd_ringbuffer_new_object(struct fd_pipe *pipe,
110b8e80941Smrg		uint32_t size);
111b8e80941Smrg
112b8e80941Smrgstruct fd_ringbuffer *fd_ringbuffer_ref(struct fd_ringbuffer *ring);
113b8e80941Smrgvoid fd_ringbuffer_del(struct fd_ringbuffer *ring);
114b8e80941Smrg
115b8e80941Smrgvoid fd_ringbuffer_grow(struct fd_ringbuffer *ring, uint32_t ndwords);
116b8e80941Smrg
117b8e80941Smrgstatic inline void fd_ringbuffer_emit(struct fd_ringbuffer *ring,
118b8e80941Smrg		uint32_t data)
119b8e80941Smrg{
120b8e80941Smrg	(*ring->cur++) = data;
121b8e80941Smrg}
122b8e80941Smrg
123b8e80941Smrgstruct fd_reloc {
124b8e80941Smrg	struct fd_bo *bo;
125b8e80941Smrg#define FD_RELOC_READ             0x0001
126b8e80941Smrg#define FD_RELOC_WRITE            0x0002
127b8e80941Smrg#define FD_RELOC_DUMP             0x0004
128b8e80941Smrg	uint32_t flags;
129b8e80941Smrg	uint32_t offset;
130b8e80941Smrg	uint32_t or;
131b8e80941Smrg	int32_t  shift;
132b8e80941Smrg	uint32_t orhi;      /* used for a5xx+ */
133b8e80941Smrg};
134b8e80941Smrg
135b8e80941Smrg/* NOTE: relocs are 2 dwords on a5xx+ */
136b8e80941Smrg
137b8e80941Smrgvoid fd_ringbuffer_reloc(struct fd_ringbuffer *ring, const struct fd_reloc *reloc);
138b8e80941Smrguint32_t fd_ringbuffer_cmd_count(struct fd_ringbuffer *ring);
139b8e80941Smrguint32_t fd_ringbuffer_emit_reloc_ring_full(struct fd_ringbuffer *ring,
140b8e80941Smrg		struct fd_ringbuffer *target, uint32_t cmd_idx);
141b8e80941Smrg
142b8e80941Smrgstatic inline uint32_t
143b8e80941Smrgoffset_bytes(void *end, void *start)
144b8e80941Smrg{
145b8e80941Smrg	return ((char *)end) - ((char *)start);
146b8e80941Smrg}
147b8e80941Smrg
148b8e80941Smrgstatic inline uint32_t
149b8e80941Smrgfd_ringbuffer_size(struct fd_ringbuffer *ring)
150b8e80941Smrg{
151b8e80941Smrg	/* only really needed for stateobj ringbuffers, and won't really
152b8e80941Smrg	 * do what you expect for growable rb's.. so lets just restrict
153b8e80941Smrg	 * this to stateobj's for now:
154b8e80941Smrg	 */
155b8e80941Smrg	debug_assert(!(ring->flags & FD_RINGBUFFER_GROWABLE));
156b8e80941Smrg	return offset_bytes(ring->cur, ring->start);
157b8e80941Smrg}
158b8e80941Smrg
159b8e80941Smrg
160b8e80941Smrg#endif /* FREEDRENO_RINGBUFFER_H_ */
161