Kristofer Jonsson | 3c6a260 | 2022-03-10 11:17:29 +0100 | [diff] [blame^] | 1 | /* |
| 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_network_info.h" |
| 26 | |
| 27 | #include "ethosu_device.h" |
| 28 | #include "ethosu_network.h" |
| 29 | #include "ethosu_mailbox.h" |
| 30 | #include "uapi/ethosu.h" |
| 31 | |
| 32 | /**************************************************************************** |
| 33 | * Functions |
| 34 | ****************************************************************************/ |
| 35 | |
| 36 | static void ethosu_network_info_destroy(struct kref *kref) |
| 37 | { |
| 38 | struct ethosu_network_info *info = |
| 39 | container_of(kref, struct ethosu_network_info, kref); |
| 40 | |
| 41 | dev_info(info->edev->dev, "Network info destroy. handle=0x%pK\n", info); |
| 42 | |
| 43 | list_del(&info->list); |
| 44 | |
| 45 | ethosu_network_put(info->net); |
| 46 | |
| 47 | devm_kfree(info->edev->dev, info); |
| 48 | } |
| 49 | |
| 50 | struct ethosu_network_info *ethosu_network_info_create( |
| 51 | struct ethosu_device *edev, |
| 52 | struct ethosu_network *net, |
| 53 | struct ethosu_uapi_network_info *uapi) |
| 54 | { |
| 55 | struct ethosu_network_info *info; |
| 56 | int ret; |
| 57 | |
| 58 | info = devm_kzalloc(edev->dev, sizeof(*info), GFP_KERNEL); |
| 59 | if (!info) |
| 60 | return ERR_PTR(-ENOMEM); |
| 61 | |
| 62 | info->edev = edev; |
| 63 | info->net = net; |
| 64 | info->uapi = uapi; |
| 65 | kref_init(&info->kref); |
| 66 | init_completion(&info->done); |
| 67 | |
| 68 | /* Insert network info to network info list */ |
| 69 | list_add(&info->list, &edev->network_info_list); |
| 70 | |
| 71 | /* Get reference to network */ |
| 72 | ethosu_network_get(net); |
| 73 | |
| 74 | /* Send network info request to firmware */ |
| 75 | ret = ethosu_mailbox_network_info_request(&info->edev->mailbox, |
| 76 | info, |
| 77 | info->net->buf, |
| 78 | info->net->index); |
| 79 | if (ret) |
| 80 | goto put_info; |
| 81 | |
| 82 | /* Increase reference count for the pending network info response */ |
| 83 | ethosu_network_info_get(info); |
| 84 | |
| 85 | dev_info(edev->dev, "Network info create. handle=%p\n", info); |
| 86 | |
| 87 | return info; |
| 88 | |
| 89 | put_info: |
| 90 | ethosu_network_info_put(info); |
| 91 | |
| 92 | return ERR_PTR(ret); |
| 93 | } |
| 94 | |
| 95 | static int ethosu_network_info_find(struct ethosu_network_info *info, |
| 96 | struct list_head *network_info_list) |
| 97 | { |
| 98 | struct ethosu_network_info *cur; |
| 99 | |
| 100 | list_for_each_entry(cur, network_info_list, list) { |
| 101 | if (cur == info) |
| 102 | return 0; |
| 103 | } |
| 104 | |
| 105 | return -EINVAL; |
| 106 | } |
| 107 | |
| 108 | void ethosu_network_info_get(struct ethosu_network_info *info) |
| 109 | { |
| 110 | kref_get(&info->kref); |
| 111 | } |
| 112 | |
| 113 | void ethosu_network_info_put(struct ethosu_network_info *info) |
| 114 | { |
| 115 | kref_put(&info->kref, ethosu_network_info_destroy); |
| 116 | } |
| 117 | |
| 118 | int ethosu_network_info_wait(struct ethosu_network_info *info, |
| 119 | int timeout_ms) |
| 120 | { |
| 121 | int timeout; |
| 122 | |
| 123 | timeout = wait_for_completion_timeout(&info->done, |
| 124 | msecs_to_jiffies(timeout_ms)); |
| 125 | |
| 126 | if (!timeout) { |
| 127 | dev_warn(info->edev->dev, |
| 128 | "Network info timed out."); |
| 129 | |
| 130 | return -ETIME; |
| 131 | } |
| 132 | |
| 133 | return info->errno; |
| 134 | } |
| 135 | |
| 136 | void ethosu_network_info_rsp(struct ethosu_device *edev, |
| 137 | struct ethosu_core_network_info_rsp *rsp) |
| 138 | { |
| 139 | struct ethosu_network_info *info = |
| 140 | (struct ethosu_network_info *)rsp->user_arg; |
| 141 | uint32_t i; |
| 142 | int ret; |
| 143 | |
| 144 | ret = ethosu_network_info_find(info, &edev->network_info_list); |
| 145 | if (0 != ret) { |
| 146 | dev_warn(edev->dev, |
| 147 | "Handle not found in network info list. handle=0x%p\n", |
| 148 | info); |
| 149 | |
| 150 | return; |
| 151 | } |
| 152 | |
| 153 | info->errno = 0; |
| 154 | |
| 155 | if (rsp->status != ETHOSU_CORE_STATUS_OK) { |
| 156 | info->errno = -EBADF; |
| 157 | goto signal_complete; |
| 158 | } |
| 159 | |
| 160 | if (rsp->ifm_count > ETHOSU_FD_MAX || rsp->ofm_count > ETHOSU_FD_MAX) { |
| 161 | info->errno = -ENFILE; |
| 162 | goto signal_complete; |
| 163 | } |
| 164 | |
| 165 | strncpy(info->uapi->desc, rsp->desc, sizeof(info->uapi->desc)); |
| 166 | |
| 167 | info->uapi->ifm_count = rsp->ifm_count; |
| 168 | for (i = 0; i < rsp->ifm_count; i++) |
| 169 | info->uapi->ifm_size[i] = rsp->ifm_size[i]; |
| 170 | |
| 171 | info->uapi->ofm_count = rsp->ofm_count; |
| 172 | for (i = 0; i < rsp->ofm_count; i++) |
| 173 | info->uapi->ofm_size[i] = rsp->ofm_size[i]; |
| 174 | |
| 175 | signal_complete: |
| 176 | complete(&info->done); |
| 177 | |
| 178 | ethosu_network_info_put(info); |
| 179 | } |