1 /* -*- mode: C; c-file-style: "k&r"; tab-width 4; indent-tabs-mode: t; -*- */
4 * Copyright (C) 2013 Rob Clark <robclark@freedesktop.org>
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:
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
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
26 * Rob Clark <robclark@freedesktop.org>
33 #include "kgsl_priv.h"
36 static int kgsl_pipe_get_param(struct fd_pipe *pipe,
37 enum fd_param_id param, uint64_t *value)
39 struct kgsl_pipe *kgsl_pipe = to_kgsl_pipe(pipe);
42 *value = kgsl_pipe->devinfo.device_id;
45 *value = kgsl_pipe->devinfo.gpu_id;
48 *value = kgsl_pipe->devinfo.gmem_sizebytes;
51 *value = kgsl_pipe->devinfo.chip_id;
55 /* unsupported on kgsl */
58 ERROR_MSG("invalid param id: %d", param);
63 static int kgsl_pipe_wait(struct fd_pipe *pipe, uint32_t timestamp,
66 struct kgsl_pipe *kgsl_pipe = to_kgsl_pipe(pipe);
67 struct kgsl_device_waittimestamp req = {
68 .timestamp = timestamp,
74 ret = ioctl(kgsl_pipe->fd, IOCTL_KGSL_DEVICE_WAITTIMESTAMP, &req);
75 } while ((ret == -1) && ((errno == EINTR) || (errno == EAGAIN)));
77 ERROR_MSG("waittimestamp failed! %d (%s)", ret, strerror(errno));
79 kgsl_pipe_process_pending(kgsl_pipe, timestamp);
83 drm_private int kgsl_pipe_timestamp(struct kgsl_pipe *kgsl_pipe,
86 struct kgsl_cmdstream_readtimestamp req = {
87 .type = KGSL_TIMESTAMP_RETIRED
89 int ret = ioctl(kgsl_pipe->fd, IOCTL_KGSL_CMDSTREAM_READTIMESTAMP, &req);
91 ERROR_MSG("readtimestamp failed! %d (%s)",
92 ret, strerror(errno));
95 *timestamp = req.timestamp;
99 static void kgsl_pipe_destroy(struct fd_pipe *pipe)
101 struct kgsl_pipe *kgsl_pipe = to_kgsl_pipe(pipe);
102 struct kgsl_drawctxt_destroy req = {
103 .drawctxt_id = kgsl_pipe->drawctxt_id,
106 if (kgsl_pipe->drawctxt_id)
107 ioctl(kgsl_pipe->fd, IOCTL_KGSL_DRAWCTXT_DESTROY, &req);
109 if (kgsl_pipe->fd >= 0)
110 close(kgsl_pipe->fd);
115 static const struct fd_pipe_funcs funcs = {
116 .ringbuffer_new = kgsl_ringbuffer_new,
117 .get_param = kgsl_pipe_get_param,
118 .wait = kgsl_pipe_wait,
119 .destroy = kgsl_pipe_destroy,
122 drm_private int is_kgsl_pipe(struct fd_pipe *pipe)
124 return pipe->funcs == &funcs;
127 /* add buffer to submit list when it is referenced in cmdstream: */
128 drm_private void kgsl_pipe_add_submit(struct kgsl_pipe *kgsl_pipe,
129 struct kgsl_bo *kgsl_bo)
131 struct fd_pipe *pipe = &kgsl_pipe->base;
132 struct fd_bo *bo = &kgsl_bo->base;
133 struct list_head *list = &kgsl_bo->list[pipe->id];
134 if (LIST_IS_EMPTY(list)) {
139 list_addtail(list, &kgsl_pipe->submit_list);
142 /* prepare buffers on submit list before flush: */
143 drm_private void kgsl_pipe_pre_submit(struct kgsl_pipe *kgsl_pipe)
145 struct fd_pipe *pipe = &kgsl_pipe->base;
146 struct kgsl_bo *kgsl_bo = NULL;
149 kgsl_pipe->p3d = fd_pipe_new(pipe->dev, FD_PIPE_3D);
151 LIST_FOR_EACH_ENTRY(kgsl_bo, &kgsl_pipe->submit_list, list[pipe->id]) {
152 uint32_t timestamp = kgsl_bo_get_timestamp(kgsl_bo);
154 fd_pipe_wait(kgsl_pipe->p3d, timestamp);
158 /* process buffers on submit list after flush: */
159 drm_private void kgsl_pipe_post_submit(struct kgsl_pipe *kgsl_pipe,
162 struct fd_pipe *pipe = &kgsl_pipe->base;
163 struct kgsl_bo *kgsl_bo = NULL, *tmp;
165 LIST_FOR_EACH_ENTRY_SAFE(kgsl_bo, tmp, &kgsl_pipe->submit_list, list[pipe->id]) {
166 struct list_head *list = &kgsl_bo->list[pipe->id];
168 kgsl_bo->timestamp[pipe->id] = timestamp;
169 list_addtail(list, &kgsl_pipe->pending_list);
171 kgsl_bo_set_timestamp(kgsl_bo, timestamp);
174 if (!kgsl_pipe_timestamp(kgsl_pipe, ×tamp))
175 kgsl_pipe_process_pending(kgsl_pipe, timestamp);
178 drm_private void kgsl_pipe_process_pending(struct kgsl_pipe *kgsl_pipe,
181 struct fd_pipe *pipe = &kgsl_pipe->base;
182 struct kgsl_bo *kgsl_bo = NULL, *tmp;
184 LIST_FOR_EACH_ENTRY_SAFE(kgsl_bo, tmp, &kgsl_pipe->pending_list, list[pipe->id]) {
185 struct list_head *list = &kgsl_bo->list[pipe->id];
186 if (kgsl_bo->timestamp[pipe->id] > timestamp)
189 kgsl_bo->timestamp[pipe->id] = 0;
190 fd_bo_del(&kgsl_bo->base);
194 static int getprop(int fd, enum kgsl_property_type type,
195 void *value, int sizebytes)
197 struct kgsl_device_getproperty req = {
200 .sizebytes = sizebytes,
202 return ioctl(fd, IOCTL_KGSL_DEVICE_GETPROPERTY, &req);
205 #define GETPROP(fd, prop, x) do { \
206 if (getprop((fd), KGSL_PROP_##prop, &(x), sizeof(x))) { \
207 ERROR_MSG("failed to get property: " #prop); \
212 drm_private struct fd_pipe * kgsl_pipe_new(struct fd_device *dev,
215 static const char *paths[] = {
216 [FD_PIPE_3D] = "/dev/kgsl-3d0",
217 [FD_PIPE_2D] = "/dev/kgsl-2d0",
219 struct kgsl_drawctxt_create req = {
220 .flags = 0x2000, /* ??? */
222 struct kgsl_pipe *kgsl_pipe = NULL;
223 struct fd_pipe *pipe = NULL;
226 fd = open(paths[id], O_RDWR);
228 ERROR_MSG("could not open %s device: %d (%s)",
229 paths[id], fd, strerror(errno));
233 ret = ioctl(fd, IOCTL_KGSL_DRAWCTXT_CREATE, &req);
235 ERROR_MSG("failed to allocate context: %d (%s)",
236 ret, strerror(errno));
240 kgsl_pipe = calloc(1, sizeof(*kgsl_pipe));
242 ERROR_MSG("allocation failed");
246 pipe = &kgsl_pipe->base;
247 pipe->funcs = &funcs;
250 kgsl_pipe->drawctxt_id = req.drawctxt_id;
252 list_inithead(&kgsl_pipe->submit_list);
253 list_inithead(&kgsl_pipe->pending_list);
255 GETPROP(fd, VERSION, kgsl_pipe->version);
256 GETPROP(fd, DEVICE_INFO, kgsl_pipe->devinfo);
258 INFO_MSG("Pipe Info:");
259 INFO_MSG(" Device: %s", paths[id]);
260 INFO_MSG(" Chip-id: %d.%d.%d.%d",
261 (kgsl_pipe->devinfo.chip_id >> 24) & 0xff,
262 (kgsl_pipe->devinfo.chip_id >> 16) & 0xff,
263 (kgsl_pipe->devinfo.chip_id >> 8) & 0xff,
264 (kgsl_pipe->devinfo.chip_id >> 0) & 0xff);
265 INFO_MSG(" Device-id: %d", kgsl_pipe->devinfo.device_id);
266 INFO_MSG(" GPU-id: %d", kgsl_pipe->devinfo.gpu_id);
267 INFO_MSG(" MMU enabled: %d", kgsl_pipe->devinfo.mmu_enabled);
268 INFO_MSG(" GMEM Base addr: 0x%08x", kgsl_pipe->devinfo.gmem_gpubaseaddr);
269 INFO_MSG(" GMEM size: 0x%08x", kgsl_pipe->devinfo.gmem_sizebytes);
270 INFO_MSG(" Driver version: %d.%d",
271 kgsl_pipe->version.drv_major, kgsl_pipe->version.drv_minor);
272 INFO_MSG(" Device version: %d.%d",
273 kgsl_pipe->version.dev_major, kgsl_pipe->version.dev_minor);