aboutsummaryrefslogtreecommitdiff
path: root/kernel/ethosu_buffer.c
blob: bf7d74592a7601f74bdbde1beacc21a33141fa3c (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
/*
 * SPDX-FileCopyrightText: Copyright 2020-2023 Arm Limited and/or its affiliates <open-source-office@arm.com>
 * SPDX-License-Identifier: GPL-2.0-only
 *
 * This program is free software and is provided to you under the terms of the
 * GNU General Public License version 2 as published by the Free Software
 * Foundation, and any use by you of this program is subject to the terms
 * of such GNU licence.
 *
 * 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.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, you can access it online at
 * http://www.gnu.org/licenses/gpl-2.0.html.
 */

/****************************************************************************
 * Includes
 ****************************************************************************/

#include "ethosu_buffer.h"

#include "ethosu_device.h"
#include "uapi/ethosu.h"

#include <linux/anon_inodes.h>
#include <linux/dma-mapping.h>
#include <linux/of_address.h>
#include <linux/file.h>
#include <linux/fs.h>
#include <linux/remoteproc.h>
#include <linux/uaccess.h>

/****************************************************************************
 * Variables
 ****************************************************************************/

static int ethosu_buffer_release(struct inode *inode,
				 struct file *file);

static int ethosu_buffer_mmap(struct file *file,
			      struct vm_area_struct *vma);

static loff_t ethosu_buffer_llseek(struct file *file,
				   loff_t offset,
				   int whence);

static const struct file_operations ethosu_buffer_fops = {
	.release = &ethosu_buffer_release,
	.mmap    = &ethosu_buffer_mmap,
	.llseek  = &ethosu_buffer_llseek,
};

/****************************************************************************
 * Functions
 ****************************************************************************/

static bool ethosu_buffer_verify(struct file *file)
{
	return file->f_op == &ethosu_buffer_fops;
}

static void ethosu_buffer_destroy(struct kref *kref)
{
	struct ethosu_buffer *buf =
		container_of(kref, struct ethosu_buffer, kref);
	struct device *dev = buf->dev;

	dev_dbg(dev, "Buffer destroy. buf=0x%pK", buf);

	memset(buf->cpu_addr, 0, buf->size);
	dma_free_coherent(dev, buf->size, buf->cpu_addr,
			  buf->dma_addr);

	memset(buf, 0, sizeof(*buf));
	devm_kfree(dev, buf);
}

static int ethosu_buffer_release(struct inode *inode,
				 struct file *file)
{
	struct ethosu_buffer *buf = file->private_data;
	struct device *dev = buf->dev;

	dev_dbg(dev, "Buffer release. file=0x%pK, buf=0x%pK\n",
		file, buf);

	ethosu_buffer_put(buf);

	return 0;
}

static int ethosu_buffer_mmap(struct file *file,
			      struct vm_area_struct *vma)
{
	struct ethosu_buffer *buf = file->private_data;
	struct device *dev = buf->dev;
	int ret;

	dev_dbg(dev, "Buffer mmap. file=0x%pK, buf=0x%pK\n",
		file, buf);

	ret = dma_mmap_coherent(dev, vma, buf->cpu_addr,
				buf->dma_addr, buf->size);

	return ret;
}

static loff_t ethosu_buffer_llseek(struct file *file,
				   loff_t offset,
				   int whence)
{
	struct ethosu_buffer *buf = file->private_data;

	if (offset != 0)
		return -EINVAL;

	/*
	 * SEEK_END and SEEK_SET is supported with a zero offset to allow buffer
	 * size discovery using seek functions e.g.
	 * size = lseek(buf_fd, 0, SEEK_END);
	 * lseek(buf_fd, 0, SEEK_SET);
	 */
	switch (whence) {
	case SEEK_END:
		return buf->size;
	case SEEK_SET:
		return 0;
	default:
		return -EINVAL;
	}
}

int ethosu_buffer_create(struct device *dev,
			 size_t size)
{
	struct ethosu_buffer *buf;
	int ret = -ENOMEM;

	if (!size)
		return -EINVAL;

	buf = devm_kzalloc(dev, sizeof(*buf), GFP_KERNEL);
	if (!buf)
		return -ENOMEM;

	buf->dev = dev;
	buf->size = size;
	kref_init(&buf->kref);

	buf->cpu_addr = dma_alloc_coherent(dev, size, &buf->dma_addr,
					   GFP_KERNEL);
	if (!buf->cpu_addr)
		goto free_buf;

	ret = anon_inode_getfd("ethosu-buffer", &ethosu_buffer_fops, buf,
			       O_RDWR | O_CLOEXEC);
	if (ret < 0)
		goto free_dma;

	buf->file = fget(ret);
	buf->file->f_mode |= FMODE_LSEEK;

	fput(buf->file);

	dev_dbg(dev,
		"Buffer create. file=0x%pK, fd=%d, buf=0x%pK, size=%zu, cpu_addr=0x%pK, dma_addr=0x%llx, phys_addr=0x%llx\n",
		buf->file, ret, buf, size, buf->cpu_addr, buf->dma_addr,
		virt_to_phys(buf->cpu_addr));

	return ret;

free_dma:
	dma_free_coherent(dev, buf->size, buf->cpu_addr,
			  buf->dma_addr);

free_buf:
	memset(buf, 0, sizeof(*buf));
	devm_kfree(dev, buf);

	return ret;
}

struct ethosu_buffer *ethosu_buffer_get_from_fd(int fd)
{
	struct ethosu_buffer *buf;
	struct file *file;

	file = fget(fd);
	if (!file)
		return ERR_PTR(-EINVAL);

	if (!ethosu_buffer_verify(file)) {
		fput(file);

		return ERR_PTR(-EINVAL);
	}

	buf = file->private_data;
	ethosu_buffer_get(buf);
	fput(file);

	return buf;
}

void ethosu_buffer_get(struct ethosu_buffer *buf)
{
	kref_get(&buf->kref);
}

void ethosu_buffer_put(struct ethosu_buffer *buf)
{
	kref_put(&buf->kref, ethosu_buffer_destroy);
}