1bf215546Sopenharmony_ci/*
2bf215546Sopenharmony_ci * Copyright (C) 2012-2018 Rob Clark <robclark@freedesktop.org>
3bf215546Sopenharmony_ci *
4bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a
5bf215546Sopenharmony_ci * copy of this software and associated documentation files (the "Software"),
6bf215546Sopenharmony_ci * to deal in the Software without restriction, including without limitation
7bf215546Sopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8bf215546Sopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the
9bf215546Sopenharmony_ci * Software is furnished to do so, subject to the following conditions:
10bf215546Sopenharmony_ci *
11bf215546Sopenharmony_ci * The above copyright notice and this permission notice (including the next
12bf215546Sopenharmony_ci * paragraph) shall be included in all copies or substantial portions of the
13bf215546Sopenharmony_ci * Software.
14bf215546Sopenharmony_ci *
15bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16bf215546Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17bf215546Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18bf215546Sopenharmony_ci * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19bf215546Sopenharmony_ci * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20bf215546Sopenharmony_ci * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21bf215546Sopenharmony_ci * SOFTWARE.
22bf215546Sopenharmony_ci *
23bf215546Sopenharmony_ci * Authors:
24bf215546Sopenharmony_ci *    Rob Clark <robclark@freedesktop.org>
25bf215546Sopenharmony_ci */
26bf215546Sopenharmony_ci
27bf215546Sopenharmony_ci#include "util/slab.h"
28bf215546Sopenharmony_ci
29bf215546Sopenharmony_ci#include "freedreno_ringbuffer_sp.h"
30bf215546Sopenharmony_ci#include "msm_priv.h"
31bf215546Sopenharmony_ci
32bf215546Sopenharmony_cistatic int
33bf215546Sopenharmony_ciquery_param(struct fd_pipe *pipe, uint32_t param, uint64_t *value)
34bf215546Sopenharmony_ci{
35bf215546Sopenharmony_ci   struct msm_pipe *msm_pipe = to_msm_pipe(pipe);
36bf215546Sopenharmony_ci   struct drm_msm_param req = {
37bf215546Sopenharmony_ci      .pipe = msm_pipe->pipe,
38bf215546Sopenharmony_ci      .param = param,
39bf215546Sopenharmony_ci   };
40bf215546Sopenharmony_ci   int ret;
41bf215546Sopenharmony_ci
42bf215546Sopenharmony_ci   ret =
43bf215546Sopenharmony_ci      drmCommandWriteRead(pipe->dev->fd, DRM_MSM_GET_PARAM, &req, sizeof(req));
44bf215546Sopenharmony_ci   if (ret)
45bf215546Sopenharmony_ci      return ret;
46bf215546Sopenharmony_ci
47bf215546Sopenharmony_ci   *value = req.value;
48bf215546Sopenharmony_ci
49bf215546Sopenharmony_ci   return 0;
50bf215546Sopenharmony_ci}
51bf215546Sopenharmony_ci
52bf215546Sopenharmony_cistatic int
53bf215546Sopenharmony_ciquery_queue_param(struct fd_pipe *pipe, uint32_t param, uint64_t *value)
54bf215546Sopenharmony_ci{
55bf215546Sopenharmony_ci   struct msm_pipe *msm_pipe = to_msm_pipe(pipe);
56bf215546Sopenharmony_ci   struct drm_msm_submitqueue_query req = {
57bf215546Sopenharmony_ci      .data = VOID2U64(value),
58bf215546Sopenharmony_ci      .id = msm_pipe->queue_id,
59bf215546Sopenharmony_ci      .param = param,
60bf215546Sopenharmony_ci      .len = sizeof(*value),
61bf215546Sopenharmony_ci   };
62bf215546Sopenharmony_ci   int ret;
63bf215546Sopenharmony_ci
64bf215546Sopenharmony_ci   ret = drmCommandWriteRead(pipe->dev->fd, DRM_MSM_SUBMITQUEUE_QUERY, &req,
65bf215546Sopenharmony_ci                             sizeof(req));
66bf215546Sopenharmony_ci   if (ret)
67bf215546Sopenharmony_ci      return ret;
68bf215546Sopenharmony_ci
69bf215546Sopenharmony_ci   return 0;
70bf215546Sopenharmony_ci}
71bf215546Sopenharmony_ci
72bf215546Sopenharmony_cistatic int
73bf215546Sopenharmony_cimsm_pipe_get_param(struct fd_pipe *pipe, enum fd_param_id param,
74bf215546Sopenharmony_ci                   uint64_t *value)
75bf215546Sopenharmony_ci{
76bf215546Sopenharmony_ci   struct msm_pipe *msm_pipe = to_msm_pipe(pipe);
77bf215546Sopenharmony_ci   switch (param) {
78bf215546Sopenharmony_ci   case FD_DEVICE_ID: // XXX probably get rid of this..
79bf215546Sopenharmony_ci   case FD_GPU_ID:
80bf215546Sopenharmony_ci      *value = msm_pipe->gpu_id;
81bf215546Sopenharmony_ci      return 0;
82bf215546Sopenharmony_ci   case FD_GMEM_SIZE:
83bf215546Sopenharmony_ci      *value = msm_pipe->gmem;
84bf215546Sopenharmony_ci      return 0;
85bf215546Sopenharmony_ci   case FD_GMEM_BASE:
86bf215546Sopenharmony_ci      *value = msm_pipe->gmem_base;
87bf215546Sopenharmony_ci      return 0;
88bf215546Sopenharmony_ci   case FD_CHIP_ID:
89bf215546Sopenharmony_ci      *value = msm_pipe->chip_id;
90bf215546Sopenharmony_ci      return 0;
91bf215546Sopenharmony_ci   case FD_MAX_FREQ:
92bf215546Sopenharmony_ci      return query_param(pipe, MSM_PARAM_MAX_FREQ, value);
93bf215546Sopenharmony_ci   case FD_TIMESTAMP:
94bf215546Sopenharmony_ci      return query_param(pipe, MSM_PARAM_TIMESTAMP, value);
95bf215546Sopenharmony_ci   case FD_NR_RINGS:
96bf215546Sopenharmony_ci      return query_param(pipe, MSM_PARAM_NR_RINGS, value);
97bf215546Sopenharmony_ci   case FD_CTX_FAULTS:
98bf215546Sopenharmony_ci      return query_queue_param(pipe, MSM_SUBMITQUEUE_PARAM_FAULTS, value);
99bf215546Sopenharmony_ci   case FD_GLOBAL_FAULTS:
100bf215546Sopenharmony_ci      return query_param(pipe, MSM_PARAM_FAULTS, value);
101bf215546Sopenharmony_ci   case FD_SUSPEND_COUNT:
102bf215546Sopenharmony_ci      return query_param(pipe, MSM_PARAM_SUSPENDS, value);
103bf215546Sopenharmony_ci   default:
104bf215546Sopenharmony_ci      ERROR_MSG("invalid param id: %d", param);
105bf215546Sopenharmony_ci      return -1;
106bf215546Sopenharmony_ci   }
107bf215546Sopenharmony_ci}
108bf215546Sopenharmony_ci
109bf215546Sopenharmony_cistatic int
110bf215546Sopenharmony_ciset_param(struct fd_pipe *pipe, uint32_t param, uint64_t value)
111bf215546Sopenharmony_ci{
112bf215546Sopenharmony_ci   struct msm_pipe *msm_pipe = to_msm_pipe(pipe);
113bf215546Sopenharmony_ci   struct drm_msm_param req = {
114bf215546Sopenharmony_ci      .pipe  = msm_pipe->pipe,
115bf215546Sopenharmony_ci      .param = param,
116bf215546Sopenharmony_ci      .value = value,
117bf215546Sopenharmony_ci   };
118bf215546Sopenharmony_ci
119bf215546Sopenharmony_ci   return drmCommandWriteRead(pipe->dev->fd, DRM_MSM_SET_PARAM,
120bf215546Sopenharmony_ci                              &req, sizeof(req));
121bf215546Sopenharmony_ci}
122bf215546Sopenharmony_ci
123bf215546Sopenharmony_cistatic int
124bf215546Sopenharmony_cimsm_pipe_set_param(struct fd_pipe *pipe, enum fd_param_id param, uint64_t value)
125bf215546Sopenharmony_ci{
126bf215546Sopenharmony_ci   switch (param) {
127bf215546Sopenharmony_ci   case FD_SYSPROF:
128bf215546Sopenharmony_ci      return set_param(pipe, MSM_PARAM_SYSPROF, value);
129bf215546Sopenharmony_ci   default:
130bf215546Sopenharmony_ci      ERROR_MSG("invalid param id: %d", param);
131bf215546Sopenharmony_ci      return -1;
132bf215546Sopenharmony_ci   }
133bf215546Sopenharmony_ci}
134bf215546Sopenharmony_ci
135bf215546Sopenharmony_cistatic int
136bf215546Sopenharmony_cimsm_pipe_wait(struct fd_pipe *pipe, const struct fd_fence *fence, uint64_t timeout)
137bf215546Sopenharmony_ci{
138bf215546Sopenharmony_ci   struct fd_device *dev = pipe->dev;
139bf215546Sopenharmony_ci   struct drm_msm_wait_fence req = {
140bf215546Sopenharmony_ci      .fence = fence->kfence,
141bf215546Sopenharmony_ci      .queueid = to_msm_pipe(pipe)->queue_id,
142bf215546Sopenharmony_ci   };
143bf215546Sopenharmony_ci   int ret;
144bf215546Sopenharmony_ci
145bf215546Sopenharmony_ci   get_abs_timeout(&req.timeout, timeout);
146bf215546Sopenharmony_ci
147bf215546Sopenharmony_ci   ret = drmCommandWrite(dev->fd, DRM_MSM_WAIT_FENCE, &req, sizeof(req));
148bf215546Sopenharmony_ci   if (ret && (ret != -ETIMEDOUT)) {
149bf215546Sopenharmony_ci      ERROR_MSG("wait-fence failed! %d (%s)", ret, strerror(errno));
150bf215546Sopenharmony_ci   }
151bf215546Sopenharmony_ci
152bf215546Sopenharmony_ci   return ret;
153bf215546Sopenharmony_ci}
154bf215546Sopenharmony_ci
155bf215546Sopenharmony_cistatic int
156bf215546Sopenharmony_ciopen_submitqueue(struct fd_pipe *pipe, uint32_t prio)
157bf215546Sopenharmony_ci{
158bf215546Sopenharmony_ci   struct drm_msm_submitqueue req = {
159bf215546Sopenharmony_ci      .flags = 0,
160bf215546Sopenharmony_ci      .prio = prio,
161bf215546Sopenharmony_ci   };
162bf215546Sopenharmony_ci   uint64_t nr_rings = 1;
163bf215546Sopenharmony_ci   int ret;
164bf215546Sopenharmony_ci
165bf215546Sopenharmony_ci   if (fd_device_version(pipe->dev) < FD_VERSION_SUBMIT_QUEUES) {
166bf215546Sopenharmony_ci      to_msm_pipe(pipe)->queue_id = 0;
167bf215546Sopenharmony_ci      return 0;
168bf215546Sopenharmony_ci   }
169bf215546Sopenharmony_ci
170bf215546Sopenharmony_ci   msm_pipe_get_param(pipe, FD_NR_RINGS, &nr_rings);
171bf215546Sopenharmony_ci
172bf215546Sopenharmony_ci   req.prio = MIN2(req.prio, MAX2(nr_rings, 1) - 1);
173bf215546Sopenharmony_ci
174bf215546Sopenharmony_ci   ret = drmCommandWriteRead(pipe->dev->fd, DRM_MSM_SUBMITQUEUE_NEW, &req,
175bf215546Sopenharmony_ci                             sizeof(req));
176bf215546Sopenharmony_ci   if (ret) {
177bf215546Sopenharmony_ci      ERROR_MSG("could not create submitqueue! %d (%s)", ret, strerror(errno));
178bf215546Sopenharmony_ci      return ret;
179bf215546Sopenharmony_ci   }
180bf215546Sopenharmony_ci
181bf215546Sopenharmony_ci   to_msm_pipe(pipe)->queue_id = req.id;
182bf215546Sopenharmony_ci   return 0;
183bf215546Sopenharmony_ci}
184bf215546Sopenharmony_ci
185bf215546Sopenharmony_cistatic void
186bf215546Sopenharmony_ciclose_submitqueue(struct fd_pipe *pipe, uint32_t queue_id)
187bf215546Sopenharmony_ci{
188bf215546Sopenharmony_ci   if (fd_device_version(pipe->dev) < FD_VERSION_SUBMIT_QUEUES)
189bf215546Sopenharmony_ci      return;
190bf215546Sopenharmony_ci
191bf215546Sopenharmony_ci   drmCommandWrite(pipe->dev->fd, DRM_MSM_SUBMITQUEUE_CLOSE, &queue_id,
192bf215546Sopenharmony_ci                   sizeof(queue_id));
193bf215546Sopenharmony_ci}
194bf215546Sopenharmony_ci
195bf215546Sopenharmony_cistatic void
196bf215546Sopenharmony_cimsm_pipe_destroy(struct fd_pipe *pipe)
197bf215546Sopenharmony_ci{
198bf215546Sopenharmony_ci   struct msm_pipe *msm_pipe = to_msm_pipe(pipe);
199bf215546Sopenharmony_ci
200bf215546Sopenharmony_ci   close_submitqueue(pipe, msm_pipe->queue_id);
201bf215546Sopenharmony_ci   fd_pipe_sp_ringpool_fini(pipe);
202bf215546Sopenharmony_ci   free(msm_pipe);
203bf215546Sopenharmony_ci}
204bf215546Sopenharmony_ci
205bf215546Sopenharmony_cistatic const struct fd_pipe_funcs sp_funcs = {
206bf215546Sopenharmony_ci   .ringbuffer_new_object = fd_ringbuffer_sp_new_object,
207bf215546Sopenharmony_ci   .submit_new = msm_submit_sp_new,
208bf215546Sopenharmony_ci   .flush = fd_pipe_sp_flush,
209bf215546Sopenharmony_ci   .get_param = msm_pipe_get_param,
210bf215546Sopenharmony_ci   .set_param = msm_pipe_set_param,
211bf215546Sopenharmony_ci   .wait = msm_pipe_wait,
212bf215546Sopenharmony_ci   .destroy = msm_pipe_destroy,
213bf215546Sopenharmony_ci};
214bf215546Sopenharmony_ci
215bf215546Sopenharmony_cistatic const struct fd_pipe_funcs legacy_funcs = {
216bf215546Sopenharmony_ci   .ringbuffer_new_object = msm_ringbuffer_new_object,
217bf215546Sopenharmony_ci   .submit_new = msm_submit_new,
218bf215546Sopenharmony_ci   .get_param = msm_pipe_get_param,
219bf215546Sopenharmony_ci   .set_param = msm_pipe_set_param,
220bf215546Sopenharmony_ci   .wait = msm_pipe_wait,
221bf215546Sopenharmony_ci   .destroy = msm_pipe_destroy,
222bf215546Sopenharmony_ci};
223bf215546Sopenharmony_ci
224bf215546Sopenharmony_cistatic uint64_t
225bf215546Sopenharmony_ciget_param(struct fd_pipe *pipe, uint32_t param)
226bf215546Sopenharmony_ci{
227bf215546Sopenharmony_ci   uint64_t value;
228bf215546Sopenharmony_ci   int ret = query_param(pipe, param, &value);
229bf215546Sopenharmony_ci   if (ret) {
230bf215546Sopenharmony_ci      ERROR_MSG("get-param failed! %d (%s)", ret, strerror(errno));
231bf215546Sopenharmony_ci      return 0;
232bf215546Sopenharmony_ci   }
233bf215546Sopenharmony_ci   return value;
234bf215546Sopenharmony_ci}
235bf215546Sopenharmony_ci
236bf215546Sopenharmony_cistruct fd_pipe *
237bf215546Sopenharmony_cimsm_pipe_new(struct fd_device *dev, enum fd_pipe_id id, uint32_t prio)
238bf215546Sopenharmony_ci{
239bf215546Sopenharmony_ci   static const uint32_t pipe_id[] = {
240bf215546Sopenharmony_ci      [FD_PIPE_3D] = MSM_PIPE_3D0,
241bf215546Sopenharmony_ci      [FD_PIPE_2D] = MSM_PIPE_2D0,
242bf215546Sopenharmony_ci   };
243bf215546Sopenharmony_ci   struct msm_pipe *msm_pipe = NULL;
244bf215546Sopenharmony_ci   struct fd_pipe *pipe = NULL;
245bf215546Sopenharmony_ci
246bf215546Sopenharmony_ci   msm_pipe = calloc(1, sizeof(*msm_pipe));
247bf215546Sopenharmony_ci   if (!msm_pipe) {
248bf215546Sopenharmony_ci      ERROR_MSG("allocation failed");
249bf215546Sopenharmony_ci      goto fail;
250bf215546Sopenharmony_ci   }
251bf215546Sopenharmony_ci
252bf215546Sopenharmony_ci   pipe = &msm_pipe->base;
253bf215546Sopenharmony_ci
254bf215546Sopenharmony_ci   if (fd_device_version(dev) >= FD_VERSION_SOFTPIN) {
255bf215546Sopenharmony_ci      pipe->funcs = &sp_funcs;
256bf215546Sopenharmony_ci   } else {
257bf215546Sopenharmony_ci      pipe->funcs = &legacy_funcs;
258bf215546Sopenharmony_ci   }
259bf215546Sopenharmony_ci
260bf215546Sopenharmony_ci   /* initialize before get_param(): */
261bf215546Sopenharmony_ci   pipe->dev = dev;
262bf215546Sopenharmony_ci   msm_pipe->pipe = pipe_id[id];
263bf215546Sopenharmony_ci
264bf215546Sopenharmony_ci   /* these params should be supported since the first version of drm/msm: */
265bf215546Sopenharmony_ci   msm_pipe->gpu_id = get_param(pipe, MSM_PARAM_GPU_ID);
266bf215546Sopenharmony_ci   msm_pipe->gmem = get_param(pipe, MSM_PARAM_GMEM_SIZE);
267bf215546Sopenharmony_ci   msm_pipe->chip_id = get_param(pipe, MSM_PARAM_CHIP_ID);
268bf215546Sopenharmony_ci
269bf215546Sopenharmony_ci   if (fd_device_version(pipe->dev) >= FD_VERSION_GMEM_BASE)
270bf215546Sopenharmony_ci      msm_pipe->gmem_base = get_param(pipe, MSM_PARAM_GMEM_BASE);
271bf215546Sopenharmony_ci
272bf215546Sopenharmony_ci   if (!(msm_pipe->gpu_id || msm_pipe->chip_id))
273bf215546Sopenharmony_ci      goto fail;
274bf215546Sopenharmony_ci
275bf215546Sopenharmony_ci   INFO_MSG("Pipe Info:");
276bf215546Sopenharmony_ci   INFO_MSG(" GPU-id:          %d", msm_pipe->gpu_id);
277bf215546Sopenharmony_ci   INFO_MSG(" Chip-id:         0x%016"PRIx64, msm_pipe->chip_id);
278bf215546Sopenharmony_ci   INFO_MSG(" GMEM size:       0x%08x", msm_pipe->gmem);
279bf215546Sopenharmony_ci
280bf215546Sopenharmony_ci   if (open_submitqueue(pipe, prio))
281bf215546Sopenharmony_ci      goto fail;
282bf215546Sopenharmony_ci
283bf215546Sopenharmony_ci   fd_pipe_sp_ringpool_init(pipe);
284bf215546Sopenharmony_ci
285bf215546Sopenharmony_ci   return pipe;
286bf215546Sopenharmony_cifail:
287bf215546Sopenharmony_ci   if (pipe)
288bf215546Sopenharmony_ci      fd_pipe_del(pipe);
289bf215546Sopenharmony_ci   return NULL;
290bf215546Sopenharmony_ci}
291