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 | |
Kristofer Jonsson | a70bfde | 2023-01-12 10:00:03 +0100 | [diff] [blame] | 125 | static int ethosu_add_carveout(struct rproc *rproc, |
| 126 | const phys_addr_t pa, |
| 127 | const size_t size, |
| 128 | const char *name) |
| 129 | { |
| 130 | struct device *dev = rproc->dev.parent; |
| 131 | dma_addr_t da; |
| 132 | void __iomem *va; |
| 133 | struct rproc_mem_entry *mem; |
| 134 | |
Mikael Olsson | 9d0ea8b | 2023-06-05 16:16:36 +0200 | [diff] [blame] | 135 | da = translate_phys_to_dma(dev, pa); |
| 136 | dev_dbg(dev, "PA to DA. pa=0x%pa, da=0x%pad", &pa, &da); |
| 137 | if (da == DMA_MAPPING_ERROR) { |
| 138 | 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] | 139 | size); |
| 140 | |
| 141 | return -ENOMEM; |
| 142 | } |
| 143 | |
| 144 | va = devm_ioremap_wc(dev, pa, size); |
| 145 | if (!va) { |
Mikael Olsson | 9d0ea8b | 2023-06-05 16:16:36 +0200 | [diff] [blame] | 146 | dev_err(dev, "Failed to remap address. pa=%pa, len=%zu", &pa, |
Kristofer Jonsson | a70bfde | 2023-01-12 10:00:03 +0100 | [diff] [blame] | 147 | size); |
| 148 | |
| 149 | return -ENOMEM; |
| 150 | } |
| 151 | |
| 152 | mem = rproc_mem_entry_init(dev, va, pa, size, da, NULL, NULL, name); |
| 153 | if (!mem) |
| 154 | return -ENOMEM; |
| 155 | |
Mikael Olsson | 9d0ea8b | 2023-06-05 16:16:36 +0200 | [diff] [blame] | 156 | dev_info(dev, "Add carveout mapping. dma=%pad, da=%x, va=%p, len=%zu", |
| 157 | &mem->dma, mem->da, mem->va, mem->len); |
Kristofer Jonsson | a70bfde | 2023-01-12 10:00:03 +0100 | [diff] [blame] | 158 | |
| 159 | rproc_add_carveout(rproc, mem); |
| 160 | |
| 161 | return 0; |
| 162 | } |
| 163 | |
| 164 | static int ethosu_rproc_prepare(struct rproc *rproc) |
| 165 | { |
| 166 | struct device *dev = rproc->dev.parent; |
| 167 | struct device_node *np = dev->of_node; |
| 168 | struct of_phandle_iterator it; |
| 169 | struct resource res; |
| 170 | int i; |
| 171 | int ret; |
| 172 | |
| 173 | dev_info(dev, "Preparing Ethos-U"); |
| 174 | |
| 175 | /* Add carveout for each 'reg' device tree entry */ |
| 176 | for (i = 0; of_address_to_resource(np, i, &res) == 0; i++) { |
| 177 | dev_info(dev, "Found resource. start=%llx, size=%llx", |
| 178 | res.start, resource_size(&res)); |
| 179 | |
| 180 | ret = ethosu_add_carveout(rproc, res.start, |
| 181 | resource_size(&res), res.name); |
| 182 | if (ret) |
| 183 | return ret; |
| 184 | } |
| 185 | |
| 186 | of_phandle_iterator_init(&it, np, "memory-region", NULL, 0); |
| 187 | while (of_phandle_iterator_next(&it) == 0) { |
| 188 | struct reserved_mem *res_mem = of_reserved_mem_lookup(it.node); |
| 189 | |
| 190 | if (!res_mem) { |
| 191 | dev_err(dev, "Failed to look up memory region. node=%p", |
| 192 | it.node); |
| 193 | |
| 194 | return -EINVAL; |
| 195 | } |
| 196 | |
| 197 | dev_info(dev, |
| 198 | "Found memory region. pa=%llx, size=%llu, name=%s", |
| 199 | res_mem->base, res_mem->size, it.node->name); |
| 200 | |
| 201 | ret = ethosu_add_carveout(rproc, res_mem->base, res_mem->size, |
| 202 | it.node->name); |
| 203 | if (ret) |
| 204 | return ret; |
| 205 | } |
| 206 | |
| 207 | return 0; |
| 208 | } |
Per Åstrand | 2cd5397 | 2021-04-12 13:46:04 +0200 | [diff] [blame] | 209 | |
| 210 | static int ethosu_rproc_start(struct rproc *rproc) |
| 211 | { |
Kristofer Jonsson | a70bfde | 2023-01-12 10:00:03 +0100 | [diff] [blame] | 212 | struct ethosu_rproc *erproc = (struct ethosu_rproc *)rproc->priv; |
| 213 | struct device *dev = erproc->dev; |
Per Åstrand | 2cd5397 | 2021-04-12 13:46:04 +0200 | [diff] [blame] | 214 | |
Kristofer Jonsson | a70bfde | 2023-01-12 10:00:03 +0100 | [diff] [blame] | 215 | dev_info(dev, "Starting up Ethos-U subsystem CPU"); |
Per Åstrand | 2cd5397 | 2021-04-12 13:46:04 +0200 | [diff] [blame] | 216 | |
Kristofer Jonsson | a70bfde | 2023-01-12 10:00:03 +0100 | [diff] [blame] | 217 | return reset_control_deassert(erproc->rstc); |
Per Åstrand | 2cd5397 | 2021-04-12 13:46:04 +0200 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | static int ethosu_rproc_stop(struct rproc *rproc) |
| 221 | { |
Kristofer Jonsson | a70bfde | 2023-01-12 10:00:03 +0100 | [diff] [blame] | 222 | struct ethosu_rproc *erproc = (struct ethosu_rproc *)rproc->priv; |
| 223 | struct device *dev = erproc->dev; |
Per Åstrand | 2cd5397 | 2021-04-12 13:46:04 +0200 | [diff] [blame] | 224 | |
Kristofer Jonsson | a70bfde | 2023-01-12 10:00:03 +0100 | [diff] [blame] | 225 | dev_info(dev, "Stopping Ethos-U subsystem CPU"); |
Per Åstrand | 2cd5397 | 2021-04-12 13:46:04 +0200 | [diff] [blame] | 226 | |
Kristofer Jonsson | a70bfde | 2023-01-12 10:00:03 +0100 | [diff] [blame] | 227 | return reset_control_assert(erproc->rstc); |
Per Åstrand | 2cd5397 | 2021-04-12 13:46:04 +0200 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | static void ethosu_rproc_kick(struct rproc *rproc, |
| 231 | int vqid) |
| 232 | { |
Kristofer Jonsson | a70bfde | 2023-01-12 10:00:03 +0100 | [diff] [blame] | 233 | struct ethosu_rproc *erproc = (struct ethosu_rproc *)rproc->priv; |
Per Åstrand | 2cd5397 | 2021-04-12 13:46:04 +0200 | [diff] [blame] | 234 | |
Kristofer Jonsson | a70bfde | 2023-01-12 10:00:03 +0100 | [diff] [blame] | 235 | dev_dbg(&rproc->dev, "Kicking Ethos-U remoteproc vqid: %d!", vqid); |
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 | mbox_send_message(erproc->ch_tx, (void *)&vqid); |
Per Åstrand | 2cd5397 | 2021-04-12 13:46:04 +0200 | [diff] [blame] | 238 | } |
| 239 | |
Mikael Olsson | 9d0ea8b | 2023-06-05 16:16:36 +0200 | [diff] [blame] | 240 | static int ethosu_rproc_handle_rsc(struct rproc *rproc, |
| 241 | u32 rsc_type, |
| 242 | void *rsc, |
| 243 | int offset, |
| 244 | int avail) |
| 245 | { |
| 246 | struct ethosu_rproc *erproc = (struct ethosu_rproc *)rproc->priv; |
| 247 | struct device *dev = erproc->dev; |
| 248 | struct fw_rsc_mapping *mapping = rsc; |
| 249 | const struct bus_dma_region *map; |
| 250 | size_t num_ranges = 0U; |
| 251 | size_t i; |
| 252 | |
| 253 | if (rsc_type != RSC_MAPPING) |
| 254 | return RSC_IGNORED; |
| 255 | |
| 256 | if (struct_size(mapping, range, mapping->num_ranges) > avail) { |
| 257 | dev_err(dev, "mapping rsc is truncated\n"); |
| 258 | |
| 259 | return -EINVAL; |
| 260 | } |
| 261 | |
| 262 | for (map = dev->dma_range_map; map->size; ++map) |
| 263 | num_ranges++; |
| 264 | |
| 265 | if (num_ranges > mapping->num_ranges) { |
| 266 | dev_err(dev, |
| 267 | "Mapping rsc doesn't have enough room for DMA ranges\n"); |
| 268 | |
| 269 | return -EINVAL; |
| 270 | } |
| 271 | |
| 272 | for (i = 0U; i < num_ranges; ++i) { |
Mikael Olsson | 9d0ea8b | 2023-06-05 16:16:36 +0200 | [diff] [blame] | 273 | struct fw_rsc_map_range *range = &mapping->range[i]; |
Mikael Olsson | f1b23f3 | 2023-10-06 13:39:16 +0200 | [diff] [blame] | 274 | map = &dev->dma_range_map[i]; |
Mikael Olsson | 9d0ea8b | 2023-06-05 16:16:36 +0200 | [diff] [blame] | 275 | |
| 276 | range->da = map->dma_start; |
| 277 | range->pa = map->cpu_start; |
| 278 | range->len = map->size; |
| 279 | } |
| 280 | |
| 281 | dev_dbg(dev, "handle_rsc: Mapping rsc setup"); |
| 282 | |
| 283 | return RSC_HANDLED; |
| 284 | } |
| 285 | |
Per Åstrand | 2cd5397 | 2021-04-12 13:46:04 +0200 | [diff] [blame] | 286 | static const struct rproc_ops ethosu_rproc_ops = { |
Mikael Olsson | 9d0ea8b | 2023-06-05 16:16:36 +0200 | [diff] [blame] | 287 | .prepare = ðosu_rproc_prepare, |
| 288 | .start = ðosu_rproc_start, |
| 289 | .stop = ðosu_rproc_stop, |
| 290 | .kick = ðosu_rproc_kick, |
| 291 | .handle_rsc = ðosu_rproc_handle_rsc, |
Per Åstrand | 2cd5397 | 2021-04-12 13:46:04 +0200 | [diff] [blame] | 292 | }; |
| 293 | |
Kristofer Jonsson | a70bfde | 2023-01-12 10:00:03 +0100 | [diff] [blame] | 294 | static int ethosu_mailbox_init(struct ethosu_rproc *erproc) |
Per Åstrand | 2cd5397 | 2021-04-12 13:46:04 +0200 | [diff] [blame] | 295 | { |
Kristofer Jonsson | a70bfde | 2023-01-12 10:00:03 +0100 | [diff] [blame] | 296 | struct device *dev = erproc->dev; |
| 297 | struct mbox_client *cl = &erproc->mbox_client; |
Per Åstrand | 2cd5397 | 2021-04-12 13:46:04 +0200 | [diff] [blame] | 298 | |
Kristofer Jonsson | a70bfde | 2023-01-12 10:00:03 +0100 | [diff] [blame] | 299 | INIT_WORK(&erproc->work, ethosu_mbox_bottom); |
Per Åstrand | 2cd5397 | 2021-04-12 13:46:04 +0200 | [diff] [blame] | 300 | |
Kristofer Jonsson | a70bfde | 2023-01-12 10:00:03 +0100 | [diff] [blame] | 301 | erproc->wq = create_singlethread_workqueue("ethosu_rproc_wq"); |
| 302 | if (!erproc->wq) { |
| 303 | dev_err(dev, "Failed to create work queue"); |
Per Åstrand | 2cd5397 | 2021-04-12 13:46:04 +0200 | [diff] [blame] | 304 | |
| 305 | return -EINVAL; |
| 306 | } |
| 307 | |
Kristofer Jonsson | a70bfde | 2023-01-12 10:00:03 +0100 | [diff] [blame] | 308 | cl->dev = dev; |
| 309 | cl->rx_callback = ethosu_mbox_top; |
| 310 | cl->tx_prepare = NULL; |
| 311 | cl->tx_done = NULL; |
| 312 | cl->tx_block = true; |
| 313 | cl->knows_txdone = false; |
| 314 | cl->tx_tout = 500; |
Per Åstrand | 2cd5397 | 2021-04-12 13:46:04 +0200 | [diff] [blame] | 315 | |
Kristofer Jonsson | a70bfde | 2023-01-12 10:00:03 +0100 | [diff] [blame] | 316 | erproc->ch_rx = mbox_request_channel_byname(cl, "rx"); |
| 317 | if (IS_ERR(erproc->ch_rx)) { |
| 318 | dev_err(dev, "Failed to request mbox chan rx"); |
Per Åstrand | 2cd5397 | 2021-04-12 13:46:04 +0200 | [diff] [blame] | 319 | |
Kristofer Jonsson | a70bfde | 2023-01-12 10:00:03 +0100 | [diff] [blame] | 320 | return PTR_ERR(erproc->ch_rx); |
Per Åstrand | 2cd5397 | 2021-04-12 13:46:04 +0200 | [diff] [blame] | 321 | } |
| 322 | |
Kristofer Jonsson | a70bfde | 2023-01-12 10:00:03 +0100 | [diff] [blame] | 323 | erproc->ch_tx = mbox_request_channel_byname(cl, "tx"); |
| 324 | if (IS_ERR(erproc->ch_tx)) { |
| 325 | dev_info(dev, "Using same channel for RX and TX"); |
| 326 | erproc->ch_tx = erproc->ch_rx; |
| 327 | } |
Per Åstrand | 2cd5397 | 2021-04-12 13:46:04 +0200 | [diff] [blame] | 328 | |
Kristofer Jonsson | a70bfde | 2023-01-12 10:00:03 +0100 | [diff] [blame] | 329 | return 0; |
Per Åstrand | 2cd5397 | 2021-04-12 13:46:04 +0200 | [diff] [blame] | 330 | } |
| 331 | |
| 332 | static const struct of_device_id ethosu_rproc_match[] = { |
Per Åstrand | 9f36f2e | 2021-09-30 09:57:34 +0200 | [diff] [blame] | 333 | { .compatible = "arm,ethosu-rproc" }, |
Per Åstrand | b9248a4 | 2022-05-18 16:05:16 +0200 | [diff] [blame] | 334 | { /* sentinel */ }, |
Per Åstrand | 2cd5397 | 2021-04-12 13:46:04 +0200 | [diff] [blame] | 335 | }; |
| 336 | |
| 337 | static int ethosu_rproc_probe(struct platform_device *pdev) |
| 338 | { |
| 339 | struct device *dev = &pdev->dev; |
| 340 | struct device_node *np = dev->of_node; |
Kristofer Jonsson | a70bfde | 2023-01-12 10:00:03 +0100 | [diff] [blame] | 341 | struct ethosu_rproc *erproc; |
Per Åstrand | 2cd5397 | 2021-04-12 13:46:04 +0200 | [diff] [blame] | 342 | struct rproc *rproc; |
Kristofer Jonsson | a70bfde | 2023-01-12 10:00:03 +0100 | [diff] [blame] | 343 | int ret; |
Per Åstrand | 2cd5397 | 2021-04-12 13:46:04 +0200 | [diff] [blame] | 344 | |
Kristofer Jonsson | a70bfde | 2023-01-12 10:00:03 +0100 | [diff] [blame] | 345 | /* map the first 'memory-region' for DMA-mapping */ |
| 346 | ret = of_reserved_mem_device_init(dev); |
| 347 | if (ret) |
| 348 | return ret; |
| 349 | |
| 350 | dma_set_mask_and_coherent(dev, DMA_BIT_MASK(DMA_ADDR_BITS)); |
| 351 | |
| 352 | rproc = devm_rproc_alloc(dev, np->name, ðosu_rproc_ops, |
| 353 | fw_filename_param, |
| 354 | sizeof(*erproc)); |
| 355 | if (!rproc) |
| 356 | return -ENOMEM; |
| 357 | |
| 358 | platform_set_drvdata(pdev, rproc); |
Per Åstrand | 2cd5397 | 2021-04-12 13:46:04 +0200 | [diff] [blame] | 359 | |
| 360 | /* Configure rproc */ |
| 361 | rproc->has_iommu = false; |
| 362 | rproc->auto_boot = auto_boot; |
| 363 | |
Kristofer Jonsson | a70bfde | 2023-01-12 10:00:03 +0100 | [diff] [blame] | 364 | /* Configure Ethos-U rproc */ |
| 365 | erproc = rproc->priv; |
| 366 | erproc->dev = dev; |
Per Åstrand | 2cd5397 | 2021-04-12 13:46:04 +0200 | [diff] [blame] | 367 | |
| 368 | /* Get the reset handler for the subsystem */ |
Kristofer Jonsson | a70bfde | 2023-01-12 10:00:03 +0100 | [diff] [blame] | 369 | erproc->rstc = devm_reset_control_get_exclusive_by_index(dev, 0); |
| 370 | if (IS_ERR(erproc->rstc)) { |
| 371 | dev_err(&pdev->dev, "Failed to get reset controller."); |
| 372 | |
| 373 | return PTR_ERR(erproc->rstc); |
Per Åstrand | 2cd5397 | 2021-04-12 13:46:04 +0200 | [diff] [blame] | 374 | } |
| 375 | |
Kristofer Jonsson | a70bfde | 2023-01-12 10:00:03 +0100 | [diff] [blame] | 376 | /* Allocate and initialize mailbox client */ |
| 377 | ret = ethosu_mailbox_init(erproc); |
Per Åstrand | 2cd5397 | 2021-04-12 13:46:04 +0200 | [diff] [blame] | 378 | if (ret) |
Kristofer Jonsson | a70bfde | 2023-01-12 10:00:03 +0100 | [diff] [blame] | 379 | return ret; |
Per Åstrand | 2cd5397 | 2021-04-12 13:46:04 +0200 | [diff] [blame] | 380 | |
| 381 | ret = rproc_add(rproc); |
Kristofer Jonsson | a70bfde | 2023-01-12 10:00:03 +0100 | [diff] [blame] | 382 | if (ret) { |
| 383 | dev_err(dev, "Failed to add rproc"); |
| 384 | goto free_mbox; |
| 385 | } |
Per Åstrand | 2cd5397 | 2021-04-12 13:46:04 +0200 | [diff] [blame] | 386 | |
Kristofer Jonsson | a70bfde | 2023-01-12 10:00:03 +0100 | [diff] [blame] | 387 | return 0; |
Per Åstrand | 2cd5397 | 2021-04-12 13:46:04 +0200 | [diff] [blame] | 388 | |
Kristofer Jonsson | a70bfde | 2023-01-12 10:00:03 +0100 | [diff] [blame] | 389 | free_mbox: |
| 390 | if (erproc->wq) |
| 391 | destroy_workqueue(erproc->wq); |
| 392 | |
| 393 | mbox_free_channel(erproc->ch_rx); |
| 394 | |
| 395 | if (erproc->ch_tx != erproc->ch_rx) |
| 396 | mbox_free_channel(erproc->ch_tx); |
Per Åstrand | 2cd5397 | 2021-04-12 13:46:04 +0200 | [diff] [blame] | 397 | |
| 398 | return ret; |
| 399 | } |
| 400 | |
| 401 | static int ethosu_rproc_remove(struct platform_device *pdev) |
| 402 | { |
| 403 | struct rproc *rproc = platform_get_drvdata(pdev); |
Kristofer Jonsson | a70bfde | 2023-01-12 10:00:03 +0100 | [diff] [blame] | 404 | struct ethosu_rproc *erproc = rproc->priv; |
| 405 | |
| 406 | if (erproc->wq) |
| 407 | destroy_workqueue(erproc->wq); |
| 408 | |
| 409 | if (erproc->ch_tx != erproc->ch_rx) |
| 410 | mbox_free_channel(erproc->ch_tx); |
| 411 | |
| 412 | mbox_free_channel(erproc->ch_rx); |
Per Åstrand | 2cd5397 | 2021-04-12 13:46:04 +0200 | [diff] [blame] | 413 | |
| 414 | rproc_del(rproc); |
Per Åstrand | 2cd5397 | 2021-04-12 13:46:04 +0200 | [diff] [blame] | 415 | |
| 416 | return 0; |
| 417 | } |
| 418 | |
| 419 | static struct platform_driver ethosu_rproc_driver = { |
| 420 | .probe = ethosu_rproc_probe, |
| 421 | .remove = ethosu_rproc_remove, |
| 422 | .driver = { |
| 423 | .name = "ethosu-rproc", |
| 424 | .of_match_table = of_match_ptr(ethosu_rproc_match), |
| 425 | }, |
| 426 | }; |
| 427 | |
| 428 | module_platform_driver(ethosu_rproc_driver); |
| 429 | |
| 430 | MODULE_LICENSE("GPL v2"); |
| 431 | MODULE_AUTHOR("Arm Ltd"); |
| 432 | MODULE_DESCRIPTION("Arm Ethos-U NPU RemoteProc Driver"); |
| 433 | MODULE_VERSION(ETHOSU_RPROC_DRIVER_VERSION); |