freedreno_priv.h revision 00a23bda
1e88f27b3Smrg/* -*- mode: C; c-file-style: "k&r"; tab-width 4; indent-tabs-mode: t; -*- */
2e88f27b3Smrg
3e88f27b3Smrg/*
4e88f27b3Smrg * Copyright (C) 2012 Rob Clark <robclark@freedesktop.org>
5e88f27b3Smrg *
6e88f27b3Smrg * Permission is hereby granted, free of charge, to any person obtaining a
7e88f27b3Smrg * copy of this software and associated documentation files (the "Software"),
8e88f27b3Smrg * to deal in the Software without restriction, including without limitation
9e88f27b3Smrg * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10e88f27b3Smrg * and/or sell copies of the Software, and to permit persons to whom the
11e88f27b3Smrg * Software is furnished to do so, subject to the following conditions:
12e88f27b3Smrg *
13e88f27b3Smrg * The above copyright notice and this permission notice (including the next
14e88f27b3Smrg * paragraph) shall be included in all copies or substantial portions of the
15e88f27b3Smrg * Software.
16e88f27b3Smrg *
17e88f27b3Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18e88f27b3Smrg * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19e88f27b3Smrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
20e88f27b3Smrg * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21e88f27b3Smrg * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22e88f27b3Smrg * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23e88f27b3Smrg * SOFTWARE.
24e88f27b3Smrg *
25e88f27b3Smrg * Authors:
26e88f27b3Smrg *    Rob Clark <robclark@freedesktop.org>
27e88f27b3Smrg */
28e88f27b3Smrg
29e88f27b3Smrg#ifndef FREEDRENO_PRIV_H_
30e88f27b3Smrg#define FREEDRENO_PRIV_H_
31e88f27b3Smrg
32baaff307Smrg#ifdef HAVE_CONFIG_H
33baaff307Smrg#include "config.h"
34baaff307Smrg#endif
35baaff307Smrg
36e88f27b3Smrg#include <stdlib.h>
37e88f27b3Smrg#include <errno.h>
38e88f27b3Smrg#include <string.h>
39e88f27b3Smrg#include <unistd.h>
40e88f27b3Smrg#include <errno.h>
41e88f27b3Smrg#include <fcntl.h>
42e88f27b3Smrg#include <sys/ioctl.h>
43e88f27b3Smrg#include <pthread.h>
44e88f27b3Smrg#include <stdio.h>
45e88f27b3Smrg#include <assert.h>
46e88f27b3Smrg
47e6188e58Smrg#include "libdrm_macros.h"
48e88f27b3Smrg#include "xf86drm.h"
49e88f27b3Smrg#include "xf86atomic.h"
50e88f27b3Smrg
513f012e29Smrg#include "util_double_list.h"
5200a23bdaSmrg#include "util_math.h"
53e88f27b3Smrg
54e88f27b3Smrg#include "freedreno_drmif.h"
55e88f27b3Smrg#include "freedreno_ringbuffer.h"
56e88f27b3Smrg#include "drm.h"
57e88f27b3Smrg
583f012e29Smrg#ifndef TRUE
593f012e29Smrg#  define TRUE 1
603f012e29Smrg#endif
613f012e29Smrg#ifndef FALSE
623f012e29Smrg#  define FALSE 0
633f012e29Smrg#endif
643f012e29Smrg
65e88f27b3Smrgstruct fd_device_funcs {
66e88f27b3Smrg	int (*bo_new_handle)(struct fd_device *dev, uint32_t size,
67e88f27b3Smrg			uint32_t flags, uint32_t *handle);
68e88f27b3Smrg	struct fd_bo * (*bo_from_handle)(struct fd_device *dev,
69e88f27b3Smrg			uint32_t size, uint32_t handle);
7000a23bdaSmrg	struct fd_pipe * (*pipe_new)(struct fd_device *dev, enum fd_pipe_id id,
7100a23bdaSmrg			unsigned prio);
72e88f27b3Smrg	void (*destroy)(struct fd_device *dev);
73e88f27b3Smrg};
74e88f27b3Smrg
75e88f27b3Smrgstruct fd_bo_bucket {
76e88f27b3Smrg	uint32_t size;
77e88f27b3Smrg	struct list_head list;
78e88f27b3Smrg};
79e88f27b3Smrg
803f012e29Smrgstruct fd_bo_cache {
813f012e29Smrg	struct fd_bo_bucket cache_bucket[14 * 4];
823f012e29Smrg	int num_buckets;
833f012e29Smrg	time_t time;
843f012e29Smrg};
853f012e29Smrg
86e88f27b3Smrgstruct fd_device {
87e88f27b3Smrg	int fd;
883f012e29Smrg	enum fd_version version;
89e88f27b3Smrg	atomic_t refcnt;
90e88f27b3Smrg
91e88f27b3Smrg	/* tables to keep track of bo's, to avoid "evil-twin" fd_bo objects:
92e88f27b3Smrg	 *
93e88f27b3Smrg	 *   handle_table: maps handle to fd_bo
94e88f27b3Smrg	 *   name_table: maps flink name to fd_bo
95e88f27b3Smrg	 *
96e88f27b3Smrg	 * We end up needing two tables, because DRM_IOCTL_GEM_OPEN always
97e88f27b3Smrg	 * returns a new handle.  So we need to figure out if the bo is already
98e88f27b3Smrg	 * open in the process first, before calling gem-open.
99e88f27b3Smrg	 */
100e88f27b3Smrg	void *handle_table, *name_table;
101e88f27b3Smrg
1023f012e29Smrg	const struct fd_device_funcs *funcs;
103e88f27b3Smrg
1043f012e29Smrg	struct fd_bo_cache bo_cache;
105e88f27b3Smrg
106e88f27b3Smrg	int closefd;        /* call close(fd) upon destruction */
107d8807b2fSmrg
108d8807b2fSmrg	/* just for valgrind: */
109d8807b2fSmrg	int bo_size;
110e88f27b3Smrg};
111e88f27b3Smrg
1123f012e29Smrgdrm_private void fd_bo_cache_init(struct fd_bo_cache *cache, int coarse);
1133f012e29Smrgdrm_private void fd_bo_cache_cleanup(struct fd_bo_cache *cache, time_t time);
1143f012e29Smrgdrm_private struct fd_bo * fd_bo_cache_alloc(struct fd_bo_cache *cache,
1153f012e29Smrg		uint32_t *size, uint32_t flags);
1163f012e29Smrgdrm_private int fd_bo_cache_free(struct fd_bo_cache *cache, struct fd_bo *bo);
117e88f27b3Smrg
118e88f27b3Smrg/* for where @table_lock is already held: */
119e6188e58Smrgdrm_private void fd_device_del_locked(struct fd_device *dev);
120e88f27b3Smrg
121e88f27b3Smrgstruct fd_pipe_funcs {
122e88f27b3Smrg	struct fd_ringbuffer * (*ringbuffer_new)(struct fd_pipe *pipe, uint32_t size);
123e88f27b3Smrg	int (*get_param)(struct fd_pipe *pipe, enum fd_param_id param, uint64_t *value);
1243f012e29Smrg	int (*wait)(struct fd_pipe *pipe, uint32_t timestamp, uint64_t timeout);
125e88f27b3Smrg	void (*destroy)(struct fd_pipe *pipe);
126e88f27b3Smrg};
127e88f27b3Smrg
128e88f27b3Smrgstruct fd_pipe {
129e88f27b3Smrg	struct fd_device *dev;
130e88f27b3Smrg	enum fd_pipe_id id;
131037b3c26Smrg	uint32_t gpu_id;
1323f012e29Smrg	const struct fd_pipe_funcs *funcs;
133e88f27b3Smrg};
134e88f27b3Smrg
135e88f27b3Smrgstruct fd_ringmarker {
136e88f27b3Smrg	struct fd_ringbuffer *ring;
137e88f27b3Smrg	uint32_t *cur;
138e88f27b3Smrg};
139e88f27b3Smrg
140e88f27b3Smrgstruct fd_ringbuffer_funcs {
141e88f27b3Smrg	void * (*hostptr)(struct fd_ringbuffer *ring);
142037b3c26Smrg	int (*flush)(struct fd_ringbuffer *ring, uint32_t *last_start,
143037b3c26Smrg			int in_fence_fd, int *out_fence_fd);
1443f012e29Smrg	void (*grow)(struct fd_ringbuffer *ring, uint32_t size);
145857b0bc6Smrg	void (*reset)(struct fd_ringbuffer *ring);
146e88f27b3Smrg	void (*emit_reloc)(struct fd_ringbuffer *ring,
147e88f27b3Smrg			const struct fd_reloc *reloc);
1483f012e29Smrg	uint32_t (*emit_reloc_ring)(struct fd_ringbuffer *ring,
1493f012e29Smrg			struct fd_ringbuffer *target, uint32_t cmd_idx,
1503f012e29Smrg			uint32_t submit_offset, uint32_t size);
1513f012e29Smrg	uint32_t (*cmd_count)(struct fd_ringbuffer *ring);
152e88f27b3Smrg	void (*destroy)(struct fd_ringbuffer *ring);
153e88f27b3Smrg};
154e88f27b3Smrg
155e88f27b3Smrgstruct fd_bo_funcs {
156e88f27b3Smrg	int (*offset)(struct fd_bo *bo, uint64_t *offset);
157e88f27b3Smrg	int (*cpu_prep)(struct fd_bo *bo, struct fd_pipe *pipe, uint32_t op);
158e88f27b3Smrg	void (*cpu_fini)(struct fd_bo *bo);
1593f012e29Smrg	int (*madvise)(struct fd_bo *bo, int willneed);
16000a23bdaSmrg	uint64_t (*iova)(struct fd_bo *bo);
161e88f27b3Smrg	void (*destroy)(struct fd_bo *bo);
162e88f27b3Smrg};
163e88f27b3Smrg
164e88f27b3Smrgstruct fd_bo {
165e88f27b3Smrg	struct fd_device *dev;
166e88f27b3Smrg	uint32_t size;
167e88f27b3Smrg	uint32_t handle;
168e88f27b3Smrg	uint32_t name;
169e88f27b3Smrg	void *map;
170e88f27b3Smrg	atomic_t refcnt;
1713f012e29Smrg	const struct fd_bo_funcs *funcs;
172e88f27b3Smrg
173e88f27b3Smrg	int bo_reuse;
174e88f27b3Smrg	struct list_head list;   /* bucket-list entry */
175e88f27b3Smrg	time_t free_time;        /* time when added to bucket-list */
176e88f27b3Smrg};
177e88f27b3Smrg
178e88f27b3Smrg#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
179e88f27b3Smrg
180e88f27b3Smrg#define enable_debug 0  /* TODO make dynamic */
181e88f27b3Smrg
182e88f27b3Smrg#define INFO_MSG(fmt, ...) \
183e88f27b3Smrg		do { drmMsg("[I] "fmt " (%s:%d)\n", \
184e88f27b3Smrg				##__VA_ARGS__, __FUNCTION__, __LINE__); } while (0)
185e88f27b3Smrg#define DEBUG_MSG(fmt, ...) \
186e88f27b3Smrg		do if (enable_debug) { drmMsg("[D] "fmt " (%s:%d)\n", \
187e88f27b3Smrg				##__VA_ARGS__, __FUNCTION__, __LINE__); } while (0)
188e88f27b3Smrg#define WARN_MSG(fmt, ...) \
189e88f27b3Smrg		do { drmMsg("[W] "fmt " (%s:%d)\n", \
190e88f27b3Smrg				##__VA_ARGS__, __FUNCTION__, __LINE__); } while (0)
191e88f27b3Smrg#define ERROR_MSG(fmt, ...) \
192e88f27b3Smrg		do { drmMsg("[E] " fmt " (%s:%d)\n", \
193e88f27b3Smrg				##__VA_ARGS__, __FUNCTION__, __LINE__); } while (0)
194e88f27b3Smrg
195e88f27b3Smrg#define U642VOID(x) ((void *)(unsigned long)(x))
196e88f27b3Smrg#define VOID2U64(x) ((uint64_t)(unsigned long)(x))
197e88f27b3Smrg
1983f012e29Smrgstatic inline uint32_t
1993f012e29Smrgoffset_bytes(void *end, void *start)
2003f012e29Smrg{
2013f012e29Smrg	return ((char *)end) - ((char *)start);
2023f012e29Smrg}
2033f012e29Smrg
20400a23bdaSmrg#if HAVE_VALGRIND
205d8807b2fSmrg#  include <memcheck.h>
206d8807b2fSmrg
207d8807b2fSmrg/*
208d8807b2fSmrg * For tracking the backing memory (if valgrind enabled, we force a mmap
209d8807b2fSmrg * for the purposes of tracking)
210d8807b2fSmrg */
211d8807b2fSmrgstatic inline void VG_BO_ALLOC(struct fd_bo *bo)
212d8807b2fSmrg{
213d8807b2fSmrg	if (bo && RUNNING_ON_VALGRIND) {
214d8807b2fSmrg		VALGRIND_MALLOCLIKE_BLOCK(fd_bo_map(bo), bo->size, 0, 1);
215d8807b2fSmrg	}
216d8807b2fSmrg}
217d8807b2fSmrg
218d8807b2fSmrgstatic inline void VG_BO_FREE(struct fd_bo *bo)
219d8807b2fSmrg{
220d8807b2fSmrg	VALGRIND_FREELIKE_BLOCK(bo->map, 0);
221d8807b2fSmrg}
222d8807b2fSmrg
223d8807b2fSmrg/*
224d8807b2fSmrg * For tracking bo structs that are in the buffer-cache, so that valgrind
225d8807b2fSmrg * doesn't attribute ownership to the first one to allocate the recycled
226d8807b2fSmrg * bo.
227d8807b2fSmrg *
228d8807b2fSmrg * Note that the list_head in fd_bo is used to track the buffers in cache
229d8807b2fSmrg * so disable error reporting on the range while they are in cache so
230d8807b2fSmrg * valgrind doesn't squawk about list traversal.
231d8807b2fSmrg *
232d8807b2fSmrg */
233d8807b2fSmrgstatic inline void VG_BO_RELEASE(struct fd_bo *bo)
234d8807b2fSmrg{
235d8807b2fSmrg	if (RUNNING_ON_VALGRIND) {
236d8807b2fSmrg		VALGRIND_DISABLE_ADDR_ERROR_REPORTING_IN_RANGE(bo, bo->dev->bo_size);
237d8807b2fSmrg		VALGRIND_MAKE_MEM_NOACCESS(bo, bo->dev->bo_size);
238d8807b2fSmrg		VALGRIND_FREELIKE_BLOCK(bo->map, 0);
239d8807b2fSmrg	}
240d8807b2fSmrg}
241d8807b2fSmrgstatic inline void VG_BO_OBTAIN(struct fd_bo *bo)
242d8807b2fSmrg{
243d8807b2fSmrg	if (RUNNING_ON_VALGRIND) {
244d8807b2fSmrg		VALGRIND_MAKE_MEM_DEFINED(bo, bo->dev->bo_size);
245d8807b2fSmrg		VALGRIND_ENABLE_ADDR_ERROR_REPORTING_IN_RANGE(bo, bo->dev->bo_size);
246d8807b2fSmrg		VALGRIND_MALLOCLIKE_BLOCK(bo->map, bo->size, 0, 1);
247d8807b2fSmrg	}
248d8807b2fSmrg}
249d8807b2fSmrg#else
250d8807b2fSmrgstatic inline void VG_BO_ALLOC(struct fd_bo *bo)   {}
251d8807b2fSmrgstatic inline void VG_BO_FREE(struct fd_bo *bo)    {}
252d8807b2fSmrgstatic inline void VG_BO_RELEASE(struct fd_bo *bo) {}
253d8807b2fSmrgstatic inline void VG_BO_OBTAIN(struct fd_bo *bo)  {}
254d8807b2fSmrg#endif
255d8807b2fSmrg
256d8807b2fSmrg
257e88f27b3Smrg#endif /* FREEDRENO_PRIV_H_ */
258