1/*
2 * Copyright (C) 2012-2018 Rob Clark <robclark@freedesktop.org>
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 * SOFTWARE.
22 *
23 * Authors:
24 *    Rob Clark <robclark@freedesktop.org>
25 */
26
27#ifndef MSM_PRIV_H_
28#define MSM_PRIV_H_
29
30#include "freedreno_priv.h"
31
32#include "util/timespec.h"
33
34#include "pipe/p_defines.h"
35
36#ifndef __user
37#define __user
38#endif
39
40#include "drm-uapi/msm_drm.h"
41
42struct msm_device {
43   struct fd_device base;
44};
45FD_DEFINE_CAST(fd_device, msm_device);
46
47struct fd_device *msm_device_new(int fd, drmVersionPtr version);
48
49struct msm_pipe {
50   struct fd_pipe base;
51   uint32_t pipe;
52   uint32_t gpu_id;
53   uint64_t chip_id;
54   uint64_t gmem_base;
55   uint32_t gmem;
56   uint32_t queue_id;
57
58   /**
59    * If we *ever* see an in-fence-fd, assume that userspace is
60    * not relying on implicit fences.
61    */
62   bool no_implicit_sync;
63};
64FD_DEFINE_CAST(fd_pipe, msm_pipe);
65
66struct fd_pipe *msm_pipe_new(struct fd_device *dev, enum fd_pipe_id id,
67                             uint32_t prio);
68
69struct fd_ringbuffer *msm_ringbuffer_new_object(struct fd_pipe *pipe,
70                                                uint32_t size);
71
72struct fd_submit *msm_submit_new(struct fd_pipe *pipe);
73struct fd_submit *msm_submit_sp_new(struct fd_pipe *pipe);
74
75struct msm_bo {
76   struct fd_bo base;
77   uint64_t offset;
78};
79FD_DEFINE_CAST(fd_bo, msm_bo);
80
81struct fd_bo *msm_bo_new(struct fd_device *dev, uint32_t size, uint32_t flags);
82struct fd_bo *msm_bo_from_handle(struct fd_device *dev, uint32_t size,
83                                 uint32_t handle);
84
85static inline void
86msm_dump_submit(struct drm_msm_gem_submit *req)
87{
88   for (unsigned i = 0; i < req->nr_bos; i++) {
89      struct drm_msm_gem_submit_bo *bos = U642VOID(req->bos);
90      struct drm_msm_gem_submit_bo *bo = &bos[i];
91      ERROR_MSG("  bos[%d]: handle=%u, flags=%x", i, bo->handle, bo->flags);
92   }
93   for (unsigned i = 0; i < req->nr_cmds; i++) {
94      struct drm_msm_gem_submit_cmd *cmds = U642VOID(req->cmds);
95      struct drm_msm_gem_submit_cmd *cmd = &cmds[i];
96      struct drm_msm_gem_submit_reloc *relocs = U642VOID(cmd->relocs);
97      ERROR_MSG("  cmd[%d]: type=%u, submit_idx=%u, submit_offset=%u, size=%u",
98                i, cmd->type, cmd->submit_idx, cmd->submit_offset, cmd->size);
99      for (unsigned j = 0; j < cmd->nr_relocs; j++) {
100         struct drm_msm_gem_submit_reloc *r = &relocs[j];
101         ERROR_MSG(
102            "    reloc[%d]: submit_offset=%u, or=%08x, shift=%d, reloc_idx=%u"
103            ", reloc_offset=%" PRIu64,
104            j, r->submit_offset, r->or, r->shift, r->reloc_idx,
105            (uint64_t)r->reloc_offset);
106      }
107   }
108}
109
110static inline void
111get_abs_timeout(struct drm_msm_timespec *tv, uint64_t ns)
112{
113   struct timespec t;
114
115   if (ns == PIPE_TIMEOUT_INFINITE)
116      ns = 3600ULL * NSEC_PER_SEC; /* 1 hour timeout is almost infinite */
117
118   clock_gettime(CLOCK_MONOTONIC, &t);
119   tv->tv_sec = t.tv_sec + ns / NSEC_PER_SEC;
120   tv->tv_nsec = t.tv_nsec + ns % NSEC_PER_SEC;
121   if (tv->tv_nsec >= NSEC_PER_SEC) { /* handle nsec overflow */
122      tv->tv_nsec -= NSEC_PER_SEC;
123      tv->tv_sec++;
124   }
125}
126
127#endif /* MSM_PRIV_H_ */
128