blob: c6d773547bc06a925eb959eda7016b362be6f3a7 [file] [log] [blame]
Per Åstrand2cd53972021-04-12 13:46:04 +02001/*
Mikael Olsson9d0ea8b2023-06-05 16:16:36 +02002 * 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 Åstrand2cd53972021-04-12 13:46:04 +02004 *
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 Åstrand2cd53972021-04-12 13:46:04 +020018 */
Mikael Olsson9d0ea8b2023-06-05 16:16:36 +020019
Kristofer Jonssona70bfde2023-01-12 10:00:03 +010020#include <linux/dma-mapping.h>
Mikael Olsson9d0ea8b2023-06-05 16:16:36 +020021#include <linux/dma-direct.h>
Per Åstrand2cd53972021-04-12 13:46:04 +020022#include <linux/firmware.h>
23#include <linux/io.h>
Kristofer Jonssona70bfde2023-01-12 10:00:03 +010024#include <linux/irqreturn.h>
Per Åstrand2cd53972021-04-12 13:46:04 +020025#include <linux/kernel.h>
Kristofer Jonssona70bfde2023-01-12 10:00:03 +010026#include <linux/mailbox_client.h>
Per Åstrand2cd53972021-04-12 13:46:04 +020027#include <linux/module.h>
28#include <linux/of.h>
29#include <linux/of_address.h>
30#include <linux/of_device.h>
Kristofer Jonssona70bfde2023-01-12 10:00:03 +010031#include <linux/of_reserved_mem.h>
Per Åstrand2cd53972021-04-12 13:46:04 +020032#include <linux/platform_device.h>
33#include <linux/remoteproc.h>
34#include <linux/reset.h>
Nir Ekhauz48239212021-11-23 10:28:34 +020035#include <linux/version.h>
Kristofer Jonssona70bfde2023-01-12 10:00:03 +010036#include <linux/workqueue.h>
37
38/****************************************************************************
39 * Defines
40 ****************************************************************************/
41
42#define DMA_ADDR_BITS 32 /* Number of address bits */
Per Åstrand2cd53972021-04-12 13:46:04 +020043
44#define ETHOSU_RPROC_DRIVER_VERSION "0.0.1"
45
46#define DEFAULT_FW_FILE "arm-ethos-u65.fw"
Kristofer Jonssona70bfde2023-01-12 10:00:03 +010047#define DEFAULT_AUTO_BOOT false
Per Åstrand2cd53972021-04-12 13:46:04 +020048
49/* firmware naming module parameter */
50static char fw_filename_param[256] = DEFAULT_FW_FILE;
Kristofer Jonssona70bfde2023-01-12 10:00:03 +010051
Per Åstrand2cd53972021-04-12 13:46:04 +020052/* As the remoteproc is setup at probe, just allow the filename readonly */
53module_param_string(filename, fw_filename_param, sizeof(fw_filename_param),
54 0444);
55MODULE_PARM_DESC(filename,
56 "Filename for firmware image for Ethos-U remoteproc");
57
58static bool auto_boot = DEFAULT_AUTO_BOOT;
Kristofer Jonssona70bfde2023-01-12 10:00:03 +010059module_param(auto_boot, bool, 0);
Per Åstrand2cd53972021-04-12 13:46:04 +020060MODULE_PARM_DESC(auto_boot, "Set to one to auto boot at load.");
61
Mikael Olsson9d0ea8b2023-06-05 16:16:36 +020062#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 */
72struct 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 */
86struct fw_rsc_mapping {
87 uint8_t num_ranges;
88 struct fw_rsc_map_range range[0];
89} __packed;
90
Per Åstrand2cd53972021-04-12 13:46:04 +020091struct ethosu_rproc {
Kristofer Jonssona70bfde2023-01-12 10:00:03 +010092 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 Åstrand2cd53972021-04-12 13:46:04 +020099};
100
Kristofer Jonssona70bfde2023-01-12 10:00:03 +0100101/* declaration is in remoteproc_internal.h */
102extern irqreturn_t rproc_vq_interrupt(struct rproc *rproc,
103 int vq_id);
Per Åstrand2cd53972021-04-12 13:46:04 +0200104
Kristofer Jonssona70bfde2023-01-12 10:00:03 +0100105static 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 Åstrand2cd53972021-04-12 13:46:04 +0200110
Kristofer Jonssona70bfde2023-01-12 10:00:03 +0100111 dev_dbg(&rproc->dev, "Handle interrupt");
112
113 rproc_vq_interrupt(rproc, 0);
114}
115
116static 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 Jonssona70bfde2023-01-12 10:00:03 +0100125static 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 Olsson9d0ea8b2023-06-05 16:16:36 +0200135 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 Jonssona70bfde2023-01-12 10:00:03 +0100139 size);
140
141 return -ENOMEM;
142 }
143
144 va = devm_ioremap_wc(dev, pa, size);
145 if (!va) {
Mikael Olsson9d0ea8b2023-06-05 16:16:36 +0200146 dev_err(dev, "Failed to remap address. pa=%pa, len=%zu", &pa,
Kristofer Jonssona70bfde2023-01-12 10:00:03 +0100147 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
Ledion Dajaedd25502023-10-17 09:15:32 +0200156 dev_dbg(dev, "Add carveout mapping. dma=%pad, da=%x, va=%p, len=%zu",
157 &mem->dma, mem->da, mem->va, mem->len);
Kristofer Jonssona70bfde2023-01-12 10:00:03 +0100158
159 rproc_add_carveout(rproc, mem);
160
161 return 0;
162}
163
164static 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
Kristofer Jonssona70bfde2023-01-12 10:00:03 +0100173 /* Add carveout for each 'reg' device tree entry */
174 for (i = 0; of_address_to_resource(np, i, &res) == 0; i++) {
Ledion Dajaedd25502023-10-17 09:15:32 +0200175 dev_dbg(dev, "Found resource. start=%llx, size=%llx",
176 res.start, resource_size(&res));
Kristofer Jonssona70bfde2023-01-12 10:00:03 +0100177
178 ret = ethosu_add_carveout(rproc, res.start,
179 resource_size(&res), res.name);
180 if (ret)
181 return ret;
182 }
183
184 of_phandle_iterator_init(&it, np, "memory-region", NULL, 0);
185 while (of_phandle_iterator_next(&it) == 0) {
186 struct reserved_mem *res_mem = of_reserved_mem_lookup(it.node);
187
188 if (!res_mem) {
189 dev_err(dev, "Failed to look up memory region. node=%p",
190 it.node);
191
192 return -EINVAL;
193 }
194
Ledion Dajaedd25502023-10-17 09:15:32 +0200195 dev_dbg(dev,
196 "Found memory region. pa=%llx, size=%llu, name=%s",
197 res_mem->base, res_mem->size, it.node->name);
Kristofer Jonssona70bfde2023-01-12 10:00:03 +0100198
199 ret = ethosu_add_carveout(rproc, res_mem->base, res_mem->size,
200 it.node->name);
201 if (ret)
202 return ret;
203 }
204
205 return 0;
206}
Per Åstrand2cd53972021-04-12 13:46:04 +0200207
208static int ethosu_rproc_start(struct rproc *rproc)
209{
Kristofer Jonssona70bfde2023-01-12 10:00:03 +0100210 struct ethosu_rproc *erproc = (struct ethosu_rproc *)rproc->priv;
211 struct device *dev = erproc->dev;
Per Åstrand2cd53972021-04-12 13:46:04 +0200212
Kristofer Jonssona70bfde2023-01-12 10:00:03 +0100213 dev_info(dev, "Starting up Ethos-U subsystem CPU");
Per Åstrand2cd53972021-04-12 13:46:04 +0200214
Kristofer Jonssona70bfde2023-01-12 10:00:03 +0100215 return reset_control_deassert(erproc->rstc);
Per Åstrand2cd53972021-04-12 13:46:04 +0200216}
217
218static int ethosu_rproc_stop(struct rproc *rproc)
219{
Kristofer Jonssona70bfde2023-01-12 10:00:03 +0100220 struct ethosu_rproc *erproc = (struct ethosu_rproc *)rproc->priv;
221 struct device *dev = erproc->dev;
Per Åstrand2cd53972021-04-12 13:46:04 +0200222
Kristofer Jonssona70bfde2023-01-12 10:00:03 +0100223 dev_info(dev, "Stopping Ethos-U subsystem CPU");
Per Åstrand2cd53972021-04-12 13:46:04 +0200224
Kristofer Jonssona70bfde2023-01-12 10:00:03 +0100225 return reset_control_assert(erproc->rstc);
Per Åstrand2cd53972021-04-12 13:46:04 +0200226}
227
228static void ethosu_rproc_kick(struct rproc *rproc,
229 int vqid)
230{
Kristofer Jonssona70bfde2023-01-12 10:00:03 +0100231 struct ethosu_rproc *erproc = (struct ethosu_rproc *)rproc->priv;
Per Åstrand2cd53972021-04-12 13:46:04 +0200232
Kristofer Jonssona70bfde2023-01-12 10:00:03 +0100233 dev_dbg(&rproc->dev, "Kicking Ethos-U remoteproc vqid: %d!", vqid);
Per Åstrand2cd53972021-04-12 13:46:04 +0200234
Kristofer Jonssona70bfde2023-01-12 10:00:03 +0100235 mbox_send_message(erproc->ch_tx, (void *)&vqid);
Per Åstrand2cd53972021-04-12 13:46:04 +0200236}
237
Mikael Olsson9d0ea8b2023-06-05 16:16:36 +0200238static int ethosu_rproc_handle_rsc(struct rproc *rproc,
239 u32 rsc_type,
240 void *rsc,
241 int offset,
242 int avail)
243{
244 struct ethosu_rproc *erproc = (struct ethosu_rproc *)rproc->priv;
245 struct device *dev = erproc->dev;
246 struct fw_rsc_mapping *mapping = rsc;
247 const struct bus_dma_region *map;
248 size_t num_ranges = 0U;
249 size_t i;
250
251 if (rsc_type != RSC_MAPPING)
252 return RSC_IGNORED;
253
254 if (struct_size(mapping, range, mapping->num_ranges) > avail) {
255 dev_err(dev, "mapping rsc is truncated\n");
256
257 return -EINVAL;
258 }
259
260 for (map = dev->dma_range_map; map->size; ++map)
261 num_ranges++;
262
263 if (num_ranges > mapping->num_ranges) {
264 dev_err(dev,
265 "Mapping rsc doesn't have enough room for DMA ranges\n");
266
267 return -EINVAL;
268 }
269
270 for (i = 0U; i < num_ranges; ++i) {
Mikael Olsson9d0ea8b2023-06-05 16:16:36 +0200271 struct fw_rsc_map_range *range = &mapping->range[i];
Mikael Olssonf1b23f32023-10-06 13:39:16 +0200272 map = &dev->dma_range_map[i];
Mikael Olsson9d0ea8b2023-06-05 16:16:36 +0200273
274 range->da = map->dma_start;
275 range->pa = map->cpu_start;
276 range->len = map->size;
277 }
278
279 dev_dbg(dev, "handle_rsc: Mapping rsc setup");
280
281 return RSC_HANDLED;
282}
283
Per Åstrand2cd53972021-04-12 13:46:04 +0200284static const struct rproc_ops ethosu_rproc_ops = {
Mikael Olsson9d0ea8b2023-06-05 16:16:36 +0200285 .prepare = &ethosu_rproc_prepare,
286 .start = &ethosu_rproc_start,
287 .stop = &ethosu_rproc_stop,
288 .kick = &ethosu_rproc_kick,
289 .handle_rsc = &ethosu_rproc_handle_rsc,
Per Åstrand2cd53972021-04-12 13:46:04 +0200290};
291
Kristofer Jonssona70bfde2023-01-12 10:00:03 +0100292static int ethosu_mailbox_init(struct ethosu_rproc *erproc)
Per Åstrand2cd53972021-04-12 13:46:04 +0200293{
Kristofer Jonssona70bfde2023-01-12 10:00:03 +0100294 struct device *dev = erproc->dev;
295 struct mbox_client *cl = &erproc->mbox_client;
Per Åstrand2cd53972021-04-12 13:46:04 +0200296
Kristofer Jonssona70bfde2023-01-12 10:00:03 +0100297 INIT_WORK(&erproc->work, ethosu_mbox_bottom);
Per Åstrand2cd53972021-04-12 13:46:04 +0200298
Kristofer Jonssona70bfde2023-01-12 10:00:03 +0100299 erproc->wq = create_singlethread_workqueue("ethosu_rproc_wq");
300 if (!erproc->wq) {
301 dev_err(dev, "Failed to create work queue");
Per Åstrand2cd53972021-04-12 13:46:04 +0200302
303 return -EINVAL;
304 }
305
Kristofer Jonssona70bfde2023-01-12 10:00:03 +0100306 cl->dev = dev;
307 cl->rx_callback = ethosu_mbox_top;
308 cl->tx_prepare = NULL;
309 cl->tx_done = NULL;
310 cl->tx_block = true;
311 cl->knows_txdone = false;
312 cl->tx_tout = 500;
Per Åstrand2cd53972021-04-12 13:46:04 +0200313
Kristofer Jonssona70bfde2023-01-12 10:00:03 +0100314 erproc->ch_rx = mbox_request_channel_byname(cl, "rx");
315 if (IS_ERR(erproc->ch_rx)) {
316 dev_err(dev, "Failed to request mbox chan rx");
Per Åstrand2cd53972021-04-12 13:46:04 +0200317
Kristofer Jonssona70bfde2023-01-12 10:00:03 +0100318 return PTR_ERR(erproc->ch_rx);
Per Åstrand2cd53972021-04-12 13:46:04 +0200319 }
320
Kristofer Jonssona70bfde2023-01-12 10:00:03 +0100321 erproc->ch_tx = mbox_request_channel_byname(cl, "tx");
322 if (IS_ERR(erproc->ch_tx)) {
Ledion Dajaedd25502023-10-17 09:15:32 +0200323 dev_dbg(dev, "Using same channel for RX and TX");
Kristofer Jonssona70bfde2023-01-12 10:00:03 +0100324 erproc->ch_tx = erproc->ch_rx;
325 }
Per Åstrand2cd53972021-04-12 13:46:04 +0200326
Kristofer Jonssona70bfde2023-01-12 10:00:03 +0100327 return 0;
Per Åstrand2cd53972021-04-12 13:46:04 +0200328}
329
330static const struct of_device_id ethosu_rproc_match[] = {
Per Åstrand9f36f2e2021-09-30 09:57:34 +0200331 { .compatible = "arm,ethosu-rproc" },
Per Åstrandb9248a42022-05-18 16:05:16 +0200332 { /* sentinel */ },
Per Åstrand2cd53972021-04-12 13:46:04 +0200333};
334
335static int ethosu_rproc_probe(struct platform_device *pdev)
336{
337 struct device *dev = &pdev->dev;
338 struct device_node *np = dev->of_node;
Kristofer Jonssona70bfde2023-01-12 10:00:03 +0100339 struct ethosu_rproc *erproc;
Per Åstrand2cd53972021-04-12 13:46:04 +0200340 struct rproc *rproc;
Kristofer Jonssona70bfde2023-01-12 10:00:03 +0100341 int ret;
Per Åstrand2cd53972021-04-12 13:46:04 +0200342
Kristofer Jonssona70bfde2023-01-12 10:00:03 +0100343 /* map the first 'memory-region' for DMA-mapping */
344 ret = of_reserved_mem_device_init(dev);
345 if (ret)
346 return ret;
347
348 dma_set_mask_and_coherent(dev, DMA_BIT_MASK(DMA_ADDR_BITS));
349
350 rproc = devm_rproc_alloc(dev, np->name, &ethosu_rproc_ops,
351 fw_filename_param,
352 sizeof(*erproc));
353 if (!rproc)
354 return -ENOMEM;
355
356 platform_set_drvdata(pdev, rproc);
Per Åstrand2cd53972021-04-12 13:46:04 +0200357
358 /* Configure rproc */
359 rproc->has_iommu = false;
360 rproc->auto_boot = auto_boot;
361
Kristofer Jonssona70bfde2023-01-12 10:00:03 +0100362 /* Configure Ethos-U rproc */
363 erproc = rproc->priv;
364 erproc->dev = dev;
Per Åstrand2cd53972021-04-12 13:46:04 +0200365
366 /* Get the reset handler for the subsystem */
Kristofer Jonssona70bfde2023-01-12 10:00:03 +0100367 erproc->rstc = devm_reset_control_get_exclusive_by_index(dev, 0);
368 if (IS_ERR(erproc->rstc)) {
369 dev_err(&pdev->dev, "Failed to get reset controller.");
370
371 return PTR_ERR(erproc->rstc);
Per Åstrand2cd53972021-04-12 13:46:04 +0200372 }
373
Kristofer Jonssona70bfde2023-01-12 10:00:03 +0100374 /* Allocate and initialize mailbox client */
375 ret = ethosu_mailbox_init(erproc);
Per Åstrand2cd53972021-04-12 13:46:04 +0200376 if (ret)
Kristofer Jonssona70bfde2023-01-12 10:00:03 +0100377 return ret;
Per Åstrand2cd53972021-04-12 13:46:04 +0200378
379 ret = rproc_add(rproc);
Kristofer Jonssona70bfde2023-01-12 10:00:03 +0100380 if (ret) {
381 dev_err(dev, "Failed to add rproc");
382 goto free_mbox;
383 }
Per Åstrand2cd53972021-04-12 13:46:04 +0200384
Kristofer Jonssona70bfde2023-01-12 10:00:03 +0100385 return 0;
Per Åstrand2cd53972021-04-12 13:46:04 +0200386
Kristofer Jonssona70bfde2023-01-12 10:00:03 +0100387free_mbox:
388 if (erproc->wq)
389 destroy_workqueue(erproc->wq);
390
391 mbox_free_channel(erproc->ch_rx);
392
393 if (erproc->ch_tx != erproc->ch_rx)
394 mbox_free_channel(erproc->ch_tx);
Per Åstrand2cd53972021-04-12 13:46:04 +0200395
396 return ret;
397}
398
399static int ethosu_rproc_remove(struct platform_device *pdev)
400{
401 struct rproc *rproc = platform_get_drvdata(pdev);
Kristofer Jonssona70bfde2023-01-12 10:00:03 +0100402 struct ethosu_rproc *erproc = rproc->priv;
403
404 if (erproc->wq)
405 destroy_workqueue(erproc->wq);
406
407 if (erproc->ch_tx != erproc->ch_rx)
408 mbox_free_channel(erproc->ch_tx);
409
410 mbox_free_channel(erproc->ch_rx);
Per Åstrand2cd53972021-04-12 13:46:04 +0200411
412 rproc_del(rproc);
Per Åstrand2cd53972021-04-12 13:46:04 +0200413
414 return 0;
415}
416
417static struct platform_driver ethosu_rproc_driver = {
418 .probe = ethosu_rproc_probe,
419 .remove = ethosu_rproc_remove,
420 .driver = {
421 .name = "ethosu-rproc",
422 .of_match_table = of_match_ptr(ethosu_rproc_match),
423 },
424};
425
426module_platform_driver(ethosu_rproc_driver);
427
428MODULE_LICENSE("GPL v2");
429MODULE_AUTHOR("Arm Ltd");
430MODULE_DESCRIPTION("Arm Ethos-U NPU RemoteProc Driver");
431MODULE_VERSION(ETHOSU_RPROC_DRIVER_VERSION);