aboutsummaryrefslogtreecommitdiff
path: root/kernel/ethosu_version.c
blob: 1a92dc25efe9a04b160020a77548dc60b100ff5d (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
/*
 * Copyright 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
 */

/****************************************************************************
 * Includes
 ****************************************************************************/

#include "ethosu_version.h"

#include "ethosu_core_rpmsg.h"

#include <linux/errno.h>

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

#define VERSION_RESP_TIMEOUT_MS 2000

/****************************************************************************
 * Functions
 ****************************************************************************/

static void ethosu_version_fail(struct ethosu_mailbox_msg *msg)
{
	struct ethosu_version *version =
		container_of(msg, typeof(*version), msg);

	if (completion_done(&version->done))
		return;

	version->errno = -EFAULT;
	complete(&version->done);
}

void ethosu_version_rsp(struct ethosu_mailbox *mailbox,
			int msg_id,
			struct ethosu_core_msg_version_rsp *rsp)
{
	struct device *dev = mailbox->dev;
	struct ethosu_mailbox_msg *msg;
	struct ethosu_version *version;

	msg = ethosu_mailbox_find(mailbox, msg_id,
				  ETHOSU_CORE_MSG_VERSION_REQ);
	if (IS_ERR(msg)) {
		dev_warn(dev,
			 "Id for version msg not found. Id=0x%0x: %ld\n",
			 msg_id, PTR_ERR(msg));

		return;
	}

	version = container_of(msg, typeof(*version), msg);

	if (completion_done(&version->done))
		return;

	if (rsp->major != ETHOSU_CORE_MSG_VERSION_MAJOR ||
	    rsp->minor != ETHOSU_CORE_MSG_VERSION_MINOR) {
		dev_warn(dev,
			 "Msg: Protocol version mismatch. Expected %u.%u.X but got %u.%u.%u",
			 ETHOSU_CORE_MSG_VERSION_MAJOR,
			 ETHOSU_CORE_MSG_VERSION_MINOR,
			 rsp->major, rsp->minor, rsp->patch);
		version->errno = -EPROTO;
	} else {
		version->errno = 0;
	}

	complete(&version->done);
}

int ethosu_version_check_request(struct device *dev,
				 struct ethosu_mailbox *mailbox)
{
	struct ethosu_version *version;
	int ret;
	int timeout;

	version = devm_kzalloc(dev, sizeof(*version), GFP_KERNEL);
	if (!version)
		return -ENOMEM;

	version->dev = dev;
	init_completion(&version->done);
	version->msg.fail = ethosu_version_fail;

	ret = ethosu_mailbox_register(mailbox, &version->msg);
	if (ret < 0)
		goto free_version;

	dev_dbg(dev, "Protocol version request created. Id=0x%x, handle=%pK\n",
		version->msg.id, version);

	ret = ethosu_mailbox_version_request(mailbox, &version->msg);
	if (ret)
		goto deregister;

	/* Unlock the mutex to not block other messages while waiting */
	device_unlock(dev);

	/* Wait for version response */
	timeout = wait_for_completion_timeout(&version->done,
					      msecs_to_jiffies(
						      VERSION_RESP_TIMEOUT_MS));

	/* Take back the mutex before resuming to do anything */
	device_lock(dev);

	if (0 == timeout) {
		dev_warn(dev, "Protocol version response timeout");
		ret = -ETIME;
		goto deregister;
	}

	if (version->errno) {
		ret = version->errno;
		goto deregister;
	}

deregister:
	ethosu_mailbox_deregister(mailbox, &version->msg);

free_version:
	dev_dbg(dev, "Protocol version destroy. Id=0x%x, handle=%pK\n",
		version->msg.id,
		version);
	devm_kfree(dev, version);

	return ret;
}