1e88f27b3Smrg/* -*- mode: C; c-file-style: "k&r"; tab-width 4; indent-tabs-mode: t; -*- */ 2e88f27b3Smrg 3e88f27b3Smrg/* 4e88f27b3Smrg * Copyright (C) 2013 Rob Clark <robclark@freedesktop.org> 5e88f27b3Smrg * 6e88f27b3Smrg * Permission is hereby granted, free of charge, to any person obtaining a 7e88f27b3Smrg * copy of this software and associated documentation files (the "Software"), 8e88f27b3Smrg * to deal in the Software without restriction, including without limitation 9e88f27b3Smrg * the rights to use, copy, modify, merge, publish, distribute, sublicense, 10e88f27b3Smrg * and/or sell copies of the Software, and to permit persons to whom the 11e88f27b3Smrg * Software is furnished to do so, subject to the following conditions: 12e88f27b3Smrg * 13e88f27b3Smrg * The above copyright notice and this permission notice (including the next 14e88f27b3Smrg * paragraph) shall be included in all copies or substantial portions of the 15e88f27b3Smrg * Software. 16e88f27b3Smrg * 17e88f27b3Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18e88f27b3Smrg * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19e88f27b3Smrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 20e88f27b3Smrg * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21e88f27b3Smrg * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22e88f27b3Smrg * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 23e88f27b3Smrg * SOFTWARE. 24e88f27b3Smrg * 25e88f27b3Smrg * Authors: 26e88f27b3Smrg * Rob Clark <robclark@freedesktop.org> 27e88f27b3Smrg */ 28e88f27b3Smrg 29e88f27b3Smrg#include "kgsl_priv.h" 30e88f27b3Smrg 31e88f27b3Smrg 32e88f27b3Smrgstatic int kgsl_pipe_get_param(struct fd_pipe *pipe, 33e88f27b3Smrg enum fd_param_id param, uint64_t *value) 34e88f27b3Smrg{ 35e88f27b3Smrg struct kgsl_pipe *kgsl_pipe = to_kgsl_pipe(pipe); 36e88f27b3Smrg switch (param) { 37e88f27b3Smrg case FD_DEVICE_ID: 38e88f27b3Smrg *value = kgsl_pipe->devinfo.device_id; 39e88f27b3Smrg return 0; 40e88f27b3Smrg case FD_GPU_ID: 41e88f27b3Smrg *value = kgsl_pipe->devinfo.gpu_id; 42e88f27b3Smrg return 0; 43e88f27b3Smrg case FD_GMEM_SIZE: 44e88f27b3Smrg *value = kgsl_pipe->devinfo.gmem_sizebytes; 45e88f27b3Smrg return 0; 46baaff307Smrg case FD_CHIP_ID: 47baaff307Smrg *value = kgsl_pipe->devinfo.chip_id; 48baaff307Smrg return 0; 493f012e29Smrg case FD_MAX_FREQ: 503f012e29Smrg case FD_TIMESTAMP: 5100a23bdaSmrg case FD_NR_RINGS: 523f012e29Smrg /* unsupported on kgsl */ 533f012e29Smrg return -1; 54e88f27b3Smrg default: 55e88f27b3Smrg ERROR_MSG("invalid param id: %d", param); 56e88f27b3Smrg return -1; 57e88f27b3Smrg } 58e88f27b3Smrg} 59e88f27b3Smrg 603f012e29Smrgstatic int kgsl_pipe_wait(struct fd_pipe *pipe, uint32_t timestamp, 613f012e29Smrg uint64_t timeout) 62e88f27b3Smrg{ 63e88f27b3Smrg struct kgsl_pipe *kgsl_pipe = to_kgsl_pipe(pipe); 64e88f27b3Smrg struct kgsl_device_waittimestamp req = { 65e88f27b3Smrg .timestamp = timestamp, 66e88f27b3Smrg .timeout = 5000, 67e88f27b3Smrg }; 68e88f27b3Smrg int ret; 69e88f27b3Smrg 70e88f27b3Smrg do { 71e88f27b3Smrg ret = ioctl(kgsl_pipe->fd, IOCTL_KGSL_DEVICE_WAITTIMESTAMP, &req); 72e88f27b3Smrg } while ((ret == -1) && ((errno == EINTR) || (errno == EAGAIN))); 73e88f27b3Smrg if (ret) 74e88f27b3Smrg ERROR_MSG("waittimestamp failed! %d (%s)", ret, strerror(errno)); 75e88f27b3Smrg else 76e88f27b3Smrg kgsl_pipe_process_pending(kgsl_pipe, timestamp); 77e88f27b3Smrg return ret; 78e88f27b3Smrg} 79e88f27b3Smrg 80e6188e58Smrgdrm_private int kgsl_pipe_timestamp(struct kgsl_pipe *kgsl_pipe, 81e6188e58Smrg uint32_t *timestamp) 82e88f27b3Smrg{ 83e88f27b3Smrg struct kgsl_cmdstream_readtimestamp req = { 84e88f27b3Smrg .type = KGSL_TIMESTAMP_RETIRED 85e88f27b3Smrg }; 86e88f27b3Smrg int ret = ioctl(kgsl_pipe->fd, IOCTL_KGSL_CMDSTREAM_READTIMESTAMP, &req); 87e88f27b3Smrg if (ret) { 88e88f27b3Smrg ERROR_MSG("readtimestamp failed! %d (%s)", 89e88f27b3Smrg ret, strerror(errno)); 90e88f27b3Smrg return ret; 91e88f27b3Smrg } 92e88f27b3Smrg *timestamp = req.timestamp; 93e88f27b3Smrg return 0; 94e88f27b3Smrg} 95e88f27b3Smrg 96e88f27b3Smrgstatic void kgsl_pipe_destroy(struct fd_pipe *pipe) 97e88f27b3Smrg{ 98e88f27b3Smrg struct kgsl_pipe *kgsl_pipe = to_kgsl_pipe(pipe); 99e88f27b3Smrg struct kgsl_drawctxt_destroy req = { 100e88f27b3Smrg .drawctxt_id = kgsl_pipe->drawctxt_id, 101e88f27b3Smrg }; 102e88f27b3Smrg 103e88f27b3Smrg if (kgsl_pipe->drawctxt_id) 104e88f27b3Smrg ioctl(kgsl_pipe->fd, IOCTL_KGSL_DRAWCTXT_DESTROY, &req); 105e88f27b3Smrg 1063f012e29Smrg if (kgsl_pipe->fd >= 0) 107e88f27b3Smrg close(kgsl_pipe->fd); 108e88f27b3Smrg 109e88f27b3Smrg free(kgsl_pipe); 110e88f27b3Smrg} 111e88f27b3Smrg 1123f012e29Smrgstatic const struct fd_pipe_funcs funcs = { 113e88f27b3Smrg .ringbuffer_new = kgsl_ringbuffer_new, 114e88f27b3Smrg .get_param = kgsl_pipe_get_param, 115e88f27b3Smrg .wait = kgsl_pipe_wait, 116e88f27b3Smrg .destroy = kgsl_pipe_destroy, 117e88f27b3Smrg}; 118e88f27b3Smrg 119e6188e58Smrgdrm_private int is_kgsl_pipe(struct fd_pipe *pipe) 120e88f27b3Smrg{ 121e88f27b3Smrg return pipe->funcs == &funcs; 122e88f27b3Smrg} 123e88f27b3Smrg 124e88f27b3Smrg/* add buffer to submit list when it is referenced in cmdstream: */ 125e6188e58Smrgdrm_private void kgsl_pipe_add_submit(struct kgsl_pipe *kgsl_pipe, 126e88f27b3Smrg struct kgsl_bo *kgsl_bo) 127e88f27b3Smrg{ 128e88f27b3Smrg struct fd_pipe *pipe = &kgsl_pipe->base; 129e88f27b3Smrg struct fd_bo *bo = &kgsl_bo->base; 130e88f27b3Smrg struct list_head *list = &kgsl_bo->list[pipe->id]; 131e88f27b3Smrg if (LIST_IS_EMPTY(list)) { 132e88f27b3Smrg fd_bo_ref(bo); 133e88f27b3Smrg } else { 134e88f27b3Smrg list_del(list); 135e88f27b3Smrg } 136e88f27b3Smrg list_addtail(list, &kgsl_pipe->submit_list); 137e88f27b3Smrg} 138e88f27b3Smrg 139e88f27b3Smrg/* prepare buffers on submit list before flush: */ 140e6188e58Smrgdrm_private void kgsl_pipe_pre_submit(struct kgsl_pipe *kgsl_pipe) 141e88f27b3Smrg{ 142e88f27b3Smrg struct fd_pipe *pipe = &kgsl_pipe->base; 143e88f27b3Smrg struct kgsl_bo *kgsl_bo = NULL; 144e88f27b3Smrg 145e88f27b3Smrg if (!kgsl_pipe->p3d) 146e88f27b3Smrg kgsl_pipe->p3d = fd_pipe_new(pipe->dev, FD_PIPE_3D); 147e88f27b3Smrg 148e88f27b3Smrg LIST_FOR_EACH_ENTRY(kgsl_bo, &kgsl_pipe->submit_list, list[pipe->id]) { 149e88f27b3Smrg uint32_t timestamp = kgsl_bo_get_timestamp(kgsl_bo); 150e88f27b3Smrg if (timestamp) 151e88f27b3Smrg fd_pipe_wait(kgsl_pipe->p3d, timestamp); 152e88f27b3Smrg } 153e88f27b3Smrg} 154e88f27b3Smrg 155e88f27b3Smrg/* process buffers on submit list after flush: */ 156e6188e58Smrgdrm_private void kgsl_pipe_post_submit(struct kgsl_pipe *kgsl_pipe, 157e6188e58Smrg uint32_t timestamp) 158e88f27b3Smrg{ 159e88f27b3Smrg struct fd_pipe *pipe = &kgsl_pipe->base; 160e88f27b3Smrg struct kgsl_bo *kgsl_bo = NULL, *tmp; 161e88f27b3Smrg 162e88f27b3Smrg LIST_FOR_EACH_ENTRY_SAFE(kgsl_bo, tmp, &kgsl_pipe->submit_list, list[pipe->id]) { 163e88f27b3Smrg struct list_head *list = &kgsl_bo->list[pipe->id]; 164e88f27b3Smrg list_del(list); 165e88f27b3Smrg kgsl_bo->timestamp[pipe->id] = timestamp; 166e88f27b3Smrg list_addtail(list, &kgsl_pipe->pending_list); 167e88f27b3Smrg 168e88f27b3Smrg kgsl_bo_set_timestamp(kgsl_bo, timestamp); 169e88f27b3Smrg } 170e88f27b3Smrg 171e88f27b3Smrg if (!kgsl_pipe_timestamp(kgsl_pipe, ×tamp)) 172e88f27b3Smrg kgsl_pipe_process_pending(kgsl_pipe, timestamp); 173e88f27b3Smrg} 174e88f27b3Smrg 175e6188e58Smrgdrm_private void kgsl_pipe_process_pending(struct kgsl_pipe *kgsl_pipe, 176e6188e58Smrg uint32_t timestamp) 177e88f27b3Smrg{ 178e88f27b3Smrg struct fd_pipe *pipe = &kgsl_pipe->base; 179e88f27b3Smrg struct kgsl_bo *kgsl_bo = NULL, *tmp; 180e88f27b3Smrg 181e88f27b3Smrg LIST_FOR_EACH_ENTRY_SAFE(kgsl_bo, tmp, &kgsl_pipe->pending_list, list[pipe->id]) { 182e88f27b3Smrg struct list_head *list = &kgsl_bo->list[pipe->id]; 183e88f27b3Smrg if (kgsl_bo->timestamp[pipe->id] > timestamp) 184e88f27b3Smrg return; 185e88f27b3Smrg list_delinit(list); 186e88f27b3Smrg kgsl_bo->timestamp[pipe->id] = 0; 187e88f27b3Smrg fd_bo_del(&kgsl_bo->base); 188e88f27b3Smrg } 189e88f27b3Smrg} 190e88f27b3Smrg 191e88f27b3Smrgstatic int getprop(int fd, enum kgsl_property_type type, 192e88f27b3Smrg void *value, int sizebytes) 193e88f27b3Smrg{ 194e88f27b3Smrg struct kgsl_device_getproperty req = { 195e88f27b3Smrg .type = type, 196e88f27b3Smrg .value = value, 197e88f27b3Smrg .sizebytes = sizebytes, 198e88f27b3Smrg }; 199e88f27b3Smrg return ioctl(fd, IOCTL_KGSL_DEVICE_GETPROPERTY, &req); 200e88f27b3Smrg} 201e88f27b3Smrg 202e88f27b3Smrg#define GETPROP(fd, prop, x) do { \ 203e88f27b3Smrg if (getprop((fd), KGSL_PROP_##prop, &(x), sizeof(x))) { \ 204e88f27b3Smrg ERROR_MSG("failed to get property: " #prop); \ 205e88f27b3Smrg goto fail; \ 206e88f27b3Smrg } } while (0) 207e88f27b3Smrg 208e88f27b3Smrg 209e6188e58Smrgdrm_private struct fd_pipe * kgsl_pipe_new(struct fd_device *dev, 21000a23bdaSmrg enum fd_pipe_id id, uint32_t prio) 211e88f27b3Smrg{ 212e88f27b3Smrg static const char *paths[] = { 213e88f27b3Smrg [FD_PIPE_3D] = "/dev/kgsl-3d0", 214e88f27b3Smrg [FD_PIPE_2D] = "/dev/kgsl-2d0", 215e88f27b3Smrg }; 216e88f27b3Smrg struct kgsl_drawctxt_create req = { 217e88f27b3Smrg .flags = 0x2000, /* ??? */ 218e88f27b3Smrg }; 219e88f27b3Smrg struct kgsl_pipe *kgsl_pipe = NULL; 220e88f27b3Smrg struct fd_pipe *pipe = NULL; 221e88f27b3Smrg int ret, fd; 222e88f27b3Smrg 223e88f27b3Smrg fd = open(paths[id], O_RDWR); 224e88f27b3Smrg if (fd < 0) { 225e88f27b3Smrg ERROR_MSG("could not open %s device: %d (%s)", 226e88f27b3Smrg paths[id], fd, strerror(errno)); 227e88f27b3Smrg goto fail; 228e88f27b3Smrg } 229e88f27b3Smrg 230e88f27b3Smrg ret = ioctl(fd, IOCTL_KGSL_DRAWCTXT_CREATE, &req); 231e88f27b3Smrg if (ret) { 232e88f27b3Smrg ERROR_MSG("failed to allocate context: %d (%s)", 233e88f27b3Smrg ret, strerror(errno)); 234e88f27b3Smrg goto fail; 235e88f27b3Smrg } 236e88f27b3Smrg 237e88f27b3Smrg kgsl_pipe = calloc(1, sizeof(*kgsl_pipe)); 238e88f27b3Smrg if (!kgsl_pipe) { 239e88f27b3Smrg ERROR_MSG("allocation failed"); 240e88f27b3Smrg goto fail; 241e88f27b3Smrg } 242e88f27b3Smrg 243e88f27b3Smrg pipe = &kgsl_pipe->base; 244e88f27b3Smrg pipe->funcs = &funcs; 245e88f27b3Smrg 246e88f27b3Smrg kgsl_pipe->fd = fd; 247e88f27b3Smrg kgsl_pipe->drawctxt_id = req.drawctxt_id; 248e88f27b3Smrg 249e88f27b3Smrg list_inithead(&kgsl_pipe->submit_list); 250e88f27b3Smrg list_inithead(&kgsl_pipe->pending_list); 251e88f27b3Smrg 252e88f27b3Smrg GETPROP(fd, VERSION, kgsl_pipe->version); 253e88f27b3Smrg GETPROP(fd, DEVICE_INFO, kgsl_pipe->devinfo); 254e88f27b3Smrg 255037b3c26Smrg if (kgsl_pipe->devinfo.gpu_id >= 500) { 256037b3c26Smrg ERROR_MSG("64b unsupported with kgsl"); 257037b3c26Smrg goto fail; 258037b3c26Smrg } 259037b3c26Smrg 260e88f27b3Smrg INFO_MSG("Pipe Info:"); 261e88f27b3Smrg INFO_MSG(" Device: %s", paths[id]); 262e88f27b3Smrg INFO_MSG(" Chip-id: %d.%d.%d.%d", 263e88f27b3Smrg (kgsl_pipe->devinfo.chip_id >> 24) & 0xff, 264e88f27b3Smrg (kgsl_pipe->devinfo.chip_id >> 16) & 0xff, 265e88f27b3Smrg (kgsl_pipe->devinfo.chip_id >> 8) & 0xff, 266e88f27b3Smrg (kgsl_pipe->devinfo.chip_id >> 0) & 0xff); 267e88f27b3Smrg INFO_MSG(" Device-id: %d", kgsl_pipe->devinfo.device_id); 268e88f27b3Smrg INFO_MSG(" GPU-id: %d", kgsl_pipe->devinfo.gpu_id); 269e88f27b3Smrg INFO_MSG(" MMU enabled: %d", kgsl_pipe->devinfo.mmu_enabled); 270e88f27b3Smrg INFO_MSG(" GMEM Base addr: 0x%08x", kgsl_pipe->devinfo.gmem_gpubaseaddr); 271e88f27b3Smrg INFO_MSG(" GMEM size: 0x%08x", kgsl_pipe->devinfo.gmem_sizebytes); 272e88f27b3Smrg INFO_MSG(" Driver version: %d.%d", 273e88f27b3Smrg kgsl_pipe->version.drv_major, kgsl_pipe->version.drv_minor); 274e88f27b3Smrg INFO_MSG(" Device version: %d.%d", 275e88f27b3Smrg kgsl_pipe->version.dev_major, kgsl_pipe->version.dev_minor); 276e88f27b3Smrg 277e88f27b3Smrg return pipe; 278e88f27b3Smrgfail: 279e88f27b3Smrg if (pipe) 280e88f27b3Smrg fd_pipe_del(pipe); 281e88f27b3Smrg return NULL; 282e88f27b3Smrg} 283