aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/gpu/drm/zocl/zocl_bo.c
blob: bd4d80223fa8f244c4efb77f7c9a46a8289abcd1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
/*
 * A GEM style CMA backed memory manager for ZynQ based OpenCL accelerators.
 *
 * Copyright (C) 2016 Xilinx, Inc. All rights reserved.
 *
 * Authors:
 *    Sonal Santan <sonal.santan@xilinx.com>
 *    Umang Parekh <umang.parekh@xilinx.com>
 *
 * This software is licensed under the terms of the GNU General Public
 * License version 2, as published by the Free Software Foundation, and
 * may be copied, distributed, and modified under those terms.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */

#include <linux/dma-buf.h>
#include <linux/module.h>
#include <linux/ramfs.h>
#include <linux/shmem_fs.h>
#include "zocl_drv.h"
#include <drm/drmP.h>
#include <drm/drm_gem.h>
#include <linux/zocl_ioctl.h>

static inline void __user *to_user_ptr(u64 address)
{
	return (void __user *)(uintptr_t)address;
}

void zocl_describe(const struct drm_zocl_bo *obj)
{
	size_t size_in_kb = obj->base.base.size / 1024;
	size_t physical_addr = obj->base.paddr;

	DRM_INFO("%p: H[0x%zxKB] D[0x%zx]\n",
		  obj,
		  size_in_kb,
		  physical_addr);
}

static struct drm_zocl_bo *zocl_create_bo(struct drm_device *dev,
	uint64_t unaligned_size)
{
	size_t size = PAGE_ALIGN(unaligned_size);
	struct drm_gem_cma_object *cma_obj;

	DRM_DEBUG("%s:%s:%d: %zd\n", __FILE__, __func__, __LINE__, size);

	if (!size)
		return ERR_PTR(-EINVAL);

	cma_obj = drm_gem_cma_create(dev, size);
	if (IS_ERR(cma_obj))
		return ERR_PTR(-ENOMEM);

	return to_zocl_bo(&cma_obj->base);
}

int zocl_create_bo_ioctl(struct drm_device *dev,
		void *data,
		struct drm_file *filp)
{
	int ret;
	struct drm_zocl_create_bo *args = data;
	struct drm_zocl_bo *bo;

	if (((args->flags & DRM_ZOCL_BO_FLAGS_COHERENT) == 0) ||
	    ((args->flags & DRM_ZOCL_BO_FLAGS_CMA) == 0))
		return -EINVAL;

	bo = zocl_create_bo(dev, args->size);
	bo->flags |= DRM_ZOCL_BO_FLAGS_COHERENT;
	bo->flags |= DRM_ZOCL_BO_FLAGS_CMA;

	DRM_DEBUG("%s:%s:%d: %p\n", __FILE__, __func__, __LINE__, bo);

	if (IS_ERR(bo)) {
		DRM_DEBUG("object creation failed\n");
		return PTR_ERR(bo);
	}
	ret = drm_gem_handle_create(filp, &bo->base.base, &args->handle);
	if (ret) {
		drm_gem_cma_free_object(&bo->base.base);
		DRM_DEBUG("handle creation failed\n");
		return ret;
	}

	zocl_describe(bo);
	drm_gem_object_unreference_unlocked(&bo->base.base);

	return ret;
}

int zocl_map_bo_ioctl(struct drm_device *dev,
		void *data,
		struct drm_file *filp)
{
	struct drm_zocl_map_bo *args = data;
	struct drm_gem_object *gem_obj;

	DRM_DEBUG("%s:%s:%d: %p\n", __FILE__, __func__, __LINE__, data);
	gem_obj = drm_gem_object_lookup(dev, filp, args->handle);
	if (!gem_obj) {
		DRM_ERROR("Failed to look up GEM BO %d\n", args->handle);
		return -EINVAL;
	}

	/* The mmap offset was set up at BO allocation time. */
	args->offset = drm_vma_node_offset_addr(&gem_obj->vma_node);
	zocl_describe(to_zocl_bo(gem_obj));
	drm_gem_object_unreference_unlocked(gem_obj);

	return 0;
}

int zocl_sync_bo_ioctl(struct drm_device *dev,
		void *data,
		struct drm_file *filp)
{
	const struct drm_zocl_sync_bo *args = data;
	struct drm_gem_object *gem_obj = drm_gem_object_lookup(dev, filp,
							       args->handle);
	void *kaddr;
	int ret = 0;

