Home | History | Annotate | Download | only in freedreno
      1 /* -*- mode: C; c-file-style: "k&r"; tab-width 4; indent-tabs-mode: t; -*- */
      2 
      3 /*
      4  * Copyright (C) 2012 Rob Clark <robclark (at) 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 (at) 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 struct fd_device_funcs {
     58 	int (*bo_new_handle)(struct fd_device *dev, uint32_t size,
     59 			uint32_t flags, uint32_t *handle);
     60 	struct fd_bo * (*bo_from_handle)(struct fd_device *dev,
     61 			uint32_t size, uint32_t handle);
     62 	struct fd_pipe * (*pipe_new)(struct fd_device *dev, enum fd_pipe_id id);
     63 	void (*destroy)(struct fd_device *dev);
     64 };
     65 
     66 struct fd_bo_bucket {
     67 	uint32_t size;
     68 	struct list_head list;
     69 };
     70 
     71 struct fd_device {
     72 	int fd;
     73 	atomic_t refcnt;
     74 
     75 	/* tables to keep track of bo's, to avoid "evil-twin" fd_bo objects:
     76 	 *
     77 	 *   handle_table: maps handle to fd_bo
     78 	 *   name_table: maps flink name to fd_bo
     79 	 *
     80 	 * We end up needing two tables, because DRM_IOCTL_GEM_OPEN always
     81 	 * returns a new handle.  So we need to figure out if the bo is already
     82 	 * open in the process first, before calling gem-open.
     83 	 */
     84 	void *handle_table, *name_table;
     85 
     86 	const struct fd_device_funcs *funcs;
     87 
     88 	struct fd_bo_bucket cache_bucket[14 * 4];
     89 	int num_buckets;
     90 	time_t time;
     91 
     92 	int closefd;        /* call close(fd) upon destruction */
     93 };
     94 
     95 drm_private void fd_cleanup_bo_cache(struct fd_device *dev, time_t time);
     96 
     97 /* for where @table_lock is already held: */
     98 drm_private void fd_device_del_locked(struct fd_device *dev);
     99 
    100 struct fd_pipe_funcs {
    101 	struct fd_ringbuffer * (*ringbuffer_new)(struct fd_pipe *pipe, uint32_t size);
    102 	int (*get_param)(struct fd_pipe *pipe, enum fd_param_id param, uint64_t *value);
    103 	int (*wait)(struct fd_pipe *pipe, uint32_t timestamp, uint64_t timeout);
    104 	void (*destroy)(struct fd_pipe *pipe);
    105 };
    106 
    107 struct fd_pipe {
    108 	struct fd_device *dev;
    109 	enum fd_pipe_id id;
    110 	const struct fd_pipe_funcs *funcs;
    111 };
    112 
    113 struct fd_ringmarker {
    114 	struct fd_ringbuffer *ring;
    115 	uint32_t *cur;
    116 };
    117 
    118 struct fd_ringbuffer_funcs {
    119 	void * (*hostptr)(struct fd_ringbuffer *ring);
    120 	int (*flush)(struct fd_ringbuffer *ring, uint32_t *last_start);
    121 	void (*reset)(struct fd_ringbuffer *ring);
    122 	void (*emit_reloc)(struct fd_ringbuffer *ring,
    123 			const struct fd_reloc *reloc);
    124 	void (*emit_reloc_ring)(struct fd_ringbuffer *ring,
    125 			struct fd_ringmarker *target, struct fd_ringmarker *end);
    126 	void (*destroy)(struct fd_ringbuffer *ring);
    127 };
    128 
    129 struct fd_bo_funcs {
    130 	int (*offset)(struct fd_bo *bo, uint64_t *offset);
    131 	int (*cpu_prep)(struct fd_bo *bo, struct fd_pipe *pipe, uint32_t op);
    132 	void (*cpu_fini)(struct fd_bo *bo);
    133 	void (*destroy)(struct fd_bo *bo);
    134 };
    135 
    136 struct fd_bo {
    137 	struct fd_device *dev;
    138 	uint32_t size;
    139 	uint32_t handle;
    140 	uint32_t name;
    141 	void *map;
    142 	atomic_t refcnt;
    143 	const struct fd_bo_funcs *funcs;
    144 
    145 	int bo_reuse;
    146 	struct list_head list;   /* bucket-list entry */
    147 	time_t free_time;        /* time when added to bucket-list */
    148 };
    149 
    150 #define ALIGN(v,a) (((v) + (a) - 1) & ~((a) - 1))
    151 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
    152 
    153 #define enable_debug 0  /* TODO make dynamic */
    154 
    155 #define INFO_MSG(fmt, ...) \
    156 		do { drmMsg("[I] "fmt " (%s:%d)\n", \
    157 				##__VA_ARGS__, __FUNCTION__, __LINE__); } while (0)
    158 #define DEBUG_MSG(fmt, ...) \
    159 		do if (enable_debug) { drmMsg("[D] "fmt " (%s:%d)\n", \
    160 				##__VA_ARGS__, __FUNCTION__, __LINE__); } while (0)
    161 #define WARN_MSG(fmt, ...) \
    162 		do { drmMsg("[W] "fmt " (%s:%d)\n", \
    163 				##__VA_ARGS__, __FUNCTION__, __LINE__); } while (0)
    164 #define ERROR_MSG(fmt, ...) \
    165 		do { drmMsg("[E] " fmt " (%s:%d)\n", \
    166 				##__VA_ARGS__, __FUNCTION__, __LINE__); } while (0)
    167 
    168 #define U642VOID(x) ((void *)(unsigned long)(x))
    169 #define VOID2U64(x) ((uint64_t)(unsigned long)(x))
    170 
    171 #endif /* FREEDRENO_PRIV_H_ */
    172