Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 1 | /* |
Mikael Olsson | d4ad9e5 | 2024-02-07 11:22:26 +0100 | [diff] [blame^] | 2 | * SPDX-FileCopyrightText: Copyright 2020-2024 Arm Limited and/or its affiliates <open-source-office@arm.com> |
Ledion Daja | edd2550 | 2023-10-17 09:15:32 +0200 | [diff] [blame] | 3 | * SPDX-License-Identifier: GPL-2.0-only |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 4 | * |
| 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 Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 18 | */ |
| 19 | |
| 20 | /**************************************************************************** |
| 21 | * Includes |
| 22 | ****************************************************************************/ |
| 23 | |
Mikael Olsson | d4ad9e5 | 2024-02-07 11:22:26 +0100 | [diff] [blame^] | 24 | #include <common/ethosu_buffer.h> |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 25 | |
Mikael Olsson | d4ad9e5 | 2024-02-07 11:22:26 +0100 | [diff] [blame^] | 26 | #include <common/ethosu_device.h> |
| 27 | #include <common/ethosu_dma_mem.h> |
| 28 | #include <uapi/ethosu.h> |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 29 | |
| 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 Jonsson | d779a08 | 2023-01-04 17:09:47 +0100 | [diff] [blame] | 35 | #include <linux/remoteproc.h> |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 36 | #include <linux/uaccess.h> |
| 37 | |
| 38 | /**************************************************************************** |
| 39 | * Variables |
| 40 | ****************************************************************************/ |
| 41 | |
| 42 | static int ethosu_buffer_release(struct inode *inode, |
| 43 | struct file *file); |
| 44 | |
| 45 | static int ethosu_buffer_mmap(struct file *file, |
| 46 | struct vm_area_struct *vma); |
| 47 | |
Mikael Olsson | 0754515 | 2023-10-17 13:05:38 +0200 | [diff] [blame] | 48 | static loff_t ethosu_buffer_llseek(struct file *file, |
| 49 | loff_t offset, |
| 50 | int whence); |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 51 | |
| 52 | static const struct file_operations ethosu_buffer_fops = { |
Mikael Olsson | 0754515 | 2023-10-17 13:05:38 +0200 | [diff] [blame] | 53 | .release = ðosu_buffer_release, |
| 54 | .mmap = ðosu_buffer_mmap, |
| 55 | .llseek = ðosu_buffer_llseek, |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 56 | }; |
| 57 | |
| 58 | /**************************************************************************** |
| 59 | * Functions |
| 60 | ****************************************************************************/ |
| 61 | |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 62 | static bool ethosu_buffer_verify(struct file *file) |
| 63 | { |
| 64 | return file->f_op == ðosu_buffer_fops; |
| 65 | } |
| 66 | |
| 67 | static void ethosu_buffer_destroy(struct kref *kref) |
| 68 | { |
| 69 | struct ethosu_buffer *buf = |
| 70 | container_of(kref, struct ethosu_buffer, kref); |
Kristofer Jonsson | ec47704 | 2023-01-20 13:38:13 +0100 | [diff] [blame] | 71 | struct device *dev = buf->dev; |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 72 | |
Ledion Daja | edd2550 | 2023-10-17 09:15:32 +0200 | [diff] [blame] | 73 | dev_dbg(dev, "Buffer destroy. buf=0x%pK", buf); |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 74 | |
Mikael Olsson | 9c999fd | 2023-10-30 11:05:39 +0100 | [diff] [blame] | 75 | ethosu_dma_mem_free(&buf->dma_mem); |
Kristofer Jonsson | 53fd03d | 2022-06-21 16:58:45 +0200 | [diff] [blame] | 76 | |
Mikael Olsson | 1182f38 | 2023-08-10 13:25:44 +0200 | [diff] [blame] | 77 | memset(buf, 0, sizeof(*buf)); |
Kristofer Jonsson | ec47704 | 2023-01-20 13:38:13 +0100 | [diff] [blame] | 78 | devm_kfree(dev, buf); |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | static int ethosu_buffer_release(struct inode *inode, |
| 82 | struct file *file) |
| 83 | { |
| 84 | struct ethosu_buffer *buf = file->private_data; |
Kristofer Jonsson | ec47704 | 2023-01-20 13:38:13 +0100 | [diff] [blame] | 85 | struct device *dev = buf->dev; |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 86 | |
Ledion Daja | edd2550 | 2023-10-17 09:15:32 +0200 | [diff] [blame] | 87 | dev_dbg(dev, "Buffer release. file=0x%pK, buf=0x%pK\n", |
| 88 | file, buf); |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 89 | |
| 90 | ethosu_buffer_put(buf); |
| 91 | |
| 92 | return 0; |
| 93 | } |
| 94 | |
| 95 | static int ethosu_buffer_mmap(struct file *file, |
| 96 | struct vm_area_struct *vma) |
| 97 | { |
| 98 | struct ethosu_buffer *buf = file->private_data; |
Kristofer Jonsson | ec47704 | 2023-01-20 13:38:13 +0100 | [diff] [blame] | 99 | struct device *dev = buf->dev; |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 100 | int ret; |
| 101 | |
Ledion Daja | edd2550 | 2023-10-17 09:15:32 +0200 | [diff] [blame] | 102 | dev_dbg(dev, "Buffer mmap. file=0x%pK, buf=0x%pK\n", |
| 103 | file, buf); |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 104 | |
Mikael Olsson | 9c999fd | 2023-10-30 11:05:39 +0100 | [diff] [blame] | 105 | ret = dma_mmap_coherent(dev, vma, buf->dma_mem->cpu_addr, |
| 106 | buf->dma_mem->dma_addr, buf->dma_mem->size); |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 107 | |
| 108 | return ret; |
| 109 | } |
| 110 | |
Mikael Olsson | 0754515 | 2023-10-17 13:05:38 +0200 | [diff] [blame] | 111 | static loff_t ethosu_buffer_llseek(struct file *file, |
| 112 | loff_t offset, |
| 113 | int whence) |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 114 | { |
| 115 | struct ethosu_buffer *buf = file->private_data; |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 116 | |
Mikael Olsson | 0754515 | 2023-10-17 13:05:38 +0200 | [diff] [blame] | 117 | if (offset != 0) |
| 118 | return -EINVAL; |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 119 | |
Mikael Olsson | 0754515 | 2023-10-17 13:05:38 +0200 | [diff] [blame] | 120 | /* |
| 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 Olsson | 9c999fd | 2023-10-30 11:05:39 +0100 | [diff] [blame] | 128 | return buf->dma_mem->size; |
Mikael Olsson | 0754515 | 2023-10-17 13:05:38 +0200 | [diff] [blame] | 129 | case SEEK_SET: |
| 130 | return 0; |
| 131 | default: |
| 132 | return -EINVAL; |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 133 | } |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 134 | } |
| 135 | |
Kristofer Jonsson | ec47704 | 2023-01-20 13:38:13 +0100 | [diff] [blame] | 136 | int ethosu_buffer_create(struct device *dev, |
Mikael Olsson | 0754515 | 2023-10-17 13:05:38 +0200 | [diff] [blame] | 137 | size_t size) |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 138 | { |
| 139 | struct ethosu_buffer *buf; |
| 140 | int ret = -ENOMEM; |
| 141 | |
Mikael Olsson | 0754515 | 2023-10-17 13:05:38 +0200 | [diff] [blame] | 142 | if (!size) |
Per Åstrand | f50f25e | 2022-05-18 09:12:15 +0200 | [diff] [blame] | 143 | return -EINVAL; |
| 144 | |
Kristofer Jonsson | ec47704 | 2023-01-20 13:38:13 +0100 | [diff] [blame] | 145 | buf = devm_kzalloc(dev, sizeof(*buf), GFP_KERNEL); |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 146 | if (!buf) |
| 147 | return -ENOMEM; |
| 148 | |
Kristofer Jonsson | d779a08 | 2023-01-04 17:09:47 +0100 | [diff] [blame] | 149 | buf->dev = dev; |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 150 | kref_init(&buf->kref); |
| 151 | |
Mikael Olsson | 9c999fd | 2023-10-30 11:05:39 +0100 | [diff] [blame] | 152 | buf->dma_mem = ethosu_dma_mem_alloc(dev, size); |
| 153 | if (IS_ERR(buf->dma_mem)) { |
| 154 | ret = PTR_ERR(buf->dma_mem); |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 155 | goto free_buf; |
Mikael Olsson | 9c999fd | 2023-10-30 11:05:39 +0100 | [diff] [blame] | 156 | } |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 157 | |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 158 | ret = anon_inode_getfd("ethosu-buffer", ðosu_buffer_fops, buf, |
| 159 | O_RDWR | O_CLOEXEC); |
| 160 | if (ret < 0) |
| 161 | goto free_dma; |
| 162 | |
| 163 | buf->file = fget(ret); |
Mikael Olsson | 0754515 | 2023-10-17 13:05:38 +0200 | [diff] [blame] | 164 | buf->file->f_mode |= FMODE_LSEEK; |
| 165 | |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 166 | fput(buf->file); |
| 167 | |
Ledion Daja | edd2550 | 2023-10-17 09:15:32 +0200 | [diff] [blame] | 168 | dev_dbg(dev, |
Mikael Olsson | 0754515 | 2023-10-17 13:05:38 +0200 | [diff] [blame] | 169 | "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 Olsson | 9c999fd | 2023-10-30 11:05:39 +0100 | [diff] [blame] | 170 | 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 Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 172 | |
| 173 | return ret; |
| 174 | |
| 175 | free_dma: |
Mikael Olsson | 9c999fd | 2023-10-30 11:05:39 +0100 | [diff] [blame] | 176 | ethosu_dma_mem_free(&buf->dma_mem); |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 177 | |
| 178 | free_buf: |
Mikael Olsson | 1182f38 | 2023-08-10 13:25:44 +0200 | [diff] [blame] | 179 | memset(buf, 0, sizeof(*buf)); |
Kristofer Jonsson | ec47704 | 2023-01-20 13:38:13 +0100 | [diff] [blame] | 180 | devm_kfree(dev, buf); |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 181 | |
| 182 | return ret; |
| 183 | } |
| 184 | |
| 185 | struct 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 | |
| 207 | void ethosu_buffer_get(struct ethosu_buffer *buf) |
| 208 | { |
| 209 | kref_get(&buf->kref); |
| 210 | } |
| 211 | |
| 212 | void ethosu_buffer_put(struct ethosu_buffer *buf) |
| 213 | { |
| 214 | kref_put(&buf->kref, ethosu_buffer_destroy); |
| 215 | } |