blob: 344627d152125cc7acc16a0efce169b020bc55fb [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
Mikael Olsson9f176ba2023-11-10 15:15:34 +0100125static 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
146static 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 Jonssona70bfde2023-01-12 10:00:03 +0100157static 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 Jonssona70bfde2023-01-12 10:00:03 +0100164 struct rproc_mem_entry *mem;
165
Mikael Olsson9d0ea8b2023-06-05 16:16:36 +0200166 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 Jonssona70bfde2023-01-12 10:00:03 +0100170 size);
171
172 return -ENOMEM;
173 }
174
Mikael Olsson9f176ba2023-11-10 15:15:34 +0100175 mem = rproc_mem_entry_init(dev, NULL, pa, size, da, ethosu_mem_alloc,
176 ethosu_mem_release, name);
Kristofer Jonssona70bfde2023-01-12 10:00:03 +0100177 if (!mem)
178 return -ENOMEM;
179
Ledion Dajaedd25502023-10-17 09:15:32 +0200180 dev_dbg(dev, "Add carveout mapping. dma=%pad, da=%x, va=%p, len=%zu",
181 &mem->dma, mem->da, mem->va, mem->len);
Kristofer Jonssona70bfde2023-01-12 10:00:03 +0100182
183 rproc_add_carveout(rproc, mem);
184
185 return 0;
186}
187
188static 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 Jonssona70bfde2023-01-12 10:00:03 +0100197 /* Add carveout for each 'reg' device tree entry */
198 for (i = 0; of_address_to_resource(np, i, &res) == 0; i++) {
Ledion Dajaedd25502023-10-17 09:15:32 +0200199 dev_dbg(dev, "Found resource. start=%llx, size=%llx",
200 res.start, resource_size(&res));
Kristofer Jonssona70bfde2023-01-12 10:00:03 +0100201
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 Dajaedd25502023-10-17 09:15:32 +0200219 dev_dbg(dev,
220 "Found memory region. pa=%llx, size=%llu, name=%s",
221 res_mem->base, res_mem->size, it.node->name);
Kristofer Jonssona70bfde2023-01-12 10:00:03 +0100222
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 Åstrand2cd53972021-04-12 13:46:04 +0200231
232static int ethosu_rproc_start(struct rproc *rproc)
233{
Kristofer Jonssona70bfde2023-01-12 10:00:03 +0100234 struct ethosu_rproc *erproc = (struct ethosu_rproc *)rproc->priv;
235 struct device *dev = erproc->dev;
Per Åstrand2cd53972021-04-12 13:46:04 +0200236
Kristofer Jonssona70bfde2023-01-12 10:00:03 +0100237 dev_info(dev, "Starting up Ethos-U subsystem CPU");
Per Åstrand2cd53972021-04-12 13:46:04 +0200238
Kristofer Jonssona70bfde2023-01-12 10:00:03 +0100239 return reset_control_deassert(erproc->rstc);
Per Åstrand2cd53972021-04-12 13:46:04 +0200240}
241
242static int ethosu_rproc_stop(struct rproc *rproc)
243{
Kristofer Jonssona70bfde2023-01-12 10:00:03 +0100244 struct ethosu_rproc *erproc = (struct ethosu_rproc *)rproc->priv;
245 struct device *dev = erproc->dev;
Per Åstrand2cd53972021-04-12 13:46:04 +0200246
Kristofer Jonssona70bfde2023-01-12 10:00:03 +0100247 dev_info(dev, "Stopping Ethos-U subsystem CPU");
Per Åstrand2cd53972021-04-12 13:46:04 +0200248
Kristofer Jonssona70bfde2023-01-12 10:00:03 +0100249 return reset_control_assert(erproc->rstc);
Per Åstrand2cd53972021-04-12 13:46:04 +0200250}
251
252static void ethosu_rproc_kick(struct rproc *rproc,
253 int vqid)
254{
Kristofer Jonssona70bfde2023-01-12 10:00:03 +0100255 struct ethosu_rproc *erproc = (struct ethosu_rproc *)rproc->priv;
Per Åstrand2cd53972021-04-12 13:46:04 +0200256
Kristofer Jonssona70bfde2023-01-12 10:00:03 +0100257 dev_dbg(&rproc->dev, "Kicking Ethos-U remoteproc vqid: %d!", vqid);
Per Åstrand2cd53972021-04-12 13:46:04 +0200258
Kristofer Jonssona70bfde2023-01-12 10:00:03 +0100259 mbox_send_message(erproc->ch_tx, (void *)&vqid);
Per Åstrand2cd53972021-04-12 13:46:04 +0200260}
261
Mikael Olsson9d0ea8b2023-06-05 16:16:36 +0200262static 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 Olsson9d0ea8b2023-06-05 16:16:36 +0200295 struct fw_rsc_map_range *range = &mapping->range[i];
Mikael Olssonf1b23f32023-10-06 13:39:16 +0200296 map = &dev->dma_range_map[i];
Mikael Olsson9d0ea8b2023-06-05 16:16:36 +0200297
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 Åstrand2cd53972021-04-12 13:46:04 +0200308static const struct rproc_ops ethosu_rproc_ops = {
Mikael Olsson9d0ea8b2023-06-05 16:16:36 +0200309 .prepare = &ethosu_rproc_prepare,
310 .start = &ethosu_rproc_start,
311 .stop = &ethosu_rproc_stop,
312 .kick = &ethosu_rproc_kick,
313 .handle_rsc = &ethosu_rproc_handle_rsc,
Per Åstrand2cd53972021-04-12 13:46:04 +0200314};
315
Kristofer Jonssona70bfde2023-01-12 10:00:03 +0100316static int ethosu_mailbox_init(struct ethosu_rproc *erproc)
Per Åstrand2cd53972021-04-12 13:46:04 +0200317{
Kristofer Jonssona70bfde2023-01-12 10:00:03 +0100318 struct device *dev = erproc->dev;
319 struct mbox_client *cl = &erproc->mbox_client;
Per Åstrand2cd53972021-04-12 13:46:04 +0200320
Kristofer Jonssona70bfde2023-01-12 10:00:03 +0100321 INIT_WORK(&erproc->work, ethosu_mbox_bottom);
Per Åstrand2cd53972021-04-12 13:46:04 +0200322
Kristofer Jonssona70bfde2023-01-12 10:00:03 +0100323 erproc->wq = create_singlethread_workqueue("ethosu_rproc_wq");
324 if (!erproc->wq) {
325 dev_err(dev, "Failed to create work queue");
Per Åstrand2cd53972021-04-12 13:46:04 +0200326
327 return -EINVAL;
328 }
329
Kristofer Jonssona70bfde2023-01-12 10:00:03 +0100330 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 Åstrand2cd53972021-04-12 13:46:04 +0200337
Kristofer Jonssona70bfde2023-01-12 10:00:03 +0100338 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 Åstrand2cd53972021-04-12 13:46:04 +0200341
Kristofer Jonssona70bfde2023-01-12 10:00:03 +0100342 return PTR_ERR(erproc->ch_rx);
Per Åstrand2cd53972021-04-12 13:46:04 +0200343 }
344
Kristofer Jonssona70bfde2023-01-12 10:00:03 +0100345 erproc->ch_tx = mbox_request_channel_byname(cl, "tx");
346 if (IS_ERR(erproc->ch_tx)) {
Ledion Dajaedd25502023-10-17 09:15:32 +0200347 dev_dbg(dev, "Using same channel for RX and TX");
Kristofer Jonssona70bfde2023-01-12 10:00:03 +0100348 erproc->ch_tx = erproc->ch_rx;
349 }
Per Åstrand2cd53972021-04-12 13:46:04 +0200350
Kristofer Jonssona70bfde2023-01-12 10:00:03 +0100351 return 0;
Per Åstrand2cd53972021-04-12 13:46:04 +0200352}
353
354static const struct of_device_id ethosu_rproc_match[] = {
Per Åstrand9f36f2e2021-09-30 09:57:34 +0200355 { .compatible = "arm,ethosu-rproc" },
Per Åstrandb9248a42022-05-18 16:05:16 +0200356 { /* sentinel */ },
Per Åstrand2cd53972021-04-12 13:46:04 +0200357};
358
359static int ethosu_rproc_probe(struct platform_device *pdev)
360{
361 struct device *dev = &pdev->dev;
362 struct device_node *np = dev->of_node;
Kristofer Jonssona70bfde2023-01-12 10:00:03 +0100363 struct ethosu_rproc *erproc;
Per Åstrand2cd53972021-04-12 13:46:04 +0200364 struct rproc *rproc;
Kristofer Jonssona70bfde2023-01-12 10:00:03 +0100365 int ret;
Per Åstrand2cd53972021-04-12 13:46:04 +0200366
Kristofer Jonssona70bfde2023-01-12 10:00:03 +0100367 /* 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, &ethosu_rproc_ops,
375 fw_filename_param,
376 sizeof(*erproc));
377 if (!rproc)
378 return -ENOMEM;
379
380 platform_set_drvdata(pdev, rproc);
Per Åstrand2cd53972021-04-12 13:46:04 +0200381
382 /* Configure rproc */
383 rproc->has_iommu = false;
384 rproc->auto_boot = auto_boot;
385
Kristofer Jonssona70bfde2023-01-12 10:00:03 +0100386 /* Configure Ethos-U rproc */
387 erproc = rproc->priv;
388 erproc->dev = dev;
Per Åstrand2cd53972021-04-12 13:46:04 +0200389
390 /* Get the reset handler for the subsystem */
Kristofer Jonssona70bfde2023-01-12 10:00:03 +0100391 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 Åstrand2cd53972021-04-12 13:46:04 +0200396 }
397
Kristofer Jonssona70bfde2023-01-12 10:00:03 +0100398 /* Allocate and initialize mailbox client */
399 ret = ethosu_mailbox_init(erproc);
Per Åstrand2cd53972021-04-12 13:46:04 +0200400 if (ret)
Kristofer Jonssona70bfde2023-01-12 10:00:03 +0100401 return ret;
Per Åstrand2cd53972021-04-12 13:46:04 +0200402
403 ret = rproc_add(rproc);
Kristofer Jonssona70bfde2023-01-12 10:00:03 +0100404 if (ret) {
405 dev_err(dev, "Failed to add rproc");
406 goto free_mbox;
407 }
Per Åstrand2cd53972021-04-12 13:46:04 +0200408
Kristofer Jonssona70bfde2023-01-12 10:00:03 +0100409 return 0;
Per Åstrand2cd53972021-04-12 13:46:04 +0200410
Kristofer Jonssona70bfde2023-01-12 10:00:03 +0100411free_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 Åstrand2cd53972021-04-12 13:46:04 +0200419
420 return ret;
421}
422
423static int ethosu_rproc_remove(struct platform_device *pdev)
424{
425 struct rproc *rproc = platform_get_drvdata(pdev);
Kristofer Jonssona70bfde2023-01-12 10:00:03 +0100426 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 Åstrand2cd53972021-04-12 13:46:04 +0200435
436 rproc_del(rproc);
Per Åstrand2cd53972021-04-12 13:46:04 +0200437
438 return 0;
439}
440
441static 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
450module_platform_driver(ethosu_rproc_driver);
451
452MODULE_LICENSE("GPL v2");
453MODULE_AUTHOR("Arm Ltd");
454MODULE_DESCRIPTION("Arm Ethos-U NPU RemoteProc Driver");
455MODULE_VERSION(ETHOSU_RPROC_DRIVER_VERSION);