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
50037b3c26Smrgstruct etna_bo_bucket {
51037b3c26Smrg	uint32_t size;
52037b3c26Smrg	struct list_head list;
53037b3c26Smrg};
54037b3c26Smrg
55037b3c26Smrgstruct etna_bo_cache {
56037b3c26Smrg	struct etna_bo_bucket cache_bucket[14 * 4];
57037b3c26Smrg	unsigned num_buckets;
58037b3c26Smrg	time_t time;
59037b3c26Smrg};
60037b3c26Smrg
61037b3c26Smrgstruct etna_device {
62037b3c26Smrg	int fd;
63037b3c26Smrg	atomic_t refcnt;
64037b3c26Smrg
65037b3c26Smrg	/* tables to keep track of bo's, to avoid "evil-twin" etna_bo objects:
66037b3c26Smrg	 *
67037b3c26Smrg	 *   handle_table: maps handle to etna_bo
68037b3c26Smrg	 *   name_table: maps flink name to etna_bo
69037b3c26Smrg	 *
70037b3c26Smrg	 * We end up needing two tables, because DRM_IOCTL_GEM_OPEN always
71037b3c26Smrg	 * returns a new handle.  So we need to figure out if the bo is already
72037b3c26Smrg	 * open in the process first, before calling gem-open.
73037b3c26Smrg	 */
74037b3c26Smrg	void *handle_table, *name_table;
75037b3c26Smrg
76037b3c26Smrg	struct etna_bo_cache bo_cache;
77037b3c26Smrg
78037b3c26Smrg	int closefd;        /* call close(fd) upon destruction */
79037b3c26Smrg};
80037b3c26Smrg
81037b3c26Smrgdrm_private void etna_bo_cache_init(struct etna_bo_cache *cache);
82037b3c26Smrgdrm_private void etna_bo_cache_cleanup(struct etna_bo_cache *cache, time_t time);
83037b3c26Smrgdrm_private struct etna_bo *etna_bo_cache_alloc(struct etna_bo_cache *cache,
84037b3c26Smrg		uint32_t *size, uint32_t flags);
85037b3c26Smrgdrm_private int etna_bo_cache_free(struct etna_bo_cache *cache, struct etna_bo *bo);
86037b3c26Smrg
87037b3c26Smrg/* for where @table_lock is already held: */
88037b3c26Smrgdrm_private void etna_device_del_locked(struct etna_device *dev);
89037b3c26Smrg
90037b3c26Smrg/* a GEM buffer object allocated from the DRM device */
91037b3c26Smrgstruct etna_bo {
92037b3c26Smrg	struct etna_device      *dev;
93037b3c26Smrg	void            *map;           /* userspace mmap'ing (if there is one) */
94037b3c26Smrg	uint32_t        size;
95037b3c26Smrg	uint32_t        handle;
96037b3c26Smrg	uint32_t        flags;
97037b3c26Smrg	uint32_t        name;           /* flink global handle (DRI2 name) */
98037b3c26Smrg	uint64_t        offset;         /* offset to mmap() */
99037b3c26Smrg	atomic_t        refcnt;
100037b3c26Smrg
101037b3c26Smrg	/* in the common case, a bo won't be referenced by more than a single
102037b3c26Smrg	 * command stream.  So to avoid looping over all the bo's in the
103037b3c26Smrg	 * reloc table to find the idx of a bo that might already be in the
104037b3c26Smrg	 * table, we cache the idx in the bo.  But in order to detect the
105037b3c26Smrg	 * slow-path where bo is ref'd in multiple streams, we also must track
106037b3c26Smrg	 * the current_stream for which the idx is valid.  See bo2idx().
107037b3c26Smrg	 */
108037b3c26Smrg	struct etna_cmd_stream *current_stream;
109037b3c26Smrg	uint32_t idx;
110037b3c26Smrg
111037b3c26Smrg	int reuse;
112037b3c26Smrg	struct list_head list;   /* bucket-list entry */
113037b3c26Smrg	time_t free_time;        /* time when added to bucket-list */
114037b3c26Smrg};
115037b3c26Smrg
116037b3c26Smrgstruct etna_gpu {
117037b3c26Smrg	struct etna_device *dev;
118037b3c26Smrg	uint32_t core;
119d8807b2fSmrg	uint32_t model;
120d8807b2fSmrg	uint32_t revision;
121037b3c26Smrg};
122037b3c26Smrg
123037b3c26Smrgstruct etna_pipe {
124037b3c26Smrg	enum etna_pipe_id id;
125037b3c26Smrg	struct etna_gpu *gpu;
126037b3c26Smrg};
127037b3c26Smrg
128037b3c26Smrgstruct etna_cmd_stream_priv {
129037b3c26Smrg	struct etna_cmd_stream base;
130037b3c26Smrg	struct etna_pipe *pipe;
131037b3c26Smrg
132037b3c26Smrg	uint32_t last_timestamp;
133037b3c26Smrg
134037b3c26Smrg	/* submit ioctl related tables: */
135037b3c26Smrg	struct {
136037b3c26Smrg		/* bo's table: */
137037b3c26Smrg		struct drm_etnaviv_gem_submit_bo *bos;
138037b3c26Smrg		uint32_t nr_bos, max_bos;
139037b3c26Smrg
140037b3c26Smrg		/* reloc's table: */
141037b3c26Smrg		struct drm_etnaviv_gem_submit_reloc *relocs;
142037b3c26Smrg		uint32_t nr_relocs, max_relocs;
14300a23bdaSmrg
14400a23bdaSmrg		/* perf's table: */
14500a23bdaSmrg		struct drm_etnaviv_gem_submit_pmr *pmrs;
14600a23bdaSmrg		uint32_t nr_pmrs, max_pmrs;
147037b3c26Smrg	} submit;
148037b3c26Smrg
149037b3c26Smrg	/* should have matching entries in submit.bos: */
150037b3c26Smrg	struct etna_bo **bos;
151037b3c26Smrg	uint32_t nr_bos, max_bos;
152037b3c26Smrg
1535324fb0dSmrg	/* notify callback if buffer reset happened */
154037b3c26Smrg	void (*reset_notify)(struct etna_cmd_stream *stream, void *priv);
155037b3c26Smrg	void *reset_notify_priv;
156037b3c26Smrg};
157037b3c26Smrg
15800a23bdaSmrgstruct etna_perfmon {
15900a23bdaSmrg	struct list_head domains;
16000a23bdaSmrg	struct etna_pipe *pipe;
16100a23bdaSmrg};
16200a23bdaSmrg
16300a23bdaSmrgstruct etna_perfmon_domain
16400a23bdaSmrg{
16500a23bdaSmrg	struct list_head head;
16600a23bdaSmrg	struct list_head signals;
16700a23bdaSmrg	uint8_t id;
16800a23bdaSmrg	char name[64];
16900a23bdaSmrg};
17000a23bdaSmrg
17100a23bdaSmrgstruct etna_perfmon_signal
17200a23bdaSmrg{
17300a23bdaSmrg	struct list_head head;
17400a23bdaSmrg	struct etna_perfmon_domain *domain;
17500a23bdaSmrg	uint8_t signal;
17600a23bdaSmrg	char name[64];
17700a23bdaSmrg};
17800a23bdaSmrg
179037b3c26Smrg#define ALIGN(v,a) (((v) + (a) - 1) & ~((a) - 1))
180037b3c26Smrg#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
181037b3c26Smrg
182037b3c26Smrg#define enable_debug 1  /* TODO make dynamic */
183037b3c26Smrg
184037b3c26Smrg#define INFO_MSG(fmt, ...) \
185037b3c26Smrg		do { drmMsg("[I] "fmt " (%s:%d)\n", \
186037b3c26Smrg				##__VA_ARGS__, __FUNCTION__, __LINE__); } while (0)
187037b3c26Smrg#define DEBUG_MSG(fmt, ...) \
188037b3c26Smrg		do if (enable_debug) { drmMsg("[D] "fmt " (%s:%d)\n", \
189037b3c26Smrg				##__VA_ARGS__, __FUNCTION__, __LINE__); } while (0)
190037b3c26Smrg#define WARN_MSG(fmt, ...) \
191037b3c26Smrg		do { drmMsg("[W] "fmt " (%s:%d)\n", \
192037b3c26Smrg				##__VA_ARGS__, __FUNCTION__, __LINE__); } while (0)
193037b3c26Smrg#define ERROR_MSG(fmt, ...) \
194037b3c26Smrg		do { drmMsg("[E] " fmt " (%s:%d)\n", \
195037b3c26Smrg				##__VA_ARGS__, __FUNCTION__, __LINE__); } while (0)
196037b3c26Smrg
197037b3c26Smrg#define VOID2U64(x) ((uint64_t)(unsigned long)(x))
198037b3c26Smrg
199037b3c26Smrgstatic inline void get_abs_timeout(struct drm_etnaviv_timespec *tv, uint64_t ns)
200037b3c26Smrg{
201037b3c26Smrg	struct timespec t;
202037b3c26Smrg	uint32_t s = ns / 1000000000;
203037b3c26Smrg	clock_gettime(CLOCK_MONOTONIC, &t);
204037b3c26Smrg	tv->tv_sec = t.tv_sec + s;
205037b3c26Smrg	tv->tv_nsec = t.tv_nsec + ns - (s * 1000000000);
206037b3c26Smrg}
207037b3c26Smrg
208037b3c26Smrg#endif /* ETNAVIV_PRIV_H_ */
209