Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 1 | /* |
| 2 | * (C) COPYRIGHT 2020 ARM Limited. All rights reserved. |
| 3 | * |
| 4 | * This program is free software and is provided to you under the terms of the |
| 5 | * GNU General Public License version 2 as published by the Free Software |
| 6 | * Foundation, and any use by you of this program is subject to the terms |
| 7 | * of such GNU licence. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, you can access it online at |
| 16 | * http://www.gnu.org/licenses/gpl-2.0.html. |
| 17 | * |
| 18 | * SPDX-License-Identifier: GPL-2.0-only |
| 19 | */ |
| 20 | |
| 21 | /**************************************************************************** |
| 22 | * Includes |
| 23 | ****************************************************************************/ |
| 24 | |
| 25 | #include "ethosu_network.h" |
| 26 | |
| 27 | #include "ethosu_buffer.h" |
| 28 | #include "ethosu_device.h" |
| 29 | #include "ethosu_inference.h" |
| 30 | #include "uapi/ethosu.h" |
| 31 | |
| 32 | #include <linux/anon_inodes.h> |
| 33 | #include <linux/file.h> |
| 34 | #include <linux/fs.h> |
| 35 | #include <linux/uaccess.h> |
| 36 | |
| 37 | /**************************************************************************** |
| 38 | * Variables |
| 39 | ****************************************************************************/ |
| 40 | |
| 41 | static int ethosu_network_release(struct inode *inode, |
| 42 | struct file *file); |
| 43 | |
| 44 | static long ethosu_network_ioctl(struct file *file, |
| 45 | unsigned int cmd, |
| 46 | unsigned long arg); |
| 47 | |
| 48 | static const struct file_operations ethosu_network_fops = { |
| 49 | .release = ðosu_network_release, |
| 50 | .unlocked_ioctl = ðosu_network_ioctl, |
| 51 | #ifdef CONFIG_COMPAT |
| 52 | .compat_ioctl = ðosu_network_ioctl, |
| 53 | #endif |
| 54 | }; |
| 55 | |
| 56 | /**************************************************************************** |
| 57 | * Functions |
| 58 | ****************************************************************************/ |
| 59 | |
| 60 | static bool ethosu_network_verify(struct file *file) |
| 61 | { |
| 62 | return file->f_op == ðosu_network_fops; |
| 63 | } |
| 64 | |
| 65 | static void ethosu_network_destroy(struct kref *kref) |
| 66 | { |
| 67 | struct ethosu_network *net = |
| 68 | container_of(kref, struct ethosu_network, kref); |
| 69 | |
| 70 | dev_info(net->edev->dev, "Network destroy. handle=0x%pK\n", net); |
| 71 | |
| 72 | ethosu_buffer_put(net->buf); |
| 73 | devm_kfree(net->edev->dev, net); |
| 74 | } |
| 75 | |
| 76 | static int ethosu_network_release(struct inode *inode, |
| 77 | struct file *file) |
| 78 | { |
| 79 | struct ethosu_network *net = file->private_data; |
| 80 | |
| 81 | dev_info(net->edev->dev, "Network release. handle=0x%pK\n", net); |
| 82 | |
| 83 | ethosu_network_put(net); |
| 84 | |
| 85 | return 0; |
| 86 | } |
| 87 | |
| 88 | static long ethosu_network_ioctl(struct file *file, |
| 89 | unsigned int cmd, |
| 90 | unsigned long arg) |
| 91 | { |
| 92 | struct ethosu_network *net = file->private_data; |
| 93 | void __user *udata = (void __user *)arg; |
| 94 | int ret = -EINVAL; |
| 95 | |
| 96 | ret = mutex_lock_interruptible(&net->edev->mutex); |
| 97 | if (ret) |
| 98 | return ret; |
| 99 | |
| 100 | dev_info(net->edev->dev, "Ioctl: cmd=%u, arg=%lu\n", cmd, arg); |
| 101 | |
| 102 | switch (cmd) { |
| 103 | case ETHOSU_IOCTL_INFERENCE_CREATE: { |
| 104 | struct ethosu_uapi_inference_create uapi; |
| 105 | |
| 106 | if (copy_from_user(&uapi, udata, sizeof(uapi))) |
| 107 | break; |
| 108 | |
| 109 | dev_info(net->edev->dev, |
| 110 | "Ioctl: Inference. ifm_fd=%u, ofm_fd=%u\n", |
| 111 | uapi.ifm_fd, uapi.ofm_fd); |
| 112 | |
| 113 | ret = ethosu_inference_create(net->edev, net, &uapi); |
| 114 | break; |
| 115 | } |
| 116 | default: { |
| 117 | dev_err(net->edev->dev, "Invalid ioctl. cmd=%u, arg=%lu", |
| 118 | cmd, arg); |
| 119 | break; |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | mutex_unlock(&net->edev->mutex); |
| 124 | |
| 125 | return ret; |
| 126 | } |
| 127 | |
| 128 | int ethosu_network_create(struct ethosu_device *edev, |
| 129 | struct ethosu_uapi_network_create *uapi) |
| 130 | { |
| 131 | struct ethosu_buffer *buf; |
| 132 | struct ethosu_network *net; |
| 133 | int ret = -ENOMEM; |
| 134 | |
| 135 | buf = ethosu_buffer_get_from_fd(uapi->fd); |
| 136 | if (IS_ERR(buf)) |
| 137 | return PTR_ERR(buf); |
| 138 | |
| 139 | net = devm_kzalloc(edev->dev, sizeof(*net), GFP_KERNEL); |
| 140 | if (!net) { |
| 141 | ret = -ENOMEM; |
| 142 | goto put_buf; |
| 143 | } |
| 144 | |
| 145 | net->edev = edev; |
| 146 | net->buf = buf; |
| 147 | kref_init(&net->kref); |
| 148 | |
| 149 | ret = anon_inode_getfd("ethosu-network", ðosu_network_fops, net, |
| 150 | O_RDWR | O_CLOEXEC); |
| 151 | if (ret < 0) |
| 152 | goto free_net; |
| 153 | |
| 154 | net->file = fget(ret); |
| 155 | fput(net->file); |
| 156 | |
| 157 | dev_info(edev->dev, "Network create. handle=0x%pK", |
| 158 | net); |
| 159 | |
| 160 | return ret; |
| 161 | |
| 162 | free_net: |
| 163 | devm_kfree(edev->dev, net); |
| 164 | |
| 165 | put_buf: |
| 166 | ethosu_buffer_put(buf); |
| 167 | |
| 168 | return ret; |
| 169 | } |
| 170 | |
| 171 | struct ethosu_network *ethosu_network_get_from_fd(int fd) |
| 172 | { |
| 173 | struct ethosu_network *net; |
| 174 | struct file *file; |
| 175 | |
| 176 | file = fget(fd); |
| 177 | if (!file) |
| 178 | return ERR_PTR(-EINVAL); |
| 179 | |
| 180 | if (!ethosu_network_verify(file)) { |
| 181 | fput(file); |
| 182 | |
| 183 | return ERR_PTR(-EINVAL); |
| 184 | } |
| 185 | |
| 186 | net = file->private_data; |
| 187 | ethosu_network_get(net); |
| 188 | fput(file); |
| 189 | |
| 190 | return net; |
| 191 | } |
| 192 | |
| 193 | void ethosu_network_get(struct ethosu_network *net) |
| 194 | { |
| 195 | kref_get(&net->kref); |
| 196 | } |
| 197 | |
| 198 | void ethosu_network_put(struct ethosu_network *net) |
| 199 | { |
| 200 | kref_put(&net->kref, ethosu_network_destroy); |
| 201 | } |