freedreno_ringbuffer.c revision 857b0bc6
1e88f27b3Smrg/* -*- mode: C; c-file-style: "k&r"; tab-width 4; indent-tabs-mode: t; -*- */
2e88f27b3Smrg
3e88f27b3Smrg/*
4e88f27b3Smrg * Copyright (C) 2012 Rob Clark <robclark@freedesktop.org>
5e88f27b3Smrg *
6e88f27b3Smrg * Permission is hereby granted, free of charge, to any person obtaining a
7e88f27b3Smrg * copy of this software and associated documentation files (the "Software"),
8e88f27b3Smrg * to deal in the Software without restriction, including without limitation
9e88f27b3Smrg * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10e88f27b3Smrg * and/or sell copies of the Software, and to permit persons to whom the
11e88f27b3Smrg * Software is furnished to do so, subject to the following conditions:
12e88f27b3Smrg *
13e88f27b3Smrg * The above copyright notice and this permission notice (including the next
14e88f27b3Smrg * paragraph) shall be included in all copies or substantial portions of the
15e88f27b3Smrg * Software.
16e88f27b3Smrg *
17e88f27b3Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18e88f27b3Smrg * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19e88f27b3Smrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
20e88f27b3Smrg * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21e88f27b3Smrg * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22e88f27b3Smrg * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23e88f27b3Smrg * SOFTWARE.
24e88f27b3Smrg *
25e88f27b3Smrg * Authors:
26e88f27b3Smrg *    Rob Clark <robclark@freedesktop.org>
27e88f27b3Smrg */
28e88f27b3Smrg
29e88f27b3Smrg#include <assert.h>
30e88f27b3Smrg
31e88f27b3Smrg#include "freedreno_drmif.h"
32e88f27b3Smrg#include "freedreno_priv.h"
33e88f27b3Smrg#include "freedreno_ringbuffer.h"
34e88f27b3Smrg
35e88f27b3Smrgstruct fd_ringbuffer * fd_ringbuffer_new(struct fd_pipe *pipe,
36e88f27b3Smrg		uint32_t size)
37e88f27b3Smrg{
38e88f27b3Smrg	struct fd_ringbuffer *ring;
39e88f27b3Smrg
40e88f27b3Smrg	ring = pipe->funcs->ringbuffer_new(pipe, size);
41e88f27b3Smrg	if (!ring)
42e88f27b3Smrg		return NULL;
43e88f27b3Smrg
44e88f27b3Smrg	ring->size = size;
45e88f27b3Smrg	ring->pipe = pipe;
46e88f27b3Smrg	ring->start = ring->funcs->hostptr(ring);
47e88f27b3Smrg	ring->end = &(ring->start[size/4]);
48e88f27b3Smrg
49e88f27b3Smrg	ring->cur = ring->last_start = ring->start;
50e88f27b3Smrg
51e88f27b3Smrg	return ring;
52e88f27b3Smrg}
53e88f27b3Smrg
54e88f27b3Smrgvoid fd_ringbuffer_del(struct fd_ringbuffer *ring)
55e88f27b3Smrg{
56e88f27b3Smrg	ring->funcs->destroy(ring);
57e88f27b3Smrg}
58e88f27b3Smrg
59e88f27b3Smrg/* ringbuffers which are IB targets should set the toplevel rb (ie.
60e88f27b3Smrg * the IB source) as it's parent before emitting reloc's, to ensure
61e88f27b3Smrg * the bookkeeping works out properly.
62e88f27b3Smrg */
63e88f27b3Smrgvoid fd_ringbuffer_set_parent(struct fd_ringbuffer *ring,
64e88f27b3Smrg		struct fd_ringbuffer *parent)
65e88f27b3Smrg{
66e88f27b3Smrg	ring->parent = parent;
67e88f27b3Smrg}
68e88f27b3Smrg
69e88f27b3Smrgvoid fd_ringbuffer_reset(struct fd_ringbuffer *ring)
70e88f27b3Smrg{
71e88f27b3Smrg	uint32_t *start = ring->start;
72e88f27b3Smrg	if (ring->pipe->id == FD_PIPE_2D)
73e88f27b3Smrg		start = &ring->start[0x140];
74e88f27b3Smrg	ring->cur = ring->last_start = start;
75857b0bc6Smrg	if (ring->funcs->reset)
76857b0bc6Smrg		ring->funcs->reset(ring);
77e88f27b3Smrg}
78e88f27b3Smrg
79e88f27b3Smrg/* maybe get rid of this and use fd_ringmarker_flush() from DDX too? */
80e88f27b3Smrgint fd_ringbuffer_flush(struct fd_ringbuffer *ring)
81e88f27b3Smrg{
82e88f27b3Smrg	return ring->funcs->flush(ring, ring->last_start);
83e88f27b3Smrg}
84e88f27b3Smrg
85e88f27b3Smrguint32_t fd_ringbuffer_timestamp(struct fd_ringbuffer *ring)
86e88f27b3Smrg{
87e88f27b3Smrg	return ring->last_timestamp;
88e88f27b3Smrg}
89e88f27b3Smrg
90e88f27b3Smrgvoid fd_ringbuffer_reloc(struct fd_ringbuffer *ring,
91e88f27b3Smrg		const struct fd_reloc *reloc)
92e88f27b3Smrg{
93e88f27b3Smrg	ring->funcs->emit_reloc(ring, reloc);
94e88f27b3Smrg}
95e88f27b3Smrg
96e88f27b3Smrgvoid fd_ringbuffer_emit_reloc_ring(struct fd_ringbuffer *ring,
97e88f27b3Smrg		struct fd_ringmarker *target, struct fd_ringmarker *end)
98e88f27b3Smrg{
99e88f27b3Smrg	assert(target->ring == end->ring);
100e88f27b3Smrg	ring->funcs->emit_reloc_ring(ring, target, end);
101e88f27b3Smrg}
102e88f27b3Smrg
103e88f27b3Smrgstruct fd_ringmarker * fd_ringmarker_new(struct fd_ringbuffer *ring)
104e88f27b3Smrg{
105e88f27b3Smrg	struct fd_ringmarker *marker = NULL;
106e88f27b3Smrg
107e88f27b3Smrg	marker = calloc(1, sizeof(*marker));
108e88f27b3Smrg	if (!marker) {
109e88f27b3Smrg		ERROR_MSG("allocation failed");
110e88f27b3Smrg		return NULL;
111e88f27b3Smrg	}
112e88f27b3Smrg
113e88f27b3Smrg	marker->ring = ring;
114e88f27b3Smrg
115e88f27b3Smrg	fd_ringmarker_mark(marker);
116e88f27b3Smrg
117e88f27b3Smrg	return marker;
118e88f27b3Smrg}
119e88f27b3Smrg
120e88f27b3Smrgvoid fd_ringmarker_del(struct fd_ringmarker *marker)
121e88f27b3Smrg{
122e88f27b3Smrg	free(marker);
123e88f27b3Smrg}
124e88f27b3Smrg
125e88f27b3Smrgvoid fd_ringmarker_mark(struct fd_ringmarker *marker)
126e88f27b3Smrg{
127e88f27b3Smrg	marker->cur = marker->ring->cur;
128e88f27b3Smrg}
129e88f27b3Smrg
130e88f27b3Smrguint32_t fd_ringmarker_dwords(struct fd_ringmarker *start,
131e88f27b3Smrg		struct fd_ringmarker *end)
132e88f27b3Smrg{
133e88f27b3Smrg	return end->cur - start->cur;
134e88f27b3Smrg}
135e88f27b3Smrg
136e88f27b3Smrgint fd_ringmarker_flush(struct fd_ringmarker *marker)
137e88f27b3Smrg{
138e88f27b3Smrg	struct fd_ringbuffer *ring = marker->ring;
139e88f27b3Smrg	return ring->funcs->flush(ring, marker->cur);
140e88f27b3Smrg}
141