1 /* -*- mode: C; c-file-style: "k&r"; tab-width 4; indent-tabs-mode: t; -*- */
4 * Copyright (C) 2012 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>
29 #include "freedreno_drmif.h"
30 #include "freedreno_priv.h"
33 static int getprop(int fd, enum kgsl_property_type type,
34 void *value, int sizebytes)
36 struct kgsl_device_getproperty req = {
39 .sizebytes = sizebytes,
41 return ioctl(fd, IOCTL_KGSL_DEVICE_GETPROPERTY, &req);
44 #define GETPROP(fd, prop, x) do { \
45 if (getprop((fd), KGSL_PROP_##prop, &(x), sizeof(x))) { \
46 ERROR_MSG("failed to get property: " #prop); \
51 struct fd_pipe * fd_pipe_new(struct fd_device *dev, enum fd_pipe_id id)
53 static const char *paths[] = {
54 [FD_PIPE_3D] = "/dev/kgsl-3d0",
55 [FD_PIPE_2D] = "/dev/kgsl-2d0",
57 struct kgsl_drawctxt_create req = {
58 .flags = 0x2000, /* ??? */
60 struct fd_pipe *pipe = NULL;
63 if (id > FD_PIPE_MAX) {
64 ERROR_MSG("invalid pipe id: %d", id);
68 fd = open(paths[id], O_RDWR);
70 ERROR_MSG("could not open %s device: %d (%s)",
71 paths[id], fd, strerror(errno));
75 ret = ioctl(fd, IOCTL_KGSL_DRAWCTXT_CREATE, &req);
77 ERROR_MSG("failed to allocate context: %d (%s)",
78 ret, strerror(errno));
82 pipe = calloc(1, sizeof(*pipe));
84 ERROR_MSG("allocation failed");
91 pipe->drawctxt_id = req.drawctxt_id;
93 list_inithead(&pipe->submit_list);
94 list_inithead(&pipe->pending_list);
96 GETPROP(fd, VERSION, pipe->version);
97 GETPROP(fd, DEVICE_INFO, pipe->devinfo);
99 INFO_MSG("Pipe Info:");
100 INFO_MSG(" Device: %s", paths[id]);
101 INFO_MSG(" Chip-id: %d.%d.%d.%d",
102 (pipe->devinfo.chip_id >> 24) & 0xff,
103 (pipe->devinfo.chip_id >> 16) & 0xff,
104 (pipe->devinfo.chip_id >> 8) & 0xff,
105 (pipe->devinfo.chip_id >> 0) & 0xff);
106 INFO_MSG(" Device-id: %d", pipe->devinfo.device_id);
107 INFO_MSG(" GPU-id: %d", pipe->devinfo.gpu_id);
108 INFO_MSG(" MMU enabled: %d", pipe->devinfo.mmu_enabled);
109 INFO_MSG(" GMEM Base addr: 0x%08x", pipe->devinfo.gmem_gpubaseaddr);
110 INFO_MSG(" GMEM size: 0x%08x", pipe->devinfo.gmem_sizebytes);
111 INFO_MSG(" Driver version: %d.%d",
112 pipe->version.drv_major, pipe->version.drv_minor);
113 INFO_MSG(" Device version: %d.%d",
114 pipe->version.dev_major, pipe->version.dev_minor);
123 void fd_pipe_del(struct fd_pipe *pipe)
125 struct kgsl_drawctxt_destroy req = {
126 .drawctxt_id = pipe->drawctxt_id,
129 if (pipe->drawctxt_id)
130 ioctl(pipe->fd, IOCTL_KGSL_DRAWCTXT_DESTROY, &req);
138 int fd_pipe_get_param(struct fd_pipe *pipe, enum fd_param_id param,
143 *value = pipe->devinfo.device_id;
146 *value = pipe->devinfo.gmem_sizebytes;
149 ERROR_MSG("invalid param id: %d", param);
154 int fd_pipe_wait(struct fd_pipe *pipe, uint32_t timestamp)
156 struct kgsl_device_waittimestamp req = {
157 .timestamp = timestamp,
163 ret = ioctl(pipe->fd, IOCTL_KGSL_DEVICE_WAITTIMESTAMP, &req);
164 } while ((ret == -1) && ((errno == EINTR) || (errno == EAGAIN)));
166 ERROR_MSG("waittimestamp failed! %d (%s)", ret, strerror(errno));
168 fd_pipe_process_pending(pipe, timestamp);
172 int fd_pipe_timestamp(struct fd_pipe *pipe, uint32_t *timestamp)
174 struct kgsl_cmdstream_readtimestamp req = {
175 .type = KGSL_TIMESTAMP_RETIRED
177 int ret = ioctl(pipe->fd, IOCTL_KGSL_CMDSTREAM_READTIMESTAMP, &req);
179 ERROR_MSG("readtimestamp failed! %d (%s)",
180 ret, strerror(errno));
183 *timestamp = req.timestamp;
187 /* add buffer to submit list when it is referenced in cmdstream: */
188 void fd_pipe_add_submit(struct fd_pipe *pipe, struct fd_bo *bo)
190 struct list_head *list = &bo->list[pipe->id];
191 if (LIST_IS_EMPTY(list)) {
196 list_addtail(list, &pipe->submit_list);
199 /* process buffers on submit list after flush: */
200 void fd_pipe_process_submit(struct fd_pipe *pipe, uint32_t timestamp)
202 struct fd_bo *bo, *tmp;
204 LIST_FOR_EACH_ENTRY_SAFE(bo, tmp, &pipe->submit_list, list[pipe->id]) {
205 struct list_head *list = &bo->list[pipe->id];
207 bo->timestamp[pipe->id] = timestamp;
208 list_addtail(list, &pipe->pending_list);
211 if (!fd_pipe_timestamp(pipe, ×tamp))
212 fd_pipe_process_pending(pipe, timestamp);
215 void fd_pipe_process_pending(struct fd_pipe *pipe, uint32_t timestamp)
217 struct fd_bo *bo, *tmp;
219 LIST_FOR_EACH_ENTRY_SAFE(bo, tmp, &pipe->pending_list, list[pipe->id]) {
220 struct list_head *list = &bo->list[pipe->id];
221 if (bo->timestamp[pipe->id] > timestamp)
224 bo->timestamp[pipe->id] = 0;