• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2012-2018 Rob Clark <robclark@freedesktop.org>
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice (including the next
12  * paragraph) shall be included in all copies or substantial portions of the
13  * Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21  * SOFTWARE.
22  *
23  * Authors:
24  *    Rob Clark <robclark@freedesktop.org>
25  */
26 
27 #include "msm_priv.h"
28 
29 static int
bo_allocate(struct msm_bo * msm_bo)30 bo_allocate(struct msm_bo *msm_bo)
31 {
32    struct fd_bo *bo = &msm_bo->base;
33    if (!msm_bo->offset) {
34       struct drm_msm_gem_info req = {
35          .handle = bo->handle,
36          .info = MSM_INFO_GET_OFFSET,
37       };
38       int ret;
39 
40       /* if the buffer is already backed by pages then this
41        * doesn't actually do anything (other than giving us
42        * the offset)
43        */
44       ret =
45          drmCommandWriteRead(bo->dev->fd, DRM_MSM_GEM_INFO, &req, sizeof(req));
46       if (ret) {
47          ERROR_MSG("alloc failed: %s", strerror(errno));
48          return ret;
49       }
50 
51       msm_bo->offset = req.value;
52    }
53 
54    return 0;
55 }
56 
57 static int
msm_bo_offset(struct fd_bo * bo,uint64_t * offset)58 msm_bo_offset(struct fd_bo *bo, uint64_t *offset)
59 {
60    struct msm_bo *msm_bo = to_msm_bo(bo);
61    int ret = bo_allocate(msm_bo);
62    if (ret)
63       return ret;
64    *offset = msm_bo->offset;
65    return 0;
66 }
67 
68 static int
msm_bo_cpu_prep(struct fd_bo * bo,struct fd_pipe * pipe,uint32_t op)69 msm_bo_cpu_prep(struct fd_bo *bo, struct fd_pipe *pipe, uint32_t op)
70 {
71    struct drm_msm_gem_cpu_prep req = {
72       .handle = bo->handle,
73       .op = op,
74    };
75 
76    get_abs_timeout(&req.timeout, OS_TIMEOUT_INFINITE);
77 
78    return drmCommandWrite(bo->dev->fd, DRM_MSM_GEM_CPU_PREP, &req, sizeof(req));
79 }
80 
81 static int
msm_bo_madvise(struct fd_bo * bo,int willneed)82 msm_bo_madvise(struct fd_bo *bo, int willneed)
83 {
84    struct drm_msm_gem_madvise req = {
85       .handle = bo->handle,
86       .madv = willneed ? MSM_MADV_WILLNEED : MSM_MADV_DONTNEED,
87    };
88    int ret;
89 
90    /* older kernels do not support this: */
91    if (bo->dev->version < FD_VERSION_MADVISE)
92       return willneed;
93 
94    ret =
95       drmCommandWriteRead(bo->dev->fd, DRM_MSM_GEM_MADVISE, &req, sizeof(req));
96    if (ret)
97       return ret;
98 
99    return req.retained;
100 }
101 
102 static uint64_t
msm_bo_iova(struct fd_bo * bo)103 msm_bo_iova(struct fd_bo *bo)
104 {
105    struct drm_msm_gem_info req = {
106       .handle = bo->handle,
107       .info = MSM_INFO_GET_IOVA,
108    };
109    int ret;
110 
111    ret = drmCommandWriteRead(bo->dev->fd, DRM_MSM_GEM_INFO, &req, sizeof(req));
112    if (ret)
113       return 0;
114 
115    return req.value;
116 }
117 
118 static void
msm_bo_set_name(struct fd_bo * bo,const char * fmt,va_list ap)119 msm_bo_set_name(struct fd_bo *bo, const char *fmt, va_list ap)
120 {
121    struct drm_msm_gem_info req = {
122       .handle = bo->handle,
123       .info = MSM_INFO_SET_NAME,
124    };
125    char buf[32];
126    int sz;
127 
128    if (bo->dev->version < FD_VERSION_SOFTPIN)
129       return;
130 
131    sz = vsnprintf(buf, sizeof(buf), fmt, ap);
132 
133    req.value = VOID2U64(buf);
134    req.len = MIN2(sz, sizeof(buf));
135 
136    drmCommandWrite(bo->dev->fd, DRM_MSM_GEM_INFO, &req, sizeof(req));
137 }
138 
139 static void
msm_bo_set_metadata(struct fd_bo * bo,void * metadata,uint32_t metadata_size)140 msm_bo_set_metadata(struct fd_bo *bo, void *metadata, uint32_t metadata_size)
141 {
142    struct drm_msm_gem_info req = {
143       .handle = bo->handle,
144       .info = MSM_INFO_SET_METADATA,
145       .value = (uintptr_t)(void *)metadata,
146       .len = metadata_size,
147    };
148 
149    int ret = drmCommandWrite(bo->dev->fd, DRM_MSM_GEM_INFO, &req, sizeof(req));
150    if (ret) {
151       mesa_logw_once("Failed to set BO metadata with DRM_MSM_GEM_INFO: %d",
152                      ret);
153    }
154 }
155 
156 static int
msm_bo_get_metadata(struct fd_bo * bo,void * metadata,uint32_t metadata_size)157 msm_bo_get_metadata(struct fd_bo *bo, void *metadata, uint32_t metadata_size)
158 {
159    struct drm_msm_gem_info req = {
160       .handle = bo->handle,
161       .info = MSM_INFO_GET_METADATA,
162       .value = (uintptr_t)(void *)metadata,
163       .len = metadata_size,
164    };
165 
166    int ret = drmCommandWrite(bo->dev->fd, DRM_MSM_GEM_INFO, &req, sizeof(req));
167    if (ret) {
168       mesa_logw_once("Failed to get BO metadata with DRM_MSM_GEM_INFO: %d",
169                      ret);
170    }
171 
172    return ret;
173 }
174 
175 static const struct fd_bo_funcs funcs = {
176    .offset = msm_bo_offset,
177    .map = fd_bo_map_os_mmap,
178    .cpu_prep = msm_bo_cpu_prep,
179    .madvise = msm_bo_madvise,
180    .iova = msm_bo_iova,
181    .set_name = msm_bo_set_name,
182    .set_metadata = msm_bo_set_metadata,
183    .get_metadata = msm_bo_get_metadata,
184    .dmabuf = fd_bo_dmabuf_drm,
185    .destroy = fd_bo_fini_common,
186 };
187 
188 /* allocate a buffer handle: */
189 static int
new_handle(struct fd_device * dev,uint32_t size,uint32_t flags,uint32_t * handle)190 new_handle(struct fd_device *dev, uint32_t size, uint32_t flags, uint32_t *handle)
191 {
192    struct drm_msm_gem_new req = {
193       .size = size,
194    };
195    int ret;
196 
197    if (flags & FD_BO_SCANOUT)
198       req.flags |= MSM_BO_SCANOUT;
199 
200    if (flags & FD_BO_GPUREADONLY)
201       req.flags |= MSM_BO_GPU_READONLY;
202 
203    if (flags & FD_BO_CACHED_COHERENT)
204       req.flags |= MSM_BO_CACHED_COHERENT;
205    else
206       req.flags |= MSM_BO_WC;
207 
208    ret = drmCommandWriteRead(dev->fd, DRM_MSM_GEM_NEW, &req, sizeof(req));
209    if (ret)
210       return ret;
211 
212    *handle = req.handle;
213 
214    return 0;
215 }
216 
217 /* allocate a new buffer object */
218 struct fd_bo *
msm_bo_new(struct fd_device * dev,uint32_t size,uint32_t flags)219 msm_bo_new(struct fd_device *dev, uint32_t size, uint32_t flags)
220 {
221    uint32_t handle;
222    int ret;
223 
224    ret = new_handle(dev, size, flags, &handle);
225    if (ret)
226       return NULL;
227 
228    return msm_bo_from_handle(dev, size, handle);
229 }
230 
231 /* allocate a new buffer object from existing handle (import) */
232 struct fd_bo *
msm_bo_from_handle(struct fd_device * dev,uint32_t size,uint32_t handle)233 msm_bo_from_handle(struct fd_device *dev, uint32_t size, uint32_t handle)
234 {
235    struct msm_bo *msm_bo;
236    struct fd_bo *bo;
237 
238    msm_bo = calloc(1, sizeof(*msm_bo));
239    if (!msm_bo)
240       return NULL;
241 
242    bo = &msm_bo->base;
243    bo->size = size;
244    bo->handle = handle;
245    bo->funcs = &funcs;
246 
247    fd_bo_init_common(bo, dev);
248 
249    return bo;
250 }
251