Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

remoteproc: pru: Add support for PRU specific interrupt configuration

The firmware blob can contain optional ELF sections: .resource_table
section and .pru_irq_map one. The second one contains the PRUSS
interrupt mapping description, which needs to be setup before powering
on the PRU core. To avoid RAM wastage this ELF section is not mapped to
any ELF segment (by the firmware linker) and therefore is not loaded to
PRU memory.

The PRU interrupt configuration is handled within the PRUSS INTC irqchip
driver and leverages the system events to interrupt channels and host
interrupts mapping configuration. Relevant irq routing information is
passed through a special .pru_irq_map ELF section (for interrupts routed
to and used by PRU cores) or via the PRU application's device tree node
(for interrupts routed to and used by the main CPU). The mappings are
currently programmed during the booting/shutdown of the PRU.

The interrupt configuration passed through .pru_irq_map ELF section is
optional. It varies on specific firmware functionality and therefore
have to be unwinded during PRU stop and performed again during
PRU start.

Reviewed-by: Mathieu Poirier <mathieu.poirier@linaro.org>
Co-developed-by: Suman Anna <s-anna@ti.com>
Signed-off-by: Suman Anna <s-anna@ti.com>
Signed-off-by: Grzegorz Jaszczyk <grzegorz.jaszczyk@linaro.org>
Link: https://lore.kernel.org/r/20201208141002.17777-4-grzegorz.jaszczyk@linaro.org
Signed-off-by: Bjorn Andersson <bjorn.andersson@linaro.org>

authored by

Grzegorz Jaszczyk and committed by
Bjorn Andersson
c75c9fda d4ce2de7

