1 /* -*- mode: C; c-file-style: "k&r"; tab-width 4; indent-tabs-mode: t; -*- */ 2 3 /* 4 * Copyright (C) 2013 Rob Clark <robclark (at) 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 (at) freedesktop.org> 27 */ 28 29 #ifdef HAVE_CONFIG_H 30 # include <config.h> 31 #endif 32 33 #include "msm_priv.h" 34 35 static int bo_allocate(struct msm_bo *msm_bo) 36 { 37 struct fd_bo *bo = &msm_bo->base; 38 if (!msm_bo->offset) { 39 struct drm_msm_gem_info req = { 40 .handle = bo->handle, 41 }; 42 int ret; 43 44 /* if the buffer is already backed by pages then this 45 * doesn't actually do anything (other than giving us 46 * the offset) 47 */ 48 ret = drmCommandWriteRead(bo->dev->fd, DRM_MSM_GEM_INFO, 49 &req, sizeof(req)); 50 if (ret) { 51 ERROR_MSG("alloc failed: %s", strerror(errno)); 52 return ret; 53 } 54 55 msm_bo->offset = req.offset; 56 } 57 58 return 0; 59 } 60 61 static int msm_bo_offset(struct fd_bo *bo, uint64_t *offset) 62 { 63 struct msm_bo *msm_bo = to_msm_bo(bo); 64 int ret = bo_allocate(msm_bo); 65 if (ret) 66 return ret; 67 *offset = msm_bo->offset; 68 return 0; 69 } 70 71 static int msm_bo_cpu_prep(struct fd_bo *bo, struct fd_pipe *pipe, uint32_t op) 72 { 73 struct drm_msm_gem_cpu_prep req = { 74 .handle = bo->handle, 75 .op = op, 76 }; 77 78 get_abs_timeout(&req.timeout, 5000000000); 79 80 return drmCommandWrite(bo->dev->fd, DRM_MSM_GEM_CPU_PREP, &req, sizeof(req)); 81 } 82 83 static void msm_bo_cpu_fini(struct fd_bo *bo) 84 { 85 struct drm_msm_gem_cpu_fini req = { 86 .handle = bo->handle, 87 }; 88 89 drmCommandWrite(bo->dev->fd, DRM_MSM_GEM_CPU_FINI, &req, sizeof(req)); 90 } 91 92 static int msm_bo_madvise(struct fd_bo *bo, int willneed) 93 { 94 struct drm_msm_gem_madvise req = { 95 .handle = bo->handle, 96 .madv = willneed ? MSM_MADV_WILLNEED : MSM_MADV_DONTNEED, 97 }; 98 int ret; 99 100 /* older kernels do not support this: */ 101 if (bo->dev->version < FD_VERSION_MADVISE) 102 return willneed; 103 104 ret = drmCommandWriteRead(bo->dev->fd, DRM_MSM_GEM_MADVISE, &req, sizeof(req)); 105 if (ret) 106 return ret; 107 108 return req.retained; 109 } 110 111 static void msm_bo_destroy(struct fd_bo *bo) 112 { 113 struct msm_bo *msm_bo = to_msm_bo(bo); 114 free(msm_bo); 115 116 } 117 118 static const struct fd_bo_funcs funcs = { 119 .offset = msm_bo_offset, 120 .cpu_prep = msm_bo_cpu_prep, 121 .cpu_fini = msm_bo_cpu_fini, 122 .madvise = msm_bo_madvise, 123 .destroy = msm_bo_destroy, 124 }; 125 126 /* allocate a buffer handle: */ 127 drm_private int msm_bo_new_handle(struct fd_device *dev, 128 uint32_t size, uint32_t flags, uint32_t *handle) 129 { 130 struct drm_msm_gem_new req = { 131 .size = size, 132 .flags = MSM_BO_WC, // TODO figure out proper flags.. 133 }; 134 int ret; 135 136 ret = drmCommandWriteRead(dev->fd, DRM_MSM_GEM_NEW, 137 &req, sizeof(req)); 138 if (ret) 139 return ret; 140 141 *handle = req.handle; 142 143 return 0; 144 } 145 146 /* allocate a new buffer object */ 147 drm_private struct fd_bo * msm_bo_from_handle(struct fd_device *dev, 148 uint32_t size, uint32_t handle) 149 { 150 struct msm_bo *msm_bo; 151 struct fd_bo *bo; 152 153 msm_bo = calloc(1, sizeof(*msm_bo)); 154 if (!msm_bo) 155 return NULL; 156 157 bo = &msm_bo->base; 158 bo->funcs = &funcs; 159 160 return bo; 161 } 162