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