Home | History | Annotate | Download | only in freedreno
      1 /* -*- mode: C; c-file-style: "k&r"; tab-width 4; indent-tabs-mode: t; -*- */
      2 
      3 /*
      4  * Copyright (C) 2012 Rob Clark <robclark (at) 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 (at) freedesktop.org>
     27  */
     28 
     29 #include <assert.h>
     30 
     31 #include "freedreno_drmif.h"
     32 #include "freedreno_priv.h"
     33 #include "freedreno_ringbuffer.h"
     34 
     35 struct fd_ringbuffer *
     36 fd_ringbuffer_new(struct fd_pipe *pipe, uint32_t size)
     37 {
     38 	struct fd_ringbuffer *ring;
     39 
     40 	ring = pipe->funcs->ringbuffer_new(pipe, size);
     41 	if (!ring)
     42 		return NULL;
     43 
     44 	ring->pipe = pipe;
     45 	ring->start = ring->funcs->hostptr(ring);
     46 	ring->end = &(ring->start[ring->size/4]);
     47 
     48 	ring->cur = ring->last_start = ring->start;
     49 
     50 	return ring;
     51 }
     52 
     53 void fd_ringbuffer_del(struct fd_ringbuffer *ring)
     54 {
     55 	fd_ringbuffer_reset(ring);
     56 	ring->funcs->destroy(ring);
     57 }
     58 
     59 /* ringbuffers which are IB targets should set the toplevel rb (ie.
     60  * the IB source) as it's parent before emitting reloc's, to ensure
     61  * the bookkeeping works out properly.
     62  */
     63 void fd_ringbuffer_set_parent(struct fd_ringbuffer *ring,
     64 					 struct fd_ringbuffer *parent)
     65 {
     66 	ring->parent = parent;
     67 }
     68 
     69 void fd_ringbuffer_reset(struct fd_ringbuffer *ring)
     70 {
     71 	uint32_t *start = ring->start;
     72 	if (ring->pipe->id == FD_PIPE_2D)
     73 		start = &ring->start[0x140];
     74 	ring->cur = ring->last_start = start;
     75 	if (ring->funcs->reset)
     76 		ring->funcs->reset(ring);
     77 }
     78 
     79 int fd_ringbuffer_flush(struct fd_ringbuffer *ring)
     80 {
     81 	return ring->funcs->flush(ring, ring->last_start, -1, NULL);
     82 }
     83 
     84 int fd_ringbuffer_flush2(struct fd_ringbuffer *ring, int in_fence_fd,
     85 		int *out_fence_fd)
     86 {
     87 	return ring->funcs->flush(ring, ring->last_start, in_fence_fd, out_fence_fd);
     88 }
     89 
     90 void fd_ringbuffer_grow(struct fd_ringbuffer *ring, uint32_t ndwords)
     91 {
     92 	assert(ring->funcs->grow);     /* unsupported on kgsl */
     93 
     94 	/* there is an upper bound on IB size, which appears to be 0x100000 */
     95 	if (ring->size < 0x100000)
     96 		ring->size *= 2;
     97 
     98 	ring->funcs->grow(ring, ring->size);
     99 
    100 	ring->start = ring->funcs->hostptr(ring);
    101 	ring->end = &(ring->start[ring->size/4]);
    102 
    103 	ring->cur = ring->last_start = ring->start;
    104 }
    105 
    106 uint32_t fd_ringbuffer_timestamp(struct fd_ringbuffer *ring)
    107 {
    108 	return ring->last_timestamp;
    109 }
    110 
    111 void fd_ringbuffer_reloc(struct fd_ringbuffer *ring,
    112 				    const struct fd_reloc *reloc)
    113 {
    114 	assert(ring->pipe->gpu_id < 500);
    115 	ring->funcs->emit_reloc(ring, reloc);
    116 }
    117 
    118 void fd_ringbuffer_reloc2(struct fd_ringbuffer *ring,
    119 				     const struct fd_reloc *reloc)
    120 {
    121 	ring->funcs->emit_reloc(ring, reloc);
    122 }
    123 
    124 void fd_ringbuffer_emit_reloc_ring(struct fd_ringbuffer *ring,
    125 		struct fd_ringmarker *target, struct fd_ringmarker *end)
    126 {
    127 	uint32_t submit_offset, size;
    128 
    129 	/* This function is deprecated and not supported on 64b devices: */
    130 	assert(ring->pipe->gpu_id < 500);
    131 	assert(target->ring == end->ring);
    132 
    133 	submit_offset = offset_bytes(target->cur, target->ring->start);
    134 	size = offset_bytes(end->cur, target->cur);
    135 
    136 	ring->funcs->emit_reloc_ring(ring, target->ring, 0, submit_offset, size);
    137 }
    138 
    139 uint32_t fd_ringbuffer_cmd_count(struct fd_ringbuffer *ring)
    140 {
    141 	if (!ring->funcs->cmd_count)
    142 		return 1;
    143 	return ring->funcs->cmd_count(ring);
    144 }
    145 
    146 uint32_t
    147 fd_ringbuffer_emit_reloc_ring_full(struct fd_ringbuffer *ring,
    148 		struct fd_ringbuffer *target, uint32_t cmd_idx)
    149 {
    150 	uint32_t size = offset_bytes(target->cur, target->start);
    151 	return ring->funcs->emit_reloc_ring(ring, target, cmd_idx, 0, size);
    152 }
    153 
    154 struct fd_ringmarker * fd_ringmarker_new(struct fd_ringbuffer *ring)
    155 {
    156 	struct fd_ringmarker *marker = NULL;
    157 
    158 	marker = calloc(1, sizeof(*marker));
    159 	if (!marker) {
    160 		ERROR_MSG("allocation failed");
    161 		return NULL;
    162 	}
    163 
    164 	marker->ring = ring;
    165 
    166 	marker->cur = marker->ring->cur;
    167 
    168 	return marker;
    169 }
    170 
    171 void fd_ringmarker_del(struct fd_ringmarker *marker)
    172 {
    173 	free(marker);
    174 }
    175 
    176 void fd_ringmarker_mark(struct fd_ringmarker *marker)
    177 {
    178 	marker->cur = marker->ring->cur;
    179 }
    180 
    181 uint32_t fd_ringmarker_dwords(struct fd_ringmarker *start,
    182 					 struct fd_ringmarker *end)
    183 {
    184 	return end->cur - start->cur;
    185 }
    186 
    187 int fd_ringmarker_flush(struct fd_ringmarker *marker)
    188 {
    189 	struct fd_ringbuffer *ring = marker->ring;
    190 	return ring->funcs->flush(ring, marker->cur, -1, NULL);
    191 }
    192