• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* -*- mode: C; c-file-style: "k&r"; tab-width 4; indent-tabs-mode: t; -*- */
2 
3 /*
4  * Copyright (C) 2012 Rob Clark <robclark@freedesktop.org>
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice (including the next
14  * paragraph) shall be included in all copies or substantial portions of the
15  * Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
20  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23  * SOFTWARE.
24  *
25  * Authors:
26  *    Rob Clark <robclark@freedesktop.org>
27  */
28 
29 #ifndef FREEDRENO_PRIV_H_
30 #define FREEDRENO_PRIV_H_
31 
32 #ifdef HAVE_CONFIG_H
33 #include "config.h"
34 #endif
35 
36 #include <stdlib.h>
37 #include <errno.h>
38 #include <string.h>
39 #include <unistd.h>
40 #include <errno.h>
41 #include <fcntl.h>
42 #include <sys/ioctl.h>
43 #include <pthread.h>
44 #include <stdio.h>
45 #include <assert.h>
46 
47 #include "libdrm_macros.h"
48 #include "xf86drm.h"
49 #include "xf86atomic.h"
50 
51 #include "util_double_list.h"
52 
53 #include "freedreno_drmif.h"
54 #include "freedreno_ringbuffer.h"
55 #include "drm.h"
56 
57 #ifndef TRUE
58 #  define TRUE 1
59 #endif
60 #ifndef FALSE
61 #  define FALSE 0
62 #endif
63 
64 struct fd_device_funcs {
65 	int (*bo_new_handle)(struct fd_device *dev, uint32_t size,
66 			uint32_t flags, uint32_t *handle);
67 	struct fd_bo * (*bo_from_handle)(struct fd_device *dev,
68 			uint32_t size, uint32_t handle);
69 	struct fd_pipe * (*pipe_new)(struct fd_device *dev, enum fd_pipe_id id);
70 	void (*destroy)(struct fd_device *dev);
71 };
72 
73 struct fd_bo_bucket {
74 	uint32_t size;
75 	struct list_head list;
76 };
77 
78 struct fd_bo_cache {
79 	struct fd_bo_bucket cache_bucket[14 * 4];
80 	int num_buckets;
81 	time_t time;
82 };
83 
84 struct fd_device {
85 	int fd;
86 	enum fd_version version;
87 	atomic_t refcnt;
88 
89 	/* tables to keep track of bo's, to avoid "evil-twin" fd_bo objects:
90 	 *
91 	 *   handle_table: maps handle to fd_bo
92 	 *   name_table: maps flink name to fd_bo
93 	 *
94 	 * We end up needing two tables, because DRM_IOCTL_GEM_OPEN always
95 	 * returns a new handle.  So we need to figure out if the bo is already
96 	 * open in the process first, before calling gem-open.
97 	 */
98 	void *handle_table, *name_table;
99 
100 	const struct fd_device_funcs *funcs;
101 
102 	struct fd_bo_cache bo_cache;
103 
104 	int closefd;        /* call close(fd) upon destruction */
105 };
106 
107 drm_private void fd_bo_cache_init(struct fd_bo_cache *cache, int coarse);
108 drm_private void fd_bo_cache_cleanup(struct fd_bo_cache *cache, time_t time);
109 drm_private struct fd_bo * fd_bo_cache_alloc(struct fd_bo_cache *cache,
110 		uint32_t *size, uint32_t flags);
111 drm_private int fd_bo_cache_free(struct fd_bo_cache *cache, struct fd_bo *bo);
112 
113 /* for where @table_lock is already held: */
114 drm_private void fd_device_del_locked(struct fd_device *dev);
115 
116 struct fd_pipe_funcs {
117 	struct fd_ringbuffer * (*ringbuffer_new)(struct fd_pipe *pipe, uint32_t size);
118 	int (*get_param)(struct fd_pipe *pipe, enum fd_param_id param, uint64_t *value);
119 	int (*wait)(struct fd_pipe *pipe, uint32_t timestamp, uint64_t timeout);
120 	void (*destroy)(struct fd_pipe *pipe);
121 };
122 
123 struct fd_pipe {
124 	struct fd_device *dev;
125 	enum fd_pipe_id id;
126 	uint32_t gpu_id;
127 	const struct fd_pipe_funcs *funcs;
128 };
129 
130 struct fd_ringmarker {
131 	struct fd_ringbuffer *ring;
132 	uint32_t *cur;
133 };
134 
135 struct fd_ringbuffer_funcs {
136 	void * (*hostptr)(struct fd_ringbuffer *ring);
137 	int (*flush)(struct fd_ringbuffer *ring, uint32_t *last_start,
138 			int in_fence_fd, int *out_fence_fd);
139 	void (*grow)(struct fd_ringbuffer *ring, uint32_t size);
140 	void (*reset)(struct fd_ringbuffer *ring);
141 	void (*emit_reloc)(struct fd_ringbuffer *ring,
142 			const struct fd_reloc *reloc);
143 	uint32_t (*emit_reloc_ring)(struct fd_ringbuffer *ring,
144 			struct fd_ringbuffer *target, uint32_t cmd_idx,
145 			uint32_t submit_offset, uint32_t size);
146 	uint32_t (*cmd_count)(struct fd_ringbuffer *ring);
147 	void (*destroy)(struct fd_ringbuffer *ring);
148 };
149 
150 struct fd_bo_funcs {
151 	int (*offset)(struct fd_bo *bo, uint64_t *offset);
152 	int (*cpu_prep)(struct fd_bo *bo, struct fd_pipe *pipe, uint32_t op);
153 	void (*cpu_fini)(struct fd_bo *bo);
154 	int (*madvise)(struct fd_bo *bo, int willneed);
155 	void (*destroy)(struct fd_bo *bo);
156 };
157 
158 struct fd_bo {
159 	struct fd_device *dev;
160 	uint32_t size;
161 	uint32_t handle;
162 	uint32_t name;
163 	void *map;
164 	atomic_t refcnt;
165 	const struct fd_bo_funcs *funcs;
166 
167 	int bo_reuse;
168 	struct list_head list;   /* bucket-list entry */
169 	time_t free_time;        /* time when added to bucket-list */
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 0  /* 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 U642VOID(x) ((void *)(unsigned long)(x))
191 #define VOID2U64(x) ((uint64_t)(unsigned long)(x))
192 
193 static inline uint32_t
offset_bytes(void * end,void * start)194 offset_bytes(void *end, void *start)
195 {
196 	return ((char *)end) - ((char *)start);
197 }
198 
199 #endif /* FREEDRENO_PRIV_H_ */
200