]> Gitweb @ Texas Instruments - Open Source Git Repositories - git.TI.com/gitweb - rpmsg/rpmsg.git/blob - drivers/remoteproc/pru_rproc.c
remoteproc/pru: add support for parsing pru interrupt mapping from DT
[rpmsg/rpmsg.git] / drivers / remoteproc / pru_rproc.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * PRU-ICSS remoteproc driver for various TI SoCs
4  *
5  * Copyright (C) 2014-2019 Texas Instruments Incorporated - http://www.ti.com/
6  *      Suman Anna <s-anna@ti.com>
7  *      Andrew F. Davis <afd@ti.com>
8  */
10 #include <linux/bitops.h>
11 #include <linux/debugfs.h>
12 #include <linux/interrupt.h>
13 #include <linux/mailbox_client.h>
14 #include <linux/module.h>
15 #include <linux/of_device.h>
16 #include <linux/pruss.h>
17 #include <linux/pruss_driver.h>
18 #include <linux/remoteproc.h>
20 #include "remoteproc_internal.h"
21 #include "pru_rproc.h"
23 /* PRU_ICSS_PRU_CTRL registers */
24 #define PRU_CTRL_CTRL           0x0000
25 #define PRU_CTRL_STS            0x0004
26 #define PRU_CTRL_WAKEUP_EN      0x0008
27 #define PRU_CTRL_CYCLE          0x000C
28 #define PRU_CTRL_STALL          0x0010
29 #define PRU_CTRL_CTBIR0         0x0020
30 #define PRU_CTRL_CTBIR1         0x0024
31 #define PRU_CTRL_CTPPR0         0x0028
32 #define PRU_CTRL_CTPPR1         0x002C
34 /* CTRL register bit-fields */
35 #define CTRL_CTRL_SOFT_RST_N    BIT(0)
36 #define CTRL_CTRL_EN            BIT(1)
37 #define CTRL_CTRL_SLEEPING      BIT(2)
38 #define CTRL_CTRL_CTR_EN        BIT(3)
39 #define CTRL_CTRL_SINGLE_STEP   BIT(8)
40 #define CTRL_CTRL_RUNSTATE      BIT(15)
42 /* PRU_ICSS_PRU_DEBUG registers */
43 #define PRU_DEBUG_GPREG(x)      (0x0000 + (x) * 4)
44 #define PRU_DEBUG_CT_REG(x)     (0x0080 + (x) * 4)
46 /* PRU Core IRAM address masks */
47 #define PRU0_IRAM_ADDR_MASK     0x34000
48 #define PRU1_IRAM_ADDR_MASK     0x38000
50 /**
51  * enum pru_iomem - PRU core memory/register range identifiers
52  */
53 enum pru_iomem {
54         PRU_IOMEM_IRAM = 0,
55         PRU_IOMEM_CTRL,
56         PRU_IOMEM_DEBUG,
57         PRU_IOMEM_MAX,
58 };
60 /**
61  * struct pru_rproc - PRU remoteproc structure
62  * @id: id of the PRU core within the PRUSS
63  * @pruss: back-reference to parent PRUSS structure
64  * @rproc: remoteproc pointer for this PRU core
65  * @client_np: client device node
66  * @mbox: mailbox channel handle used for vring signalling with MPU
67  * @client: mailbox client to request the mailbox channel
68  * @irq_ring: IRQ number to use for processing vring buffers
69  * @irq_kick: IRQ number to use to perform virtio kick
70  * @mem_regions: data for each of the PRU memory regions
71  * @intc_config: PRU INTC configuration data
72  * @rmw_lock: lock for read, modify, write operations on registers
73  * @iram_da: device address of Instruction RAM for this PRU
74  * @pdram_da: device address of primary Data RAM for this PRU
75  * @sdram_da: device address of secondary Data RAM for this PRU
76  * @shrdram_da: device address of shared Data RAM
77  * @fw_name: name of firmware image used during loading
78  * @dt_irqs: number of irqs configured from DT
79  * @lock: mutex to protect client usage
80  * @dbg_single_step: debug state variable to set PRU into single step mode
81  * @dbg_continuous: debug state variable to restore PRU execution mode
82  * @fw_has_intc_rsc: boolean flag to indicate INTC config through firmware
83  */
84 struct pru_rproc {
85         int id;
86         struct pruss *pruss;
87         struct rproc *rproc;
88         struct device_node *client_np;
89         struct mbox_chan *mbox;
90         struct mbox_client client;
91         int irq_vring;
92         int irq_kick;
93         struct pruss_mem_region mem_regions[PRU_IOMEM_MAX];
94         struct pruss_intc_config intc_config;
95         spinlock_t rmw_lock; /* register access lock */
96         u32 iram_da;
97         u32 pdram_da;
98         u32 sdram_da;
99         u32 shrdram_da;
100         const char *fw_name;
101         int dt_irqs;
102         struct mutex lock; /* client access lock */
103         u32 dbg_single_step;
104         u32 dbg_continuous;
105         unsigned int fw_has_intc_rsc : 1;
106 };
108 static void *pru_d_da_to_va(struct pru_rproc *pru, u32 da, int len);
110 static inline u32 pru_control_read_reg(struct pru_rproc *pru, unsigned int reg)
112         return readl_relaxed(pru->mem_regions[PRU_IOMEM_CTRL].va + reg);
115 static inline
116 void pru_control_write_reg(struct pru_rproc *pru, unsigned int reg, u32 val)
118         writel_relaxed(val, pru->mem_regions[PRU_IOMEM_CTRL].va + reg);
121 static inline
122 void pru_control_set_reg(struct pru_rproc *pru, unsigned int reg,
123                          u32 mask, u32 set)
125         u32 val;
126         unsigned long flags;
128         spin_lock_irqsave(&pru->rmw_lock, flags);
130         val = pru_control_read_reg(pru, reg);
131         val &= ~mask;
132         val |= (set & mask);
133         pru_control_write_reg(pru, reg, val);
135         spin_unlock_irqrestore(&pru->rmw_lock, flags);
138 /**
139  * pru_rproc_set_firmware() - set firmware for a pru core
140  * @rproc: the rproc instance of the PRU
141  * @fw_name: the new firmware name, or NULL if default is desired
142  */
143 static int pru_rproc_set_firmware(struct rproc *rproc, const char *fw_name)
145         struct pru_rproc *pru = rproc->priv;
147         if (!fw_name)
148                 fw_name = pru->fw_name;
150         return rproc_set_firmware(rproc, fw_name);
153 static int pru_rproc_intc_dt_config(struct pru_rproc *pru, int index)
155         struct device *dev = &pru->rproc->dev;
156         struct device_node *np = pru->client_np;
157         struct property *prop;
158         const char *prop_name = "ti,pru-interrupt-map";
159         int ret = 0, i;
160         int dt_irqs;
161         u32 *arr;
162         bool has_irqs = false;
164         prop = of_find_property(np, prop_name, NULL);
165         if (!prop)
166                 return 0;
168         dt_irqs = of_property_count_u32_elems(np, prop_name);
169         if (dt_irqs <= 0 || dt_irqs % 4) {
170                 dev_err(dev, "bad interrupt map data %d, expected multiple of 4\n",
171                         dt_irqs);
172                 return -EINVAL;
173         }
175         arr = kmalloc_array(dt_irqs, sizeof(u32), GFP_KERNEL);
176         if (!arr)
177                 return -ENOMEM;
179         ret = of_property_read_u32_array(np, prop_name, arr, dt_irqs);
180         if (ret) {
181                 dev_err(dev, "failed to read pru irq map: %d\n", ret);
182                 goto out;
183         }
185         for (i = 0; i < ARRAY_SIZE(pru->intc_config.sysev_to_ch); i++)
186                 pru->intc_config.sysev_to_ch[i] = -1;
188         for (i = 0; i < ARRAY_SIZE(pru->intc_config.ch_to_host); i++)
189                 pru->intc_config.ch_to_host[i] = -1;
191         for (i = 0; i < dt_irqs; i += 4) {
192                 if (arr[i] != index)
193                         continue;
195                 if (arr[i + 1] < 0 ||
196                     arr[i + 1] >= MAX_PRU_SYS_EVENTS) {
197                         dev_err(dev, "bad sys event %d\n", arr[i + 1]);
198                         ret = -EINVAL;
199                         goto out;
200                 }
202                 if (arr[i + 2] < 0 ||
203                     arr[i + 2] >= MAX_PRU_CHANNELS) {
204                         dev_err(dev, "bad channel %d\n", arr[i + 2]);
205                         ret = -EINVAL;
206                         goto out;
207                 }
209                 if (arr[i + 3] < 0 ||
210                     arr[i + 3] >= MAX_PRU_HOST_INT) {
211                         dev_err(dev, "bad irq %d\n", arr[i + 3]);
212                         ret = -EINVAL;
213                         goto out;
214                 }
216                 pru->intc_config.sysev_to_ch[arr[i + 1]] = arr[i + 2];
217                 dev_dbg(dev, "sysevt-to-ch[%d] -> %d\n", arr[i + 1],
218                         arr[i + 2]);
220                 pru->intc_config.ch_to_host[arr[i + 2]] = arr[i + 3];
221                 dev_dbg(dev, "chnl-to-host[%d] -> %d\n", arr[i + 2],
222                         arr[i + 3]);
224                 has_irqs = true;
225         }
227         /*
228          * The property "ti,pru-interrupt-map" is used in a consumer node, but
229          * need not necessarily have data for all referenced PRUs. Provide a
230          * fallback to get the interrupt data from firmware for PRUs ith no
231          * interrupt data.
232          */
233         if (!has_irqs) {
234                 dev_dbg(dev, "no DT irqs, falling back to firmware intc rsc mode\n");
235                 goto out;
236         }
238         pru->dt_irqs = dt_irqs;
239         ret = pruss_intc_configure(pru->pruss, &pru->intc_config);
240         if (ret) {
241                 dev_err(dev, "failed to configure intc %d\n", ret);
242                 pru->dt_irqs = 0;
243         }
245 out:
246         kfree(arr);
248         return ret;
251 static struct rproc *__pru_rproc_get(struct device_node *np, int index)
253         struct device_node *rproc_np = NULL;
254         struct platform_device *pdev;
255         struct rproc *rproc;
257         rproc_np = of_parse_phandle(np, "prus", index);
258         if (!rproc_np || !of_device_is_available(rproc_np))
259                 return ERR_PTR(-ENODEV);
261         pdev = of_find_device_by_node(rproc_np);
262         of_node_put(rproc_np);
264         if (!pdev)
265                 /* probably PRU not yet probed */
266                 return ERR_PTR(-EPROBE_DEFER);
268         /* TODO: replace the crude string based check to make sure it is PRU */
269         if (!strstr(dev_name(&pdev->dev), "pru")) {
270                 put_device(&pdev->dev);
271                 return ERR_PTR(-ENODEV);
272         }
274         rproc = platform_get_drvdata(pdev);
275         put_device(&pdev->dev);
276         if (!rproc)
277                 return ERR_PTR(-EPROBE_DEFER);
279         get_device(&rproc->dev);
281         return rproc;
284 /**
285  * pru_rproc_get() - get the PRU rproc instance from a device node
286  * @np: the user/client device node
287  * @index: index to use for the prus property
288  *
289  * This function looks through a client device node's "prus" property at index
290  * @index and returns the rproc handle for a valid PRU remote processor if
291  * found. The function allows only one user to own the PRU rproc resource at
292  * a time. Caller must call pru_rproc_put() when done with using the rproc,
293  * not required if the function returns a failure.
294  *
295  * Returns the rproc handle on success, and an ERR_PTR on failure using one
296  * of the following error values
297  *    -ENODEV if device is not found
298  *    -EBUSY if PRU is already acquired by anyone
299  *    -EPROBE_DEFER is PRU device is not probed yet
300  */
301 struct rproc *pru_rproc_get(struct device_node *np, int index)
303         struct rproc *rproc;
304         struct pru_rproc *pru;
305         const char *fw_name;
306         struct device *dev;
307         int ret;
309         rproc = __pru_rproc_get(np, index);
310         if (IS_ERR(rproc))
311                 return rproc;
313         pru = rproc->priv;
314         dev = &rproc->dev;
316         mutex_lock(&pru->lock);
318         if (pru->client_np) {
319                 mutex_unlock(&pru->lock);
320                 put_device(&rproc->dev);
321                 return ERR_PTR(-EBUSY);
322         }
324         pru->client_np = np;
325         rproc->deny_sysfs_ops = 1;
327         mutex_unlock(&pru->lock);
329         ret = of_property_read_string_index(np, "firmware-name", index,
330                                             &fw_name);
331         if (!ret) {
332                 ret = pru_rproc_set_firmware(rproc, fw_name);
333                 if (ret) {
334                         dev_err(dev, "failed to set firmware: %d\n", ret);
335                         goto err;
336                 }
337         }
339         ret = pru_rproc_intc_dt_config(pru, index);
340         if (ret)
341                 goto err;
343         return rproc;
345 err:
346         pru_rproc_put(rproc);
347         return ERR_PTR(ret);
349 EXPORT_SYMBOL_GPL(pru_rproc_get);
351 /**
352  * pru_rproc_put() - release the PRU rproc resource
353  * @rproc: the rproc resource to release
354  *
355  * Releases the PRU rproc resource and makes it available to other
356  * users.
357  */
358 void pru_rproc_put(struct rproc *rproc)
360         struct pru_rproc *pru;
362         if (IS_ERR_OR_NULL(rproc))
363                 return;
365         /* TODO: replace the crude string based check to make sure it is PRU */
366         if (!strstr(dev_name(rproc->dev.parent), "pru"))
367                 return;
369         pru = rproc->priv;
370         if (!pru->client_np)
371                 return;
373         if (pru->dt_irqs)
374                 pruss_intc_unconfigure(pru->pruss, &pru->intc_config);
376         pru_rproc_set_firmware(rproc, NULL);
378         mutex_lock(&pru->lock);
379         pru->client_np = NULL;
380         rproc->deny_sysfs_ops = 0;
381         mutex_unlock(&pru->lock);
383         put_device(&rproc->dev);
385 EXPORT_SYMBOL_GPL(pru_rproc_put);
387 /**
388  * pru_rproc_set_ctable() - set the constant table index for the PRU
389  * @rproc: the rproc instance of the PRU
390  * @c: constant table index to set
391  * @addr: physical address to set it to
392  */
393 int pru_rproc_set_ctable(struct rproc *rproc, enum pru_ctable_idx c, u32 addr)
395         struct pru_rproc *pru = rproc->priv;
396         unsigned int reg;
397         u32 mask, set;
398         u16 idx;
399         u16 idx_mask;
401         /* pointer is 16 bit and index is 8-bit so mask out the rest */
402         idx_mask = (c >= PRU_C28) ? 0xFFFF : 0xFF;
404         /* ctable uses bit 8 and upwards only */
405         idx = (addr >> 8) & idx_mask;
407         /* configurable ctable (i.e. C24) starts at PRU_CTRL_CTBIR0 */
408         reg = PRU_CTRL_CTBIR0 + 4 * (c >> 1);
409         mask = idx_mask << (16 * (c & 1));
410         set = idx << (16 * (c & 1));
412         pru_control_set_reg(pru, reg, mask, set);
414         return 0;
416 EXPORT_SYMBOL_GPL(pru_rproc_set_ctable);
418 static inline u32 pru_debug_read_reg(struct pru_rproc *pru, unsigned int reg)
420         return readl_relaxed(pru->mem_regions[PRU_IOMEM_DEBUG].va + reg);
423 static inline
424 void pru_debug_write_reg(struct pru_rproc *pru, unsigned int reg, u32 val)
426         writel_relaxed(val, pru->mem_regions[PRU_IOMEM_DEBUG].va + reg);
429 static int regs_show(struct seq_file *s, void *data)
431         struct rproc *rproc = s->private;
432         struct pru_rproc *pru = rproc->priv;
433         int i, nregs = 32;
434         u32 pru_sts;
435         int pru_is_running;
437         seq_puts(s, "============== Control Registers ==============\n");
438         seq_printf(s, "CTRL      := 0x%08x\n",
439                    pru_control_read_reg(pru, PRU_CTRL_CTRL));
440         pru_sts = pru_control_read_reg(pru, PRU_CTRL_STS);
441         seq_printf(s, "STS (PC)  := 0x%08x (0x%08x)\n", pru_sts, pru_sts << 2);
442         seq_printf(s, "WAKEUP_EN := 0x%08x\n",
443                    pru_control_read_reg(pru, PRU_CTRL_WAKEUP_EN));
444         seq_printf(s, "CYCLE     := 0x%08x\n",
445                    pru_control_read_reg(pru, PRU_CTRL_CYCLE));
446         seq_printf(s, "STALL     := 0x%08x\n",
447                    pru_control_read_reg(pru, PRU_CTRL_STALL));
448         seq_printf(s, "CTBIR0    := 0x%08x\n",
449                    pru_control_read_reg(pru, PRU_CTRL_CTBIR0));
450         seq_printf(s, "CTBIR1    := 0x%08x\n",
451                    pru_control_read_reg(pru, PRU_CTRL_CTBIR1));
452         seq_printf(s, "CTPPR0    := 0x%08x\n",
453                    pru_control_read_reg(pru, PRU_CTRL_CTPPR0));
454         seq_printf(s, "CTPPR1    := 0x%08x\n",
455                    pru_control_read_reg(pru, PRU_CTRL_CTPPR1));
457         seq_puts(s, "=============== Debug Registers ===============\n");
458         pru_is_running = pru_control_read_reg(pru, PRU_CTRL_CTRL) &
459                                 CTRL_CTRL_RUNSTATE;
460         if (pru_is_running) {
461                 seq_puts(s, "PRU is executing, cannot print/access debug registers.\n");
462                 return 0;
463         }
465         for (i = 0; i < nregs; i++) {
466                 seq_printf(s, "GPREG%-2d := 0x%08x\tCT_REG%-2d := 0x%08x\n",
467                            i, pru_debug_read_reg(pru, PRU_DEBUG_GPREG(i)),
468                            i, pru_debug_read_reg(pru, PRU_DEBUG_CT_REG(i)));
469         }
471         return 0;
474 DEFINE_SHOW_ATTRIBUTE(regs);
476 /*
477  * Control PRU single-step mode
478  *
479  * This is a debug helper function used for controlling the single-step
480  * mode of the PRU. The PRU Debug registers are not accessible when the
481  * PRU is in RUNNING state.
482  *
483  * Writing a non-zero value sets the PRU into single-step mode irrespective
484  * of its previous state. The PRU mode is saved only on the first set into
485  * a single-step mode. Writing a zero value will restore the PRU into its
486  * original mode.
487  */
488 static int pru_rproc_debug_ss_set(void *data, u64 val)
490         struct rproc *rproc = data;
491         struct pru_rproc *pru = rproc->priv;
492         u32 reg_val;
494         val = val ? 1 : 0;
495         if (!val && !pru->dbg_single_step)
496                 return 0;
498         reg_val = pru_control_read_reg(pru, PRU_CTRL_CTRL);
500         if (val && !pru->dbg_single_step)
501                 pru->dbg_continuous = reg_val;
503         if (val)
504                 reg_val |= CTRL_CTRL_SINGLE_STEP | CTRL_CTRL_EN;
505         else
506                 reg_val = pru->dbg_continuous;
508         pru->dbg_single_step = val;
509         pru_control_write_reg(pru, PRU_CTRL_CTRL, reg_val);
511         return 0;
514 static int pru_rproc_debug_ss_get(void *data, u64 *val)
516         struct rproc *rproc = data;
517         struct pru_rproc *pru = rproc->priv;
519         *val = pru->dbg_single_step;
521         return 0;
523 DEFINE_SIMPLE_ATTRIBUTE(pru_rproc_debug_ss_fops, pru_rproc_debug_ss_get,
524                         pru_rproc_debug_ss_set, "%llu\n");
526 /*
527  * Create PRU-specific debugfs entries
528  *
529  * The entries are created only if the parent remoteproc debugfs directory
530  * exists, and will be cleaned up by the remoteproc core.
531  */
532 static void pru_rproc_create_debug_entries(struct rproc *rproc)
534         if (!rproc->dbg_dir)
535                 return;
537         debugfs_create_file("regs", 0400, rproc->dbg_dir,
538                             rproc, &regs_fops);
539         debugfs_create_file("single_step", 0600, rproc->dbg_dir,
540                             rproc, &pru_rproc_debug_ss_fops);
543 /**
544  * pru_rproc_mbox_callback() - inbound mailbox message handler
545  * @client: mailbox client pointer used for requesting the mailbox channel
546  * @data: mailbox payload
547  *
548  * This handler is invoked by omap's mailbox driver whenever a mailbox
549  * message is received. Usually, the mailbox payload simply contains
550  * the index of the virtqueue that is kicked by the PRU remote processor,
551  * and we let remoteproc core handle it.
552  *
553  * In addition to virtqueue indices, we might also have some out-of-band
554  * values that indicates different events. Those values are deliberately
555  * very big so they don't coincide with virtqueue indices.
556  */
557 static void pru_rproc_mbox_callback(struct mbox_client *client, void *data)
559         struct pru_rproc *pru = container_of(client, struct pru_rproc, client);
560         struct device *dev = &pru->rproc->dev;
561         u32 msg = (u32)data;
563         dev_dbg(dev, "mbox msg: 0x%x\n", msg);
565         /* msg contains the index of the triggered vring */
566         if (rproc_vq_interrupt(pru->rproc, msg) == IRQ_NONE)
567                 dev_dbg(dev, "no message was found in vqid %d\n", msg);
570 /**
571  * pru_rproc_vring_interrupt() - interrupt handler for processing vrings
572  * @irq: irq number associated with the PRU event MPU is listening on
573  * @data: interrupt handler data, will be a PRU rproc structure
574  *
575  * This handler is used by the PRU remoteproc driver when using PRU system
576  * events for processing the virtqueues. Unlike the mailbox IP, there is
577  * no payload associated with an interrupt, so either a unique event is
578  * used for each virtqueue kick, or a both virtqueues are processed on
579  * a single event. The latter is chosen to conserve the usable PRU system
580  * events.
581  */
582 static irqreturn_t pru_rproc_vring_interrupt(int irq, void *data)
584         struct pru_rproc *pru = data;
586         dev_dbg(&pru->rproc->dev, "got vring irq\n");
588         /* process incoming buffers on both the Rx and Tx vrings */
589         rproc_vq_interrupt(pru->rproc, 0);
590         rproc_vq_interrupt(pru->rproc, 1);
592         return IRQ_HANDLED;
595 /* kick a virtqueue */
596 static void pru_rproc_kick(struct rproc *rproc, int vq_id)
598         struct device *dev = &rproc->dev;
599         struct pru_rproc *pru = rproc->priv;
600         int ret;
602         dev_dbg(dev, "kicking vqid %d on PRU%d\n", vq_id, pru->id);
604         if (pru->irq_kick > 0) {
605                 ret = pruss_intc_trigger(pru->irq_kick);
606                 if (ret < 0)
607                         dev_err(dev, "pruss_intc_trigger failed: %d\n", ret);
608         } else if (pru->mbox) {
609                 /*
610                  * send the index of the triggered virtqueue in the mailbox
611                  * payload
612                  */
613                 ret = mbox_send_message(pru->mbox, (void *)vq_id);
614                 if (ret < 0)
615                         dev_err(dev, "mbox_send_message failed: %d\n", ret);
616         }
619 /* start a PRU core */
620 static int pru_rproc_start(struct rproc *rproc)
622         struct device *dev = &rproc->dev;
623         struct pru_rproc *pru = rproc->priv;
624         u32 val;
625         int ret;
627         dev_dbg(dev, "starting PRU%d: entry-point = 0x%x\n",
628                 pru->id, (rproc->bootaddr >> 2));
630         if (!list_empty(&pru->rproc->rvdevs)) {
631                 if (!pru->mbox && (pru->irq_vring <= 0 || pru->irq_kick <= 0)) {
632                         dev_err(dev, "virtio vring interrupt mechanisms are not provided\n");
633                         ret = -EINVAL;
634                         goto fail;
635                 }
637                 if (!pru->mbox && pru->irq_vring > 0) {
638                         ret = request_threaded_irq(pru->irq_vring, NULL,
639                                                    pru_rproc_vring_interrupt,
640                                                    IRQF_ONESHOT, dev_name(dev),
641                                                    pru);
642                         if (ret) {
643                                 dev_err(dev, "failed to enable vring interrupt, ret = %d\n",
644                                         ret);
645                                 goto fail;
646                         }
647                 }
648         }
650         val = CTRL_CTRL_EN | ((rproc->bootaddr >> 2) << 16);
651         pru_control_write_reg(pru, PRU_CTRL_CTRL, val);
653         return 0;
655 fail:
656         if (!pru->dt_irqs && pru->fw_has_intc_rsc)
657                 pruss_intc_unconfigure(pru->pruss, &pru->intc_config);
658         return ret;
661 /* stop/disable a PRU core */
662 static int pru_rproc_stop(struct rproc *rproc)
664         struct device *dev = &rproc->dev;
665         struct pru_rproc *pru = rproc->priv;
666         u32 val;
668         dev_dbg(dev, "stopping PRU%d\n", pru->id);
670         val = pru_control_read_reg(pru, PRU_CTRL_CTRL);
671         val &= ~CTRL_CTRL_EN;
672         pru_control_write_reg(pru, PRU_CTRL_CTRL, val);
674         if (!list_empty(&pru->rproc->rvdevs) &&
675             !pru->mbox && pru->irq_vring > 0)
676                 free_irq(pru->irq_vring, pru);
678         /* undo INTC config */
679         if (!pru->dt_irqs && pru->fw_has_intc_rsc)
680                 pruss_intc_unconfigure(pru->pruss, &pru->intc_config);
682         return 0;
685 /*
686  * parse the custom PRU interrupt map resource and configure the INTC
687  * appropriately
688  */
689 static int pru_handle_vendor_intrmap(struct rproc *rproc,
690                                      struct fw_rsc_vendor *rsc)
692         struct device *dev = rproc->dev.parent;
693         struct pru_rproc *pru = rproc->priv;
694         struct pruss *pruss = pru->pruss;
695         struct pruss_event_chnl *event_chnl_map;
696         struct fw_rsc_pruss_intrmap *intr_rsc =
697                 (struct fw_rsc_pruss_intrmap *)rsc->data;
698         int i, ret;
699         s8 sys_evt, chnl, intr_no;
701         dev_dbg(dev, "version %d event_chnl_map_size %d event_chnl_map_addr 0x%x\n",
702                 rsc->u.st.st_ver, intr_rsc->event_chnl_map_size,
703                 intr_rsc->event_chnl_map_addr);
705         if (rsc->u.st.st_ver != 0) {
706                 dev_err(dev, "only PRU interrupt resource version 0 supported\n");
707                 return -EINVAL;
708         }
710         if (intr_rsc->event_chnl_map_size < 0 ||
711             intr_rsc->event_chnl_map_size >= MAX_PRU_SYS_EVENTS) {
712                 dev_err(dev, "PRU interrupt resource has more events than present on hardware\n");
713                 return -EINVAL;
714         }
716         /*
717          * XXX: The event_chnl_map_addr mapping is currently a pointer in device
718          * memory, evaluate if this needs to be directly in firmware file.
719          */
720         event_chnl_map = pru_d_da_to_va(pru, intr_rsc->event_chnl_map_addr,
721                                         intr_rsc->event_chnl_map_size *
722                                         sizeof(*event_chnl_map));
723         if (!event_chnl_map) {
724                 dev_err(dev, "PRU interrupt resource has inadequate event_chnl_map configuration\n");
725                 return -EINVAL;
726         }
728         /* init intc_config to defaults */
729         for (i = 0; i < ARRAY_SIZE(pru->intc_config.sysev_to_ch); i++)
730                 pru->intc_config.sysev_to_ch[i] = -1;
732         for (i = 0; i < ARRAY_SIZE(pru->intc_config.ch_to_host); i++)
733                 pru->intc_config.ch_to_host[i] = -1;
735         /* parse and fill in system event to interrupt channel mapping */
736         for (i = 0; i < intr_rsc->event_chnl_map_size; i++) {
737                 sys_evt = event_chnl_map[i].event;
738                 chnl = event_chnl_map[i].chnl;
740                 if (sys_evt < 0 || sys_evt >= MAX_PRU_SYS_EVENTS) {
741                         dev_err(dev, "[%d] bad sys event %d\n", i, sys_evt);
742                         return -EINVAL;
743                 }
744                 if (chnl < 0 || chnl >= MAX_PRU_CHANNELS) {
745                         dev_err(dev, "[%d] bad channel value %d\n", i, chnl);
746                         return -EINVAL;
747                 }
749                 pru->intc_config.sysev_to_ch[sys_evt] = chnl;
750                 dev_dbg(dev, "sysevt-to-ch[%d] -> %d\n", sys_evt, chnl);
751         }
753         /* parse and handle interrupt channel-to-host interrupt mapping */
754         for (i = 0; i < MAX_PRU_CHANNELS; i++) {
755                 intr_no = intr_rsc->chnl_host_intr_map[i];
756                 if (intr_no < 0) {
757                         dev_dbg(dev, "skip intr mapping for chnl %d\n", i);
758                         continue;
759                 }
761                 if (intr_no >= MAX_PRU_HOST_INT) {
762                         dev_err(dev, "bad intr mapping for chnl %d, intr_no %d\n",
763                                 i, intr_no);
764                         return -EINVAL;
765                 }
767                 pru->intc_config.ch_to_host[i] = intr_no;
768                 dev_dbg(dev, "chnl-to-host[%d] -> %d\n", i, intr_no);
769         }
771         pru->fw_has_intc_rsc = 1;
773         ret = pruss_intc_configure(pruss, &pru->intc_config);
774         if (ret)
775                 dev_err(dev, "failed to configure pruss intc %d\n", ret);
777         return ret;
780 /* PRU-specific vendor resource handler */
781 static int pru_rproc_handle_vendor_rsc(struct rproc *rproc,
782                                        struct fw_rsc_vendor *rsc)
784         struct device *dev = rproc->dev.parent;
785         struct pru_rproc *pru = rproc->priv;
786         int ret = 0;
788         switch (rsc->u.st.st_type) {
789         case PRUSS_RSC_INTRS:
790                 if (!pru->dt_irqs)
791                         ret = pru_handle_vendor_intrmap(rproc, rsc);
792                 break;
793         default:
794                 dev_err(dev, "%s: cannot handle unknown type %d\n", __func__,
795                         rsc->u.st.st_type);
796                 ret = -EINVAL;
797         }
799         return ret;
802 /*
803  * Convert PRU device address (data spaces only) to kernel virtual address
804  *
805  * Each PRU has access to all data memories within the PRUSS, accessible at
806  * different ranges. So, look through both its primary and secondary Data
807  * RAMs as well as any shared Data RAM to convert a PRU device address to
808  * kernel virtual address. Data RAM0 is primary Data RAM for PRU0 and Data
809  * RAM1 is primary Data RAM for PRU1.
810  */
811 static void *pru_d_da_to_va(struct pru_rproc *pru, u32 da, int len)
813         struct pruss_mem_region dram0, dram1, shrd_ram;
814         struct pruss *pruss = pru->pruss;
815         u32 offset;
816         void *va = NULL;
818         if (len <= 0)
819                 return NULL;
821         dram0 = pruss->mem_regions[PRUSS_MEM_DRAM0];
822         dram1 = pruss->mem_regions[PRUSS_MEM_DRAM1];
823         /* PRU1 has its local RAM addresses reversed */
824         if (pru->id == 1)
825                 swap(dram0, dram1);
826         shrd_ram = pruss->mem_regions[PRUSS_MEM_SHRD_RAM2];
828         if (da >= pru->pdram_da && da + len <= pru->pdram_da + dram0.size) {
829                 offset = da - pru->pdram_da;
830                 va = (__force void *)(dram0.va + offset);
831         } else if (da >= pru->sdram_da &&
832                    da + len <= pru->sdram_da + dram1.size) {
833                 offset = da - pru->sdram_da;
834                 va = (__force void *)(dram1.va + offset);
835         } else if (da >= pru->shrdram_da &&
836                    da + len <= pru->shrdram_da + shrd_ram.size) {
837                 offset = da - pru->shrdram_da;
838                 va = (__force void *)(shrd_ram.va + offset);
839         }
841         return va;
844 /*
845  * Convert PRU device address (instruction space) to kernel virtual address
846  *
847  * A PRU does not have an unified address space. Each PRU has its very own
848  * private Instruction RAM, and its device address is identical to that of
849  * its primary Data RAM device address.
850  */
851 static void *pru_i_da_to_va(struct pru_rproc *pru, u32 da, int len)
853         u32 offset;
854         void *va = NULL;
856         if (len <= 0)
857                 return NULL;
859         if (da >= pru->iram_da &&
860             da + len <= pru->iram_da + pru->mem_regions[PRU_IOMEM_IRAM].size) {
861                 offset = da - pru->iram_da;
862                 va = (__force void *)(pru->mem_regions[PRU_IOMEM_IRAM].va +
863                                       offset);
864         }
866         return va;
869 /* PRU-specific address translator */
870 static void *pru_da_to_va(struct rproc *rproc, u64 da, int len, u32 flags)
872         struct pru_rproc *pru = rproc->priv;
873         void *va;
874         u32 exec_flag;
876         exec_flag = ((flags & RPROC_FLAGS_ELF_SHDR) ? flags & SHF_EXECINSTR :
877                      ((flags & RPROC_FLAGS_ELF_PHDR) ? flags & PF_X : 0));
879         if (exec_flag)
880                 va = pru_i_da_to_va(pru, da, len);
881         else
882                 va = pru_d_da_to_va(pru, da, len);
884         return va;
887 static struct rproc_ops pru_rproc_ops = {
888         .start                  = pru_rproc_start,
889         .stop                   = pru_rproc_stop,
890         .kick                   = pru_rproc_kick,
891         .handle_vendor_rsc      = pru_rproc_handle_vendor_rsc,
892         .da_to_va               = pru_da_to_va,
893 };
895 /*
896  * compute PRU id based on the IRAM addresses. The PRU IRAMs are
897  * always at a particular offset within the PRUSS address space.
898  * The other alternative is to use static data for each core (not a
899  * hardware property to define it in DT), and the id can always be
900  * computated using this inherent address logic.
901  */
902 static int pru_rproc_set_id(struct pru_rproc *pru)
904         int ret = 0;
905         u32 mask1 = PRU0_IRAM_ADDR_MASK;
906         u32 mask2 = PRU1_IRAM_ADDR_MASK;
908         if ((pru->mem_regions[PRU_IOMEM_IRAM].pa & mask1) == mask1)
909                 pru->id = 0;
910         else if ((pru->mem_regions[PRU_IOMEM_IRAM].pa & mask2) == mask2)
911                 pru->id = 1;
912         else
913                 ret = -EINVAL;
915         return ret;
918 static int pru_rproc_probe(struct platform_device *pdev)
920         struct device *dev = &pdev->dev;
921         struct device_node *np = dev->of_node;
922         struct platform_device *ppdev = to_platform_device(dev->parent);
923         struct pru_rproc *pru;
924         const char *fw_name;
925         struct rproc *rproc = NULL;
926         struct mbox_client *client;
927         struct resource *res;
928         int i, ret;
929         const char *mem_names[PRU_IOMEM_MAX] = { "iram", "control", "debug" };
931         if (!np) {
932                 dev_err(dev, "Non-DT platform device not supported\n");
933                 return -ENODEV;
934         }
936         ret = of_property_read_string(np, "firmware-name", &fw_name);
937         if (ret) {
938                 dev_err(dev, "unable to retrieve firmware-name %d\n", ret);
939                 return ret;
940         }
942         rproc = rproc_alloc(dev, pdev->name, &pru_rproc_ops, fw_name,
943                             sizeof(*pru));
944         if (!rproc) {
945                 dev_err(dev, "rproc_alloc failed\n");
946                 return -ENOMEM;
947         }
948         /* error recovery is not supported for PRUs */
949         rproc->recovery_disabled = true;
951         /*
952          * rproc_add will auto-boot the processor normally, but this is
953          * not desired with PRU client driven boot-flow methodology. A PRU
954          * application/client driver will boot the corresponding PRU
955          * remote-processor as part of its state machine either through
956          * the remoteproc sysfs interface or through the equivalent kernel API
957          */
958         rproc->auto_boot = false;
960         pru = rproc->priv;
961         pru->pruss = platform_get_drvdata(ppdev);
962         pru->rproc = rproc;
963         pru->fw_name = fw_name;
964         spin_lock_init(&pru->rmw_lock);
965         mutex_init(&pru->lock);
967         /* XXX: get this from match data if different in the future */
968         pru->iram_da = 0;
969         pru->pdram_da = 0;
970         pru->sdram_da = 0x2000;
971         pru->shrdram_da = 0x10000;
973         for (i = 0; i < ARRAY_SIZE(mem_names); i++) {
974                 res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
975                                                    mem_names[i]);
976                 pru->mem_regions[i].va = devm_ioremap_resource(dev, res);
977                 if (IS_ERR(pru->mem_regions[i].va)) {
978                         dev_err(dev, "failed to parse and map memory resource %d %s\n",
979                                 i, mem_names[i]);
980                         ret = PTR_ERR(pru->mem_regions[i].va);
981                         goto free_rproc;
982                 }
983                 pru->mem_regions[i].pa = res->start;
984                 pru->mem_regions[i].size = resource_size(res);
986                 dev_dbg(dev, "memory %8s: pa %pa size 0x%zx va %p\n",
987                         mem_names[i], &pru->mem_regions[i].pa,
988                         pru->mem_regions[i].size, pru->mem_regions[i].va);
989         }
991         ret = pru_rproc_set_id(pru);
992         if (ret < 0)
993                 goto free_rproc;
995         platform_set_drvdata(pdev, rproc);
997         /* get optional vring and kick interrupts for supporting virtio rpmsg */
998         pru->irq_vring = platform_get_irq_byname(pdev, "vring");
999         if (pru->irq_vring <= 0) {
1000                 ret = pru->irq_vring;
1001                 if (ret == -EPROBE_DEFER)
1002                         goto free_rproc;
1003                 dev_dbg(dev, "unable to get vring interrupt, status = %d\n",
1004                         ret);
1005         }
1007         pru->irq_kick = platform_get_irq_byname(pdev, "kick");
1008         if (pru->irq_kick <= 0) {
1009                 ret = pru->irq_kick;
1010                 if (ret == -EPROBE_DEFER)
1011                         goto free_rproc;
1012                 dev_dbg(dev, "unable to get kick interrupt, status = %d\n",
1013                         ret);
1014         }
1016         /*
1017          * get optional mailbox for virtio rpmsg signalling if vring and kick
1018          * interrupts are not specified for OMAP architecture based SoCs
1019          */
1020         if (pru->irq_vring <= 0 && pru->irq_kick <= 0 &&
1021             !of_device_is_compatible(np, "ti,k2g-pru")) {
1022                 client = &pru->client;
1023                 client->dev = dev;
1024                 client->tx_done = NULL;
1025                 client->rx_callback = pru_rproc_mbox_callback;
1026                 client->tx_block = false;
1027                 client->knows_txdone = false;
1028                 pru->mbox = mbox_request_channel(client, 0);
1029                 if (IS_ERR(pru->mbox)) {
1030                         ret = PTR_ERR(pru->mbox);
1031                         pru->mbox = NULL;
1032                         dev_dbg(dev, "unable to get mailbox channel, status = %d\n",
1033                                 ret);
1034                 }
1035         }
1037         ret = rproc_add(pru->rproc);
1038         if (ret) {
1039                 dev_err(dev, "rproc_add failed: %d\n", ret);
1040                 goto put_mbox;
1041         }
1043         pru_rproc_create_debug_entries(rproc);
1045         dev_info(dev, "PRU rproc node %s probed successfully\n", np->full_name);
1047         return 0;
1049 put_mbox:
1050         mbox_free_channel(pru->mbox);
1051 free_rproc:
1052         rproc_free(rproc);
1053         return ret;
1056 static int pru_rproc_remove(struct platform_device *pdev)
1058         struct device *dev = &pdev->dev;
1059         struct rproc *rproc = platform_get_drvdata(pdev);
1060         struct pru_rproc *pru = rproc->priv;
1062         dev_info(dev, "%s: removing rproc %s\n", __func__, rproc->name);
1064         mbox_free_channel(pru->mbox);
1066         rproc_del(rproc);
1067         rproc_free(rproc);
1069         return 0;
1072 static const struct of_device_id pru_rproc_match[] = {
1073         { .compatible = "ti,am3356-pru", },
1074         { .compatible = "ti,am4376-pru", },
1075         { .compatible = "ti,am5728-pru", },
1076         { .compatible = "ti,k2g-pru",    },
1077         {},
1078 };
1079 MODULE_DEVICE_TABLE(of, pru_rproc_match);
1081 static struct platform_driver pru_rproc_driver = {
1082         .driver = {
1083                 .name   = "pru-rproc",
1084                 .of_match_table = pru_rproc_match,
1085                 .suppress_bind_attrs = true,
1086         },
1087         .probe  = pru_rproc_probe,
1088         .remove = pru_rproc_remove,
1089 };
1090 module_platform_driver(pru_rproc_driver);
1092 MODULE_AUTHOR("Suman Anna <s-anna@ti.com>");
1093 MODULE_DESCRIPTION("PRU-ICSS Remote Processor Driver");
1094 MODULE_LICENSE("GPL v2");