etnaviv: fix a null pointer dereference
[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/macros.h"
45 #include "util/u_atomic.h"
46 #include "util/u_debug.h"
47
48 #include "etnaviv_drmif.h"
49 #include "drm-uapi/etnaviv_drm.h"
50
51 struct etna_bo_bucket {
52 uint32_t size;
53 struct list_head list;
54 };
55
56 struct etna_bo_cache {
57 struct etna_bo_bucket cache_bucket[14 * 4];
58 unsigned num_buckets;
59 time_t time;
60 };
61
62 struct etna_device {
63 int fd;
64 int refcnt;
65
66 /* tables to keep track of bo's, to avoid "evil-twin" etna_bo objects:
67 *
68 * handle_table: maps handle to etna_bo
69 * name_table: maps flink name to etna_bo
70 *
71 * We end up needing two tables, because DRM_IOCTL_GEM_OPEN always
72 * returns a new handle. So we need to figure out if the bo is already
73 * open in the process first, before calling gem-open.
74 */
75 void *handle_table, *name_table;
76
77 struct etna_bo_cache bo_cache;
78
79 int closefd; /* call close(fd) upon destruction */
80 };
81
82 void etna_bo_cache_init(struct etna_bo_cache *cache);
83 void etna_bo_cache_cleanup(struct etna_bo_cache *cache, time_t time);
84 struct etna_bo *etna_bo_cache_alloc(struct etna_bo_cache *cache,
85 uint32_t *size, uint32_t flags);
86 int etna_bo_cache_free(struct etna_bo_cache *cache, struct etna_bo *bo);
87
88 /* for where @etna_drm_table_lock is already held: */
89 void etna_device_del_locked(struct etna_device *dev);
90
91 /* a GEM buffer object allocated from the DRM device */
92 struct etna_bo {
93 struct etna_device *dev;
94 void *map; /* userspace mmap'ing (if there is one) */
95 uint32_t size;
96 uint32_t handle;
97 uint32_t flags;
98 uint32_t name; /* flink global handle (DRI2 name) */
99 uint64_t offset; /* offset to mmap() */
100 int refcnt;
101
102 /* in the common case, a bo won't be referenced by more than a single
103 * command stream. So to avoid looping over all the bo's in the
104 * reloc table to find the idx of a bo that might already be in the
105 * table, we cache the idx in the bo. But in order to detect the
106 * slow-path where bo is ref'd in multiple streams, we also must track
107 * the current_stream for which the idx is valid. See bo2idx().
108 */
109 struct etna_cmd_stream *current_stream;
110 uint32_t idx;
111
112 int reuse;
113 struct list_head list; /* bucket-list entry */
114 time_t free_time; /* time when added to bucket-list */
115 };
116
117 struct etna_gpu {
118 struct etna_device *dev;
119 uint32_t core;
120 uint32_t model;
121 uint32_t revision;
122 };
123
124 struct etna_pipe {
125 enum etna_pipe_id id;
126 struct etna_gpu *gpu;
127 };
128
129 struct etna_cmd_stream_priv {
130 struct etna_cmd_stream base;
131 struct etna_pipe *pipe;
132
133 uint32_t last_timestamp;
134
135 /* submit ioctl related tables: */
136 struct {
137 /* bo's table: */
138 struct drm_etnaviv_gem_submit_bo *bos;
139 uint32_t nr_bos, max_bos;
140
141 /* reloc's table: */
142 struct drm_etnaviv_gem_submit_reloc *relocs;
143 uint32_t nr_relocs, max_relocs;
144
145 /* perf's table: */
146 struct drm_etnaviv_gem_submit_pmr *pmrs;
147 uint32_t nr_pmrs, max_pmrs;
148 } submit;
149
150 /* should have matching entries in submit.bos: */
151 struct etna_bo **bos;
152 uint32_t nr_bos, max_bos;
153
154 /* notify callback if buffer reset happened */
155 void (*reset_notify)(struct etna_cmd_stream *stream, void *priv);
156 void *reset_notify_priv;
157 };
158
159 struct etna_perfmon {
160 struct list_head domains;
161 struct etna_pipe *pipe;
162 };
163
164 struct etna_perfmon_domain
165 {
166 struct list_head head;
167 struct list_head signals;
168 uint8_t id;
169 char name[64];
170 };
171
172 struct etna_perfmon_signal
173 {
174 struct list_head head;
175 struct etna_perfmon_domain *domain;
176 uint8_t signal;
177 char name[64];
178 };
179
180 #define ALIGN(v,a) (((v) + (a) - 1) & ~((a) - 1))
181
182 #define enable_debug 0 /* TODO make dynamic */
183
184 #define INFO_MSG(fmt, ...) \
185 do { debug_printf("[I] "fmt " (%s:%d)\n", \
186 ##__VA_ARGS__, __FUNCTION__, __LINE__); } while (0)
187 #define DEBUG_MSG(fmt, ...) \
188 do if (enable_debug) { debug_printf("[D] "fmt " (%s:%d)\n", \
189 ##__VA_ARGS__, __FUNCTION__, __LINE__); } while (0)
190 #define WARN_MSG(fmt, ...) \
191 do { debug_printf("[W] "fmt " (%s:%d)\n", \
192 ##__VA_ARGS__, __FUNCTION__, __LINE__); } while (0)
193 #define ERROR_MSG(fmt, ...) \
194 do { debug_printf("[E] " fmt " (%s:%d)\n", \
195 ##__VA_ARGS__, __FUNCTION__, __LINE__); } while (0)
196
197 #define VOID2U64(x) ((uint64_t)(unsigned long)(x))
198
199 static inline void get_abs_timeout(struct drm_etnaviv_timespec *tv, uint64_t ns)
200 {
201 struct timespec t;
202 uint32_t s = ns / 1000000000;
203 clock_gettime(CLOCK_MONOTONIC, &t);
204 tv->tv_sec = t.tv_sec + s;
205 tv->tv_nsec = t.tv_nsec + ns - (s * 1000000000);
206 }
207
208 #endif /* ETNAVIV_PRIV_H_ */