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 "kgsl_priv.h"
34 
35 
kgsl_pipe_get_param(struct fd_pipe * pipe,enum fd_param_id param,uint64_t * value)36 static int kgsl_pipe_get_param(struct fd_pipe *pipe,
37 		enum fd_param_id param, uint64_t *value)
38 {
39 	struct kgsl_pipe *kgsl_pipe = to_kgsl_pipe(pipe);
40 	switch (param) {
41 	case FD_DEVICE_ID:
42 		*value = kgsl_pipe->devinfo.device_id;
43 		return 0;
44 	case FD_GPU_ID:
45 		*value = kgsl_pipe->devinfo.gpu_id;
46 		return 0;
47 	case FD_GMEM_SIZE:
48 		*value = kgsl_pipe->devinfo.gmem_sizebytes;
49 		return 0;
50 	case FD_CHIP_ID:
51 		*value = kgsl_pipe->devinfo.chip_id;
52 		return 0;
53 	default:
54 		ERROR_MSG("invalid param id: %d", param);
55 		return -1;
56 	}
57 }
58 
kgsl_pipe_wait(struct fd_pipe * pipe,uint32_t timestamp,uint64_t timeout)59 static int kgsl_pipe_wait(struct fd_pipe *pipe, uint32_t timestamp,
60 		uint64_t timeout)
61 {
62 	struct kgsl_pipe *kgsl_pipe = to_kgsl_pipe(pipe);
63 	struct kgsl_device_waittimestamp req = {
64 			.timestamp = timestamp,
65 			.timeout   = 5000,
66 	};
67 	int ret;
68 
69 	do {
70 		ret = ioctl(kgsl_pipe->fd, IOCTL_KGSL_DEVICE_WAITTIMESTAMP, &req);
71 	} while ((ret == -1) && ((errno == EINTR) || (errno == EAGAIN)));
72 	if (ret)
73 		ERROR_MSG("waittimestamp failed! %d (%s)", ret, strerror(errno));
74 	else
75 		kgsl_pipe_process_pending(kgsl_pipe, timestamp);
76 	return ret;
77 }
78 
kgsl_pipe_timestamp(struct kgsl_pipe * kgsl_pipe,uint32_t * timestamp)79 drm_private int kgsl_pipe_timestamp(struct kgsl_pipe *kgsl_pipe,
80 		uint32_t *timestamp)
81 {
82 	struct kgsl_cmdstream_readtimestamp req = {
83 			.type = KGSL_TIMESTAMP_RETIRED
84 	};
85 	int ret = ioctl(kgsl_pipe->fd, IOCTL_KGSL_CMDSTREAM_READTIMESTAMP, &req);
86 	if (ret) {
87 		ERROR_MSG("readtimestamp failed! %d (%s)",
88 				ret, strerror(errno));
89 		return ret;
90 	}
91 	*timestamp = req.timestamp;
92 	return 0;
93 }
94 
kgsl_pipe_destroy(struct fd_pipe * pipe)95 static void kgsl_pipe_destroy(struct fd_pipe *pipe)
96 {
97 	struct kgsl_pipe *kgsl_pipe = to_kgsl_pipe(pipe);
98 	struct kgsl_drawctxt_destroy req = {
99 			.drawctxt_id = kgsl_pipe->drawctxt_id,
100 	};
101 
102 	if (kgsl_pipe->drawctxt_id)
103 		ioctl(kgsl_pipe->fd, IOCTL_KGSL_DRAWCTXT_DESTROY, &req);
104 
105 	if (kgsl_pipe->fd >= 0)
106 		close(kgsl_pipe->fd);
107 
108 	free(kgsl_pipe);
109 }
110 
111 static const struct fd_pipe_funcs funcs = {
112 		.ringbuffer_new = kgsl_ringbuffer_new,
113 		.get_param = kgsl_pipe_get_param,
114 		.wait = kgsl_pipe_wait,
115 		.destroy = kgsl_pipe_destroy,
116 };
117 
is_kgsl_pipe(struct fd_pipe * pipe)118 drm_private int is_kgsl_pipe(struct fd_pipe *pipe)
119 {
120 	return pipe->funcs == &funcs;
121 }
122 
123 /* add buffer to submit list when it is referenced in cmdstream: */
kgsl_pipe_add_submit(struct kgsl_pipe * kgsl_pipe,struct kgsl_bo * kgsl_bo)124 drm_private void kgsl_pipe_add_submit(struct kgsl_pipe *kgsl_pipe,
125 		struct kgsl_bo *kgsl_bo)
126 {
127 	struct fd_pipe *pipe = &kgsl_pipe->base;
128 	struct fd_bo *bo = &kgsl_bo->base;
129 	struct list_head *list = &kgsl_bo->list[pipe->id];
130 	if (LIST_IS_EMPTY(list)) {
131 		fd_bo_ref(bo);
132 	} else {
133 		list_del(list);
134 	}
135 	list_addtail(list, &kgsl_pipe->submit_list);
136 }
137 
138 /* prepare buffers on submit list before flush: */
kgsl_pipe_pre_submit(struct kgsl_pipe * kgsl_pipe)139 drm_private void kgsl_pipe_pre_submit(struct kgsl_pipe *kgsl_pipe)
140 {
141 	struct fd_pipe *pipe = &kgsl_pipe->base;
142 	struct kgsl_bo *kgsl_bo = NULL;
143 
144 	if (!kgsl_pipe->p3d)
145 		kgsl_pipe->p3d = fd_pipe_new(pipe->dev, FD_PIPE_3D);
146 
147 	LIST_FOR_EACH_ENTRY(kgsl_bo, &kgsl_pipe->submit_list, list[pipe->id]) {
148 		uint32_t timestamp = kgsl_bo_get_timestamp(kgsl_bo);
149 		if (timestamp)
150 			fd_pipe_wait(kgsl_pipe->p3d, timestamp);
151 	}
152 }
153 
154 /* process buffers on submit list after flush: */
kgsl_pipe_post_submit(struct kgsl_pipe * kgsl_pipe,uint32_t timestamp)155 drm_private void kgsl_pipe_post_submit(struct kgsl_pipe *kgsl_pipe,
156 		uint32_t timestamp)
157 {
158 	struct fd_pipe *pipe = &kgsl_pipe->base;
159 	struct kgsl_bo *kgsl_bo = NULL, *tmp;
160 
161 	LIST_FOR_EACH_ENTRY_SAFE(kgsl_bo, tmp, &kgsl_pipe->submit_list, list[pipe->id]) {
162 		struct list_head *list = &kgsl_bo->list[pipe->id];
163 		list_del(list);
164 		kgsl_bo->timestamp[pipe->id] = timestamp;
165 		list_addtail(list, &kgsl_pipe->pending_list);
166 
167 		kgsl_bo_set_timestamp(kgsl_bo, timestamp);
168 	}
169 
170 	if (!kgsl_pipe_timestamp(kgsl_pipe, &timestamp))
171 		kgsl_pipe_process_pending(kgsl_pipe, timestamp);
172 }
173 
kgsl_pipe_process_pending(struct kgsl_pipe * kgsl_pipe,uint32_t timestamp)174 drm_private void kgsl_pipe_process_pending(struct kgsl_pipe *kgsl_pipe,
175 		uint32_t timestamp)
176 {
177 	struct fd_pipe *pipe = &kgsl_pipe->base;
178 	struct kgsl_bo *kgsl_bo = NULL, *tmp;
179 
180 	LIST_FOR_EACH_ENTRY_SAFE(kgsl_bo, tmp, &kgsl_pipe->pending_list, list[pipe->id]) {
181 		struct list_head *list = &kgsl_bo->list[pipe->id];
182 		if (kgsl_bo->timestamp[pipe->id] > timestamp)
183 			return;
184 		list_delinit(list);
185 		kgsl_bo->timestamp[pipe->id] = 0;
186 		fd_bo_del(&kgsl_bo->base);
187 	}
188 }
189 
getprop(int fd,enum kgsl_property_type type,void * value,int sizebytes)190 static int getprop(int fd, enum kgsl_property_type type,
191 		void *value, int sizebytes)
192 {
193 	struct kgsl_device_getproperty req = {
194 			.type = type,
195 			.value = value,
196 			.sizebytes = sizebytes,
197 	};
198 	return ioctl(fd, IOCTL_KGSL_DEVICE_GETPROPERTY, &req);
199 }
200 
201 #define GETPROP(fd, prop, x) do { \
202 	if (getprop((fd), KGSL_PROP_##prop, &(x), sizeof(x))) {     \
203 		ERROR_MSG("failed to get property: " #prop);            \
204 		goto fail;                                              \
205 	} } while (0)
206 
207 
kgsl_pipe_new(struct fd_device * dev,enum fd_pipe_id id)208 drm_private struct fd_pipe * kgsl_pipe_new(struct fd_device *dev,
209 		enum fd_pipe_id id)
210 {
211 	static const char *paths[] = {
212 			[FD_PIPE_3D] = "/dev/kgsl-3d0",
213 			[FD_PIPE_2D] = "/dev/kgsl-2d0",
214 	};
215 	struct kgsl_drawctxt_create req = {
216 			.flags = 0x2000, /* ??? */
217 	};
218 	struct kgsl_pipe *kgsl_pipe = NULL;
219 	struct fd_pipe *pipe = NULL;
220 	int ret, fd;
221 
222 	fd = open(paths[id], O_RDWR);
223 	if (fd < 0) {
224 		ERROR_MSG("could not open %s device: %d (%s)",
225 				paths[id], fd, strerror(errno));
226 		goto fail;
227 	}
228 
229 	ret = ioctl(fd, IOCTL_KGSL_DRAWCTXT_CREATE, &req);
230 	if (ret) {
231 		ERROR_MSG("failed to allocate context: %d (%s)",
232 				ret, strerror(errno));
233 		goto fail;
234 	}
235 
236 	kgsl_pipe = calloc(1, sizeof(*kgsl_pipe));
237 	if (!kgsl_pipe) {
238 		ERROR_MSG("allocation failed");
239 		goto fail;
240 	}
241 
242 	pipe = &kgsl_pipe->base;
243 	pipe->funcs = &funcs;
244 
245 	kgsl_pipe->fd = fd;
246 	kgsl_pipe->drawctxt_id = req.drawctxt_id;
247 
248 	list_inithead(&kgsl_pipe->submit_list);
249 	list_inithead(&kgsl_pipe->pending_list);
250 
251 	GETPROP(fd, VERSION,     kgsl_pipe->version);
252 	GETPROP(fd, DEVICE_INFO, kgsl_pipe->devinfo);
253 
254 	INFO_MSG("Pipe Info:");
255 	INFO_MSG(" Device:          %s", paths[id]);
256 	INFO_MSG(" Chip-id:         %d.%d.%d.%d",
257 			(kgsl_pipe->devinfo.chip_id >> 24) & 0xff,
258 			(kgsl_pipe->devinfo.chip_id >> 16) & 0xff,
259 			(kgsl_pipe->devinfo.chip_id >>  8) & 0xff,
260 			(kgsl_pipe->devinfo.chip_id >>  0) & 0xff);
261 	INFO_MSG(" Device-id:       %d", kgsl_pipe->devinfo.device_id);
262 	INFO_MSG(" GPU-id:          %d", kgsl_pipe->devinfo.gpu_id);
263 	INFO_MSG(" MMU enabled:     %d", kgsl_pipe->devinfo.mmu_enabled);
264 	INFO_MSG(" GMEM Base addr:  0x%08x", kgsl_pipe->devinfo.gmem_gpubaseaddr);
265 	INFO_MSG(" GMEM size:       0x%08x", kgsl_pipe->devinfo.gmem_sizebytes);
266 	INFO_MSG(" Driver version:  %d.%d",
267 			kgsl_pipe->version.drv_major, kgsl_pipe->version.drv_minor);
268 	INFO_MSG(" Device version:  %d.%d",
269 			kgsl_pipe->version.dev_major, kgsl_pipe->version.dev_minor);
270 
271 	return pipe;
272 fail:
273 	if (pipe)
274 		fd_pipe_del(pipe);
275 	return NULL;
276 }
277