blob: befdd2f8c1b5b9d210e4e334044747c1b908a30e [file] [log] [blame]
Davide Grohmann7e8f5082022-03-23 12:48:45 +01001/*
2 * Copyright (c) 2022 Arm Limited.
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_cancel_inference.h"
26
27#include "ethosu_core_interface.h"
28#include "ethosu_device.h"
29#include "ethosu_inference.h"
30
31#include <linux/wait.h>
32
33/****************************************************************************
34 * Defines
35 ****************************************************************************/
36
37#define CANCEL_INFERENCE_RESP_TIMEOUT_MS 2000
38
39/****************************************************************************
40 * Functions
41 ****************************************************************************/
42
43static void ethosu_cancel_inference_destroy(struct kref *kref)
44{
45 struct ethosu_cancel_inference *cancellation =
46 container_of(kref, struct ethosu_cancel_inference, kref);
47
48 dev_info(cancellation->edev->dev,
49 "Cancel inference destroy. handle=0x%p\n", cancellation);
50 list_del(&cancellation->msg.list);
51 /* decrease the reference on the inference we are refering to */
52 ethosu_inference_put(cancellation->inf);
53 devm_kfree(cancellation->edev->dev, cancellation);
54}
55
56static int ethosu_cancel_inference_send(
57 struct ethosu_cancel_inference *cancellation)
58{
59 return ethosu_mailbox_cancel_inference(&cancellation->edev->mailbox,
60 cancellation, cancellation->inf);
61}
62
63static void ethosu_cancel_inference_fail(struct ethosu_mailbox_msg *msg)
64{
65 struct ethosu_cancel_inference *cancellation =
66 container_of(msg, typeof(*cancellation), msg);
67
68 if (completion_done(&cancellation->done))
69 return;
70
71 cancellation->errno = -EFAULT;
72 cancellation->uapi->status = ETHOSU_UAPI_STATUS_ERROR;
73 complete(&cancellation->done);
74}
75
76static int ethosu_cancel_inference_complete(struct ethosu_mailbox_msg *msg)
77{
78 struct ethosu_cancel_inference *cancellation =
79 container_of(msg, typeof(*cancellation), msg);
80
81 if (completion_done(&cancellation->done))
82 return 0;
83
84 cancellation->errno = 0;
85 cancellation->uapi->status =
86 cancellation->inf->done &&
87 cancellation->inf->status != ETHOSU_UAPI_STATUS_OK ?
88 ETHOSU_UAPI_STATUS_OK :
89 ETHOSU_UAPI_STATUS_ERROR;
90 complete(&cancellation->done);
91
92 return 0;
93}
94
95int ethosu_cancel_inference_request(struct ethosu_inference *inf,
96 struct ethosu_uapi_cancel_inference_status *uapi)
97{
98 struct ethosu_cancel_inference *cancellation;
99 int ret;
100 int timeout;
101
102 if (inf->done) {
103 uapi->status = ETHOSU_UAPI_STATUS_ERROR;
104
105 return 0;
106 }
107
108 cancellation =
109 devm_kzalloc(inf->edev->dev,
110 sizeof(struct ethosu_cancel_inference),
111 GFP_KERNEL);
112 if (!cancellation)
113 return -ENOMEM;
114
115 /* increase ref count on the inference we are refering to */
116 ethosu_inference_get(inf);
117 /* mark inference ABORTING to avoid resending the inference message */
118 inf->status = ETHOSU_CORE_STATUS_ABORTING;
119
120 cancellation->edev = inf->edev;
121 cancellation->inf = inf;
122 cancellation->uapi = uapi;
123 kref_init(&cancellation->kref);
124 init_completion(&cancellation->done);
125 cancellation->msg.fail = ethosu_cancel_inference_fail;
126
127 /* Never resend messages but always complete, since we have restart the
128 * whole firmware and marked the inference as aborted */
129 cancellation->msg.resend = ethosu_cancel_inference_complete;
130
131 /* Add cancel inference to pending list */
132 list_add(&cancellation->msg.list,
133 &cancellation->edev->mailbox.pending_list);
134
135 ret = ethosu_cancel_inference_send(cancellation);
136 if (0 != ret)
137 goto put_kref;
138
139 /* Unlock the mutex before going to block on the condition */
140 mutex_unlock(&cancellation->edev->mutex);
141 /* wait for response to arrive back */
142 timeout = wait_for_completion_timeout(&cancellation->done,
143 msecs_to_jiffies(
144 CANCEL_INFERENCE_RESP_TIMEOUT_MS));
145 /* take back the mutex before resuming to do anything */
146 ret = mutex_lock_interruptible(&cancellation->edev->mutex);
147 if (0 != ret)
148 goto put_kref;
149
150 if (0 == timeout /* timed out*/) {
151 dev_warn(inf->edev->dev,
152 "Msg: Cancel Inference response lost - timeout\n");
153 ret = -EIO;
154 goto put_kref;
155 }
156
157 if (cancellation->errno) {
158 ret = cancellation->errno;
159 goto put_kref;
160 }
161
Davide Grohmann8b1fe552022-04-07 16:58:32 +0200162 /* if cancellation failed and the inference did not complete then reset
163 * the firmware */
164 if (cancellation->uapi->status == ETHOSU_UAPI_STATUS_ERROR &&
165 !cancellation->inf->done) {
166 ret = ethosu_firmware_reset(cancellation->edev);
167 if (ret)
168 goto put_kref;
169 }
170
Davide Grohmann7e8f5082022-03-23 12:48:45 +0100171put_kref:
172 kref_put(&cancellation->kref, &ethosu_cancel_inference_destroy);
173
174 return ret;
175}
176
177void ethosu_cancel_inference_rsp(struct ethosu_device *edev,
178 struct ethosu_core_cancel_inference_rsp *rsp)
179{
180 struct ethosu_cancel_inference *cancellation =
181 (struct ethosu_cancel_inference *)rsp->user_arg;
182 int ret;
183
184 ret = ethosu_mailbox_find(&edev->mailbox, &cancellation->msg);
185 if (ret) {
186 dev_warn(edev->dev,
187 "Handle not found in cancel inference list. handle=0x%p\n",
188 rsp);
189
190 return;
191 }
192
193 if (completion_done(&cancellation->done))
194 return;
195
196 cancellation->errno = 0;
197 switch (rsp->status) {
198 case ETHOSU_CORE_STATUS_OK:
199 cancellation->uapi->status = ETHOSU_UAPI_STATUS_OK;
200 break;
201 case ETHOSU_CORE_STATUS_ERROR:
202 cancellation->uapi->status = ETHOSU_UAPI_STATUS_ERROR;
203 break;
204 }
205
206 complete(&cancellation->done);
207}