msm_pipe.c revision 00a23bda
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
35static 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
55static 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	case FD_TIMESTAMP:
73		return query_param(pipe, MSM_PARAM_TIMESTAMP, value);
74	case FD_NR_RINGS:
75		return query_param(pipe, MSM_PARAM_NR_RINGS, value);
76	default:
77		ERROR_MSG("invalid param id: %d", param);
78		return -1;
79	}
80}
81
82static int msm_pipe_wait(struct fd_pipe *pipe, uint32_t timestamp,
83		uint64_t timeout)
84{
85	struct fd_device *dev = pipe->dev;
86	struct drm_msm_wait_fence req = {
87			.fence = timestamp,
88			.queueid = to_msm_pipe(pipe)->queue_id,
89	};
90	int ret;
91
92	get_abs_timeout(&req.timeout, timeout);
93
94	ret = drmCommandWrite(dev->fd, DRM_MSM_WAIT_FENCE, &req, sizeof(req));
95	if (ret) {
96		ERROR_MSG("wait-fence failed! %d (%s)", ret, strerror(errno));
97		return ret;
98	}
99
100	return 0;
101}
102
103static int open_submitqueue(struct fd_pipe *pipe, uint32_t prio)
104{
105	struct drm_msm_submitqueue req = {
106		.flags = 0,
107		.prio = prio,
108	};
109	uint64_t nr_rings = 1;
110	int ret;
111
112	if (fd_device_version(pipe->dev) < FD_VERSION_SUBMIT_QUEUES) {
113		to_msm_pipe(pipe)->queue_id = 0;
114		return 0;
115	}
116
117	msm_pipe_get_param(pipe, FD_NR_RINGS, &nr_rings);
118
119	req.prio = MIN2(req.prio, MAX2(nr_rings, 1) - 1);
120
121	ret = drmCommandWriteRead(pipe->dev->fd, DRM_MSM_SUBMITQUEUE_NEW,
122			&req, sizeof(req));
123	if (ret) {
124		ERROR_MSG("could not create submitqueue! %d (%s)", ret, strerror(errno));
125		return ret;
126	}
127
128	to_msm_pipe(pipe)->queue_id = req.id;
129	return 0;
130}
131
132static void close_submitqueue(struct fd_pipe *pipe, uint32_t queue_id)
133{
134	if (fd_device_version(pipe->dev) < FD_VERSION_SUBMIT_QUEUES)
135		return;
136
137	drmCommandWrite(pipe->dev->fd, DRM_MSM_SUBMITQUEUE_CLOSE,
138			&queue_id, sizeof(queue_id));
139}
140
141static void msm_pipe_destroy(struct fd_pipe *pipe)
142{
143	struct msm_pipe *msm_pipe = to_msm_pipe(pipe);
144	close_submitqueue(pipe, msm_pipe->queue_id);
145	free(msm_pipe);
146}
147
148static const struct fd_pipe_funcs funcs = {
149		.ringbuffer_new = msm_ringbuffer_new,
150		.get_param = msm_pipe_get_param,
151		.wait = msm_pipe_wait,
152		.destroy = msm_pipe_destroy,
153};
154
155static uint64_t get_param(struct fd_pipe *pipe, uint32_t param)
156{
157	uint64_t value;
158	int ret = query_param(pipe, param, &value);
159	if (ret) {
160		ERROR_MSG("get-param failed! %d (%s)", ret, strerror(errno));
161		return 0;
162	}
163	return value;
164}
165
166drm_private struct fd_pipe * msm_pipe_new(struct fd_device *dev,
167		enum fd_pipe_id id, uint32_t prio)
168{
169	static const uint32_t pipe_id[] = {
170			[FD_PIPE_3D] = MSM_PIPE_3D0,
171			[FD_PIPE_2D] = MSM_PIPE_2D0,
172	};
173	struct msm_pipe *msm_pipe = NULL;
174	struct fd_pipe *pipe = NULL;
175
176	msm_pipe = calloc(1, sizeof(*msm_pipe));
177	if (!msm_pipe) {
178		ERROR_MSG("allocation failed");
179		goto fail;
180	}
181
182	pipe = &msm_pipe->base;
183	pipe->funcs = &funcs;
184
185	/* initialize before get_param(): */
186	pipe->dev = dev;
187	msm_pipe->pipe = pipe_id[id];
188
189	/* these params should be supported since the first version of drm/msm: */
190	msm_pipe->gpu_id = get_param(pipe, MSM_PARAM_GPU_ID);
191	msm_pipe->gmem   = get_param(pipe, MSM_PARAM_GMEM_SIZE);
192	msm_pipe->chip_id = get_param(pipe, MSM_PARAM_CHIP_ID);
193
194	if (! msm_pipe->gpu_id)
195		goto fail;
196
197	INFO_MSG("Pipe Info:");
198	INFO_MSG(" GPU-id:          %d", msm_pipe->gpu_id);
199	INFO_MSG(" Chip-id:         0x%08x", msm_pipe->chip_id);
200	INFO_MSG(" GMEM size:       0x%08x", msm_pipe->gmem);
201
202	if (open_submitqueue(pipe, prio))
203		goto fail;
204
205	return pipe;
206fail:
207	if (pipe)
208		fd_pipe_del(pipe);
209	return NULL;
210}
211