freedreno: add support for FD_MAX_FREQ
[platform/upstream/libdrm.git] / freedreno / msm / msm_pipe.c
1 /* -*- mode: C; c-file-style: "k&r"; tab-width 4; indent-tabs-mode: t; -*- */
2
3 /*
4  * Copyright (C) 2013 Rob Clark <robclark@freedesktop.org>
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice (including the next
14  * paragraph) shall be included in all copies or substantial portions of the
15  * Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
20  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23  * SOFTWARE.
24  *
25  * Authors:
26  *    Rob Clark <robclark@freedesktop.org>
27  */
28
29 #ifdef HAVE_CONFIG_H
30 # include <config.h>
31 #endif
32
33 #include "msm_priv.h"
34
35 static int query_param(struct fd_pipe *pipe, uint32_t param,
36                 uint64_t *value)
37 {
38         struct msm_pipe *msm_pipe = to_msm_pipe(pipe);
39         struct drm_msm_param req = {
40                         .pipe = msm_pipe->pipe,
41                         .param = param,
42         };
43         int ret;
44
45         ret = drmCommandWriteRead(pipe->dev->fd, DRM_MSM_GET_PARAM,
46                         &req, sizeof(req));
47         if (ret)
48                 return ret;
49
50         *value = req.value;
51
52         return 0;
53 }
54
55 static int msm_pipe_get_param(struct fd_pipe *pipe,
56                 enum fd_param_id param, uint64_t *value)
57 {
58         struct msm_pipe *msm_pipe = to_msm_pipe(pipe);
59         switch(param) {
60         case FD_DEVICE_ID: // XXX probably get rid of this..
61         case FD_GPU_ID:
62                 *value = msm_pipe->gpu_id;
63                 return 0;
64         case FD_GMEM_SIZE:
65                 *value = msm_pipe->gmem;
66                 return 0;
67         case FD_CHIP_ID:
68                 *value = msm_pipe->chip_id;
69                 return 0;
70         case FD_MAX_FREQ:
71                 return query_param(pipe, MSM_PARAM_MAX_FREQ, 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         };
85         int ret;
86
87         get_abs_timeout(&req.timeout, timeout);
88
89         ret = drmCommandWrite(dev->fd, DRM_MSM_WAIT_FENCE, &req, sizeof(req));
90         if (ret) {
91                 ERROR_MSG("wait-fence failed! %d (%s)", ret, strerror(errno));
92                 return ret;
93         }
94
95         return 0;
96 }
97
98 static void msm_pipe_destroy(struct fd_pipe *pipe)
99 {
100         struct msm_pipe *msm_pipe = to_msm_pipe(pipe);
101         free(msm_pipe);
102 }
103
104 static const struct fd_pipe_funcs funcs = {
105                 .ringbuffer_new = msm_ringbuffer_new,
106                 .get_param = msm_pipe_get_param,
107                 .wait = msm_pipe_wait,
108                 .destroy = msm_pipe_destroy,
109 };
110
111 static uint64_t get_param(struct fd_pipe *pipe, uint32_t param)
112 {
113         uint64_t value;
114         int ret = query_param(pipe, param, &value);
115         if (ret) {
116                 ERROR_MSG("get-param failed! %d (%s)", ret, strerror(errno));
117                 return 0;
118         }
119         return value;
120 }
121
122 drm_private struct fd_pipe * msm_pipe_new(struct fd_device *dev,
123                 enum fd_pipe_id id)
124 {
125         static const uint32_t pipe_id[] = {
126                         [FD_PIPE_3D] = MSM_PIPE_3D0,
127                         [FD_PIPE_2D] = MSM_PIPE_2D0,
128         };
129         struct msm_pipe *msm_pipe = NULL;
130         struct fd_pipe *pipe = NULL;
131
132         msm_pipe = calloc(1, sizeof(*msm_pipe));
133         if (!msm_pipe) {
134                 ERROR_MSG("allocation failed");
135                 goto fail;
136         }
137
138         pipe = &msm_pipe->base;
139         pipe->funcs = &funcs;
140
141         /* initialize before get_param(): */
142         pipe->dev = dev;
143         msm_pipe->pipe = pipe_id[id];
144
145         /* these params should be supported since the first version of drm/msm: */
146         msm_pipe->gpu_id = get_param(pipe, MSM_PARAM_GPU_ID);
147         msm_pipe->gmem   = get_param(pipe, MSM_PARAM_GMEM_SIZE);
148         msm_pipe->chip_id = get_param(pipe, MSM_PARAM_CHIP_ID);
149
150         if (! msm_pipe->gpu_id)
151                 goto fail;
152
153         INFO_MSG("Pipe Info:");
154         INFO_MSG(" GPU-id:          %d", msm_pipe->gpu_id);
155         INFO_MSG(" Chip-id:         0x%08x", msm_pipe->chip_id);
156         INFO_MSG(" GMEM size:       0x%08x", msm_pipe->gmem);
157
158         return pipe;
159 fail:
160         if (pipe)
161                 fd_pipe_del(pipe);
162         return NULL;
163 }