Mikael Olsson | 7c843dc | 2023-08-03 12:41:48 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2023 Arm Limited and/or its affiliates |
| 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_version.h" |
| 26 | |
| 27 | #include "ethosu_core_rpmsg.h" |
| 28 | |
| 29 | #include <linux/errno.h> |
| 30 | |
| 31 | /**************************************************************************** |
| 32 | * Defines |
| 33 | ****************************************************************************/ |
| 34 | |
| 35 | #define VERSION_RESP_TIMEOUT_MS 2000 |
| 36 | |
| 37 | /**************************************************************************** |
| 38 | * Functions |
| 39 | ****************************************************************************/ |
| 40 | |
| 41 | static void ethosu_version_fail(struct ethosu_mailbox_msg *msg) |
| 42 | { |
| 43 | struct ethosu_version *version = |
| 44 | container_of(msg, typeof(*version), msg); |
| 45 | |
| 46 | if (completion_done(&version->done)) |
| 47 | return; |
| 48 | |
| 49 | version->errno = -EFAULT; |
| 50 | complete(&version->done); |
| 51 | } |
| 52 | |
| 53 | void ethosu_version_rsp(struct ethosu_mailbox *mailbox, |
| 54 | int msg_id, |
| 55 | struct ethosu_core_msg_version_rsp *rsp) |
| 56 | { |
| 57 | struct device *dev = mailbox->dev; |
| 58 | struct ethosu_mailbox_msg *msg; |
| 59 | struct ethosu_version *version; |
| 60 | |
| 61 | msg = ethosu_mailbox_find(mailbox, msg_id, |
| 62 | ETHOSU_CORE_MSG_VERSION_REQ); |
| 63 | if (IS_ERR(msg)) { |
| 64 | dev_warn(dev, |
| 65 | "Id for version msg not found. Id=0x%0x: %ld\n", |
| 66 | msg_id, PTR_ERR(msg)); |
| 67 | |
| 68 | return; |
| 69 | } |
| 70 | |
| 71 | version = container_of(msg, typeof(*version), msg); |
| 72 | |
| 73 | if (completion_done(&version->done)) |
| 74 | return; |
| 75 | |
| 76 | if (rsp->major != ETHOSU_CORE_MSG_VERSION_MAJOR || |
| 77 | rsp->minor != ETHOSU_CORE_MSG_VERSION_MINOR) { |
| 78 | dev_warn(dev, |
| 79 | "Msg: Protocol version mismatch. Expected %u.%u.X but got %u.%u.%u", |
| 80 | ETHOSU_CORE_MSG_VERSION_MAJOR, |
| 81 | ETHOSU_CORE_MSG_VERSION_MINOR, |
| 82 | rsp->major, rsp->minor, rsp->patch); |
| 83 | version->errno = -EPROTO; |
| 84 | } else { |
| 85 | version->errno = 0; |
| 86 | } |
| 87 | |
| 88 | complete(&version->done); |
| 89 | } |
| 90 | |
| 91 | int ethosu_version_check_request(struct device *dev, |
| 92 | struct ethosu_mailbox *mailbox) |
| 93 | { |
| 94 | struct ethosu_version *version; |
| 95 | int ret; |
| 96 | int timeout; |
| 97 | |
| 98 | version = devm_kzalloc(dev, sizeof(*version), GFP_KERNEL); |
| 99 | if (!version) |
| 100 | return -ENOMEM; |
| 101 | |
| 102 | version->dev = dev; |
| 103 | init_completion(&version->done); |
| 104 | version->msg.fail = ethosu_version_fail; |
| 105 | |
| 106 | ret = ethosu_mailbox_register(mailbox, &version->msg); |
| 107 | if (ret < 0) |
| 108 | goto free_version; |
| 109 | |
| 110 | dev_dbg(dev, "Protocol version request created. Id=0x%x, handle=%pK\n", |
| 111 | version->msg.id, version); |
| 112 | |
| 113 | ret = ethosu_mailbox_version_request(mailbox, &version->msg); |
| 114 | if (ret) |
| 115 | goto deregister; |
| 116 | |
| 117 | /* Unlock the mutex to not block other messages while waiting */ |
| 118 | device_unlock(dev); |
| 119 | |
| 120 | /* Wait for version response */ |
| 121 | timeout = wait_for_completion_timeout(&version->done, |
| 122 | msecs_to_jiffies( |
| 123 | VERSION_RESP_TIMEOUT_MS)); |
| 124 | |
| 125 | /* Take back the mutex before resuming to do anything */ |
| 126 | device_lock(dev); |
| 127 | |
| 128 | if (0 == timeout) { |
| 129 | dev_warn(dev, "Protocol version response timeout"); |
| 130 | ret = -ETIME; |
| 131 | goto deregister; |
| 132 | } |
| 133 | |
| 134 | if (version->errno) { |
| 135 | ret = version->errno; |
| 136 | goto deregister; |
| 137 | } |
| 138 | |
| 139 | deregister: |
| 140 | ethosu_mailbox_deregister(mailbox, &version->msg); |
| 141 | |
| 142 | free_version: |
| 143 | dev_dbg(dev, "Protocol version destroy. Id=0x%x, handle=%pK\n", |
| 144 | version->msg.id, |
| 145 | version); |
| 146 | devm_kfree(dev, version); |
| 147 | |
| 148 | return ret; |
| 149 | } |