etnaviv_priv.h revision 037b3c26
1037b3c26Smrg/*
2037b3c26Smrg * Copyright (C) 2014-2015 Etnaviv Project
3037b3c26Smrg *
4037b3c26Smrg * Permission is hereby granted, free of charge, to any person obtaining a
5037b3c26Smrg * copy of this software and associated documentation files (the "Software"),
6037b3c26Smrg * to deal in the Software without restriction, including without limitation
7037b3c26Smrg * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8037b3c26Smrg * and/or sell copies of the Software, and to permit persons to whom the
9037b3c26Smrg * Software is furnished to do so, subject to the following conditions:
10037b3c26Smrg *
11037b3c26Smrg * The above copyright notice and this permission notice (including the next
12037b3c26Smrg * paragraph) shall be included in all copies or substantial portions of the
13037b3c26Smrg * Software.
14037b3c26Smrg *
15037b3c26Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16037b3c26Smrg * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17037b3c26Smrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18037b3c26Smrg * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19037b3c26Smrg * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20037b3c26Smrg * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21037b3c26Smrg * SOFTWARE.
22037b3c26Smrg *
23037b3c26Smrg * Authors:
24037b3c26Smrg *    Christian Gmeiner <christian.gmeiner@gmail.com>
25037b3c26Smrg */
26037b3c26Smrg
27037b3c26Smrg#ifndef ETNAVIV_PRIV_H_
28037b3c26Smrg#define ETNAVIV_PRIV_H_
29037b3c26Smrg
30037b3c26Smrg#include <stdlib.h>
31037b3c26Smrg#include <errno.h>
32037b3c26Smrg#include <string.h>
33037b3c26Smrg#include <unistd.h>
34037b3c26Smrg#include <errno.h>
35037b3c26Smrg#include <fcntl.h>
36037b3c26Smrg#include <sys/ioctl.h>
37037b3c26Smrg#include <pthread.h>
38037b3c26Smrg#include <stdio.h>
39037b3c26Smrg#include <assert.h>
40037b3c26Smrg
41037b3c26Smrg#include "libdrm_macros.h"
42037b3c26Smrg#include "xf86drm.h"
43037b3c26Smrg#include "xf86atomic.h"
44037b3c26Smrg
45037b3c26Smrg#include "util_double_list.h"
46037b3c26Smrg
47037b3c26Smrg#include "etnaviv_drmif.h"
48037b3c26Smrg#include "etnaviv_drm.h"
49037b3c26Smrg
50037b3c26Smrg#define VIV_FEATURES_WORD_COUNT 7
51037b3c26Smrg
52037b3c26Smrgstruct etna_specs {
53037b3c26Smrg	uint32_t model;
54037b3c26Smrg	uint32_t revision;
55037b3c26Smrg	uint32_t features[VIV_FEATURES_WORD_COUNT];
56037b3c26Smrg	uint32_t stream_count;
57037b3c26Smrg	uint32_t register_max;
58037b3c26Smrg	uint32_t thread_count;
59037b3c26Smrg	uint32_t shader_core_count;
60037b3c26Smrg	uint32_t vertex_cache_size;
61037b3c26Smrg	uint32_t vertex_output_buffer_size;
62037b3c26Smrg	uint32_t pixel_pipes;
63037b3c26Smrg	uint32_t instruction_count;
64037b3c26Smrg	uint32_t num_constants;
65037b3c26Smrg	uint32_t num_varyings;
66037b3c26Smrg	uint32_t buffer_size;
67037b3c26Smrg};
68037b3c26Smrg
69037b3c26Smrgstruct etna_bo_bucket {
70037b3c26Smrg	uint32_t size;
71037b3c26Smrg	struct list_head list;
72037b3c26Smrg};
73037b3c26Smrg
74037b3c26Smrgstruct etna_bo_cache {
75037b3c26Smrg	struct etna_bo_bucket cache_bucket[14 * 4];
76037b3c26Smrg	unsigned num_buckets;
77037b3c26Smrg	time_t time;
78037b3c26Smrg};
79037b3c26Smrg
80037b3c26Smrgstruct etna_device {
81037b3c26Smrg	int fd;
82037b3c26Smrg	atomic_t refcnt;
83037b3c26Smrg
84037b3c26Smrg	/* tables to keep track of bo's, to avoid "evil-twin" etna_bo objects:
85037b3c26Smrg	 *
86037b3c26Smrg	 *   handle_table: maps handle to etna_bo
87037b3c26Smrg	 *   name_table: maps flink name to etna_bo
88037b3c26Smrg	 *
89037b3c26Smrg	 * We end up needing two tables, because DRM_IOCTL_GEM_OPEN always
90037b3c26Smrg	 * returns a new handle.  So we need to figure out if the bo is already
91037b3c26Smrg	 * open in the process first, before calling gem-open.
92037b3c26Smrg	 */
93037b3c26Smrg	void *handle_table, *name_table;
94037b3c26Smrg
95037b3c26Smrg	struct etna_bo_cache bo_cache;
96037b3c26Smrg
97037b3c26Smrg	int closefd;        /* call close(fd) upon destruction */
98037b3c26Smrg};
99037b3c26Smrg
100037b3c26Smrgdrm_private void etna_bo_cache_init(struct etna_bo_cache *cache);
101037b3c26Smrgdrm_private void etna_bo_cache_cleanup(struct etna_bo_cache *cache, time_t time);
102037b3c26Smrgdrm_private struct etna_bo *etna_bo_cache_alloc(struct etna_bo_cache *cache,
103037b3c26Smrg		uint32_t *size, uint32_t flags);
104037b3c26Smrgdrm_private int etna_bo_cache_free(struct etna_bo_cache *cache, struct etna_bo *bo);
105037b3c26Smrg
106037b3c26Smrg/* for where @table_lock is already held: */
107037b3c26Smrgdrm_private void etna_device_del_locked(struct etna_device *dev);
108037b3c26Smrg
109037b3c26Smrg/* a GEM buffer object allocated from the DRM device */
110037b3c26Smrgstruct etna_bo {
111037b3c26Smrg	struct etna_device      *dev;
112037b3c26Smrg	void            *map;           /* userspace mmap'ing (if there is one) */
113037b3c26Smrg	uint32_t        size;
114037b3c26Smrg	uint32_t        handle;
115037b3c26Smrg	uint32_t        flags;
116037b3c26Smrg	uint32_t        name;           /* flink global handle (DRI2 name) */
117037b3c26Smrg	uint64_t        offset;         /* offset to mmap() */
118037b3c26Smrg	atomic_t        refcnt;
119037b3c26Smrg
120037b3c26Smrg	/* in the common case, a bo won't be referenced by more than a single
121037b3c26Smrg	 * command stream.  So to avoid looping over all the bo's in the
122037b3c26Smrg	 * reloc table to find the idx of a bo that might already be in the
123037b3c26Smrg	 * table, we cache the idx in the bo.  But in order to detect the
124037b3c26Smrg	 * slow-path where bo is ref'd in multiple streams, we also must track
125037b3c26Smrg	 * the current_stream for which the idx is valid.  See bo2idx().
126037b3c26Smrg	 */
127037b3c26Smrg	struct etna_cmd_stream *current_stream;
128037b3c26Smrg	uint32_t idx;
129037b3c26Smrg
130037b3c26Smrg	int reuse;
131037b3c26Smrg	struct list_head list;   /* bucket-list entry */
132037b3c26Smrg	time_t free_time;        /* time when added to bucket-list */
133037b3c26Smrg};
134037b3c26Smrg
135037b3c26Smrgstruct etna_gpu {
136037b3c26Smrg	struct etna_device *dev;
137037b3c26Smrg	struct etna_specs specs;
138037b3c26Smrg	uint32_t core;
139037b3c26Smrg};
140037b3c26Smrg
141037b3c26Smrgstruct etna_pipe {
142037b3c26Smrg	enum etna_pipe_id id;
143037b3c26Smrg	struct etna_gpu *gpu;
144037b3c26Smrg};
145037b3c26Smrg
146037b3c26Smrgstruct etna_cmd_stream_priv {
147037b3c26Smrg	struct etna_cmd_stream base;
148037b3c26Smrg	struct etna_pipe *pipe;
149037b3c26Smrg
150037b3c26Smrg	uint32_t last_timestamp;
151037b3c26Smrg
152037b3c26Smrg	/* submit ioctl related tables: */
153037b3c26Smrg	struct {
154037b3c26Smrg		/* bo's table: */
155037b3c26Smrg		struct drm_etnaviv_gem_submit_bo *bos;
156037b3c26Smrg		uint32_t nr_bos, max_bos;
157037b3c26Smrg
158037b3c26Smrg		/* reloc's table: */
159037b3c26Smrg		struct drm_etnaviv_gem_submit_reloc *relocs;
160037b3c26Smrg		uint32_t nr_relocs, max_relocs;
161037b3c26Smrg	} submit;
162037b3c26Smrg
163037b3c26Smrg	/* should have matching entries in submit.bos: */
164037b3c26Smrg	struct etna_bo **bos;
165037b3c26Smrg	uint32_t nr_bos, max_bos;
166037b3c26Smrg
167037b3c26Smrg	/* notify callback if buffer reset happend */
168037b3c26Smrg	void (*reset_notify)(struct etna_cmd_stream *stream, void *priv);
169037b3c26Smrg	void *reset_notify_priv;
170037b3c26Smrg};
171037b3c26Smrg
172037b3c26Smrg#define ALIGN(v,a) (((v) + (a) - 1) & ~((a) - 1))
173037b3c26Smrg#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
174037b3c26Smrg
175037b3c26Smrg#define enable_debug 1  /* TODO make dynamic */
176037b3c26Smrg
177037b3c26Smrg#define INFO_MSG(fmt, ...) \
178037b3c26Smrg		do { drmMsg("[I] "fmt " (%s:%d)\n", \
179037b3c26Smrg				##__VA_ARGS__, __FUNCTION__, __LINE__); } while (0)
180037b3c26Smrg#define DEBUG_MSG(fmt, ...) \
181037b3c26Smrg		do if (enable_debug) { drmMsg("[D] "fmt " (%s:%d)\n", \
182037b3c26Smrg				##__VA_ARGS__, __FUNCTION__, __LINE__); } while (0)
183037b3c26Smrg#define WARN_MSG(fmt, ...) \
184037b3c26Smrg		do { drmMsg("[W] "fmt " (%s:%d)\n", \
185037b3c26Smrg				##__VA_ARGS__, __FUNCTION__, __LINE__); } while (0)
186037b3c26Smrg#define ERROR_MSG(fmt, ...) \
187037b3c26Smrg		do { drmMsg("[E] " fmt " (%s:%d)\n", \
188037b3c26Smrg				##__VA_ARGS__, __FUNCTION__, __LINE__); } while (0)
189037b3c26Smrg
190037b3c26Smrg#define VOID2U64(x) ((uint64_t)(unsigned long)(x))
191037b3c26Smrg
192037b3c26Smrgstatic inline void get_abs_timeout(struct drm_etnaviv_timespec *tv, uint64_t ns)
193037b3c26Smrg{
194037b3c26Smrg	struct timespec t;
195037b3c26Smrg	uint32_t s = ns / 1000000000;
196037b3c26Smrg	clock_gettime(CLOCK_MONOTONIC, &t);
197037b3c26Smrg	tv->tv_sec = t.tv_sec + s;
198037b3c26Smrg	tv->tv_nsec = t.tv_nsec + ns - (s * 1000000000);
199037b3c26Smrg}
200037b3c26Smrg
201037b3c26Smrg#endif /* ETNAVIV_PRIV_H_ */
202