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