]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - drivers/remoteproc/pru_rproc.c
remoteproc: pru: Fixup interrupt-parent logic for fw events
[mirror_ubuntu-jammy-kernel.git] / drivers / remoteproc / pru_rproc.c
CommitLineData
d4ce2de7
SA
1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * PRU-ICSS remoteproc driver for various TI SoCs
4 *
5 * Copyright (C) 2014-2020 Texas Instruments Incorporated - https://www.ti.com/
6 *
7 * Author(s):
8 * Suman Anna <s-anna@ti.com>
9 * Andrew F. Davis <afd@ti.com>
10 * Grzegorz Jaszczyk <grzegorz.jaszczyk@linaro.org> for Texas Instruments
11 */
12
13#include <linux/bitops.h>
20ad1de0 14#include <linux/debugfs.h>
c75c9fda 15#include <linux/irqdomain.h>
d4ce2de7
SA
16#include <linux/module.h>
17#include <linux/of_device.h>
c75c9fda 18#include <linux/of_irq.h>
d4ce2de7
SA
19#include <linux/pruss_driver.h>
20#include <linux/remoteproc.h>
21
22#include "remoteproc_internal.h"
23#include "remoteproc_elf_helpers.h"
c75c9fda 24#include "pru_rproc.h"
d4ce2de7
SA
25
26/* PRU_ICSS_PRU_CTRL registers */
27#define PRU_CTRL_CTRL 0x0000
28#define PRU_CTRL_STS 0x0004
20ad1de0
SA
29#define PRU_CTRL_WAKEUP_EN 0x0008
30#define PRU_CTRL_CYCLE 0x000C
31#define PRU_CTRL_STALL 0x0010
32#define PRU_CTRL_CTBIR0 0x0020
33#define PRU_CTRL_CTBIR1 0x0024
34#define PRU_CTRL_CTPPR0 0x0028
35#define PRU_CTRL_CTPPR1 0x002C
d4ce2de7
SA
36
37/* CTRL register bit-fields */
38#define CTRL_CTRL_SOFT_RST_N BIT(0)
39#define CTRL_CTRL_EN BIT(1)
40#define CTRL_CTRL_SLEEPING BIT(2)
41#define CTRL_CTRL_CTR_EN BIT(3)
42#define CTRL_CTRL_SINGLE_STEP BIT(8)
43#define CTRL_CTRL_RUNSTATE BIT(15)
44
20ad1de0
SA
45/* PRU_ICSS_PRU_DEBUG registers */
46#define PRU_DEBUG_GPREG(x) (0x0000 + (x) * 4)
47#define PRU_DEBUG_CT_REG(x) (0x0080 + (x) * 4)
48
1d39f4d1 49/* PRU/RTU/Tx_PRU Core IRAM address masks */
d4ce2de7
SA
50#define PRU_IRAM_ADDR_MASK 0x3ffff
51#define PRU0_IRAM_ADDR_MASK 0x34000
52#define PRU1_IRAM_ADDR_MASK 0x38000
1d39f4d1
SA
53#define RTU0_IRAM_ADDR_MASK 0x4000
54#define RTU1_IRAM_ADDR_MASK 0x6000
55#define TX_PRU0_IRAM_ADDR_MASK 0xa000
56#define TX_PRU1_IRAM_ADDR_MASK 0xc000
d4ce2de7
SA
57
58/* PRU device addresses for various type of PRU RAMs */
59#define PRU_IRAM_DA 0 /* Instruction RAM */
60#define PRU_PDRAM_DA 0 /* Primary Data RAM */
61#define PRU_SDRAM_DA 0x2000 /* Secondary Data RAM */
62#define PRU_SHRDRAM_DA 0x10000 /* Shared Data RAM */
63
c75c9fda
GJ
64#define MAX_PRU_SYS_EVENTS 160
65
d4ce2de7
SA
66/**
67 * enum pru_iomem - PRU core memory/register range identifiers
68 *
69 * @PRU_IOMEM_IRAM: PRU Instruction RAM range
70 * @PRU_IOMEM_CTRL: PRU Control register range
71 * @PRU_IOMEM_DEBUG: PRU Debug register range
72 * @PRU_IOMEM_MAX: just keep this one at the end
73 */
74enum pru_iomem {
75 PRU_IOMEM_IRAM = 0,
76 PRU_IOMEM_CTRL,
77 PRU_IOMEM_DEBUG,
78 PRU_IOMEM_MAX,
79};
80
1d39f4d1
SA
81/**
82 * enum pru_type - PRU core type identifier
83 *
84 * @PRU_TYPE_PRU: Programmable Real-time Unit
85 * @PRU_TYPE_RTU: Auxiliary Programmable Real-Time Unit
86 * @PRU_TYPE_TX_PRU: Transmit Programmable Real-Time Unit
87 * @PRU_TYPE_MAX: just keep this one at the end
88 */
89enum pru_type {
90 PRU_TYPE_PRU = 0,
91 PRU_TYPE_RTU,
92 PRU_TYPE_TX_PRU,
93 PRU_TYPE_MAX,
94};
95
96/**
97 * struct pru_private_data - device data for a PRU core
98 * @type: type of the PRU core (PRU, RTU, Tx_PRU)
99 * @is_k3: flag used to identify the need for special load handling
100 */
101struct pru_private_data {
102 enum pru_type type;
103 unsigned int is_k3 : 1;
104};
105
d4ce2de7
SA
106/**
107 * struct pru_rproc - PRU remoteproc structure
108 * @id: id of the PRU core within the PRUSS
109 * @dev: PRU core device pointer
110 * @pruss: back-reference to parent PRUSS structure
111 * @rproc: remoteproc pointer for this PRU core
1d39f4d1 112 * @data: PRU core specific data
d4ce2de7
SA
113 * @mem_regions: data for each of the PRU memory regions
114 * @fw_name: name of firmware image used during loading
c75c9fda
GJ
115 * @mapped_irq: virtual interrupt numbers of created fw specific mapping
116 * @pru_interrupt_map: pointer to interrupt mapping description (firmware)
117 * @pru_interrupt_map_sz: pru_interrupt_map size
20ad1de0
SA
118 * @dbg_single_step: debug state variable to set PRU into single step mode
119 * @dbg_continuous: debug state variable to restore PRU execution mode
c75c9fda 120 * @evt_count: number of mapped events
d4ce2de7
SA
121 */
122struct pru_rproc {
123 int id;
124 struct device *dev;
125 struct pruss *pruss;
126 struct rproc *rproc;
1d39f4d1 127 const struct pru_private_data *data;
d4ce2de7
SA
128 struct pruss_mem_region mem_regions[PRU_IOMEM_MAX];
129 const char *fw_name;
c75c9fda
GJ
130 unsigned int *mapped_irq;
131 struct pru_irq_rsc *pru_interrupt_map;
132 size_t pru_interrupt_map_sz;
20ad1de0
SA
133 u32 dbg_single_step;
134 u32 dbg_continuous;
c75c9fda 135 u8 evt_count;
d4ce2de7
SA
136};
137
138static inline u32 pru_control_read_reg(struct pru_rproc *pru, unsigned int reg)
139{
140 return readl_relaxed(pru->mem_regions[PRU_IOMEM_CTRL].va + reg);
141}
142
143static inline
144void pru_control_write_reg(struct pru_rproc *pru, unsigned int reg, u32 val)
145{
146 writel_relaxed(val, pru->mem_regions[PRU_IOMEM_CTRL].va + reg);
147}
148
20ad1de0
SA
149static inline u32 pru_debug_read_reg(struct pru_rproc *pru, unsigned int reg)
150{
151 return readl_relaxed(pru->mem_regions[PRU_IOMEM_DEBUG].va + reg);
152}
153
154static int regs_show(struct seq_file *s, void *data)
155{
156 struct rproc *rproc = s->private;
157 struct pru_rproc *pru = rproc->priv;
158 int i, nregs = 32;
159 u32 pru_sts;
160 int pru_is_running;
161
162 seq_puts(s, "============== Control Registers ==============\n");
163 seq_printf(s, "CTRL := 0x%08x\n",
164 pru_control_read_reg(pru, PRU_CTRL_CTRL));
165 pru_sts = pru_control_read_reg(pru, PRU_CTRL_STS);
166 seq_printf(s, "STS (PC) := 0x%08x (0x%08x)\n", pru_sts, pru_sts << 2);
167 seq_printf(s, "WAKEUP_EN := 0x%08x\n",
168 pru_control_read_reg(pru, PRU_CTRL_WAKEUP_EN));
169 seq_printf(s, "CYCLE := 0x%08x\n",
170 pru_control_read_reg(pru, PRU_CTRL_CYCLE));
171 seq_printf(s, "STALL := 0x%08x\n",
172 pru_control_read_reg(pru, PRU_CTRL_STALL));
173 seq_printf(s, "CTBIR0 := 0x%08x\n",
174 pru_control_read_reg(pru, PRU_CTRL_CTBIR0));
175 seq_printf(s, "CTBIR1 := 0x%08x\n",
176 pru_control_read_reg(pru, PRU_CTRL_CTBIR1));
177 seq_printf(s, "CTPPR0 := 0x%08x\n",
178 pru_control_read_reg(pru, PRU_CTRL_CTPPR0));
179 seq_printf(s, "CTPPR1 := 0x%08x\n",
180 pru_control_read_reg(pru, PRU_CTRL_CTPPR1));
181
182 seq_puts(s, "=============== Debug Registers ===============\n");
183 pru_is_running = pru_control_read_reg(pru, PRU_CTRL_CTRL) &
184 CTRL_CTRL_RUNSTATE;
185 if (pru_is_running) {
186 seq_puts(s, "PRU is executing, cannot print/access debug registers.\n");
187 return 0;
188 }
189
190 for (i = 0; i < nregs; i++) {
191 seq_printf(s, "GPREG%-2d := 0x%08x\tCT_REG%-2d := 0x%08x\n",
192 i, pru_debug_read_reg(pru, PRU_DEBUG_GPREG(i)),
193 i, pru_debug_read_reg(pru, PRU_DEBUG_CT_REG(i)));
194 }
195
196 return 0;
197}
198DEFINE_SHOW_ATTRIBUTE(regs);
199
200/*
201 * Control PRU single-step mode
202 *
203 * This is a debug helper function used for controlling the single-step
204 * mode of the PRU. The PRU Debug registers are not accessible when the
205 * PRU is in RUNNING state.
206 *
207 * Writing a non-zero value sets the PRU into single-step mode irrespective
208 * of its previous state. The PRU mode is saved only on the first set into
209 * a single-step mode. Writing a zero value will restore the PRU into its
210 * original mode.
211 */
212static int pru_rproc_debug_ss_set(void *data, u64 val)
213{
214 struct rproc *rproc = data;
215 struct pru_rproc *pru = rproc->priv;
216 u32 reg_val;
217
218 val = val ? 1 : 0;
219 if (!val && !pru->dbg_single_step)
220 return 0;
221
222 reg_val = pru_control_read_reg(pru, PRU_CTRL_CTRL);
223
224 if (val && !pru->dbg_single_step)
225 pru->dbg_continuous = reg_val;
226
227 if (val)
228 reg_val |= CTRL_CTRL_SINGLE_STEP | CTRL_CTRL_EN;
229 else
230 reg_val = pru->dbg_continuous;
231
232 pru->dbg_single_step = val;
233 pru_control_write_reg(pru, PRU_CTRL_CTRL, reg_val);
234
235 return 0;
236}
237
238static int pru_rproc_debug_ss_get(void *data, u64 *val)
239{
240 struct rproc *rproc = data;
241 struct pru_rproc *pru = rproc->priv;
242
243 *val = pru->dbg_single_step;
244
245 return 0;
246}
780a980e
YL
247DEFINE_DEBUGFS_ATTRIBUTE(pru_rproc_debug_ss_fops, pru_rproc_debug_ss_get,
248 pru_rproc_debug_ss_set, "%llu\n");
20ad1de0
SA
249
250/*
251 * Create PRU-specific debugfs entries
252 *
253 * The entries are created only if the parent remoteproc debugfs directory
254 * exists, and will be cleaned up by the remoteproc core.
255 */
256static void pru_rproc_create_debug_entries(struct rproc *rproc)
257{
258 if (!rproc->dbg_dir)
259 return;
260
261 debugfs_create_file("regs", 0400, rproc->dbg_dir,
262 rproc, &regs_fops);
263 debugfs_create_file("single_step", 0600, rproc->dbg_dir,
264 rproc, &pru_rproc_debug_ss_fops);
265}
266
c75c9fda
GJ
267static void pru_dispose_irq_mapping(struct pru_rproc *pru)
268{
269 while (pru->evt_count--) {
270 if (pru->mapped_irq[pru->evt_count] > 0)
271 irq_dispose_mapping(pru->mapped_irq[pru->evt_count]);
272 }
273
274 kfree(pru->mapped_irq);
275}
276
277/*
278 * Parse the custom PRU interrupt map resource and configure the INTC
279 * appropriately.
280 */
281static int pru_handle_intrmap(struct rproc *rproc)
282{
283 struct device *dev = rproc->dev.parent;
284 struct pru_rproc *pru = rproc->priv;
285 struct pru_irq_rsc *rsc = pru->pru_interrupt_map;
286 struct irq_fwspec fwspec;
6d1f2803 287 struct device_node *parent, *irq_parent;
c75c9fda
GJ
288 int i, ret = 0;
289
290 /* not having pru_interrupt_map is not an error */
291 if (!rsc)
292 return 0;
293
294 /* currently supporting only type 0 */
295 if (rsc->type != 0) {
296 dev_err(dev, "unsupported rsc type: %d\n", rsc->type);
297 return -EINVAL;
298 }
299
300 if (rsc->num_evts > MAX_PRU_SYS_EVENTS)
301 return -EINVAL;
302
303 if (sizeof(*rsc) + rsc->num_evts * sizeof(struct pruss_int_map) !=
304 pru->pru_interrupt_map_sz)
305 return -EINVAL;
306
307 pru->evt_count = rsc->num_evts;
308 pru->mapped_irq = kcalloc(pru->evt_count, sizeof(unsigned int),
309 GFP_KERNEL);
310 if (!pru->mapped_irq)
311 return -ENOMEM;
312
313 /*
314 * parse and fill in system event to interrupt channel and
6d1f2803
SA
315 * channel-to-host mapping. The interrupt controller to be used
316 * for these mappings for a given PRU remoteproc is always its
317 * corresponding sibling PRUSS INTC node.
c75c9fda 318 */
6d1f2803
SA
319 parent = of_get_parent(dev_of_node(pru->dev));
320 if (!parent)
321 return -ENODEV;
322
323 irq_parent = of_get_child_by_name(parent, "interrupt-controller");
324 of_node_put(parent);
c75c9fda
GJ
325 if (!irq_parent) {
326 kfree(pru->mapped_irq);
327 return -ENODEV;
328 }
329
330 fwspec.fwnode = of_node_to_fwnode(irq_parent);
331 fwspec.param_count = 3;
332 for (i = 0; i < pru->evt_count; i++) {
333 fwspec.param[0] = rsc->pru_intc_map[i].event;
334 fwspec.param[1] = rsc->pru_intc_map[i].chnl;
335 fwspec.param[2] = rsc->pru_intc_map[i].host;
336
337 dev_dbg(dev, "mapping%d: event %d, chnl %d, host %d\n",
338 i, fwspec.param[0], fwspec.param[1], fwspec.param[2]);
339
340 pru->mapped_irq[i] = irq_create_fwspec_mapping(&fwspec);
341 if (!pru->mapped_irq[i]) {
342 dev_err(dev, "failed to get virq\n");
343 ret = pru->mapped_irq[i];
344 goto map_fail;
345 }
346 }
6d1f2803 347 of_node_put(irq_parent);
c75c9fda
GJ
348
349 return ret;
350
351map_fail:
352 pru_dispose_irq_mapping(pru);
6d1f2803 353 of_node_put(irq_parent);
c75c9fda
GJ
354
355 return ret;
356}
357
d4ce2de7
SA
358static int pru_rproc_start(struct rproc *rproc)
359{
360 struct device *dev = &rproc->dev;
361 struct pru_rproc *pru = rproc->priv;
1d39f4d1 362 const char *names[PRU_TYPE_MAX] = { "PRU", "RTU", "Tx_PRU" };
d4ce2de7 363 u32 val;
c75c9fda 364 int ret;
d4ce2de7 365
1d39f4d1
SA
366 dev_dbg(dev, "starting %s%d: entry-point = 0x%llx\n",
367 names[pru->data->type], pru->id, (rproc->bootaddr >> 2));
d4ce2de7 368
c75c9fda
GJ
369 ret = pru_handle_intrmap(rproc);
370 /*
371 * reset references to pru interrupt map - they will stop being valid
372 * after rproc_start returns
373 */
374 pru->pru_interrupt_map = NULL;
375 pru->pru_interrupt_map_sz = 0;
376 if (ret)
377 return ret;
378
d4ce2de7
SA
379 val = CTRL_CTRL_EN | ((rproc->bootaddr >> 2) << 16);
380 pru_control_write_reg(pru, PRU_CTRL_CTRL, val);
381
382 return 0;
383}
384
385static int pru_rproc_stop(struct rproc *rproc)
386{
387 struct device *dev = &rproc->dev;
388 struct pru_rproc *pru = rproc->priv;
1d39f4d1 389 const char *names[PRU_TYPE_MAX] = { "PRU", "RTU", "Tx_PRU" };
d4ce2de7
SA
390 u32 val;
391
1d39f4d1 392 dev_dbg(dev, "stopping %s%d\n", names[pru->data->type], pru->id);
d4ce2de7
SA
393
394 val = pru_control_read_reg(pru, PRU_CTRL_CTRL);
395 val &= ~CTRL_CTRL_EN;
396 pru_control_write_reg(pru, PRU_CTRL_CTRL, val);
397
c75c9fda
GJ
398 /* dispose irq mapping - new firmware can provide new mapping */
399 if (pru->mapped_irq)
400 pru_dispose_irq_mapping(pru);
401
d4ce2de7
SA
402 return 0;
403}
404
405/*
406 * Convert PRU device address (data spaces only) to kernel virtual address.
407 *
408 * Each PRU has access to all data memories within the PRUSS, accessible at
409 * different ranges. So, look through both its primary and secondary Data
410 * RAMs as well as any shared Data RAM to convert a PRU device address to
411 * kernel virtual address. Data RAM0 is primary Data RAM for PRU0 and Data
412 * RAM1 is primary Data RAM for PRU1.
413 */
414static void *pru_d_da_to_va(struct pru_rproc *pru, u32 da, size_t len)
415{
416 struct pruss_mem_region dram0, dram1, shrd_ram;
417 struct pruss *pruss = pru->pruss;
418 u32 offset;
419 void *va = NULL;
420
421 if (len == 0)
422 return NULL;
423
424 dram0 = pruss->mem_regions[PRUSS_MEM_DRAM0];
425 dram1 = pruss->mem_regions[PRUSS_MEM_DRAM1];
426 /* PRU1 has its local RAM addresses reversed */
427 if (pru->id == 1)
428 swap(dram0, dram1);
429 shrd_ram = pruss->mem_regions[PRUSS_MEM_SHRD_RAM2];
430
431 if (da >= PRU_PDRAM_DA && da + len <= PRU_PDRAM_DA + dram0.size) {
432 offset = da - PRU_PDRAM_DA;
433 va = (__force void *)(dram0.va + offset);
434 } else if (da >= PRU_SDRAM_DA &&
435 da + len <= PRU_SDRAM_DA + dram1.size) {
436 offset = da - PRU_SDRAM_DA;
437 va = (__force void *)(dram1.va + offset);
438 } else if (da >= PRU_SHRDRAM_DA &&
439 da + len <= PRU_SHRDRAM_DA + shrd_ram.size) {
440 offset = da - PRU_SHRDRAM_DA;
441 va = (__force void *)(shrd_ram.va + offset);
442 }
443
444 return va;
445}
446
447/*
448 * Convert PRU device address (instruction space) to kernel virtual address.
449 *
450 * A PRU does not have an unified address space. Each PRU has its very own
451 * private Instruction RAM, and its device address is identical to that of
452 * its primary Data RAM device address.
453 */
454static void *pru_i_da_to_va(struct pru_rproc *pru, u32 da, size_t len)
455{
456 u32 offset;
457 void *va = NULL;
458
459 if (len == 0)
460 return NULL;
461
462 if (da >= PRU_IRAM_DA &&
463 da + len <= PRU_IRAM_DA + pru->mem_regions[PRU_IOMEM_IRAM].size) {
464 offset = da - PRU_IRAM_DA;
465 va = (__force void *)(pru->mem_regions[PRU_IOMEM_IRAM].va +
466 offset);
467 }
468
469 return va;
470}
471
472/*
473 * Provide address translations for only PRU Data RAMs through the remoteproc
474 * core for any PRU client drivers. The PRU Instruction RAM access is restricted
475 * only to the PRU loader code.
476 */
40df0a91 477static void *pru_rproc_da_to_va(struct rproc *rproc, u64 da, size_t len, bool *is_iomem)
d4ce2de7
SA
478{
479 struct pru_rproc *pru = rproc->priv;
480
481 return pru_d_da_to_va(pru, da, len);
482}
483
484/* PRU-specific address translator used by PRU loader. */
485static void *pru_da_to_va(struct rproc *rproc, u64 da, size_t len, bool is_iram)
486{
487 struct pru_rproc *pru = rproc->priv;
488 void *va;
489
490 if (is_iram)
491 va = pru_i_da_to_va(pru, da, len);
492 else
493 va = pru_d_da_to_va(pru, da, len);
494
495 return va;
496}
497
498static struct rproc_ops pru_rproc_ops = {
499 .start = pru_rproc_start,
500 .stop = pru_rproc_stop,
501 .da_to_va = pru_rproc_da_to_va,
502};
503
1d39f4d1
SA
504/*
505 * Custom memory copy implementation for ICSSG PRU/RTU/Tx_PRU Cores
506 *
507 * The ICSSG PRU/RTU/Tx_PRU cores have a memory copying issue with IRAM
508 * memories, that is not seen on previous generation SoCs. The data is reflected
509 * properly in the IRAM memories only for integer (4-byte) copies. Any unaligned
510 * copies result in all the other pre-existing bytes zeroed out within that
511 * 4-byte boundary, thereby resulting in wrong text/code in the IRAMs. Also, the
512 * IRAM memory port interface does not allow any 8-byte copies (as commonly used
513 * by ARM64 memcpy implementation) and throws an exception. The DRAM memory
514 * ports do not show this behavior.
515 */
516static int pru_rproc_memcpy(void *dest, const void *src, size_t count)
517{
518 const u32 *s = src;
519 u32 *d = dest;
520 size_t size = count / 4;
521 u32 *tmp_src = NULL;
522
523 /*
524 * TODO: relax limitation of 4-byte aligned dest addresses and copy
525 * sizes
526 */
527 if ((long)dest % 4 || count % 4)
528 return -EINVAL;
529
530 /* src offsets in ELF firmware image can be non-aligned */
531 if ((long)src % 4) {
532 tmp_src = kmemdup(src, count, GFP_KERNEL);
533 if (!tmp_src)
534 return -ENOMEM;
535 s = tmp_src;
536 }
537
538 while (size--)
539 *d++ = *s++;
540
541 kfree(tmp_src);
542
543 return 0;
544}
545
d4ce2de7
SA
546static int
547pru_rproc_load_elf_segments(struct rproc *rproc, const struct firmware *fw)
548{
1d39f4d1 549 struct pru_rproc *pru = rproc->priv;
d4ce2de7
SA
550 struct device *dev = &rproc->dev;
551 struct elf32_hdr *ehdr;
552 struct elf32_phdr *phdr;
553 int i, ret = 0;
554 const u8 *elf_data = fw->data;
555
556 ehdr = (struct elf32_hdr *)elf_data;
557 phdr = (struct elf32_phdr *)(elf_data + ehdr->e_phoff);
558
559 /* go through the available ELF segments */
560 for (i = 0; i < ehdr->e_phnum; i++, phdr++) {
561 u32 da = phdr->p_paddr;
562 u32 memsz = phdr->p_memsz;
563 u32 filesz = phdr->p_filesz;
564 u32 offset = phdr->p_offset;
565 bool is_iram;
566 void *ptr;
567
568 if (phdr->p_type != PT_LOAD || !filesz)
569 continue;
570
571 dev_dbg(dev, "phdr: type %d da 0x%x memsz 0x%x filesz 0x%x\n",
572 phdr->p_type, da, memsz, filesz);
573
574 if (filesz > memsz) {
575 dev_err(dev, "bad phdr filesz 0x%x memsz 0x%x\n",
576 filesz, memsz);
577 ret = -EINVAL;
578 break;
579 }
580
581 if (offset + filesz > fw->size) {
582 dev_err(dev, "truncated fw: need 0x%x avail 0x%zx\n",
583 offset + filesz, fw->size);
584 ret = -EINVAL;
585 break;
586 }
587
588 /* grab the kernel address for this device address */
589 is_iram = phdr->p_flags & PF_X;
590 ptr = pru_da_to_va(rproc, da, memsz, is_iram);
591 if (!ptr) {
592 dev_err(dev, "bad phdr da 0x%x mem 0x%x\n", da, memsz);
593 ret = -EINVAL;
594 break;
595 }
596
1d39f4d1
SA
597 if (pru->data->is_k3 && is_iram) {
598 ret = pru_rproc_memcpy(ptr, elf_data + phdr->p_offset,
599 filesz);
600 if (ret) {
601 dev_err(dev, "PRU memory copy failed for da 0x%x memsz 0x%x\n",
602 da, memsz);
603 break;
604 }
605 } else {
606 memcpy(ptr, elf_data + phdr->p_offset, filesz);
607 }
d4ce2de7
SA
608
609 /* skip the memzero logic performed by remoteproc ELF loader */
610 }
611
612 return ret;
613}
614
c75c9fda
GJ
615static const void *
616pru_rproc_find_interrupt_map(struct device *dev, const struct firmware *fw)
617{
618 struct elf32_shdr *shdr, *name_table_shdr;
619 const char *name_table;
620 const u8 *elf_data = fw->data;
621 struct elf32_hdr *ehdr = (struct elf32_hdr *)elf_data;
622 u16 shnum = ehdr->e_shnum;
623 u16 shstrndx = ehdr->e_shstrndx;
624 int i;
625
626 /* first, get the section header */
627 shdr = (struct elf32_shdr *)(elf_data + ehdr->e_shoff);
628 /* compute name table section header entry in shdr array */
629 name_table_shdr = shdr + shstrndx;
630 /* finally, compute the name table section address in elf */
631 name_table = elf_data + name_table_shdr->sh_offset;
632
633 for (i = 0; i < shnum; i++, shdr++) {
634 u32 size = shdr->sh_size;
635 u32 offset = shdr->sh_offset;
636 u32 name = shdr->sh_name;
637
638 if (strcmp(name_table + name, ".pru_irq_map"))
639 continue;
640
641 /* make sure we have the entire irq map */
642 if (offset + size > fw->size || offset + size < size) {
643 dev_err(dev, ".pru_irq_map section truncated\n");
644 return ERR_PTR(-EINVAL);
645 }
646
647 /* make sure irq map has at least the header */
648 if (sizeof(struct pru_irq_rsc) > size) {
649 dev_err(dev, "header-less .pru_irq_map section\n");
650 return ERR_PTR(-EINVAL);
651 }
652
653 return shdr;
654 }
655
656 dev_dbg(dev, "no .pru_irq_map section found for this fw\n");
657
658 return NULL;
659}
660
d4ce2de7
SA
661/*
662 * Use a custom parse_fw callback function for dealing with PRU firmware
663 * specific sections.
c75c9fda
GJ
664 *
665 * The firmware blob can contain optional ELF sections: .resource_table section
666 * and .pru_irq_map one. The second one contains the PRUSS interrupt mapping
667 * description, which needs to be setup before powering on the PRU core. To
668 * avoid RAM wastage this ELF section is not mapped to any ELF segment (by the
669 * firmware linker) and therefore is not loaded to PRU memory.
d4ce2de7
SA
670 */
671static int pru_rproc_parse_fw(struct rproc *rproc, const struct firmware *fw)
672{
c75c9fda
GJ
673 struct device *dev = &rproc->dev;
674 struct pru_rproc *pru = rproc->priv;
675 const u8 *elf_data = fw->data;
676 const void *shdr;
677 u8 class = fw_elf_get_class(fw);
678 u64 sh_offset;
d4ce2de7
SA
679 int ret;
680
681 /* load optional rsc table */
682 ret = rproc_elf_load_rsc_table(rproc, fw);
683 if (ret == -EINVAL)
684 dev_dbg(&rproc->dev, "no resource table found for this fw\n");
685 else if (ret)
686 return ret;
687
c75c9fda
GJ
688 /* find .pru_interrupt_map section, not having it is not an error */
689 shdr = pru_rproc_find_interrupt_map(dev, fw);
690 if (IS_ERR(shdr))
691 return PTR_ERR(shdr);
692
693 if (!shdr)
694 return 0;
695
696 /* preserve pointer to PRU interrupt map together with it size */
697 sh_offset = elf_shdr_get_sh_offset(class, shdr);
698 pru->pru_interrupt_map = (struct pru_irq_rsc *)(elf_data + sh_offset);
699 pru->pru_interrupt_map_sz = elf_shdr_get_sh_size(class, shdr);
700
d4ce2de7
SA
701 return 0;
702}
703
704/*
705 * Compute PRU id based on the IRAM addresses. The PRU IRAMs are
706 * always at a particular offset within the PRUSS address space.
707 */
708static int pru_rproc_set_id(struct pru_rproc *pru)
709{
710 int ret = 0;
711
712 switch (pru->mem_regions[PRU_IOMEM_IRAM].pa & PRU_IRAM_ADDR_MASK) {
1d39f4d1
SA
713 case TX_PRU0_IRAM_ADDR_MASK:
714 fallthrough;
715 case RTU0_IRAM_ADDR_MASK:
716 fallthrough;
d4ce2de7
SA
717 case PRU0_IRAM_ADDR_MASK:
718 pru->id = 0;
719 break;
1d39f4d1
SA
720 case TX_PRU1_IRAM_ADDR_MASK:
721 fallthrough;
722 case RTU1_IRAM_ADDR_MASK:
723 fallthrough;
d4ce2de7
SA
724 case PRU1_IRAM_ADDR_MASK:
725 pru->id = 1;
726 break;
727 default:
728 ret = -EINVAL;
729 }
730
731 return ret;
732}
733
734static int pru_rproc_probe(struct platform_device *pdev)
735{
736 struct device *dev = &pdev->dev;
737 struct device_node *np = dev->of_node;
738 struct platform_device *ppdev = to_platform_device(dev->parent);
739 struct pru_rproc *pru;
740 const char *fw_name;
741 struct rproc *rproc = NULL;
742 struct resource *res;
743 int i, ret;
1d39f4d1 744 const struct pru_private_data *data;
d4ce2de7
SA
745 const char *mem_names[PRU_IOMEM_MAX] = { "iram", "control", "debug" };
746
1d39f4d1
SA
747 data = of_device_get_match_data(&pdev->dev);
748 if (!data)
749 return -ENODEV;
750
d4ce2de7
SA
751 ret = of_property_read_string(np, "firmware-name", &fw_name);
752 if (ret) {
753 dev_err(dev, "unable to retrieve firmware-name %d\n", ret);
754 return ret;
755 }
756
757 rproc = devm_rproc_alloc(dev, pdev->name, &pru_rproc_ops, fw_name,
758 sizeof(*pru));
759 if (!rproc) {
760 dev_err(dev, "rproc_alloc failed\n");
761 return -ENOMEM;
762 }
763 /* use a custom load function to deal with PRU-specific quirks */
764 rproc->ops->load = pru_rproc_load_elf_segments;
765
766 /* use a custom parse function to deal with PRU-specific resources */
767 rproc->ops->parse_fw = pru_rproc_parse_fw;
768
769 /* error recovery is not supported for PRUs */
770 rproc->recovery_disabled = true;
771
772 /*
773 * rproc_add will auto-boot the processor normally, but this is not
774 * desired with PRU client driven boot-flow methodology. A PRU
775 * application/client driver will boot the corresponding PRU
776 * remote-processor as part of its state machine either through the
777 * remoteproc sysfs interface or through the equivalent kernel API.
778 */
779 rproc->auto_boot = false;
780
781 pru = rproc->priv;
782 pru->dev = dev;
1d39f4d1 783 pru->data = data;
d4ce2de7
SA
784 pru->pruss = platform_get_drvdata(ppdev);
785 pru->rproc = rproc;
786 pru->fw_name = fw_name;
787
788 for (i = 0; i < ARRAY_SIZE(mem_names); i++) {
789 res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
790 mem_names[i]);
791 pru->mem_regions[i].va = devm_ioremap_resource(dev, res);
792 if (IS_ERR(pru->mem_regions[i].va)) {
793 dev_err(dev, "failed to parse and map memory resource %d %s\n",
794 i, mem_names[i]);
795 ret = PTR_ERR(pru->mem_regions[i].va);
796 return ret;
797 }
798 pru->mem_regions[i].pa = res->start;
799 pru->mem_regions[i].size = resource_size(res);
800
801 dev_dbg(dev, "memory %8s: pa %pa size 0x%zx va %pK\n",
802 mem_names[i], &pru->mem_regions[i].pa,
803 pru->mem_regions[i].size, pru->mem_regions[i].va);
804 }
805
806 ret = pru_rproc_set_id(pru);
807 if (ret < 0)
808 return ret;
809
810 platform_set_drvdata(pdev, rproc);
811
812 ret = devm_rproc_add(dev, pru->rproc);
813 if (ret) {
814 dev_err(dev, "rproc_add failed: %d\n", ret);
815 return ret;
816 }
817
20ad1de0
SA
818 pru_rproc_create_debug_entries(rproc);
819
d4ce2de7
SA
820 dev_dbg(dev, "PRU rproc node %pOF probed successfully\n", np);
821
822 return 0;
823}
824
825static int pru_rproc_remove(struct platform_device *pdev)
826{
827 struct device *dev = &pdev->dev;
828 struct rproc *rproc = platform_get_drvdata(pdev);
829
830 dev_dbg(dev, "%s: removing rproc %s\n", __func__, rproc->name);
831
832 return 0;
833}
834
1d39f4d1
SA
835static const struct pru_private_data pru_data = {
836 .type = PRU_TYPE_PRU,
837};
838
839static const struct pru_private_data k3_pru_data = {
840 .type = PRU_TYPE_PRU,
841 .is_k3 = 1,
842};
843
844static const struct pru_private_data k3_rtu_data = {
845 .type = PRU_TYPE_RTU,
846 .is_k3 = 1,
847};
848
849static const struct pru_private_data k3_tx_pru_data = {
850 .type = PRU_TYPE_TX_PRU,
851 .is_k3 = 1,
852};
853
d4ce2de7 854static const struct of_device_id pru_rproc_match[] = {
1d39f4d1
SA
855 { .compatible = "ti,am3356-pru", .data = &pru_data },
856 { .compatible = "ti,am4376-pru", .data = &pru_data },
857 { .compatible = "ti,am5728-pru", .data = &pru_data },
858 { .compatible = "ti,k2g-pru", .data = &pru_data },
859 { .compatible = "ti,am654-pru", .data = &k3_pru_data },
860 { .compatible = "ti,am654-rtu", .data = &k3_rtu_data },
861 { .compatible = "ti,am654-tx-pru", .data = &k3_tx_pru_data },
b44786c9
SA
862 { .compatible = "ti,j721e-pru", .data = &k3_pru_data },
863 { .compatible = "ti,j721e-rtu", .data = &k3_rtu_data },
864 { .compatible = "ti,j721e-tx-pru", .data = &k3_tx_pru_data },
d4ce2de7
SA
865 {},
866};
867MODULE_DEVICE_TABLE(of, pru_rproc_match);
868
869static struct platform_driver pru_rproc_driver = {
870 .driver = {
871 .name = "pru-rproc",
872 .of_match_table = pru_rproc_match,
873 .suppress_bind_attrs = true,
874 },
875 .probe = pru_rproc_probe,
876 .remove = pru_rproc_remove,
877};
878module_platform_driver(pru_rproc_driver);
879
880MODULE_AUTHOR("Suman Anna <s-anna@ti.com>");
881MODULE_AUTHOR("Andrew F. Davis <afd@ti.com>");
882MODULE_AUTHOR("Grzegorz Jaszczyk <grzegorz.jaszczyk@linaro.org>");
883MODULE_DESCRIPTION("PRU-ICSS Remote Processor Driver");
884MODULE_LICENSE("GPL v2");