blob: b07c0ec1cf4e00f15e1b4186e98ff614bfcfb762 [file] [log] [blame]
Davide Grohmann7e8f5082022-03-23 12:48:45 +01001/*
Mikael Olsson6d5e2d22024-01-16 11:19:09 +01002 * SPDX-FileCopyrightText: Copyright 2022-2024 Arm Limited and/or its affiliates <open-source-office@arm.com>
Ledion Dajaedd25502023-10-17 09:15:32 +02003 * SPDX-License-Identifier: GPL-2.0-only
Davide Grohmann7e8f5082022-03-23 12:48:45 +01004 *
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.
18 *
Davide Grohmann7e8f5082022-03-23 12:48:45 +010019 */
20
21/****************************************************************************
22 * Includes
23 ****************************************************************************/
24
Mikael Olssond4ad9e52024-02-07 11:22:26 +010025#include <rpmsg/ethosu_rpmsg_cancel_inference.h>
Davide Grohmann7e8f5082022-03-23 12:48:45 +010026
Mikael Olssond4ad9e52024-02-07 11:22:26 +010027#include <common/ethosu_device.h>
28#include <rpmsg/ethosu_rpmsg.h>
29#include <rpmsg/ethosu_rpmsg_inference.h>
Davide Grohmann7e8f5082022-03-23 12:48:45 +010030
Kristofer Jonsson074ef902023-01-23 13:05:36 +010031#include <linux/remoteproc.h>
Davide Grohmann7e8f5082022-03-23 12:48:45 +010032#include <linux/wait.h>
33
34/****************************************************************************
35 * Defines
36 ****************************************************************************/
37
38#define CANCEL_INFERENCE_RESP_TIMEOUT_MS 2000
39
40/****************************************************************************
41 * Functions
42 ****************************************************************************/
43
Mikael Olsson16be2852024-02-12 09:56:56 +010044static int ethosu_rpmsg_cancel_inference_send(
45 struct ethosu_rpmsg_cancel_inference *cancellation,
46 struct ethosu_rpmsg_mailbox *mailbox)
Davide Grohmann7e8f5082022-03-23 12:48:45 +010047{
Mikael Olsson16be2852024-02-12 09:56:56 +010048 return ethosu_rpmsg_mailbox_cancel_inference(mailbox,
49 &cancellation->msg,
50 cancellation->inf->msg.id);
Davide Grohmann7e8f5082022-03-23 12:48:45 +010051}
52
Mikael Olsson16be2852024-02-12 09:56:56 +010053static void ethosu_rpmsg_cancel_inference_fail(
54 struct ethosu_rpmsg_mailbox_msg *msg)
Davide Grohmann7e8f5082022-03-23 12:48:45 +010055{
Mikael Olsson16be2852024-02-12 09:56:56 +010056 struct ethosu_rpmsg_cancel_inference *cancellation =
Davide Grohmann7e8f5082022-03-23 12:48:45 +010057 container_of(msg, typeof(*cancellation), msg);
58
59 if (completion_done(&cancellation->done))
60 return;
61
62 cancellation->errno = -EFAULT;
63 cancellation->uapi->status = ETHOSU_UAPI_STATUS_ERROR;
64 complete(&cancellation->done);
65}
66
Mikael Olsson16be2852024-02-12 09:56:56 +010067int ethosu_rpmsg_cancel_inference_request(struct device *dev,
68 struct ethosu_rpmsg_mailbox *mailbox,
69 struct ethosu_rpmsg_inference *inf,
70 struct ethosu_uapi_cancel_inference_status *uapi)
Davide Grohmann7e8f5082022-03-23 12:48:45 +010071{
Mikael Olsson16be2852024-02-12 09:56:56 +010072 struct ethosu_rpmsg_cancel_inference *cancellation;
Davide Grohmann7e8f5082022-03-23 12:48:45 +010073 int ret;
74 int timeout;
75
76 if (inf->done) {
77 uapi->status = ETHOSU_UAPI_STATUS_ERROR;
78
79 return 0;
80 }
81
82 cancellation =
Kristofer Jonssonec477042023-01-20 13:38:13 +010083 devm_kzalloc(dev,
Mikael Olsson16be2852024-02-12 09:56:56 +010084 sizeof(struct ethosu_rpmsg_cancel_inference),
Davide Grohmann7e8f5082022-03-23 12:48:45 +010085 GFP_KERNEL);
Mikael Olssond80345f2024-04-04 17:28:52 +020086 if (!cancellation) {
87 dev_err(dev, "Cancel inference. Failed to allocate struct");
88
Davide Grohmann7e8f5082022-03-23 12:48:45 +010089 return -ENOMEM;
Mikael Olssond80345f2024-04-04 17:28:52 +020090 }
Davide Grohmann7e8f5082022-03-23 12:48:45 +010091
92 /* increase ref count on the inference we are refering to */
Mikael Olsson16be2852024-02-12 09:56:56 +010093 ethosu_rpmsg_inference_get(inf);
Davide Grohmann7e8f5082022-03-23 12:48:45 +010094 /* mark inference ABORTING to avoid resending the inference message */
Mikael Olsson6d5e2d22024-01-16 11:19:09 +010095 inf->status = ETHOSU_UAPI_STATUS_ABORTING;
Davide Grohmann7e8f5082022-03-23 12:48:45 +010096
Kristofer Jonssonec477042023-01-20 13:38:13 +010097 cancellation->dev = dev;
Davide Grohmann7e8f5082022-03-23 12:48:45 +010098 cancellation->inf = inf;
99 cancellation->uapi = uapi;
Davide Grohmann7e8f5082022-03-23 12:48:45 +0100100 init_completion(&cancellation->done);
Mikael Olsson16be2852024-02-12 09:56:56 +0100101 cancellation->msg.fail = ethosu_rpmsg_cancel_inference_fail;
Davide Grohmann7e8f5082022-03-23 12:48:45 +0100102
Mikael Olsson16be2852024-02-12 09:56:56 +0100103 ret = ethosu_rpmsg_mailbox_register(mailbox,
104 &cancellation->msg);
Davide Grohmann32660f92022-04-27 16:49:07 +0200105 if (ret < 0)
106 goto kfree;
107
Ledion Dajaedd25502023-10-17 09:15:32 +0200108 dev_dbg(dev,
109 "Inference cancellation create. cancel=0x%pK, msg.id=%ddev",
110 cancellation, cancellation->msg.id);
Davide Grohmann7e8f5082022-03-23 12:48:45 +0100111
Mikael Olsson16be2852024-02-12 09:56:56 +0100112 ret = ethosu_rpmsg_cancel_inference_send(cancellation, mailbox);
Davide Grohmann7e8f5082022-03-23 12:48:45 +0100113 if (0 != ret)
Davide Grohmann32660f92022-04-27 16:49:07 +0200114 goto deregister;
Davide Grohmann7e8f5082022-03-23 12:48:45 +0100115
116 /* Unlock the mutex before going to block on the condition */
Kristofer Jonssonec477042023-01-20 13:38:13 +0100117 device_unlock(dev);
Kristofer Jonsson074ef902023-01-23 13:05:36 +0100118
Davide Grohmann7e8f5082022-03-23 12:48:45 +0100119 /* wait for response to arrive back */
120 timeout = wait_for_completion_timeout(&cancellation->done,
121 msecs_to_jiffies(
122 CANCEL_INFERENCE_RESP_TIMEOUT_MS));
123 /* take back the mutex before resuming to do anything */
Kristofer Jonssonec477042023-01-20 13:38:13 +0100124 ret = device_lock_interruptible(dev);
Davide Grohmann7e8f5082022-03-23 12:48:45 +0100125 if (0 != ret)
Davide Grohmann32660f92022-04-27 16:49:07 +0200126 goto deregister;
Davide Grohmann7e8f5082022-03-23 12:48:45 +0100127
128 if (0 == timeout /* timed out*/) {
Kristofer Jonssonec477042023-01-20 13:38:13 +0100129 dev_warn(dev,
130 "Msg: Cancel Inference response lost - timeoutdev");
Davide Grohmann7e8f5082022-03-23 12:48:45 +0100131 ret = -EIO;
Kristofer Jonsson074ef902023-01-23 13:05:36 +0100132
133 rproc_report_crash(rproc_get_by_child(dev), RPROC_FATAL_ERROR);
Davide Grohmann32660f92022-04-27 16:49:07 +0200134 goto deregister;
Davide Grohmann7e8f5082022-03-23 12:48:45 +0100135 }
136
137 if (cancellation->errno) {
138 ret = cancellation->errno;
Kristofer Jonsson074ef902023-01-23 13:05:36 +0100139 rproc_report_crash(rproc_get_by_child(dev), RPROC_FATAL_ERROR);
Davide Grohmann32660f92022-04-27 16:49:07 +0200140 goto deregister;
Davide Grohmann7e8f5082022-03-23 12:48:45 +0100141 }
142
Mikael Olsson6d5e2d22024-01-16 11:19:09 +0100143 if (inf->status != ETHOSU_UAPI_STATUS_ABORTED)
144 inf->status = ETHOSU_UAPI_STATUS_ABORTED;
145
Davide Grohmann32660f92022-04-27 16:49:07 +0200146deregister:
Mikael Olsson16be2852024-02-12 09:56:56 +0100147 ethosu_rpmsg_mailbox_deregister(mailbox,
148 &cancellation->msg);
Davide Grohmann32660f92022-04-27 16:49:07 +0200149
150kfree:
Ledion Dajaedd25502023-10-17 09:15:32 +0200151 dev_dbg(dev,
152 "Cancel inference destroy. cancel=0x%pK", cancellation);
Kristofer Jonsson074ef902023-01-23 13:05:36 +0100153
Davide Grohmann32660f92022-04-27 16:49:07 +0200154 /* decrease the reference on the inference we are refering to */
Mikael Olsson16be2852024-02-12 09:56:56 +0100155 ethosu_rpmsg_inference_put(cancellation->inf);
Kristofer Jonssonec477042023-01-20 13:38:13 +0100156 devm_kfree(dev, cancellation);
Davide Grohmann7e8f5082022-03-23 12:48:45 +0100157
158 return ret;
159}
160
Mikael Olsson16be2852024-02-12 09:56:56 +0100161void ethosu_rpmsg_cancel_inference_rsp(struct ethosu_rpmsg_mailbox *mailbox,
162 int msg_id,
163 struct ethosu_rpmsg_cancel_inference_rsp *rsp)
Davide Grohmann7e8f5082022-03-23 12:48:45 +0100164{
Kristofer Jonssonec477042023-01-20 13:38:13 +0100165 struct device *dev = mailbox->dev;
Mikael Olsson16be2852024-02-12 09:56:56 +0100166 struct ethosu_rpmsg_mailbox_msg *msg;
167 struct ethosu_rpmsg_cancel_inference *cancellation;
Davide Grohmann7e8f5082022-03-23 12:48:45 +0100168
Mikael Olsson16be2852024-02-12 09:56:56 +0100169 msg = ethosu_rpmsg_mailbox_find(mailbox, msg_id,
170 ETHOSU_RPMSG_CANCEL_INFERENCE_REQ);
Davide Grohmann32660f92022-04-27 16:49:07 +0200171 if (IS_ERR(msg)) {
Kristofer Jonssonec477042023-01-20 13:38:13 +0100172 dev_warn(dev,
Mikael Olsson09965b02023-06-13 12:17:04 +0200173 "Id for cancel inference msg not found. Id=0x%x: %ld",
174 msg_id, PTR_ERR(msg));
Davide Grohmann7e8f5082022-03-23 12:48:45 +0100175
176 return;
177 }
178
Davide Grohmann32660f92022-04-27 16:49:07 +0200179 cancellation = container_of(msg, typeof(*cancellation), msg);
180
Davide Grohmann7e8f5082022-03-23 12:48:45 +0100181 if (completion_done(&cancellation->done))
182 return;
183
184 cancellation->errno = 0;
185 switch (rsp->status) {
Mikael Olsson16be2852024-02-12 09:56:56 +0100186 case ETHOSU_RPMSG_STATUS_OK:
Davide Grohmann7e8f5082022-03-23 12:48:45 +0100187 cancellation->uapi->status = ETHOSU_UAPI_STATUS_OK;
188 break;
Mikael Olsson16be2852024-02-12 09:56:56 +0100189 case ETHOSU_RPMSG_STATUS_ERROR:
Davide Grohmann7e8f5082022-03-23 12:48:45 +0100190 cancellation->uapi->status = ETHOSU_UAPI_STATUS_ERROR;
191 break;
192 }
193
194 complete(&cancellation->done);
195}