freedreno: add support for msm drm
[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 #include "msm_priv.h"
30
31
32 static int msm_pipe_get_param(struct fd_pipe *pipe,
33                 enum fd_param_id param, uint64_t *value)
34 {
35         struct msm_pipe *msm_pipe = to_msm_pipe(pipe);
36         switch(param) {
37         case FD_DEVICE_ID: // XXX probably get rid of this..
38         case FD_GPU_ID:
39                 *value = msm_pipe->gpu_id;
40                 return 0;
41         case FD_GMEM_SIZE:
42                 *value = msm_pipe->gmem;
43                 return 0;
44         default:
45                 ERROR_MSG("invalid param id: %d", param);
46                 return -1;
47         }
48 }
49
50 static int msm_pipe_wait(struct fd_pipe *pipe, uint32_t timestamp)
51 {
52         struct fd_device *dev = pipe->dev;
53         struct drm_msm_wait_fence req = {
54                         .fence = timestamp,
55         };
56         int ret;
57
58         get_abs_timeout(&req.timeout, 5000);
59
60         ret = drmCommandWrite(dev->fd, DRM_MSM_WAIT_FENCE, &req, sizeof(req));
61         if (ret) {
62                 ERROR_MSG("wait-fence failed! %d (%s)", ret, strerror(errno));
63                 return ret;
64         }
65
66         return 0;
67 }
68
69 static void msm_pipe_destroy(struct fd_pipe *pipe)
70 {
71         struct msm_pipe *msm_pipe = to_msm_pipe(pipe);
72         free(msm_pipe);
73 }
74
75 static struct fd_pipe_funcs funcs = {
76                 .ringbuffer_new = msm_ringbuffer_new,
77                 .get_param = msm_pipe_get_param,
78                 .wait = msm_pipe_wait,
79                 .destroy = msm_pipe_destroy,
80 };
81
82 static uint64_t get_param(struct fd_device *dev, uint32_t pipe, uint32_t param)
83 {
84         struct drm_msm_param req = {
85                         .pipe = pipe,
86                         .param = param,
87         };
88         int ret;
89
90         ret = drmCommandWriteRead(dev->fd, DRM_MSM_GET_PARAM, &req, sizeof(req));
91         if (ret) {
92                 ERROR_MSG("get-param failed! %d (%s)", ret, strerror(errno));
93                 return 0;
94         }
95
96         return req.value;
97 }
98
99 struct fd_pipe * msm_pipe_new(struct fd_device *dev, enum fd_pipe_id id)
100 {
101         static const uint32_t pipe_id[] = {
102                         [FD_PIPE_3D] = MSM_PIPE_3D0,
103                         [FD_PIPE_2D] = MSM_PIPE_2D0,
104         };
105         struct msm_pipe *msm_pipe = NULL;
106         struct fd_pipe *pipe = NULL;
107
108         msm_pipe = calloc(1, sizeof(*msm_pipe));
109         if (!msm_pipe) {
110                 ERROR_MSG("allocation failed");
111                 goto fail;
112         }
113
114         pipe = &msm_pipe->base;
115         pipe->funcs = &funcs;
116
117         msm_pipe->pipe = pipe_id[id];
118         msm_pipe->gpu_id = get_param(dev, pipe_id[id], MSM_PARAM_GPU_ID);
119         msm_pipe->gmem   = get_param(dev, pipe_id[id], MSM_PARAM_GMEM_SIZE);
120
121         if (! msm_pipe->gpu_id)
122                 goto fail;
123
124         INFO_MSG("Pipe Info:");
125         INFO_MSG(" GPU-id:          %d", msm_pipe->gpu_id);
126         INFO_MSG(" GMEM size:       0x%08x", msm_pipe->gmem);
127
128         return pipe;
129 fail:
130         if (pipe)
131                 fd_pipe_del(pipe);
132         return NULL;
133 }