Path: blob/21.2-virgl/src/freedreno/drm/freedreno_priv.h
4564 views
/*1* Copyright (C) 2012-2018 Rob Clark <[email protected]>2*3* Permission is hereby granted, free of charge, to any person obtaining a4* copy of this software and associated documentation files (the "Software"),5* to deal in the Software without restriction, including without limitation6* the rights to use, copy, modify, merge, publish, distribute, sublicense,7* and/or sell copies of the Software, and to permit persons to whom the8* Software is furnished to do so, subject to the following conditions:9*10* The above copyright notice and this permission notice (including the next11* paragraph) shall be included in all copies or substantial portions of the12* Software.13*14* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR15* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,16* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL17* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER18* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,19* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE20* SOFTWARE.21*22* Authors:23* Rob Clark <[email protected]>24*/2526#ifndef FREEDRENO_PRIV_H_27#define FREEDRENO_PRIV_H_2829#include <errno.h>30#include <fcntl.h>31#include <stdio.h>32#include <stdlib.h>33#include <string.h>34#include <unistd.h>35#include <sys/ioctl.h>36#include <sys/mman.h>3738#include <xf86drm.h>3940#include "util/hash_table.h"41#include "util/list.h"42#include "util/log.h"43#include "util/simple_mtx.h"44#include "util/u_atomic.h"45#include "util/u_debug.h"46#include "util/u_math.h"4748#include "freedreno_drmif.h"49#include "freedreno_ringbuffer.h"5051extern simple_mtx_t table_lock;5253/*54* Stupid/simple growable array implementation:55*/5657#define MAX_ARRAY_SIZE ((unsigned short)~0)5859static inline void60grow(void **ptr, uint16_t nr, uint16_t *max, uint16_t sz)61{62assert((nr + 1) < MAX_ARRAY_SIZE);63if ((nr + 1) > *max) {64if (*max > MAX_ARRAY_SIZE/2)65*max = MAX_ARRAY_SIZE;66else if ((*max * 2) < (nr + 1))67*max = nr + 5;68else69*max = *max * 2;70*ptr = realloc(*ptr, *max * sz);71}72}7374#define DECLARE_ARRAY(type, name) \75unsigned short nr_##name, max_##name; \76type *name;7778#define APPEND(x, name, ...) \79({ \80grow((void **)&(x)->name, (x)->nr_##name, &(x)->max_##name, \81sizeof((x)->name[0])); \82(x)->name[(x)->nr_##name] = __VA_ARGS__; \83(x)->nr_##name++; \84})8586#define READ_ONCE(x) (*(volatile __typeof__(x) *)&(x))878889struct fd_device_funcs {90int (*bo_new_handle)(struct fd_device *dev, uint32_t size, uint32_t flags,91uint32_t *handle);92struct fd_bo *(*bo_from_handle)(struct fd_device *dev, uint32_t size,93uint32_t handle);94struct fd_pipe *(*pipe_new)(struct fd_device *dev, enum fd_pipe_id id,95unsigned prio);96void (*destroy)(struct fd_device *dev);97};9899struct fd_bo_bucket {100uint32_t size;101struct list_head list;102};103104struct fd_bo_cache {105struct fd_bo_bucket cache_bucket[14 * 4];106int num_buckets;107time_t time;108};109110struct fd_device {111int fd;112enum fd_version version;113int32_t refcnt;114115/* tables to keep track of bo's, to avoid "evil-twin" fd_bo objects:116*117* handle_table: maps handle to fd_bo118* name_table: maps flink name to fd_bo119*120* We end up needing two tables, because DRM_IOCTL_GEM_OPEN always121* returns a new handle. So we need to figure out if the bo is already122* open in the process first, before calling gem-open.123*/124struct hash_table *handle_table, *name_table;125126const struct fd_device_funcs *funcs;127128struct fd_bo_cache bo_cache;129struct fd_bo_cache ring_cache;130131int closefd; /* call close(fd) upon destruction */132133/* just for valgrind: */134int bo_size;135136/**137* List of deferred submits, protected by submit_lock. The deferred138* submits are tracked globally per-device, even if they execute in139* different order on the kernel side (ie. due to different priority140* submitqueues, etc) to preserve the order that they are passed off141* to the kernel. Once the kernel has them, it is the fences' job142* to preserve correct order of execution.143*/144struct list_head deferred_submits;145unsigned deferred_cmds;146simple_mtx_t submit_lock;147};148149#define foreach_submit(name, list) \150list_for_each_entry(struct fd_submit, name, list, node)151#define foreach_submit_safe(name, list) \152list_for_each_entry_safe(struct fd_submit, name, list, node)153#define last_submit(list) \154list_last_entry(list, struct fd_submit, node)155156void fd_bo_cache_init(struct fd_bo_cache *cache, int coarse);157void fd_bo_cache_cleanup(struct fd_bo_cache *cache, time_t time);158struct fd_bo *fd_bo_cache_alloc(struct fd_bo_cache *cache, uint32_t *size,159uint32_t flags);160int fd_bo_cache_free(struct fd_bo_cache *cache, struct fd_bo *bo);161162/* for where @table_lock is already held: */163void fd_bo_del_locked(struct fd_bo *bo);164void fd_device_del_locked(struct fd_device *dev);165void fd_pipe_del_locked(struct fd_pipe *pipe);166167struct fd_pipe_funcs {168struct fd_ringbuffer *(*ringbuffer_new_object)(struct fd_pipe *pipe,169uint32_t size);170struct fd_submit *(*submit_new)(struct fd_pipe *pipe);171172/**173* Flush any deferred submits (if deferred submits are supported by174* the pipe implementation)175*/176void (*flush)(struct fd_pipe *pipe, uint32_t fence);177178int (*get_param)(struct fd_pipe *pipe, enum fd_param_id param,179uint64_t *value);180int (*wait)(struct fd_pipe *pipe, const struct fd_fence *fence,181uint64_t timeout);182void (*destroy)(struct fd_pipe *pipe);183};184185struct fd_pipe_control {186uint32_t fence;187};188#define control_ptr(pipe, member) \189(pipe)->control_mem, offsetof(struct fd_pipe_control, member), 0, 0190191struct fd_pipe {192struct fd_device *dev;193enum fd_pipe_id id;194uint32_t gpu_id;195196/**197* Note refcnt is *not* atomic, but protected by table_lock, since the198* table_lock is held in fd_bo_add_fence(), which is the hotpath.199*/200int32_t refcnt;201202/**203* Previous fence seqno allocated for this pipe. The fd_pipe represents204* a single timeline, fences allocated by this pipe can be compared to205* each other, but fences from different pipes are not comparable (as206* there could be preemption of multiple priority level submitqueues at207* play)208*/209uint32_t last_fence;210211struct fd_bo *control_mem;212volatile struct fd_pipe_control *control;213214const struct fd_pipe_funcs *funcs;215};216217uint32_t fd_pipe_emit_fence(struct fd_pipe *pipe, struct fd_ringbuffer *ring);218219static inline void220fd_pipe_flush(struct fd_pipe *pipe, uint32_t fence)221{222if (!pipe->funcs->flush)223return;224pipe->funcs->flush(pipe, fence);225}226227struct fd_submit_funcs {228struct fd_ringbuffer *(*new_ringbuffer)(struct fd_submit *submit,229uint32_t size,230enum fd_ringbuffer_flags flags);231int (*flush)(struct fd_submit *submit, int in_fence_fd,232struct fd_submit_fence *out_fence);233void (*destroy)(struct fd_submit *submit);234};235236struct fd_submit {237int32_t refcnt;238struct fd_pipe *pipe;239const struct fd_submit_funcs *funcs;240241struct fd_ringbuffer *primary;242uint32_t fence;243struct list_head node; /* node in fd_pipe::deferred_submits */244};245246static inline unsigned247fd_dev_count_deferred_cmds(struct fd_device *dev)248{249unsigned nr = 0;250251simple_mtx_assert_locked(&dev->submit_lock);252253list_for_each_entry (struct fd_submit, submit, &dev->deferred_submits, node) {254nr += fd_ringbuffer_cmd_count(submit->primary);255}256257return nr;258}259260struct fd_bo_funcs {261int (*offset)(struct fd_bo *bo, uint64_t *offset);262int (*cpu_prep)(struct fd_bo *bo, struct fd_pipe *pipe, uint32_t op);263void (*cpu_fini)(struct fd_bo *bo);264int (*madvise)(struct fd_bo *bo, int willneed);265uint64_t (*iova)(struct fd_bo *bo);266void (*set_name)(struct fd_bo *bo, const char *fmt, va_list ap);267void (*destroy)(struct fd_bo *bo);268};269270struct fd_bo_fence {271/* For non-shared buffers, track the last pipe the buffer was active272* on, and the per-pipe fence value that indicates when the buffer is273* idle:274*/275uint32_t fence;276struct fd_pipe *pipe;277};278279struct fd_bo {280struct fd_device *dev;281uint32_t size;282uint32_t handle;283uint32_t name;284int32_t refcnt;285uint32_t flags; /* flags like FD_RELOC_DUMP to use for relocs to this BO */286uint64_t iova;287void *map;288const struct fd_bo_funcs *funcs;289290enum {291NO_CACHE = 0,292BO_CACHE = 1,293RING_CACHE = 2,294} bo_reuse : 2;295296/* Buffers that are shared (imported or exported) may be used in297* other processes, so we need to fallback to kernel to determine298* busyness.299*/300bool shared : 1;301302/* We need to be able to disable userspace fence synchronization for303* special internal buffers, namely the pipe->control buffer, to avoid304* a circular reference loop.305*/306bool nosync : 1;307308struct list_head list; /* bucket-list entry */309time_t free_time; /* time when added to bucket-list */310311DECLARE_ARRAY(struct fd_bo_fence, fences);312313/* In the common case, there is no more than one fence attached.314* This provides storage for the fences table until it grows to315* be larger than a single element.316*/317struct fd_bo_fence _inline_fence;318};319320void fd_bo_add_fence(struct fd_bo *bo, struct fd_pipe *pipe, uint32_t fence);321322enum fd_bo_state {323FD_BO_STATE_IDLE,324FD_BO_STATE_BUSY,325FD_BO_STATE_UNKNOWN,326};327enum fd_bo_state fd_bo_state(struct fd_bo *bo);328329struct fd_bo *fd_bo_new_ring(struct fd_device *dev, uint32_t size);330331#define enable_debug 0 /* TODO make dynamic */332333bool fd_dbg(void);334335#define INFO_MSG(fmt, ...) \336do { \337if (fd_dbg()) \338mesa_logi("%s:%d: " fmt, __FUNCTION__, __LINE__, ##__VA_ARGS__); \339} while (0)340#define DEBUG_MSG(fmt, ...) \341do \342if (enable_debug) { \343mesa_logd("%s:%d: " fmt, __FUNCTION__, __LINE__, ##__VA_ARGS__); \344} \345while (0)346#define WARN_MSG(fmt, ...) \347do { \348mesa_logw("%s:%d: " fmt, __FUNCTION__, __LINE__, ##__VA_ARGS__); \349} while (0)350#define ERROR_MSG(fmt, ...) \351do { \352mesa_loge("%s:%d: " fmt, __FUNCTION__, __LINE__, ##__VA_ARGS__); \353} while (0)354355#define U642VOID(x) ((void *)(unsigned long)(x))356#define VOID2U64(x) ((uint64_t)(unsigned long)(x))357358#if HAVE_VALGRIND359#include <memcheck.h>360361/*362* For tracking the backing memory (if valgrind enabled, we force a mmap363* for the purposes of tracking)364*/365static inline void366VG_BO_ALLOC(struct fd_bo *bo)367{368if (bo && RUNNING_ON_VALGRIND) {369VALGRIND_MALLOCLIKE_BLOCK(fd_bo_map(bo), bo->size, 0, 1);370}371}372373static inline void374VG_BO_FREE(struct fd_bo *bo)375{376VALGRIND_FREELIKE_BLOCK(bo->map, 0);377}378379/*380* For tracking bo structs that are in the buffer-cache, so that valgrind381* doesn't attribute ownership to the first one to allocate the recycled382* bo.383*384* Note that the list_head in fd_bo is used to track the buffers in cache385* so disable error reporting on the range while they are in cache so386* valgrind doesn't squawk about list traversal.387*388*/389static inline void390VG_BO_RELEASE(struct fd_bo *bo)391{392if (RUNNING_ON_VALGRIND) {393VALGRIND_DISABLE_ADDR_ERROR_REPORTING_IN_RANGE(bo, bo->dev->bo_size);394VALGRIND_MAKE_MEM_NOACCESS(bo, bo->dev->bo_size);395VALGRIND_FREELIKE_BLOCK(bo->map, 0);396}397}398static inline void399VG_BO_OBTAIN(struct fd_bo *bo)400{401if (RUNNING_ON_VALGRIND) {402VALGRIND_MAKE_MEM_DEFINED(bo, bo->dev->bo_size);403VALGRIND_ENABLE_ADDR_ERROR_REPORTING_IN_RANGE(bo, bo->dev->bo_size);404VALGRIND_MALLOCLIKE_BLOCK(bo->map, bo->size, 0, 1);405}406}407#else408static inline void409VG_BO_ALLOC(struct fd_bo *bo)410{411}412static inline void413VG_BO_FREE(struct fd_bo *bo)414{415}416static inline void417VG_BO_RELEASE(struct fd_bo *bo)418{419}420static inline void421VG_BO_OBTAIN(struct fd_bo *bo)422{423}424#endif425426#define FD_DEFINE_CAST(parent, child) \427static inline struct child *to_##child(struct parent *x) \428{ \429return (struct child *)x; \430}431432#endif /* FREEDRENO_PRIV_H_ */433434435