blob: 519e2708f16966d46cedc8f4a54e540079536845 [file] [log] [blame]
Kristofer Jonsson116a6352020-08-20 17:25:23 +02001/*
Mikael Olssond4ad9e52024-02-07 11:22:26 +01002 * SPDX-FileCopyrightText: Copyright 2020-2024 Arm Limited and/or its affiliates <open-source-office@arm.com>
Ledion Dajaedd25502023-10-17 09:15:32 +02003 * SPDX-License-Identifier: GPL-2.0-only
Kristofer Jonsson116a6352020-08-20 17:25:23 +02004 *
5 * This program is free software and is provided to you under the terms of the
6 * GNU General Public License version 2 as published by the Free Software
7 * Foundation, and any use by you of this program is subject to the terms
8 * of such GNU licence.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, you can access it online at
17 * http://www.gnu.org/licenses/gpl-2.0.html.
Kristofer Jonsson116a6352020-08-20 17:25:23 +020018 */
19
20/****************************************************************************
21 * Includes
22 ****************************************************************************/
23
Mikael Olssond4ad9e52024-02-07 11:22:26 +010024#include <common/ethosu_buffer.h>
Kristofer Jonsson116a6352020-08-20 17:25:23 +020025
Mikael Olssond4ad9e52024-02-07 11:22:26 +010026#include <common/ethosu_device.h>
27#include <common/ethosu_dma_mem.h>
28#include <uapi/ethosu.h>
Kristofer Jonsson116a6352020-08-20 17:25:23 +020029
30#include <linux/anon_inodes.h>
31#include <linux/dma-mapping.h>
32#include <linux/of_address.h>
33#include <linux/file.h>
34#include <linux/fs.h>
Kristofer Jonssond779a082023-01-04 17:09:47 +010035#include <linux/remoteproc.h>
Kristofer Jonsson116a6352020-08-20 17:25:23 +020036#include <linux/uaccess.h>
37
38/****************************************************************************
39 * Variables
40 ****************************************************************************/
41
42static int ethosu_buffer_release(struct inode *inode,
43 struct file *file);
44
45static int ethosu_buffer_mmap(struct file *file,
46 struct vm_area_struct *vma);
47
Mikael Olsson07545152023-10-17 13:05:38 +020048static loff_t ethosu_buffer_llseek(struct file *file,
49 loff_t offset,
50 int whence);
Kristofer Jonsson116a6352020-08-20 17:25:23 +020051
52static const struct file_operations ethosu_buffer_fops = {
Mikael Olsson07545152023-10-17 13:05:38 +020053 .release = &ethosu_buffer_release,
54 .mmap = &ethosu_buffer_mmap,
55 .llseek = &ethosu_buffer_llseek,
Kristofer Jonsson116a6352020-08-20 17:25:23 +020056};
57
58/****************************************************************************
59 * Functions
60 ****************************************************************************/
61
Kristofer Jonsson116a6352020-08-20 17:25:23 +020062static bool ethosu_buffer_verify(struct file *file)
63{
64 return file->f_op == &ethosu_buffer_fops;
65}
66
67static void ethosu_buffer_destroy(struct kref *kref)
68{
69 struct ethosu_buffer *buf =
70 container_of(kref, struct ethosu_buffer, kref);
Kristofer Jonssonec477042023-01-20 13:38:13 +010071 struct device *dev = buf->dev;
Kristofer Jonsson116a6352020-08-20 17:25:23 +020072
Ledion Dajaedd25502023-10-17 09:15:32 +020073 dev_dbg(dev, "Buffer destroy. buf=0x%pK", buf);
Kristofer Jonsson116a6352020-08-20 17:25:23 +020074
Mikael Olsson9c999fd2023-10-30 11:05:39 +010075 ethosu_dma_mem_free(&buf->dma_mem);
Kristofer Jonsson53fd03d2022-06-21 16:58:45 +020076
Mikael Olsson1182f382023-08-10 13:25:44 +020077 memset(buf, 0, sizeof(*buf));
Kristofer Jonssonec477042023-01-20 13:38:13 +010078 devm_kfree(dev, buf);
Kristofer Jonsson116a6352020-08-20 17:25:23 +020079}
80
81static int ethosu_buffer_release(struct inode *inode,
82 struct file *file)
83{
84 struct ethosu_buffer *buf = file->private_data;
Kristofer Jonssonec477042023-01-20 13:38:13 +010085 struct device *dev = buf->dev;
Kristofer Jonsson116a6352020-08-20 17:25:23 +020086
Ledion Dajaedd25502023-10-17 09:15:32 +020087 dev_dbg(dev, "Buffer release. file=0x%pK, buf=0x%pK\n",
88 file, buf);
Kristofer Jonsson116a6352020-08-20 17:25:23 +020089
90 ethosu_buffer_put(buf);
91
92 return 0;
93}
94
95static int ethosu_buffer_mmap(struct file *file,
96 struct vm_area_struct *vma)
97{
98 struct ethosu_buffer *buf = file->private_data;
Kristofer Jonssonec477042023-01-20 13:38:13 +010099 struct device *dev = buf->dev;
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200100 int ret;
101
Ledion Dajaedd25502023-10-17 09:15:32 +0200102 dev_dbg(dev, "Buffer mmap. file=0x%pK, buf=0x%pK\n",
103 file, buf);
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200104
Mikael Olsson9c999fd2023-10-30 11:05:39 +0100105 ret = dma_mmap_coherent(dev, vma, buf->dma_mem->cpu_addr,
106 buf->dma_mem->dma_addr, buf->dma_mem->size);
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200107
108 return ret;
109}
110
Mikael Olsson07545152023-10-17 13:05:38 +0200111static loff_t ethosu_buffer_llseek(struct file *file,
112 loff_t offset,
113 int whence)
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200114{
115 struct ethosu_buffer *buf = file->private_data;
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200116
Mikael Olsson07545152023-10-17 13:05:38 +0200117 if (offset != 0)
118 return -EINVAL;
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200119
Mikael Olsson07545152023-10-17 13:05:38 +0200120 /*
121 * SEEK_END and SEEK_SET is supported with a zero offset to allow buffer
122 * size discovery using seek functions e.g.
123 * size = lseek(buf_fd, 0, SEEK_END);
124 * lseek(buf_fd, 0, SEEK_SET);
125 */
126 switch (whence) {
127 case SEEK_END:
Mikael Olsson9c999fd2023-10-30 11:05:39 +0100128 return buf->dma_mem->size;
Mikael Olsson07545152023-10-17 13:05:38 +0200129 case SEEK_SET:
130 return 0;
131 default:
132 return -EINVAL;
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200133 }
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200134}
135
Kristofer Jonssonec477042023-01-20 13:38:13 +0100136int ethosu_buffer_create(struct device *dev,
Mikael Olsson07545152023-10-17 13:05:38 +0200137 size_t size)
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200138{
139 struct ethosu_buffer *buf;
140 int ret = -ENOMEM;
141
Mikael Olssond80345f2024-04-04 17:28:52 +0200142 if (!size) {
143 dev_err(dev, "Buffer create. Invalid zero size");
144
Per Åstrandf50f25e2022-05-18 09:12:15 +0200145 return -EINVAL;
Mikael Olssond80345f2024-04-04 17:28:52 +0200146 }
Per Åstrandf50f25e2022-05-18 09:12:15 +0200147
Kristofer Jonssonec477042023-01-20 13:38:13 +0100148 buf = devm_kzalloc(dev, sizeof(*buf), GFP_KERNEL);
Mikael Olssond80345f2024-04-04 17:28:52 +0200149 if (!buf) {
150 dev_err(dev, "Buffer create. Failed to allocate struct");
151
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200152 return -ENOMEM;
Mikael Olssond80345f2024-04-04 17:28:52 +0200153 }
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200154
Kristofer Jonssond779a082023-01-04 17:09:47 +0100155 buf->dev = dev;
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200156 kref_init(&buf->kref);
157
Mikael Olsson9c999fd2023-10-30 11:05:39 +0100158 buf->dma_mem = ethosu_dma_mem_alloc(dev, size);
159 if (IS_ERR(buf->dma_mem)) {
160 ret = PTR_ERR(buf->dma_mem);
Mikael Olssond80345f2024-04-04 17:28:52 +0200161 dev_err(dev,
162 "Buffer create. Failed to allocate DMA memory. ret=%d",
163 ret);
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200164 goto free_buf;
Mikael Olsson9c999fd2023-10-30 11:05:39 +0100165 }
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200166
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200167 ret = anon_inode_getfd("ethosu-buffer", &ethosu_buffer_fops, buf,
168 O_RDWR | O_CLOEXEC);
Mikael Olssond80345f2024-04-04 17:28:52 +0200169 if (ret < 0) {
170 dev_err(dev,
171 "Buffer create. Failed to get file descriptor. ret=%d",
172 ret);
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200173 goto free_dma;
Mikael Olssond80345f2024-04-04 17:28:52 +0200174 }
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200175
176 buf->file = fget(ret);
Mikael Olsson07545152023-10-17 13:05:38 +0200177 buf->file->f_mode |= FMODE_LSEEK;
178
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200179 fput(buf->file);
180
Ledion Dajaedd25502023-10-17 09:15:32 +0200181 dev_dbg(dev,
Mikael Olsson07545152023-10-17 13:05:38 +0200182 "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",
Mikael Olsson9c999fd2023-10-30 11:05:39 +0100183 buf->file, ret, buf, size, buf->dma_mem->cpu_addr,
184 buf->dma_mem->dma_addr, virt_to_phys(buf->dma_mem->cpu_addr));
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200185
186 return ret;
187
188free_dma:
Mikael Olsson9c999fd2023-10-30 11:05:39 +0100189 ethosu_dma_mem_free(&buf->dma_mem);
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200190
191free_buf:
Mikael Olsson1182f382023-08-10 13:25:44 +0200192 memset(buf, 0, sizeof(*buf));
Kristofer Jonssonec477042023-01-20 13:38:13 +0100193 devm_kfree(dev, buf);
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200194
195 return ret;
196}
197
198struct ethosu_buffer *ethosu_buffer_get_from_fd(int fd)
199{
200 struct ethosu_buffer *buf;
201 struct file *file;
202
203 file = fget(fd);
204 if (!file)
205 return ERR_PTR(-EINVAL);
206
207 if (!ethosu_buffer_verify(file)) {
208 fput(file);
209
210 return ERR_PTR(-EINVAL);
211 }
212
213 buf = file->private_data;
214 ethosu_buffer_get(buf);
215 fput(file);
216
217 return buf;
218}
219
220void ethosu_buffer_get(struct ethosu_buffer *buf)
221{
222 kref_get(&buf->kref);
223}
224
225void ethosu_buffer_put(struct ethosu_buffer *buf)
226{
227 kref_put(&buf->kref, ethosu_buffer_destroy);
228}