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 * enum pru_type - PRU core type identifier
87 * @PRU_TYPE_PRU: Programmable Real-time Unit
88 * @PRU_TYPE_RTU: Auxiliary Programmable Real-Time Unit
89 * @PRU_TYPE_TX_PRU: Transmit Programmable Real-Time Unit
90 * @PRU_TYPE_MAX: just keep this one at the end
100 * struct pru_private_data - device data for a PRU core
101 * @type: type of the PRU core (PRU, RTU, Tx_PRU)
102 * @is_k3: flag used to identify the need for special load handling
104 struct pru_private_data {
106 unsigned int is_k3 : 1;
110 * struct pru_rproc - PRU remoteproc structure
111 * @id: id of the PRU core within the PRUSS
112 * @dev: PRU core device pointer
113 * @pruss: back-reference to parent PRUSS structure
114 * @rproc: remoteproc pointer for this PRU core
115 * @data: PRU core specific data
116 * @mem_regions: data for each of the PRU memory regions
117 * @client_np: client device node
118 * @lock: mutex to protect client usage
119 * @fw_name: name of firmware image used during loading
120 * @mapped_irq: virtual interrupt numbers of created fw specific mapping
121 * @pru_interrupt_map: pointer to interrupt mapping description (firmware)
122 * @pru_interrupt_map_sz: pru_interrupt_map size
123 * @rmw_lock: lock for read, modify, write operations on registers
124 * @dbg_single_step: debug state variable to set PRU into single step mode
125 * @dbg_continuous: debug state variable to restore PRU execution mode
126 * @evt_count: number of mapped events
133 const struct pru_private_data *data;
134 struct pruss_mem_region mem_regions[PRU_IOMEM_MAX];
135 struct device_node *client_np;
138 unsigned int *mapped_irq;
139 struct pru_irq_rsc *pru_interrupt_map;
140 size_t pru_interrupt_map_sz;
147 static inline u32 pru_control_read_reg(struct pru_rproc *pru, unsigned int reg)
149 return readl_relaxed(pru->mem_regions[PRU_IOMEM_CTRL].va + reg);
153 void pru_control_write_reg(struct pru_rproc *pru, unsigned int reg, u32 val)
155 writel_relaxed(val, pru->mem_regions[PRU_IOMEM_CTRL].va + reg);
159 void pru_control_set_reg(struct pru_rproc *pru, unsigned int reg,
165 spin_lock_irqsave(&pru->rmw_lock, flags);
167 val = pru_control_read_reg(pru, reg);
170 pru_control_write_reg(pru, reg, val);
172 spin_unlock_irqrestore(&pru->rmw_lock, flags);
176 * pru_rproc_set_firmware() - set firmware for a PRU core
177 * @rproc: the rproc instance of the PRU
178 * @fw_name: the new firmware name, or NULL if default is desired
180 * Return: 0 on success, or errno in error case.
182 static int pru_rproc_set_firmware(struct rproc *rproc, const char *fw_name)
184 struct pru_rproc *pru = rproc->priv;
187 fw_name = pru->fw_name;
189 return rproc_set_firmware(rproc, fw_name);
192 static struct rproc *__pru_rproc_get(struct device_node *np, int index)
195 phandle rproc_phandle;
198 ret = of_property_read_u32_index(np, "ti,prus", index, &rproc_phandle);
202 rproc = rproc_get_by_phandle(rproc_phandle);
208 /* make sure it is PRU rproc */
209 if (!is_pru_rproc(rproc->dev.parent)) {
211 return ERR_PTR(-ENODEV);
218 * pru_rproc_get() - get the PRU rproc instance from a device node
219 * @np: the user/client device node
220 * @index: index to use for the ti,prus property
221 * @pru_id: optional pointer to return the PRU remoteproc processor id
223 * This function looks through a client device node's "ti,prus" property at
224 * index @index and returns the rproc handle for a valid PRU remote processor if
225 * found. The function allows only one user to own the PRU rproc resource at a
226 * time. Caller must call pru_rproc_put() when done with using the rproc, not
227 * required if the function returns a failure.
229 * When optional @pru_id pointer is passed the PRU remoteproc processor id is
232 * Return: rproc handle on success, and an ERR_PTR on failure using one
233 * of the following error values
234 * -ENODEV if device is not found
235 * -EBUSY if PRU is already acquired by anyone
236 * -EPROBE_DEFER is PRU device is not probed yet
238 struct rproc *pru_rproc_get(struct device_node *np, int index,
239 enum pruss_pru_id *pru_id)
242 struct pru_rproc *pru;
247 rproc = __pru_rproc_get(np, index);
254 mutex_lock(&pru->lock);
256 if (pru->client_np) {
257 mutex_unlock(&pru->lock);
259 goto err_no_rproc_handle;
263 rproc->sysfs_read_only = true;
265 mutex_unlock(&pru->lock);
270 ret = of_property_read_string_index(np, "firmware-name", index,
273 ret = pru_rproc_set_firmware(rproc, fw_name);
275 dev_err(dev, "failed to set firmware: %d\n", ret);
287 pru_rproc_put(rproc);
290 EXPORT_SYMBOL_GPL(pru_rproc_get);
293 * pru_rproc_put() - release the PRU rproc resource
294 * @rproc: the rproc resource to release
296 * Releases the PRU rproc resource and makes it available to other
299 void pru_rproc_put(struct rproc *rproc)
301 struct pru_rproc *pru;
303 if (IS_ERR_OR_NULL(rproc) || !is_pru_rproc(rproc->dev.parent))
308 pru_rproc_set_firmware(rproc, NULL);
310 mutex_lock(&pru->lock);
312 if (!pru->client_np) {
313 mutex_unlock(&pru->lock);
317 pru->client_np = NULL;
318 rproc->sysfs_read_only = false;
319 mutex_unlock(&pru->lock);
323 EXPORT_SYMBOL_GPL(pru_rproc_put);
326 * pru_rproc_set_ctable() - set the constant table index for the PRU
327 * @rproc: the rproc instance of the PRU
328 * @c: constant table index to set
329 * @addr: physical address to set it to
331 * Return: 0 on success, or errno in error case.
333 int pru_rproc_set_ctable(struct rproc *rproc, enum pru_ctable_idx c, u32 addr)
335 struct pru_rproc *pru = rproc->priv;
341 if (IS_ERR_OR_NULL(rproc))
344 if (!rproc->dev.parent || !is_pru_rproc(rproc->dev.parent))
347 /* pointer is 16 bit and index is 8-bit so mask out the rest */
348 idx_mask = (c >= PRU_C28) ? 0xFFFF : 0xFF;
350 /* ctable uses bit 8 and upwards only */
351 idx = (addr >> 8) & idx_mask;
353 /* configurable ctable (i.e. C24) starts at PRU_CTRL_CTBIR0 */
354 reg = PRU_CTRL_CTBIR0 + 4 * (c >> 1);
355 mask = idx_mask << (16 * (c & 1));
356 set = idx << (16 * (c & 1));
358 pru_control_set_reg(pru, reg, mask, set);
362 EXPORT_SYMBOL_GPL(pru_rproc_set_ctable);
364 static inline u32 pru_debug_read_reg(struct pru_rproc *pru, unsigned int reg)
366 return readl_relaxed(pru->mem_regions[PRU_IOMEM_DEBUG].va + reg);
369 static int regs_show(struct seq_file *s, void *data)
371 struct rproc *rproc = s->private;
372 struct pru_rproc *pru = rproc->priv;
377 seq_puts(s, "============== Control Registers ==============\n");
378 seq_printf(s, "CTRL := 0x%08x\n",
379 pru_control_read_reg(pru, PRU_CTRL_CTRL));
380 pru_sts = pru_control_read_reg(pru, PRU_CTRL_STS);
381 seq_printf(s, "STS (PC) := 0x%08x (0x%08x)\n", pru_sts, pru_sts << 2);
382 seq_printf(s, "WAKEUP_EN := 0x%08x\n",
383 pru_control_read_reg(pru, PRU_CTRL_WAKEUP_EN));
384 seq_printf(s, "CYCLE := 0x%08x\n",
385 pru_control_read_reg(pru, PRU_CTRL_CYCLE));
386 seq_printf(s, "STALL := 0x%08x\n",
387 pru_control_read_reg(pru, PRU_CTRL_STALL));
388 seq_printf(s, "CTBIR0 := 0x%08x\n",
389 pru_control_read_reg(pru, PRU_CTRL_CTBIR0));
390 seq_printf(s, "CTBIR1 := 0x%08x\n",
391 pru_control_read_reg(pru, PRU_CTRL_CTBIR1));
392 seq_printf(s, "CTPPR0 := 0x%08x\n",
393 pru_control_read_reg(pru, PRU_CTRL_CTPPR0));
394 seq_printf(s, "CTPPR1 := 0x%08x\n",
395 pru_control_read_reg(pru, PRU_CTRL_CTPPR1));
397 seq_puts(s, "=============== Debug Registers ===============\n");
398 pru_is_running = pru_control_read_reg(pru, PRU_CTRL_CTRL) &
400 if (pru_is_running) {
401 seq_puts(s, "PRU is executing, cannot print/access debug registers.\n");
405 for (i = 0; i < nregs; i++) {
406 seq_printf(s, "GPREG%-2d := 0x%08x\tCT_REG%-2d := 0x%08x\n",
407 i, pru_debug_read_reg(pru, PRU_DEBUG_GPREG(i)),
408 i, pru_debug_read_reg(pru, PRU_DEBUG_CT_REG(i)));
413 DEFINE_SHOW_ATTRIBUTE(regs);
416 * Control PRU single-step mode
418 * This is a debug helper function used for controlling the single-step
419 * mode of the PRU. The PRU Debug registers are not accessible when the
420 * PRU is in RUNNING state.
422 * Writing a non-zero value sets the PRU into single-step mode irrespective
423 * of its previous state. The PRU mode is saved only on the first set into
424 * a single-step mode. Writing a zero value will restore the PRU into its
427 static int pru_rproc_debug_ss_set(void *data, u64 val)
429 struct rproc *rproc = data;
430 struct pru_rproc *pru = rproc->priv;
434 if (!val && !pru->dbg_single_step)
437 reg_val = pru_control_read_reg(pru, PRU_CTRL_CTRL);
439 if (val && !pru->dbg_single_step)
440 pru->dbg_continuous = reg_val;
443 reg_val |= CTRL_CTRL_SINGLE_STEP | CTRL_CTRL_EN;
445 reg_val = pru->dbg_continuous;
447 pru->dbg_single_step = val;
448 pru_control_write_reg(pru, PRU_CTRL_CTRL, reg_val);
453 static int pru_rproc_debug_ss_get(void *data, u64 *val)
455 struct rproc *rproc = data;
456 struct pru_rproc *pru = rproc->priv;
458 *val = pru->dbg_single_step;
462 DEFINE_DEBUGFS_ATTRIBUTE(pru_rproc_debug_ss_fops, pru_rproc_debug_ss_get,
463 pru_rproc_debug_ss_set, "%llu\n");
466 * Create PRU-specific debugfs entries
468 * The entries are created only if the parent remoteproc debugfs directory
469 * exists, and will be cleaned up by the remoteproc core.
471 static void pru_rproc_create_debug_entries(struct rproc *rproc)
476 debugfs_create_file("regs", 0400, rproc->dbg_dir,
478 debugfs_create_file("single_step", 0600, rproc->dbg_dir,
479 rproc, &pru_rproc_debug_ss_fops);
482 static void pru_dispose_irq_mapping(struct pru_rproc *pru)
484 if (!pru->mapped_irq)
487 while (pru->evt_count) {
489 if (pru->mapped_irq[pru->evt_count] > 0)
490 irq_dispose_mapping(pru->mapped_irq[pru->evt_count]);
493 kfree(pru->mapped_irq);
494 pru->mapped_irq = NULL;
498 * Parse the custom PRU interrupt map resource and configure the INTC
501 static int pru_handle_intrmap(struct rproc *rproc)
503 struct device *dev = rproc->dev.parent;
504 struct pru_rproc *pru = rproc->priv;
505 struct pru_irq_rsc *rsc = pru->pru_interrupt_map;
506 struct irq_fwspec fwspec;
507 struct device_node *parent, *irq_parent;
510 /* not having pru_interrupt_map is not an error */
514 /* currently supporting only type 0 */
515 if (rsc->type != 0) {
516 dev_err(dev, "unsupported rsc type: %d\n", rsc->type);
520 if (rsc->num_evts > MAX_PRU_SYS_EVENTS)
523 if (sizeof(*rsc) + rsc->num_evts * sizeof(struct pruss_int_map) !=
524 pru->pru_interrupt_map_sz)
527 pru->evt_count = rsc->num_evts;
528 pru->mapped_irq = kcalloc(pru->evt_count, sizeof(unsigned int),
530 if (!pru->mapped_irq) {
536 * parse and fill in system event to interrupt channel and
537 * channel-to-host mapping. The interrupt controller to be used
538 * for these mappings for a given PRU remoteproc is always its
539 * corresponding sibling PRUSS INTC node.
541 parent = of_get_parent(dev_of_node(pru->dev));
543 kfree(pru->mapped_irq);
544 pru->mapped_irq = NULL;
549 irq_parent = of_get_child_by_name(parent, "interrupt-controller");
552 kfree(pru->mapped_irq);
553 pru->mapped_irq = NULL;
558 fwspec.fwnode = of_node_to_fwnode(irq_parent);
559 fwspec.param_count = 3;
560 for (i = 0; i < pru->evt_count; i++) {
561 fwspec.param[0] = rsc->pru_intc_map[i].event;
562 fwspec.param[1] = rsc->pru_intc_map[i].chnl;
563 fwspec.param[2] = rsc->pru_intc_map[i].host;
565 dev_dbg(dev, "mapping%d: event %d, chnl %d, host %d\n",
566 i, fwspec.param[0], fwspec.param[1], fwspec.param[2]);
568 pru->mapped_irq[i] = irq_create_fwspec_mapping(&fwspec);
569 if (!pru->mapped_irq[i]) {
570 dev_err(dev, "failed to get virq for fw mapping %d: event %d chnl %d host %d\n",
571 i, fwspec.param[0], fwspec.param[1],
577 of_node_put(irq_parent);
582 pru_dispose_irq_mapping(pru);
583 of_node_put(irq_parent);
588 static int pru_rproc_start(struct rproc *rproc)
590 struct device *dev = &rproc->dev;
591 struct pru_rproc *pru = rproc->priv;
592 const char *names[PRU_TYPE_MAX] = { "PRU", "RTU", "Tx_PRU" };
596 dev_dbg(dev, "starting %s%d: entry-point = 0x%llx\n",
597 names[pru->data->type], pru->id, (rproc->bootaddr >> 2));
599 ret = pru_handle_intrmap(rproc);
601 * reset references to pru interrupt map - they will stop being valid
602 * after rproc_start returns
604 pru->pru_interrupt_map = NULL;
605 pru->pru_interrupt_map_sz = 0;
609 val = CTRL_CTRL_EN | ((rproc->bootaddr >> 2) << 16);
610 pru_control_write_reg(pru, PRU_CTRL_CTRL, val);
615 static int pru_rproc_stop(struct rproc *rproc)
617 struct device *dev = &rproc->dev;
618 struct pru_rproc *pru = rproc->priv;
619 const char *names[PRU_TYPE_MAX] = { "PRU", "RTU", "Tx_PRU" };
622 dev_dbg(dev, "stopping %s%d\n", names[pru->data->type], pru->id);
624 val = pru_control_read_reg(pru, PRU_CTRL_CTRL);
625 val &= ~CTRL_CTRL_EN;
626 pru_control_write_reg(pru, PRU_CTRL_CTRL, val);
628 /* dispose irq mapping - new firmware can provide new mapping */
629 pru_dispose_irq_mapping(pru);
635 * Convert PRU device address (data spaces only) to kernel virtual address.
637 * Each PRU has access to all data memories within the PRUSS, accessible at
638 * different ranges. So, look through both its primary and secondary Data
639 * RAMs as well as any shared Data RAM to convert a PRU device address to
640 * kernel virtual address. Data RAM0 is primary Data RAM for PRU0 and Data
641 * RAM1 is primary Data RAM for PRU1.
643 static void *pru_d_da_to_va(struct pru_rproc *pru, u32 da, size_t len)
645 struct pruss_mem_region dram0, dram1, shrd_ram;
646 struct pruss *pruss = pru->pruss;
653 dram0 = pruss->mem_regions[PRUSS_MEM_DRAM0];
654 dram1 = pruss->mem_regions[PRUSS_MEM_DRAM1];
655 /* PRU1 has its local RAM addresses reversed */
656 if (pru->id == PRUSS_PRU1)
658 shrd_ram = pruss->mem_regions[PRUSS_MEM_SHRD_RAM2];
660 if (da >= PRU_PDRAM_DA && da + len <= PRU_PDRAM_DA + dram0.size) {
661 offset = da - PRU_PDRAM_DA;
662 va = (__force void *)(dram0.va + offset);
663 } else if (da >= PRU_SDRAM_DA &&
664 da + len <= PRU_SDRAM_DA + dram1.size) {
665 offset = da - PRU_SDRAM_DA;
666 va = (__force void *)(dram1.va + offset);
667 } else if (da >= PRU_SHRDRAM_DA &&
668 da + len <= PRU_SHRDRAM_DA + shrd_ram.size) {
669 offset = da - PRU_SHRDRAM_DA;
670 va = (__force void *)(shrd_ram.va + offset);
677 * Convert PRU device address (instruction space) to kernel virtual address.
679 * A PRU does not have an unified address space. Each PRU has its very own
680 * private Instruction RAM, and its device address is identical to that of
681 * its primary Data RAM device address.
683 static void *pru_i_da_to_va(struct pru_rproc *pru, u32 da, size_t len)
692 * GNU binutils do not support multiple address spaces. The GNU
693 * linker's default linker script places IRAM at an arbitrary high
694 * offset, in order to differentiate it from DRAM. Hence we need to
695 * strip the artificial offset in the IRAM addresses coming from the
698 * The TI proprietary linker would never set those higher IRAM address
699 * bits anyway. PRU architecture limits the program counter to 16-bit
700 * word-address range. This in turn corresponds to 18-bit IRAM
701 * byte-address range for ELF.
703 * Two more bits are added just in case to make the final 20-bit mask.
704 * Idea is to have a safeguard in case TI decides to add banking
709 if (da >= PRU_IRAM_DA &&
710 da + len <= PRU_IRAM_DA + pru->mem_regions[PRU_IOMEM_IRAM].size) {
711 offset = da - PRU_IRAM_DA;
712 va = (__force void *)(pru->mem_regions[PRU_IOMEM_IRAM].va +
720 * Provide address translations for only PRU Data RAMs through the remoteproc
721 * core for any PRU client drivers. The PRU Instruction RAM access is restricted
722 * only to the PRU loader code.
724 static void *pru_rproc_da_to_va(struct rproc *rproc, u64 da, size_t len, bool *is_iomem)
726 struct pru_rproc *pru = rproc->priv;
728 return pru_d_da_to_va(pru, da, len);
731 /* PRU-specific address translator used by PRU loader. */
732 static void *pru_da_to_va(struct rproc *rproc, u64 da, size_t len, bool is_iram)
734 struct pru_rproc *pru = rproc->priv;
738 va = pru_i_da_to_va(pru, da, len);
740 va = pru_d_da_to_va(pru, da, len);
745 static struct rproc_ops pru_rproc_ops = {
746 .start = pru_rproc_start,
747 .stop = pru_rproc_stop,
748 .da_to_va = pru_rproc_da_to_va,
752 * Custom memory copy implementation for ICSSG PRU/RTU/Tx_PRU Cores
754 * The ICSSG PRU/RTU/Tx_PRU cores have a memory copying issue with IRAM
755 * memories, that is not seen on previous generation SoCs. The data is reflected
756 * properly in the IRAM memories only for integer (4-byte) copies. Any unaligned
757 * copies result in all the other pre-existing bytes zeroed out within that
758 * 4-byte boundary, thereby resulting in wrong text/code in the IRAMs. Also, the
759 * IRAM memory port interface does not allow any 8-byte copies (as commonly used
760 * by ARM64 memcpy implementation) and throws an exception. The DRAM memory
761 * ports do not show this behavior.
763 static int pru_rproc_memcpy(void *dest, const void *src, size_t count)
767 size_t size = count / 4;
771 * TODO: relax limitation of 4-byte aligned dest addresses and copy
774 if ((long)dest % 4 || count % 4)
777 /* src offsets in ELF firmware image can be non-aligned */
779 tmp_src = kmemdup(src, count, GFP_KERNEL);
794 pru_rproc_load_elf_segments(struct rproc *rproc, const struct firmware *fw)
796 struct pru_rproc *pru = rproc->priv;
797 struct device *dev = &rproc->dev;
798 struct elf32_hdr *ehdr;
799 struct elf32_phdr *phdr;
801 const u8 *elf_data = fw->data;
803 ehdr = (struct elf32_hdr *)elf_data;
804 phdr = (struct elf32_phdr *)(elf_data + ehdr->e_phoff);
806 /* go through the available ELF segments */
807 for (i = 0; i < ehdr->e_phnum; i++, phdr++) {
808 u32 da = phdr->p_paddr;
809 u32 memsz = phdr->p_memsz;
810 u32 filesz = phdr->p_filesz;
811 u32 offset = phdr->p_offset;
815 if (phdr->p_type != PT_LOAD || !filesz)
818 dev_dbg(dev, "phdr: type %d da 0x%x memsz 0x%x filesz 0x%x\n",
819 phdr->p_type, da, memsz, filesz);
821 if (filesz > memsz) {
822 dev_err(dev, "bad phdr filesz 0x%x memsz 0x%x\n",
828 if (offset + filesz > fw->size) {
829 dev_err(dev, "truncated fw: need 0x%x avail 0x%zx\n",
830 offset + filesz, fw->size);
835 /* grab the kernel address for this device address */
836 is_iram = phdr->p_flags & PF_X;
837 ptr = pru_da_to_va(rproc, da, memsz, is_iram);
839 dev_err(dev, "bad phdr da 0x%x mem 0x%x\n", da, memsz);
844 if (pru->data->is_k3) {
845 ret = pru_rproc_memcpy(ptr, elf_data + phdr->p_offset,
848 dev_err(dev, "PRU memory copy failed for da 0x%x memsz 0x%x\n",
853 memcpy(ptr, elf_data + phdr->p_offset, filesz);
856 /* skip the memzero logic performed by remoteproc ELF loader */
863 pru_rproc_find_interrupt_map(struct device *dev, const struct firmware *fw)
865 struct elf32_shdr *shdr, *name_table_shdr;
866 const char *name_table;
867 const u8 *elf_data = fw->data;
868 struct elf32_hdr *ehdr = (struct elf32_hdr *)elf_data;
869 u16 shnum = ehdr->e_shnum;
870 u16 shstrndx = ehdr->e_shstrndx;
873 /* first, get the section header */
874 shdr = (struct elf32_shdr *)(elf_data + ehdr->e_shoff);
875 /* compute name table section header entry in shdr array */
876 name_table_shdr = shdr + shstrndx;
877 /* finally, compute the name table section address in elf */
878 name_table = elf_data + name_table_shdr->sh_offset;
880 for (i = 0; i < shnum; i++, shdr++) {
881 u32 size = shdr->sh_size;
882 u32 offset = shdr->sh_offset;
883 u32 name = shdr->sh_name;
885 if (strcmp(name_table + name, ".pru_irq_map"))
888 /* make sure we have the entire irq map */
889 if (offset + size > fw->size || offset + size < size) {
890 dev_err(dev, ".pru_irq_map section truncated\n");
891 return ERR_PTR(-EINVAL);
894 /* make sure irq map has at least the header */
895 if (sizeof(struct pru_irq_rsc) > size) {
896 dev_err(dev, "header-less .pru_irq_map section\n");
897 return ERR_PTR(-EINVAL);
903 dev_dbg(dev, "no .pru_irq_map section found for this fw\n");
909 * Use a custom parse_fw callback function for dealing with PRU firmware
912 * The firmware blob can contain optional ELF sections: .resource_table section
913 * and .pru_irq_map one. The second one contains the PRUSS interrupt mapping
914 * description, which needs to be setup before powering on the PRU core. To
915 * avoid RAM wastage this ELF section is not mapped to any ELF segment (by the
916 * firmware linker) and therefore is not loaded to PRU memory.
918 static int pru_rproc_parse_fw(struct rproc *rproc, const struct firmware *fw)
920 struct device *dev = &rproc->dev;
921 struct pru_rproc *pru = rproc->priv;
922 const u8 *elf_data = fw->data;
924 u8 class = fw_elf_get_class(fw);
928 /* load optional rsc table */
929 ret = rproc_elf_load_rsc_table(rproc, fw);
931 dev_dbg(&rproc->dev, "no resource table found for this fw\n");
935 /* find .pru_interrupt_map section, not having it is not an error */
936 shdr = pru_rproc_find_interrupt_map(dev, fw);
938 return PTR_ERR(shdr);
943 /* preserve pointer to PRU interrupt map together with it size */
944 sh_offset = elf_shdr_get_sh_offset(class, shdr);
945 pru->pru_interrupt_map = (struct pru_irq_rsc *)(elf_data + sh_offset);
946 pru->pru_interrupt_map_sz = elf_shdr_get_sh_size(class, shdr);
952 * Compute PRU id based on the IRAM addresses. The PRU IRAMs are
953 * always at a particular offset within the PRUSS address space.
955 static int pru_rproc_set_id(struct pru_rproc *pru)
959 switch (pru->mem_regions[PRU_IOMEM_IRAM].pa & PRU_IRAM_ADDR_MASK) {
960 case TX_PRU0_IRAM_ADDR_MASK:
962 case RTU0_IRAM_ADDR_MASK:
964 case PRU0_IRAM_ADDR_MASK:
965 pru->id = PRUSS_PRU0;
967 case TX_PRU1_IRAM_ADDR_MASK:
969 case RTU1_IRAM_ADDR_MASK:
971 case PRU1_IRAM_ADDR_MASK:
972 pru->id = PRUSS_PRU1;
981 static int pru_rproc_probe(struct platform_device *pdev)
983 struct device *dev = &pdev->dev;
984 struct device_node *np = dev->of_node;
985 struct platform_device *ppdev = to_platform_device(dev->parent);
986 struct pru_rproc *pru;
988 struct rproc *rproc = NULL;
989 struct resource *res;
991 const struct pru_private_data *data;
992 const char *mem_names[PRU_IOMEM_MAX] = { "iram", "control", "debug" };
994 data = of_device_get_match_data(&pdev->dev);
998 ret = of_property_read_string(np, "firmware-name", &fw_name);
1000 dev_err(dev, "unable to retrieve firmware-name %d\n", ret);
1004 rproc = devm_rproc_alloc(dev, pdev->name, &pru_rproc_ops, fw_name,
1007 dev_err(dev, "rproc_alloc failed\n");
1010 /* use a custom load function to deal with PRU-specific quirks */
1011 rproc->ops->load = pru_rproc_load_elf_segments;
1013 /* use a custom parse function to deal with PRU-specific resources */
1014 rproc->ops->parse_fw = pru_rproc_parse_fw;
1016 /* error recovery is not supported for PRUs */
1017 rproc->recovery_disabled = true;
1020 * rproc_add will auto-boot the processor normally, but this is not
1021 * desired with PRU client driven boot-flow methodology. A PRU
1022 * application/client driver will boot the corresponding PRU
1023 * remote-processor as part of its state machine either through the
1024 * remoteproc sysfs interface or through the equivalent kernel API.
1026 rproc->auto_boot = false;
1031 pru->pruss = platform_get_drvdata(ppdev);
1033 pru->fw_name = fw_name;
1034 pru->client_np = NULL;
1035 spin_lock_init(&pru->rmw_lock);
1036 mutex_init(&pru->lock);
1038 for (i = 0; i < ARRAY_SIZE(mem_names); i++) {
1039 res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
1041 pru->mem_regions[i].va = devm_ioremap_resource(dev, res);
1042 if (IS_ERR(pru->mem_regions[i].va)) {
1043 dev_err(dev, "failed to parse and map memory resource %d %s\n",
1045 ret = PTR_ERR(pru->mem_regions[i].va);
1048 pru->mem_regions[i].pa = res->start;
1049 pru->mem_regions[i].size = resource_size(res);
1051 dev_dbg(dev, "memory %8s: pa %pa size 0x%zx va %pK\n",
1052 mem_names[i], &pru->mem_regions[i].pa,
1053 pru->mem_regions[i].size, pru->mem_regions[i].va);
1056 ret = pru_rproc_set_id(pru);
1060 platform_set_drvdata(pdev, rproc);
1062 ret = devm_rproc_add(dev, pru->rproc);
1064 dev_err(dev, "rproc_add failed: %d\n", ret);
1068 pru_rproc_create_debug_entries(rproc);
1070 dev_dbg(dev, "PRU rproc node %pOF probed successfully\n", np);
1075 static int pru_rproc_remove(struct platform_device *pdev)
1077 struct device *dev = &pdev->dev;
1078 struct rproc *rproc = platform_get_drvdata(pdev);
1080 dev_dbg(dev, "%s: removing rproc %s\n", __func__, rproc->name);
1085 static const struct pru_private_data pru_data = {
1086 .type = PRU_TYPE_PRU,
1089 static const struct pru_private_data k3_pru_data = {
1090 .type = PRU_TYPE_PRU,
1094 static const struct pru_private_data k3_rtu_data = {
1095 .type = PRU_TYPE_RTU,
1099 static const struct pru_private_data k3_tx_pru_data = {
1100 .type = PRU_TYPE_TX_PRU,
1104 static const struct of_device_id pru_rproc_match[] = {
1105 { .compatible = "ti,am3356-pru", .data = &pru_data },
1106 { .compatible = "ti,am4376-pru", .data = &pru_data },
1107 { .compatible = "ti,am5728-pru", .data = &pru_data },
1108 { .compatible = "ti,am642-pru", .data = &k3_pru_data },
1109 { .compatible = "ti,am642-rtu", .data = &k3_rtu_data },
1110 { .compatible = "ti,am642-tx-pru", .data = &k3_tx_pru_data },
1111 { .compatible = "ti,k2g-pru", .data = &pru_data },
1112 { .compatible = "ti,am654-pru", .data = &k3_pru_data },
1113 { .compatible = "ti,am654-rtu", .data = &k3_rtu_data },
1114 { .compatible = "ti,am654-tx-pru", .data = &k3_tx_pru_data },
1115 { .compatible = "ti,j721e-pru", .data = &k3_pru_data },
1116 { .compatible = "ti,j721e-rtu", .data = &k3_rtu_data },
1117 { .compatible = "ti,j721e-tx-pru", .data = &k3_tx_pru_data },
1118 { .compatible = "ti,am625-pru", .data = &k3_pru_data },
1121 MODULE_DEVICE_TABLE(of, pru_rproc_match);
1123 static struct platform_driver pru_rproc_driver = {
1125 .name = PRU_RPROC_DRVNAME,
1126 .of_match_table = pru_rproc_match,
1127 .suppress_bind_attrs = true,
1129 .probe = pru_rproc_probe,
1130 .remove = pru_rproc_remove,
1132 module_platform_driver(pru_rproc_driver);
1134 MODULE_AUTHOR("Suman Anna <s-anna@ti.com>");
1135 MODULE_AUTHOR("Andrew F. Davis <afd@ti.com>");
1136 MODULE_AUTHOR("Grzegorz Jaszczyk <grzegorz.jaszczyk@linaro.org>");
1137 MODULE_AUTHOR("Puranjay Mohan <p-mohan@ti.com>");
1138 MODULE_AUTHOR("Md Danish Anwar <danishanwar@ti.com>");
1139 MODULE_DESCRIPTION("PRU-ICSS Remote Processor Driver");
1140 MODULE_LICENSE("GPL v2");