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