xref: /linux/drivers/remoteproc/pru_rproc.c (revision 68a052239fc4b351e961f698b824f7654a346091)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * PRU-ICSS remoteproc driver for various TI SoCs
4  *
5  * Copyright (C) 2014-2022 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  *	Puranjay Mohan <p-mohan@ti.com>
12  *	Md Danish Anwar <danishanwar@ti.com>
13  */
14 
15 #include <linux/bitops.h>
16 #include <linux/debugfs.h>
17 #include <linux/irqdomain.h>
18 #include <linux/module.h>
19 #include <linux/of.h>
20 #include <linux/of_irq.h>
21 #include <linux/platform_device.h>
22 #include <linux/remoteproc/pruss.h>
23 #include <linux/pruss_driver.h>
24 #include <linux/remoteproc.h>
25 
26 #include "remoteproc_internal.h"
27 #include "remoteproc_elf_helpers.h"
28 #include "pru_rproc.h"
29 
30 /* PRU_ICSS_PRU_CTRL registers */
31 #define PRU_CTRL_CTRL		0x0000
32 #define PRU_CTRL_STS		0x0004
33 #define PRU_CTRL_WAKEUP_EN	0x0008
34 #define PRU_CTRL_CYCLE		0x000C
35 #define PRU_CTRL_STALL		0x0010
36 #define PRU_CTRL_CTBIR0		0x0020
37 #define PRU_CTRL_CTBIR1		0x0024
38 #define PRU_CTRL_CTPPR0		0x0028
39 #define PRU_CTRL_CTPPR1		0x002C
40 
41 /* CTRL register bit-fields */
42 #define CTRL_CTRL_SOFT_RST_N	BIT(0)
43 #define CTRL_CTRL_EN		BIT(1)
44 #define CTRL_CTRL_SLEEPING	BIT(2)
45 #define CTRL_CTRL_CTR_EN	BIT(3)
46 #define CTRL_CTRL_SINGLE_STEP	BIT(8)
47 #define CTRL_CTRL_RUNSTATE	BIT(15)
48 
49 /* PRU_ICSS_PRU_DEBUG registers */
50 #define PRU_DEBUG_GPREG(x)	(0x0000 + (x) * 4)
51 #define PRU_DEBUG_CT_REG(x)	(0x0080 + (x) * 4)
52 
53 /* PRU/RTU/Tx_PRU Core IRAM address masks */
54 #define PRU_IRAM_ADDR_MASK	0x3ffff
55 #define PRU0_IRAM_ADDR_MASK	0x34000
56 #define PRU1_IRAM_ADDR_MASK	0x38000
57 #define RTU0_IRAM_ADDR_MASK	0x4000
58 #define RTU1_IRAM_ADDR_MASK	0x6000
59 #define TX_PRU0_IRAM_ADDR_MASK	0xa000
60 #define TX_PRU1_IRAM_ADDR_MASK	0xc000
61 
62 /* PRU device addresses for various type of PRU RAMs */
63 #define PRU_IRAM_DA	0	/* Instruction RAM */
64 #define PRU_PDRAM_DA	0	/* Primary Data RAM */
65 #define PRU_SDRAM_DA	0x2000	/* Secondary Data RAM */
66 #define PRU_SHRDRAM_DA	0x10000 /* Shared Data RAM */
67 
68 #define MAX_PRU_SYS_EVENTS 160
69 
70 /**
71  * enum pru_iomem - PRU core memory/register range identifiers
72  *
73  * @PRU_IOMEM_IRAM: PRU Instruction RAM range
74  * @PRU_IOMEM_CTRL: PRU Control register range
75  * @PRU_IOMEM_DEBUG: PRU Debug register range
76  * @PRU_IOMEM_MAX: just keep this one at the end
77  */
78 enum pru_iomem {
79 	PRU_IOMEM_IRAM = 0,
80 	PRU_IOMEM_CTRL,
81 	PRU_IOMEM_DEBUG,
82 	PRU_IOMEM_MAX,
83 };
84 
85 /**
86  * struct pru_private_data - device data for a PRU core
87  * @type: type of the PRU core (PRU, RTU, Tx_PRU)
88  * @is_k3: flag used to identify the need for special load handling
89  */
90 struct pru_private_data {
91 	enum pru_type type;
92 	unsigned int is_k3 : 1;
93 };
94 
95 /**
96  * struct pru_rproc - PRU remoteproc structure
97  * @id: id of the PRU core within the PRUSS
98  * @dev: PRU core device pointer
99  * @pruss: back-reference to parent PRUSS structure
100  * @rproc: remoteproc pointer for this PRU core
101  * @data: PRU core specific data
102  * @mem_regions: data for each of the PRU memory regions
103  * @client_np: client device node
104  * @lock: mutex to protect client usage
105  * @fw_name: name of firmware image used during loading
106  * @mapped_irq: virtual interrupt numbers of created fw specific mapping
107  * @pru_interrupt_map: pointer to interrupt mapping description (firmware)
108  * @pru_interrupt_map_sz: pru_interrupt_map size
109  * @rmw_lock: lock for read, modify, write operations on registers
110  * @dbg_single_step: debug state variable to set PRU into single step mode
111  * @dbg_continuous: debug state variable to restore PRU execution mode
112  * @evt_count: number of mapped events
113  * @gpmux_save: saved value for gpmux config
114  */
115 struct pru_rproc {
116 	int id;
117 	struct device *dev;
118 	struct pruss *pruss;
119 	struct rproc *rproc;
120 	const struct pru_private_data *data;
121 	struct pruss_mem_region mem_regions[PRU_IOMEM_MAX];
122 	struct device_node *client_np;
123 	struct mutex lock;
124 	const char *fw_name;
125 	unsigned int *mapped_irq;
126 	struct pru_irq_rsc *pru_interrupt_map;
127 	size_t pru_interrupt_map_sz;
128 	spinlock_t rmw_lock;
129 	u32 dbg_single_step;
130 	u32 dbg_continuous;
131 	u8 evt_count;
132 	u8 gpmux_save;
133 };
134 
135 static inline u32 pru_control_read_reg(struct pru_rproc *pru, unsigned int reg)
136 {
137 	return readl_relaxed(pru->mem_regions[PRU_IOMEM_CTRL].va + reg);
138 }
139 
140 static inline
141 void pru_control_write_reg(struct pru_rproc *pru, unsigned int reg, u32 val)
142 {
143 	writel_relaxed(val, pru->mem_regions[PRU_IOMEM_CTRL].va + reg);
144 }
145 
146 static inline
147 void pru_control_set_reg(struct pru_rproc *pru, unsigned int reg,
148 			 u32 mask, u32 set)
149 {
150 	u32 val;
151 	unsigned long flags;
152 
153 	spin_lock_irqsave(&pru->rmw_lock, flags);
154 
155 	val = pru_control_read_reg(pru, reg);
156 	val &= ~mask;
157 	val |= (set & mask);
158 	pru_control_write_reg(pru, reg, val);
159 
160 	spin_unlock_irqrestore(&pru->rmw_lock, flags);
161 }
162 
163 /**
164  * pru_rproc_set_firmware() - set firmware for a PRU core
165  * @rproc: the rproc instance of the PRU
166  * @fw_name: the new firmware name, or NULL if default is desired
167  *
168  * Return: 0 on success, or errno in error case.
169  */
170 static int pru_rproc_set_firmware(struct rproc *rproc, const char *fw_name)
171 {
172 	struct pru_rproc *pru = rproc->priv;
173 
174 	if (!fw_name)
175 		fw_name = pru->fw_name;
176 
177 	return rproc_set_firmware(rproc, fw_name);
178 }
179 
180 static struct rproc *__pru_rproc_get(struct device_node *np, int index)
181 {
182 	struct rproc *rproc;
183 	phandle rproc_phandle;
184 	int ret;
185 
186 	ret = of_property_read_u32_index(np, "ti,prus", index, &rproc_phandle);
187 	if (ret)
188 		return ERR_PTR(ret);
189 
190 	rproc = rproc_get_by_phandle(rproc_phandle);
191 	if (!rproc) {
192 		ret = -EPROBE_DEFER;
193 		return ERR_PTR(ret);
194 	}
195 
196 	/* make sure it is PRU rproc */
197 	if (!is_pru_rproc(rproc->dev.parent)) {
198 		rproc_put(rproc);
199 		return ERR_PTR(-ENODEV);
200 	}
201 
202 	return rproc;
203 }
204 
205 /**
206  * pru_rproc_get() - get the PRU rproc instance from a device node
207  * @np: the user/client device node
208  * @index: index to use for the ti,prus property
209  * @pru_id: optional pointer to return the PRU remoteproc processor id
210  *
211  * This function looks through a client device node's "ti,prus" property at
212  * index @index and returns the rproc handle for a valid PRU remote processor if
213  * found. The function allows only one user to own the PRU rproc resource at a
214  * time. Caller must call pru_rproc_put() when done with using the rproc, not
215  * required if the function returns a failure.
216  *
217  * When optional @pru_id pointer is passed the PRU remoteproc processor id is
218  * returned.
219  *
220  * Return: rproc handle on success, and an ERR_PTR on failure using one
221  * of the following error values
222  *    -ENODEV if device is not found
223  *    -EBUSY if PRU is already acquired by anyone
224  *    -EPROBE_DEFER is PRU device is not probed yet
225  */
226 struct rproc *pru_rproc_get(struct device_node *np, int index,
227 			    enum pruss_pru_id *pru_id)
228 {
229 	struct rproc *rproc;
230 	struct pru_rproc *pru;
231 	struct device *dev;
232 	const char *fw_name;
233 	int ret;
234 	u32 mux;
235 
236 	rproc = __pru_rproc_get(np, index);
237 	if (IS_ERR(rproc))
238 		return rproc;
239 
240 	pru = rproc->priv;
241 	dev = &rproc->dev;
242 
243 	mutex_lock(&pru->lock);
244 
245 	if (pru->client_np) {
246 		mutex_unlock(&pru->lock);
247 		ret = -EBUSY;
248 		goto err_no_rproc_handle;
249 	}
250 
251 	pru->client_np = np;
252 	rproc->sysfs_read_only = true;
253 
254 	mutex_unlock(&pru->lock);
255 
256 	if (pru_id)
257 		*pru_id = pru->id;
258 
259 	ret = pruss_cfg_get_gpmux(pru->pruss, pru->id, &pru->gpmux_save);
260 	if (ret) {
261 		dev_err(dev, "failed to get cfg gpmux: %d\n", ret);
262 		goto err;
263 	}
264 
265 	/* An error here is acceptable for backward compatibility */
266 	ret = of_property_read_u32_index(np, "ti,pruss-gp-mux-sel", index,
267 					 &mux);
268 	if (!ret) {
269 		ret = pruss_cfg_set_gpmux(pru->pruss, pru->id, mux);
270 		if (ret) {
271 			dev_err(dev, "failed to set cfg gpmux: %d\n", ret);
272 			goto err;
273 		}
274 	}
275 
276 	ret = of_property_read_string_index(np, "firmware-name", index,
277 					    &fw_name);
278 	if (!ret) {
279 		ret = pru_rproc_set_firmware(rproc, fw_name);
280 		if (ret) {
281 			dev_err(dev, "failed to set firmware: %d\n", ret);
282 			goto err;
283 		}
284 	}
285 
286 	return rproc;
287 
288 err_no_rproc_handle:
289 	rproc_put(rproc);
290 	return ERR_PTR(ret);
291 
292 err:
293 	pru_rproc_put(rproc);
294 	return ERR_PTR(ret);
295 }
296 EXPORT_SYMBOL_GPL(pru_rproc_get);
297 
298 /**
299  * pru_rproc_put() - release the PRU rproc resource
300  * @rproc: the rproc resource to release
301  *
302  * Releases the PRU rproc resource and makes it available to other
303  * users.
304  */
305 void pru_rproc_put(struct rproc *rproc)
306 {
307 	struct pru_rproc *pru;
308 
309 	if (IS_ERR_OR_NULL(rproc) || !is_pru_rproc(rproc->dev.parent))
310 		return;
311 
312 	pru = rproc->priv;
313 
314 	pruss_cfg_set_gpmux(pru->pruss, pru->id, pru->gpmux_save);
315 
316 	pru_rproc_set_firmware(rproc, NULL);
317 
318 	mutex_lock(&pru->lock);
319 
320 	if (!pru->client_np) {
321 		mutex_unlock(&pru->lock);
322 		return;
323 	}
324 
325 	pru->client_np = NULL;
326 	rproc->sysfs_read_only = false;
327 	mutex_unlock(&pru->lock);
328 
329 	rproc_put(rproc);
330 }
331 EXPORT_SYMBOL_GPL(pru_rproc_put);
332 
333 /**
334  * pru_rproc_set_ctable() - set the constant table index for the PRU
335  * @rproc: the rproc instance of the PRU
336  * @c: constant table index to set
337  * @addr: physical address to set it to
338  *
339  * Return: 0 on success, or errno in error case.
340  */
341 int pru_rproc_set_ctable(struct rproc *rproc, enum pru_ctable_idx c, u32 addr)
342 {
343 	struct pru_rproc *pru;
344 	unsigned int reg;
345 	u32 mask, set;
346 	u16 idx;
347 	u16 idx_mask;
348 
349 	if (IS_ERR_OR_NULL(rproc))
350 		return -EINVAL;
351 
352 	if (!rproc->dev.parent || !is_pru_rproc(rproc->dev.parent))
353 		return -ENODEV;
354 
355 	pru = rproc->priv;
356 	/* pointer is 16 bit and index is 8-bit so mask out the rest */
357 	idx_mask = (c >= PRU_C28) ? 0xFFFF : 0xFF;
358 
359 	/* ctable uses bit 8 and upwards only */
360 	idx = (addr >> 8) & idx_mask;
361 
362 	/* configurable ctable (i.e. C24) starts at PRU_CTRL_CTBIR0 */
363 	reg = PRU_CTRL_CTBIR0 + 4 * (c >> 1);
364 	mask = idx_mask << (16 * (c & 1));
365 	set = idx << (16 * (c & 1));
366 
367 	pru_control_set_reg(pru, reg, mask, set);
368 
369 	return 0;
370 }
371 EXPORT_SYMBOL_GPL(pru_rproc_set_ctable);
372 
373 static inline u32 pru_debug_read_reg(struct pru_rproc *pru, unsigned int reg)
374 {
375 	return readl_relaxed(pru->mem_regions[PRU_IOMEM_DEBUG].va + reg);
376 }
377 
378 static int regs_show(struct seq_file *s, void *data)
379 {
380 	struct rproc *rproc = s->private;
381 	struct pru_rproc *pru = rproc->priv;
382 	int i, nregs = 32;
383 	u32 pru_sts;
384 	int pru_is_running;
385 
386 	seq_puts(s, "============== Control Registers ==============\n");
387 	seq_printf(s, "CTRL      := 0x%08x\n",
388 		   pru_control_read_reg(pru, PRU_CTRL_CTRL));
389 	pru_sts = pru_control_read_reg(pru, PRU_CTRL_STS);
390 	seq_printf(s, "STS (PC)  := 0x%08x (0x%08x)\n", pru_sts, pru_sts << 2);
391 	seq_printf(s, "WAKEUP_EN := 0x%08x\n",
392 		   pru_control_read_reg(pru, PRU_CTRL_WAKEUP_EN));
393 	seq_printf(s, "CYCLE     := 0x%08x\n",
394 		   pru_control_read_reg(pru, PRU_CTRL_CYCLE));
395 	seq_printf(s, "STALL     := 0x%08x\n",
396 		   pru_control_read_reg(pru, PRU_CTRL_STALL));
397 	seq_printf(s, "CTBIR0    := 0x%08x\n",
398 		   pru_control_read_reg(pru, PRU_CTRL_CTBIR0));
399 	seq_printf(s, "CTBIR1    := 0x%08x\n",
400 		   pru_control_read_reg(pru, PRU_CTRL_CTBIR1));
401 	seq_printf(s, "CTPPR0    := 0x%08x\n",
402 		   pru_control_read_reg(pru, PRU_CTRL_CTPPR0));
403 	seq_printf(s, "CTPPR1    := 0x%08x\n",
404 		   pru_control_read_reg(pru, PRU_CTRL_CTPPR1));
405 
406 	seq_puts(s, "=============== Debug Registers ===============\n");
407 	pru_is_running = pru_control_read_reg(pru, PRU_CTRL_CTRL) &
408 				CTRL_CTRL_RUNSTATE;
409 	if (pru_is_running) {
410 		seq_puts(s, "PRU is executing, cannot print/access debug registers.\n");
411 		return 0;
412 	}
413 
414 	for (i = 0; i < nregs; i++) {
415 		seq_printf(s, "GPREG%-2d := 0x%08x\tCT_REG%-2d := 0x%08x\n",
416 			   i, pru_debug_read_reg(pru, PRU_DEBUG_GPREG(i)),
417 			   i, pru_debug_read_reg(pru, PRU_DEBUG_CT_REG(i)));
418 	}
419 
420 	return 0;
421 }
422 DEFINE_SHOW_ATTRIBUTE(regs);
423 
424 /*
425  * Control PRU single-step mode
426  *
427  * This is a debug helper function used for controlling the single-step
428  * mode of the PRU. The PRU Debug registers are not accessible when the
429  * PRU is in RUNNING state.
430  *
431  * Writing a non-zero value sets the PRU into single-step mode irrespective
432  * of its previous state. The PRU mode is saved only on the first set into
433  * a single-step mode. Writing a zero value will restore the PRU into its
434  * original mode.
435  */
436 static int pru_rproc_debug_ss_set(void *data, u64 val)
437 {
438 	struct rproc *rproc = data;
439 	struct pru_rproc *pru = rproc->priv;
440 	u32 reg_val;
441 
442 	val = val ? 1 : 0;
443 	if (!val && !pru->dbg_single_step)
444 		return 0;
445 
446 	reg_val = pru_control_read_reg(pru, PRU_CTRL_CTRL);
447 
448 	if (val && !pru->dbg_single_step)
449 		pru->dbg_continuous = reg_val;
450 
451 	if (val)
452 		reg_val |= CTRL_CTRL_SINGLE_STEP | CTRL_CTRL_EN;
453 	else
454 		reg_val = pru->dbg_continuous;
455 
456 	pru->dbg_single_step = val;
457 	pru_control_write_reg(pru, PRU_CTRL_CTRL, reg_val);
458 
459 	return 0;
460 }
461 
462 static int pru_rproc_debug_ss_get(void *data, u64 *val)
463 {
464 	struct rproc *rproc = data;
465 	struct pru_rproc *pru = rproc->priv;
466 
467 	*val = pru->dbg_single_step;
468 
469 	return 0;
470 }
471 DEFINE_DEBUGFS_ATTRIBUTE(pru_rproc_debug_ss_fops, pru_rproc_debug_ss_get,
472 			 pru_rproc_debug_ss_set, "%llu\n");
473 
474 /*
475  * Create PRU-specific debugfs entries
476  *
477  * The entries are created only if the parent remoteproc debugfs directory
478  * exists, and will be cleaned up by the remoteproc core.
479  */
480 static void pru_rproc_create_debug_entries(struct rproc *rproc)
481 {
482 	if (!rproc->dbg_dir)
483 		return;
484 
485 	debugfs_create_file("regs", 0400, rproc->dbg_dir,
486 			    rproc, &regs_fops);
487 	debugfs_create_file("single_step", 0600, rproc->dbg_dir,
488 			    rproc, &pru_rproc_debug_ss_fops);
489 }
490 
491 static void pru_dispose_irq_mapping(struct pru_rproc *pru)
492 {
493 	if (!pru->mapped_irq)
494 		return;
495 
496 	while (pru->evt_count) {
497 		pru->evt_count--;
498 		if (pru->mapped_irq[pru->evt_count] > 0)
499 			irq_dispose_mapping(pru->mapped_irq[pru->evt_count]);
500 	}
501 
502 	kfree(pru->mapped_irq);
503 	pru->mapped_irq = NULL;
504 }
505 
506 /*
507  * Parse the custom PRU interrupt map resource and configure the INTC
508  * appropriately.
509  */
510 static int pru_handle_intrmap(struct rproc *rproc)
511 {
512 	struct device *dev = rproc->dev.parent;
513 	struct pru_rproc *pru = rproc->priv;
514 	struct pru_irq_rsc *rsc = pru->pru_interrupt_map;
515 	struct irq_fwspec fwspec;
516 	struct device_node *parent, *irq_parent;
517 	int i, ret = 0;
518 
519 	/* not having pru_interrupt_map is not an error */
520 	if (!rsc)
521 		return 0;
522 
523 	/* currently supporting only type 0 */
524 	if (rsc->type != 0) {
525 		dev_err(dev, "unsupported rsc type: %d\n", rsc->type);
526 		return -EINVAL;
527 	}
528 
529 	if (rsc->num_evts > MAX_PRU_SYS_EVENTS)
530 		return -EINVAL;
531 
532 	if (sizeof(*rsc) + rsc->num_evts * sizeof(struct pruss_int_map) !=
533 	    pru->pru_interrupt_map_sz)
534 		return -EINVAL;
535 
536 	pru->evt_count = rsc->num_evts;
537 	pru->mapped_irq = kcalloc(pru->evt_count, sizeof(unsigned int),
538 				  GFP_KERNEL);
539 	if (!pru->mapped_irq) {
540 		pru->evt_count = 0;
541 		return -ENOMEM;
542 	}
543 
544 	/*
545 	 * parse and fill in system event to interrupt channel and
546 	 * channel-to-host mapping. The interrupt controller to be used
547 	 * for these mappings for a given PRU remoteproc is always its
548 	 * corresponding sibling PRUSS INTC node.
549 	 */
550 	parent = of_get_parent(dev_of_node(pru->dev));
551 	if (!parent) {
552 		kfree(pru->mapped_irq);
553 		pru->mapped_irq = NULL;
554 		pru->evt_count = 0;
555 		return -ENODEV;
556 	}
557 
558 	irq_parent = of_get_child_by_name(parent, "interrupt-controller");
559 	of_node_put(parent);
560 	if (!irq_parent) {
561 		kfree(pru->mapped_irq);
562 		pru->mapped_irq = NULL;
563 		pru->evt_count = 0;
564 		return -ENODEV;
565 	}
566 
567 	fwspec.fwnode = of_fwnode_handle(irq_parent);
568 	fwspec.param_count = 3;
569 	for (i = 0; i < pru->evt_count; i++) {
570 		fwspec.param[0] = rsc->pru_intc_map[i].event;
571 		fwspec.param[1] = rsc->pru_intc_map[i].chnl;
572 		fwspec.param[2] = rsc->pru_intc_map[i].host;
573 
574 		dev_dbg(dev, "mapping%d: event %d, chnl %d, host %d\n",
575 			i, fwspec.param[0], fwspec.param[1], fwspec.param[2]);
576 
577 		pru->mapped_irq[i] = irq_create_fwspec_mapping(&fwspec);
578 		if (!pru->mapped_irq[i]) {
579 			dev_err(dev, "failed to get virq for fw mapping %d: event %d chnl %d host %d\n",
580 				i, fwspec.param[0], fwspec.param[1],
581 				fwspec.param[2]);
582 			ret = -EINVAL;
583 			goto map_fail;
584 		}
585 	}
586 	of_node_put(irq_parent);
587 
588 	return ret;
589 
590 map_fail:
591 	pru_dispose_irq_mapping(pru);
592 	of_node_put(irq_parent);
593 
594 	return ret;
595 }
596 
597 static int pru_rproc_start(struct rproc *rproc)
598 {
599 	struct device *dev = &rproc->dev;
600 	struct pru_rproc *pru = rproc->priv;
601 	const char *names[PRU_TYPE_MAX] = { "PRU", "RTU", "Tx_PRU" };
602 	u32 val;
603 	int ret;
604 
605 	dev_dbg(dev, "starting %s%d: entry-point = 0x%llx\n",
606 		names[pru->data->type], pru->id, (rproc->bootaddr >> 2));
607 
608 	ret = pru_handle_intrmap(rproc);
609 	/*
610 	 * reset references to pru interrupt map - they will stop being valid
611 	 * after rproc_start returns
612 	 */
613 	pru->pru_interrupt_map = NULL;
614 	pru->pru_interrupt_map_sz = 0;
615 	if (ret)
616 		return ret;
617 
618 	val = CTRL_CTRL_EN | ((rproc->bootaddr >> 2) << 16);
619 	pru_control_write_reg(pru, PRU_CTRL_CTRL, val);
620 
621 	return 0;
622 }
623 
624 static int pru_rproc_stop(struct rproc *rproc)
625 {
626 	struct device *dev = &rproc->dev;
627 	struct pru_rproc *pru = rproc->priv;
628 	const char *names[PRU_TYPE_MAX] = { "PRU", "RTU", "Tx_PRU" };
629 	u32 val;
630 
631 	dev_dbg(dev, "stopping %s%d\n", names[pru->data->type], pru->id);
632 
633 	val = pru_control_read_reg(pru, PRU_CTRL_CTRL);
634 	val &= ~CTRL_CTRL_EN;
635 	pru_control_write_reg(pru, PRU_CTRL_CTRL, val);
636 
637 	/* dispose irq mapping - new firmware can provide new mapping */
638 	pru_dispose_irq_mapping(pru);
639 
640 	return 0;
641 }
642 
643 /*
644  * Convert PRU device address (data spaces only) to kernel virtual address.
645  *
646  * Each PRU has access to all data memories within the PRUSS, accessible at
647  * different ranges. So, look through both its primary and secondary Data
648  * RAMs as well as any shared Data RAM to convert a PRU device address to
649  * kernel virtual address. Data RAM0 is primary Data RAM for PRU0 and Data
650  * RAM1 is primary Data RAM for PRU1.
651  */
652 static void *pru_d_da_to_va(struct pru_rproc *pru, u32 da, size_t len)
653 {
654 	struct pruss_mem_region dram0, dram1, shrd_ram;
655 	struct pruss *pruss = pru->pruss;
656 	u32 offset;
657 	void *va = NULL;
658 
659 	if (len == 0)
660 		return NULL;
661 
662 	dram0 = pruss->mem_regions[PRUSS_MEM_DRAM0];
663 	dram1 = pruss->mem_regions[PRUSS_MEM_DRAM1];
664 	/* PRU1 has its local RAM addresses reversed */
665 	if (pru->id == PRUSS_PRU1)
666 		swap(dram0, dram1);
667 	shrd_ram = pruss->mem_regions[PRUSS_MEM_SHRD_RAM2];
668 
669 	if (da + len <= PRU_PDRAM_DA + dram0.size) {
670 		offset = da - PRU_PDRAM_DA;
671 		va = (__force void *)(dram0.va + offset);
672 	} else if (da >= PRU_SDRAM_DA &&
673 		   da + len <= PRU_SDRAM_DA + dram1.size) {
674 		offset = da - PRU_SDRAM_DA;
675 		va = (__force void *)(dram1.va + offset);
676 	} else if (da >= PRU_SHRDRAM_DA &&
677 		   da + len <= PRU_SHRDRAM_DA + shrd_ram.size) {
678 		offset = da - PRU_SHRDRAM_DA;
679 		va = (__force void *)(shrd_ram.va + offset);
680 	}
681 
682 	return va;
683 }
684 
685 /*
686  * Convert PRU device address (instruction space) to kernel virtual address.
687  *
688  * A PRU does not have an unified address space. Each PRU has its very own
689  * private Instruction RAM, and its device address is identical to that of
690  * its primary Data RAM device address.
691  */
692 static void *pru_i_da_to_va(struct pru_rproc *pru, u32 da, size_t len)
693 {
694 	u32 offset;
695 	void *va = NULL;
696 
697 	if (len == 0)
698 		return NULL;
699 
700 	/*
701 	 * GNU binutils do not support multiple address spaces. The GNU
702 	 * linker's default linker script places IRAM at an arbitrary high
703 	 * offset, in order to differentiate it from DRAM. Hence we need to
704 	 * strip the artificial offset in the IRAM addresses coming from the
705 	 * ELF file.
706 	 *
707 	 * The TI proprietary linker would never set those higher IRAM address
708 	 * bits anyway. PRU architecture limits the program counter to 16-bit
709 	 * word-address range. This in turn corresponds to 18-bit IRAM
710 	 * byte-address range for ELF.
711 	 *
712 	 * Two more bits are added just in case to make the final 20-bit mask.
713 	 * Idea is to have a safeguard in case TI decides to add banking
714 	 * in future SoCs.
715 	 */
716 	da &= 0xfffff;
717 
718 	if (da + len <= PRU_IRAM_DA + pru->mem_regions[PRU_IOMEM_IRAM].size) {
719 		offset = da - PRU_IRAM_DA;
720 		va = (__force void *)(pru->mem_regions[PRU_IOMEM_IRAM].va +
721 				      offset);
722 	}
723 
724 	return va;
725 }
726 
727 /*
728  * Provide address translations for only PRU Data RAMs through the remoteproc
729  * core for any PRU client drivers. The PRU Instruction RAM access is restricted
730  * only to the PRU loader code.
731  */
732 static void *pru_rproc_da_to_va(struct rproc *rproc, u64 da, size_t len, bool *is_iomem)
733 {
734 	struct pru_rproc *pru = rproc->priv;
735 
736 	return pru_d_da_to_va(pru, da, len);
737 }
738 
739 /* PRU-specific address translator used by PRU loader. */
740 static void *pru_da_to_va(struct rproc *rproc, u64 da, size_t len, bool is_iram)
741 {
742 	struct pru_rproc *pru = rproc->priv;
743 	void *va;
744 
745 	if (is_iram)
746 		va = pru_i_da_to_va(pru, da, len);
747 	else
748 		va = pru_d_da_to_va(pru, da, len);
749 
750 	return va;
751 }
752 
753 static struct rproc_ops pru_rproc_ops = {
754 	.start		= pru_rproc_start,
755 	.stop		= pru_rproc_stop,
756 	.da_to_va	= pru_rproc_da_to_va,
757 };
758 
759 /*
760  * Custom memory copy implementation for ICSSG PRU/RTU/Tx_PRU Cores
761  *
762  * The ICSSG PRU/RTU/Tx_PRU cores have a memory copying issue with IRAM
763  * memories, that is not seen on previous generation SoCs. The data is reflected
764  * properly in the IRAM memories only for integer (4-byte) copies. Any unaligned
765  * copies result in all the other pre-existing bytes zeroed out within that
766  * 4-byte boundary, thereby resulting in wrong text/code in the IRAMs. Also, the
767  * IRAM memory port interface does not allow any 8-byte copies (as commonly used
768  * by ARM64 memcpy implementation) and throws an exception. The DRAM memory
769  * ports do not show this behavior.
770  */
771 static int pru_rproc_memcpy(void *dest, const void *src, size_t count)
772 {
773 	const u32 *s = src;
774 	u32 *d = dest;
775 	size_t size = count / 4;
776 	u32 *tmp_src = NULL;
777 
778 	/*
779 	 * TODO: relax limitation of 4-byte aligned dest addresses and copy
780 	 * sizes
781 	 */
782 	if ((long)dest % 4 || count % 4)
783 		return -EINVAL;
784 
785 	/* src offsets in ELF firmware image can be non-aligned */
786 	if ((long)src % 4) {
787 		tmp_src = kmemdup(src, count, GFP_KERNEL);
788 		if (!tmp_src)
789 			return -ENOMEM;
790 		s = tmp_src;
791 	}
792 
793 	while (size--)
794 		*d++ = *s++;
795 
796 	kfree(tmp_src);
797 
798 	return 0;
799 }
800 
801 static int
802 pru_rproc_load_elf_segments(struct rproc *rproc, const struct firmware *fw)
803 {
804 	struct pru_rproc *pru = rproc->priv;
805 	struct device *dev = &rproc->dev;
806 	struct elf32_hdr *ehdr;
807 	struct elf32_phdr *phdr;
808 	int i, ret = 0;
809 	const u8 *elf_data = fw->data;
810 
811 	ehdr = (struct elf32_hdr *)elf_data;
812 	phdr = (struct elf32_phdr *)(elf_data + ehdr->e_phoff);
813 
814 	/* go through the available ELF segments */
815 	for (i = 0; i < ehdr->e_phnum; i++, phdr++) {
816 		u32 da = phdr->p_paddr;
817 		u32 memsz = phdr->p_memsz;
818 		u32 filesz = phdr->p_filesz;
819 		u32 offset = phdr->p_offset;
820 		bool is_iram;
821 		void *ptr;
822 
823 		if (phdr->p_type != PT_LOAD || !filesz)
824 			continue;
825 
826 		dev_dbg(dev, "phdr: type %d da 0x%x memsz 0x%x filesz 0x%x\n",
827 			phdr->p_type, da, memsz, filesz);
828 
829 		if (filesz > memsz) {
830 			dev_err(dev, "bad phdr filesz 0x%x memsz 0x%x\n",
831 				filesz, memsz);
832 			ret = -EINVAL;
833 			break;
834 		}
835 
836 		if (offset + filesz > fw->size) {
837 			dev_err(dev, "truncated fw: need 0x%x avail 0x%zx\n",
838 				offset + filesz, fw->size);
839 			ret = -EINVAL;
840 			break;
841 		}
842 
843 		/* grab the kernel address for this device address */
844 		is_iram = phdr->p_flags & PF_X;
845 		ptr = pru_da_to_va(rproc, da, memsz, is_iram);
846 		if (!ptr) {
847 			dev_err(dev, "bad phdr da 0x%x mem 0x%x\n", da, memsz);
848 			ret = -EINVAL;
849 			break;
850 		}
851 
852 		if (pru->data->is_k3) {
853 			ret = pru_rproc_memcpy(ptr, elf_data + phdr->p_offset,
854 					       filesz);
855 			if (ret) {
856 				dev_err(dev, "PRU memory copy failed for da 0x%x memsz 0x%x\n",
857 					da, memsz);
858 				break;
859 			}
860 		} else {
861 			memcpy(ptr, elf_data + phdr->p_offset, filesz);
862 		}
863 
864 		/* skip the memzero logic performed by remoteproc ELF loader */
865 	}
866 
867 	return ret;
868 }
869 
870 static const void *
871 pru_rproc_find_interrupt_map(struct device *dev, const struct firmware *fw)
872 {
873 	struct elf32_shdr *shdr, *name_table_shdr;
874 	const char *name_table;
875 	const u8 *elf_data = fw->data;
876 	struct elf32_hdr *ehdr = (struct elf32_hdr *)elf_data;
877 	u16 shnum = ehdr->e_shnum;
878 	u16 shstrndx = ehdr->e_shstrndx;
879 	int i;
880 
881 	/* first, get the section header */
882 	shdr = (struct elf32_shdr *)(elf_data + ehdr->e_shoff);
883 	/* compute name table section header entry in shdr array */
884 	name_table_shdr = shdr + shstrndx;
885 	/* finally, compute the name table section address in elf */
886 	name_table = elf_data + name_table_shdr->sh_offset;
887 
888 	for (i = 0; i < shnum; i++, shdr++) {
889 		u32 size = shdr->sh_size;
890 		u32 offset = shdr->sh_offset;
891 		u32 name = shdr->sh_name;
892 
893 		if (strcmp(name_table + name, ".pru_irq_map"))
894 			continue;
895 
896 		/* make sure we have the entire irq map */
897 		if (offset + size > fw->size || offset + size < size) {
898 			dev_err(dev, ".pru_irq_map section truncated\n");
899 			return ERR_PTR(-EINVAL);
900 		}
901 
902 		/* make sure irq map has at least the header */
903 		if (sizeof(struct pru_irq_rsc) > size) {
904 			dev_err(dev, "header-less .pru_irq_map section\n");
905 			return ERR_PTR(-EINVAL);
906 		}
907 
908 		return shdr;
909 	}
910 
911 	dev_dbg(dev, "no .pru_irq_map section found for this fw\n");
912 
913 	return NULL;
914 }
915 
916 /*
917  * Use a custom parse_fw callback function for dealing with PRU firmware
918  * specific sections.
919  *
920  * The firmware blob can contain optional ELF sections: .resource_table section
921  * and .pru_irq_map one. The second one contains the PRUSS interrupt mapping
922  * description, which needs to be setup before powering on the PRU core. To
923  * avoid RAM wastage this ELF section is not mapped to any ELF segment (by the
924  * firmware linker) and therefore is not loaded to PRU memory.
925  */
926 static int pru_rproc_parse_fw(struct rproc *rproc, const struct firmware *fw)
927 {
928 	struct device *dev = &rproc->dev;
929 	struct pru_rproc *pru = rproc->priv;
930 	const u8 *elf_data = fw->data;
931 	const void *shdr;
932 	u8 class = fw_elf_get_class(fw);
933 	u64 sh_offset;
934 	int ret;
935 
936 	/* load optional rsc table */
937 	ret = rproc_elf_load_rsc_table(rproc, fw);
938 	if (ret == -EINVAL)
939 		dev_dbg(&rproc->dev, "no resource table found for this fw\n");
940 	else if (ret)
941 		return ret;
942 
943 	/* find .pru_interrupt_map section, not having it is not an error */
944 	shdr = pru_rproc_find_interrupt_map(dev, fw);
945 	if (IS_ERR(shdr))
946 		return PTR_ERR(shdr);
947 
948 	if (!shdr)
949 		return 0;
950 
951 	/* preserve pointer to PRU interrupt map together with it size */
952 	sh_offset = elf_shdr_get_sh_offset(class, shdr);
953 	pru->pru_interrupt_map = (struct pru_irq_rsc *)(elf_data + sh_offset);
954 	pru->pru_interrupt_map_sz = elf_shdr_get_sh_size(class, shdr);
955 
956 	return 0;
957 }
958 
959 /*
960  * Compute PRU id based on the IRAM addresses. The PRU IRAMs are
961  * always at a particular offset within the PRUSS address space.
962  */
963 static int pru_rproc_set_id(struct pru_rproc *pru)
964 {
965 	int ret = 0;
966 
967 	switch (pru->mem_regions[PRU_IOMEM_IRAM].pa & PRU_IRAM_ADDR_MASK) {
968 	case TX_PRU0_IRAM_ADDR_MASK:
969 		fallthrough;
970 	case RTU0_IRAM_ADDR_MASK:
971 		fallthrough;
972 	case PRU0_IRAM_ADDR_MASK:
973 		pru->id = PRUSS_PRU0;
974 		break;
975 	case TX_PRU1_IRAM_ADDR_MASK:
976 		fallthrough;
977 	case RTU1_IRAM_ADDR_MASK:
978 		fallthrough;
979 	case PRU1_IRAM_ADDR_MASK:
980 		pru->id = PRUSS_PRU1;
981 		break;
982 	default:
983 		ret = -EINVAL;
984 	}
985 
986 	return ret;
987 }
988 
989 static int pru_rproc_probe(struct platform_device *pdev)
990 {
991 	struct device *dev = &pdev->dev;
992 	struct device_node *np = dev->of_node;
993 	struct platform_device *ppdev = to_platform_device(dev->parent);
994 	struct pru_rproc *pru;
995 	const char *fw_name;
996 	struct rproc *rproc = NULL;
997 	struct resource *res;
998 	int i, ret;
999 	const struct pru_private_data *data;
1000 	const char *mem_names[PRU_IOMEM_MAX] = { "iram", "control", "debug" };
1001 
1002 	data = of_device_get_match_data(&pdev->dev);
1003 	if (!data)
1004 		return -ENODEV;
1005 
1006 	ret = of_property_read_string(np, "firmware-name", &fw_name);
1007 	if (ret) {
1008 		dev_err(dev, "unable to retrieve firmware-name %d\n", ret);
1009 		return ret;
1010 	}
1011 
1012 	rproc = devm_rproc_alloc(dev, pdev->name, &pru_rproc_ops, fw_name,
1013 				 sizeof(*pru));
1014 	if (!rproc) {
1015 		dev_err(dev, "rproc_alloc failed\n");
1016 		return -ENOMEM;
1017 	}
1018 	/* use a custom load function to deal with PRU-specific quirks */
1019 	rproc->ops->load = pru_rproc_load_elf_segments;
1020 
1021 	/* use a custom parse function to deal with PRU-specific resources */
1022 	rproc->ops->parse_fw = pru_rproc_parse_fw;
1023 
1024 	/* error recovery is not supported for PRUs */
1025 	rproc->recovery_disabled = true;
1026 
1027 	/*
1028 	 * rproc_add will auto-boot the processor normally, but this is not
1029 	 * desired with PRU client driven boot-flow methodology. A PRU
1030 	 * application/client driver will boot the corresponding PRU
1031 	 * remote-processor as part of its state machine either through the
1032 	 * remoteproc sysfs interface or through the equivalent kernel API.
1033 	 */
1034 	rproc->auto_boot = false;
1035 
1036 	pru = rproc->priv;
1037 	pru->dev = dev;
1038 	pru->data = data;
1039 	pru->pruss = platform_get_drvdata(ppdev);
1040 	pru->rproc = rproc;
1041 	pru->fw_name = fw_name;
1042 	pru->client_np = NULL;
1043 	spin_lock_init(&pru->rmw_lock);
1044 	mutex_init(&pru->lock);
1045 
1046 	for (i = 0; i < ARRAY_SIZE(mem_names); i++) {
1047 		res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
1048 						   mem_names[i]);
1049 		pru->mem_regions[i].va = devm_ioremap_resource(dev, res);
1050 		if (IS_ERR(pru->mem_regions[i].va)) {
1051 			dev_err(dev, "failed to parse and map memory resource %d %s\n",
1052 				i, mem_names[i]);
1053 			ret = PTR_ERR(pru->mem_regions[i].va);
1054 			return ret;
1055 		}
1056 		pru->mem_regions[i].pa = res->start;
1057 		pru->mem_regions[i].size = resource_size(res);
1058 
1059 		dev_dbg(dev, "memory %8s: pa %pa size 0x%zx va %p\n",
1060 			mem_names[i], &pru->mem_regions[i].pa,
1061 			pru->mem_regions[i].size, pru->mem_regions[i].va);
1062 	}
1063 
1064 	ret = pru_rproc_set_id(pru);
1065 	if (ret < 0)
1066 		return ret;
1067 
1068 	platform_set_drvdata(pdev, rproc);
1069 
1070 	ret = devm_rproc_add(dev, pru->rproc);
1071 	if (ret) {
1072 		dev_err(dev, "rproc_add failed: %d\n", ret);
1073 		return ret;
1074 	}
1075 
1076 	pru_rproc_create_debug_entries(rproc);
1077 
1078 	dev_dbg(dev, "PRU rproc node %pOF probed successfully\n", np);
1079 
1080 	return 0;
1081 }
1082 
1083 static void pru_rproc_remove(struct platform_device *pdev)
1084 {
1085 	struct device *dev = &pdev->dev;
1086 	struct rproc *rproc = platform_get_drvdata(pdev);
1087 
1088 	dev_dbg(dev, "%s: removing rproc %s\n", __func__, rproc->name);
1089 }
1090 
1091 static const struct pru_private_data pru_data = {
1092 	.type = PRU_TYPE_PRU,
1093 };
1094 
1095 static const struct pru_private_data k3_pru_data = {
1096 	.type = PRU_TYPE_PRU,
1097 	.is_k3 = 1,
1098 };
1099 
1100 static const struct pru_private_data k3_rtu_data = {
1101 	.type = PRU_TYPE_RTU,
1102 	.is_k3 = 1,
1103 };
1104 
1105 static const struct pru_private_data k3_tx_pru_data = {
1106 	.type = PRU_TYPE_TX_PRU,
1107 	.is_k3 = 1,
1108 };
1109 
1110 static const struct of_device_id pru_rproc_match[] = {
1111 	{ .compatible = "ti,am3356-pru",	.data = &pru_data },
1112 	{ .compatible = "ti,am4376-pru",	.data = &pru_data },
1113 	{ .compatible = "ti,am5728-pru",	.data = &pru_data },
1114 	{ .compatible = "ti,am642-pru",		.data = &k3_pru_data },
1115 	{ .compatible = "ti,am642-rtu",		.data = &k3_rtu_data },
1116 	{ .compatible = "ti,am642-tx-pru",	.data = &k3_tx_pru_data },
1117 	{ .compatible = "ti,k2g-pru",		.data = &pru_data },
1118 	{ .compatible = "ti,am654-pru",		.data = &k3_pru_data },
1119 	{ .compatible = "ti,am654-rtu",		.data = &k3_rtu_data },
1120 	{ .compatible = "ti,am654-tx-pru",	.data = &k3_tx_pru_data },
1121 	{ .compatible = "ti,j721e-pru",		.data = &k3_pru_data },
1122 	{ .compatible = "ti,j721e-rtu",		.data = &k3_rtu_data },
1123 	{ .compatible = "ti,j721e-tx-pru",	.data = &k3_tx_pru_data },
1124 	{ .compatible = "ti,am625-pru",		.data = &k3_pru_data },
1125 	{},
1126 };
1127 MODULE_DEVICE_TABLE(of, pru_rproc_match);
1128 
1129 static struct platform_driver pru_rproc_driver = {
1130 	.driver = {
1131 		.name   = PRU_RPROC_DRVNAME,
1132 		.of_match_table = pru_rproc_match,
1133 		.suppress_bind_attrs = true,
1134 	},
1135 	.probe  = pru_rproc_probe,
1136 	.remove = pru_rproc_remove,
1137 };
1138 module_platform_driver(pru_rproc_driver);
1139 
1140 MODULE_AUTHOR("Suman Anna <s-anna@ti.com>");
1141 MODULE_AUTHOR("Andrew F. Davis <afd@ti.com>");
1142 MODULE_AUTHOR("Grzegorz Jaszczyk <grzegorz.jaszczyk@linaro.org>");
1143 MODULE_AUTHOR("Puranjay Mohan <p-mohan@ti.com>");
1144 MODULE_AUTHOR("Md Danish Anwar <danishanwar@ti.com>");
1145 MODULE_DESCRIPTION("PRU-ICSS Remote Processor Driver");
1146 MODULE_LICENSE("GPL v2");
1147