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_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>
24
25 #include "remoteproc_internal.h"
26 #include "remoteproc_elf_helpers.h"
27 #include "pru_rproc.h"
28
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
39
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)
47
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)
51
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
60
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 */
66
67 #define MAX_PRU_SYS_EVENTS 160
68
69 /**
70 * enum pru_iomem - PRU core memory/register range identifiers
71 *
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
76 */
77 enum pru_iomem {
78 PRU_IOMEM_IRAM = 0,
79 PRU_IOMEM_CTRL,
80 PRU_IOMEM_DEBUG,
81 PRU_IOMEM_MAX,
82 };
83
84 /**
85 * enum pru_type - PRU core type identifier
86 *
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
91 */
92 enum pru_type {
93 PRU_TYPE_PRU = 0,
94 PRU_TYPE_RTU,
95 PRU_TYPE_TX_PRU,
96 PRU_TYPE_MAX,
97 };
98
99 /**
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
103 */
104 struct pru_private_data {
105 enum pru_type type;
106 unsigned int is_k3 : 1;
107 };
108
109 /**
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
127 */
128 struct pru_rproc {
129 int id;
130 struct device *dev;
131 struct pruss *pruss;
132 struct rproc *rproc;
133 const struct pru_private_data *data;
134 struct pruss_mem_region mem_regions[PRU_IOMEM_MAX];
135 struct device_node *client_np;
136 struct mutex lock;
137 const char *fw_name;
138 unsigned int *mapped_irq;
139 struct pru_irq_rsc *pru_interrupt_map;
140 size_t pru_interrupt_map_sz;
141 spinlock_t rmw_lock;
142 u32 dbg_single_step;
143 u32 dbg_continuous;
144 u8 evt_count;
145 };
146
pru_control_read_reg(struct pru_rproc * pru,unsigned int reg)147 static inline u32 pru_control_read_reg(struct pru_rproc *pru, unsigned int reg)
148 {
149 return readl_relaxed(pru->mem_regions[PRU_IOMEM_CTRL].va + reg);
150 }
151
152 static inline
pru_control_write_reg(struct pru_rproc * pru,unsigned int reg,u32 val)153 void pru_control_write_reg(struct pru_rproc *pru, unsigned int reg, u32 val)
154 {
155 writel_relaxed(val, pru->mem_regions[PRU_IOMEM_CTRL].va + reg);
156 }
157
158 static inline
pru_control_set_reg(struct pru_rproc * pru,unsigned int reg,u32 mask,u32 set)159 void pru_control_set_reg(struct pru_rproc *pru, unsigned int reg,
160 u32 mask, u32 set)
161 {
162 u32 val;
163 unsigned long flags;
164
165 spin_lock_irqsave(&pru->rmw_lock, flags);
166
167 val = pru_control_read_reg(pru, reg);
168 val &= ~mask;
169 val |= (set & mask);
170 pru_control_write_reg(pru, reg, val);
171
172 spin_unlock_irqrestore(&pru->rmw_lock, flags);
173 }
174
175 /**
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
179 *
180 * Return: 0 on success, or errno in error case.
181 */
pru_rproc_set_firmware(struct rproc * rproc,const char * fw_name)182 static int pru_rproc_set_firmware(struct rproc *rproc, const char *fw_name)
183 {
184 struct pru_rproc *pru = rproc->priv;
185
186 if (!fw_name)
187 fw_name = pru->fw_name;
188
189 return rproc_set_firmware(rproc, fw_name);
190 }
191
__pru_rproc_get(struct device_node * np,int index)192 static struct rproc *__pru_rproc_get(struct device_node *np, int index)
193 {
194 struct rproc *rproc;
195 phandle rproc_phandle;
196 int ret;
197
198 ret = of_property_read_u32_index(np, "ti,prus", index, &rproc_phandle);
199 if (ret)
200 return ERR_PTR(ret);
201
202 rproc = rproc_get_by_phandle(rproc_phandle);
203 if (!rproc) {
204 ret = -EPROBE_DEFER;
205 return ERR_PTR(ret);
206 }
207
208 /* make sure it is PRU rproc */
209 if (!is_pru_rproc(rproc->dev.parent)) {
210 rproc_put(rproc);
211 return ERR_PTR(-ENODEV);
212 }
213
214 return rproc;
215 }
216
217 /**
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
222 *
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.
228 *
229 * When optional @pru_id pointer is passed the PRU remoteproc processor id is
230 * returned.
231 *
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
237 */
pru_rproc_get(struct device_node * np,int index,enum pruss_pru_id * pru_id)238 struct rproc *pru_rproc_get(struct device_node *np, int index,
239 enum pruss_pru_id *pru_id)
240 {
241 struct rproc *rproc;
242 struct pru_rproc *pru;
243 struct device *dev;
244 const char *fw_name;
245 int ret;
246
247 rproc = __pru_rproc_get(np, index);
248 if (IS_ERR(rproc))
249 return rproc;
250
251 pru = rproc->priv;
252 dev = &rproc->dev;
253
254 mutex_lock(&pru->lock);
255
256 if (pru->client_np) {
257 mutex_unlock(&pru->lock);
258 ret = -EBUSY;
259 goto err_no_rproc_handle;
260 }
261
262 pru->client_np = np;
263 rproc->sysfs_read_only = true;
264
265 mutex_unlock(&pru->lock);
266
267 if (pru_id)
268 *pru_id = pru->id;
269
270 ret = of_property_read_string_index(np, "firmware-name", index,
271 &fw_name);
272 if (!ret) {
273 ret = pru_rproc_set_firmware(rproc, fw_name);
274 if (ret) {
275 dev_err(dev, "failed to set firmware: %d\n", ret);
276 goto err;
277 }
278 }
279
280 return rproc;
281
282 err_no_rproc_handle:
283 rproc_put(rproc);
284 return ERR_PTR(ret);
285
286 err:
287 pru_rproc_put(rproc);
288 return ERR_PTR(ret);
289 }
290 EXPORT_SYMBOL_GPL(pru_rproc_get);
291
292 /**
293 * pru_rproc_put() - release the PRU rproc resource
294 * @rproc: the rproc resource to release
295 *
296 * Releases the PRU rproc resource and makes it available to other
297 * users.
298 */
pru_rproc_put(struct rproc * rproc)299 void pru_rproc_put(struct rproc *rproc)
300 {
301 struct pru_rproc *pru;
302
303 if (IS_ERR_OR_NULL(rproc) || !is_pru_rproc(rproc->dev.parent))
304 return;
305
306 pru = rproc->priv;
307
308 pru_rproc_set_firmware(rproc, NULL);
309
310 mutex_lock(&pru->lock);
311
312 if (!pru->client_np) {
313 mutex_unlock(&pru->lock);
314 return;
315 }
316
317 pru->client_np = NULL;
318 rproc->sysfs_read_only = false;
319 mutex_unlock(&pru->lock);
320
321 rproc_put(rproc);
322 }
323 EXPORT_SYMBOL_GPL(pru_rproc_put);
324
325 /**
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
330 *
331 * Return: 0 on success, or errno in error case.
332 */
pru_rproc_set_ctable(struct rproc * rproc,enum pru_ctable_idx c,u32 addr)333 int pru_rproc_set_ctable(struct rproc *rproc, enum pru_ctable_idx c, u32 addr)
334 {
335 struct pru_rproc *pru = rproc->priv;
336 unsigned int reg;
337 u32 mask, set;
338 u16 idx;
339 u16 idx_mask;
340
341 if (IS_ERR_OR_NULL(rproc))
342 return -EINVAL;
343
344 if (!rproc->dev.parent || !is_pru_rproc(rproc->dev.parent))
345 return -ENODEV;
346
347 /* pointer is 16 bit and index is 8-bit so mask out the rest */
348 idx_mask = (c >= PRU_C28) ? 0xFFFF : 0xFF;
349
350 /* ctable uses bit 8 and upwards only */
351 idx = (addr >> 8) & idx_mask;
352
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));
357
358 pru_control_set_reg(pru, reg, mask, set);
359
360 return 0;
361 }
362 EXPORT_SYMBOL_GPL(pru_rproc_set_ctable);
363
pru_debug_read_reg(struct pru_rproc * pru,unsigned int reg)364 static inline u32 pru_debug_read_reg(struct pru_rproc *pru, unsigned int reg)
365 {
366 return readl_relaxed(pru->mem_regions[PRU_IOMEM_DEBUG].va + reg);
367 }
368
regs_show(struct seq_file * s,void * data)369 static int regs_show(struct seq_file *s, void *data)
370 {
371 struct rproc *rproc = s->private;
372 struct pru_rproc *pru = rproc->priv;
373 int i, nregs = 32;
374 u32 pru_sts;
375 int pru_is_running;
376
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));
396
397 seq_puts(s, "=============== Debug Registers ===============\n");
398 pru_is_running = pru_control_read_reg(pru, PRU_CTRL_CTRL) &
399 CTRL_CTRL_RUNSTATE;
400 if (pru_is_running) {
401 seq_puts(s, "PRU is executing, cannot print/access debug registers.\n");
402 return 0;
403 }
404
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)));
409 }
410
411 return 0;
412 }
413 DEFINE_SHOW_ATTRIBUTE(regs);
414
415 /*
416 * Control PRU single-step mode
417 *
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.
421 *
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
425 * original mode.
426 */
pru_rproc_debug_ss_set(void * data,u64 val)427 static int pru_rproc_debug_ss_set(void *data, u64 val)
428 {
429 struct rproc *rproc = data;
430 struct pru_rproc *pru = rproc->priv;
431 u32 reg_val;
432
433 val = val ? 1 : 0;
434 if (!val && !pru->dbg_single_step)
435 return 0;
436
437 reg_val = pru_control_read_reg(pru, PRU_CTRL_CTRL);
438
439 if (val && !pru->dbg_single_step)
440 pru->dbg_continuous = reg_val;
441
442 if (val)
443 reg_val |= CTRL_CTRL_SINGLE_STEP | CTRL_CTRL_EN;
444 else
445 reg_val = pru->dbg_continuous;
446
447 pru->dbg_single_step = val;
448 pru_control_write_reg(pru, PRU_CTRL_CTRL, reg_val);
449
450 return 0;
451 }
452
pru_rproc_debug_ss_get(void * data,u64 * val)453 static int pru_rproc_debug_ss_get(void *data, u64 *val)
454 {
455 struct rproc *rproc = data;
456 struct pru_rproc *pru = rproc->priv;
457
458 *val = pru->dbg_single_step;
459
460 return 0;
461 }
462 DEFINE_DEBUGFS_ATTRIBUTE(pru_rproc_debug_ss_fops, pru_rproc_debug_ss_get,
463 pru_rproc_debug_ss_set, "%llu\n");
464
465 /*
466 * Create PRU-specific debugfs entries
467 *
468 * The entries are created only if the parent remoteproc debugfs directory
469 * exists, and will be cleaned up by the remoteproc core.
470 */
pru_rproc_create_debug_entries(struct rproc * rproc)471 static void pru_rproc_create_debug_entries(struct rproc *rproc)
472 {
473 if (!rproc->dbg_dir)
474 return;
475
476 debugfs_create_file("regs", 0400, rproc->dbg_dir,
477 rproc, ®s_fops);
478 debugfs_create_file("single_step", 0600, rproc->dbg_dir,
479 rproc, &pru_rproc_debug_ss_fops);
480 }
481
pru_dispose_irq_mapping(struct pru_rproc * pru)482 static void pru_dispose_irq_mapping(struct pru_rproc *pru)
483 {
484 if (!pru->mapped_irq)
485 return;
486
487 while (pru->evt_count) {
488 pru->evt_count--;
489 if (pru->mapped_irq[pru->evt_count] > 0)
490 irq_dispose_mapping(pru->mapped_irq[pru->evt_count]);
491 }
492
493 kfree(pru->mapped_irq);
494 pru->mapped_irq = NULL;
495 }
496
497 /*
498 * Parse the custom PRU interrupt map resource and configure the INTC
499 * appropriately.
500 */
pru_handle_intrmap(struct rproc * rproc)501 static int pru_handle_intrmap(struct rproc *rproc)
502 {
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;
508 int i, ret = 0;
509
510 /* not having pru_interrupt_map is not an error */
511 if (!rsc)
512 return 0;
513
514 /* currently supporting only type 0 */
515 if (rsc->type != 0) {
516 dev_err(dev, "unsupported rsc type: %d\n", rsc->type);
517 return -EINVAL;
518 }
519
520 if (rsc->num_evts > MAX_PRU_SYS_EVENTS)
521 return -EINVAL;
522
523 if (sizeof(*rsc) + rsc->num_evts * sizeof(struct pruss_int_map) !=
524 pru->pru_interrupt_map_sz)
525 return -EINVAL;
526
527 pru->evt_count = rsc->num_evts;
528 pru->mapped_irq = kcalloc(pru->evt_count, sizeof(unsigned int),
529 GFP_KERNEL);
530 if (!pru->mapped_irq) {
531 pru->evt_count = 0;
532 return -ENOMEM;
533 }
534
535 /*
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.
540 */
541 parent = of_get_parent(dev_of_node(pru->dev));
542 if (!parent) {
543 kfree(pru->mapped_irq);
544 pru->mapped_irq = NULL;
545 pru->evt_count = 0;
546 return -ENODEV;
547 }
548
549 irq_parent = of_get_child_by_name(parent, "interrupt-controller");
550 of_node_put(parent);
551 if (!irq_parent) {
552 kfree(pru->mapped_irq);
553 pru->mapped_irq = NULL;
554 pru->evt_count = 0;
555 return -ENODEV;
556 }
557
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;
564
565 dev_dbg(dev, "mapping%d: event %d, chnl %d, host %d\n",
566 i, fwspec.param[0], fwspec.param[1], fwspec.param[2]);
567
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],
572 fwspec.param[2]);
573 ret = -EINVAL;
574 goto map_fail;
575 }
576 }
577 of_node_put(irq_parent);
578
579 return ret;
580
581 map_fail:
582 pru_dispose_irq_mapping(pru);
583 of_node_put(irq_parent);
584
585 return ret;
586 }
587
pru_rproc_start(struct rproc * rproc)588 static int pru_rproc_start(struct rproc *rproc)
589 {
590 struct device *dev = &rproc->dev;
591 struct pru_rproc *pru = rproc->priv;
592 const char *names[PRU_TYPE_MAX] = { "PRU", "RTU", "Tx_PRU" };
593 u32 val;
594 int ret;
595
596 dev_dbg(dev, "starting %s%d: entry-point = 0x%llx\n",
597 names[pru->data->type], pru->id, (rproc->bootaddr >> 2));
598
599 ret = pru_handle_intrmap(rproc);
600 /*
601 * reset references to pru interrupt map - they will stop being valid
602 * after rproc_start returns
603 */
604 pru->pru_interrupt_map = NULL;
605 pru->pru_interrupt_map_sz = 0;
606 if (ret)
607 return ret;
608
609 val = CTRL_CTRL_EN | ((rproc->bootaddr >> 2) << 16);
610 pru_control_write_reg(pru, PRU_CTRL_CTRL, val);
611
612 return 0;
613 }
614
pru_rproc_stop(struct rproc * rproc)615 static int pru_rproc_stop(struct rproc *rproc)
616 {
617 struct device *dev = &rproc->dev;
618 struct pru_rproc *pru = rproc->priv;
619 const char *names[PRU_TYPE_MAX] = { "PRU", "RTU", "Tx_PRU" };
620 u32 val;
621
622 dev_dbg(dev, "stopping %s%d\n", names[pru->data->type], pru->id);
623
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);
627
628 /* dispose irq mapping - new firmware can provide new mapping */
629 pru_dispose_irq_mapping(pru);
630
631 return 0;
632 }
633
634 /*
635 * Convert PRU device address (data spaces only) to kernel virtual address.
636 *
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.
642 */
pru_d_da_to_va(struct pru_rproc * pru,u32 da,size_t len)643 static void *pru_d_da_to_va(struct pru_rproc *pru, u32 da, size_t len)
644 {
645 struct pruss_mem_region dram0, dram1, shrd_ram;
646 struct pruss *pruss = pru->pruss;
647 u32 offset;
648 void *va = NULL;
649
650 if (len == 0)
651 return NULL;
652
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)
657 swap(dram0, dram1);
658 shrd_ram = pruss->mem_regions[PRUSS_MEM_SHRD_RAM2];
659
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);
671 }
672
673 return va;
674 }
675
676 /*
677 * Convert PRU device address (instruction space) to kernel virtual address.
678 *
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.
682 */
pru_i_da_to_va(struct pru_rproc * pru,u32 da,size_t len)683 static void *pru_i_da_to_va(struct pru_rproc *pru, u32 da, size_t len)
684 {
685 u32 offset;
686 void *va = NULL;
687
688 if (len == 0)
689 return NULL;
690
691 /*
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
696 * ELF file.
697 *
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.
702 *
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
705 * in future SoCs.
706 */
707 da &= 0xfffff;
708
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 +
713 offset);
714 }
715
716 return va;
717 }
718
719 /*
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.
723 */
pru_rproc_da_to_va(struct rproc * rproc,u64 da,size_t len,bool * is_iomem)724 static void *pru_rproc_da_to_va(struct rproc *rproc, u64 da, size_t len, bool *is_iomem)
725 {
726 struct pru_rproc *pru = rproc->priv;
727
728 return pru_d_da_to_va(pru, da, len);
729 }
730
731 /* PRU-specific address translator used by PRU loader. */
pru_da_to_va(struct rproc * rproc,u64 da,size_t len,bool is_iram)732 static void *pru_da_to_va(struct rproc *rproc, u64 da, size_t len, bool is_iram)
733 {
734 struct pru_rproc *pru = rproc->priv;
735 void *va;
736
737 if (is_iram)
738 va = pru_i_da_to_va(pru, da, len);
739 else
740 va = pru_d_da_to_va(pru, da, len);
741
742 return va;
743 }
744
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,
749 };
750
751 /*
752 * Custom memory copy implementation for ICSSG PRU/RTU/Tx_PRU Cores
753 *
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.
762 */
pru_rproc_memcpy(void * dest,const void * src,size_t count)763 static int pru_rproc_memcpy(void *dest, const void *src, size_t count)
764 {
765 const u32 *s = src;
766 u32 *d = dest;
767 size_t size = count / 4;
768 u32 *tmp_src = NULL;
769
770 /*
771 * TODO: relax limitation of 4-byte aligned dest addresses and copy
772 * sizes
773 */
774 if ((long)dest % 4 || count % 4)
775 return -EINVAL;
776
777 /* src offsets in ELF firmware image can be non-aligned */
778 if ((long)src % 4) {
779 tmp_src = kmemdup(src, count, GFP_KERNEL);
780 if (!tmp_src)
781 return -ENOMEM;
782 s = tmp_src;
783 }
784
785 while (size--)
786 *d++ = *s++;
787
788 kfree(tmp_src);
789
790 return 0;
791 }
792
793 static int
pru_rproc_load_elf_segments(struct rproc * rproc,const struct firmware * fw)794 pru_rproc_load_elf_segments(struct rproc *rproc, const struct firmware *fw)
795 {
796 struct pru_rproc *pru = rproc->priv;
797 struct device *dev = &rproc->dev;
798 struct elf32_hdr *ehdr;
799 struct elf32_phdr *phdr;
800 int i, ret = 0;
801 const u8 *elf_data = fw->data;
802
803 ehdr = (struct elf32_hdr *)elf_data;
804 phdr = (struct elf32_phdr *)(elf_data + ehdr->e_phoff);
805
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;
812 bool is_iram;
813 void *ptr;
814
815 if (phdr->p_type != PT_LOAD || !filesz)
816 continue;
817
818 dev_dbg(dev, "phdr: type %d da 0x%x memsz 0x%x filesz 0x%x\n",
819 phdr->p_type, da, memsz, filesz);
820
821 if (filesz > memsz) {
822 dev_err(dev, "bad phdr filesz 0x%x memsz 0x%x\n",
823 filesz, memsz);
824 ret = -EINVAL;
825 break;
826 }
827
828 if (offset + filesz > fw->size) {
829 dev_err(dev, "truncated fw: need 0x%x avail 0x%zx\n",
830 offset + filesz, fw->size);
831 ret = -EINVAL;
832 break;
833 }
834
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);
838 if (!ptr) {
839 dev_err(dev, "bad phdr da 0x%x mem 0x%x\n", da, memsz);
840 ret = -EINVAL;
841 break;
842 }
843
844 if (pru->data->is_k3) {
845 ret = pru_rproc_memcpy(ptr, elf_data + phdr->p_offset,
846 filesz);
847 if (ret) {
848 dev_err(dev, "PRU memory copy failed for da 0x%x memsz 0x%x\n",
849 da, memsz);
850 break;
851 }
852 } else {
853 memcpy(ptr, elf_data + phdr->p_offset, filesz);
854 }
855
856 /* skip the memzero logic performed by remoteproc ELF loader */
857 }
858
859 return ret;
860 }
861
862 static const void *
pru_rproc_find_interrupt_map(struct device * dev,const struct firmware * fw)863 pru_rproc_find_interrupt_map(struct device *dev, const struct firmware *fw)
864 {
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;
871 int i;
872
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;
879
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;
884
885 if (strcmp(name_table + name, ".pru_irq_map"))
886 continue;
887
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);
892 }
893
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);
898 }
899
900 return shdr;
901 }
902
903 dev_dbg(dev, "no .pru_irq_map section found for this fw\n");
904
905 return NULL;
906 }
907
908 /*
909 * Use a custom parse_fw callback function for dealing with PRU firmware
910 * specific sections.
911 *
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.
917 */
pru_rproc_parse_fw(struct rproc * rproc,const struct firmware * fw)918 static int pru_rproc_parse_fw(struct rproc *rproc, const struct firmware *fw)
919 {
920 struct device *dev = &rproc->dev;
921 struct pru_rproc *pru = rproc->priv;
922 const u8 *elf_data = fw->data;
923 const void *shdr;
924 u8 class = fw_elf_get_class(fw);
925 u64 sh_offset;
926 int ret;
927
928 /* load optional rsc table */
929 ret = rproc_elf_load_rsc_table(rproc, fw);
930 if (ret == -EINVAL)
931 dev_dbg(&rproc->dev, "no resource table found for this fw\n");
932 else if (ret)
933 return ret;
934
935 /* find .pru_interrupt_map section, not having it is not an error */
936 shdr = pru_rproc_find_interrupt_map(dev, fw);
937 if (IS_ERR(shdr))
938 return PTR_ERR(shdr);
939
940 if (!shdr)
941 return 0;
942
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);
947
948 return 0;
949 }
950
951 /*
952 * Compute PRU id based on the IRAM addresses. The PRU IRAMs are
953 * always at a particular offset within the PRUSS address space.
954 */
pru_rproc_set_id(struct pru_rproc * pru)955 static int pru_rproc_set_id(struct pru_rproc *pru)
956 {
957 int ret = 0;
958
959 switch (pru->mem_regions[PRU_IOMEM_IRAM].pa & PRU_IRAM_ADDR_MASK) {
960 case TX_PRU0_IRAM_ADDR_MASK:
961 fallthrough;
962 case RTU0_IRAM_ADDR_MASK:
963 fallthrough;
964 case PRU0_IRAM_ADDR_MASK:
965 pru->id = PRUSS_PRU0;
966 break;
967 case TX_PRU1_IRAM_ADDR_MASK:
968 fallthrough;
969 case RTU1_IRAM_ADDR_MASK:
970 fallthrough;
971 case PRU1_IRAM_ADDR_MASK:
972 pru->id = PRUSS_PRU1;
973 break;
974 default:
975 ret = -EINVAL;
976 }
977
978 return ret;
979 }
980
pru_rproc_probe(struct platform_device * pdev)981 static int pru_rproc_probe(struct platform_device *pdev)
982 {
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;
987 const char *fw_name;
988 struct rproc *rproc = NULL;
989 struct resource *res;
990 int i, ret;
991 const struct pru_private_data *data;
992 const char *mem_names[PRU_IOMEM_MAX] = { "iram", "control", "debug" };
993
994 data = of_device_get_match_data(&pdev->dev);
995 if (!data)
996 return -ENODEV;
997
998 ret = of_property_read_string(np, "firmware-name", &fw_name);
999 if (ret) {
1000 dev_err(dev, "unable to retrieve firmware-name %d\n", ret);
1001 return ret;
1002 }
1003
1004 rproc = devm_rproc_alloc(dev, pdev->name, &pru_rproc_ops, fw_name,
1005 sizeof(*pru));
1006 if (!rproc) {
1007 dev_err(dev, "rproc_alloc failed\n");
1008 return -ENOMEM;
1009 }
1010 /* use a custom load function to deal with PRU-specific quirks */
1011 rproc->ops->load = pru_rproc_load_elf_segments;
1012
1013 /* use a custom parse function to deal with PRU-specific resources */
1014 rproc->ops->parse_fw = pru_rproc_parse_fw;
1015
1016 /* error recovery is not supported for PRUs */
1017 rproc->recovery_disabled = true;
1018
1019 /*
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.
1025 */
1026 rproc->auto_boot = false;
1027
1028 pru = rproc->priv;
1029 pru->dev = dev;
1030 pru->data = data;
1031 pru->pruss = platform_get_drvdata(ppdev);
1032 pru->rproc = rproc;
1033 pru->fw_name = fw_name;
1034 pru->client_np = NULL;
1035 spin_lock_init(&pru->rmw_lock);
1036 mutex_init(&pru->lock);
1037
1038 for (i = 0; i < ARRAY_SIZE(mem_names); i++) {
1039 res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
1040 mem_names[i]);
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",
1044 i, mem_names[i]);
1045 ret = PTR_ERR(pru->mem_regions[i].va);
1046 return ret;
1047 }
1048 pru->mem_regions[i].pa = res->start;
1049 pru->mem_regions[i].size = resource_size(res);
1050
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);
1054 }
1055
1056 ret = pru_rproc_set_id(pru);
1057 if (ret < 0)
1058 return ret;
1059
1060 platform_set_drvdata(pdev, rproc);
1061
1062 ret = devm_rproc_add(dev, pru->rproc);
1063 if (ret) {
1064 dev_err(dev, "rproc_add failed: %d\n", ret);
1065 return ret;
1066 }
1067
1068 pru_rproc_create_debug_entries(rproc);
1069
1070 dev_dbg(dev, "PRU rproc node %pOF probed successfully\n", np);
1071
1072 return 0;
1073 }
1074
pru_rproc_remove(struct platform_device * pdev)1075 static int pru_rproc_remove(struct platform_device *pdev)
1076 {
1077 struct device *dev = &pdev->dev;
1078 struct rproc *rproc = platform_get_drvdata(pdev);
1079
1080 dev_dbg(dev, "%s: removing rproc %s\n", __func__, rproc->name);
1081
1082 return 0;
1083 }
1084
1085 static const struct pru_private_data pru_data = {
1086 .type = PRU_TYPE_PRU,
1087 };
1088
1089 static const struct pru_private_data k3_pru_data = {
1090 .type = PRU_TYPE_PRU,
1091 .is_k3 = 1,
1092 };
1093
1094 static const struct pru_private_data k3_rtu_data = {
1095 .type = PRU_TYPE_RTU,
1096 .is_k3 = 1,
1097 };
1098
1099 static const struct pru_private_data k3_tx_pru_data = {
1100 .type = PRU_TYPE_TX_PRU,
1101 .is_k3 = 1,
1102 };
1103
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 },
1119 {},
1120 };
1121 MODULE_DEVICE_TABLE(of, pru_rproc_match);
1122
1123 static struct platform_driver pru_rproc_driver = {
1124 .driver = {
1125 .name = PRU_RPROC_DRVNAME,
1126 .of_match_table = pru_rproc_match,
1127 .suppress_bind_attrs = true,
1128 },
1129 .probe = pru_rproc_probe,
1130 .remove = pru_rproc_remove,
1131 };
1132 module_platform_driver(pru_rproc_driver);
1133
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");
1141