13defc6d9178078bbc82c4237d79c8b4501a241a
[mesa.git] / src / gallium / drivers / freedreno / drm / msm_pipe.c
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 #include "util/slab.h"
28
29 #include "msm_priv.h"
30
31 static int query_param(struct fd_pipe *pipe, uint32_t param,
32 uint64_t *value)
33 {
34 struct msm_pipe *msm_pipe = to_msm_pipe(pipe);
35 struct drm_msm_param req = {
36 .pipe = msm_pipe->pipe,
37 .param = param,
38 };
39 int ret;
40
41 ret = drmCommandWriteRead(pipe->dev->fd, DRM_MSM_GET_PARAM,
42 &req, sizeof(req));
43 if (ret)
44 return ret;
45
46 *value = req.value;
47
48 return 0;
49 }
50
51 static int msm_pipe_get_param(struct fd_pipe *pipe,
52 enum fd_param_id param, uint64_t *value)
53 {
54 struct msm_pipe *msm_pipe = to_msm_pipe(pipe);
55 switch(param) {
56 case FD_DEVICE_ID: // XXX probably get rid of this..
57 case FD_GPU_ID:
58 *value = msm_pipe->gpu_id;
59 return 0;
60 case FD_GMEM_SIZE:
61 *value = msm_pipe->gmem;
62 return 0;
63 case FD_CHIP_ID:
64 *value = msm_pipe->chip_id;
65 return 0;
66 case FD_MAX_FREQ:
67 return query_param(pipe, MSM_PARAM_MAX_FREQ, value);
68 case FD_TIMESTAMP:
69 return query_param(pipe, MSM_PARAM_TIMESTAMP, value);
70 case FD_NR_RINGS:
71 return query_param(pipe, MSM_PARAM_NR_RINGS, value);
72 default:
73 ERROR_MSG("invalid param id: %d", param);
74 return -1;
75 }
76 }
77
78 static int msm_pipe_wait(struct fd_pipe *pipe, uint32_t timestamp,
79 uint64_t timeout)
80 {
81 struct fd_device *dev = pipe->dev;
82 struct drm_msm_wait_fence req = {
83 .fence = timestamp,
84 .queueid = to_msm_pipe(pipe)->queue_id,
85 };
86 int ret;
87
88 get_abs_timeout(&req.timeout, timeout);
89
90 ret = drmCommandWrite(dev->fd, DRM_MSM_WAIT_FENCE, &req, sizeof(req));
91 if (ret) {
92 ERROR_MSG("wait-fence failed! %d (%s)", ret, strerror(errno));
93 return ret;
94 }
95
96 return 0;
97 }
98
99 static int open_submitqueue(struct fd_pipe *pipe, uint32_t prio)
100 {
101 struct drm_msm_submitqueue req = {
102 .flags = 0,
103 .prio = prio,
104 };
105 uint64_t nr_rings = 1;
106 int ret;
107
108 if (fd_device_version(pipe->dev) < FD_VERSION_SUBMIT_QUEUES) {
109 to_msm_pipe(pipe)->queue_id = 0;
110 return 0;
111 }
112
113 msm_pipe_get_param(pipe, FD_NR_RINGS, &nr_rings);
114
115 req.prio = MIN2(req.prio, MAX2(nr_rings, 1) - 1);
116
117 ret = drmCommandWriteRead(pipe->dev->fd, DRM_MSM_SUBMITQUEUE_NEW,
118 &req, sizeof(req));
119 if (ret) {
120 ERROR_MSG("could not create submitqueue! %d (%s)", ret, strerror(errno));
121 return ret;
122 }
123
124 to_msm_pipe(pipe)->queue_id = req.id;
125 return 0;
126 }
127
128 static void close_submitqueue(struct fd_pipe *pipe, uint32_t queue_id)
129 {
130 if (fd_device_version(pipe->dev) < FD_VERSION_SUBMIT_QUEUES)
131 return;
132
133 drmCommandWrite(pipe->dev->fd, DRM_MSM_SUBMITQUEUE_CLOSE,
134 &queue_id, sizeof(queue_id));
135 }
136
137 static void msm_pipe_destroy(struct fd_pipe *pipe)
138 {
139 struct msm_pipe *msm_pipe = to_msm_pipe(pipe);
140 close_submitqueue(pipe, msm_pipe->queue_id);
141 free(msm_pipe);
142 }
143
144 static const struct fd_pipe_funcs sp_funcs = {
145 .ringbuffer_new_object = msm_ringbuffer_sp_new_object,
146 .submit_new = msm_submit_sp_new,
147 .get_param = msm_pipe_get_param,
148 .wait = msm_pipe_wait,
149 .destroy = msm_pipe_destroy,
150 };
151
152 static const struct fd_pipe_funcs legacy_funcs = {
153 .ringbuffer_new_object = msm_ringbuffer_new_object,
154 .submit_new = msm_submit_new,
155 .get_param = msm_pipe_get_param,
156 .wait = msm_pipe_wait,
157 .destroy = msm_pipe_destroy,
158 };
159
160 static uint64_t get_param(struct fd_pipe *pipe, uint32_t param)
161 {
162 uint64_t value;
163 int ret = query_param(pipe, param, &value);
164 if (ret) {
165 ERROR_MSG("get-param failed! %d (%s)", ret, strerror(errno));
166 return 0;
167 }
168 return value;
169 }
170
171 static bool use_softpin(void)
172 {
173 static int sp = -1;
174 if (sp < 0) {
175 const char *str = getenv("FD_MESA_DEBUG");
176 sp = str && strstr(str, "softpin");
177 }
178 return sp;
179 }
180
181 struct fd_pipe * msm_pipe_new(struct fd_device *dev,
182 enum fd_pipe_id id, uint32_t prio)
183 {
184 static const uint32_t pipe_id[] = {
185 [FD_PIPE_3D] = MSM_PIPE_3D0,
186 [FD_PIPE_2D] = MSM_PIPE_2D0,
187 };
188 struct msm_pipe *msm_pipe = NULL;
189 struct fd_pipe *pipe = NULL;
190
191 msm_pipe = calloc(1, sizeof(*msm_pipe));
192 if (!msm_pipe) {
193 ERROR_MSG("allocation failed");
194 goto fail;
195 }
196
197 pipe = &msm_pipe->base;
198
199 // TODO once kernel changes are in place, this switch will be
200 // based on kernel version:
201 if (use_softpin()) {
202 pipe->funcs = &sp_funcs;
203 } else {
204 pipe->funcs = &legacy_funcs;
205 }
206
207 /* initialize before get_param(): */
208 pipe->dev = dev;
209 msm_pipe->pipe = pipe_id[id];
210
211 /* these params should be supported since the first version of drm/msm: */
212 msm_pipe->gpu_id = get_param(pipe, MSM_PARAM_GPU_ID);
213 msm_pipe->gmem = get_param(pipe, MSM_PARAM_GMEM_SIZE);
214 msm_pipe->chip_id = get_param(pipe, MSM_PARAM_CHIP_ID);
215
216 if (! msm_pipe->gpu_id)
217 goto fail;
218
219 INFO_MSG("Pipe Info:");
220 INFO_MSG(" GPU-id: %d", msm_pipe->gpu_id);
221 INFO_MSG(" Chip-id: 0x%08x", msm_pipe->chip_id);
222 INFO_MSG(" GMEM size: 0x%08x", msm_pipe->gmem);
223
224 if (open_submitqueue(pipe, prio))
225 goto fail;
226
227 return pipe;
228 fail:
229 if (pipe)
230 fd_pipe_del(pipe);
231 return NULL;
232 }