1 // SPDX-License-Identifier: GPL-2.0-only
3 * PRU-ICSS remoteproc driver for various TI SoCs
5 * Copyright (C) 2014-2022 Texas Instruments Incorporated - https://www.ti.com/
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 * Puranjay Mohan <p-mohan@ti.com>
12 * Md Danish Anwar <danishanwar@ti.com>
15 #include <linux/bitops.h>
16 #include <linux/debugfs.h>
17 #include <linux/irqdomain.h>
18 #include <linux/module.h>
19 #include <linux/of_device.h>
20 #include <linux/of_irq.h>
21 #include <linux/remoteproc/pruss.h>
22 #include <linux/pruss_driver.h>
23 #include <linux/remoteproc.h>
25 #include "remoteproc_internal.h"
26 #include "remoteproc_elf_helpers.h"
27 #include "pru_rproc.h"
29 /* PRU_ICSS_PRU_CTRL registers */
30 #define PRU_CTRL_CTRL 0x0000
31 #define PRU_CTRL_STS 0x0004
32 #define PRU_CTRL_WAKEUP_EN 0x0008
33 #define PRU_CTRL_CYCLE 0x000C
34 #define PRU_CTRL_STALL 0x0010
35 #define PRU_CTRL_CTBIR0 0x0020
36 #define PRU_CTRL_CTBIR1 0x0024
37 #define PRU_CTRL_CTPPR0 0x0028
38 #define PRU_CTRL_CTPPR1 0x002C
40 /* CTRL register bit-fields */
41 #define CTRL_CTRL_SOFT_RST_N BIT(0)
42 #define CTRL_CTRL_EN BIT(1)
43 #define CTRL_CTRL_SLEEPING BIT(2)
44 #define CTRL_CTRL_CTR_EN BIT(3)
45 #define CTRL_CTRL_SINGLE_STEP BIT(8)
46 #define CTRL_CTRL_RUNSTATE BIT(15)
48 /* PRU_ICSS_PRU_DEBUG registers */
49 #define PRU_DEBUG_GPREG(x) (0x0000 + (x) * 4)
50 #define PRU_DEBUG_CT_REG(x) (0x0080 + (x) * 4)
52 /* PRU/RTU/Tx_PRU Core IRAM address masks */
53 #define PRU_IRAM_ADDR_MASK 0x3ffff
54 #define PRU0_IRAM_ADDR_MASK 0x34000
55 #define PRU1_IRAM_ADDR_MASK 0x38000
56 #define RTU0_IRAM_ADDR_MASK 0x4000
57 #define RTU1_IRAM_ADDR_MASK 0x6000
58 #define TX_PRU0_IRAM_ADDR_MASK 0xa000
59 #define TX_PRU1_IRAM_ADDR_MASK 0xc000
61 /* PRU device addresses for various type of PRU RAMs */
62 #define PRU_IRAM_DA 0 /* Instruction RAM */
63 #define PRU_PDRAM_DA 0 /* Primary Data RAM */
64 #define PRU_SDRAM_DA 0x2000 /* Secondary Data RAM */
65 #define PRU_SHRDRAM_DA 0x10000 /* Shared Data RAM */
67 #define MAX_PRU_SYS_EVENTS 160
70 * enum pru_iomem - PRU core memory/register range identifiers
72 * @PRU_IOMEM_IRAM: PRU Instruction RAM range
73 * @PRU_IOMEM_CTRL: PRU Control register range
74 * @PRU_IOMEM_DEBUG: PRU Debug register range
75 * @PRU_IOMEM_MAX: just keep this one at the end
85 * struct pru_private_data - device data for a PRU core
86 * @type: type of the PRU core (PRU, RTU, Tx_PRU)
87 * @is_k3: flag used to identify the need for special load handling
89 struct pru_private_data {
91 unsigned int is_k3 : 1;
95 * struct pru_rproc - PRU remoteproc structure
96 * @id: id of the PRU core within the PRUSS
97 * @dev: PRU core device pointer
98 * @pruss: back-reference to parent PRUSS structure
99 * @rproc: remoteproc pointer for this PRU core
100 * @data: PRU core specific data
101 * @mem_regions: data for each of the PRU memory regions
102 * @client_np: client device node
103 * @lock: mutex to protect client usage
104 * @fw_name: name of firmware image used during loading
105 * @mapped_irq: virtual interrupt numbers of created fw specific mapping
106 * @pru_interrupt_map: pointer to interrupt mapping description (firmware)
107 * @pru_interrupt_map_sz: pru_interrupt_map size
108 * @rmw_lock: lock for read, modify, write operations on registers
109 * @dbg_single_step: debug state variable to set PRU into single step mode
110 * @dbg_continuous: debug state variable to restore PRU execution mode
111 * @evt_count: number of mapped events
118 const struct pru_private_data *data;
119 struct pruss_mem_region mem_regions[PRU_IOMEM_MAX];
120 struct device_node *client_np;
123 unsigned int *mapped_irq;
124 struct pru_irq_rsc *pru_interrupt_map;
125 size_t pru_interrupt_map_sz;
132 static inline u32 pru_control_read_reg(struct pru_rproc *pru, unsigned int reg)
134 return readl_relaxed(pru->mem_regions[PRU_IOMEM_CTRL].va + reg);
138 void pru_control_write_reg(struct pru_rproc *pru, unsigned int reg, u32 val)
140 writel_relaxed(val, pru->mem_regions[PRU_IOMEM_CTRL].va + reg);
144 void pru_control_set_reg(struct pru_rproc *pru, unsigned int reg,
150 spin_lock_irqsave(&pru->rmw_lock, flags);
152 val = pru_control_read_reg(pru, reg);
155 pru_control_write_reg(pru, reg, val);
157 spin_unlock_irqrestore(&pru->rmw_lock, flags);
161 * pru_rproc_set_firmware() - set firmware for a PRU core
162 * @rproc: the rproc instance of the PRU
163 * @fw_name: the new firmware name, or NULL if default is desired
165 * Return: 0 on success, or errno in error case.
167 static int pru_rproc_set_firmware(struct rproc *rproc, const char *fw_name)
169 struct pru_rproc *pru = rproc->priv;
172 fw_name = pru->fw_name;
174 return rproc_set_firmware(rproc, fw_name);
177 static struct rproc *__pru_rproc_get(struct device_node *np, int index)
180 phandle rproc_phandle;
183 ret = of_property_read_u32_index(np, "ti,prus", index, &rproc_phandle);
187 rproc = rproc_get_by_phandle(rproc_phandle);
193 /* make sure it is PRU rproc */
194 if (!is_pru_rproc(rproc->dev.parent)) {
196 return ERR_PTR(-ENODEV);
203 * pru_rproc_get() - get the PRU rproc instance from a device node
204 * @np: the user/client device node
205 * @index: index to use for the ti,prus property
206 * @pru_id: optional pointer to return the PRU remoteproc processor id
208 * This function looks through a client device node's "ti,prus" property at
209 * index @index and returns the rproc handle for a valid PRU remote processor if
210 * found. The function allows only one user to own the PRU rproc resource at a
211 * time. Caller must call pru_rproc_put() when done with using the rproc, not
212 * required if the function returns a failure.
214 * When optional @pru_id pointer is passed the PRU remoteproc processor id is
217 * Return: rproc handle on success, and an ERR_PTR on failure using one
218 * of the following error values
219 * -ENODEV if device is not found
220 * -EBUSY if PRU is already acquired by anyone
221 * -EPROBE_DEFER is PRU device is not probed yet
223 struct rproc *pru_rproc_get(struct device_node *np, int index,
224 enum pruss_pru_id *pru_id)
227 struct pru_rproc *pru;
232 rproc = __pru_rproc_get(np, index);
239 mutex_lock(&pru->lock);
241 if (pru->client_np) {
242 mutex_unlock(&pru->lock);
244 goto err_no_rproc_handle;
248 rproc->sysfs_read_only = true;
250 mutex_unlock(&pru->lock);
255 ret = of_property_read_string_index(np, "firmware-name", index,
258 ret = pru_rproc_set_firmware(rproc, fw_name);
260 dev_err(dev, "failed to set firmware: %d\n", ret);
272 pru_rproc_put(rproc);
275 EXPORT_SYMBOL_GPL(pru_rproc_get);
278 * pru_rproc_put() - release the PRU rproc resource
279 * @rproc: the rproc resource to release
281 * Releases the PRU rproc resource and makes it available to other
284 void pru_rproc_put(struct rproc *rproc)
286 struct pru_rproc *pru;
288 if (IS_ERR_OR_NULL(rproc) || !is_pru_rproc(rproc->dev.parent))
293 pru_rproc_set_firmware(rproc, NULL);
295 mutex_lock(&pru->lock);
297 if (!pru->client_np) {
298 mutex_unlock(&pru->lock);
302 pru->client_np = NULL;
303 rproc->sysfs_read_only = false;
304 mutex_unlock(&pru->lock);
308 EXPORT_SYMBOL_GPL(pru_rproc_put);
311 * pru_rproc_set_ctable() - set the constant table index for the PRU
312 * @rproc: the rproc instance of the PRU
313 * @c: constant table index to set
314 * @addr: physical address to set it to
316 * Return: 0 on success, or errno in error case.
318 int pru_rproc_set_ctable(struct rproc *rproc, enum pru_ctable_idx c, u32 addr)
320 struct pru_rproc *pru = rproc->priv;
326 if (IS_ERR_OR_NULL(rproc))
329 if (!rproc->dev.parent || !is_pru_rproc(rproc->dev.parent))
332 /* pointer is 16 bit and index is 8-bit so mask out the rest */
333 idx_mask = (c >= PRU_C28) ? 0xFFFF : 0xFF;
335 /* ctable uses bit 8 and upwards only */
336 idx = (addr >> 8) & idx_mask;
338 /* configurable ctable (i.e. C24) starts at PRU_CTRL_CTBIR0 */
339 reg = PRU_CTRL_CTBIR0 + 4 * (c >> 1);
340 mask = idx_mask << (16 * (c & 1));
341 set = idx << (16 * (c & 1));
343 pru_control_set_reg(pru, reg, mask, set);
347 EXPORT_SYMBOL_GPL(pru_rproc_set_ctable);
349 static inline u32 pru_debug_read_reg(struct pru_rproc *pru, unsigned int reg)
351 return readl_relaxed(pru->mem_regions[PRU_IOMEM_DEBUG].va + reg);
354 static int regs_show(struct seq_file *s, void *data)
356 struct rproc *rproc = s->private;
357 struct pru_rproc *pru = rproc->priv;
362 seq_puts(s, "============== Control Registers ==============\n");
363 seq_printf(s, "CTRL := 0x%08x\n",
364 pru_control_read_reg(pru, PRU_CTRL_CTRL));
365 pru_sts = pru_control_read_reg(pru, PRU_CTRL_STS);
366 seq_printf(s, "STS (PC) := 0x%08x (0x%08x)\n", pru_sts, pru_sts << 2);
367 seq_printf(s, "WAKEUP_EN := 0x%08x\n",
368 pru_control_read_reg(pru, PRU_CTRL_WAKEUP_EN));
369 seq_printf(s, "CYCLE := 0x%08x\n",
370 pru_control_read_reg(pru, PRU_CTRL_CYCLE));
371 seq_printf(s, "STALL := 0x%08x\n",
372 pru_control_read_reg(pru, PRU_CTRL_STALL));
373 seq_printf(s, "CTBIR0 := 0x%08x\n",
374 pru_control_read_reg(pru, PRU_CTRL_CTBIR0));
375 seq_printf(s, "CTBIR1 := 0x%08x\n",
376 pru_control_read_reg(pru, PRU_CTRL_CTBIR1));
377 seq_printf(s, "CTPPR0 := 0x%08x\n",
378 pru_control_read_reg(pru, PRU_CTRL_CTPPR0));
379 seq_printf(s, "CTPPR1 := 0x%08x\n",
380 pru_control_read_reg(pru, PRU_CTRL_CTPPR1));
382 seq_puts(s, "=============== Debug Registers ===============\n");
383 pru_is_running = pru_control_read_reg(pru, PRU_CTRL_CTRL) &
385 if (pru_is_running) {
386 seq_puts(s, "PRU is executing, cannot print/access debug registers.\n");
390 for (i = 0; i < nregs; i++) {
391 seq_printf(s, "GPREG%-2d := 0x%08x\tCT_REG%-2d := 0x%08x\n",
392 i, pru_debug_read_reg(pru, PRU_DEBUG_GPREG(i)),
393 i, pru_debug_read_reg(pru, PRU_DEBUG_CT_REG(i)));
398 DEFINE_SHOW_ATTRIBUTE(regs);
401 * Control PRU single-step mode
403 * This is a debug helper function used for controlling the single-step
404 * mode of the PRU. The PRU Debug registers are not accessible when the
405 * PRU is in RUNNING state.
407 * Writing a non-zero value sets the PRU into single-step mode irrespective
408 * of its previous state. The PRU mode is saved only on the first set into
409 * a single-step mode. Writing a zero value will restore the PRU into its
412 static int pru_rproc_debug_ss_set(void *data, u64 val)
414 struct rproc *rproc = data;
415 struct pru_rproc *pru = rproc->priv;
419 if (!val && !pru->dbg_single_step)
422 reg_val = pru_control_read_reg(pru, PRU_CTRL_CTRL);
424 if (val && !pru->dbg_single_step)
425 pru->dbg_continuous = reg_val;
428 reg_val |= CTRL_CTRL_SINGLE_STEP | CTRL_CTRL_EN;
430 reg_val = pru->dbg_continuous;
432 pru->dbg_single_step = val;
433 pru_control_write_reg(pru, PRU_CTRL_CTRL, reg_val);
438 static int pru_rproc_debug_ss_get(void *data, u64 *val)
440 struct rproc *rproc = data;
441 struct pru_rproc *pru = rproc->priv;
443 *val = pru->dbg_single_step;
447 DEFINE_DEBUGFS_ATTRIBUTE(pru_rproc_debug_ss_fops, pru_rproc_debug_ss_get,
448 pru_rproc_debug_ss_set, "%llu\n");
451 * Create PRU-specific debugfs entries
453 * The entries are created only if the parent remoteproc debugfs directory
454 * exists, and will be cleaned up by the remoteproc core.
456 static void pru_rproc_create_debug_entries(struct rproc *rproc)
461 debugfs_create_file("regs", 0400, rproc->dbg_dir,
463 debugfs_create_file("single_step", 0600, rproc->dbg_dir,
464 rproc, &pru_rproc_debug_ss_fops);
467 static void pru_dispose_irq_mapping(struct pru_rproc *pru)
469 if (!pru->mapped_irq)
472 while (pru->evt_count) {
474 if (pru->mapped_irq[pru->evt_count] > 0)
475 irq_dispose_mapping(pru->mapped_irq[pru->evt_count]);
478 kfree(pru->mapped_irq);
479 pru->mapped_irq = NULL;
483 * Parse the custom PRU interrupt map resource and configure the INTC
486 static int pru_handle_intrmap(struct rproc *rproc)
488 struct device *dev = rproc->dev.parent;
489 struct pru_rproc *pru = rproc->priv;
490 struct pru_irq_rsc *rsc = pru->pru_interrupt_map;
491 struct irq_fwspec fwspec;
492 struct device_node *parent, *irq_parent;
495 /* not having pru_interrupt_map is not an error */
499 /* currently supporting only type 0 */
500 if (rsc->type != 0) {
501 dev_err(dev, "unsupported rsc type: %d\n", rsc->type);
505 if (rsc->num_evts > MAX_PRU_SYS_EVENTS)
508 if (sizeof(*rsc) + rsc->num_evts * sizeof(struct pruss_int_map) !=
509 pru->pru_interrupt_map_sz)
512 pru->evt_count = rsc->num_evts;
513 pru->mapped_irq = kcalloc(pru->evt_count, sizeof(unsigned int),
515 if (!pru->mapped_irq) {
521 * parse and fill in system event to interrupt channel and
522 * channel-to-host mapping. The interrupt controller to be used
523 * for these mappings for a given PRU remoteproc is always its
524 * corresponding sibling PRUSS INTC node.
526 parent = of_get_parent(dev_of_node(pru->dev));
528 kfree(pru->mapped_irq);
529 pru->mapped_irq = NULL;
534 irq_parent = of_get_child_by_name(parent, "interrupt-controller");
537 kfree(pru->mapped_irq);
538 pru->mapped_irq = NULL;
543 fwspec.fwnode = of_node_to_fwnode(irq_parent);
544 fwspec.param_count = 3;
545 for (i = 0; i < pru->evt_count; i++) {
546 fwspec.param[0] = rsc->pru_intc_map[i].event;
547 fwspec.param[1] = rsc->pru_intc_map[i].chnl;
548 fwspec.param[2] = rsc->pru_intc_map[i].host;
550 dev_dbg(dev, "mapping%d: event %d, chnl %d, host %d\n",
551 i, fwspec.param[0], fwspec.param[1], fwspec.param[2]);
553 pru->mapped_irq[i] = irq_create_fwspec_mapping(&fwspec);
554 if (!pru->mapped_irq[i]) {
555 dev_err(dev, "failed to get virq for fw mapping %d: event %d chnl %d host %d\n",
556 i, fwspec.param[0], fwspec.param[1],
562 of_node_put(irq_parent);
567 pru_dispose_irq_mapping(pru);
568 of_node_put(irq_parent);
573 static int pru_rproc_start(struct rproc *rproc)
575 struct device *dev = &rproc->dev;
576 struct pru_rproc *pru = rproc->priv;
577 const char *names[PRU_TYPE_MAX] = { "PRU", "RTU", "Tx_PRU" };
581 dev_dbg(dev, "starting %s%d: entry-point = 0x%llx\n",
582 names[pru->data->type], pru->id, (rproc->bootaddr >> 2));
584 ret = pru_handle_intrmap(rproc);
586 * reset references to pru interrupt map - they will stop being valid
587 * after rproc_start returns
589 pru->pru_interrupt_map = NULL;
590 pru->pru_interrupt_map_sz = 0;
594 val = CTRL_CTRL_EN | ((rproc->bootaddr >> 2) << 16);
595 pru_control_write_reg(pru, PRU_CTRL_CTRL, val);
600 static int pru_rproc_stop(struct rproc *rproc)
602 struct device *dev = &rproc->dev;
603 struct pru_rproc *pru = rproc->priv;
604 const char *names[PRU_TYPE_MAX] = { "PRU", "RTU", "Tx_PRU" };
607 dev_dbg(dev, "stopping %s%d\n", names[pru->data->type], pru->id);
609 val = pru_control_read_reg(pru, PRU_CTRL_CTRL);
610 val &= ~CTRL_CTRL_EN;
611 pru_control_write_reg(pru, PRU_CTRL_CTRL, val);
613 /* dispose irq mapping - new firmware can provide new mapping */
614 pru_dispose_irq_mapping(pru);
620 * Convert PRU device address (data spaces only) to kernel virtual address.
622 * Each PRU has access to all data memories within the PRUSS, accessible at
623 * different ranges. So, look through both its primary and secondary Data
624 * RAMs as well as any shared Data RAM to convert a PRU device address to
625 * kernel virtual address. Data RAM0 is primary Data RAM for PRU0 and Data
626 * RAM1 is primary Data RAM for PRU1.
628 static void *pru_d_da_to_va(struct pru_rproc *pru, u32 da, size_t len)
630 struct pruss_mem_region dram0, dram1, shrd_ram;
631 struct pruss *pruss = pru->pruss;
638 dram0 = pruss->mem_regions[PRUSS_MEM_DRAM0];
639 dram1 = pruss->mem_regions[PRUSS_MEM_DRAM1];
640 /* PRU1 has its local RAM addresses reversed */
641 if (pru->id == PRUSS_PRU1)
643 shrd_ram = pruss->mem_regions[PRUSS_MEM_SHRD_RAM2];
645 if (da + len <= PRU_PDRAM_DA + dram0.size) {
646 offset = da - PRU_PDRAM_DA;
647 va = (__force void *)(dram0.va + offset);
648 } else if (da >= PRU_SDRAM_DA &&
649 da + len <= PRU_SDRAM_DA + dram1.size) {
650 offset = da - PRU_SDRAM_DA;
651 va = (__force void *)(dram1.va + offset);
652 } else if (da >= PRU_SHRDRAM_DA &&
653 da + len <= PRU_SHRDRAM_DA + shrd_ram.size) {
654 offset = da - PRU_SHRDRAM_DA;
655 va = (__force void *)(shrd_ram.va + offset);
662 * Convert PRU device address (instruction space) to kernel virtual address.
664 * A PRU does not have an unified address space. Each PRU has its very own
665 * private Instruction RAM, and its device address is identical to that of
666 * its primary Data RAM device address.
668 static void *pru_i_da_to_va(struct pru_rproc *pru, u32 da, size_t len)
677 * GNU binutils do not support multiple address spaces. The GNU
678 * linker's default linker script places IRAM at an arbitrary high
679 * offset, in order to differentiate it from DRAM. Hence we need to
680 * strip the artificial offset in the IRAM addresses coming from the
683 * The TI proprietary linker would never set those higher IRAM address
684 * bits anyway. PRU architecture limits the program counter to 16-bit
685 * word-address range. This in turn corresponds to 18-bit IRAM
686 * byte-address range for ELF.
688 * Two more bits are added just in case to make the final 20-bit mask.
689 * Idea is to have a safeguard in case TI decides to add banking
694 if (da + len <= PRU_IRAM_DA + pru->mem_regions[PRU_IOMEM_IRAM].size) {
695 offset = da - PRU_IRAM_DA;
696 va = (__force void *)(pru->mem_regions[PRU_IOMEM_IRAM].va +
704 * Provide address translations for only PRU Data RAMs through the remoteproc
705 * core for any PRU client drivers. The PRU Instruction RAM access is restricted
706 * only to the PRU loader code.
708 static void *pru_rproc_da_to_va(struct rproc *rproc, u64 da, size_t len, bool *is_iomem)
710 struct pru_rproc *pru = rproc->priv;
712 return pru_d_da_to_va(pru, da, len);
715 /* PRU-specific address translator used by PRU loader. */
716 static void *pru_da_to_va(struct rproc *rproc, u64 da, size_t len, bool is_iram)
718 struct pru_rproc *pru = rproc->priv;
722 va = pru_i_da_to_va(pru, da, len);
724 va = pru_d_da_to_va(pru, da, len);
729 static struct rproc_ops pru_rproc_ops = {
730 .start = pru_rproc_start,
731 .stop = pru_rproc_stop,
732 .da_to_va = pru_rproc_da_to_va,
736 * Custom memory copy implementation for ICSSG PRU/RTU/Tx_PRU Cores
738 * The ICSSG PRU/RTU/Tx_PRU cores have a memory copying issue with IRAM
739 * memories, that is not seen on previous generation SoCs. The data is reflected
740 * properly in the IRAM memories only for integer (4-byte) copies. Any unaligned
741 * copies result in all the other pre-existing bytes zeroed out within that
742 * 4-byte boundary, thereby resulting in wrong text/code in the IRAMs. Also, the
743 * IRAM memory port interface does not allow any 8-byte copies (as commonly used
744 * by ARM64 memcpy implementation) and throws an exception. The DRAM memory
745 * ports do not show this behavior.
747 static int pru_rproc_memcpy(void *dest, const void *src, size_t count)
751 size_t size = count / 4;
755 * TODO: relax limitation of 4-byte aligned dest addresses and copy
758 if ((long)dest % 4 || count % 4)
761 /* src offsets in ELF firmware image can be non-aligned */
763 tmp_src = kmemdup(src, count, GFP_KERNEL);
778 pru_rproc_load_elf_segments(struct rproc *rproc, const struct firmware *fw)
780 struct pru_rproc *pru = rproc->priv;
781 struct device *dev = &rproc->dev;
782 struct elf32_hdr *ehdr;
783 struct elf32_phdr *phdr;
785 const u8 *elf_data = fw->data;
787 ehdr = (struct elf32_hdr *)elf_data;
788 phdr = (struct elf32_phdr *)(elf_data + ehdr->e_phoff);
790 /* go through the available ELF segments */
791 for (i = 0; i < ehdr->e_phnum; i++, phdr++) {
792 u32 da = phdr->p_paddr;
793 u32 memsz = phdr->p_memsz;
794 u32 filesz = phdr->p_filesz;
795 u32 offset = phdr->p_offset;
799 if (phdr->p_type != PT_LOAD || !filesz)
802 dev_dbg(dev, "phdr: type %d da 0x%x memsz 0x%x filesz 0x%x\n",
803 phdr->p_type, da, memsz, filesz);
805 if (filesz > memsz) {
806 dev_err(dev, "bad phdr filesz 0x%x memsz 0x%x\n",
812 if (offset + filesz > fw->size) {
813 dev_err(dev, "truncated fw: need 0x%x avail 0x%zx\n",
814 offset + filesz, fw->size);
819 /* grab the kernel address for this device address */
820 is_iram = phdr->p_flags & PF_X;
821 ptr = pru_da_to_va(rproc, da, memsz, is_iram);
823 dev_err(dev, "bad phdr da 0x%x mem 0x%x\n", da, memsz);
828 if (pru->data->is_k3) {
829 ret = pru_rproc_memcpy(ptr, elf_data + phdr->p_offset,
832 dev_err(dev, "PRU memory copy failed for da 0x%x memsz 0x%x\n",
837 memcpy(ptr, elf_data + phdr->p_offset, filesz);
840 /* skip the memzero logic performed by remoteproc ELF loader */
847 pru_rproc_find_interrupt_map(struct device *dev, const struct firmware *fw)
849 struct elf32_shdr *shdr, *name_table_shdr;
850 const char *name_table;
851 const u8 *elf_data = fw->data;
852 struct elf32_hdr *ehdr = (struct elf32_hdr *)elf_data;
853 u16 shnum = ehdr->e_shnum;
854 u16 shstrndx = ehdr->e_shstrndx;
857 /* first, get the section header */
858 shdr = (struct elf32_shdr *)(elf_data + ehdr->e_shoff);
859 /* compute name table section header entry in shdr array */
860 name_table_shdr = shdr + shstrndx;
861 /* finally, compute the name table section address in elf */
862 name_table = elf_data + name_table_shdr->sh_offset;
864 for (i = 0; i < shnum; i++, shdr++) {
865 u32 size = shdr->sh_size;
866 u32 offset = shdr->sh_offset;
867 u32 name = shdr->sh_name;
869 if (strcmp(name_table + name, ".pru_irq_map"))
872 /* make sure we have the entire irq map */
873 if (offset + size > fw->size || offset + size < size) {
874 dev_err(dev, ".pru_irq_map section truncated\n");
875 return ERR_PTR(-EINVAL);
878 /* make sure irq map has at least the header */
879 if (sizeof(struct pru_irq_rsc) > size) {
880 dev_err(dev, "header-less .pru_irq_map section\n");
881 return ERR_PTR(-EINVAL);
887 dev_dbg(dev, "no .pru_irq_map section found for this fw\n");
893 * Use a custom parse_fw callback function for dealing with PRU firmware
896 * The firmware blob can contain optional ELF sections: .resource_table section
897 * and .pru_irq_map one. The second one contains the PRUSS interrupt mapping
898 * description, which needs to be setup before powering on the PRU core. To
899 * avoid RAM wastage this ELF section is not mapped to any ELF segment (by the
900 * firmware linker) and therefore is not loaded to PRU memory.
902 static int pru_rproc_parse_fw(struct rproc *rproc, const struct firmware *fw)
904 struct device *dev = &rproc->dev;
905 struct pru_rproc *pru = rproc->priv;
906 const u8 *elf_data = fw->data;
908 u8 class = fw_elf_get_class(fw);
912 /* load optional rsc table */
913 ret = rproc_elf_load_rsc_table(rproc, fw);
915 dev_dbg(&rproc->dev, "no resource table found for this fw\n");
919 /* find .pru_interrupt_map section, not having it is not an error */
920 shdr = pru_rproc_find_interrupt_map(dev, fw);
922 return PTR_ERR(shdr);
927 /* preserve pointer to PRU interrupt map together with it size */
928 sh_offset = elf_shdr_get_sh_offset(class, shdr);
929 pru->pru_interrupt_map = (struct pru_irq_rsc *)(elf_data + sh_offset);
930 pru->pru_interrupt_map_sz = elf_shdr_get_sh_size(class, shdr);
936 * Compute PRU id based on the IRAM addresses. The PRU IRAMs are
937 * always at a particular offset within the PRUSS address space.
939 static int pru_rproc_set_id(struct pru_rproc *pru)
943 switch (pru->mem_regions[PRU_IOMEM_IRAM].pa & PRU_IRAM_ADDR_MASK) {
944 case TX_PRU0_IRAM_ADDR_MASK:
946 case RTU0_IRAM_ADDR_MASK:
948 case PRU0_IRAM_ADDR_MASK:
949 pru->id = PRUSS_PRU0;
951 case TX_PRU1_IRAM_ADDR_MASK:
953 case RTU1_IRAM_ADDR_MASK:
955 case PRU1_IRAM_ADDR_MASK:
956 pru->id = PRUSS_PRU1;
965 static int pru_rproc_probe(struct platform_device *pdev)
967 struct device *dev = &pdev->dev;
968 struct device_node *np = dev->of_node;
969 struct platform_device *ppdev = to_platform_device(dev->parent);
970 struct pru_rproc *pru;
972 struct rproc *rproc = NULL;
973 struct resource *res;
975 const struct pru_private_data *data;
976 const char *mem_names[PRU_IOMEM_MAX] = { "iram", "control", "debug" };
978 data = of_device_get_match_data(&pdev->dev);
982 ret = of_property_read_string(np, "firmware-name", &fw_name);
984 dev_err(dev, "unable to retrieve firmware-name %d\n", ret);
988 rproc = devm_rproc_alloc(dev, pdev->name, &pru_rproc_ops, fw_name,
991 dev_err(dev, "rproc_alloc failed\n");
994 /* use a custom load function to deal with PRU-specific quirks */
995 rproc->ops->load = pru_rproc_load_elf_segments;
997 /* use a custom parse function to deal with PRU-specific resources */
998 rproc->ops->parse_fw = pru_rproc_parse_fw;
1000 /* error recovery is not supported for PRUs */
1001 rproc->recovery_disabled = true;
1004 * rproc_add will auto-boot the processor normally, but this is not
1005 * desired with PRU client driven boot-flow methodology. A PRU
1006 * application/client driver will boot the corresponding PRU
1007 * remote-processor as part of its state machine either through the
1008 * remoteproc sysfs interface or through the equivalent kernel API.
1010 rproc->auto_boot = false;
1015 pru->pruss = platform_get_drvdata(ppdev);
1017 pru->fw_name = fw_name;
1018 pru->client_np = NULL;
1019 spin_lock_init(&pru->rmw_lock);
1020 mutex_init(&pru->lock);
1022 for (i = 0; i < ARRAY_SIZE(mem_names); i++) {
1023 res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
1025 pru->mem_regions[i].va = devm_ioremap_resource(dev, res);
1026 if (IS_ERR(pru->mem_regions[i].va)) {
1027 dev_err(dev, "failed to parse and map memory resource %d %s\n",
1029 ret = PTR_ERR(pru->mem_regions[i].va);
1032 pru->mem_regions[i].pa = res->start;
1033 pru->mem_regions[i].size = resource_size(res);
1035 dev_dbg(dev, "memory %8s: pa %pa size 0x%zx va %pK\n",
1036 mem_names[i], &pru->mem_regions[i].pa,
1037 pru->mem_regions[i].size, pru->mem_regions[i].va);
1040 ret = pru_rproc_set_id(pru);
1044 platform_set_drvdata(pdev, rproc);
1046 ret = devm_rproc_add(dev, pru->rproc);
1048 dev_err(dev, "rproc_add failed: %d\n", ret);
1052 pru_rproc_create_debug_entries(rproc);
1054 dev_dbg(dev, "PRU rproc node %pOF probed successfully\n", np);
1059 static void pru_rproc_remove(struct platform_device *pdev)
1061 struct device *dev = &pdev->dev;
1062 struct rproc *rproc = platform_get_drvdata(pdev);
1064 dev_dbg(dev, "%s: removing rproc %s\n", __func__, rproc->name);
1067 static const struct pru_private_data pru_data = {
1068 .type = PRU_TYPE_PRU,
1071 static const struct pru_private_data k3_pru_data = {
1072 .type = PRU_TYPE_PRU,
1076 static const struct pru_private_data k3_rtu_data = {
1077 .type = PRU_TYPE_RTU,
1081 static const struct pru_private_data k3_tx_pru_data = {
1082 .type = PRU_TYPE_TX_PRU,
1086 static const struct of_device_id pru_rproc_match[] = {
1087 { .compatible = "ti,am3356-pru", .data = &pru_data },
1088 { .compatible = "ti,am4376-pru", .data = &pru_data },
1089 { .compatible = "ti,am5728-pru", .data = &pru_data },
1090 { .compatible = "ti,am642-pru", .data = &k3_pru_data },
1091 { .compatible = "ti,am642-rtu", .data = &k3_rtu_data },
1092 { .compatible = "ti,am642-tx-pru", .data = &k3_tx_pru_data },
1093 { .compatible = "ti,k2g-pru", .data = &pru_data },
1094 { .compatible = "ti,am654-pru", .data = &k3_pru_data },
1095 { .compatible = "ti,am654-rtu", .data = &k3_rtu_data },
1096 { .compatible = "ti,am654-tx-pru", .data = &k3_tx_pru_data },
1097 { .compatible = "ti,j721e-pru", .data = &k3_pru_data },
1098 { .compatible = "ti,j721e-rtu", .data = &k3_rtu_data },
1099 { .compatible = "ti,j721e-tx-pru", .data = &k3_tx_pru_data },
1100 { .compatible = "ti,am625-pru", .data = &k3_pru_data },
1103 MODULE_DEVICE_TABLE(of, pru_rproc_match);
1105 static struct platform_driver pru_rproc_driver = {
1107 .name = PRU_RPROC_DRVNAME,
1108 .of_match_table = pru_rproc_match,
1109 .suppress_bind_attrs = true,
1111 .probe = pru_rproc_probe,
1112 .remove_new = pru_rproc_remove,
1114 module_platform_driver(pru_rproc_driver);
1116 MODULE_AUTHOR("Suman Anna <s-anna@ti.com>");
1117 MODULE_AUTHOR("Andrew F. Davis <afd@ti.com>");
1118 MODULE_AUTHOR("Grzegorz Jaszczyk <grzegorz.jaszczyk@linaro.org>");
1119 MODULE_AUTHOR("Puranjay Mohan <p-mohan@ti.com>");
1120 MODULE_AUTHOR("Md Danish Anwar <danishanwar@ti.com>");
1121 MODULE_DESCRIPTION("PRU-ICSS Remote Processor Driver");
1122 MODULE_LICENSE("GPL v2");