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