1 /* -*- mode: C; c-file-style: "k&r"; tab-width 4; indent-tabs-mode: t; -*- */
4 * Copyright (C) 2012 Rob Clark <robclark@freedesktop.org>
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice (including the next
14 * paragraph) shall be included in all copies or substantial portions of the
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26 * Rob Clark <robclark@freedesktop.org>
31 #include "freedreno_drmif.h"
32 #include "freedreno_priv.h"
33 #include "freedreno_ringbuffer.h"
35 drm_public struct fd_ringbuffer *
36 fd_ringbuffer_new_flags(struct fd_pipe *pipe, uint32_t size,
37 enum fd_ringbuffer_flags flags)
39 struct fd_ringbuffer *ring;
41 /* we can't really support "growable" rb's in general for
42 * stateobj's since we need a single gpu addr (ie. can't
43 * do the trick of a chain of IB packets):
45 if (flags & FD_RINGBUFFER_OBJECT)
48 ring = pipe->funcs->ringbuffer_new(pipe, size, flags);
54 ring->start = ring->funcs->hostptr(ring);
55 ring->end = &(ring->start[ring->size/4]);
57 ring->cur = ring->last_start = ring->start;
62 drm_public struct fd_ringbuffer *
63 fd_ringbuffer_new(struct fd_pipe *pipe, uint32_t size)
65 return fd_ringbuffer_new_flags(pipe, size, 0);
68 drm_public struct fd_ringbuffer *
69 fd_ringbuffer_new_object(struct fd_pipe *pipe, uint32_t size)
71 return fd_ringbuffer_new_flags(pipe, size, FD_RINGBUFFER_OBJECT);
74 drm_public void fd_ringbuffer_del(struct fd_ringbuffer *ring)
76 if (!atomic_dec_and_test(&ring->refcnt))
79 fd_ringbuffer_reset(ring);
80 ring->funcs->destroy(ring);
83 drm_public struct fd_ringbuffer *
84 fd_ringbuffer_ref(struct fd_ringbuffer *ring)
86 STATIC_ASSERT(sizeof(ring->refcnt) <= sizeof(ring->__pad));
87 atomic_inc(&ring->refcnt);
91 /* ringbuffers which are IB targets should set the toplevel rb (ie.
92 * the IB source) as it's parent before emitting reloc's, to ensure
93 * the bookkeeping works out properly.
95 drm_public void fd_ringbuffer_set_parent(struct fd_ringbuffer *ring,
96 struct fd_ringbuffer *parent)
98 /* state objects should not be parented! */
99 assert(!(ring->flags & FD_RINGBUFFER_OBJECT));
100 ring->parent = parent;
103 drm_public void fd_ringbuffer_reset(struct fd_ringbuffer *ring)
105 uint32_t *start = ring->start;
106 if (ring->pipe->id == FD_PIPE_2D)
107 start = &ring->start[0x140];
108 ring->cur = ring->last_start = start;
109 if (ring->funcs->reset)
110 ring->funcs->reset(ring);
113 drm_public int fd_ringbuffer_flush(struct fd_ringbuffer *ring)
115 return ring->funcs->flush(ring, ring->last_start, -1, NULL);
118 drm_public int fd_ringbuffer_flush2(struct fd_ringbuffer *ring, int in_fence_fd,
121 return ring->funcs->flush(ring, ring->last_start, in_fence_fd, out_fence_fd);
124 drm_public void fd_ringbuffer_grow(struct fd_ringbuffer *ring, uint32_t ndwords)
126 assert(ring->funcs->grow); /* unsupported on kgsl */
128 /* there is an upper bound on IB size, which appears to be 0x100000 */
129 if (ring->size < 0x100000)
132 ring->funcs->grow(ring, ring->size);
134 ring->start = ring->funcs->hostptr(ring);
135 ring->end = &(ring->start[ring->size/4]);
137 ring->cur = ring->last_start = ring->start;
140 drm_public uint32_t fd_ringbuffer_timestamp(struct fd_ringbuffer *ring)
142 return ring->last_timestamp;
145 drm_public void fd_ringbuffer_reloc(struct fd_ringbuffer *ring,
146 const struct fd_reloc *reloc)
148 assert(ring->pipe->gpu_id < 500);
149 ring->funcs->emit_reloc(ring, reloc);
152 drm_public void fd_ringbuffer_reloc2(struct fd_ringbuffer *ring,
153 const struct fd_reloc *reloc)
155 ring->funcs->emit_reloc(ring, reloc);
158 drm_public uint32_t fd_ringbuffer_cmd_count(struct fd_ringbuffer *ring)
160 if (!ring->funcs->cmd_count)
162 return ring->funcs->cmd_count(ring);
166 fd_ringbuffer_emit_reloc_ring_full(struct fd_ringbuffer *ring,
167 struct fd_ringbuffer *target, uint32_t cmd_idx)
169 return ring->funcs->emit_reloc_ring(ring, target, cmd_idx);
173 fd_ringbuffer_size(struct fd_ringbuffer *ring)
175 /* only really needed for stateobj ringbuffers, and won't really
176 * do what you expect for growable rb's.. so lets just restrict
177 * this to stateobj's for now:
179 assert(ring->flags & FD_RINGBUFFER_OBJECT);
180 return offset_bytes(ring->cur, ring->start);