• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* -*- mode: C; c-file-style: "k&r"; tab-width 4; indent-tabs-mode: t; -*- */
2 
3 /*
4  * Copyright (C) 2013 Rob Clark <robclark@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@freedesktop.org>
27  */
28 
29 #include "msm_priv.h"
30 
bo_allocate(struct msm_bo * msm_bo)31 static int bo_allocate(struct msm_bo *msm_bo)
32 {
33 	struct fd_bo *bo = &msm_bo->base;
34 	if (!msm_bo->offset) {
35 		struct drm_msm_gem_info req = {
36 				.handle = bo->handle,
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 = drmCommandWriteRead(bo->dev->fd, DRM_MSM_GEM_INFO,
45 				&req, sizeof(req));
46 		if (ret) {
47 			ERROR_MSG("alloc failed: %s", strerror(errno));
48 			return ret;
49 		}
50 
51 		msm_bo->offset = req.offset;
52 	}
53 
54 	return 0;
55 }
56 
msm_bo_offset(struct fd_bo * bo,uint64_t * offset)57 static int msm_bo_offset(struct fd_bo *bo, uint64_t *offset)
58 {
59 	struct msm_bo *msm_bo = to_msm_bo(bo);
60 	int ret = bo_allocate(msm_bo);
61 	if (ret)
62 		return ret;
63 	*offset = msm_bo->offset;
64 	return 0;
65 }
66 
msm_bo_cpu_prep(struct fd_bo * bo,struct fd_pipe * pipe,uint32_t op)67 static int msm_bo_cpu_prep(struct fd_bo *bo, struct fd_pipe *pipe, uint32_t op)
68 {
69 	struct drm_msm_gem_cpu_prep req = {
70 			.handle = bo->handle,
71 			.op = op,
72 	};
73 
74 	get_abs_timeout(&req.timeout, 5000000000);
75 
76 	return drmCommandWrite(bo->dev->fd, DRM_MSM_GEM_CPU_PREP, &req, sizeof(req));
77 }
78 
msm_bo_cpu_fini(struct fd_bo * bo)79 static void msm_bo_cpu_fini(struct fd_bo *bo)
80 {
81 	struct drm_msm_gem_cpu_fini req = {
82 			.handle = bo->handle,
83 	};
84 
85 	drmCommandWrite(bo->dev->fd, DRM_MSM_GEM_CPU_FINI, &req, sizeof(req));
86 }
87 
msm_bo_madvise(struct fd_bo * bo,int willneed)88 static int msm_bo_madvise(struct fd_bo *bo, int willneed)
89 {
90 	struct drm_msm_gem_madvise req = {
91 			.handle = bo->handle,
92 			.madv = willneed ? MSM_MADV_WILLNEED : MSM_MADV_DONTNEED,
93 	};
94 	int ret;
95 
96 	/* older kernels do not support this: */
97 	if (bo->dev->version < FD_VERSION_MADVISE)
98 		return willneed;
99 
100 	ret = drmCommandWriteRead(bo->dev->fd, DRM_MSM_GEM_MADVISE, &req, sizeof(req));
101 	if (ret)
102 		return ret;
103 
104 	return req.retained;
105 }
106 
msm_bo_iova(struct fd_bo * bo)107 static uint64_t msm_bo_iova(struct fd_bo *bo)
108 {
109 	struct drm_msm_gem_info req = {
110 			.handle = bo->handle,
111 			.flags = MSM_INFO_IOVA,
112 	};
113 
114 	drmCommandWriteRead(bo->dev->fd, DRM_MSM_GEM_INFO, &req, sizeof(req));
115 
116 	return req.offset;
117 }
118 
msm_bo_destroy(struct fd_bo * bo)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: */
msm_bo_new_handle(struct fd_device * dev,uint32_t size,uint32_t flags,uint32_t * handle)136 drm_private 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 */
msm_bo_from_handle(struct fd_device * dev,uint32_t size,uint32_t handle)156 drm_private 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 }
171