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