freedreno: move drm to common location
[mesa.git] / src / freedreno / drm / msm_priv.h
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 #ifndef __user
33 # define __user
34 #endif
35
36 #include "msm_drm.h"
37
38 struct msm_device {
39 struct fd_device base;
40 struct fd_bo_cache ring_cache;
41 };
42 FD_DEFINE_CAST(fd_device, msm_device);
43
44 struct fd_device * msm_device_new(int fd);
45
46 struct msm_pipe {
47 struct fd_pipe base;
48 uint32_t pipe;
49 uint32_t gpu_id;
50 uint32_t gmem;
51 uint32_t chip_id;
52 uint32_t queue_id;
53 };
54 FD_DEFINE_CAST(fd_pipe, msm_pipe);
55
56 struct fd_pipe * msm_pipe_new(struct fd_device *dev,
57 enum fd_pipe_id id, uint32_t prio);
58
59 struct fd_ringbuffer * msm_ringbuffer_new_object(struct fd_pipe *pipe, uint32_t size);
60 struct fd_ringbuffer * msm_ringbuffer_sp_new_object(struct fd_pipe *pipe, uint32_t size);
61
62 struct fd_submit * msm_submit_new(struct fd_pipe *pipe);
63 struct fd_submit * msm_submit_sp_new(struct fd_pipe *pipe);
64
65 struct msm_bo {
66 struct fd_bo base;
67 uint64_t offset;
68 /* to avoid excess hashtable lookups, cache the ring this bo was
69 * last emitted on (since that will probably also be the next ring
70 * it is emitted on)
71 */
72 unsigned current_submit_seqno;
73 uint32_t idx;
74 };
75 FD_DEFINE_CAST(fd_bo, msm_bo);
76
77 int msm_bo_new_handle(struct fd_device *dev,
78 uint32_t size, uint32_t flags, uint32_t *handle);
79 struct fd_bo * msm_bo_from_handle(struct fd_device *dev,
80 uint32_t size, uint32_t handle);
81
82 static inline void
83 msm_dump_submit(struct drm_msm_gem_submit *req)
84 {
85 for (unsigned i = 0; i < req->nr_bos; i++) {
86 struct drm_msm_gem_submit_bo *bos = U642VOID(req->bos);
87 struct drm_msm_gem_submit_bo *bo = &bos[i];
88 ERROR_MSG(" bos[%d]: handle=%u, flags=%x", i, bo->handle, bo->flags);
89 }
90 for (unsigned i = 0; i < req->nr_cmds; i++) {
91 struct drm_msm_gem_submit_cmd *cmds = U642VOID(req->cmds);
92 struct drm_msm_gem_submit_cmd *cmd = &cmds[i];
93 struct drm_msm_gem_submit_reloc *relocs = U642VOID(cmd->relocs);
94 ERROR_MSG(" cmd[%d]: type=%u, submit_idx=%u, submit_offset=%u, size=%u",
95 i, cmd->type, cmd->submit_idx, cmd->submit_offset, cmd->size);
96 for (unsigned j = 0; j < cmd->nr_relocs; j++) {
97 struct drm_msm_gem_submit_reloc *r = &relocs[j];
98 ERROR_MSG(" reloc[%d]: submit_offset=%u, or=%08x, shift=%d, reloc_idx=%u"
99 ", reloc_offset=%"PRIu64, j, r->submit_offset, r->or, r->shift,
100 r->reloc_idx, r->reloc_offset);
101 }
102 }
103 }
104
105 static inline void get_abs_timeout(struct drm_msm_timespec *tv, uint64_t ns)
106 {
107 struct timespec t;
108 uint32_t s = ns / 1000000000;
109 clock_gettime(CLOCK_MONOTONIC, &t);
110 tv->tv_sec = t.tv_sec + s;
111 tv->tv_nsec = t.tv_nsec + ns - (s * 1000000000);
112 }
113
114 /*
115 * Stupid/simple growable array implementation:
116 */
117
118 static inline void *
119 grow(void *ptr, uint16_t nr, uint16_t *max, uint16_t sz)
120 {
121 if ((nr + 1) > *max) {
122 if ((*max * 2) < (nr + 1))
123 *max = nr + 5;
124 else
125 *max = *max * 2;
126 ptr = realloc(ptr, *max * sz);
127 }
128 return ptr;
129 }
130
131 #define DECLARE_ARRAY(type, name) \
132 unsigned short nr_ ## name, max_ ## name; \
133 type * name;
134
135 #define APPEND(x, name) ({ \
136 (x)->name = grow((x)->name, (x)->nr_ ## name, &(x)->max_ ## name, sizeof((x)->name[0])); \
137 (x)->nr_ ## name ++; \
138 })
139
140 #endif /* MSM_PRIV_H_ */