+227
+181
drivers/remoteproc/pru_rproc.c
··· 11 11 */ 12 12 13 13 #include <linux/bitops.h> 14 + #include <linux/irqdomain.h> 14 15 #include <linux/module.h> 15 16 #include <linux/of_device.h> 17 + #include <linux/of_irq.h> 16 18 #include <linux/pruss_driver.h> 17 19 #include <linux/remoteproc.h> 18 20 19 21 #include "remoteproc_internal.h" 20 22 #include "remoteproc_elf_helpers.h" 23 + #include "pru_rproc.h" 21 24 22 25 /* PRU_ICSS_PRU_CTRL registers */ 23 26 #define PRU_CTRL_CTRL 0x0000 ··· 45 42 #define PRU_SDRAM_DA 0x2000 /* Secondary Data RAM */ 46 43 #define PRU_SHRDRAM_DA 0x10000 /* Shared Data RAM */ 47 44 45 + #define MAX_PRU_SYS_EVENTS 160 46 + 48 47 /** 49 48 * enum pru_iomem - PRU core memory/register range identifiers 50 49 * ··· 70 65 * @rproc: remoteproc pointer for this PRU core 71 66 * @mem_regions: data for each of the PRU memory regions 72 67 * @fw_name: name of firmware image used during loading 68 + * @mapped_irq: virtual interrupt numbers of created fw specific mapping 69 + * @pru_interrupt_map: pointer to interrupt mapping description (firmware) 70 + * @pru_interrupt_map_sz: pru_interrupt_map size 71 + * @evt_count: number of mapped events 73 72 */ 74 73 struct pru_rproc { 75 74 int id; ··· 82 73 struct rproc *rproc; 83 74 struct pruss_mem_region mem_regions[PRU_IOMEM_MAX]; 84 75 const char *fw_name; 76 + unsigned int *mapped_irq; 77 + struct pru_irq_rsc *pru_interrupt_map; 78 + size_t pru_interrupt_map_sz; 79 + u8 evt_count; 85 80 }; 86 81 87 82 static inline u32 pru_control_read_reg(struct pru_rproc *pru, unsigned int reg) ··· 99 86 writel_relaxed(val, pru->mem_regions[PRU_IOMEM_CTRL].va + reg); 100 87 } 101 88 89 + static void pru_dispose_irq_mapping(struct pru_rproc *pru) 90 + { 91 + while (pru->evt_count--) { 92 + if (pru->mapped_irq[pru->evt_count] > 0) 93 + irq_dispose_mapping(pru->mapped_irq[pru->evt_count]); 94 + } 95 + 96 + kfree(pru->mapped_irq); 97 + } 98 + 99 + /* 100 + * Parse the custom PRU interrupt map resource and configure the INTC 101 + * appropriately. 102 + */ 103 + static int pru_handle_intrmap(struct rproc *rproc) 104 + { 105 + struct device *dev = rproc->dev.parent; 106 + struct pru_rproc *pru = rproc->priv; 107 + struct pru_irq_rsc *rsc = pru->pru_interrupt_map; 108 + struct irq_fwspec fwspec; 109 + struct device_node *irq_parent; 110 + int i, ret = 0; 111 + 112 + /* not having pru_interrupt_map is not an error */ 113 + if (!rsc) 114 + return 0; 115 + 116 + /* currently supporting only type 0 */ 117 + if (rsc->type != 0) { 118 + dev_err(dev, "unsupported rsc type: %d\n", rsc->type); 119 + return -EINVAL; 120 + } 121 + 122 + if (rsc->num_evts > MAX_PRU_SYS_EVENTS) 123 + return -EINVAL; 124 + 125 + if (sizeof(*rsc) + rsc->num_evts * sizeof(struct pruss_int_map) != 126 + pru->pru_interrupt_map_sz) 127 + return -EINVAL; 128 + 129 + pru->evt_count = rsc->num_evts; 130 + pru->mapped_irq = kcalloc(pru->evt_count, sizeof(unsigned int), 131 + GFP_KERNEL); 132 + if (!pru->mapped_irq) 133 + return -ENOMEM; 134 + 135 + /* 136 + * parse and fill in system event to interrupt channel and 137 + * channel-to-host mapping 138 + */ 139 + irq_parent = of_irq_find_parent(pru->dev->of_node); 140 + if (!irq_parent) { 141 + kfree(pru->mapped_irq); 142 + return -ENODEV; 143 + } 144 + 145 + fwspec.fwnode = of_node_to_fwnode(irq_parent); 146 + fwspec.param_count = 3; 147 + for (i = 0; i < pru->evt_count; i++) { 148 + fwspec.param[0] = rsc->pru_intc_map[i].event; 149 + fwspec.param[1] = rsc->pru_intc_map[i].chnl; 150 + fwspec.param[2] = rsc->pru_intc_map[i].host; 151 + 152 + dev_dbg(dev, "mapping%d: event %d, chnl %d, host %d\n", 153 + i, fwspec.param[0], fwspec.param[1], fwspec.param[2]); 154 + 155 + pru->mapped_irq[i] = irq_create_fwspec_mapping(&fwspec); 156 + if (!pru->mapped_irq[i]) { 157 + dev_err(dev, "failed to get virq\n"); 158 + ret = pru->mapped_irq[i]; 159 + goto map_fail; 160 + } 161 + } 162 + 163 + return ret; 164 + 165 + map_fail: 166 + pru_dispose_irq_mapping(pru); 167 + 168 + return ret; 169 + } 170 + 102 171 static int pru_rproc_start(struct rproc *rproc) 103 172 { 104 173 struct device *dev = &rproc->dev; 105 174 struct pru_rproc *pru = rproc->priv; 106 175 u32 val; 176 + int ret; 107 177 108 178 dev_dbg(dev, "starting PRU%d: entry-point = 0x%llx\n", 109 179 pru->id, (rproc->bootaddr >> 2)); 180 + 181 + ret = pru_handle_intrmap(rproc); 182 + /* 183 + * reset references to pru interrupt map - they will stop being valid 184 + * after rproc_start returns 185 + */ 186 + pru->pru_interrupt_map = NULL; 187 + pru->pru_interrupt_map_sz = 0; 188 + if (ret) 189 + return ret; 110 190 111 191 val = CTRL_CTRL_EN | ((rproc->bootaddr >> 2) << 16); 112 192 pru_control_write_reg(pru, PRU_CTRL_CTRL, val); ··· 218 112 val = pru_control_read_reg(pru, PRU_CTRL_CTRL); 219 113 val &= ~CTRL_CTRL_EN; 220 114 pru_control_write_reg(pru, PRU_CTRL_CTRL, val); 115 + 116 + /* dispose irq mapping - new firmware can provide new mapping */ 117 + if (pru->mapped_irq) 118 + pru_dispose_irq_mapping(pru); 221 119 222 120 return 0; 223 121 } ··· 383 273 return ret; 384 274 } 385 275 276 + static const void * 277 + pru_rproc_find_interrupt_map(struct device *dev, const struct firmware *fw) 278 + { 279 + struct elf32_shdr *shdr, *name_table_shdr; 280 + const char *name_table; 281 + const u8 *elf_data = fw->data; 282 + struct elf32_hdr *ehdr = (struct elf32_hdr *)elf_data; 283 + u16 shnum = ehdr->e_shnum; 284 + u16 shstrndx = ehdr->e_shstrndx; 285 + int i; 286 + 287 + /* first, get the section header */ 288 + shdr = (struct elf32_shdr *)(elf_data + ehdr->e_shoff); 289 + /* compute name table section header entry in shdr array */ 290 + name_table_shdr = shdr + shstrndx; 291 + /* finally, compute the name table section address in elf */ 292 + name_table = elf_data + name_table_shdr->sh_offset; 293 + 294 + for (i = 0; i < shnum; i++, shdr++) { 295 + u32 size = shdr->sh_size; 296 + u32 offset = shdr->sh_offset; 297 + u32 name = shdr->sh_name; 298 + 299 + if (strcmp(name_table + name, ".pru_irq_map")) 300 + continue; 301 + 302 + /* make sure we have the entire irq map */ 303 + if (offset + size > fw->size || offset + size < size) { 304 + dev_err(dev, ".pru_irq_map section truncated\n"); 305 + return ERR_PTR(-EINVAL); 306 + } 307 + 308 + /* make sure irq map has at least the header */ 309 + if (sizeof(struct pru_irq_rsc) > size) { 310 + dev_err(dev, "header-less .pru_irq_map section\n"); 311 + return ERR_PTR(-EINVAL); 312 + } 313 + 314 + return shdr; 315 + } 316 + 317 + dev_dbg(dev, "no .pru_irq_map section found for this fw\n"); 318 + 319 + return NULL; 320 + } 321 + 386 322 /* 387 323 * Use a custom parse_fw callback function for dealing with PRU firmware 388 324 * specific sections. 325 + * 326 + * The firmware blob can contain optional ELF sections: .resource_table section 327 + * and .pru_irq_map one. The second one contains the PRUSS interrupt mapping 328 + * description, which needs to be setup before powering on the PRU core. To 329 + * avoid RAM wastage this ELF section is not mapped to any ELF segment (by the 330 + * firmware linker) and therefore is not loaded to PRU memory. 389 331 */ 390 332 static int pru_rproc_parse_fw(struct rproc *rproc, const struct firmware *fw) 391 333 { 334 + struct device *dev = &rproc->dev; 335 + struct pru_rproc *pru = rproc->priv; 336 + const u8 *elf_data = fw->data; 337 + const void *shdr; 338 + u8 class = fw_elf_get_class(fw); 339 + u64 sh_offset; 392 340 int ret; 393 341 394 342 /* load optional rsc table */ ··· 455 287 dev_dbg(&rproc->dev, "no resource table found for this fw\n"); 456 288 else if (ret) 457 289 return ret; 290 + 291 + /* find .pru_interrupt_map section, not having it is not an error */ 292 + shdr = pru_rproc_find_interrupt_map(dev, fw); 293 + if (IS_ERR(shdr)) 294 + return PTR_ERR(shdr); 295 + 296 + if (!shdr) 297 + return 0; 298 + 299 + /* preserve pointer to PRU interrupt map together with it size */ 300 + sh_offset = elf_shdr_get_sh_offset(class, shdr); 301 + pru->pru_interrupt_map = (struct pru_irq_rsc *)(elf_data + sh_offset); 302 + pru->pru_interrupt_map_sz = elf_shdr_get_sh_size(class, shdr); 458 303 459 304 return 0; 460 305 }
+46
drivers/remoteproc/pru_rproc.h
··· 1 + /* SPDX-License-Identifier: (GPL-2.0-only OR BSD-3-Clause) */ 2 + /* 3 + * PRUSS Remote Processor specific types 4 + * 5 + * Copyright (C) 2014-2020 Texas Instruments Incorporated - https://www.ti.com/ 6 + * Suman Anna <s-anna@ti.com> 7 + */ 8 + 9 + #ifndef _PRU_RPROC_H_ 10 + #define _PRU_RPROC_H_ 11 + 12 + /** 13 + * struct pruss_int_map - PRU system events _to_ channel and host mapping 14 + * @event: number of the system event 15 + * @chnl: channel number assigned to a given @event 16 + * @host: host number assigned to a given @chnl 17 + * 18 + * PRU system events are mapped to channels, and these channels are mapped 19 + * to host interrupts. Events can be mapped to channels in a one-to-one or 20 + * many-to-one ratio (multiple events per channel), and channels can be 21 + * mapped to host interrupts in a one-to-one or many-to-one ratio (multiple 22 + * channels per interrupt). 23 + */ 24 + struct pruss_int_map { 25 + u8 event; 26 + u8 chnl; 27 + u8 host; 28 + }; 29 + 30 + /** 31 + * struct pru_irq_rsc - PRU firmware section header for IRQ data 32 + * @type: resource type 33 + * @num_evts: number of described events 34 + * @pru_intc_map: PRU interrupt routing description 35 + * 36 + * The PRU firmware blob can contain optional .pru_irq_map ELF section, which 37 + * provides the PRUSS interrupt mapping description. The pru_irq_rsc struct 38 + * describes resource entry format. 39 + */ 40 + struct pru_irq_rsc { 41 + u8 type; 42 + u8 num_evts; 43 + struct pruss_int_map pru_intc_map[]; 44 + } __packed; 45 + 46 + #endif /* _PRU_RPROC_H_ */