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