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 <assert.h> 30e88f27b3Smrg 317cdc0497Smrg#include "xf86atomic.h" 32e88f27b3Smrg#include "freedreno_ringbuffer.h" 33e88f27b3Smrg#include "kgsl_priv.h" 34e88f27b3Smrg 35e88f27b3Smrg 36e88f27b3Smrg/* because kgsl tries to validate the gpuaddr on kernel side in ISSUEIBCMDS, 37e88f27b3Smrg * we can't use normal gem bo's for ringbuffer.. someday the kernel part 38e88f27b3Smrg * needs to be reworked into a single sane drm driver :-/ 39e88f27b3Smrg */ 40e88f27b3Smrgstruct kgsl_rb_bo { 41e88f27b3Smrg struct kgsl_pipe *pipe; 42e88f27b3Smrg void *hostptr; 43e88f27b3Smrg uint32_t gpuaddr; 44e88f27b3Smrg uint32_t size; 45e88f27b3Smrg}; 46e88f27b3Smrg 47e88f27b3Smrgstruct kgsl_ringbuffer { 48e88f27b3Smrg struct fd_ringbuffer base; 49e88f27b3Smrg struct kgsl_rb_bo *bo; 50e88f27b3Smrg}; 51e88f27b3Smrg 52e88f27b3Smrgstatic inline struct kgsl_ringbuffer * to_kgsl_ringbuffer(struct fd_ringbuffer *x) 53e88f27b3Smrg{ 54e88f27b3Smrg return (struct kgsl_ringbuffer *)x; 55e88f27b3Smrg} 56e88f27b3Smrg 57e88f27b3Smrgstatic void kgsl_rb_bo_del(struct kgsl_rb_bo *bo) 58e88f27b3Smrg{ 59e88f27b3Smrg struct kgsl_sharedmem_free req = { 60e88f27b3Smrg .gpuaddr = bo->gpuaddr, 61e88f27b3Smrg }; 62e88f27b3Smrg int ret; 63e88f27b3Smrg 64baaff307Smrg drm_munmap(bo->hostptr, bo->size); 65e88f27b3Smrg 66e88f27b3Smrg ret = ioctl(bo->pipe->fd, IOCTL_KGSL_SHAREDMEM_FREE, &req); 67e88f27b3Smrg if (ret) { 68e88f27b3Smrg ERROR_MSG("sharedmem free failed: %s", strerror(errno)); 69e88f27b3Smrg } 70e88f27b3Smrg 71e88f27b3Smrg free(bo); 72e88f27b3Smrg} 73e88f27b3Smrg 74e88f27b3Smrgstatic struct kgsl_rb_bo * kgsl_rb_bo_new(struct kgsl_pipe *pipe, uint32_t size) 75e88f27b3Smrg{ 76e88f27b3Smrg struct kgsl_rb_bo *bo; 77e88f27b3Smrg struct kgsl_gpumem_alloc req = { 78e88f27b3Smrg .size = ALIGN(size, 4096), 79e88f27b3Smrg .flags = KGSL_MEMFLAGS_GPUREADONLY, 80e88f27b3Smrg }; 81e88f27b3Smrg int ret; 82e88f27b3Smrg 83e88f27b3Smrg bo = calloc(1, sizeof(*bo)); 84e88f27b3Smrg if (!bo) { 85e88f27b3Smrg ERROR_MSG("allocation failed"); 86e88f27b3Smrg return NULL; 87e88f27b3Smrg } 88e88f27b3Smrg ret = ioctl(pipe->fd, IOCTL_KGSL_GPUMEM_ALLOC, &req); 89e88f27b3Smrg if (ret) { 90e88f27b3Smrg ERROR_MSG("gpumem allocation failed: %s", strerror(errno)); 91e88f27b3Smrg goto fail; 92e88f27b3Smrg } 93e88f27b3Smrg 94e88f27b3Smrg bo->pipe = pipe; 95e88f27b3Smrg bo->gpuaddr = req.gpuaddr; 96e88f27b3Smrg bo->size = size; 97baaff307Smrg bo->hostptr = drm_mmap(NULL, size, PROT_WRITE|PROT_READ, 98e88f27b3Smrg MAP_SHARED, pipe->fd, req.gpuaddr); 99e88f27b3Smrg 100e88f27b3Smrg return bo; 101e88f27b3Smrgfail: 102e88f27b3Smrg if (bo) 103e88f27b3Smrg kgsl_rb_bo_del(bo); 104e88f27b3Smrg return NULL; 105e88f27b3Smrg} 106e88f27b3Smrg 107e88f27b3Smrgstatic void * kgsl_ringbuffer_hostptr(struct fd_ringbuffer *ring) 108e88f27b3Smrg{ 109e88f27b3Smrg struct kgsl_ringbuffer *kgsl_ring = to_kgsl_ringbuffer(ring); 110e88f27b3Smrg return kgsl_ring->bo->hostptr; 111e88f27b3Smrg} 112e88f27b3Smrg 113037b3c26Smrgstatic int kgsl_ringbuffer_flush(struct fd_ringbuffer *ring, uint32_t *last_start, 114037b3c26Smrg int in_fence_fd, int *out_fence_fd) 115e88f27b3Smrg{ 116e88f27b3Smrg struct kgsl_ringbuffer *kgsl_ring = to_kgsl_ringbuffer(ring); 117e88f27b3Smrg struct kgsl_pipe *kgsl_pipe = to_kgsl_pipe(ring->pipe); 118e88f27b3Smrg uint32_t offset = (uint8_t *)last_start - (uint8_t *)ring->start; 119e88f27b3Smrg struct kgsl_ibdesc ibdesc = { 120e88f27b3Smrg .gpuaddr = kgsl_ring->bo->gpuaddr + offset, 121e88f27b3Smrg .hostptr = last_start, 122e88f27b3Smrg .sizedwords = ring->cur - last_start, 123e88f27b3Smrg }; 124e88f27b3Smrg struct kgsl_ringbuffer_issueibcmds req = { 125e88f27b3Smrg .drawctxt_id = kgsl_pipe->drawctxt_id, 126e88f27b3Smrg .ibdesc_addr = (unsigned long)&ibdesc, 127e88f27b3Smrg .numibs = 1, 128e88f27b3Smrg .flags = KGSL_CONTEXT_SUBMIT_IB_LIST, 129e88f27b3Smrg }; 130e88f27b3Smrg int ret; 131e88f27b3Smrg 132037b3c26Smrg assert(in_fence_fd == -1); 133037b3c26Smrg assert(out_fence_fd == NULL); 134037b3c26Smrg 135e88f27b3Smrg kgsl_pipe_pre_submit(kgsl_pipe); 136e88f27b3Smrg 137e88f27b3Smrg /* z180_cmdstream_issueibcmds() is made of fail: */ 138e88f27b3Smrg if (ring->pipe->id == FD_PIPE_2D) { 139e88f27b3Smrg /* fix up size field in last cmd packet */ 140e88f27b3Smrg uint32_t last_size = (uint32_t)(ring->cur - last_start); 141e88f27b3Smrg /* 5 is length of first packet, 2 for the two 7f000000's */ 142e88f27b3Smrg last_start[2] = last_size - (5 + 2); 143e88f27b3Smrg ibdesc.gpuaddr = kgsl_ring->bo->gpuaddr; 144e88f27b3Smrg ibdesc.hostptr = kgsl_ring->bo->hostptr; 145e88f27b3Smrg ibdesc.sizedwords = 0x145; 14600a23bdaSmrg req.timestamp = (uintptr_t)kgsl_ring->bo->hostptr; 147e88f27b3Smrg } 148e88f27b3Smrg 149e88f27b3Smrg do { 150e88f27b3Smrg ret = ioctl(kgsl_pipe->fd, IOCTL_KGSL_RINGBUFFER_ISSUEIBCMDS, &req); 151e88f27b3Smrg } while ((ret == -1) && ((errno == EINTR) || (errno == EAGAIN))); 152e88f27b3Smrg if (ret) 153e88f27b3Smrg ERROR_MSG("issueibcmds failed! %d (%s)", ret, strerror(errno)); 154e88f27b3Smrg 155e88f27b3Smrg ring->last_timestamp = req.timestamp; 156e88f27b3Smrg ring->last_start = ring->cur; 157e88f27b3Smrg 158e88f27b3Smrg kgsl_pipe_post_submit(kgsl_pipe, req.timestamp); 159e88f27b3Smrg 160e88f27b3Smrg return ret; 161e88f27b3Smrg} 162e88f27b3Smrg 163e88f27b3Smrgstatic void kgsl_ringbuffer_emit_reloc(struct fd_ringbuffer *ring, 164e88f27b3Smrg const struct fd_reloc *r) 165e88f27b3Smrg{ 166e88f27b3Smrg struct kgsl_bo *kgsl_bo = to_kgsl_bo(r->bo); 167e88f27b3Smrg uint32_t addr = kgsl_bo_gpuaddr(kgsl_bo, r->offset); 168e88f27b3Smrg assert(addr); 169e88f27b3Smrg if (r->shift < 0) 170e88f27b3Smrg addr >>= -r->shift; 171e88f27b3Smrg else 172e88f27b3Smrg addr <<= r->shift; 173e88f27b3Smrg (*ring->cur++) = addr | r->or; 174e88f27b3Smrg kgsl_pipe_add_submit(to_kgsl_pipe(ring->pipe), kgsl_bo); 175e88f27b3Smrg} 176e88f27b3Smrg 1773f012e29Smrgstatic uint32_t kgsl_ringbuffer_emit_reloc_ring(struct fd_ringbuffer *ring, 1787cdc0497Smrg struct fd_ringbuffer *target, uint32_t cmd_idx) 179e88f27b3Smrg{ 1803f012e29Smrg struct kgsl_ringbuffer *target_ring = to_kgsl_ringbuffer(target); 1813f012e29Smrg assert(cmd_idx == 0); 1827cdc0497Smrg (*ring->cur++) = target_ring->bo->gpuaddr; 1837cdc0497Smrg return offset_bytes(target->cur, target->start); 184e88f27b3Smrg} 185e88f27b3Smrg 186e88f27b3Smrgstatic void kgsl_ringbuffer_destroy(struct fd_ringbuffer *ring) 187e88f27b3Smrg{ 188e88f27b3Smrg struct kgsl_ringbuffer *kgsl_ring = to_kgsl_ringbuffer(ring); 189e88f27b3Smrg if (ring->last_timestamp) 190e88f27b3Smrg fd_pipe_wait(ring->pipe, ring->last_timestamp); 191e88f27b3Smrg if (kgsl_ring->bo) 192e88f27b3Smrg kgsl_rb_bo_del(kgsl_ring->bo); 193e88f27b3Smrg free(kgsl_ring); 194e88f27b3Smrg} 195e88f27b3Smrg 1963f012e29Smrgstatic const struct fd_ringbuffer_funcs funcs = { 197e88f27b3Smrg .hostptr = kgsl_ringbuffer_hostptr, 198e88f27b3Smrg .flush = kgsl_ringbuffer_flush, 199e88f27b3Smrg .emit_reloc = kgsl_ringbuffer_emit_reloc, 200e88f27b3Smrg .emit_reloc_ring = kgsl_ringbuffer_emit_reloc_ring, 201e88f27b3Smrg .destroy = kgsl_ringbuffer_destroy, 202e88f27b3Smrg}; 203e88f27b3Smrg 204e6188e58Smrgdrm_private struct fd_ringbuffer * kgsl_ringbuffer_new(struct fd_pipe *pipe, 2057cdc0497Smrg uint32_t size, enum fd_ringbuffer_flags flags) 206e88f27b3Smrg{ 207e88f27b3Smrg struct kgsl_ringbuffer *kgsl_ring; 208e88f27b3Smrg struct fd_ringbuffer *ring = NULL; 209e88f27b3Smrg 2107cdc0497Smrg assert(!flags); 2117cdc0497Smrg 212e88f27b3Smrg kgsl_ring = calloc(1, sizeof(*kgsl_ring)); 213e88f27b3Smrg if (!kgsl_ring) { 214e88f27b3Smrg ERROR_MSG("allocation failed"); 215e88f27b3Smrg goto fail; 216e88f27b3Smrg } 217e88f27b3Smrg 218e88f27b3Smrg ring = &kgsl_ring->base; 2197cdc0497Smrg atomic_set(&ring->refcnt, 1); 2207cdc0497Smrg 221e88f27b3Smrg ring->funcs = &funcs; 2223f012e29Smrg ring->size = size; 223e88f27b3Smrg 224e88f27b3Smrg kgsl_ring->bo = kgsl_rb_bo_new(to_kgsl_pipe(pipe), size); 225e88f27b3Smrg if (!kgsl_ring->bo) { 226e88f27b3Smrg ERROR_MSG("ringbuffer allocation failed"); 227e88f27b3Smrg goto fail; 228e88f27b3Smrg } 229e88f27b3Smrg 230e88f27b3Smrg return ring; 231e88f27b3Smrgfail: 232e88f27b3Smrg if (ring) 233e88f27b3Smrg fd_ringbuffer_del(ring); 234e88f27b3Smrg return NULL; 235e88f27b3Smrg} 236