blob: b13ff4283232b39f332b6d51b2da05cef6ed386b [file] [log] [blame]
Suman Annad4ce2de2020-12-08 15:09:58 +01001// 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>
Suman Anna20ad1de2020-12-08 15:10:00 +010014#include <linux/debugfs.h>
Grzegorz Jaszczykc75c9fd2020-12-08 15:09:59 +010015#include <linux/irqdomain.h>
Suman Annad4ce2de2020-12-08 15:09:58 +010016#include <linux/module.h>
17#include <linux/of_device.h>
Grzegorz Jaszczykc75c9fd2020-12-08 15:09:59 +010018#include <linux/of_irq.h>
Suman Annad4ce2de2020-12-08 15:09:58 +010019#include <linux/pruss_driver.h>
20#include <linux/remoteproc.h>
21
22#include "remoteproc_internal.h"
23#include "remoteproc_elf_helpers.h"
Grzegorz Jaszczykc75c9fd2020-12-08 15:09:59 +010024#include "pru_rproc.h"
Suman Annad4ce2de2020-12-08 15:09:58 +010025
26/* PRU_ICSS_PRU_CTRL registers */
27#define PRU_CTRL_CTRL 0x0000
28#define PRU_CTRL_STS 0x0004
Suman Anna20ad1de2020-12-08 15:10:00 +010029#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
Suman Annad4ce2de2020-12-08 15:09:58 +010036
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
Suman Anna20ad1de2020-12-08 15:10:00 +010045/* 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
Suman Anna1d39f4d2020-12-08 15:10:01 +010049/* PRU/RTU/Tx_PRU Core IRAM address masks */
Suman Annad4ce2de2020-12-08 15:09:58 +010050#define PRU_IRAM_ADDR_MASK 0x3ffff
51#define PRU0_IRAM_ADDR_MASK 0x34000
52#define PRU1_IRAM_ADDR_MASK 0x38000
Suman Anna1d39f4d2020-12-08 15:10:01 +010053#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
Suman Annad4ce2de2020-12-08 15:09:58 +010057
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
Grzegorz Jaszczykc75c9fd2020-12-08 15:09:59 +010064#define MAX_PRU_SYS_EVENTS 160
65
Suman Annad4ce2de2020-12-08 15:09:58 +010066/**
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
81/**
Suman Anna1d39f4d2020-12-08 15:10:01 +010082 * 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
106/**
Suman Annad4ce2de2020-12-08 15:09:58 +0100107 * 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
Suman Anna1d39f4d2020-12-08 15:10:01 +0100112 * @data: PRU core specific data
Suman Annad4ce2de2020-12-08 15:09:58 +0100113 * @mem_regions: data for each of the PRU memory regions
114 * @fw_name: name of firmware image used during loading
Grzegorz Jaszczykc75c9fd2020-12-08 15:09:59 +0100115 * @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
Suman Anna20ad1de2020-12-08 15:10:00 +0100118 * @dbg_single_step: debug state variable to set PRU into single step mode
119 * @dbg_continuous: debug state variable to restore PRU execution mode
Grzegorz Jaszczykc75c9fd2020-12-08 15:09:59 +0100120 * @evt_count: number of mapped events
Suman Annad4ce2de2020-12-08 15:09:58 +0100121 */
122struct pru_rproc {
123 int id;
124 struct device *dev;
125 struct pruss *pruss;
126 struct rproc *rproc;
Suman Anna1d39f4d2020-12-08 15:10:01 +0100127 const struct pru_private_data *data;
Suman Annad4ce2de2020-12-08 15:09:58 +0100128 struct pruss_mem_region mem_regions[PRU_IOMEM_MAX];
129 const char *fw_name;
Grzegorz Jaszczykc75c9fd2020-12-08 15:09:59 +0100130 unsigned int *mapped_irq;
131 struct pru_irq_rsc *pru_interrupt_map;
132 size_t pru_interrupt_map_sz;
Suman Anna20ad1de2020-12-08 15:10:00 +0100133 u32 dbg_single_step;
134 u32 dbg_continuous;
Grzegorz Jaszczykc75c9fd2020-12-08 15:09:59 +0100135 u8 evt_count;
Suman Annad4ce2de2020-12-08 15:09:58 +0100136};
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
Suman Anna20ad1de2020-12-08 15:10:00 +0100149static 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}
Yang Li780a9802021-02-24 16:20:29 +0800247DEFINE_DEBUGFS_ATTRIBUTE(pru_rproc_debug_ss_fops, pru_rproc_debug_ss_get,
248 pru_rproc_debug_ss_set, "%llu\n");
Suman Anna20ad1de2020-12-08 15:10:00 +0100249
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
Grzegorz Jaszczykc75c9fd2020-12-08 15:09:59 +0100267static void pru_dispose_irq_mapping(struct pru_rproc *pru)
268{
Suman Anna880a66e2021-04-07 10:56:41 -0500269 if (!pru->mapped_irq)
270 return;
271
272 while (pru->evt_count) {
273 pru->evt_count--;
Grzegorz Jaszczykc75c9fd2020-12-08 15:09:59 +0100274 if (pru->mapped_irq[pru->evt_count] > 0)
275 irq_dispose_mapping(pru->mapped_irq[pru->evt_count]);
276 }
277
278 kfree(pru->mapped_irq);
Suman Anna880a66e2021-04-07 10:56:41 -0500279 pru->mapped_irq = NULL;
Grzegorz Jaszczykc75c9fd2020-12-08 15:09:59 +0100280}
281
282/*
283 * Parse the custom PRU interrupt map resource and configure the INTC
284 * appropriately.
285 */
286static int pru_handle_intrmap(struct rproc *rproc)
287{
288 struct device *dev = rproc->dev.parent;
289 struct pru_rproc *pru = rproc->priv;
290 struct pru_irq_rsc *rsc = pru->pru_interrupt_map;
291 struct irq_fwspec fwspec;
Suman Anna6d1f2802021-04-07 10:56:39 -0500292 struct device_node *parent, *irq_parent;
Grzegorz Jaszczykc75c9fd2020-12-08 15:09:59 +0100293 int i, ret = 0;
294
295 /* not having pru_interrupt_map is not an error */
296 if (!rsc)
297 return 0;
298
299 /* currently supporting only type 0 */
300 if (rsc->type != 0) {
301 dev_err(dev, "unsupported rsc type: %d\n", rsc->type);
302 return -EINVAL;
303 }
304
305 if (rsc->num_evts > MAX_PRU_SYS_EVENTS)
306 return -EINVAL;
307
308 if (sizeof(*rsc) + rsc->num_evts * sizeof(struct pruss_int_map) !=
309 pru->pru_interrupt_map_sz)
310 return -EINVAL;
311
312 pru->evt_count = rsc->num_evts;
313 pru->mapped_irq = kcalloc(pru->evt_count, sizeof(unsigned int),
314 GFP_KERNEL);
Suman Anna880a66e2021-04-07 10:56:41 -0500315 if (!pru->mapped_irq) {
316 pru->evt_count = 0;
Grzegorz Jaszczykc75c9fd2020-12-08 15:09:59 +0100317 return -ENOMEM;
Suman Anna880a66e2021-04-07 10:56:41 -0500318 }
Grzegorz Jaszczykc75c9fd2020-12-08 15:09:59 +0100319
320 /*
321 * parse and fill in system event to interrupt channel and
Suman Anna6d1f2802021-04-07 10:56:39 -0500322 * channel-to-host mapping. The interrupt controller to be used
323 * for these mappings for a given PRU remoteproc is always its
324 * corresponding sibling PRUSS INTC node.
Grzegorz Jaszczykc75c9fd2020-12-08 15:09:59 +0100325 */
Suman Anna6d1f2802021-04-07 10:56:39 -0500326 parent = of_get_parent(dev_of_node(pru->dev));
Suman Anna880a66e2021-04-07 10:56:41 -0500327 if (!parent) {
328 kfree(pru->mapped_irq);
329 pru->mapped_irq = NULL;
330 pru->evt_count = 0;
Suman Anna6d1f2802021-04-07 10:56:39 -0500331 return -ENODEV;
Suman Anna880a66e2021-04-07 10:56:41 -0500332 }
Suman Anna6d1f2802021-04-07 10:56:39 -0500333
334 irq_parent = of_get_child_by_name(parent, "interrupt-controller");
335 of_node_put(parent);
Grzegorz Jaszczykc75c9fd2020-12-08 15:09:59 +0100336 if (!irq_parent) {
337 kfree(pru->mapped_irq);
Suman Anna880a66e2021-04-07 10:56:41 -0500338 pru->mapped_irq = NULL;
339 pru->evt_count = 0;
Grzegorz Jaszczykc75c9fd2020-12-08 15:09:59 +0100340 return -ENODEV;
341 }
342
343 fwspec.fwnode = of_node_to_fwnode(irq_parent);
344 fwspec.param_count = 3;
345 for (i = 0; i < pru->evt_count; i++) {
346 fwspec.param[0] = rsc->pru_intc_map[i].event;
347 fwspec.param[1] = rsc->pru_intc_map[i].chnl;
348 fwspec.param[2] = rsc->pru_intc_map[i].host;
349
350 dev_dbg(dev, "mapping%d: event %d, chnl %d, host %d\n",
351 i, fwspec.param[0], fwspec.param[1], fwspec.param[2]);
352
353 pru->mapped_irq[i] = irq_create_fwspec_mapping(&fwspec);
354 if (!pru->mapped_irq[i]) {
Suman Anna1fe72bc2021-04-07 10:56:40 -0500355 dev_err(dev, "failed to get virq for fw mapping %d: event %d chnl %d host %d\n",
356 i, fwspec.param[0], fwspec.param[1],
357 fwspec.param[2]);
358 ret = -EINVAL;
Grzegorz Jaszczykc75c9fd2020-12-08 15:09:59 +0100359 goto map_fail;
360 }
361 }
Suman Anna6d1f2802021-04-07 10:56:39 -0500362 of_node_put(irq_parent);
Grzegorz Jaszczykc75c9fd2020-12-08 15:09:59 +0100363
364 return ret;
365
366map_fail:
367 pru_dispose_irq_mapping(pru);
Suman Anna6d1f2802021-04-07 10:56:39 -0500368 of_node_put(irq_parent);
Grzegorz Jaszczykc75c9fd2020-12-08 15:09:59 +0100369
370 return ret;
371}
372
Suman Annad4ce2de2020-12-08 15:09:58 +0100373static int pru_rproc_start(struct rproc *rproc)
374{
375 struct device *dev = &rproc->dev;
376 struct pru_rproc *pru = rproc->priv;
Suman Anna1d39f4d2020-12-08 15:10:01 +0100377 const char *names[PRU_TYPE_MAX] = { "PRU", "RTU", "Tx_PRU" };
Suman Annad4ce2de2020-12-08 15:09:58 +0100378 u32 val;
Grzegorz Jaszczykc75c9fd2020-12-08 15:09:59 +0100379 int ret;
Suman Annad4ce2de2020-12-08 15:09:58 +0100380
Suman Anna1d39f4d2020-12-08 15:10:01 +0100381 dev_dbg(dev, "starting %s%d: entry-point = 0x%llx\n",
382 names[pru->data->type], pru->id, (rproc->bootaddr >> 2));
Suman Annad4ce2de2020-12-08 15:09:58 +0100383
Grzegorz Jaszczykc75c9fd2020-12-08 15:09:59 +0100384 ret = pru_handle_intrmap(rproc);
385 /*
386 * reset references to pru interrupt map - they will stop being valid
387 * after rproc_start returns
388 */
389 pru->pru_interrupt_map = NULL;
390 pru->pru_interrupt_map_sz = 0;
391 if (ret)
392 return ret;
393
Suman Annad4ce2de2020-12-08 15:09:58 +0100394 val = CTRL_CTRL_EN | ((rproc->bootaddr >> 2) << 16);
395 pru_control_write_reg(pru, PRU_CTRL_CTRL, val);
396
397 return 0;
398}
399
400static int pru_rproc_stop(struct rproc *rproc)
401{
402 struct device *dev = &rproc->dev;
403 struct pru_rproc *pru = rproc->priv;
Suman Anna1d39f4d2020-12-08 15:10:01 +0100404 const char *names[PRU_TYPE_MAX] = { "PRU", "RTU", "Tx_PRU" };
Suman Annad4ce2de2020-12-08 15:09:58 +0100405 u32 val;
406
Suman Anna1d39f4d2020-12-08 15:10:01 +0100407 dev_dbg(dev, "stopping %s%d\n", names[pru->data->type], pru->id);
Suman Annad4ce2de2020-12-08 15:09:58 +0100408
409 val = pru_control_read_reg(pru, PRU_CTRL_CTRL);
410 val &= ~CTRL_CTRL_EN;
411 pru_control_write_reg(pru, PRU_CTRL_CTRL, val);
412
Grzegorz Jaszczykc75c9fd2020-12-08 15:09:59 +0100413 /* dispose irq mapping - new firmware can provide new mapping */
Suman Anna880a66e2021-04-07 10:56:41 -0500414 pru_dispose_irq_mapping(pru);
Grzegorz Jaszczykc75c9fd2020-12-08 15:09:59 +0100415
Suman Annad4ce2de2020-12-08 15:09:58 +0100416 return 0;
417}
418
419/*
420 * Convert PRU device address (data spaces only) to kernel virtual address.
421 *
422 * Each PRU has access to all data memories within the PRUSS, accessible at
423 * different ranges. So, look through both its primary and secondary Data
424 * RAMs as well as any shared Data RAM to convert a PRU device address to
425 * kernel virtual address. Data RAM0 is primary Data RAM for PRU0 and Data
426 * RAM1 is primary Data RAM for PRU1.
427 */
428static void *pru_d_da_to_va(struct pru_rproc *pru, u32 da, size_t len)
429{
430 struct pruss_mem_region dram0, dram1, shrd_ram;
431 struct pruss *pruss = pru->pruss;
432 u32 offset;
433 void *va = NULL;
434
435 if (len == 0)
436 return NULL;
437
438 dram0 = pruss->mem_regions[PRUSS_MEM_DRAM0];
439 dram1 = pruss->mem_regions[PRUSS_MEM_DRAM1];
440 /* PRU1 has its local RAM addresses reversed */
441 if (pru->id == 1)
442 swap(dram0, dram1);
443 shrd_ram = pruss->mem_regions[PRUSS_MEM_SHRD_RAM2];
444
445 if (da >= PRU_PDRAM_DA && da + len <= PRU_PDRAM_DA + dram0.size) {
446 offset = da - PRU_PDRAM_DA;
447 va = (__force void *)(dram0.va + offset);
448 } else if (da >= PRU_SDRAM_DA &&
449 da + len <= PRU_SDRAM_DA + dram1.size) {
450 offset = da - PRU_SDRAM_DA;
451 va = (__force void *)(dram1.va + offset);
452 } else if (da >= PRU_SHRDRAM_DA &&
453 da + len <= PRU_SHRDRAM_DA + shrd_ram.size) {
454 offset = da - PRU_SHRDRAM_DA;
455 va = (__force void *)(shrd_ram.va + offset);
456 }
457
458 return va;
459}
460
461/*
462 * Convert PRU device address (instruction space) to kernel virtual address.
463 *
464 * A PRU does not have an unified address space. Each PRU has its very own
465 * private Instruction RAM, and its device address is identical to that of
466 * its primary Data RAM device address.
467 */
468static void *pru_i_da_to_va(struct pru_rproc *pru, u32 da, size_t len)
469{
470 u32 offset;
471 void *va = NULL;
472
473 if (len == 0)
474 return NULL;
475
476 if (da >= PRU_IRAM_DA &&
477 da + len <= PRU_IRAM_DA + pru->mem_regions[PRU_IOMEM_IRAM].size) {
478 offset = da - PRU_IRAM_DA;
479 va = (__force void *)(pru->mem_regions[PRU_IOMEM_IRAM].va +
480 offset);
481 }
482
483 return va;
484}
485
486/*
487 * Provide address translations for only PRU Data RAMs through the remoteproc
488 * core for any PRU client drivers. The PRU Instruction RAM access is restricted
489 * only to the PRU loader code.
490 */
Peng Fan40df0a92021-03-06 19:24:19 +0800491static void *pru_rproc_da_to_va(struct rproc *rproc, u64 da, size_t len, bool *is_iomem)
Suman Annad4ce2de2020-12-08 15:09:58 +0100492{
493 struct pru_rproc *pru = rproc->priv;
494
495 return pru_d_da_to_va(pru, da, len);
496}
497
498/* PRU-specific address translator used by PRU loader. */
499static void *pru_da_to_va(struct rproc *rproc, u64 da, size_t len, bool is_iram)
500{
501 struct pru_rproc *pru = rproc->priv;
502 void *va;
503
504 if (is_iram)
505 va = pru_i_da_to_va(pru, da, len);
506 else
507 va = pru_d_da_to_va(pru, da, len);
508
509 return va;
510}
511
512static struct rproc_ops pru_rproc_ops = {
513 .start = pru_rproc_start,
514 .stop = pru_rproc_stop,
515 .da_to_va = pru_rproc_da_to_va,
516};
517
Suman Anna1d39f4d2020-12-08 15:10:01 +0100518/*
519 * Custom memory copy implementation for ICSSG PRU/RTU/Tx_PRU Cores
520 *
521 * The ICSSG PRU/RTU/Tx_PRU cores have a memory copying issue with IRAM
522 * memories, that is not seen on previous generation SoCs. The data is reflected
523 * properly in the IRAM memories only for integer (4-byte) copies. Any unaligned
524 * copies result in all the other pre-existing bytes zeroed out within that
525 * 4-byte boundary, thereby resulting in wrong text/code in the IRAMs. Also, the
526 * IRAM memory port interface does not allow any 8-byte copies (as commonly used
527 * by ARM64 memcpy implementation) and throws an exception. The DRAM memory
528 * ports do not show this behavior.
529 */
530static int pru_rproc_memcpy(void *dest, const void *src, size_t count)
531{
532 const u32 *s = src;
533 u32 *d = dest;
534 size_t size = count / 4;
535 u32 *tmp_src = NULL;
536
537 /*
538 * TODO: relax limitation of 4-byte aligned dest addresses and copy
539 * sizes
540 */
541 if ((long)dest % 4 || count % 4)
542 return -EINVAL;
543
544 /* src offsets in ELF firmware image can be non-aligned */
545 if ((long)src % 4) {
546 tmp_src = kmemdup(src, count, GFP_KERNEL);
547 if (!tmp_src)
548 return -ENOMEM;
549 s = tmp_src;
550 }
551
552 while (size--)
553 *d++ = *s++;
554
555 kfree(tmp_src);
556
557 return 0;
558}
559
Suman Annad4ce2de2020-12-08 15:09:58 +0100560static int
561pru_rproc_load_elf_segments(struct rproc *rproc, const struct firmware *fw)
562{
Suman Anna1d39f4d2020-12-08 15:10:01 +0100563 struct pru_rproc *pru = rproc->priv;
Suman Annad4ce2de2020-12-08 15:09:58 +0100564 struct device *dev = &rproc->dev;
565 struct elf32_hdr *ehdr;
566 struct elf32_phdr *phdr;
567 int i, ret = 0;
568 const u8 *elf_data = fw->data;
569
570 ehdr = (struct elf32_hdr *)elf_data;
571 phdr = (struct elf32_phdr *)(elf_data + ehdr->e_phoff);
572
573 /* go through the available ELF segments */
574 for (i = 0; i < ehdr->e_phnum; i++, phdr++) {
575 u32 da = phdr->p_paddr;
576 u32 memsz = phdr->p_memsz;
577 u32 filesz = phdr->p_filesz;
578 u32 offset = phdr->p_offset;
579 bool is_iram;
580 void *ptr;
581
582 if (phdr->p_type != PT_LOAD || !filesz)
583 continue;
584
585 dev_dbg(dev, "phdr: type %d da 0x%x memsz 0x%x filesz 0x%x\n",
586 phdr->p_type, da, memsz, filesz);
587
588 if (filesz > memsz) {
589 dev_err(dev, "bad phdr filesz 0x%x memsz 0x%x\n",
590 filesz, memsz);
591 ret = -EINVAL;
592 break;
593 }
594
595 if (offset + filesz > fw->size) {
596 dev_err(dev, "truncated fw: need 0x%x avail 0x%zx\n",
597 offset + filesz, fw->size);
598 ret = -EINVAL;
599 break;
600 }
601
602 /* grab the kernel address for this device address */
603 is_iram = phdr->p_flags & PF_X;
604 ptr = pru_da_to_va(rproc, da, memsz, is_iram);
605 if (!ptr) {
606 dev_err(dev, "bad phdr da 0x%x mem 0x%x\n", da, memsz);
607 ret = -EINVAL;
608 break;
609 }
610
Suman Anna1d39f4d2020-12-08 15:10:01 +0100611 if (pru->data->is_k3 && is_iram) {
612 ret = pru_rproc_memcpy(ptr, elf_data + phdr->p_offset,
613 filesz);
614 if (ret) {
615 dev_err(dev, "PRU memory copy failed for da 0x%x memsz 0x%x\n",
616 da, memsz);
617 break;
618 }
619 } else {
620 memcpy(ptr, elf_data + phdr->p_offset, filesz);
621 }
Suman Annad4ce2de2020-12-08 15:09:58 +0100622
623 /* skip the memzero logic performed by remoteproc ELF loader */
624 }
625
626 return ret;
627}
628
Grzegorz Jaszczykc75c9fd2020-12-08 15:09:59 +0100629static const void *
630pru_rproc_find_interrupt_map(struct device *dev, const struct firmware *fw)
631{
632 struct elf32_shdr *shdr, *name_table_shdr;
633 const char *name_table;
634 const u8 *elf_data = fw->data;
635 struct elf32_hdr *ehdr = (struct elf32_hdr *)elf_data;
636 u16 shnum = ehdr->e_shnum;
637 u16 shstrndx = ehdr->e_shstrndx;
638 int i;
639
640 /* first, get the section header */
641 shdr = (struct elf32_shdr *)(elf_data + ehdr->e_shoff);
642 /* compute name table section header entry in shdr array */
643 name_table_shdr = shdr + shstrndx;
644 /* finally, compute the name table section address in elf */
645 name_table = elf_data + name_table_shdr->sh_offset;
646
647 for (i = 0; i < shnum; i++, shdr++) {
648 u32 size = shdr->sh_size;
649 u32 offset = shdr->sh_offset;
650 u32 name = shdr->sh_name;
651
652 if (strcmp(name_table + name, ".pru_irq_map"))
653 continue;
654
655 /* make sure we have the entire irq map */
656 if (offset + size > fw->size || offset + size < size) {
657 dev_err(dev, ".pru_irq_map section truncated\n");
658 return ERR_PTR(-EINVAL);
659 }
660
661 /* make sure irq map has at least the header */
662 if (sizeof(struct pru_irq_rsc) > size) {
663 dev_err(dev, "header-less .pru_irq_map section\n");
664 return ERR_PTR(-EINVAL);
665 }
666
667 return shdr;
668 }
669
670 dev_dbg(dev, "no .pru_irq_map section found for this fw\n");
671
672 return NULL;
673}
674
Suman Annad4ce2de2020-12-08 15:09:58 +0100675/*
676 * Use a custom parse_fw callback function for dealing with PRU firmware
677 * specific sections.
Grzegorz Jaszczykc75c9fd2020-12-08 15:09:59 +0100678 *
679 * The firmware blob can contain optional ELF sections: .resource_table section
680 * and .pru_irq_map one. The second one contains the PRUSS interrupt mapping
681 * description, which needs to be setup before powering on the PRU core. To
682 * avoid RAM wastage this ELF section is not mapped to any ELF segment (by the
683 * firmware linker) and therefore is not loaded to PRU memory.
Suman Annad4ce2de2020-12-08 15:09:58 +0100684 */
685static int pru_rproc_parse_fw(struct rproc *rproc, const struct firmware *fw)
686{
Grzegorz Jaszczykc75c9fd2020-12-08 15:09:59 +0100687 struct device *dev = &rproc->dev;
688 struct pru_rproc *pru = rproc->priv;
689 const u8 *elf_data = fw->data;
690 const void *shdr;
691 u8 class = fw_elf_get_class(fw);
692 u64 sh_offset;
Suman Annad4ce2de2020-12-08 15:09:58 +0100693 int ret;
694
695 /* load optional rsc table */
696 ret = rproc_elf_load_rsc_table(rproc, fw);
697 if (ret == -EINVAL)
698 dev_dbg(&rproc->dev, "no resource table found for this fw\n");
699 else if (ret)
700 return ret;
701
Grzegorz Jaszczykc75c9fd2020-12-08 15:09:59 +0100702 /* find .pru_interrupt_map section, not having it is not an error */
703 shdr = pru_rproc_find_interrupt_map(dev, fw);
704 if (IS_ERR(shdr))
705 return PTR_ERR(shdr);
706
707 if (!shdr)
708 return 0;
709
710 /* preserve pointer to PRU interrupt map together with it size */
711 sh_offset = elf_shdr_get_sh_offset(class, shdr);
712 pru->pru_interrupt_map = (struct pru_irq_rsc *)(elf_data + sh_offset);
713 pru->pru_interrupt_map_sz = elf_shdr_get_sh_size(class, shdr);
714
Suman Annad4ce2de2020-12-08 15:09:58 +0100715 return 0;
716}
717
718/*
719 * Compute PRU id based on the IRAM addresses. The PRU IRAMs are
720 * always at a particular offset within the PRUSS address space.
721 */
722static int pru_rproc_set_id(struct pru_rproc *pru)
723{
724 int ret = 0;
725
726 switch (pru->mem_regions[PRU_IOMEM_IRAM].pa & PRU_IRAM_ADDR_MASK) {
Suman Anna1d39f4d2020-12-08 15:10:01 +0100727 case TX_PRU0_IRAM_ADDR_MASK:
728 fallthrough;
729 case RTU0_IRAM_ADDR_MASK:
730 fallthrough;
Suman Annad4ce2de2020-12-08 15:09:58 +0100731 case PRU0_IRAM_ADDR_MASK:
732 pru->id = 0;
733 break;
Suman Anna1d39f4d2020-12-08 15:10:01 +0100734 case TX_PRU1_IRAM_ADDR_MASK:
735 fallthrough;
736 case RTU1_IRAM_ADDR_MASK:
737 fallthrough;
Suman Annad4ce2de2020-12-08 15:09:58 +0100738 case PRU1_IRAM_ADDR_MASK:
739 pru->id = 1;
740 break;
741 default:
742 ret = -EINVAL;
743 }
744
745 return ret;
746}
747
748static int pru_rproc_probe(struct platform_device *pdev)
749{
750 struct device *dev = &pdev->dev;
751 struct device_node *np = dev->of_node;
752 struct platform_device *ppdev = to_platform_device(dev->parent);
753 struct pru_rproc *pru;
754 const char *fw_name;
755 struct rproc *rproc = NULL;
756 struct resource *res;
757 int i, ret;
Suman Anna1d39f4d2020-12-08 15:10:01 +0100758 const struct pru_private_data *data;
Suman Annad4ce2de2020-12-08 15:09:58 +0100759 const char *mem_names[PRU_IOMEM_MAX] = { "iram", "control", "debug" };
760
Suman Anna1d39f4d2020-12-08 15:10:01 +0100761 data = of_device_get_match_data(&pdev->dev);
762 if (!data)
763 return -ENODEV;
764
Suman Annad4ce2de2020-12-08 15:09:58 +0100765 ret = of_property_read_string(np, "firmware-name", &fw_name);
766 if (ret) {
767 dev_err(dev, "unable to retrieve firmware-name %d\n", ret);
768 return ret;
769 }
770
771 rproc = devm_rproc_alloc(dev, pdev->name, &pru_rproc_ops, fw_name,
772 sizeof(*pru));
773 if (!rproc) {
774 dev_err(dev, "rproc_alloc failed\n");
775 return -ENOMEM;
776 }
777 /* use a custom load function to deal with PRU-specific quirks */
778 rproc->ops->load = pru_rproc_load_elf_segments;
779
780 /* use a custom parse function to deal with PRU-specific resources */
781 rproc->ops->parse_fw = pru_rproc_parse_fw;
782
783 /* error recovery is not supported for PRUs */
784 rproc->recovery_disabled = true;
785
786 /*
787 * rproc_add will auto-boot the processor normally, but this is not
788 * desired with PRU client driven boot-flow methodology. A PRU
789 * application/client driver will boot the corresponding PRU
790 * remote-processor as part of its state machine either through the
791 * remoteproc sysfs interface or through the equivalent kernel API.
792 */
793 rproc->auto_boot = false;
794
795 pru = rproc->priv;
796 pru->dev = dev;
Suman Anna1d39f4d2020-12-08 15:10:01 +0100797 pru->data = data;
Suman Annad4ce2de2020-12-08 15:09:58 +0100798 pru->pruss = platform_get_drvdata(ppdev);
799 pru->rproc = rproc;
800 pru->fw_name = fw_name;
801
802 for (i = 0; i < ARRAY_SIZE(mem_names); i++) {
803 res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
804 mem_names[i]);
805 pru->mem_regions[i].va = devm_ioremap_resource(dev, res);
806 if (IS_ERR(pru->mem_regions[i].va)) {
807 dev_err(dev, "failed to parse and map memory resource %d %s\n",
808 i, mem_names[i]);
809 ret = PTR_ERR(pru->mem_regions[i].va);
810 return ret;
811 }
812 pru->mem_regions[i].pa = res->start;
813 pru->mem_regions[i].size = resource_size(res);
814
815 dev_dbg(dev, "memory %8s: pa %pa size 0x%zx va %pK\n",
816 mem_names[i], &pru->mem_regions[i].pa,
817 pru->mem_regions[i].size, pru->mem_regions[i].va);
818 }
819
820 ret = pru_rproc_set_id(pru);
821 if (ret < 0)
822 return ret;
823
824 platform_set_drvdata(pdev, rproc);
825
826 ret = devm_rproc_add(dev, pru->rproc);
827 if (ret) {
828 dev_err(dev, "rproc_add failed: %d\n", ret);
829 return ret;
830 }
831
Suman Anna20ad1de2020-12-08 15:10:00 +0100832 pru_rproc_create_debug_entries(rproc);
833
Suman Annad4ce2de2020-12-08 15:09:58 +0100834 dev_dbg(dev, "PRU rproc node %pOF probed successfully\n", np);
835
836 return 0;
837}
838
839static int pru_rproc_remove(struct platform_device *pdev)
840{
841 struct device *dev = &pdev->dev;
842 struct rproc *rproc = platform_get_drvdata(pdev);
843
844 dev_dbg(dev, "%s: removing rproc %s\n", __func__, rproc->name);
845
846 return 0;
847}
848
Suman Anna1d39f4d2020-12-08 15:10:01 +0100849static const struct pru_private_data pru_data = {
850 .type = PRU_TYPE_PRU,
851};
852
853static const struct pru_private_data k3_pru_data = {
854 .type = PRU_TYPE_PRU,
855 .is_k3 = 1,
856};
857
858static const struct pru_private_data k3_rtu_data = {
859 .type = PRU_TYPE_RTU,
860 .is_k3 = 1,
861};
862
863static const struct pru_private_data k3_tx_pru_data = {
864 .type = PRU_TYPE_TX_PRU,
865 .is_k3 = 1,
866};
867
Suman Annad4ce2de2020-12-08 15:09:58 +0100868static const struct of_device_id pru_rproc_match[] = {
Suman Anna1d39f4d2020-12-08 15:10:01 +0100869 { .compatible = "ti,am3356-pru", .data = &pru_data },
870 { .compatible = "ti,am4376-pru", .data = &pru_data },
871 { .compatible = "ti,am5728-pru", .data = &pru_data },
872 { .compatible = "ti,k2g-pru", .data = &pru_data },
873 { .compatible = "ti,am654-pru", .data = &k3_pru_data },
874 { .compatible = "ti,am654-rtu", .data = &k3_rtu_data },
875 { .compatible = "ti,am654-tx-pru", .data = &k3_tx_pru_data },
Suman Annab44786c2020-12-08 15:10:02 +0100876 { .compatible = "ti,j721e-pru", .data = &k3_pru_data },
877 { .compatible = "ti,j721e-rtu", .data = &k3_rtu_data },
878 { .compatible = "ti,j721e-tx-pru", .data = &k3_tx_pru_data },
Suman Annad4ce2de2020-12-08 15:09:58 +0100879 {},
880};
881MODULE_DEVICE_TABLE(of, pru_rproc_match);
882
883static struct platform_driver pru_rproc_driver = {
884 .driver = {
885 .name = "pru-rproc",
886 .of_match_table = pru_rproc_match,
887 .suppress_bind_attrs = true,
888 },
889 .probe = pru_rproc_probe,
890 .remove = pru_rproc_remove,
891};
892module_platform_driver(pru_rproc_driver);
893
894MODULE_AUTHOR("Suman Anna <s-anna@ti.com>");
895MODULE_AUTHOR("Andrew F. Davis <afd@ti.com>");
896MODULE_AUTHOR("Grzegorz Jaszczyk <grzegorz.jaszczyk@linaro.org>");
897MODULE_DESCRIPTION("PRU-ICSS Remote Processor Driver");
898MODULE_LICENSE("GPL v2");