blob: 5fa751586013b2f2252a3b033aeed989acdf4032 [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);
86 if (!cancellation)
87 return -ENOMEM;
88
89 /* increase ref count on the inference we are refering to */
Mikael Olsson16be2852024-02-12 09:56:56 +010090 ethosu_rpmsg_inference_get(inf);
Davide Grohmann7e8f5082022-03-23 12:48:45 +010091 /* mark inference ABORTING to avoid resending the inference message */
Mikael Olsson6d5e2d22024-01-16 11:19:09 +010092 inf->status = ETHOSU_UAPI_STATUS_ABORTING;
Davide Grohmann7e8f5082022-03-23 12:48:45 +010093
Kristofer Jonssonec477042023-01-20 13:38:13 +010094 cancellation->dev = dev;
Davide Grohmann7e8f5082022-03-23 12:48:45 +010095 cancellation->inf = inf;
96 cancellation->uapi = uapi;
Davide Grohmann7e8f5082022-03-23 12:48:45 +010097 init_completion(&cancellation->done);
Mikael Olsson16be2852024-02-12 09:56:56 +010098 cancellation->msg.fail = ethosu_rpmsg_cancel_inference_fail;
Davide Grohmann7e8f5082022-03-23 12:48:45 +010099
Mikael Olsson16be2852024-02-12 09:56:56 +0100100 ret = ethosu_rpmsg_mailbox_register(mailbox,
101 &cancellation->msg);
Davide Grohmann32660f92022-04-27 16:49:07 +0200102 if (ret < 0)
103 goto kfree;
104
Ledion Dajaedd25502023-10-17 09:15:32 +0200105 dev_dbg(dev,
106 "Inference cancellation create. cancel=0x%pK, msg.id=%ddev",
107 cancellation, cancellation->msg.id);
Davide Grohmann7e8f5082022-03-23 12:48:45 +0100108
Mikael Olsson16be2852024-02-12 09:56:56 +0100109 ret = ethosu_rpmsg_cancel_inference_send(cancellation, mailbox);
Davide Grohmann7e8f5082022-03-23 12:48:45 +0100110 if (0 != ret)
Davide Grohmann32660f92022-04-27 16:49:07 +0200111 goto deregister;
Davide Grohmann7e8f5082022-03-23 12:48:45 +0100112
113 /* Unlock the mutex before going to block on the condition */
Kristofer Jonssonec477042023-01-20 13:38:13 +0100114 device_unlock(dev);
Kristofer Jonsson074ef902023-01-23 13:05:36 +0100115
Davide Grohmann7e8f5082022-03-23 12:48:45 +0100116 /* wait for response to arrive back */
117 timeout = wait_for_completion_timeout(&cancellation->done,
118 msecs_to_jiffies(
119 CANCEL_INFERENCE_RESP_TIMEOUT_MS));
120 /* take back the mutex before resuming to do anything */
Kristofer Jonssonec477042023-01-20 13:38:13 +0100121 ret = device_lock_interruptible(dev);
Davide Grohmann7e8f5082022-03-23 12:48:45 +0100122 if (0 != ret)
Davide Grohmann32660f92022-04-27 16:49:07 +0200123 goto deregister;
Davide Grohmann7e8f5082022-03-23 12:48:45 +0100124
125 if (0 == timeout /* timed out*/) {
Kristofer Jonssonec477042023-01-20 13:38:13 +0100126 dev_warn(dev,
127 "Msg: Cancel Inference response lost - timeoutdev");
Davide Grohmann7e8f5082022-03-23 12:48:45 +0100128 ret = -EIO;
Kristofer Jonsson074ef902023-01-23 13:05:36 +0100129
130 rproc_report_crash(rproc_get_by_child(dev), RPROC_FATAL_ERROR);
Davide Grohmann32660f92022-04-27 16:49:07 +0200131 goto deregister;
Davide Grohmann7e8f5082022-03-23 12:48:45 +0100132 }
133
134 if (cancellation->errno) {
135 ret = cancellation->errno;
Kristofer Jonsson074ef902023-01-23 13:05:36 +0100136 rproc_report_crash(rproc_get_by_child(dev), RPROC_FATAL_ERROR);
Davide Grohmann32660f92022-04-27 16:49:07 +0200137 goto deregister;
Davide Grohmann7e8f5082022-03-23 12:48:45 +0100138 }
139
Mikael Olsson6d5e2d22024-01-16 11:19:09 +0100140 if (inf->status != ETHOSU_UAPI_STATUS_ABORTED)
141 inf->status = ETHOSU_UAPI_STATUS_ABORTED;
142
Davide Grohmann32660f92022-04-27 16:49:07 +0200143deregister:
Mikael Olsson16be2852024-02-12 09:56:56 +0100144 ethosu_rpmsg_mailbox_deregister(mailbox,
145 &cancellation->msg);
Davide Grohmann32660f92022-04-27 16:49:07 +0200146
147kfree:
Ledion Dajaedd25502023-10-17 09:15:32 +0200148 dev_dbg(dev,
149 "Cancel inference destroy. cancel=0x%pK", cancellation);
Kristofer Jonsson074ef902023-01-23 13:05:36 +0100150
Davide Grohmann32660f92022-04-27 16:49:07 +0200151 /* decrease the reference on the inference we are refering to */
Mikael Olsson16be2852024-02-12 09:56:56 +0100152 ethosu_rpmsg_inference_put(cancellation->inf);
Kristofer Jonssonec477042023-01-20 13:38:13 +0100153 devm_kfree(dev, cancellation);
Davide Grohmann7e8f5082022-03-23 12:48:45 +0100154
155 return ret;
156}
157
Mikael Olsson16be2852024-02-12 09:56:56 +0100158void ethosu_rpmsg_cancel_inference_rsp(struct ethosu_rpmsg_mailbox *mailbox,
159 int msg_id,
160 struct ethosu_rpmsg_cancel_inference_rsp *rsp)
Davide Grohmann7e8f5082022-03-23 12:48:45 +0100161{
Kristofer Jonssonec477042023-01-20 13:38:13 +0100162 struct device *dev = mailbox->dev;
Mikael Olsson16be2852024-02-12 09:56:56 +0100163 struct ethosu_rpmsg_mailbox_msg *msg;
164 struct ethosu_rpmsg_cancel_inference *cancellation;
Davide Grohmann7e8f5082022-03-23 12:48:45 +0100165
Mikael Olsson16be2852024-02-12 09:56:56 +0100166 msg = ethosu_rpmsg_mailbox_find(mailbox, msg_id,
167 ETHOSU_RPMSG_CANCEL_INFERENCE_REQ);
Davide Grohmann32660f92022-04-27 16:49:07 +0200168 if (IS_ERR(msg)) {
Kristofer Jonssonec477042023-01-20 13:38:13 +0100169 dev_warn(dev,
Mikael Olsson09965b02023-06-13 12:17:04 +0200170 "Id for cancel inference msg not found. Id=0x%x: %ld",
171 msg_id, PTR_ERR(msg));
Davide Grohmann7e8f5082022-03-23 12:48:45 +0100172
173 return;
174 }
175
Davide Grohmann32660f92022-04-27 16:49:07 +0200176 cancellation = container_of(msg, typeof(*cancellation), msg);
177
Davide Grohmann7e8f5082022-03-23 12:48:45 +0100178 if (completion_done(&cancellation->done))
179 return;
180
181 cancellation->errno = 0;
182 switch (rsp->status) {
Mikael Olsson16be2852024-02-12 09:56:56 +0100183 case ETHOSU_RPMSG_STATUS_OK:
Davide Grohmann7e8f5082022-03-23 12:48:45 +0100184 cancellation->uapi->status = ETHOSU_UAPI_STATUS_OK;
185 break;
Mikael Olsson16be2852024-02-12 09:56:56 +0100186 case ETHOSU_RPMSG_STATUS_ERROR:
Davide Grohmann7e8f5082022-03-23 12:48:45 +0100187 cancellation->uapi->status = ETHOSU_UAPI_STATUS_ERROR;
188 break;
189 }
190
191 complete(&cancellation->done);
192}