Davide Grohmann | 32660f9 | 2022-04-27 16:49:07 +0200 | [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 | 32660f9 | 2022-04-27 16:49:07 +0200 | [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_capabilities.h" |
| 26 | |
Kristofer Jonsson | d779a08 | 2023-01-04 17:09:47 +0100 | [diff] [blame^] | 27 | #include "ethosu_core_rpmsg.h" |
Davide Grohmann | 32660f9 | 2022-04-27 16:49:07 +0200 | [diff] [blame] | 28 | #include "ethosu_device.h" |
| 29 | |
| 30 | #include <linux/delay.h> |
| 31 | #include <linux/dma-mapping.h> |
| 32 | #include <linux/errno.h> |
| 33 | |
| 34 | /**************************************************************************** |
| 35 | * Defines |
| 36 | ****************************************************************************/ |
| 37 | |
| 38 | #define CAPABILITIES_RESP_TIMEOUT_MS 2000 |
| 39 | |
| 40 | /**************************************************************************** |
| 41 | * Functions |
| 42 | ****************************************************************************/ |
| 43 | |
| 44 | static inline int ethosu_capabilities_send(struct ethosu_capabilities *cap) |
| 45 | { |
| 46 | return ethosu_mailbox_capabilities_request(&cap->edev->mailbox, |
| 47 | &cap->msg); |
| 48 | } |
| 49 | |
| 50 | static void ethosu_capabilities_fail(struct ethosu_mailbox_msg *msg) |
| 51 | { |
| 52 | struct ethosu_capabilities *cap = |
| 53 | container_of(msg, typeof(*cap), msg); |
| 54 | |
| 55 | if (completion_done(&cap->done)) |
| 56 | return; |
| 57 | |
| 58 | cap->errno = -EFAULT; |
| 59 | complete(&cap->done); |
| 60 | } |
| 61 | |
Davide Grohmann | 32660f9 | 2022-04-27 16:49:07 +0200 | [diff] [blame] | 62 | void ethosu_capability_rsp(struct ethosu_device *edev, |
Kristofer Jonsson | d779a08 | 2023-01-04 17:09:47 +0100 | [diff] [blame^] | 63 | int msg_id, |
Davide Grohmann | 32660f9 | 2022-04-27 16:49:07 +0200 | [diff] [blame] | 64 | struct ethosu_core_msg_capabilities_rsp *rsp) |
| 65 | { |
Davide Grohmann | 32660f9 | 2022-04-27 16:49:07 +0200 | [diff] [blame] | 66 | struct ethosu_mailbox_msg *msg; |
| 67 | struct ethosu_capabilities *cap; |
| 68 | |
Kristofer Jonsson | d779a08 | 2023-01-04 17:09:47 +0100 | [diff] [blame^] | 69 | msg = ethosu_mailbox_find(&edev->mailbox, msg_id); |
Davide Grohmann | 32660f9 | 2022-04-27 16:49:07 +0200 | [diff] [blame] | 70 | if (IS_ERR(msg)) { |
| 71 | dev_warn(edev->dev, |
| 72 | "Id for capabilities msg not found. id=%d\n", |
Kristofer Jonsson | d779a08 | 2023-01-04 17:09:47 +0100 | [diff] [blame^] | 73 | msg_id); |
Davide Grohmann | 32660f9 | 2022-04-27 16:49:07 +0200 | [diff] [blame] | 74 | |
| 75 | return; |
| 76 | } |
| 77 | |
| 78 | cap = container_of(msg, typeof(*cap), msg); |
| 79 | |
| 80 | if (completion_done(&cap->done)) |
| 81 | return; |
| 82 | |
| 83 | cap->uapi->hw_id.version_status = rsp->version_status; |
| 84 | cap->uapi->hw_id.version_minor = rsp->version_minor; |
| 85 | cap->uapi->hw_id.version_major = rsp->version_major; |
| 86 | cap->uapi->hw_id.product_major = rsp->product_major; |
| 87 | cap->uapi->hw_id.arch_patch_rev = rsp->arch_patch_rev; |
| 88 | cap->uapi->hw_id.arch_minor_rev = rsp->arch_minor_rev; |
| 89 | cap->uapi->hw_id.arch_major_rev = rsp->arch_major_rev; |
| 90 | cap->uapi->driver_patch_rev = rsp->driver_patch_rev; |
| 91 | cap->uapi->driver_minor_rev = rsp->driver_minor_rev; |
| 92 | cap->uapi->driver_major_rev = rsp->driver_major_rev; |
| 93 | cap->uapi->hw_cfg.macs_per_cc = rsp->macs_per_cc; |
| 94 | cap->uapi->hw_cfg.cmd_stream_version = rsp->cmd_stream_version; |
| 95 | cap->uapi->hw_cfg.custom_dma = rsp->custom_dma; |
| 96 | |
| 97 | cap->errno = 0; |
| 98 | complete(&cap->done); |
| 99 | } |
| 100 | |
| 101 | int ethosu_capabilities_request(struct ethosu_device *edev, |
| 102 | struct ethosu_uapi_device_capabilities *uapi) |
| 103 | { |
| 104 | struct ethosu_capabilities *cap; |
| 105 | int ret; |
| 106 | int timeout; |
| 107 | |
| 108 | cap = devm_kzalloc(edev->dev, sizeof(struct ethosu_capabilities), |
| 109 | GFP_KERNEL); |
| 110 | if (!cap) |
| 111 | return -ENOMEM; |
| 112 | |
| 113 | cap->edev = edev; |
| 114 | cap->uapi = uapi; |
| 115 | init_completion(&cap->done); |
| 116 | cap->msg.fail = ethosu_capabilities_fail; |
Davide Grohmann | 32660f9 | 2022-04-27 16:49:07 +0200 | [diff] [blame] | 117 | |
| 118 | ret = ethosu_mailbox_register(&cap->edev->mailbox, &cap->msg); |
| 119 | if (ret < 0) |
| 120 | goto kfree; |
| 121 | |
| 122 | dev_info(edev->dev, "Capabilities create. Id=%d, handle=0x%p\n", |
| 123 | cap->msg.id, cap); |
| 124 | |
| 125 | ret = ethosu_capabilities_send(cap); |
| 126 | if (0 != ret) |
| 127 | goto deregister; |
| 128 | |
| 129 | /* Unlock the mutex before going to block on the condition */ |
| 130 | mutex_unlock(&edev->mutex); |
| 131 | |
| 132 | /* wait for response to arrive back */ |
| 133 | timeout = wait_for_completion_timeout(&cap->done, |
| 134 | msecs_to_jiffies( |
| 135 | CAPABILITIES_RESP_TIMEOUT_MS)); |
| 136 | |
| 137 | /* take back the mutex before resuming to do anything */ |
| 138 | mutex_lock(&edev->mutex); |
| 139 | |
| 140 | if (0 == timeout) { |
| 141 | dev_warn(edev->dev, "Capabilities response timeout"); |
| 142 | ret = -ETIME; |
| 143 | goto deregister; |
| 144 | } |
| 145 | |
| 146 | if (cap->errno) { |
| 147 | ret = cap->errno; |
| 148 | goto deregister; |
| 149 | } |
| 150 | |
| 151 | deregister: |
| 152 | ethosu_mailbox_deregister(&cap->edev->mailbox, &cap->msg); |
| 153 | |
| 154 | kfree: |
| 155 | dev_info(cap->edev->dev, "Capabilities destroy. Id=%d, handle=0x%p\n", |
| 156 | cap->msg.id, cap); |
| 157 | devm_kfree(cap->edev->dev, cap); |
| 158 | |
| 159 | return ret; |
| 160 | } |