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 "freedreno_ringbuffer_sp.h"
30 #include "msm_priv.h"
31 
32 static int
query_param(struct fd_pipe * pipe,uint32_t param,uint64_t * value)33 query_param(struct fd_pipe *pipe, uint32_t param, uint64_t *value)
34 {
35    struct msm_pipe *msm_pipe = to_msm_pipe(pipe);
36    struct drm_msm_param req = {
37       .pipe = msm_pipe->pipe,
38       .param = param,
39    };
40    int ret;
41 
42    ret =
43       drmCommandWriteRead(pipe->dev->fd, DRM_MSM_GET_PARAM, &req, sizeof(req));
44    if (ret)
45       return ret;
46 
47    *value = req.value;
48 
49    return 0;
50 }
51 
52 static int
query_queue_param(struct fd_pipe * pipe,uint32_t param,uint64_t * value)53 query_queue_param(struct fd_pipe *pipe, uint32_t param, uint64_t *value)
54 {
55    struct msm_pipe *msm_pipe = to_msm_pipe(pipe);
56    struct drm_msm_submitqueue_query req = {
57       .data = VOID2U64(value),
58       .id = msm_pipe->queue_id,
59       .param = param,
60       .len = sizeof(*value),
61    };
62    int ret;
63 
64    ret = drmCommandWriteRead(pipe->dev->fd, DRM_MSM_SUBMITQUEUE_QUERY, &req,
65                              sizeof(req));
66    if (ret)
67       return ret;
68 
69    return 0;
70 }
71 
72 static int
msm_pipe_get_param(struct fd_pipe * pipe,enum fd_param_id param,uint64_t * value)73 msm_pipe_get_param(struct fd_pipe *pipe, enum fd_param_id param,
74                    uint64_t *value)
75 {
76    struct msm_pipe *msm_pipe = to_msm_pipe(pipe);
77    switch (param) {
78    case FD_DEVICE_ID: // XXX probably get rid of this..
79    case FD_GPU_ID:
80       *value = msm_pipe->gpu_id;
81       return 0;
82    case FD_GMEM_SIZE:
83       *value = msm_pipe->gmem;
84       return 0;
85    case FD_GMEM_BASE:
86       *value = msm_pipe->gmem_base;
87       return 0;
88    case FD_CHIP_ID:
89       *value = msm_pipe->chip_id;
90       return 0;
91    case FD_MAX_FREQ:
92       return query_param(pipe, MSM_PARAM_MAX_FREQ, value);
93    case FD_TIMESTAMP:
94       return query_param(pipe, MSM_PARAM_TIMESTAMP, value);
95    case FD_NR_RINGS:
96       return query_param(pipe, MSM_PARAM_NR_RINGS, value);
97    case FD_PP_PGTABLE:
98       return query_param(pipe, MSM_PARAM_PP_PGTABLE, value);
99    case FD_CTX_FAULTS:
100       return query_queue_param(pipe, MSM_SUBMITQUEUE_PARAM_FAULTS, value);
101    case FD_GLOBAL_FAULTS:
102       return query_param(pipe, MSM_PARAM_FAULTS, value);
103    case FD_SUSPEND_COUNT:
104       return query_param(pipe, MSM_PARAM_SUSPENDS, value);
105    default:
106       ERROR_MSG("invalid param id: %d", param);
107       return -1;
108    }
109 }
110 
111 static int
set_param(struct fd_pipe * pipe,uint32_t param,uint64_t value)112 set_param(struct fd_pipe *pipe, uint32_t param, uint64_t value)
113 {
114    struct msm_pipe *msm_pipe = to_msm_pipe(pipe);
115    struct drm_msm_param req = {
116       .pipe  = msm_pipe->pipe,
117       .param = param,
118       .value = value,
119    };
120 
121    return drmCommandWriteRead(pipe->dev->fd, DRM_MSM_SET_PARAM,
122                               &req, sizeof(req));
123 }
124 
125 static int
msm_pipe_set_param(struct fd_pipe * pipe,enum fd_param_id param,uint64_t value)126 msm_pipe_set_param(struct fd_pipe *pipe, enum fd_param_id param, uint64_t value)
127 {
128    switch (param) {
129    case FD_SYSPROF:
130       return set_param(pipe, MSM_PARAM_SYSPROF, value);
131    default:
132       ERROR_MSG("invalid param id: %d", param);
133       return -1;
134    }
135 }
136 
137 static int
msm_pipe_wait(struct fd_pipe * pipe,const struct fd_fence * fence,uint64_t timeout)138 msm_pipe_wait(struct fd_pipe *pipe, const struct fd_fence *fence, uint64_t timeout)
139 {
140    struct fd_device *dev = pipe->dev;
141    struct drm_msm_wait_fence req = {
142       .fence = fence->kfence,
143       .queueid = to_msm_pipe(pipe)->queue_id,
144    };
145    int ret;
146 
147    get_abs_timeout(&req.timeout, timeout);
148 
149    ret = drmCommandWrite(dev->fd, DRM_MSM_WAIT_FENCE, &req, sizeof(req));
150    if (ret && (ret != -ETIMEDOUT)) {
151       ERROR_MSG("wait-fence failed! %d (%s)", ret, strerror(errno));
152    }
153 
154    return ret;
155 }
156 
157 static int
open_submitqueue(struct fd_pipe * pipe,uint32_t prio)158 open_submitqueue(struct fd_pipe *pipe, uint32_t prio)
159 {
160    struct drm_msm_submitqueue req = {
161       .flags = 0,
162       .prio = prio,
163    };
164    uint64_t nr_rings = 1;
165    int ret;
166 
167    if (fd_device_version(pipe->dev) < FD_VERSION_SUBMIT_QUEUES) {
168       to_msm_pipe(pipe)->queue_id = 0;
169       return 0;
170    }
171 
172    msm_pipe_get_param(pipe, FD_NR_RINGS, &nr_rings);
173 
174    req.prio = MIN2(req.prio, MAX2(nr_rings, 1) - 1);
175 
176    ret = drmCommandWriteRead(pipe->dev->fd, DRM_MSM_SUBMITQUEUE_NEW, &req,
177                              sizeof(req));
178    if (ret) {
179       ERROR_MSG("could not create submitqueue! %d (%s)", ret, strerror(errno));
180       return ret;
181    }
182 
183    to_msm_pipe(pipe)->queue_id = req.id;
184    return 0;
185 }
186 
187 static void
close_submitqueue(struct fd_pipe * pipe,uint32_t queue_id)188 close_submitqueue(struct fd_pipe *pipe, uint32_t queue_id)
189 {
190    if (fd_device_version(pipe->dev) < FD_VERSION_SUBMIT_QUEUES)
191       return;
192 
193    drmCommandWrite(pipe->dev->fd, DRM_MSM_SUBMITQUEUE_CLOSE, &queue_id,
194                    sizeof(queue_id));
195 }
196 
197 static void
msm_pipe_destroy(struct fd_pipe * pipe)198 msm_pipe_destroy(struct fd_pipe *pipe)
199 {
200    struct msm_pipe *msm_pipe = to_msm_pipe(pipe);
201 
202    close_submitqueue(pipe, msm_pipe->queue_id);
203    fd_pipe_sp_ringpool_fini(pipe);
204    free(msm_pipe);
205 }
206 
207 static const struct fd_pipe_funcs sp_funcs = {
208    .ringbuffer_new_object = fd_ringbuffer_sp_new_object,
209    .submit_new = msm_submit_sp_new,
210    .flush = fd_pipe_sp_flush,
211    .get_param = msm_pipe_get_param,
212    .set_param = msm_pipe_set_param,
213    .wait = msm_pipe_wait,
214    .destroy = msm_pipe_destroy,
215 };
216 
217 static const struct fd_pipe_funcs legacy_funcs = {
218    .ringbuffer_new_object = msm_ringbuffer_new_object,
219    .submit_new = msm_submit_new,
220    .get_param = msm_pipe_get_param,
221    .set_param = msm_pipe_set_param,
222    .wait = msm_pipe_wait,
223    .destroy = msm_pipe_destroy,
224 };
225 
226 static uint64_t
get_param(struct fd_pipe * pipe,uint32_t param)227 get_param(struct fd_pipe *pipe, uint32_t param)
228 {
229    uint64_t value;
230    int ret = query_param(pipe, param, &value);
231    if (ret) {
232       ERROR_MSG("get-param failed! %d (%s)", ret, strerror(errno));
233       return 0;
234    }
235    return value;
236 }
237 
238 struct fd_pipe *
msm_pipe_new(struct fd_device * dev,enum fd_pipe_id id,uint32_t prio)239 msm_pipe_new(struct fd_device *dev, enum fd_pipe_id id, uint32_t prio)
240 {
241    static const uint32_t pipe_id[] = {
242       [FD_PIPE_3D] = MSM_PIPE_3D0,
243       [FD_PIPE_2D] = MSM_PIPE_2D0,
244    };
245    struct msm_pipe *msm_pipe = NULL;
246    struct fd_pipe *pipe = NULL;
247 
248    msm_pipe = calloc(1, sizeof(*msm_pipe));
249    if (!msm_pipe) {
250       ERROR_MSG("allocation failed");
251       goto fail;
252    }
253 
254    pipe = &msm_pipe->base;
255 
256    if (fd_device_version(dev) >= FD_VERSION_SOFTPIN) {
257       pipe->funcs = &sp_funcs;
258    } else {
259       pipe->funcs = &legacy_funcs;
260    }
261 
262    /* initialize before get_param(): */
263    pipe->dev = dev;
264    msm_pipe->pipe = pipe_id[id];
265 
266    /* these params should be supported since the first version of drm/msm: */
267    msm_pipe->gpu_id = get_param(pipe, MSM_PARAM_GPU_ID);
268    msm_pipe->gmem = get_param(pipe, MSM_PARAM_GMEM_SIZE);
269    msm_pipe->chip_id = get_param(pipe, MSM_PARAM_CHIP_ID);
270 
271    if (fd_device_version(pipe->dev) >= FD_VERSION_GMEM_BASE)
272       msm_pipe->gmem_base = get_param(pipe, MSM_PARAM_GMEM_BASE);
273 
274    if (!(msm_pipe->gpu_id || msm_pipe->chip_id))
275       goto fail;
276 
277    INFO_MSG("Pipe Info:");
278    INFO_MSG(" GPU-id:          %d", msm_pipe->gpu_id);
279    INFO_MSG(" Chip-id:         0x%016"PRIx64, msm_pipe->chip_id);
280    INFO_MSG(" GMEM size:       0x%08x", msm_pipe->gmem);
281 
282    if (open_submitqueue(pipe, prio))
283       goto fail;
284 
285    fd_pipe_sp_ringpool_init(pipe);
286 
287    return pipe;
288 fail:
289    if (pipe)
290       fd_pipe_del(pipe);
291    return NULL;
292 }
293