etnaviv: drm: Use mesa's atomic definitions
[mesa.git] / src / etnaviv / drm / etnaviv_priv.h
1 /*
2 * Copyright (C) 2014-2015 Etnaviv Project
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 * SOFTWARE.
22 *
23 * Authors:
24 * Christian Gmeiner <christian.gmeiner@gmail.com>
25 */
26
27 #ifndef ETNAVIV_PRIV_H_
28 #define ETNAVIV_PRIV_H_
29
30 #include <stdlib.h>
31 #include <errno.h>
32 #include <string.h>
33 #include <unistd.h>
34 #include <errno.h>
35 #include <fcntl.h>
36 #include <sys/ioctl.h>
37 #include <pthread.h>
38 #include <stdio.h>
39 #include <assert.h>
40
41 #include <xf86drm.h>
42
43 #include "util/list.h"
44 #include "util/u_atomic.h"
45
46 #include "etnaviv_drmif.h"
47 #include "etnaviv_drm.h"
48
49 struct etna_bo_bucket {
50 uint32_t size;
51 struct list_head list;
52 };
53
54 struct etna_bo_cache {
55 struct etna_bo_bucket cache_bucket[14 * 4];
56 unsigned num_buckets;
57 time_t time;
58 };
59
60 struct etna_device {
61 int fd;
62 int refcnt;
63
64 /* tables to keep track of bo's, to avoid "evil-twin" etna_bo objects:
65 *
66 * handle_table: maps handle to etna_bo
67 * name_table: maps flink name to etna_bo
68 *
69 * We end up needing two tables, because DRM_IOCTL_GEM_OPEN always
70 * returns a new handle. So we need to figure out if the bo is already
71 * open in the process first, before calling gem-open.
72 */
73 void *handle_table, *name_table;
74
75 struct etna_bo_cache bo_cache;
76
77 int closefd; /* call close(fd) upon destruction */
78 };
79
80 void etna_bo_cache_init(struct etna_bo_cache *cache);
81 void etna_bo_cache_cleanup(struct etna_bo_cache *cache, time_t time);
82 struct etna_bo *etna_bo_cache_alloc(struct etna_bo_cache *cache,
83 uint32_t *size, uint32_t flags);
84 int etna_bo_cache_free(struct etna_bo_cache *cache, struct etna_bo *bo);
85
86 /* for where @table_lock is already held: */
87 void etna_device_del_locked(struct etna_device *dev);
88
89 /* a GEM buffer object allocated from the DRM device */
90 struct etna_bo {
91 struct etna_device *dev;
92 void *map; /* userspace mmap'ing (if there is one) */
93 uint32_t size;
94 uint32_t handle;
95 uint32_t flags;
96 uint32_t name; /* flink global handle (DRI2 name) */
97 uint64_t offset; /* offset to mmap() */
98 int refcnt;
99
100 /* in the common case, a bo won't be referenced by more than a single
101 * command stream. So to avoid looping over all the bo's in the
102 * reloc table to find the idx of a bo that might already be in the
103 * table, we cache the idx in the bo. But in order to detect the
104 * slow-path where bo is ref'd in multiple streams, we also must track
105 * the current_stream for which the idx is valid. See bo2idx().
106 */
107 struct etna_cmd_stream *current_stream;
108 uint32_t idx;
109
110 int reuse;
111 struct list_head list; /* bucket-list entry */
112 time_t free_time; /* time when added to bucket-list */
113 };
114
115 struct etna_gpu {
116 struct etna_device *dev;
117 uint32_t core;
118 uint32_t model;
119 uint32_t revision;
120 };
121
122 struct etna_pipe {
123 enum etna_pipe_id id;
124 struct etna_gpu *gpu;
125 };
126
127 struct etna_cmd_stream_priv {
128 struct etna_cmd_stream base;
129 struct etna_pipe *pipe;
130
131 uint32_t last_timestamp;
132
133 /* submit ioctl related tables: */
134 struct {
135 /* bo's table: */
136 struct drm_etnaviv_gem_submit_bo *bos;
137 uint32_t nr_bos, max_bos;
138
139 /* reloc's table: */
140 struct drm_etnaviv_gem_submit_reloc *relocs;
141 uint32_t nr_relocs, max_relocs;
142
143 /* perf's table: */
144 struct drm_etnaviv_gem_submit_pmr *pmrs;
145 uint32_t nr_pmrs, max_pmrs;
146 } submit;
147
148 /* should have matching entries in submit.bos: */
149 struct etna_bo **bos;
150 uint32_t nr_bos, max_bos;
151
152 /* notify callback if buffer reset happened */
153 void (*reset_notify)(struct etna_cmd_stream *stream, void *priv);
154 void *reset_notify_priv;
155 };
156
157 struct etna_perfmon {
158 struct list_head domains;
159 struct etna_pipe *pipe;
160 };
161
162 struct etna_perfmon_domain
163 {
164 struct list_head head;
165 struct list_head signals;
166 uint8_t id;
167 char name[64];
168 };
169
170 struct etna_perfmon_signal
171 {
172 struct list_head head;
173 struct etna_perfmon_domain *domain;
174 uint8_t signal;
175 char name[64];
176 };
177
178 #define ALIGN(v,a) (((v) + (a) - 1) & ~((a) - 1))
179 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
180
181 #define enable_debug 1 /* TODO make dynamic */
182
183 #define INFO_MSG(fmt, ...) \
184 do { drmMsg("[I] "fmt " (%s:%d)\n", \
185 ##__VA_ARGS__, __FUNCTION__, __LINE__); } while (0)
186 #define DEBUG_MSG(fmt, ...) \
187 do if (enable_debug) { drmMsg("[D] "fmt " (%s:%d)\n", \
188 ##__VA_ARGS__, __FUNCTION__, __LINE__); } while (0)
189 #define WARN_MSG(fmt, ...) \
190 do { drmMsg("[W] "fmt " (%s:%d)\n", \
191 ##__VA_ARGS__, __FUNCTION__, __LINE__); } while (0)
192 #define ERROR_MSG(fmt, ...) \
193 do { drmMsg("[E] " fmt " (%s:%d)\n", \
194 ##__VA_ARGS__, __FUNCTION__, __LINE__); } while (0)
195
196 #define VOID2U64(x) ((uint64_t)(unsigned long)(x))
197
198 static inline void get_abs_timeout(struct drm_etnaviv_timespec *tv, uint64_t ns)
199 {
200 struct timespec t;
201 uint32_t s = ns / 1000000000;
202 clock_gettime(CLOCK_MONOTONIC, &t);
203 tv->tv_sec = t.tv_sec + s;
204 tv->tv_nsec = t.tv_nsec + ns - (s * 1000000000);
205 }
206
207 #endif /* ETNAVIV_PRIV_H_ */