	DRM_DEBUG("%s:%s:%d: %p\n", __FILE__, __func__, __LINE__, data);

	if (!gem_obj) {
		DRM_ERROR("Failed to look up GEM BO %d\n", args->handle);
		return -EINVAL;
	}

	if ((args->offset > gem_obj->size) || (args->size > gem_obj->size) ||
		((args->offset + args->size) > gem_obj->size)) {
		ret = -EINVAL;
		goto out;
	}

	kaddr = drm_gem_cma_prime_vmap(gem_obj);

	/* only invalidate the range of addresses requested by the user */
	kaddr += args->offset;

	if (args->dir == DRM_ZOCL_SYNC_BO_TO_DEVICE)
		flush_kernel_vmap_range(kaddr, args->size);
	else if (args->dir == DRM_ZOCL_SYNC_BO_FROM_DEVICE)
		invalidate_kernel_vmap_range(kaddr, args->size);
	else
		ret = -EINVAL;

out:
	drm_gem_object_unreference_unlocked(gem_obj);

	return ret;
}

int zocl_info_bo_ioctl(struct drm_device *dev,
		void *data,
		struct drm_file *filp)
{
	const struct drm_zocl_bo *bo;
	struct drm_zocl_info_bo *args = data;
	struct drm_gem_object *gem_obj = drm_gem_object_lookup(dev, filp,
							       args->handle);

	DRM_DEBUG("%s:%s:%d: %p\n", __FILE__, __func__, __LINE__, data);

	if (!gem_obj) {
		DRM_ERROR("Failed to look up GEM BO %d\n", args->handle);
		return -EINVAL;
	}

	bo = to_zocl_bo(gem_obj);

	args->size = bo->base.base.size;
	args->paddr = bo->base.paddr;
	drm_gem_object_unreference_unlocked(gem_obj);

	return 0;
}

int zocl_pwrite_bo_ioctl(struct drm_device *dev, void *data,
		struct drm_file *filp)
{
	const struct drm_zocl_pwrite_bo *args = data;
	struct drm_gem_object *gem_obj = drm_gem_object_lookup(dev, filp,
							       args->handle);
	char __user *user_data = to_user_ptr(args->data_ptr);
	int ret = 0;
	void *kaddr;

	DRM_DEBUG("%s:%s:%d: %p\n", __FILE__, __func__, __LINE__, data);

	if (!gem_obj) {
		DRM_ERROR("Failed to look up GEM BO %d\n", args->handle);
		return -EINVAL;
	}

	if ((args->offset > gem_obj->size) || (args->size > gem_obj->size)
		|| ((args->offset + args->size) > gem_obj->size)) {
		ret = -EINVAL;
		goto out;
	}

	if (args->size == 0) {
		ret = 0;
		goto out;
	}

	if (!access_ok(VERIFY_READ, user_data, args->size)) {
		ret = -EFAULT;
		goto out;
	}

	kaddr = drm_gem_cma_prime_vmap(gem_obj);
	kaddr += args->offset;

	ret = copy_from_user(kaddr, user_data, args->size);
out:
	drm_gem_object_unreference_unlocked(gem_obj);

	return ret;
}

int zocl_pread_bo_ioctl(struct drm_device *dev, void *data,
		struct drm_file *filp)
{
	const struct drm_zocl_pread_bo *args = data;
	struct drm_gem_object *gem_obj = drm_gem_object_lookup(dev, filp,
							       args->handle);
	char __user *user_data = to_user_ptr(args->data_ptr);
	int ret = 0;
	void *kaddr;

	DRM_DEBUG("%s:%s:%d: %p\n", __FILE__, __func__, __LINE__, data);

	if (!gem_obj) {
		DRM_ERROR("Failed to look up GEM BO %d\n", args->handle);
		return -EINVAL;
	}

	if ((args->offset > gem_obj->size) || (args->size > gem_obj->size)
		|| ((args->offset + args->size) > gem_obj->size)) {
		ret = -EINVAL;
		goto out;
	}

	if (args->size == 0) {
		ret = 0;
		goto out;
	}

	if (!access_ok(VERIFY_WRITE, user_data, args->size)) {
		ret = EFAULT;
		goto out;
	}

	kaddr = drm_gem_cma_prime_vmap(gem_obj);
	kaddr += args->offset;

	ret = copy_to_user(user_data, kaddr, args->size);

out:
	drm_gem_object_unreference_unlocked(gem_obj);

	return ret;
}