Per Åstrand | 2cd5397 | 2021-04-12 13:46:04 +0200 | [diff] [blame] | 1 | /* |
Mikael Olsson | 9d0ea8b | 2023-06-05 16:16:36 +0200 | [diff] [blame] | 2 | * SPDX-FileCopyrightText: Copyright 2021-2023 Arm Limited and/or its affiliates <open-source-office@arm.com> |
| 3 | * SPDX-License-Identifier: GPL-2.0-only |
Per Åstrand | 2cd5397 | 2021-04-12 13:46:04 +0200 | [diff] [blame] | 4 | * |
| 5 | * This program is free software and is provided to you under the terms of the |
| 6 | * GNU General Public License version 2 as published by the Free Software |
| 7 | * Foundation, and any use by you of this program is subject to the terms |
| 8 | * of such GNU licence. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, you can access it online at |
| 17 | * http://www.gnu.org/licenses/gpl-2.0.html. |
Per Åstrand | 2cd5397 | 2021-04-12 13:46:04 +0200 | [diff] [blame] | 18 | */ |
Mikael Olsson | 9d0ea8b | 2023-06-05 16:16:36 +0200 | [diff] [blame] | 19 | |
Kristofer Jonsson | a70bfde | 2023-01-12 10:00:03 +0100 | [diff] [blame] | 20 | #include <linux/dma-mapping.h> |
Mikael Olsson | 9d0ea8b | 2023-06-05 16:16:36 +0200 | [diff] [blame] | 21 | #include <linux/dma-direct.h> |
Per Åstrand | 2cd5397 | 2021-04-12 13:46:04 +0200 | [diff] [blame] | 22 | #include <linux/firmware.h> |
| 23 | #include <linux/io.h> |
Kristofer Jonsson | a70bfde | 2023-01-12 10:00:03 +0100 | [diff] [blame] | 24 | #include <linux/irqreturn.h> |
Per Åstrand | 2cd5397 | 2021-04-12 13:46:04 +0200 | [diff] [blame] | 25 | #include <linux/kernel.h> |
Kristofer Jonsson | a70bfde | 2023-01-12 10:00:03 +0100 | [diff] [blame] | 26 | #include <linux/mailbox_client.h> |
Per Åstrand | 2cd5397 | 2021-04-12 13:46:04 +0200 | [diff] [blame] | 27 | #include <linux/module.h> |
| 28 | #include <linux/of.h> |
| 29 | #include <linux/of_address.h> |
| 30 | #include <linux/of_device.h> |
Kristofer Jonsson | a70bfde | 2023-01-12 10:00:03 +0100 | [diff] [blame] | 31 | #include <linux/of_reserved_mem.h> |
Per Åstrand | 2cd5397 | 2021-04-12 13:46:04 +0200 | [diff] [blame] | 32 | #include <linux/platform_device.h> |
| 33 | #include <linux/remoteproc.h> |
| 34 | #include <linux/reset.h> |
Nir Ekhauz | 4823921 | 2021-11-23 10:28:34 +0200 | [diff] [blame] | 35 | #include <linux/version.h> |
Kristofer Jonsson | a70bfde | 2023-01-12 10:00:03 +0100 | [diff] [blame] | 36 | #include <linux/workqueue.h> |
| 37 | |
| 38 | /**************************************************************************** |
| 39 | * Defines |
| 40 | ****************************************************************************/ |
| 41 | |
| 42 | #define DMA_ADDR_BITS 32 /* Number of address bits */ |
Per Åstrand | 2cd5397 | 2021-04-12 13:46:04 +0200 | [diff] [blame] | 43 | |
| 44 | #define ETHOSU_RPROC_DRIVER_VERSION "0.0.1" |
| 45 | |
| 46 | #define DEFAULT_FW_FILE "arm-ethos-u65.fw" |
Kristofer Jonsson | a70bfde | 2023-01-12 10:00:03 +0100 | [diff] [blame] | 47 | #define DEFAULT_AUTO_BOOT false |
Per Åstrand | 2cd5397 | 2021-04-12 13:46:04 +0200 | [diff] [blame] | 48 | |
| 49 | /* firmware naming module parameter */ |
| 50 | static char fw_filename_param[256] = DEFAULT_FW_FILE; |
Kristofer Jonsson | a70bfde | 2023-01-12 10:00:03 +0100 | [diff] [blame] | 51 | |
Per Åstrand | 2cd5397 | 2021-04-12 13:46:04 +0200 | [diff] [blame] | 52 | /* As the remoteproc is setup at probe, just allow the filename readonly */ |
| 53 | module_param_string(filename, fw_filename_param, sizeof(fw_filename_param), |
| 54 | 0444); |
| 55 | MODULE_PARM_DESC(filename, |
| 56 | "Filename for firmware image for Ethos-U remoteproc"); |
| 57 | |
| 58 | static bool auto_boot = DEFAULT_AUTO_BOOT; |
Kristofer Jonsson | a70bfde | 2023-01-12 10:00:03 +0100 | [diff] [blame] | 59 | module_param(auto_boot, bool, 0); |
Per Åstrand | 2cd5397 | 2021-04-12 13:46:04 +0200 | [diff] [blame] | 60 | MODULE_PARM_DESC(auto_boot, "Set to one to auto boot at load."); |
| 61 | |
Mikael Olsson | 9d0ea8b | 2023-06-05 16:16:36 +0200 | [diff] [blame] | 62 | #define RSC_MAPPING RSC_VENDOR_START + 1 |
| 63 | |
| 64 | /** |
| 65 | * struct fw_rsc_map_range - memory map range |
| 66 | * @da: Start device address of the memory address range |
| 67 | * @pa: Start physical address of the memory address range |
| 68 | * @len: length of memory address range |
| 69 | * |
| 70 | * Memory range to translate between physical and device addresses. |
| 71 | */ |
| 72 | struct fw_rsc_map_range { |
| 73 | uint32_t da; |
| 74 | uint32_t pa; |
| 75 | uint32_t len; |
| 76 | } __packed; |
| 77 | |
| 78 | /** |
| 79 | * struct fw_rsc_mapping - memory map for address translation |
| 80 | * @num_ranges: Number of ranges in the memory map |
| 81 | * @range: Array of the ranges in the memory map |
| 82 | * |
| 83 | * This resource entry requests the host to provide information for how to |
| 84 | * translate between physical and device addresses. |
| 85 | */ |
| 86 | struct fw_rsc_mapping { |
| 87 | uint8_t num_ranges; |
| 88 | struct fw_rsc_map_range range[0]; |
| 89 | } __packed; |
| 90 | |
Per Åstrand | 2cd5397 | 2021-04-12 13:46:04 +0200 | [diff] [blame] | 91 | struct ethosu_rproc { |
Kristofer Jonsson | a70bfde | 2023-01-12 10:00:03 +0100 | [diff] [blame] | 92 | struct device *dev; |
| 93 | struct reset_control *rstc; |
| 94 | struct mbox_client mbox_client; |
| 95 | struct mbox_chan *ch_rx; |
| 96 | struct mbox_chan *ch_tx; |
| 97 | struct workqueue_struct *wq; |
| 98 | struct work_struct work; |
Per Åstrand | 2cd5397 | 2021-04-12 13:46:04 +0200 | [diff] [blame] | 99 | }; |
| 100 | |
Kristofer Jonsson | a70bfde | 2023-01-12 10:00:03 +0100 | [diff] [blame] | 101 | /* declaration is in remoteproc_internal.h */ |
| 102 | extern irqreturn_t rproc_vq_interrupt(struct rproc *rproc, |
| 103 | int vq_id); |
Per Åstrand | 2cd5397 | 2021-04-12 13:46:04 +0200 | [diff] [blame] | 104 | |
Kristofer Jonsson | a70bfde | 2023-01-12 10:00:03 +0100 | [diff] [blame] | 105 | static void ethosu_mbox_bottom(struct work_struct *work) |
| 106 | { |
| 107 | struct ethosu_rproc *erproc = container_of( |
| 108 | work, struct ethosu_rproc, work); |
| 109 | struct rproc *rproc = dev_get_drvdata(erproc->dev); |
Per Åstrand | 2cd5397 | 2021-04-12 13:46:04 +0200 | [diff] [blame] | 110 | |
Kristofer Jonsson | a70bfde | 2023-01-12 10:00:03 +0100 | [diff] [blame] | 111 | dev_dbg(&rproc->dev, "Handle interrupt"); |
| 112 | |
| 113 | rproc_vq_interrupt(rproc, 0); |
| 114 | } |
| 115 | |
| 116 | static void ethosu_mbox_top(struct mbox_client *client, |
| 117 | void *message) |
| 118 | { |
| 119 | struct ethosu_rproc *erproc = container_of( |
| 120 | client, struct ethosu_rproc, mbox_client); |
| 121 | |
| 122 | queue_work(erproc->wq, &erproc->work); |
| 123 | } |
| 124 | |
Mikael Olsson | 9f176ba | 2023-11-10 15:15:34 +0100 | [diff] [blame] | 125 | static int ethosu_mem_alloc(struct rproc *rproc, |
| 126 | struct rproc_mem_entry *mem) |
| 127 | { |
| 128 | struct device *dev = rproc->dev.parent; |
| 129 | void *va; |
| 130 | |
| 131 | va = (__force void *)devm_ioremap_wc(dev, mem->dma, mem->len); |
| 132 | if (IS_ERR_OR_NULL(va)) { |
| 133 | dev_err(dev, "Failed to remap address. pa=%pa, len=%zu", |
| 134 | &mem->dma, |
| 135 | mem->len); |
| 136 | |
| 137 | return -ENOMEM; |
| 138 | } |
| 139 | |
| 140 | mem->va = va; |
| 141 | mem->is_iomem = true; |
| 142 | |
| 143 | return 0; |
| 144 | } |
| 145 | |
| 146 | static int ethosu_mem_release(struct rproc *rproc, |
| 147 | struct rproc_mem_entry *mem) |
| 148 | { |
| 149 | struct device *dev = rproc->dev.parent; |
| 150 | |
| 151 | if (mem->va) |
| 152 | devm_iounmap(dev, (__force __iomem void *)mem->va); |
| 153 | |
| 154 | return 0; |
| 155 | } |
| 156 | |
Kristofer Jonsson | a70bfde | 2023-01-12 10:00:03 +0100 | [diff] [blame] | 157 | static int ethosu_add_carveout(struct rproc *rproc, |
| 158 | const phys_addr_t pa, |
| 159 | const size_t size, |
| 160 | const char *name) |
| 161 | { |
| 162 | struct device *dev = rproc->dev.parent; |
| 163 | dma_addr_t da; |
Kristofer Jonsson | a70bfde | 2023-01-12 10:00:03 +0100 | [diff] [blame] | 164 | struct rproc_mem_entry *mem; |
| 165 | |
Mikael Olsson | 9d0ea8b | 2023-06-05 16:16:36 +0200 | [diff] [blame] | 166 | da = translate_phys_to_dma(dev, pa); |
| 167 | dev_dbg(dev, "PA to DA. pa=0x%pa, da=0x%pad", &pa, &da); |
| 168 | if (da == DMA_MAPPING_ERROR) { |
| 169 | dev_err(dev, "No mapping found for PA. pa=%pa, size=%zu", &pa, |
Kristofer Jonsson | a70bfde | 2023-01-12 10:00:03 +0100 | [diff] [blame] | 170 | size); |
| 171 | |
| 172 | return -ENOMEM; |
| 173 | } |
| 174 | |
Mikael Olsson | 9f176ba | 2023-11-10 15:15:34 +0100 | [diff] [blame] | 175 | mem = rproc_mem_entry_init(dev, NULL, pa, size, da, ethosu_mem_alloc, |
| 176 | ethosu_mem_release, name); |
Kristofer Jonsson | a70bfde | 2023-01-12 10:00:03 +0100 | [diff] [blame] | 177 | if (!mem) |
| 178 | return -ENOMEM; |
| 179 | |
Ledion Daja | edd2550 | 2023-10-17 09:15:32 +0200 | [diff] [blame] | 180 | dev_dbg(dev, "Add carveout mapping. dma=%pad, da=%x, va=%p, len=%zu", |
| 181 | &mem->dma, mem->da, mem->va, mem->len); |
Kristofer Jonsson | a70bfde | 2023-01-12 10:00:03 +0100 | [diff] [blame] | 182 | |
| 183 | rproc_add_carveout(rproc, mem); |
| 184 | |
| 185 | return 0; |
| 186 | } |
| 187 | |
| 188 | static int ethosu_rproc_prepare(struct rproc *rproc) |
| 189 | { |
| 190 | struct device *dev = rproc->dev.parent; |
| 191 | struct device_node *np = dev->of_node; |
| 192 | struct of_phandle_iterator it; |
| 193 | struct resource res; |
| 194 | int i; |
| 195 | int ret; |
| 196 | |
Kristofer Jonsson | a70bfde | 2023-01-12 10:00:03 +0100 | [diff] [blame] | 197 | /* Add carveout for each 'reg' device tree entry */ |
| 198 | for (i = 0; of_address_to_resource(np, i, &res) == 0; i++) { |
Ledion Daja | edd2550 | 2023-10-17 09:15:32 +0200 | [diff] [blame] | 199 | dev_dbg(dev, "Found resource. start=%llx, size=%llx", |
| 200 | res.start, resource_size(&res)); |
Kristofer Jonsson | a70bfde | 2023-01-12 10:00:03 +0100 | [diff] [blame] | 201 | |
| 202 | ret = ethosu_add_carveout(rproc, res.start, |
| 203 | resource_size(&res), res.name); |
| 204 | if (ret) |
| 205 | return ret; |
| 206 | } |
| 207 | |
| 208 | of_phandle_iterator_init(&it, np, "memory-region", NULL, 0); |
| 209 | while (of_phandle_iterator_next(&it) == 0) { |
| 210 | struct reserved_mem *res_mem = of_reserved_mem_lookup(it.node); |
| 211 | |
| 212 | if (!res_mem) { |
| 213 | dev_err(dev, "Failed to look up memory region. node=%p", |
| 214 | it.node); |
| 215 | |
| 216 | return -EINVAL; |
| 217 | } |
| 218 | |
Ledion Daja | edd2550 | 2023-10-17 09:15:32 +0200 | [diff] [blame] | 219 | dev_dbg(dev, |
| 220 | "Found memory region. pa=%llx, size=%llu, name=%s", |
| 221 | res_mem->base, res_mem->size, it.node->name); |
Kristofer Jonsson | a70bfde | 2023-01-12 10:00:03 +0100 | [diff] [blame] | 222 | |
| 223 | ret = ethosu_add_carveout(rproc, res_mem->base, res_mem->size, |
| 224 | it.node->name); |
| 225 | if (ret) |
| 226 | return ret; |
| 227 | } |
| 228 | |
| 229 | return 0; |
| 230 | } |
Per Åstrand | 2cd5397 | 2021-04-12 13:46:04 +0200 | [diff] [blame] | 231 | |
| 232 | static int ethosu_rproc_start(struct rproc *rproc) |
| 233 | { |
Kristofer Jonsson | a70bfde | 2023-01-12 10:00:03 +0100 | [diff] [blame] | 234 | struct ethosu_rproc *erproc = (struct ethosu_rproc *)rproc->priv; |
| 235 | struct device *dev = erproc->dev; |
Per Åstrand | 2cd5397 | 2021-04-12 13:46:04 +0200 | [diff] [blame] | 236 | |
Kristofer Jonsson | a70bfde | 2023-01-12 10:00:03 +0100 | [diff] [blame] | 237 | dev_info(dev, "Starting up Ethos-U subsystem CPU"); |
Per Åstrand | 2cd5397 | 2021-04-12 13:46:04 +0200 | [diff] [blame] | 238 | |
Kristofer Jonsson | a70bfde | 2023-01-12 10:00:03 +0100 | [diff] [blame] | 239 | return reset_control_deassert(erproc->rstc); |
Per Åstrand | 2cd5397 | 2021-04-12 13:46:04 +0200 | [diff] [blame] | 240 | } |
| 241 | |
| 242 | static int ethosu_rproc_stop(struct rproc *rproc) |
| 243 | { |
Kristofer Jonsson | a70bfde | 2023-01-12 10:00:03 +0100 | [diff] [blame] | 244 | struct ethosu_rproc *erproc = (struct ethosu_rproc *)rproc->priv; |
| 245 | struct device *dev = erproc->dev; |
Per Åstrand | 2cd5397 | 2021-04-12 13:46:04 +0200 | [diff] [blame] | 246 | |
Kristofer Jonsson | a70bfde | 2023-01-12 10:00:03 +0100 | [diff] [blame] | 247 | dev_info(dev, "Stopping Ethos-U subsystem CPU"); |
Per Åstrand | 2cd5397 | 2021-04-12 13:46:04 +0200 | [diff] [blame] | 248 | |
Kristofer Jonsson | a70bfde | 2023-01-12 10:00:03 +0100 | [diff] [blame] | 249 | return reset_control_assert(erproc->rstc); |
Per Åstrand | 2cd5397 | 2021-04-12 13:46:04 +0200 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | static void ethosu_rproc_kick(struct rproc *rproc, |
| 253 | int vqid) |
| 254 | { |
Kristofer Jonsson | a70bfde | 2023-01-12 10:00:03 +0100 | [diff] [blame] | 255 | struct ethosu_rproc *erproc = (struct ethosu_rproc *)rproc->priv; |
Per Åstrand | 2cd5397 | 2021-04-12 13:46:04 +0200 | [diff] [blame] | 256 | |
Kristofer Jonsson | a70bfde | 2023-01-12 10:00:03 +0100 | [diff] [blame] | 257 | dev_dbg(&rproc->dev, "Kicking Ethos-U remoteproc vqid: %d!", vqid); |
Per Åstrand | 2cd5397 | 2021-04-12 13:46:04 +0200 | [diff] [blame] | 258 | |
Kristofer Jonsson | a70bfde | 2023-01-12 10:00:03 +0100 | [diff] [blame] | 259 | mbox_send_message(erproc->ch_tx, (void *)&vqid); |
Per Åstrand | 2cd5397 | 2021-04-12 13:46:04 +0200 | [diff] [blame] | 260 | } |
| 261 | |
Mikael Olsson | 9d0ea8b | 2023-06-05 16:16:36 +0200 | [diff] [blame] | 262 | static int ethosu_rproc_handle_rsc(struct rproc *rproc, |
| 263 | u32 rsc_type, |
| 264 | void *rsc, |
| 265 | int offset, |
| 266 | int avail) |
| 267 | { |
| 268 | struct ethosu_rproc *erproc = (struct ethosu_rproc *)rproc->priv; |
| 269 | struct device *dev = erproc->dev; |
| 270 | struct fw_rsc_mapping *mapping = rsc; |
| 271 | const struct bus_dma_region *map; |
| 272 | size_t num_ranges = 0U; |
| 273 | size_t i; |
| 274 | |
| 275 | if (rsc_type != RSC_MAPPING) |
| 276 | return RSC_IGNORED; |
| 277 | |
| 278 | if (struct_size(mapping, range, mapping->num_ranges) > avail) { |
| 279 | dev_err(dev, "mapping rsc is truncated\n"); |
| 280 | |
| 281 | return -EINVAL; |
| 282 | } |
| 283 | |
| 284 | for (map = dev->dma_range_map; map->size; ++map) |
| 285 | num_ranges++; |
| 286 | |
| 287 | if (num_ranges > mapping->num_ranges) { |
| 288 | dev_err(dev, |
| 289 | "Mapping rsc doesn't have enough room for DMA ranges\n"); |
| 290 | |
| 291 | return -EINVAL; |
| 292 | } |
| 293 | |
| 294 | for (i = 0U; i < num_ranges; ++i) { |
Mikael Olsson | 9d0ea8b | 2023-06-05 16:16:36 +0200 | [diff] [blame] | 295 | struct fw_rsc_map_range *range = &mapping->range[i]; |
Mikael Olsson | f1b23f3 | 2023-10-06 13:39:16 +0200 | [diff] [blame] | 296 | map = &dev->dma_range_map[i]; |
Mikael Olsson | 9d0ea8b | 2023-06-05 16:16:36 +0200 | [diff] [blame] | 297 | |
| 298 | range->da = map->dma_start; |
| 299 | range->pa = map->cpu_start; |
| 300 | range->len = map->size; |
| 301 | } |
| 302 | |
| 303 | dev_dbg(dev, "handle_rsc: Mapping rsc setup"); |
| 304 | |
| 305 | return RSC_HANDLED; |
| 306 | } |
| 307 | |
Per Åstrand | 2cd5397 | 2021-04-12 13:46:04 +0200 | [diff] [blame] | 308 | static const struct rproc_ops ethosu_rproc_ops = { |
Mikael Olsson | 9d0ea8b | 2023-06-05 16:16:36 +0200 | [diff] [blame] | 309 | .prepare = ðosu_rproc_prepare, |
| 310 | .start = ðosu_rproc_start, |
| 311 | .stop = ðosu_rproc_stop, |
| 312 | .kick = ðosu_rproc_kick, |
| 313 | .handle_rsc = ðosu_rproc_handle_rsc, |
Per Åstrand | 2cd5397 | 2021-04-12 13:46:04 +0200 | [diff] [blame] | 314 | }; |
| 315 | |
Kristofer Jonsson | a70bfde | 2023-01-12 10:00:03 +0100 | [diff] [blame] | 316 | static int ethosu_mailbox_init(struct ethosu_rproc *erproc) |
Per Åstrand | 2cd5397 | 2021-04-12 13:46:04 +0200 | [diff] [blame] | 317 | { |
Kristofer Jonsson | a70bfde | 2023-01-12 10:00:03 +0100 | [diff] [blame] | 318 | struct device *dev = erproc->dev; |
| 319 | struct mbox_client *cl = &erproc->mbox_client; |
Per Åstrand | 2cd5397 | 2021-04-12 13:46:04 +0200 | [diff] [blame] | 320 | |
Kristofer Jonsson | a70bfde | 2023-01-12 10:00:03 +0100 | [diff] [blame] | 321 | INIT_WORK(&erproc->work, ethosu_mbox_bottom); |
Per Åstrand | 2cd5397 | 2021-04-12 13:46:04 +0200 | [diff] [blame] | 322 | |
Kristofer Jonsson | a70bfde | 2023-01-12 10:00:03 +0100 | [diff] [blame] | 323 | erproc->wq = create_singlethread_workqueue("ethosu_rproc_wq"); |
| 324 | if (!erproc->wq) { |
| 325 | dev_err(dev, "Failed to create work queue"); |
Per Åstrand | 2cd5397 | 2021-04-12 13:46:04 +0200 | [diff] [blame] | 326 | |
| 327 | return -EINVAL; |
| 328 | } |
| 329 | |
Kristofer Jonsson | a70bfde | 2023-01-12 10:00:03 +0100 | [diff] [blame] | 330 | cl->dev = dev; |
| 331 | cl->rx_callback = ethosu_mbox_top; |
| 332 | cl->tx_prepare = NULL; |
| 333 | cl->tx_done = NULL; |
| 334 | cl->tx_block = true; |
| 335 | cl->knows_txdone = false; |
| 336 | cl->tx_tout = 500; |
Per Åstrand | 2cd5397 | 2021-04-12 13:46:04 +0200 | [diff] [blame] | 337 | |
Kristofer Jonsson | a70bfde | 2023-01-12 10:00:03 +0100 | [diff] [blame] | 338 | erproc->ch_rx = mbox_request_channel_byname(cl, "rx"); |
| 339 | if (IS_ERR(erproc->ch_rx)) { |
| 340 | dev_err(dev, "Failed to request mbox chan rx"); |
Per Åstrand | 2cd5397 | 2021-04-12 13:46:04 +0200 | [diff] [blame] | 341 | |
Kristofer Jonsson | a70bfde | 2023-01-12 10:00:03 +0100 | [diff] [blame] | 342 | return PTR_ERR(erproc->ch_rx); |
Per Åstrand | 2cd5397 | 2021-04-12 13:46:04 +0200 | [diff] [blame] | 343 | } |
| 344 | |
Kristofer Jonsson | a70bfde | 2023-01-12 10:00:03 +0100 | [diff] [blame] | 345 | erproc->ch_tx = mbox_request_channel_byname(cl, "tx"); |
| 346 | if (IS_ERR(erproc->ch_tx)) { |
Ledion Daja | edd2550 | 2023-10-17 09:15:32 +0200 | [diff] [blame] | 347 | dev_dbg(dev, "Using same channel for RX and TX"); |
Kristofer Jonsson | a70bfde | 2023-01-12 10:00:03 +0100 | [diff] [blame] | 348 | erproc->ch_tx = erproc->ch_rx; |
| 349 | } |
Per Åstrand | 2cd5397 | 2021-04-12 13:46:04 +0200 | [diff] [blame] | 350 | |
Kristofer Jonsson | a70bfde | 2023-01-12 10:00:03 +0100 | [diff] [blame] | 351 | return 0; |
Per Åstrand | 2cd5397 | 2021-04-12 13:46:04 +0200 | [diff] [blame] | 352 | } |
| 353 | |
| 354 | static const struct of_device_id ethosu_rproc_match[] = { |
Per Åstrand | 9f36f2e | 2021-09-30 09:57:34 +0200 | [diff] [blame] | 355 | { .compatible = "arm,ethosu-rproc" }, |
Per Åstrand | b9248a4 | 2022-05-18 16:05:16 +0200 | [diff] [blame] | 356 | { /* sentinel */ }, |
Per Åstrand | 2cd5397 | 2021-04-12 13:46:04 +0200 | [diff] [blame] | 357 | }; |
| 358 | |
| 359 | static int ethosu_rproc_probe(struct platform_device *pdev) |
| 360 | { |
| 361 | struct device *dev = &pdev->dev; |
| 362 | struct device_node *np = dev->of_node; |
Kristofer Jonsson | a70bfde | 2023-01-12 10:00:03 +0100 | [diff] [blame] | 363 | struct ethosu_rproc *erproc; |
Per Åstrand | 2cd5397 | 2021-04-12 13:46:04 +0200 | [diff] [blame] | 364 | struct rproc *rproc; |
Kristofer Jonsson | a70bfde | 2023-01-12 10:00:03 +0100 | [diff] [blame] | 365 | int ret; |
Per Åstrand | 2cd5397 | 2021-04-12 13:46:04 +0200 | [diff] [blame] | 366 | |
Kristofer Jonsson | a70bfde | 2023-01-12 10:00:03 +0100 | [diff] [blame] | 367 | /* map the first 'memory-region' for DMA-mapping */ |
| 368 | ret = of_reserved_mem_device_init(dev); |
| 369 | if (ret) |
| 370 | return ret; |
| 371 | |
| 372 | dma_set_mask_and_coherent(dev, DMA_BIT_MASK(DMA_ADDR_BITS)); |
| 373 | |
| 374 | rproc = devm_rproc_alloc(dev, np->name, ðosu_rproc_ops, |
| 375 | fw_filename_param, |
| 376 | sizeof(*erproc)); |
| 377 | if (!rproc) |
| 378 | return -ENOMEM; |
| 379 | |
| 380 | platform_set_drvdata(pdev, rproc); |
Per Åstrand | 2cd5397 | 2021-04-12 13:46:04 +0200 | [diff] [blame] | 381 | |
| 382 | /* Configure rproc */ |
| 383 | rproc->has_iommu = false; |
| 384 | rproc->auto_boot = auto_boot; |
| 385 | |
Kristofer Jonsson | a70bfde | 2023-01-12 10:00:03 +0100 | [diff] [blame] | 386 | /* Configure Ethos-U rproc */ |
| 387 | erproc = rproc->priv; |
| 388 | erproc->dev = dev; |
Per Åstrand | 2cd5397 | 2021-04-12 13:46:04 +0200 | [diff] [blame] | 389 | |
| 390 | /* Get the reset handler for the subsystem */ |
Kristofer Jonsson | a70bfde | 2023-01-12 10:00:03 +0100 | [diff] [blame] | 391 | erproc->rstc = devm_reset_control_get_exclusive_by_index(dev, 0); |
| 392 | if (IS_ERR(erproc->rstc)) { |
| 393 | dev_err(&pdev->dev, "Failed to get reset controller."); |
| 394 | |
| 395 | return PTR_ERR(erproc->rstc); |
Per Åstrand | 2cd5397 | 2021-04-12 13:46:04 +0200 | [diff] [blame] | 396 | } |
| 397 | |
Kristofer Jonsson | a70bfde | 2023-01-12 10:00:03 +0100 | [diff] [blame] | 398 | /* Allocate and initialize mailbox client */ |
| 399 | ret = ethosu_mailbox_init(erproc); |
Per Åstrand | 2cd5397 | 2021-04-12 13:46:04 +0200 | [diff] [blame] | 400 | if (ret) |
Kristofer Jonsson | a70bfde | 2023-01-12 10:00:03 +0100 | [diff] [blame] | 401 | return ret; |
Per Åstrand | 2cd5397 | 2021-04-12 13:46:04 +0200 | [diff] [blame] | 402 | |
| 403 | ret = rproc_add(rproc); |
Kristofer Jonsson | a70bfde | 2023-01-12 10:00:03 +0100 | [diff] [blame] | 404 | if (ret) { |
| 405 | dev_err(dev, "Failed to add rproc"); |
| 406 | goto free_mbox; |
| 407 | } |
Per Åstrand | 2cd5397 | 2021-04-12 13:46:04 +0200 | [diff] [blame] | 408 | |
Kristofer Jonsson | a70bfde | 2023-01-12 10:00:03 +0100 | [diff] [blame] | 409 | return 0; |
Per Åstrand | 2cd5397 | 2021-04-12 13:46:04 +0200 | [diff] [blame] | 410 | |
Kristofer Jonsson | a70bfde | 2023-01-12 10:00:03 +0100 | [diff] [blame] | 411 | free_mbox: |
| 412 | if (erproc->wq) |
| 413 | destroy_workqueue(erproc->wq); |
| 414 | |
| 415 | mbox_free_channel(erproc->ch_rx); |
| 416 | |
| 417 | if (erproc->ch_tx != erproc->ch_rx) |
| 418 | mbox_free_channel(erproc->ch_tx); |
Per Åstrand | 2cd5397 | 2021-04-12 13:46:04 +0200 | [diff] [blame] | 419 | |
| 420 | return ret; |
| 421 | } |
| 422 | |
| 423 | static int ethosu_rproc_remove(struct platform_device *pdev) |
| 424 | { |
| 425 | struct rproc *rproc = platform_get_drvdata(pdev); |
Kristofer Jonsson | a70bfde | 2023-01-12 10:00:03 +0100 | [diff] [blame] | 426 | struct ethosu_rproc *erproc = rproc->priv; |
| 427 | |
| 428 | if (erproc->wq) |
| 429 | destroy_workqueue(erproc->wq); |
| 430 | |
| 431 | if (erproc->ch_tx != erproc->ch_rx) |
| 432 | mbox_free_channel(erproc->ch_tx); |
| 433 | |
| 434 | mbox_free_channel(erproc->ch_rx); |
Per Åstrand | 2cd5397 | 2021-04-12 13:46:04 +0200 | [diff] [blame] | 435 | |
| 436 | rproc_del(rproc); |
Per Åstrand | 2cd5397 | 2021-04-12 13:46:04 +0200 | [diff] [blame] | 437 | |
| 438 | return 0; |
| 439 | } |
| 440 | |
| 441 | static struct platform_driver ethosu_rproc_driver = { |
| 442 | .probe = ethosu_rproc_probe, |
| 443 | .remove = ethosu_rproc_remove, |
| 444 | .driver = { |
| 445 | .name = "ethosu-rproc", |
| 446 | .of_match_table = of_match_ptr(ethosu_rproc_match), |
| 447 | }, |
| 448 | }; |
| 449 | |
| 450 | module_platform_driver(ethosu_rproc_driver); |
| 451 | |
| 452 | MODULE_LICENSE("GPL v2"); |
| 453 | MODULE_AUTHOR("Arm Ltd"); |
| 454 | MODULE_DESCRIPTION("Arm Ethos-U NPU RemoteProc Driver"); |
| 455 | MODULE_VERSION(ETHOSU_RPROC_DRIVER_VERSION); |