summaryrefstree
path: root/kernel/drivers/misc/tzdev/ion_fd2phys.c
blob: bb52a33a90ff86268447d1f456dc5e07d642c4af (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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
/*
 * Copyright (C) 2012-2018 Samsung Electronics, Inc.
 *
 * 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.
 */

#define pr_fmt(fmt) "IONFD2PHYS: " fmt
/* #define DEBUG */

#include <linux/uaccess.h>
#include <linux/device.h>
#include <linux/fs.h>
#include <linux/init.h>
#include <linux/ion.h>
#include <linux/kernel.h>
#include <linux/mm.h>
#include <linux/module.h>
#include <linux/slab.h>

#if defined(CONFIG_ARCH_MSM)
#include <linux/msm_ion.h>
#endif

#include "tz_cdev.h"
#include "tzdev.h"

#define TZDEV_MAX_PFNS_COUNT	(SIZE_MAX / sizeof(sk_pfn_t))

struct ionfd2phys {
	int fd;
	size_t nr_pfns;
	sk_pfn_t *pfns;
};

#ifdef CONFIG_COMPAT
struct ionfd2phys32 {
	int fd;
	unsigned int nr_pfns;
	u32 pfns;
};
#endif

#if defined(CONFIG_ARCH_EXYNOS)
extern struct ion_device *ion_exynos;
#elif defined(CONFIG_ARCH_WHALE) || defined(CONFIG_ARCH_RANCHU)
extern struct ion_device *idev;
#elif defined(CONFIG_ARCH_MT6755) || defined(CONFIG_ARCH_MT6735) || defined(CONFIG_MACH_MT6757)
extern struct ion_device *g_ion_device;
#endif

static struct ion_client *client;

static long __ionfd2phys_ioctl(int fd, size_t nr_pfns, sk_pfn_t *pfns)
{
	int ret;
	struct ion_handle *handle;
	void *addr;
	int pfn;
	size_t size = 0;

	handle = ion_import_dma_buf(client, fd);
	if (IS_ERR_OR_NULL(handle)) {
		pr_err("Failed to import an ION FD\n");
		ret = handle ? PTR_ERR(handle) : -EINVAL;
		goto out;
	}

	ret = ion_phys(client, handle, (ion_phys_addr_t *)&addr, &size);
	if (ret) {
		pr_err("Failed ION FD: ion_phys()\n");
		addr = ion_map_kernel(client, handle);
		if (IS_ERR_OR_NULL(addr)) {
			pr_err("Failed to map an ION FD\n");
			ret = addr ? PTR_ERR(addr) : -EINVAL;
			goto handle_free;
		}

		/* There is no kernel public API for getting imported buffer
		*  size. So just use what user has supplied */
		for (pfn = 0; pfn < nr_pfns; ++pfn) {
			pfns[pfn] = vmalloc_to_pfn(addr);
			addr += PAGE_SIZE;
		}
		ion_unmap_kernel(client, handle);
	} else {
		for (pfn = 0; pfn < nr_pfns; ++pfn) {
			pfns[pfn] = (sk_pfn_t)((unsigned long)addr >> PAGE_SHIFT);
			addr += PAGE_SIZE;
		}
	}

	ret = 0;

handle_free:
	ion_free(client, handle);
out:
	return ret;
}

static long ionfd2phys_ioctl(struct file *file, unsigned int cmd,
		unsigned long arg)
{
	int ret;
	struct ionfd2phys data;
	sk_pfn_t *pfns;

	if (copy_from_user(&data, (void __user *)arg, sizeof(data))) {
		pr_err("Failed ION FD: copy_from_user()\n");
		ret = -EFAULT;
		goto out;
	}

	if (data.nr_pfns > TZDEV_MAX_PFNS_COUNT) {
		ret = -EINVAL;
		goto out;
	}

	pfns = kzalloc(data.nr_pfns * sizeof(sk_pfn_t), GFP_KERNEL);
	if (!pfns) {
		pr_err("Failed ION FD: kzalloc(%zu)\n", data.nr_pfns);
		ret = -ENOMEM;
		goto out;
	}

	ret = __ionfd2phys_ioctl(data.fd, data.nr_pfns, pfns);
	if (ret) {
		pr_err("Failed ION FD: __ionfd2phys_ioctl()\n");
		goto pfns_free;
	}

	if (copy_to_user((void __user *)data.pfns, pfns,
				data.nr_pfns * sizeof(sk_pfn_t))) {
		pr_err("Failed ION FD: copy_to_user()\n");
		ret = -EFAULT;
		goto pfns_free;
	}

pfns_free:
	kfree(pfns);
out:
	return ret;
}

#ifdef CONFIG_COMPAT
static long compat_ionfd2phys_ioctl(struct file *file, unsigned int cmd,
		unsigned long arg)
{
	int ret;
	struct ionfd2phys32 data;
	sk_pfn_t *pfns;

	if (copy_from_user(&data, (void __user *)arg, sizeof(data))) {
		pr_err("Failed ION FD: copy_from_user()\n");
		ret = -EFAULT;
		goto out;
	}

	if (data.nr_pfns > TZDEV_MAX_PFNS_COUNT) {
		ret = -EINVAL;
		goto out;
	}

	pfns = kzalloc(data.nr_pfns * sizeof(sk_pfn_t), GFP_KERNEL);
	if (!pfns) {
		pr_err("Failed ION FD: kzalloc(%d)\n", data.nr_pfns);
		ret = -ENOMEM;
		goto out;
	}

	ret = __ionfd2phys_ioctl(data.fd, (size_t)data.nr_pfns, pfns);
	if (ret) {
		pr_err("Failed ION FD: __ionfd2phys_ioctl()\n");
		goto pfns_free;
	}

	if (copy_to_user(compat_ptr(data.pfns), pfns,
				data.nr_pfns * sizeof(sk_pfn_t))) {
		pr_err("Failed ION FD: copy_to_user()\n");
		ret = -EFAULT;
		goto pfns_free;
	}

pfns_free:
	kfree(pfns);
out:
	return ret;
}
#endif

static const struct file_operations ionfd2phys_fops = {
	.unlocked_ioctl = ionfd2phys_ioctl,
#ifdef CONFIG_COMPAT
	.compat_ioctl = compat_ionfd2phys_ioctl,
#endif
};

static struct tz_cdev ionfd2phys_cdev = {
	.name = "ionfd2phys",
	.fops = &ionfd2phys_fops,
	.owner = THIS_MODULE,
};

static ssize_t system_heap_id_show(struct device *dev, struct device_attribute *attr,
			char *buf)
{
#if defined(CONFIG_ARCH_EXYNOS) || defined(CONFIG_ARCH_RANCHU)
	return sprintf(buf, "%d\n", ION_HEAP_TYPE_SYSTEM);
#elif defined(CONFIG_ARCH_WHALE)
	/* This is dirty hack for Whale platform. This platform uses
	 * DTS/device tree to get information about preallocated ION heaps and
	 * in DTS/device tree system heap has ID 1, but kernel headers defines
	 * system heap ID as ION_HEAP_TYPE_SYSTEM and system heap ID
	 * is zero */
#define ION_HEAP_TYPE_SYSTEM_WHALE (1)
	return sprintf(buf, "%d\n", ION_HEAP_TYPE_SYSTEM_WHALE);
#elif defined(CONFIG_ARCH_MT6755)
	/* This is dirty hack for MT6755 platform. This platform uses
	 * DTS/device tree to get information about preallocated ION heaps and
	 * in DTS/device tree system heap has ID 1, but kernel headers defines
	 * system heap ID as ION_HEAP_TYPE_SYSTEM and system heap ID
	 * is zero */
#define ION_HEAP_TYPE_SYSTEM_MT6755 (1)
	return sprintf(buf, "%d\n", ION_HEAP_TYPE_SYSTEM_MT6755);
#elif defined(CONFIG_ARCH_MT6735)
	/* This is dirty hack for MT6735 platform. This platform does
	 * not have ion_system_heap. Instead, ion_mm_heap should be used,
	 * which is listed in a separate platform-specific enum, and has type
	 * value 10 */
#define ION_HEAP_TYPE_MM_MT6735 (10)
	return sprintf(buf, "%d\n", ION_HEAP_TYPE_MM_MT6735);
#elif defined(CONFIG_MACH_MT6757)
	/* This is dirty hack for MT6757 platform. This platform does
	 * not have ion_system_heap. Instead, ion_mm_heap should be used,
	 * which is listed in a separate platform-specific enum, and has type
	 * value 10 */
#define ION_HEAP_TYPE_MM_MT6757 (10)
	return sprintf(buf, "%d\n", ION_HEAP_TYPE_MM_MT6757);

#elif defined(CONFIG_ARCH_MSM)
	return sprintf(buf, "%d\n", ION_SYSTEM_HEAP_ID);
#endif
	return -ENOSYS;
}

static DEVICE_ATTR(system_heap_id, S_IRUGO, system_heap_id_show, NULL);

static ssize_t ionfd2phys_id_show(struct device *dev, struct device_attribute *attr,
			char *buf)
{
	return sprintf(buf, "%d\n", 0);
}

static DEVICE_ATTR(ionfd2phys_id, S_IRUGO, ionfd2phys_id_show, NULL);

static int __init ionfd2phys_init(void)
{
	int ret;

#if defined(CONFIG_ARCH_EXYNOS)
	struct ion_device *ion_dev = ion_exynos;
#elif defined(CONFIG_ARCH_WHALE) || defined(CONFIG_ARCH_RANCHU)
	struct ion_device *ion_dev = idev;
#elif defined(CONFIG_ARCH_MT6755) || defined(CONFIG_ARCH_MT6735) || defined(CONFIG_MACH_MT6757)
	struct ion_device *ion_dev = g_ion_device;
#endif
	pr_devel("module init\n");

#if defined(CONFIG_ARCH_EXYNOS) || defined(CONFIG_ARCH_WHALE) || defined(CONFIG_ARCH_MT6755) \
 || defined(CONFIG_ARCH_MT6735) || defined(CONFIG_ARCH_RANCHU) || defined(CONFIG_MACH_MT6757)
	if (!ion_dev) {
		pr_err("Failed to get ion device\n");
		return 0;
	}

	client = ion_client_create(ion_dev, "IONFD2PHYS");
#elif defined(CONFIG_ARCH_MSM)
/* The msm_ion_client_create() function has two parameters for old version of
 * linux kernel. This parameter is not used by function. In more fresh version
 * of linux kernel the function contains only one parameter. New version of
 * linux kernel is used for MSM8939/MSM8996, so parameter is 1
 */
#if defined(CONFIG_ARCH_MSM8939) || defined(CONFIG_ARCH_MSM8996)
	client = msm_ion_client_create("IONFD2PHYS");
#else
	client = msm_ion_client_create(-1, "IONFD2PHYS");
#endif
#endif
	if (IS_ERR_OR_NULL(client)) {
		pr_err("Failed to create an ION client\n");
		ret = client ? PTR_ERR(client) : -ENOSYS;
		goto out;
	}

	ret = tz_cdev_register(&ionfd2phys_cdev);
	if (ret)
		goto ion_destroy;

	ret = device_create_file(ionfd2phys_cdev.device, &dev_attr_system_heap_id);
	if (ret) {
		pr_err("system_heap_id sysfs file creation failed\n");
		goto out_unregister;
	}

	ret = device_create_file(ionfd2phys_cdev.device, &dev_attr_ionfd2phys_id);
	if (ret) {
		pr_err("ionfd2phys_id sysfs file creation failed\n");
		goto out_remove_file;
	}
	return 0;

out_remove_file:
	device_remove_file(ionfd2phys_cdev.device, &dev_attr_system_heap_id);
out_unregister:
	tz_cdev_unregister(&ionfd2phys_cdev);
ion_destroy:
	ion_client_destroy(client);
out:
	return ret;
}

static void __exit ionfd2phys_exit(void)
{
	pr_devel("module exit\n");
	tz_cdev_unregister(&ionfd2phys_cdev);
	ion_client_destroy(client);
}

#if defined(CONFIG_ARCH_WHALE) || defined(CONFIG_ARCH_RANCHU)
/* On Whale and Ranchu platforms ION driver starts as module,
 * so ionfd2phys_init() is executed when there is
 * no idev in the system. To avoid this situation
 * ionfd2phys_init() is moved to late_initcall
 * section */
late_initcall(ionfd2phys_init);
#else
module_init(ionfd2phys_init);
#endif
module_exit(ionfd2phys_exit);

MODULE_LICENSE("GPL");
MODULE_AUTHOR("Sergey Fedorov <s.fedorov@samsung.com>");