aboutsummaryrefslogtreecommitdiff
path: root/kernel/ethosu_driver.c
blob: 085b2c058358ce8962c2f01374467c476b151f1b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/*
 * Copyright 2020-2023 Arm Limited and/or its affiliates
 *
 * This program is free software and is provided to you under the terms of the
 * GNU General Public License version 2 as published by the Free Software
 * Foundation, and any use by you of this program is subject to the terms
 * of such GNU licence.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, you can access it online at
 * http://www.gnu.org/licenses/gpl-2.0.html.
 *
 * SPDX-License-Identifier: GPL-2.0-only
 */

#include <linux/bitmap.h>
#include <linux/fs.h>
#include <linux/module.h>
#include <linux/io.h>
#include <linux/of.h>
#include <linux/of_address.h>
#include <linux/rpmsg.h>

#include "ethosu_device.h"
#include "uapi/ethosu.h"

/****************************************************************************
 * Defines
 ****************************************************************************/

#define ETHOSU_DRIVER_STR(s) #s
#define ETHOSU_DRIVER_VERSION_STR(major, minor, patch) \
	ETHOSU_DRIVER_STR(major) "."		       \
	ETHOSU_DRIVER_STR(minor) "."		       \
	ETHOSU_DRIVER_STR(patch)
#define ETHOSU_DRIVER_VERSION ETHOSU_DRIVER_VERSION_STR( \
		ETHOSU_KERNEL_DRIVER_VERSION_MAJOR,	 \
		ETHOSU_KERNEL_DRIVER_VERSION_MINOR,	 \
		ETHOSU_KERNEL_DRIVER_VERSION_PATCH)

#define ETHOSU_DRIVER_NAME    "ethosu"

#define MINOR_BASE      0 /* Minor version starts at 0 */
#define MINOR_COUNT    64 /* Allocate minor versions */

/****************************************************************************
 * Variables
 ****************************************************************************/

static struct class *ethosu_class;

static dev_t devt;

/****************************************************************************
 * Rpmsg driver
 ****************************************************************************/

static int ethosu_rpmsg_probe(struct rpmsg_device *rpdev)
{
	struct device *dev = &rpdev->dev;
	int ret;

	dev_info(dev, "%s", __FUNCTION__);

	/* Initialize device */
	ret = ethosu_dev_init(rpdev, ethosu_class, devt);
	if (ret)
		return ret;

	return 0;
}

static void ethosu_rpmsg_remove(struct rpmsg_device *rpdev)
{
	struct device *dev = &rpdev->dev;

	dev_info(dev, "%s", __FUNCTION__);

	ethosu_dev_deinit(rpdev);
}

static int ethosu_rpmsg_cb(struct rpmsg_device *rpdev,
			   void *data,
			   int len,
			   void *priv,
			   u32 src)
{
	dev_err(&rpdev->dev, "%s", __FUNCTION__);

	return -EINVAL;
}

static struct rpmsg_device_id ethosu_rpmsg_driver_id_table[] = {
	{ .name = "ethos-u-0.0" },
	{},
};

MODULE_DEVICE_TABLE(rpmsg, ethosu_rpmsg_driver_id_table);

static struct rpmsg_driver ethosu_rpmsg_driver = {
	.drv                = {
		.name       = ETHOSU_DRIVER_NAME,
		.owner      = THIS_MODULE,
		.probe_type = PROBE_PREFER_ASYNCHRONOUS,
	},
	.id_table           = ethosu_rpmsg_driver_id_table,
	.probe              = ethosu_rpmsg_probe,
	.callback           = ethosu_rpmsg_cb,
	.remove             = ethosu_rpmsg_remove,
};

/****************************************************************************
 * Module init and exit
 ****************************************************************************/

static void __exit ethosu_exit(void)
{
	unregister_rpmsg_driver(&ethosu_rpmsg_driver);
	unregister_chrdev_region(devt, MINOR_COUNT);
	class_destroy(ethosu_class);
}

static int __init ethosu_init(void)
{
	int ret;

	ethosu_class = class_create(THIS_MODULE, ETHOSU_DRIVER_NAME);
	if (IS_ERR(ethosu_class)) {
		pr_err("Failed to create class '%s'.\n", ETHOSU_DRIVER_NAME);

		return PTR_ERR(ethosu_class);
	}

	ret = alloc_chrdev_region(&devt, MINOR_BASE, MINOR_COUNT,
				  ETHOSU_DRIVER_NAME);
	if (ret) {
		pr_err("Failed to allocate chrdev region.\n");
		goto destroy_class;
	}

	ret = register_rpmsg_driver(&ethosu_rpmsg_driver);
	if (ret) {
		pr_err("Failed to register Arm Ethos-U rpmsg driver.\n");
		goto region_unregister;
	}

	return 0;

region_unregister:
	unregister_chrdev_region(devt, MINOR_COUNT);

destroy_class:
	class_destroy(ethosu_class);

	return ret;
}

module_init(ethosu_init)
module_exit(ethosu_exit)

MODULE_LICENSE("GPL v2");
MODULE_AUTHOR("Arm Ltd");
MODULE_DESCRIPTION("Arm Ethos-U NPU Driver");
MODULE_VERSION(ETHOSU_DRIVER_VERSION);