1e88f27b3Smrg/* -*- mode: C; c-file-style: "k&r"; tab-width 4; indent-tabs-mode: t; -*- */
2e88f27b3Smrg
3e88f27b3Smrg/*
4e88f27b3Smrg * Copyright (C) 2013 Rob Clark <robclark@freedesktop.org>
5e88f27b3Smrg *
6e88f27b3Smrg * Permission is hereby granted, free of charge, to any person obtaining a
7e88f27b3Smrg * copy of this software and associated documentation files (the "Software"),
8e88f27b3Smrg * to deal in the Software without restriction, including without limitation
9e88f27b3Smrg * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10e88f27b3Smrg * and/or sell copies of the Software, and to permit persons to whom the
11e88f27b3Smrg * Software is furnished to do so, subject to the following conditions:
12e88f27b3Smrg *
13e88f27b3Smrg * The above copyright notice and this permission notice (including the next
14e88f27b3Smrg * paragraph) shall be included in all copies or substantial portions of the
15e88f27b3Smrg * Software.
16e88f27b3Smrg *
17e88f27b3Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18e88f27b3Smrg * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19e88f27b3Smrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
20e88f27b3Smrg * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21e88f27b3Smrg * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22e88f27b3Smrg * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23e88f27b3Smrg * SOFTWARE.
24e88f27b3Smrg *
25e88f27b3Smrg * Authors:
26e88f27b3Smrg *    Rob Clark <robclark@freedesktop.org>
27e88f27b3Smrg */
28e88f27b3Smrg
29e88f27b3Smrg#include "msm_priv.h"
30e88f27b3Smrg
31e88f27b3Smrgstatic int bo_allocate(struct msm_bo *msm_bo)
32e88f27b3Smrg{
33e88f27b3Smrg	struct fd_bo *bo = &msm_bo->base;
34e88f27b3Smrg	if (!msm_bo->offset) {
35e88f27b3Smrg		struct drm_msm_gem_info req = {
36e88f27b3Smrg				.handle = bo->handle,
37e88f27b3Smrg		};
38e88f27b3Smrg		int ret;
39e88f27b3Smrg
40e88f27b3Smrg		/* if the buffer is already backed by pages then this
41e88f27b3Smrg		 * doesn't actually do anything (other than giving us
42e88f27b3Smrg		 * the offset)
43e88f27b3Smrg		 */
44e88f27b3Smrg		ret = drmCommandWriteRead(bo->dev->fd, DRM_MSM_GEM_INFO,
45e88f27b3Smrg				&req, sizeof(req));
46e88f27b3Smrg		if (ret) {
47e88f27b3Smrg			ERROR_MSG("alloc failed: %s", strerror(errno));
48e88f27b3Smrg			return ret;
49e88f27b3Smrg		}
50e88f27b3Smrg
51e88f27b3Smrg		msm_bo->offset = req.offset;
52e88f27b3Smrg	}
53e88f27b3Smrg
54e88f27b3Smrg	return 0;
55e88f27b3Smrg}
56e88f27b3Smrg
57e88f27b3Smrgstatic int msm_bo_offset(struct fd_bo *bo, uint64_t *offset)
58e88f27b3Smrg{
59e88f27b3Smrg	struct msm_bo *msm_bo = to_msm_bo(bo);
60e88f27b3Smrg	int ret = bo_allocate(msm_bo);
61e88f27b3Smrg	if (ret)
62e88f27b3Smrg		return ret;
63e88f27b3Smrg	*offset = msm_bo->offset;
64e88f27b3Smrg	return 0;
65e88f27b3Smrg}
66e88f27b3Smrg
67e88f27b3Smrgstatic int msm_bo_cpu_prep(struct fd_bo *bo, struct fd_pipe *pipe, uint32_t op)
68e88f27b3Smrg{
69e88f27b3Smrg	struct drm_msm_gem_cpu_prep req = {
70e88f27b3Smrg			.handle = bo->handle,
71e88f27b3Smrg			.op = op,
72e88f27b3Smrg	};
73e88f27b3Smrg
743f012e29Smrg	get_abs_timeout(&req.timeout, 5000000000);
75e88f27b3Smrg
76e88f27b3Smrg	return drmCommandWrite(bo->dev->fd, DRM_MSM_GEM_CPU_PREP, &req, sizeof(req));
77e88f27b3Smrg}
78e88f27b3Smrg
79e88f27b3Smrgstatic void msm_bo_cpu_fini(struct fd_bo *bo)
80e88f27b3Smrg{
81e88f27b3Smrg	struct drm_msm_gem_cpu_fini req = {
82e88f27b3Smrg			.handle = bo->handle,
83e88f27b3Smrg	};
84e88f27b3Smrg
85e88f27b3Smrg	drmCommandWrite(bo->dev->fd, DRM_MSM_GEM_CPU_FINI, &req, sizeof(req));
86e88f27b3Smrg}
87e88f27b3Smrg
883f012e29Smrgstatic int msm_bo_madvise(struct fd_bo *bo, int willneed)
893f012e29Smrg{
903f012e29Smrg	struct drm_msm_gem_madvise req = {
913f012e29Smrg			.handle = bo->handle,
923f012e29Smrg			.madv = willneed ? MSM_MADV_WILLNEED : MSM_MADV_DONTNEED,
933f012e29Smrg	};
943f012e29Smrg	int ret;
953f012e29Smrg
963f012e29Smrg	/* older kernels do not support this: */
973f012e29Smrg	if (bo->dev->version < FD_VERSION_MADVISE)
983f012e29Smrg		return willneed;
993f012e29Smrg
1003f012e29Smrg	ret = drmCommandWriteRead(bo->dev->fd, DRM_MSM_GEM_MADVISE, &req, sizeof(req));
1013f012e29Smrg	if (ret)
1023f012e29Smrg		return ret;
1033f012e29Smrg
1043f012e29Smrg	return req.retained;
1053f012e29Smrg}
1063f012e29Smrg
10700a23bdaSmrgstatic uint64_t msm_bo_iova(struct fd_bo *bo)
10800a23bdaSmrg{
10900a23bdaSmrg	struct drm_msm_gem_info req = {
11000a23bdaSmrg			.handle = bo->handle,
11100a23bdaSmrg			.flags = MSM_INFO_IOVA,
11200a23bdaSmrg	};
11300a23bdaSmrg
11400a23bdaSmrg	drmCommandWriteRead(bo->dev->fd, DRM_MSM_GEM_INFO, &req, sizeof(req));
11500a23bdaSmrg
11600a23bdaSmrg	return req.offset;
11700a23bdaSmrg}
11800a23bdaSmrg
119e88f27b3Smrgstatic void msm_bo_destroy(struct fd_bo *bo)
120e88f27b3Smrg{
121e88f27b3Smrg	struct msm_bo *msm_bo = to_msm_bo(bo);
122e88f27b3Smrg	free(msm_bo);
123e88f27b3Smrg
124e88f27b3Smrg}
125e88f27b3Smrg
1263f012e29Smrgstatic const struct fd_bo_funcs funcs = {
127e88f27b3Smrg		.offset = msm_bo_offset,
128e88f27b3Smrg		.cpu_prep = msm_bo_cpu_prep,
129e88f27b3Smrg		.cpu_fini = msm_bo_cpu_fini,
1303f012e29Smrg		.madvise = msm_bo_madvise,
13100a23bdaSmrg		.iova = msm_bo_iova,
132e88f27b3Smrg		.destroy = msm_bo_destroy,
133e88f27b3Smrg};
134e88f27b3Smrg
135e88f27b3Smrg/* allocate a buffer handle: */
136e6188e58Smrgdrm_private int msm_bo_new_handle(struct fd_device *dev,
137e88f27b3Smrg		uint32_t size, uint32_t flags, uint32_t *handle)
138e88f27b3Smrg{
139e88f27b3Smrg	struct drm_msm_gem_new req = {
140e88f27b3Smrg			.size = size,
141e88f27b3Smrg			.flags = MSM_BO_WC,  // TODO figure out proper flags..
142e88f27b3Smrg	};
143e88f27b3Smrg	int ret;
144e88f27b3Smrg
145e88f27b3Smrg	ret = drmCommandWriteRead(dev->fd, DRM_MSM_GEM_NEW,
146e88f27b3Smrg			&req, sizeof(req));
147e88f27b3Smrg	if (ret)
148e88f27b3Smrg		return ret;
149e88f27b3Smrg
150e88f27b3Smrg	*handle = req.handle;
151e88f27b3Smrg
152e88f27b3Smrg	return 0;
153e88f27b3Smrg}
154e88f27b3Smrg
155e88f27b3Smrg/* allocate a new buffer object */
156e6188e58Smrgdrm_private struct fd_bo * msm_bo_from_handle(struct fd_device *dev,
157e88f27b3Smrg		uint32_t size, uint32_t handle)
158e88f27b3Smrg{
159e88f27b3Smrg	struct msm_bo *msm_bo;
160e88f27b3Smrg	struct fd_bo *bo;
161e88f27b3Smrg
162e88f27b3Smrg	msm_bo = calloc(1, sizeof(*msm_bo));
163e88f27b3Smrg	if (!msm_bo)
164e88f27b3Smrg		return NULL;
165e88f27b3Smrg
166e88f27b3Smrg	bo = &msm_bo->base;
167e88f27b3Smrg	bo->funcs = &funcs;
168e88f27b3Smrg
169e88f27b3Smrg	return bo;
170e88f27b3Smrg}
171