blob: e82b4c04aece6843aac805ec143aa93c4be03a73 [file] [log] [blame]
Kristofer Jonsson116a6352020-08-20 17:25:23 +02001/*
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_device.h"
26
27#include "ethosu_buffer.h"
28#include "ethosu_core_interface.h"
29#include "ethosu_inference.h"
30#include "ethosu_network.h"
31#include "uapi/ethosu.h"
32
33#include <linux/delay.h>
34#include <linux/dma-mapping.h>
35#include <linux/errno.h>
36#include <linux/fs.h>
37#include <linux/interrupt.h>
38#include <linux/io.h>
39#include <linux/of_reserved_mem.h>
40#include <linux/uaccess.h>
41
42/****************************************************************************
43 * Defines
44 ****************************************************************************/
45
46#define MINOR_VERSION 0 /* Minor version starts at 0 */
47#define MINOR_COUNT 1 /* Allocate 1 minor version */
48#define DMA_ADDR_BITS 32 /* Number of address bits */
49
50/****************************************************************************
51 * Types
52 ****************************************************************************/
53
54/****************************************************************************
55 * Functions
56 ****************************************************************************/
57
Jonny Svärd7c24c772021-01-14 19:53:17 +010058/* Incoming messages */
Kristofer Jonsson116a6352020-08-20 17:25:23 +020059static int ethosu_handle_msg(struct ethosu_device *edev)
60{
Jonny Svärd7c24c772021-01-14 19:53:17 +010061 int ret;
Kristofer Jonsson116a6352020-08-20 17:25:23 +020062 struct ethosu_core_msg header;
63
64 union {
Jonny Svärd7c24c772021-01-14 19:53:17 +010065 struct ethosu_core_msg_err error;
Kristofer Jonsson116a6352020-08-20 17:25:23 +020066 struct ethosu_core_inference_rsp inf;
Jonny Svärd7c24c772021-01-14 19:53:17 +010067 struct ethosu_core_msg_version version;
Kristofer Jonsson116a6352020-08-20 17:25:23 +020068 } data;
Kristofer Jonsson116a6352020-08-20 17:25:23 +020069
70 /* Read message */
71 ret = ethosu_mailbox_read(&edev->mailbox, &header, &data, sizeof(data));
72 if (ret)
73 return ret;
74
75 switch (header.type) {
Jonny Svärd7c24c772021-01-14 19:53:17 +010076 case ETHOSU_CORE_MSG_ERR:
77 if (header.length != sizeof(data.error)) {
78 dev_warn(edev->dev,
79 "Msg: Error message of incorrect size. size=%u, expected=%zu\n", header.length,
80 sizeof(data.error));
81 ret = -EBADMSG;
82 break;
83 }
84
85 data.error.msg[sizeof(data.error.msg) - 1] = '\0';
86 dev_warn(edev->dev, "Msg: Error. type=%u, msg=\"%s\"\n",
87 data.error.type, data.error.msg);
88 ret = -EBADMSG;
89 break;
Kristofer Jonsson116a6352020-08-20 17:25:23 +020090 case ETHOSU_CORE_MSG_PING:
91 dev_info(edev->dev, "Msg: Ping\n");
Jonny Svärd7c24c772021-01-14 19:53:17 +010092 ret = ethosu_mailbox_pong(&edev->mailbox);
Kristofer Jonsson116a6352020-08-20 17:25:23 +020093 break;
94 case ETHOSU_CORE_MSG_PONG:
95 dev_info(edev->dev, "Msg: Pong\n");
96 break;
97 case ETHOSU_CORE_MSG_INFERENCE_RSP:
Jonny Svärd7c24c772021-01-14 19:53:17 +010098 if (header.length != sizeof(data.inf)) {
99 dev_warn(edev->dev,
100 "Msg: Inference response of incorrect size. size=%u, expected=%zu\n", header.length,
101 sizeof(data.inf));
102 ret = -EBADMSG;
103 break;
104 }
105
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200106 dev_info(edev->dev,
Kristofer Jonssonb74492c2020-09-10 13:26:01 +0200107 "Msg: Inference response. user_arg=0x%llx, ofm_count=%u, status=%u\n",
108 data.inf.user_arg, data.inf.ofm_count,
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200109 data.inf.status);
110 ethosu_inference_rsp(edev, &data.inf);
111 break;
Jonny Svärd7c24c772021-01-14 19:53:17 +0100112 case ETHOSU_CORE_MSG_VERSION_RSP:
113 if (header.length != sizeof(data.version)) {
114 dev_warn(edev->dev,
115 "Msg: Version response of incorrect size. size=%u, expected=%zu\n", header.length,
116 sizeof(data.version));
117 ret = -EBADMSG;
118 break;
119 }
120
121 dev_info(edev->dev, "Msg: Version response v%u.%u.%u\n",
122 data.version.major, data.version.minor,
123 data.version.patch);
124
125 /* Check major and minor version match, else return error */
126 if (data.version.major != ETHOSU_CORE_MSG_VERSION_MAJOR ||
127 data.version.minor != ETHOSU_CORE_MSG_VERSION_MINOR) {
128 dev_warn(edev->dev, "Msg: Version mismatch detected! ");
129 dev_warn(edev->dev, "Local version: v%u.%u.%u\n",
130 ETHOSU_CORE_MSG_VERSION_MAJOR,
131 ETHOSU_CORE_MSG_VERSION_MINOR,
132 ETHOSU_CORE_MSG_VERSION_PATCH);
133 }
134
135 break;
136
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200137 default:
Jonny Svärd7c24c772021-01-14 19:53:17 +0100138 /* This should not happen due to version checks */
139 dev_warn(edev->dev, "Msg: Protocol error\n");
140 ret = -EPROTO;
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200141 break;
142 }
143
144 return ret;
145}
146
147static int ethosu_open(struct inode *inode,
148 struct file *file)
149{
150 struct ethosu_device *edev =
151 container_of(inode->i_cdev, struct ethosu_device, cdev);
152
153 file->private_data = edev;
154
155 dev_info(edev->dev, "Opening device node.\n");
156
157 return nonseekable_open(inode, file);
158}
159
160static long ethosu_ioctl(struct file *file,
161 unsigned int cmd,
162 unsigned long arg)
163{
164 struct ethosu_device *edev = file->private_data;
165 void __user *udata = (void __user *)arg;
166 int ret = -EINVAL;
167
168 ret = mutex_lock_interruptible(&edev->mutex);
169 if (ret)
170 return ret;
171
172 dev_info(edev->dev, "Ioctl. cmd=%u, arg=%lu\n", cmd, arg);
173
174 switch (cmd) {
Jonny Svärd7c24c772021-01-14 19:53:17 +0100175 case ETHOSU_IOCTL_VERSION_REQ:
176 dev_info(edev->dev, "Ioctl: Send version request\n");
177 ret = ethosu_mailbox_version_request(&edev->mailbox);
178 break;
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200179 case ETHOSU_IOCTL_PING: {
180 dev_info(edev->dev, "Ioctl: Send ping\n");
181 ret = ethosu_mailbox_ping(&edev->mailbox);
182 break;
183 }
184 case ETHOSU_IOCTL_BUFFER_CREATE: {
185 struct ethosu_uapi_buffer_create uapi;
186
187 dev_info(edev->dev, "Ioctl: Buffer create\n");
188
189 if (copy_from_user(&uapi, udata, sizeof(uapi)))
190 break;
191
192 dev_info(edev->dev, "Ioctl: Buffer. capacity=%u\n",
193 uapi.capacity);
194
195 ret = ethosu_buffer_create(edev, uapi.capacity);
196 break;
197 }
198 case ETHOSU_IOCTL_NETWORK_CREATE: {
199 struct ethosu_uapi_network_create uapi;
200
201 if (copy_from_user(&uapi, udata, sizeof(uapi)))
202 break;
203
204 dev_info(edev->dev, "Ioctl: Network. fd=%u\n", uapi.fd);
205
206 ret = ethosu_network_create(edev, &uapi);
207 break;
208 }
209 default: {
210 dev_err(edev->dev, "Invalid ioctl. cmd=%u, arg=%lu",
211 cmd, arg);
212 break;
213 }
214 }
215
216 mutex_unlock(&edev->mutex);
217
218 return ret;
219}
220
221static void ethosu_mbox_rx(void *user_arg)
222{
223 struct ethosu_device *edev = user_arg;
224 int ret;
225
226 mutex_lock(&edev->mutex);
227
228 do {
229 ret = ethosu_handle_msg(edev);
Jonny Svärd7c24c772021-01-14 19:53:17 +0100230 if (ret && ret != -ENOMSG)
231 /* Need to start over in case of error, empty the queue
232 * by fast-forwarding read position to write position.
233 * */
234 ethosu_mailbox_reset(&edev->mailbox);
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200235 } while (ret == 0);
236
237 mutex_unlock(&edev->mutex);
238}
239
240int ethosu_dev_init(struct ethosu_device *edev,
241 struct device *dev,
242 struct class *class,
243 struct resource *in_queue,
244 struct resource *out_queue)
245{
246 static const struct file_operations fops = {
247 .owner = THIS_MODULE,
248 .open = &ethosu_open,
249 .unlocked_ioctl = &ethosu_ioctl,
250#ifdef CONFIG_COMPAT
251 .compat_ioctl = &ethosu_ioctl,
252#endif
253 };
254 struct device *sysdev;
255 int ret;
256
257 edev->dev = dev;
258 edev->class = class;
259 mutex_init(&edev->mutex);
260 INIT_LIST_HEAD(&edev->inference_list);
261
262 ret = of_reserved_mem_device_init(edev->dev);
263 if (ret)
264 return ret;
265
266 dma_set_mask_and_coherent(edev->dev, DMA_BIT_MASK(DMA_ADDR_BITS));
267
268 ret = ethosu_mailbox_init(&edev->mailbox, dev, in_queue, out_queue,
269 ethosu_mbox_rx, edev);
270 if (ret)
271 goto release_reserved_mem;
272
273 ret = alloc_chrdev_region(&edev->devt, MINOR_VERSION, MINOR_COUNT,
274 "ethosu");
275 if (ret) {
276 dev_err(edev->dev, "Failed to allocate chrdev region.\n");
277 goto deinit_mailbox;
278 }
279
280 cdev_init(&edev->cdev, &fops);
281 edev->cdev.owner = THIS_MODULE;
282
283 ret = cdev_add(&edev->cdev, edev->devt, MINOR_COUNT);
284 if (ret) {
285 dev_err(edev->dev, "Failed to add character device.\n");
286 goto region_unregister;
287 }
288
289 sysdev = device_create(edev->class, NULL, edev->devt, edev,
290 "ethosu%d", MAJOR(edev->devt));
291 if (IS_ERR(sysdev)) {
292 dev_err(edev->dev, "Failed to create device.\n");
293 ret = PTR_ERR(sysdev);
294 goto del_cdev;
295 }
296
297 dev_info(edev->dev,
298 "Created Arm Ethos-U device. name=%s, major=%d, minor=%d\n",
299 dev_name(sysdev), MAJOR(edev->devt), MINOR(edev->devt));
300
301 return 0;
302
303del_cdev:
304 cdev_del(&edev->cdev);
305
306region_unregister:
307 unregister_chrdev_region(edev->devt, 1);
308
309deinit_mailbox:
310 ethosu_mailbox_deinit(&edev->mailbox);
311
312release_reserved_mem:
313 of_reserved_mem_device_release(edev->dev);
314
315 return ret;
316}
317
318void ethosu_dev_deinit(struct ethosu_device *edev)
319{
320 ethosu_mailbox_deinit(&edev->mailbox);
321 device_destroy(edev->class, edev->cdev.dev);
322 cdev_del(&edev->cdev);
323 unregister_chrdev_region(edev->devt, MINOR_COUNT);
324 of_reserved_mem_device_release(edev->dev);
325
326 dev_info(edev->dev, "%s\n", __FUNCTION__);
327}