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