Davide Grohmann | 7e8f508 | 2022-03-23 12:48:45 +0100 | [diff] [blame] | 1 | /* |
Kristofer Jonsson | b42bc0b | 2023-01-04 17:09:28 +0100 | [diff] [blame] | 2 | * Copyright 2022-2023 Arm Limited and/or its affiliates |
Davide Grohmann | 7e8f508 | 2022-03-23 12:48:45 +0100 | [diff] [blame] | 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 | |
Kristofer Jonsson | d779a08 | 2023-01-04 17:09:47 +0100 | [diff] [blame] | 27 | #include "ethosu_core_rpmsg.h" |
Davide Grohmann | 7e8f508 | 2022-03-23 12:48:45 +0100 | [diff] [blame] | 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 | |
Davide Grohmann | 7e8f508 | 2022-03-23 12:48:45 +0100 | [diff] [blame] | 43 | static int ethosu_cancel_inference_send( |
Kristofer Jonsson | ec47704 | 2023-01-20 13:38:13 +0100 | [diff] [blame^] | 44 | struct ethosu_cancel_inference *cancellation, |
| 45 | struct ethosu_mailbox *mailbox) |
Davide Grohmann | 7e8f508 | 2022-03-23 12:48:45 +0100 | [diff] [blame] | 46 | { |
Kristofer Jonsson | ec47704 | 2023-01-20 13:38:13 +0100 | [diff] [blame^] | 47 | return ethosu_mailbox_cancel_inference(mailbox, |
Davide Grohmann | 32660f9 | 2022-04-27 16:49:07 +0200 | [diff] [blame] | 48 | &cancellation->msg, |
| 49 | cancellation->inf->msg.id); |
Davide Grohmann | 7e8f508 | 2022-03-23 12:48:45 +0100 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | static void ethosu_cancel_inference_fail(struct ethosu_mailbox_msg *msg) |
| 53 | { |
| 54 | struct ethosu_cancel_inference *cancellation = |
| 55 | container_of(msg, typeof(*cancellation), msg); |
| 56 | |
| 57 | if (completion_done(&cancellation->done)) |
| 58 | return; |
| 59 | |
| 60 | cancellation->errno = -EFAULT; |
| 61 | cancellation->uapi->status = ETHOSU_UAPI_STATUS_ERROR; |
| 62 | complete(&cancellation->done); |
| 63 | } |
| 64 | |
Kristofer Jonsson | ec47704 | 2023-01-20 13:38:13 +0100 | [diff] [blame^] | 65 | int ethosu_cancel_inference_request(struct device *dev, |
| 66 | struct ethosu_mailbox *mailbox, |
| 67 | struct ethosu_inference *inf, |
Davide Grohmann | 7e8f508 | 2022-03-23 12:48:45 +0100 | [diff] [blame] | 68 | struct ethosu_uapi_cancel_inference_status *uapi) |
| 69 | { |
| 70 | struct ethosu_cancel_inference *cancellation; |
| 71 | int ret; |
| 72 | int timeout; |
| 73 | |
| 74 | if (inf->done) { |
| 75 | uapi->status = ETHOSU_UAPI_STATUS_ERROR; |
| 76 | |
| 77 | return 0; |
| 78 | } |
| 79 | |
| 80 | cancellation = |
Kristofer Jonsson | ec47704 | 2023-01-20 13:38:13 +0100 | [diff] [blame^] | 81 | devm_kzalloc(dev, |
Davide Grohmann | 7e8f508 | 2022-03-23 12:48:45 +0100 | [diff] [blame] | 82 | sizeof(struct ethosu_cancel_inference), |
| 83 | GFP_KERNEL); |
| 84 | if (!cancellation) |
| 85 | return -ENOMEM; |
| 86 | |
| 87 | /* increase ref count on the inference we are refering to */ |
| 88 | ethosu_inference_get(inf); |
| 89 | /* mark inference ABORTING to avoid resending the inference message */ |
| 90 | inf->status = ETHOSU_CORE_STATUS_ABORTING; |
| 91 | |
Kristofer Jonsson | ec47704 | 2023-01-20 13:38:13 +0100 | [diff] [blame^] | 92 | cancellation->dev = dev; |
Davide Grohmann | 7e8f508 | 2022-03-23 12:48:45 +0100 | [diff] [blame] | 93 | cancellation->inf = inf; |
| 94 | cancellation->uapi = uapi; |
Davide Grohmann | 7e8f508 | 2022-03-23 12:48:45 +0100 | [diff] [blame] | 95 | init_completion(&cancellation->done); |
| 96 | cancellation->msg.fail = ethosu_cancel_inference_fail; |
| 97 | |
Kristofer Jonsson | ec47704 | 2023-01-20 13:38:13 +0100 | [diff] [blame^] | 98 | ret = ethosu_mailbox_register(mailbox, |
Davide Grohmann | 32660f9 | 2022-04-27 16:49:07 +0200 | [diff] [blame] | 99 | &cancellation->msg); |
| 100 | if (ret < 0) |
| 101 | goto kfree; |
| 102 | |
Kristofer Jonsson | ec47704 | 2023-01-20 13:38:13 +0100 | [diff] [blame^] | 103 | dev_info(dev, |
| 104 | "Inference cancellation create. cancel=0x%pK, msg.id=%ddev", |
Kristofer Jonsson | 53fd03d | 2022-06-21 16:58:45 +0200 | [diff] [blame] | 105 | cancellation, cancellation->msg.id); |
Davide Grohmann | 7e8f508 | 2022-03-23 12:48:45 +0100 | [diff] [blame] | 106 | |
Kristofer Jonsson | ec47704 | 2023-01-20 13:38:13 +0100 | [diff] [blame^] | 107 | ret = ethosu_cancel_inference_send(cancellation, mailbox); |
Davide Grohmann | 7e8f508 | 2022-03-23 12:48:45 +0100 | [diff] [blame] | 108 | if (0 != ret) |
Davide Grohmann | 32660f9 | 2022-04-27 16:49:07 +0200 | [diff] [blame] | 109 | goto deregister; |
Davide Grohmann | 7e8f508 | 2022-03-23 12:48:45 +0100 | [diff] [blame] | 110 | |
| 111 | /* Unlock the mutex before going to block on the condition */ |
Kristofer Jonsson | ec47704 | 2023-01-20 13:38:13 +0100 | [diff] [blame^] | 112 | device_unlock(dev); |
Davide Grohmann | 7e8f508 | 2022-03-23 12:48:45 +0100 | [diff] [blame] | 113 | /* wait for response to arrive back */ |
| 114 | timeout = wait_for_completion_timeout(&cancellation->done, |
| 115 | msecs_to_jiffies( |
| 116 | CANCEL_INFERENCE_RESP_TIMEOUT_MS)); |
| 117 | /* take back the mutex before resuming to do anything */ |
Kristofer Jonsson | ec47704 | 2023-01-20 13:38:13 +0100 | [diff] [blame^] | 118 | ret = device_lock_interruptible(dev); |
Davide Grohmann | 7e8f508 | 2022-03-23 12:48:45 +0100 | [diff] [blame] | 119 | if (0 != ret) |
Davide Grohmann | 32660f9 | 2022-04-27 16:49:07 +0200 | [diff] [blame] | 120 | goto deregister; |
Davide Grohmann | 7e8f508 | 2022-03-23 12:48:45 +0100 | [diff] [blame] | 121 | |
| 122 | if (0 == timeout /* timed out*/) { |
Kristofer Jonsson | ec47704 | 2023-01-20 13:38:13 +0100 | [diff] [blame^] | 123 | dev_warn(dev, |
| 124 | "Msg: Cancel Inference response lost - timeoutdev"); |
Davide Grohmann | 7e8f508 | 2022-03-23 12:48:45 +0100 | [diff] [blame] | 125 | ret = -EIO; |
Davide Grohmann | 32660f9 | 2022-04-27 16:49:07 +0200 | [diff] [blame] | 126 | goto deregister; |
Davide Grohmann | 7e8f508 | 2022-03-23 12:48:45 +0100 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | if (cancellation->errno) { |
| 130 | ret = cancellation->errno; |
Davide Grohmann | 32660f9 | 2022-04-27 16:49:07 +0200 | [diff] [blame] | 131 | goto deregister; |
Davide Grohmann | 7e8f508 | 2022-03-23 12:48:45 +0100 | [diff] [blame] | 132 | } |
| 133 | |
Davide Grohmann | 32660f9 | 2022-04-27 16:49:07 +0200 | [diff] [blame] | 134 | deregister: |
Kristofer Jonsson | ec47704 | 2023-01-20 13:38:13 +0100 | [diff] [blame^] | 135 | ethosu_mailbox_deregister(mailbox, |
Davide Grohmann | 32660f9 | 2022-04-27 16:49:07 +0200 | [diff] [blame] | 136 | &cancellation->msg); |
| 137 | |
| 138 | kfree: |
Kristofer Jonsson | ec47704 | 2023-01-20 13:38:13 +0100 | [diff] [blame^] | 139 | dev_info(dev, |
| 140 | "Cancel inference destroy. cancel=0x%pK", cancellation); |
Davide Grohmann | 32660f9 | 2022-04-27 16:49:07 +0200 | [diff] [blame] | 141 | /* decrease the reference on the inference we are refering to */ |
| 142 | ethosu_inference_put(cancellation->inf); |
Kristofer Jonsson | ec47704 | 2023-01-20 13:38:13 +0100 | [diff] [blame^] | 143 | devm_kfree(dev, cancellation); |
Davide Grohmann | 7e8f508 | 2022-03-23 12:48:45 +0100 | [diff] [blame] | 144 | |
| 145 | return ret; |
| 146 | } |
| 147 | |
Kristofer Jonsson | ec47704 | 2023-01-20 13:38:13 +0100 | [diff] [blame^] | 148 | void ethosu_cancel_inference_rsp(struct ethosu_mailbox *mailbox, |
Kristofer Jonsson | d779a08 | 2023-01-04 17:09:47 +0100 | [diff] [blame] | 149 | int msg_id, |
| 150 | struct ethosu_core_msg_cancel_inference_rsp *rsp) |
Davide Grohmann | 7e8f508 | 2022-03-23 12:48:45 +0100 | [diff] [blame] | 151 | { |
Kristofer Jonsson | ec47704 | 2023-01-20 13:38:13 +0100 | [diff] [blame^] | 152 | struct device *dev = mailbox->dev; |
Davide Grohmann | 32660f9 | 2022-04-27 16:49:07 +0200 | [diff] [blame] | 153 | struct ethosu_mailbox_msg *msg; |
| 154 | struct ethosu_cancel_inference *cancellation; |
Davide Grohmann | 7e8f508 | 2022-03-23 12:48:45 +0100 | [diff] [blame] | 155 | |
Kristofer Jonsson | ec47704 | 2023-01-20 13:38:13 +0100 | [diff] [blame^] | 156 | msg = ethosu_mailbox_find(mailbox, msg_id); |
Davide Grohmann | 32660f9 | 2022-04-27 16:49:07 +0200 | [diff] [blame] | 157 | if (IS_ERR(msg)) { |
Kristofer Jonsson | ec47704 | 2023-01-20 13:38:13 +0100 | [diff] [blame^] | 158 | dev_warn(dev, |
| 159 | "Id for cancel inference msg not found. id=%ddev", |
Kristofer Jonsson | d779a08 | 2023-01-04 17:09:47 +0100 | [diff] [blame] | 160 | msg_id); |
Davide Grohmann | 7e8f508 | 2022-03-23 12:48:45 +0100 | [diff] [blame] | 161 | |
| 162 | return; |
| 163 | } |
| 164 | |
Davide Grohmann | 32660f9 | 2022-04-27 16:49:07 +0200 | [diff] [blame] | 165 | cancellation = container_of(msg, typeof(*cancellation), msg); |
| 166 | |
Davide Grohmann | 7e8f508 | 2022-03-23 12:48:45 +0100 | [diff] [blame] | 167 | if (completion_done(&cancellation->done)) |
| 168 | return; |
| 169 | |
| 170 | cancellation->errno = 0; |
| 171 | switch (rsp->status) { |
| 172 | case ETHOSU_CORE_STATUS_OK: |
| 173 | cancellation->uapi->status = ETHOSU_UAPI_STATUS_OK; |
| 174 | break; |
| 175 | case ETHOSU_CORE_STATUS_ERROR: |
| 176 | cancellation->uapi->status = ETHOSU_UAPI_STATUS_ERROR; |
| 177 | break; |
| 178 | } |
| 179 | |
| 180 | complete(&cancellation->done); |
| 181 | } |