msm_bo.c revision 00a23bda
1eea2e341Smrg/* -*- mode: C; c-file-style: "k&r"; tab-width 4; indent-tabs-mode: t; -*- */
2eea2e341Smrg
3eea2e341Smrg/*
4eea2e341Smrg * Copyright (C) 2013 Rob Clark <robclark@freedesktop.org>
5eea2e341Smrg *
6eea2e341Smrg * Permission is hereby granted, free of charge, to any person obtaining a
7eea2e341Smrg * copy of this software and associated documentation files (the "Software"),
8eea2e341Smrg * to deal in the Software without restriction, including without limitation
9eea2e341Smrg * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10eea2e341Smrg * and/or sell copies of the Software, and to permit persons to whom the
11eea2e341Smrg * Software is furnished to do so, subject to the following conditions:
12eea2e341Smrg *
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#ifdef HAVE_CONFIG_H
30# include <config.h>
31#endif
32
33#include "msm_priv.h"
34
35static 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
61static 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
71static 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
83static 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
92static 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
111static uint64_t msm_bo_iova(struct fd_bo *bo)
112{
113	struct drm_msm_gem_info req = {
114			.handle = bo->handle,
115			.flags = MSM_INFO_IOVA,
116	};
117
118	drmCommandWriteRead(bo->dev->fd, DRM_MSM_GEM_INFO, &req, sizeof(req));
119
120	return req.offset;
121}
122
123static void msm_bo_destroy(struct fd_bo *bo)
124{
125	struct msm_bo *msm_bo = to_msm_bo(bo);
126	free(msm_bo);
127
128}
129
130static const struct fd_bo_funcs funcs = {
131		.offset = msm_bo_offset,
132		.cpu_prep = msm_bo_cpu_prep,
133		.cpu_fini = msm_bo_cpu_fini,
134		.madvise = msm_bo_madvise,
135		.iova = msm_bo_iova,
136		.destroy = msm_bo_destroy,
137};
138
139/* allocate a buffer handle: */
140drm_private int msm_bo_new_handle(struct fd_device *dev,
141		uint32_t size, uint32_t flags, uint32_t *handle)
142{
143	struct drm_msm_gem_new req = {
144			.size = size,
145			.flags = MSM_BO_WC,  // TODO figure out proper flags..
146	};
147	int ret;
148
149	ret = drmCommandWriteRead(dev->fd, DRM_MSM_GEM_NEW,
150			&req, sizeof(req));
151	if (ret)
152		return ret;
153
154	*handle = req.handle;
155
156	return 0;
157}
158
159/* allocate a new buffer object */
160drm_private struct fd_bo * msm_bo_from_handle(struct fd_device *dev,
161		uint32_t size, uint32_t handle)
162{
163	struct msm_bo *msm_bo;
164	struct fd_bo *bo;
165
166	msm_bo = calloc(1, sizeof(*msm_bo));
167	if (!msm_bo)
168		return NULL;
169
170	bo = &msm_bo->base;
171	bo->funcs = &funcs;
172
173	return bo;
174}
175