blob: cf41b8d4b33578b5a2d9cc4ae322abba17367e5c [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 Olsson07545152023-10-17 13:05:38 +0200142 if (!size)
Per Åstrandf50f25e2022-05-18 09:12:15 +0200143 return -EINVAL;
144
Kristofer Jonssonec477042023-01-20 13:38:13 +0100145 buf = devm_kzalloc(dev, sizeof(*buf), GFP_KERNEL);
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200146 if (!buf)
147 return -ENOMEM;
148
Kristofer Jonssond779a082023-01-04 17:09:47 +0100149 buf->dev = dev;
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200150 kref_init(&buf->kref);
151
Mikael Olsson9c999fd2023-10-30 11:05:39 +0100152 buf->dma_mem = ethosu_dma_mem_alloc(dev, size);
153 if (IS_ERR(buf->dma_mem)) {
154 ret = PTR_ERR(buf->dma_mem);
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200155 goto free_buf;
Mikael Olsson9c999fd2023-10-30 11:05:39 +0100156 }
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200157
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200158 ret = anon_inode_getfd("ethosu-buffer", &ethosu_buffer_fops, buf,
159 O_RDWR | O_CLOEXEC);
160 if (ret < 0)
161 goto free_dma;
162
163 buf->file = fget(ret);
Mikael Olsson07545152023-10-17 13:05:38 +0200164 buf->file->f_mode |= FMODE_LSEEK;
165
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200166 fput(buf->file);
167
Ledion Dajaedd25502023-10-17 09:15:32 +0200168 dev_dbg(dev,
Mikael Olsson07545152023-10-17 13:05:38 +0200169 "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 +0100170 buf->file, ret, buf, size, buf->dma_mem->cpu_addr,
171 buf->dma_mem->dma_addr, virt_to_phys(buf->dma_mem->cpu_addr));
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200172
173 return ret;
174
175free_dma:
Mikael Olsson9c999fd2023-10-30 11:05:39 +0100176 ethosu_dma_mem_free(&buf->dma_mem);
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200177
178free_buf:
Mikael Olsson1182f382023-08-10 13:25:44 +0200179 memset(buf, 0, sizeof(*buf));
Kristofer Jonssonec477042023-01-20 13:38:13 +0100180 devm_kfree(dev, buf);
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200181
182 return ret;
183}
184
185struct ethosu_buffer *ethosu_buffer_get_from_fd(int fd)
186{
187 struct ethosu_buffer *buf;
188 struct file *file;
189
190 file = fget(fd);
191 if (!file)
192 return ERR_PTR(-EINVAL);
193
194 if (!ethosu_buffer_verify(file)) {
195 fput(file);
196
197 return ERR_PTR(-EINVAL);
198 }
199
200 buf = file->private_data;
201 ethosu_buffer_get(buf);
202 fput(file);
203
204 return buf;
205}
206
207void ethosu_buffer_get(struct ethosu_buffer *buf)
208{
209 kref_get(&buf->kref);
210}
211
212void ethosu_buffer_put(struct ethosu_buffer *buf)
213{
214 kref_put(&buf->kref, ethosu_buffer_destroy);
215}