17e102996Smaya/*
27e102996Smaya * Copyright (C) 2012-2018 Rob Clark <robclark@freedesktop.org>
37e102996Smaya *
47e102996Smaya * Permission is hereby granted, free of charge, to any person obtaining a
57e102996Smaya * copy of this software and associated documentation files (the "Software"),
67e102996Smaya * to deal in the Software without restriction, including without limitation
77e102996Smaya * the rights to use, copy, modify, merge, publish, distribute, sublicense,
87e102996Smaya * and/or sell copies of the Software, and to permit persons to whom the
97e102996Smaya * Software is furnished to do so, subject to the following conditions:
107e102996Smaya *
117e102996Smaya * The above copyright notice and this permission notice (including the next
127e102996Smaya * paragraph) shall be included in all copies or substantial portions of the
137e102996Smaya * Software.
147e102996Smaya *
157e102996Smaya * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
167e102996Smaya * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
177e102996Smaya * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
187e102996Smaya * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
197e102996Smaya * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
207e102996Smaya * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
217e102996Smaya * SOFTWARE.
227e102996Smaya *
237e102996Smaya * Authors:
247e102996Smaya *    Rob Clark <robclark@freedesktop.org>
257e102996Smaya */
267e102996Smaya
277e102996Smaya#ifndef FREEDRENO_DRMIF_H_
287e102996Smaya#define FREEDRENO_DRMIF_H_
297e102996Smaya
307e102996Smaya#include <stdint.h>
317e102996Smaya
327ec681f3Smrg#include "util/bitset.h"
337e102996Smaya#include "util/u_debug.h"
347e102996Smaya
357ec681f3Smrg#ifdef __cplusplus
367ec681f3Smrgextern "C" {
377ec681f3Smrg#endif
387ec681f3Smrg
397e102996Smayastruct fd_bo;
407e102996Smayastruct fd_pipe;
417e102996Smayastruct fd_device;
427e102996Smaya
437e102996Smayaenum fd_pipe_id {
447ec681f3Smrg   FD_PIPE_3D = 1,
457ec681f3Smrg   FD_PIPE_2D = 2,
467ec681f3Smrg   /* some devices have two 2d blocks.. not really sure how to
477ec681f3Smrg    * use that yet, so just ignoring the 2nd 2d pipe for now
487ec681f3Smrg    */
497ec681f3Smrg   FD_PIPE_MAX
507e102996Smaya};
517e102996Smaya
527e102996Smayaenum fd_param_id {
537ec681f3Smrg   FD_DEVICE_ID,
547ec681f3Smrg   FD_GMEM_SIZE,
557ec681f3Smrg   FD_GMEM_BASE,     /* 64b */
567ec681f3Smrg   FD_GPU_ID,
577ec681f3Smrg   FD_CHIP_ID,       /* 64b */
587ec681f3Smrg   FD_MAX_FREQ,
597ec681f3Smrg   FD_TIMESTAMP,
607ec681f3Smrg   FD_NR_RINGS,      /* # of rings == # of distinct priority levels */
617ec681f3Smrg   FD_PP_PGTABLE,    /* are per-process pagetables used for the pipe/ctx */
627ec681f3Smrg   FD_CTX_FAULTS,    /* # of per context faults */
637ec681f3Smrg   FD_GLOBAL_FAULTS, /* # of global (all context) faults */
647ec681f3Smrg   FD_SUSPEND_COUNT, /* # of times the GPU has suspended, and potentially lost state */
657ec681f3Smrg};
667ec681f3Smrg
677ec681f3Smrg/**
687ec681f3Smrg * Helper for fence/seqno comparisions which deals properly with rollover.
697ec681f3Smrg * Returns true if fence 'a' is before fence 'b'
707ec681f3Smrg */
717ec681f3Smrgstatic inline bool
727ec681f3Smrgfd_fence_before(uint32_t a, uint32_t b)
737ec681f3Smrg{
747ec681f3Smrg   return (int32_t)(a - b) < 0;
757ec681f3Smrg}
767ec681f3Smrg
777ec681f3Smrgstatic inline bool
787ec681f3Smrgfd_fence_after(uint32_t a, uint32_t b)
797ec681f3Smrg{
807ec681f3Smrg   return (int32_t)(a - b) > 0;
817ec681f3Smrg}
827ec681f3Smrg
837ec681f3Smrg/**
847ec681f3Smrg * Per submit, there are actually two fences:
857ec681f3Smrg *  1) The userspace maintained fence, which is used to optimistically
867ec681f3Smrg *     avoid kernel ioctls to query if specific rendering is completed
877ec681f3Smrg *  2) The kernel maintained fence, which we cannot directly do anything
887ec681f3Smrg *     with, other than pass it back to the kernel
897ec681f3Smrg *
907ec681f3Smrg * The userspace fence is mostly internal to the drm layer, but we want
917ec681f3Smrg * the gallium layer to be able to pass it back to us for things like
927ec681f3Smrg * fd_pipe_wait().  So this struct encapsulates the two.
937ec681f3Smrg */
947ec681f3Smrgstruct fd_fence {
957ec681f3Smrg   uint32_t kfence;     /* kernel fence */
967ec681f3Smrg   uint32_t ufence;     /* userspace fence */
977e102996Smaya};
987e102996Smaya
997e102996Smaya/* bo flags: */
1007ec681f3Smrg#define FD_BO_GPUREADONLY         BITSET_BIT(1)
1017ec681f3Smrg#define FD_BO_SCANOUT             BITSET_BIT(2)
1027ec681f3Smrg#define FD_BO_CACHED_COHERENT     BITSET_BIT(3)
1037ec681f3Smrg/* Default caching is WRITECOMBINE */
1047e102996Smaya
1057e102996Smaya/* bo access flags: (keep aligned to MSM_PREP_x) */
1067ec681f3Smrg#define FD_BO_PREP_READ   BITSET_BIT(0)
1077ec681f3Smrg#define FD_BO_PREP_WRITE  BITSET_BIT(1)
1087ec681f3Smrg#define FD_BO_PREP_NOSYNC BITSET_BIT(2)
1097ec681f3Smrg#define FD_BO_PREP_FLUSH  BITSET_BIT(3)
1107ec681f3Smrg
1117e102996Smaya
1127e102996Smaya/* device functions:
1137e102996Smaya */
1147e102996Smaya
1157ec681f3Smrgstruct fd_device *fd_device_new(int fd);
1167ec681f3Smrgstruct fd_device *fd_device_new_dup(int fd);
1177ec681f3Smrgstruct fd_device *fd_device_ref(struct fd_device *dev);
1187ec681f3Smrgvoid fd_device_purge(struct fd_device *dev);
1197e102996Smayavoid fd_device_del(struct fd_device *dev);
1207e102996Smayaint fd_device_fd(struct fd_device *dev);
1217e102996Smaya
1227e102996Smayaenum fd_version {
1237ec681f3Smrg   FD_VERSION_MADVISE = 1,             /* kernel supports madvise */
1247ec681f3Smrg   FD_VERSION_UNLIMITED_CMDS = 1,      /* submits w/ >4 cmd buffers (growable ringbuffer) */
1257ec681f3Smrg   FD_VERSION_FENCE_FD = 2,            /* submit command supports in/out fences */
1267ec681f3Smrg   FD_VERSION_GMEM_BASE = 3,           /* supports querying GMEM base address */
1277ec681f3Smrg   FD_VERSION_SUBMIT_QUEUES = 3,       /* submit queues and multiple priority levels */
1287ec681f3Smrg   FD_VERSION_BO_IOVA = 3,             /* supports fd_bo_get/put_iova() */
1297ec681f3Smrg   FD_VERSION_SOFTPIN = 4,             /* adds softpin, bo name, and dump flag */
1307ec681f3Smrg   FD_VERSION_ROBUSTNESS = 5,          /* adds FD_NR_FAULTS and FD_PP_PGTABLE */
1317ec681f3Smrg   FD_VERSION_MEMORY_FD = 2,           /* supports shared memory objects */
1327ec681f3Smrg   FD_VERSION_SUSPENDS = 7,            /* Adds MSM_PARAM_SUSPENDS to detect device suspend */
1337ec681f3Smrg   FD_VERSION_CACHED_COHERENT = 8,     /* Adds cached-coherent support (a6xx+) */
1347e102996Smaya};
1357e102996Smayaenum fd_version fd_device_version(struct fd_device *dev);
1367e102996Smaya
1377ec681f3Smrgbool fd_has_syncobj(struct fd_device *dev);
1387ec681f3Smrg
1397e102996Smaya/* pipe functions:
1407e102996Smaya */
1417e102996Smaya
1427ec681f3Smrgstruct fd_pipe *fd_pipe_new(struct fd_device *dev, enum fd_pipe_id id);
1437ec681f3Smrgstruct fd_pipe *fd_pipe_new2(struct fd_device *dev, enum fd_pipe_id id,
1447ec681f3Smrg                             uint32_t prio);
1457ec681f3Smrgstruct fd_pipe *fd_pipe_ref(struct fd_pipe *pipe);
1467ec681f3Smrgstruct fd_pipe *fd_pipe_ref_locked(struct fd_pipe *pipe);
1477e102996Smayavoid fd_pipe_del(struct fd_pipe *pipe);
1487ec681f3Smrgvoid fd_pipe_purge(struct fd_pipe *pipe);
1497ec681f3Smrgconst struct fd_dev_id * fd_pipe_dev_id(struct fd_pipe *pipe);
1507e102996Smayaint fd_pipe_get_param(struct fd_pipe *pipe, enum fd_param_id param,
1517ec681f3Smrg                      uint64_t *value);
1527ec681f3Smrgint fd_pipe_wait(struct fd_pipe *pipe, const struct fd_fence *fence);
1537e102996Smaya/* timeout in nanosec */
1547ec681f3Smrgint fd_pipe_wait_timeout(struct fd_pipe *pipe, const struct fd_fence *fence,
1557ec681f3Smrg                         uint64_t timeout);
1567e102996Smaya
1577e102996Smaya/* buffer-object functions:
1587e102996Smaya */
1597e102996Smaya
1607ec681f3Smrgstruct fd_bo *_fd_bo_new(struct fd_device *dev, uint32_t size, uint32_t flags);
1617e102996Smayavoid _fd_bo_set_name(struct fd_bo *bo, const char *fmt, va_list ap);
1627e102996Smaya
1637ec681f3Smrgstatic inline void fd_bo_set_name(struct fd_bo *bo, const char *fmt, ...)
1647ec681f3Smrg   _util_printf_format(2, 3);
1657e102996Smaya
1667e102996Smayastatic inline void
1677e102996Smayafd_bo_set_name(struct fd_bo *bo, const char *fmt, ...)
1687e102996Smaya{
1697e102996Smaya#ifndef NDEBUG
1707ec681f3Smrg   va_list ap;
1717ec681f3Smrg   va_start(ap, fmt);
1727ec681f3Smrg   _fd_bo_set_name(bo, fmt, ap);
1737ec681f3Smrg   va_end(ap);
1747e102996Smaya#endif
1757e102996Smaya}
1767e102996Smaya
1777ec681f3Smrgstatic inline struct fd_bo *fd_bo_new(struct fd_device *dev, uint32_t size,
1787ec681f3Smrg                                      uint32_t flags, const char *fmt, ...)
1797ec681f3Smrg   _util_printf_format(4, 5);
1807e102996Smaya
1817e102996Smayastatic inline struct fd_bo *
1827ec681f3Smrgfd_bo_new(struct fd_device *dev, uint32_t size, uint32_t flags, const char *fmt,
1837ec681f3Smrg          ...)
1847e102996Smaya{
1857ec681f3Smrg   struct fd_bo *bo = _fd_bo_new(dev, size, flags);
1867e102996Smaya#ifndef NDEBUG
1877ec681f3Smrg   if (fmt) {
1887ec681f3Smrg      va_list ap;
1897ec681f3Smrg      va_start(ap, fmt);
1907ec681f3Smrg      _fd_bo_set_name(bo, fmt, ap);
1917ec681f3Smrg      va_end(ap);
1927ec681f3Smrg   }
1937e102996Smaya#endif
1947ec681f3Smrg   return bo;
1957e102996Smaya}
1967e102996Smaya
1977ec681f3Smrgstruct fd_bo *fd_bo_from_handle(struct fd_device *dev, uint32_t handle,
1987ec681f3Smrg                                uint32_t size);
1997ec681f3Smrgstruct fd_bo *fd_bo_from_name(struct fd_device *dev, uint32_t name);
2007ec681f3Smrgstruct fd_bo *fd_bo_from_dmabuf(struct fd_device *dev, int fd);
2017ec681f3Smrgvoid fd_bo_mark_for_dump(struct fd_bo *bo);
2027e102996Smayauint64_t fd_bo_get_iova(struct fd_bo *bo);
2037ec681f3Smrgstruct fd_bo *fd_bo_ref(struct fd_bo *bo);
2047e102996Smayavoid fd_bo_del(struct fd_bo *bo);
2057e102996Smayaint fd_bo_get_name(struct fd_bo *bo, uint32_t *name);
2067e102996Smayauint32_t fd_bo_handle(struct fd_bo *bo);
2077e102996Smayaint fd_bo_dmabuf(struct fd_bo *bo);
2087e102996Smayauint32_t fd_bo_size(struct fd_bo *bo);
2097ec681f3Smrgvoid *fd_bo_map(struct fd_bo *bo);
2107e102996Smayaint fd_bo_cpu_prep(struct fd_bo *bo, struct fd_pipe *pipe, uint32_t op);
2117e102996Smayavoid fd_bo_cpu_fini(struct fd_bo *bo);
2127ec681f3Smrgbool fd_bo_is_cached(struct fd_bo *bo);
2137ec681f3Smrg
2147ec681f3Smrg#ifdef __cplusplus
2157ec681f3Smrg} /* end of extern "C" */
2167ec681f3Smrg#endif
2177e102996Smaya
2187e102996Smaya#endif /* FREEDRENO_DRMIF_H_ */
219