1/* -*- mode: C; c-file-style: "k&r"; tab-width 4; indent-tabs-mode: t; -*- */
2
3/*
4 * Copyright (C) 2012 Rob Clark <robclark@freedesktop.org>
5 *
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:
12 *
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
15 * Software.
16 *
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
23 * SOFTWARE.
24 *
25 * Authors:
26 *    Rob Clark <robclark@freedesktop.org>
27 */
28
29#ifdef HAVE_CONFIG_H
30# include <config.h>
31#endif
32
33#include <assert.h>
34
35#include "freedreno_drmif.h"
36#include "freedreno_priv.h"
37#include "freedreno_ringbuffer.h"
38
39drm_public struct fd_ringbuffer *
40fd_ringbuffer_new(struct fd_pipe *pipe, uint32_t size)
41{
42	struct fd_ringbuffer *ring;
43
44	ring = pipe->funcs->ringbuffer_new(pipe, size);
45	if (!ring)
46		return NULL;
47
48	ring->size = size;
49	ring->pipe = pipe;
50	ring->start = ring->funcs->hostptr(ring);
51	ring->end = &(ring->start[size/4]);
52
53	ring->cur = ring->last_start = ring->start;
54
55	return ring;
56}
57
58drm_public void fd_ringbuffer_del(struct fd_ringbuffer *ring)
59{
60	ring->funcs->destroy(ring);
61}
62
63/* ringbuffers which are IB targets should set the toplevel rb (ie.
64 * the IB source) as it's parent before emitting reloc's, to ensure
65 * the bookkeeping works out properly.
66 */
67drm_public void fd_ringbuffer_set_parent(struct fd_ringbuffer *ring,
68					 struct fd_ringbuffer *parent)
69{
70	ring->parent = parent;
71}
72
73drm_public void fd_ringbuffer_reset(struct fd_ringbuffer *ring)
74{
75	uint32_t *start = ring->start;
76	if (ring->pipe->id == FD_PIPE_2D)
77		start = &ring->start[0x140];
78	ring->cur = ring->last_start = start;
79	if (ring->funcs->reset)
80		ring->funcs->reset(ring);
81}
82
83/* maybe get rid of this and use fd_ringmarker_flush() from DDX too? */
84drm_public int fd_ringbuffer_flush(struct fd_ringbuffer *ring)
85{
86	return ring->funcs->flush(ring, ring->last_start);
87}
88
89drm_public uint32_t fd_ringbuffer_timestamp(struct fd_ringbuffer *ring)
90{
91	return ring->last_timestamp;
92}
93
94drm_public void fd_ringbuffer_reloc(struct fd_ringbuffer *ring,
95				    const struct fd_reloc *reloc)
96{
97	ring->funcs->emit_reloc(ring, reloc);
98}
99
100drm_public void
101fd_ringbuffer_emit_reloc_ring(struct fd_ringbuffer *ring,
102			      struct fd_ringmarker *target,
103			      struct fd_ringmarker *end)
104{
105	assert(target->ring == end->ring);
106	ring->funcs->emit_reloc_ring(ring, target, end);
107}
108
109drm_public struct fd_ringmarker * fd_ringmarker_new(struct fd_ringbuffer *ring)
110{
111	struct fd_ringmarker *marker = NULL;
112
113	marker = calloc(1, sizeof(*marker));
114	if (!marker) {
115		ERROR_MSG("allocation failed");
116		return NULL;
117	}
118
119	marker->ring = ring;
120
121	fd_ringmarker_mark(marker);
122
123	return marker;
124}
125
126drm_public void fd_ringmarker_del(struct fd_ringmarker *marker)
127{
128	free(marker);
129}
130
131drm_public void fd_ringmarker_mark(struct fd_ringmarker *marker)
132{
133	marker->cur = marker->ring->cur;
134}
135
136drm_public uint32_t fd_ringmarker_dwords(struct fd_ringmarker *start,
137					 struct fd_ringmarker *end)
138{
139	return end->cur - start->cur;
140}
141
142drm_public int fd_ringmarker_flush(struct fd_ringmarker *marker)
143{
144	struct fd_ringbuffer *ring = marker->ring;
145	return ring->funcs->flush(ring, marker->cur);
146}
147