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
32e88f27b3Smrg#include <stdlib.h>
33e88f27b3Smrg#include <errno.h>
34e88f27b3Smrg#include <string.h>
35e88f27b3Smrg#include <unistd.h>
36e88f27b3Smrg#include <errno.h>
37e88f27b3Smrg#include <fcntl.h>
38e88f27b3Smrg#include <sys/ioctl.h>
39e88f27b3Smrg#include <pthread.h>
40e88f27b3Smrg#include <stdio.h>
41e88f27b3Smrg#include <assert.h>
42e88f27b3Smrg
43e6188e58Smrg#include "libdrm_macros.h"
44e88f27b3Smrg#include "xf86drm.h"
45e88f27b3Smrg#include "xf86atomic.h"
46e88f27b3Smrg
473f012e29Smrg#include "util_double_list.h"
4800a23bdaSmrg#include "util_math.h"
49e88f27b3Smrg
50e88f27b3Smrg#include "freedreno_drmif.h"
51e88f27b3Smrg#include "freedreno_ringbuffer.h"
52e88f27b3Smrg#include "drm.h"
53e88f27b3Smrg
543f012e29Smrg#ifndef TRUE
553f012e29Smrg#  define TRUE 1
563f012e29Smrg#endif
573f012e29Smrg#ifndef FALSE
583f012e29Smrg#  define FALSE 0
593f012e29Smrg#endif
603f012e29Smrg
61e88f27b3Smrgstruct fd_device_funcs {
62e88f27b3Smrg	int (*bo_new_handle)(struct fd_device *dev, uint32_t size,
63e88f27b3Smrg			uint32_t flags, uint32_t *handle);
64e88f27b3Smrg	struct fd_bo * (*bo_from_handle)(struct fd_device *dev,
65e88f27b3Smrg			uint32_t size, uint32_t handle);
6600a23bdaSmrg	struct fd_pipe * (*pipe_new)(struct fd_device *dev, enum fd_pipe_id id,
6700a23bdaSmrg			unsigned prio);
68e88f27b3Smrg	void (*destroy)(struct fd_device *dev);
69e88f27b3Smrg};
70e88f27b3Smrg
71e88f27b3Smrgstruct fd_bo_bucket {
72e88f27b3Smrg	uint32_t size;
73e88f27b3Smrg	struct list_head list;
74e88f27b3Smrg};
75e88f27b3Smrg
763f012e29Smrgstruct fd_bo_cache {
773f012e29Smrg	struct fd_bo_bucket cache_bucket[14 * 4];
783f012e29Smrg	int num_buckets;
793f012e29Smrg	time_t time;
803f012e29Smrg};
813f012e29Smrg
82e88f27b3Smrgstruct fd_device {
83e88f27b3Smrg	int fd;
843f012e29Smrg	enum fd_version version;
85e88f27b3Smrg	atomic_t refcnt;
86e88f27b3Smrg
87e88f27b3Smrg	/* tables to keep track of bo's, to avoid "evil-twin" fd_bo objects:
88e88f27b3Smrg	 *
89e88f27b3Smrg	 *   handle_table: maps handle to fd_bo
90e88f27b3Smrg	 *   name_table: maps flink name to fd_bo
91e88f27b3Smrg	 *
92e88f27b3Smrg	 * We end up needing two tables, because DRM_IOCTL_GEM_OPEN always
93e88f27b3Smrg	 * returns a new handle.  So we need to figure out if the bo is already
94e88f27b3Smrg	 * open in the process first, before calling gem-open.
95e88f27b3Smrg	 */
96e88f27b3Smrg	void *handle_table, *name_table;
97e88f27b3Smrg
983f012e29Smrg	const struct fd_device_funcs *funcs;
99e88f27b3Smrg
1003f012e29Smrg	struct fd_bo_cache bo_cache;
1017cdc0497Smrg	struct fd_bo_cache ring_cache;
102e88f27b3Smrg
103e88f27b3Smrg	int closefd;        /* call close(fd) upon destruction */
104d8807b2fSmrg
105d8807b2fSmrg	/* just for valgrind: */
106d8807b2fSmrg	int bo_size;
107e88f27b3Smrg};
108e88f27b3Smrg
1093f012e29Smrgdrm_private void fd_bo_cache_init(struct fd_bo_cache *cache, int coarse);
1103f012e29Smrgdrm_private void fd_bo_cache_cleanup(struct fd_bo_cache *cache, time_t time);
1113f012e29Smrgdrm_private struct fd_bo * fd_bo_cache_alloc(struct fd_bo_cache *cache,
1123f012e29Smrg		uint32_t *size, uint32_t flags);
1133f012e29Smrgdrm_private int fd_bo_cache_free(struct fd_bo_cache *cache, struct fd_bo *bo);
114e88f27b3Smrg
115e88f27b3Smrg/* for where @table_lock is already held: */
116e6188e58Smrgdrm_private void fd_device_del_locked(struct fd_device *dev);
117e88f27b3Smrg
118e88f27b3Smrgstruct fd_pipe_funcs {
1197cdc0497Smrg	struct fd_ringbuffer * (*ringbuffer_new)(struct fd_pipe *pipe, uint32_t size,
1207cdc0497Smrg			enum fd_ringbuffer_flags flags);
121e88f27b3Smrg	int (*get_param)(struct fd_pipe *pipe, enum fd_param_id param, uint64_t *value);
1223f012e29Smrg	int (*wait)(struct fd_pipe *pipe, uint32_t timestamp, uint64_t timeout);
123e88f27b3Smrg	void (*destroy)(struct fd_pipe *pipe);
124e88f27b3Smrg};
125e88f27b3Smrg
126e88f27b3Smrgstruct fd_pipe {
127e88f27b3Smrg	struct fd_device *dev;
128e88f27b3Smrg	enum fd_pipe_id id;
129037b3c26Smrg	uint32_t gpu_id;
1307cdc0497Smrg	atomic_t refcnt;
1313f012e29Smrg	const struct fd_pipe_funcs *funcs;
132e88f27b3Smrg};
133e88f27b3Smrg
134e88f27b3Smrgstruct fd_ringbuffer_funcs {
135e88f27b3Smrg	void * (*hostptr)(struct fd_ringbuffer *ring);
136037b3c26Smrg	int (*flush)(struct fd_ringbuffer *ring, uint32_t *last_start,
137037b3c26Smrg			int in_fence_fd, int *out_fence_fd);
1383f012e29Smrg	void (*grow)(struct fd_ringbuffer *ring, uint32_t size);
139857b0bc6Smrg	void (*reset)(struct fd_ringbuffer *ring);
140e88f27b3Smrg	void (*emit_reloc)(struct fd_ringbuffer *ring,
141e88f27b3Smrg			const struct fd_reloc *reloc);
1423f012e29Smrg	uint32_t (*emit_reloc_ring)(struct fd_ringbuffer *ring,
1437cdc0497Smrg			struct fd_ringbuffer *target, uint32_t cmd_idx);
1443f012e29Smrg	uint32_t (*cmd_count)(struct fd_ringbuffer *ring);
145e88f27b3Smrg	void (*destroy)(struct fd_ringbuffer *ring);
146e88f27b3Smrg};
147e88f27b3Smrg
148e88f27b3Smrgstruct fd_bo_funcs {
149e88f27b3Smrg	int (*offset)(struct fd_bo *bo, uint64_t *offset);
150e88f27b3Smrg	int (*cpu_prep)(struct fd_bo *bo, struct fd_pipe *pipe, uint32_t op);
151e88f27b3Smrg	void (*cpu_fini)(struct fd_bo *bo);
1523f012e29Smrg	int (*madvise)(struct fd_bo *bo, int willneed);
15300a23bdaSmrg	uint64_t (*iova)(struct fd_bo *bo);
154e88f27b3Smrg	void (*destroy)(struct fd_bo *bo);
155e88f27b3Smrg};
156e88f27b3Smrg
157e88f27b3Smrgstruct fd_bo {
158e88f27b3Smrg	struct fd_device *dev;
159e88f27b3Smrg	uint32_t size;
160e88f27b3Smrg	uint32_t handle;
161e88f27b3Smrg	uint32_t name;
162e88f27b3Smrg	void *map;
163e88f27b3Smrg	atomic_t refcnt;
1643f012e29Smrg	const struct fd_bo_funcs *funcs;
165e88f27b3Smrg
1667cdc0497Smrg	enum {
1677cdc0497Smrg		NO_CACHE = 0,
1687cdc0497Smrg		BO_CACHE = 1,
1697cdc0497Smrg		RING_CACHE = 2,
1707cdc0497Smrg	} bo_reuse;
1717cdc0497Smrg
172e88f27b3Smrg	struct list_head list;   /* bucket-list entry */
173e88f27b3Smrg	time_t free_time;        /* time when added to bucket-list */
174e88f27b3Smrg};
175e88f27b3Smrg
1767cdc0497Smrgdrm_private struct fd_bo *fd_bo_new_ring(struct fd_device *dev,
1777cdc0497Smrg		uint32_t size, uint32_t flags);
1787cdc0497Smrg
179e88f27b3Smrg#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
180e88f27b3Smrg
181e88f27b3Smrg#define enable_debug 0  /* TODO make dynamic */
182e88f27b3Smrg
183e88f27b3Smrg#define INFO_MSG(fmt, ...) \
184e88f27b3Smrg		do { drmMsg("[I] "fmt " (%s:%d)\n", \
185e88f27b3Smrg				##__VA_ARGS__, __FUNCTION__, __LINE__); } while (0)
186e88f27b3Smrg#define DEBUG_MSG(fmt, ...) \
187e88f27b3Smrg		do if (enable_debug) { drmMsg("[D] "fmt " (%s:%d)\n", \
188e88f27b3Smrg				##__VA_ARGS__, __FUNCTION__, __LINE__); } while (0)
189e88f27b3Smrg#define WARN_MSG(fmt, ...) \
190e88f27b3Smrg		do { drmMsg("[W] "fmt " (%s:%d)\n", \
191e88f27b3Smrg				##__VA_ARGS__, __FUNCTION__, __LINE__); } while (0)
192e88f27b3Smrg#define ERROR_MSG(fmt, ...) \
193e88f27b3Smrg		do { drmMsg("[E] " fmt " (%s:%d)\n", \
194e88f27b3Smrg				##__VA_ARGS__, __FUNCTION__, __LINE__); } while (0)
195e88f27b3Smrg
196e88f27b3Smrg#define U642VOID(x) ((void *)(unsigned long)(x))
197e88f27b3Smrg#define VOID2U64(x) ((uint64_t)(unsigned long)(x))
198e88f27b3Smrg
1993f012e29Smrgstatic inline uint32_t
2003f012e29Smrgoffset_bytes(void *end, void *start)
2013f012e29Smrg{
2023f012e29Smrg	return ((char *)end) - ((char *)start);
2033f012e29Smrg}
2043f012e29Smrg
20500a23bdaSmrg#if HAVE_VALGRIND
206d8807b2fSmrg#  include <memcheck.h>
207d8807b2fSmrg
208d8807b2fSmrg/*
209d8807b2fSmrg * For tracking the backing memory (if valgrind enabled, we force a mmap
210d8807b2fSmrg * for the purposes of tracking)
211d8807b2fSmrg */
212d8807b2fSmrgstatic inline void VG_BO_ALLOC(struct fd_bo *bo)
213d8807b2fSmrg{
214d8807b2fSmrg	if (bo && RUNNING_ON_VALGRIND) {
215d8807b2fSmrg		VALGRIND_MALLOCLIKE_BLOCK(fd_bo_map(bo), bo->size, 0, 1);
216d8807b2fSmrg	}
217d8807b2fSmrg}
218d8807b2fSmrg
219d8807b2fSmrgstatic inline void VG_BO_FREE(struct fd_bo *bo)
220d8807b2fSmrg{
221d8807b2fSmrg	VALGRIND_FREELIKE_BLOCK(bo->map, 0);
222d8807b2fSmrg}
223d8807b2fSmrg
224d8807b2fSmrg/*
225d8807b2fSmrg * For tracking bo structs that are in the buffer-cache, so that valgrind
226d8807b2fSmrg * doesn't attribute ownership to the first one to allocate the recycled
227d8807b2fSmrg * bo.
228d8807b2fSmrg *
229d8807b2fSmrg * Note that the list_head in fd_bo is used to track the buffers in cache
230d8807b2fSmrg * so disable error reporting on the range while they are in cache so
231d8807b2fSmrg * valgrind doesn't squawk about list traversal.
232d8807b2fSmrg *
233d8807b2fSmrg */
234d8807b2fSmrgstatic inline void VG_BO_RELEASE(struct fd_bo *bo)
235d8807b2fSmrg{
236d8807b2fSmrg	if (RUNNING_ON_VALGRIND) {
237d8807b2fSmrg		VALGRIND_DISABLE_ADDR_ERROR_REPORTING_IN_RANGE(bo, bo->dev->bo_size);
238d8807b2fSmrg		VALGRIND_MAKE_MEM_NOACCESS(bo, bo->dev->bo_size);
239d8807b2fSmrg		VALGRIND_FREELIKE_BLOCK(bo->map, 0);
240d8807b2fSmrg	}
241d8807b2fSmrg}
242d8807b2fSmrgstatic inline void VG_BO_OBTAIN(struct fd_bo *bo)
243d8807b2fSmrg{
244d8807b2fSmrg	if (RUNNING_ON_VALGRIND) {
245d8807b2fSmrg		VALGRIND_MAKE_MEM_DEFINED(bo, bo->dev->bo_size);
246d8807b2fSmrg		VALGRIND_ENABLE_ADDR_ERROR_REPORTING_IN_RANGE(bo, bo->dev->bo_size);
247d8807b2fSmrg		VALGRIND_MALLOCLIKE_BLOCK(bo->map, bo->size, 0, 1);
248d8807b2fSmrg	}
249d8807b2fSmrg}
250d8807b2fSmrg#else
251d8807b2fSmrgstatic inline void VG_BO_ALLOC(struct fd_bo *bo)   {}
252d8807b2fSmrgstatic inline void VG_BO_FREE(struct fd_bo *bo)    {}
253d8807b2fSmrgstatic inline void VG_BO_RELEASE(struct fd_bo *bo) {}
254d8807b2fSmrgstatic inline void VG_BO_OBTAIN(struct fd_bo *bo)  {}
255d8807b2fSmrg#endif
256d8807b2fSmrg
257d8807b2fSmrg
258e88f27b3Smrg#endif /* FREEDRENO_PRIV_H_ */
259