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/slab.h"
33
34 #ifndef __user
35 # define __user
36 #endif
37
38 #include "drm-uapi/msm_drm.h"
39
40 struct msm_device {
41 struct fd_device base;
42 struct fd_bo_cache ring_cache;
43 };
44 FD_DEFINE_CAST(fd_device, msm_device);
45
46 struct fd_device * msm_device_new(int fd);
47
48 struct msm_pipe {
49 struct fd_pipe base;
50 uint32_t pipe;
51 uint32_t gpu_id;
52 uint64_t gmem_base;
53 uint32_t gmem;
54 uint32_t chip_id;
55 uint32_t queue_id;
56 struct slab_parent_pool ring_pool;
57 };
58 FD_DEFINE_CAST(fd_pipe, msm_pipe);
59
60 struct fd_pipe * msm_pipe_new(struct fd_device *dev,
61 enum fd_pipe_id id, uint32_t prio);
62
63 struct fd_ringbuffer * msm_ringbuffer_new_object(struct fd_pipe *pipe, uint32_t size);
64 struct fd_ringbuffer * msm_ringbuffer_sp_new_object(struct fd_pipe *pipe, uint32_t size);
65
66 struct fd_submit * msm_submit_new(struct fd_pipe *pipe);
67 struct fd_submit * msm_submit_sp_new(struct fd_pipe *pipe);
68
69 void msm_pipe_sp_ringpool_init(struct msm_pipe *msm_pipe);
70 void msm_pipe_sp_ringpool_fini(struct msm_pipe *msm_pipe);
71
72
73 struct msm_bo {
74 struct fd_bo base;
75 uint64_t offset;
76 uint32_t idx;
77 };
78 FD_DEFINE_CAST(fd_bo, msm_bo);
79
80 int msm_bo_new_handle(struct fd_device *dev,
81 uint32_t size, uint32_t flags, uint32_t *handle);
82 struct fd_bo * msm_bo_from_handle(struct fd_device *dev,
83 uint32_t size, uint32_t handle);
84
85 static inline void
msm_dump_submit(struct drm_msm_gem_submit * req)86 msm_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(" reloc[%d]: submit_offset=%u, or=%08x, shift=%d, reloc_idx=%u"
102 ", reloc_offset=%"PRIu64, j, r->submit_offset, r->or, r->shift,
103 r->reloc_idx, (uint64_t)r->reloc_offset);
104 }
105 }
106 }
107
get_abs_timeout(struct drm_msm_timespec * tv,uint64_t ns)108 static inline void get_abs_timeout(struct drm_msm_timespec *tv, uint64_t ns)
109 {
110 struct timespec t;
111 clock_gettime(CLOCK_MONOTONIC, &t);
112 tv->tv_sec = t.tv_sec + ns / 1000000000;
113 tv->tv_nsec = t.tv_nsec + ns % 1000000000;
114 }
115
116 /*
117 * Stupid/simple growable array implementation:
118 */
119
120 static inline void *
grow(void * ptr,uint16_t nr,uint16_t * max,uint16_t sz)121 grow(void *ptr, uint16_t nr, uint16_t *max, uint16_t sz)
122 {
123 if ((nr + 1) > *max) {
124 if ((*max * 2) < (nr + 1))
125 *max = nr + 5;
126 else
127 *max = *max * 2;
128 ptr = realloc(ptr, *max * sz);
129 }
130 return ptr;
131 }
132
133 #define DECLARE_ARRAY(type, name) \
134 unsigned short nr_ ## name, max_ ## name; \
135 type * name;
136
137 #define APPEND(x, name) ({ \
138 (x)->name = grow((x)->name, (x)->nr_ ## name, &(x)->max_ ## name, sizeof((x)->name[0])); \
139 (x)->nr_ ## name ++; \
140 })
141
142 #define READ_ONCE(x) (*(volatile __typeof__(x) *)&(x))
143
144 #endif /* MSM_PRIV_H_ */
145