remoteproc: pru: Add support for PRU specific interrupt configuration
[linux-2.6-microblaze.git] / drivers / remoteproc / pru_rproc.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * PRU-ICSS remoteproc driver for various TI SoCs
4  *
5  * Copyright (C) 2014-2020 Texas Instruments Incorporated - https://www.ti.com/
6  *
7  * Author(s):
8  *      Suman Anna <s-anna@ti.com>
9  *      Andrew F. Davis <afd@ti.com>
10  *      Grzegorz Jaszczyk <grzegorz.jaszczyk@linaro.org> for Texas Instruments
11  */
12
13 #include <linux/bitops.h>
14 #include <linux/irqdomain.h>
15 #include <linux/module.h>
16 #include <linux/of_device.h>
17 #include <linux/of_irq.h>
18 #include <linux/pruss_driver.h>
19 #include <linux/remoteproc.h>
20
21 #include "remoteproc_internal.h"
22 #include "remoteproc_elf_helpers.h"
23 #include "pru_rproc.h"
24
25 /* PRU_ICSS_PRU_CTRL registers */
26 #define PRU_CTRL_CTRL           0x0000
27 #define PRU_CTRL_STS            0x0004
28
29 /* CTRL register bit-fields */
30 #define CTRL_CTRL_SOFT_RST_N    BIT(0)
31 #define CTRL_CTRL_EN            BIT(1)
32 #define CTRL_CTRL_SLEEPING      BIT(2)
33 #define CTRL_CTRL_CTR_EN        BIT(3)
34 #define CTRL_CTRL_SINGLE_STEP   BIT(8)
35 #define CTRL_CTRL_RUNSTATE      BIT(15)
36
37 /* PRU Core IRAM address masks */
38 #define PRU_IRAM_ADDR_MASK      0x3ffff
39 #define PRU0_IRAM_ADDR_MASK     0x34000
40 #define PRU1_IRAM_ADDR_MASK     0x38000
41
42 /* PRU device addresses for various type of PRU RAMs */
43 #define PRU_IRAM_DA     0       /* Instruction RAM */
44 #define PRU_PDRAM_DA    0       /* Primary Data RAM */
45 #define PRU_SDRAM_DA    0x2000  /* Secondary Data RAM */
46 #define PRU_SHRDRAM_DA  0x10000 /* Shared Data RAM */
47
48 #define MAX_PRU_SYS_EVENTS 160
49
50 /**
51  * enum pru_iomem - PRU core memory/register range identifiers
52  *
53  * @PRU_IOMEM_IRAM: PRU Instruction RAM range
54  * @PRU_IOMEM_CTRL: PRU Control register range
55  * @PRU_IOMEM_DEBUG: PRU Debug register range
56  * @PRU_IOMEM_MAX: just keep this one at the end
57  */
58 enum pru_iomem {
59         PRU_IOMEM_IRAM = 0,
60         PRU_IOMEM_CTRL,
61         PRU_IOMEM_DEBUG,
62         PRU_IOMEM_MAX,
63 };
64
65 /**
66  * struct pru_rproc - PRU remoteproc structure
67  * @id: id of the PRU core within the PRUSS
68  * @dev: PRU core device pointer
69  * @pruss: back-reference to parent PRUSS structure
70  * @rproc: remoteproc pointer for this PRU core
71  * @mem_regions: data for each of the PRU memory regions
72  * @fw_name: name of firmware image used during loading
73  * @mapped_irq: virtual interrupt numbers of created fw specific mapping
74  * @pru_interrupt_map: pointer to interrupt mapping description (firmware)
75  * @pru_interrupt_map_sz: pru_interrupt_map size
76  * @evt_count: number of mapped events
77  */
78 struct pru_rproc {
79         int id;
80         struct device *dev;
81         struct pruss *pruss;
82         struct rproc *rproc;
83         struct pruss_mem_region mem_regions[PRU_IOMEM_MAX];
84         const char *fw_name;
85         unsigned int *mapped_irq;
86         struct pru_irq_rsc *pru_interrupt_map;
87         size_t pru_interrupt_map_sz;
88         u8 evt_count;
89 };
90
91 static inline u32 pru_control_read_reg(struct pru_rproc *pru, unsigned int reg)
92 {
93         return readl_relaxed(pru->mem_regions[PRU_IOMEM_CTRL].va + reg);
94 }
95
96 static inline
97 void pru_control_write_reg(struct pru_rproc *pru, unsigned int reg, u32 val)
98 {
99         writel_relaxed(val, pru->mem_regions[PRU_IOMEM_CTRL].va + reg);
100 }
101
102 static void pru_dispose_irq_mapping(struct pru_rproc *pru)
103 {
104         while (pru->evt_count--) {
105                 if (pru->mapped_irq[pru->evt_count] > 0)
106                         irq_dispose_mapping(pru->mapped_irq[pru->evt_count]);
107         }
108
109         kfree(pru->mapped_irq);
110 }
111
112 /*
113  * Parse the custom PRU interrupt map resource and configure the INTC
114  * appropriately.
115  */
116 static int pru_handle_intrmap(struct rproc *rproc)
117 {
118         struct device *dev = rproc->dev.parent;
119         struct pru_rproc *pru = rproc->priv;
120         struct pru_irq_rsc *rsc = pru->pru_interrupt_map;
121         struct irq_fwspec fwspec;
122         struct device_node *irq_parent;
123         int i, ret = 0;
124
125         /* not having pru_interrupt_map is not an error */
126         if (!rsc)
127                 return 0;
128
129         /* currently supporting only type 0 */
130         if (rsc->type != 0) {
131                 dev_err(dev, "unsupported rsc type: %d\n", rsc->type);
132                 return -EINVAL;
133         }
134
135         if (rsc->num_evts > MAX_PRU_SYS_EVENTS)
136                 return -EINVAL;
137
138         if (sizeof(*rsc) + rsc->num_evts * sizeof(struct pruss_int_map) !=
139             pru->pru_interrupt_map_sz)
140                 return -EINVAL;
141
142         pru->evt_count = rsc->num_evts;
143         pru->mapped_irq = kcalloc(pru->evt_count, sizeof(unsigned int),
144                                   GFP_KERNEL);
145         if (!pru->mapped_irq)
146                 return -ENOMEM;
147
148         /*
149          * parse and fill in system event to interrupt channel and
150          * channel-to-host mapping
151          */
152         irq_parent = of_irq_find_parent(pru->dev->of_node);
153         if (!irq_parent) {
154                 kfree(pru->mapped_irq);
155                 return -ENODEV;
156         }
157
158         fwspec.fwnode = of_node_to_fwnode(irq_parent);
159         fwspec.param_count = 3;
160         for (i = 0; i < pru->evt_count; i++) {
161                 fwspec.param[0] = rsc->pru_intc_map[i].event;
162                 fwspec.param[1] = rsc->pru_intc_map[i].chnl;
163                 fwspec.param[2] = rsc->pru_intc_map[i].host;
164
165                 dev_dbg(dev, "mapping%d: event %d, chnl %d, host %d\n",
166                         i, fwspec.param[0], fwspec.param[1], fwspec.param[2]);
167
168                 pru->mapped_irq[i] = irq_create_fwspec_mapping(&fwspec);
169                 if (!pru->mapped_irq[i]) {
170                         dev_err(dev, "failed to get virq\n");
171                         ret = pru->mapped_irq[i];
172                         goto map_fail;
173                 }
174         }
175
176         return ret;
177
178 map_fail:
179         pru_dispose_irq_mapping(pru);
180
181         return ret;
182 }
183
184 static int pru_rproc_start(struct rproc *rproc)
185 {
186         struct device *dev = &rproc->dev;
187         struct pru_rproc *pru = rproc->priv;
188         u32 val;
189         int ret;
190
191         dev_dbg(dev, "starting PRU%d: entry-point = 0x%llx\n",
192                 pru->id, (rproc->bootaddr >> 2));
193
194         ret = pru_handle_intrmap(rproc);
195         /*
196          * reset references to pru interrupt map - they will stop being valid
197          * after rproc_start returns
198          */
199         pru->pru_interrupt_map = NULL;
200         pru->pru_interrupt_map_sz = 0;
201         if (ret)
202                 return ret;
203
204         val = CTRL_CTRL_EN | ((rproc->bootaddr >> 2) << 16);
205         pru_control_write_reg(pru, PRU_CTRL_CTRL, val);
206
207         return 0;
208 }
209
210 static int pru_rproc_stop(struct rproc *rproc)
211 {
212         struct device *dev = &rproc->dev;
213         struct pru_rproc *pru = rproc->priv;
214         u32 val;
215
216         dev_dbg(dev, "stopping PRU%d\n", pru->id);
217
218         val = pru_control_read_reg(pru, PRU_CTRL_CTRL);
219         val &= ~CTRL_CTRL_EN;
220         pru_control_write_reg(pru, PRU_CTRL_CTRL, val);
221
222         /* dispose irq mapping - new firmware can provide new mapping */
223         if (pru->mapped_irq)
224                 pru_dispose_irq_mapping(pru);
225
226         return 0;
227 }
228
229 /*
230  * Convert PRU device address (data spaces only) to kernel virtual address.
231  *
232  * Each PRU has access to all data memories within the PRUSS, accessible at
233  * different ranges. So, look through both its primary and secondary Data
234  * RAMs as well as any shared Data RAM to convert a PRU device address to
235  * kernel virtual address. Data RAM0 is primary Data RAM for PRU0 and Data
236  * RAM1 is primary Data RAM for PRU1.
237  */
238 static void *pru_d_da_to_va(struct pru_rproc *pru, u32 da, size_t len)
239 {
240         struct pruss_mem_region dram0, dram1, shrd_ram;
241         struct pruss *pruss = pru->pruss;
242         u32 offset;
243         void *va = NULL;
244
245         if (len == 0)
246                 return NULL;
247
248         dram0 = pruss->mem_regions[PRUSS_MEM_DRAM0];
249         dram1 = pruss->mem_regions[PRUSS_MEM_DRAM1];
250         /* PRU1 has its local RAM addresses reversed */
251         if (pru->id == 1)
252                 swap(dram0, dram1);
253         shrd_ram = pruss->mem_regions[PRUSS_MEM_SHRD_RAM2];
254
255         if (da >= PRU_PDRAM_DA && da + len <= PRU_PDRAM_DA + dram0.size) {
256                 offset = da - PRU_PDRAM_DA;
257                 va = (__force void *)(dram0.va + offset);
258         } else if (da >= PRU_SDRAM_DA &&
259                    da + len <= PRU_SDRAM_DA + dram1.size) {
260                 offset = da - PRU_SDRAM_DA;
261                 va = (__force void *)(dram1.va + offset);
262         } else if (da >= PRU_SHRDRAM_DA &&
263                    da + len <= PRU_SHRDRAM_DA + shrd_ram.size) {
264                 offset = da - PRU_SHRDRAM_DA;
265                 va = (__force void *)(shrd_ram.va + offset);
266         }
267
268         return va;
269 }
270
271 /*
272  * Convert PRU device address (instruction space) to kernel virtual address.
273  *
274  * A PRU does not have an unified address space. Each PRU has its very own
275  * private Instruction RAM, and its device address is identical to that of
276  * its primary Data RAM device address.
277  */
278 static void *pru_i_da_to_va(struct pru_rproc *pru, u32 da, size_t len)
279 {
280         u32 offset;
281         void *va = NULL;
282
283         if (len == 0)
284                 return NULL;
285
286         if (da >= PRU_IRAM_DA &&
287             da + len <= PRU_IRAM_DA + pru->mem_regions[PRU_IOMEM_IRAM].size) {
288                 offset = da - PRU_IRAM_DA;
289                 va = (__force void *)(pru->mem_regions[PRU_IOMEM_IRAM].va +
290                                       offset);
291         }
292
293         return va;
294 }
295
296 /*
297  * Provide address translations for only PRU Data RAMs through the remoteproc
298  * core for any PRU client drivers. The PRU Instruction RAM access is restricted
299  * only to the PRU loader code.
300  */
301 static void *pru_rproc_da_to_va(struct rproc *rproc, u64 da, size_t len)
302 {
303         struct pru_rproc *pru = rproc->priv;
304
305         return pru_d_da_to_va(pru, da, len);
306 }
307
308 /* PRU-specific address translator used by PRU loader. */
309 static void *pru_da_to_va(struct rproc *rproc, u64 da, size_t len, bool is_iram)
310 {
311         struct pru_rproc *pru = rproc->priv;
312         void *va;
313
314         if (is_iram)
315                 va = pru_i_da_to_va(pru, da, len);
316         else
317                 va = pru_d_da_to_va(pru, da, len);
318
319         return va;
320 }
321
322 static struct rproc_ops pru_rproc_ops = {
323         .start          = pru_rproc_start,
324         .stop           = pru_rproc_stop,
325         .da_to_va       = pru_rproc_da_to_va,
326 };
327
328 static int
329 pru_rproc_load_elf_segments(struct rproc *rproc, const struct firmware *fw)
330 {
331         struct device *dev = &rproc->dev;
332         struct elf32_hdr *ehdr;
333         struct elf32_phdr *phdr;
334         int i, ret = 0;
335         const u8 *elf_data = fw->data;
336
337         ehdr = (struct elf32_hdr *)elf_data;
338         phdr = (struct elf32_phdr *)(elf_data + ehdr->e_phoff);
339
340         /* go through the available ELF segments */
341         for (i = 0; i < ehdr->e_phnum; i++, phdr++) {
342                 u32 da = phdr->p_paddr;
343                 u32 memsz = phdr->p_memsz;
344                 u32 filesz = phdr->p_filesz;
345                 u32 offset = phdr->p_offset;
346                 bool is_iram;
347                 void *ptr;
348
349                 if (phdr->p_type != PT_LOAD || !filesz)
350                         continue;
351
352                 dev_dbg(dev, "phdr: type %d da 0x%x memsz 0x%x filesz 0x%x\n",
353                         phdr->p_type, da, memsz, filesz);
354
355                 if (filesz > memsz) {
356                         dev_err(dev, "bad phdr filesz 0x%x memsz 0x%x\n",
357                                 filesz, memsz);
358                         ret = -EINVAL;
359                         break;
360                 }
361
362                 if (offset + filesz > fw->size) {
363                         dev_err(dev, "truncated fw: need 0x%x avail 0x%zx\n",
364                                 offset + filesz, fw->size);
365                         ret = -EINVAL;
366                         break;
367                 }
368
369                 /* grab the kernel address for this device address */
370                 is_iram = phdr->p_flags & PF_X;
371                 ptr = pru_da_to_va(rproc, da, memsz, is_iram);
372                 if (!ptr) {
373                         dev_err(dev, "bad phdr da 0x%x mem 0x%x\n", da, memsz);
374                         ret = -EINVAL;
375                         break;
376                 }
377
378                 memcpy(ptr, elf_data + phdr->p_offset, filesz);
379
380                 /* skip the memzero logic performed by remoteproc ELF loader */
381         }
382
383         return ret;
384 }
385
386 static const void *
387 pru_rproc_find_interrupt_map(struct device *dev, const struct firmware *fw)
388 {
389         struct elf32_shdr *shdr, *name_table_shdr;
390         const char *name_table;
391         const u8 *elf_data = fw->data;
392         struct elf32_hdr *ehdr = (struct elf32_hdr *)elf_data;
393         u16 shnum = ehdr->e_shnum;
394         u16 shstrndx = ehdr->e_shstrndx;
395         int i;
396
397         /* first, get the section header */
398         shdr = (struct elf32_shdr *)(elf_data + ehdr->e_shoff);
399         /* compute name table section header entry in shdr array */
400         name_table_shdr = shdr + shstrndx;
401         /* finally, compute the name table section address in elf */
402         name_table = elf_data + name_table_shdr->sh_offset;
403
404         for (i = 0; i < shnum; i++, shdr++) {
405                 u32 size = shdr->sh_size;
406                 u32 offset = shdr->sh_offset;
407                 u32 name = shdr->sh_name;
408
409                 if (strcmp(name_table + name, ".pru_irq_map"))
410                         continue;
411
412                 /* make sure we have the entire irq map */
413                 if (offset + size > fw->size || offset + size < size) {
414                         dev_err(dev, ".pru_irq_map section truncated\n");
415                         return ERR_PTR(-EINVAL);
416                 }
417
418                 /* make sure irq map has at least the header */
419                 if (sizeof(struct pru_irq_rsc) > size) {
420                         dev_err(dev, "header-less .pru_irq_map section\n");
421                         return ERR_PTR(-EINVAL);
422                 }
423
424                 return shdr;
425         }
426
427         dev_dbg(dev, "no .pru_irq_map section found for this fw\n");
428
429         return NULL;
430 }
431
432 /*
433  * Use a custom parse_fw callback function for dealing with PRU firmware
434  * specific sections.
435  *
436  * The firmware blob can contain optional ELF sections: .resource_table section
437  * and .pru_irq_map one. The second one contains the PRUSS interrupt mapping
438  * description, which needs to be setup before powering on the PRU core. To
439  * avoid RAM wastage this ELF section is not mapped to any ELF segment (by the
440  * firmware linker) and therefore is not loaded to PRU memory.
441  */
442 static int pru_rproc_parse_fw(struct rproc *rproc, const struct firmware *fw)
443 {
444         struct device *dev = &rproc->dev;
445         struct pru_rproc *pru = rproc->priv;
446         const u8 *elf_data = fw->data;
447         const void *shdr;
448         u8 class = fw_elf_get_class(fw);
449         u64 sh_offset;
450         int ret;
451
452         /* load optional rsc table */
453         ret = rproc_elf_load_rsc_table(rproc, fw);
454         if (ret == -EINVAL)
455                 dev_dbg(&rproc->dev, "no resource table found for this fw\n");
456         else if (ret)
457                 return ret;
458
459         /* find .pru_interrupt_map section, not having it is not an error */
460         shdr = pru_rproc_find_interrupt_map(dev, fw);
461         if (IS_ERR(shdr))
462                 return PTR_ERR(shdr);
463
464         if (!shdr)
465                 return 0;
466
467         /* preserve pointer to PRU interrupt map together with it size */
468         sh_offset = elf_shdr_get_sh_offset(class, shdr);
469         pru->pru_interrupt_map = (struct pru_irq_rsc *)(elf_data + sh_offset);
470         pru->pru_interrupt_map_sz = elf_shdr_get_sh_size(class, shdr);
471
472         return 0;
473 }
474
475 /*
476  * Compute PRU id based on the IRAM addresses. The PRU IRAMs are
477  * always at a particular offset within the PRUSS address space.
478  */
479 static int pru_rproc_set_id(struct pru_rproc *pru)
480 {
481         int ret = 0;
482
483         switch (pru->mem_regions[PRU_IOMEM_IRAM].pa & PRU_IRAM_ADDR_MASK) {
484         case PRU0_IRAM_ADDR_MASK:
485                 pru->id = 0;
486                 break;
487         case PRU1_IRAM_ADDR_MASK:
488                 pru->id = 1;
489                 break;
490         default:
491                 ret = -EINVAL;
492         }
493
494         return ret;
495 }
496
497 static int pru_rproc_probe(struct platform_device *pdev)
498 {
499         struct device *dev = &pdev->dev;
500         struct device_node *np = dev->of_node;
501         struct platform_device *ppdev = to_platform_device(dev->parent);
502         struct pru_rproc *pru;
503         const char *fw_name;
504         struct rproc *rproc = NULL;
505         struct resource *res;
506         int i, ret;
507         const char *mem_names[PRU_IOMEM_MAX] = { "iram", "control", "debug" };
508
509         ret = of_property_read_string(np, "firmware-name", &fw_name);
510         if (ret) {
511                 dev_err(dev, "unable to retrieve firmware-name %d\n", ret);
512                 return ret;
513         }
514
515         rproc = devm_rproc_alloc(dev, pdev->name, &pru_rproc_ops, fw_name,
516                                  sizeof(*pru));
517         if (!rproc) {
518                 dev_err(dev, "rproc_alloc failed\n");
519                 return -ENOMEM;
520         }
521         /* use a custom load function to deal with PRU-specific quirks */
522         rproc->ops->load = pru_rproc_load_elf_segments;
523
524         /* use a custom parse function to deal with PRU-specific resources */
525         rproc->ops->parse_fw = pru_rproc_parse_fw;
526
527         /* error recovery is not supported for PRUs */
528         rproc->recovery_disabled = true;
529
530         /*
531          * rproc_add will auto-boot the processor normally, but this is not
532          * desired with PRU client driven boot-flow methodology. A PRU
533          * application/client driver will boot the corresponding PRU
534          * remote-processor as part of its state machine either through the
535          * remoteproc sysfs interface or through the equivalent kernel API.
536          */
537         rproc->auto_boot = false;
538
539         pru = rproc->priv;
540         pru->dev = dev;
541         pru->pruss = platform_get_drvdata(ppdev);
542         pru->rproc = rproc;
543         pru->fw_name = fw_name;
544
545         for (i = 0; i < ARRAY_SIZE(mem_names); i++) {
546                 res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
547                                                    mem_names[i]);
548                 pru->mem_regions[i].va = devm_ioremap_resource(dev, res);
549                 if (IS_ERR(pru->mem_regions[i].va)) {
550                         dev_err(dev, "failed to parse and map memory resource %d %s\n",
551                                 i, mem_names[i]);
552                         ret = PTR_ERR(pru->mem_regions[i].va);
553                         return ret;
554                 }
555                 pru->mem_regions[i].pa = res->start;
556                 pru->mem_regions[i].size = resource_size(res);
557
558                 dev_dbg(dev, "memory %8s: pa %pa size 0x%zx va %pK\n",
559                         mem_names[i], &pru->mem_regions[i].pa,
560                         pru->mem_regions[i].size, pru->mem_regions[i].va);
561         }
562
563         ret = pru_rproc_set_id(pru);
564         if (ret < 0)
565                 return ret;
566
567         platform_set_drvdata(pdev, rproc);
568
569         ret = devm_rproc_add(dev, pru->rproc);
570         if (ret) {
571                 dev_err(dev, "rproc_add failed: %d\n", ret);
572                 return ret;
573         }
574
575         dev_dbg(dev, "PRU rproc node %pOF probed successfully\n", np);
576
577         return 0;
578 }
579
580 static int pru_rproc_remove(struct platform_device *pdev)
581 {
582         struct device *dev = &pdev->dev;
583         struct rproc *rproc = platform_get_drvdata(pdev);
584
585         dev_dbg(dev, "%s: removing rproc %s\n", __func__, rproc->name);
586
587         return 0;
588 }
589
590 static const struct of_device_id pru_rproc_match[] = {
591         { .compatible = "ti,am3356-pru", },
592         { .compatible = "ti,am4376-pru", },
593         { .compatible = "ti,am5728-pru", },
594         { .compatible = "ti,k2g-pru",    },
595         {},
596 };
597 MODULE_DEVICE_TABLE(of, pru_rproc_match);
598
599 static struct platform_driver pru_rproc_driver = {
600         .driver = {
601                 .name   = "pru-rproc",
602                 .of_match_table = pru_rproc_match,
603                 .suppress_bind_attrs = true,
604         },
605         .probe  = pru_rproc_probe,
606         .remove = pru_rproc_remove,
607 };
608 module_platform_driver(pru_rproc_driver);
609
610 MODULE_AUTHOR("Suman Anna <s-anna@ti.com>");
611 MODULE_AUTHOR("Andrew F. Davis <afd@ti.com>");
612 MODULE_AUTHOR("Grzegorz Jaszczyk <grzegorz.jaszczyk@linaro.org>");
613 MODULE_DESCRIPTION("PRU-ICSS Remote Processor Driver");
614 MODULE_LICENSE("GPL v2");