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 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 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 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 */ 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 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 */ 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 */ 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 */ 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 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 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 */ 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 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 */ 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 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 */ 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 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 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 */ 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 + 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 */ 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 + len <= PRU_IRAM_DA + pru->mem_regions[PRU_IOMEM_IRAM].size) { 710 offset = da - PRU_IRAM_DA; 711 va = (__force void *)(pru->mem_regions[PRU_IOMEM_IRAM].va + 712 offset); 713 } 714 715 return va; 716 } 717 718 /* 719 * Provide address translations for only PRU Data RAMs through the remoteproc 720 * core for any PRU client drivers. The PRU Instruction RAM access is restricted 721 * only to the PRU loader code. 722 */ 723 static void *pru_rproc_da_to_va(struct rproc *rproc, u64 da, size_t len, bool *is_iomem) 724 { 725 struct pru_rproc *pru = rproc->priv; 726 727 return pru_d_da_to_va(pru, da, len); 728 } 729 730 /* PRU-specific address translator used by PRU loader. */ 731 static void *pru_da_to_va(struct rproc *rproc, u64 da, size_t len, bool is_iram) 732 { 733 struct pru_rproc *pru = rproc->priv; 734 void *va; 735 736 if (is_iram) 737 va = pru_i_da_to_va(pru, da, len); 738 else 739 va = pru_d_da_to_va(pru, da, len); 740 741 return va; 742 } 743 744 static struct rproc_ops pru_rproc_ops = { 745 .start = pru_rproc_start, 746 .stop = pru_rproc_stop, 747 .da_to_va = pru_rproc_da_to_va, 748 }; 749 750 /* 751 * Custom memory copy implementation for ICSSG PRU/RTU/Tx_PRU Cores 752 * 753 * The ICSSG PRU/RTU/Tx_PRU cores have a memory copying issue with IRAM 754 * memories, that is not seen on previous generation SoCs. The data is reflected 755 * properly in the IRAM memories only for integer (4-byte) copies. Any unaligned 756 * copies result in all the other pre-existing bytes zeroed out within that 757 * 4-byte boundary, thereby resulting in wrong text/code in the IRAMs. Also, the 758 * IRAM memory port interface does not allow any 8-byte copies (as commonly used 759 * by ARM64 memcpy implementation) and throws an exception. The DRAM memory 760 * ports do not show this behavior. 761 */ 762 static int pru_rproc_memcpy(void *dest, const void *src, size_t count) 763 { 764 const u32 *s = src; 765 u32 *d = dest; 766 size_t size = count / 4; 767 u32 *tmp_src = NULL; 768 769 /* 770 * TODO: relax limitation of 4-byte aligned dest addresses and copy 771 * sizes 772 */ 773 if ((long)dest % 4 || count % 4) 774 return -EINVAL; 775 776 /* src offsets in ELF firmware image can be non-aligned */ 777 if ((long)src % 4) { 778 tmp_src = kmemdup(src, count, GFP_KERNEL); 779 if (!tmp_src) 780 return -ENOMEM; 781 s = tmp_src; 782 } 783 784 while (size--) 785 *d++ = *s++; 786 787 kfree(tmp_src); 788 789 return 0; 790 } 791 792 static int 793 pru_rproc_load_elf_segments(struct rproc *rproc, const struct firmware *fw) 794 { 795 struct pru_rproc *pru = rproc->priv; 796 struct device *dev = &rproc->dev; 797 struct elf32_hdr *ehdr; 798 struct elf32_phdr *phdr; 799 int i, ret = 0; 800 const u8 *elf_data = fw->data; 801 802 ehdr = (struct elf32_hdr *)elf_data; 803 phdr = (struct elf32_phdr *)(elf_data + ehdr->e_phoff); 804 805 /* go through the available ELF segments */ 806 for (i = 0; i < ehdr->e_phnum; i++, phdr++) { 807 u32 da = phdr->p_paddr; 808 u32 memsz = phdr->p_memsz; 809 u32 filesz = phdr->p_filesz; 810 u32 offset = phdr->p_offset; 811 bool is_iram; 812 void *ptr; 813 814 if (phdr->p_type != PT_LOAD || !filesz) 815 continue; 816 817 dev_dbg(dev, "phdr: type %d da 0x%x memsz 0x%x filesz 0x%x\n", 818 phdr->p_type, da, memsz, filesz); 819 820 if (filesz > memsz) { 821 dev_err(dev, "bad phdr filesz 0x%x memsz 0x%x\n", 822 filesz, memsz); 823 ret = -EINVAL; 824 break; 825 } 826 827 if (offset + filesz > fw->size) { 828 dev_err(dev, "truncated fw: need 0x%x avail 0x%zx\n", 829 offset + filesz, fw->size); 830 ret = -EINVAL; 831 break; 832 } 833 834 /* grab the kernel address for this device address */ 835 is_iram = phdr->p_flags & PF_X; 836 ptr = pru_da_to_va(rproc, da, memsz, is_iram); 837 if (!ptr) { 838 dev_err(dev, "bad phdr da 0x%x mem 0x%x\n", da, memsz); 839 ret = -EINVAL; 840 break; 841 } 842 843 if (pru->data->is_k3) { 844 ret = pru_rproc_memcpy(ptr, elf_data + phdr->p_offset, 845 filesz); 846 if (ret) { 847 dev_err(dev, "PRU memory copy failed for da 0x%x memsz 0x%x\n", 848 da, memsz); 849 break; 850 } 851 } else { 852 memcpy(ptr, elf_data + phdr->p_offset, filesz); 853 } 854 855 /* skip the memzero logic performed by remoteproc ELF loader */ 856 } 857 858 return ret; 859 } 860 861 static const void * 862 pru_rproc_find_interrupt_map(struct device *dev, const struct firmware *fw) 863 { 864 struct elf32_shdr *shdr, *name_table_shdr; 865 const char *name_table; 866 const u8 *elf_data = fw->data; 867 struct elf32_hdr *ehdr = (struct elf32_hdr *)elf_data; 868 u16 shnum = ehdr->e_shnum; 869 u16 shstrndx = ehdr->e_shstrndx; 870 int i; 871 872 /* first, get the section header */ 873 shdr = (struct elf32_shdr *)(elf_data + ehdr->e_shoff); 874 /* compute name table section header entry in shdr array */ 875 name_table_shdr = shdr + shstrndx; 876 /* finally, compute the name table section address in elf */ 877 name_table = elf_data + name_table_shdr->sh_offset; 878 879 for (i = 0; i < shnum; i++, shdr++) { 880 u32 size = shdr->sh_size; 881 u32 offset = shdr->sh_offset; 882 u32 name = shdr->sh_name; 883 884 if (strcmp(name_table + name, ".pru_irq_map")) 885 continue; 886 887 /* make sure we have the entire irq map */ 888 if (offset + size > fw->size || offset + size < size) { 889 dev_err(dev, ".pru_irq_map section truncated\n"); 890 return ERR_PTR(-EINVAL); 891 } 892 893 /* make sure irq map has at least the header */ 894 if (sizeof(struct pru_irq_rsc) > size) { 895 dev_err(dev, "header-less .pru_irq_map section\n"); 896 return ERR_PTR(-EINVAL); 897 } 898 899 return shdr; 900 } 901 902 dev_dbg(dev, "no .pru_irq_map section found for this fw\n"); 903 904 return NULL; 905 } 906 907 /* 908 * Use a custom parse_fw callback function for dealing with PRU firmware 909 * specific sections. 910 * 911 * The firmware blob can contain optional ELF sections: .resource_table section 912 * and .pru_irq_map one. The second one contains the PRUSS interrupt mapping 913 * description, which needs to be setup before powering on the PRU core. To 914 * avoid RAM wastage this ELF section is not mapped to any ELF segment (by the 915 * firmware linker) and therefore is not loaded to PRU memory. 916 */ 917 static int pru_rproc_parse_fw(struct rproc *rproc, const struct firmware *fw) 918 { 919 struct device *dev = &rproc->dev; 920 struct pru_rproc *pru = rproc->priv; 921 const u8 *elf_data = fw->data; 922 const void *shdr; 923 u8 class = fw_elf_get_class(fw); 924 u64 sh_offset; 925 int ret; 926 927 /* load optional rsc table */ 928 ret = rproc_elf_load_rsc_table(rproc, fw); 929 if (ret == -EINVAL) 930 dev_dbg(&rproc->dev, "no resource table found for this fw\n"); 931 else if (ret) 932 return ret; 933 934 /* find .pru_interrupt_map section, not having it is not an error */ 935 shdr = pru_rproc_find_interrupt_map(dev, fw); 936 if (IS_ERR(shdr)) 937 return PTR_ERR(shdr); 938 939 if (!shdr) 940 return 0; 941 942 /* preserve pointer to PRU interrupt map together with it size */ 943 sh_offset = elf_shdr_get_sh_offset(class, shdr); 944 pru->pru_interrupt_map = (struct pru_irq_rsc *)(elf_data + sh_offset); 945 pru->pru_interrupt_map_sz = elf_shdr_get_sh_size(class, shdr); 946 947 return 0; 948 } 949 950 /* 951 * Compute PRU id based on the IRAM addresses. The PRU IRAMs are 952 * always at a particular offset within the PRUSS address space. 953 */ 954 static int pru_rproc_set_id(struct pru_rproc *pru) 955 { 956 int ret = 0; 957 958 switch (pru->mem_regions[PRU_IOMEM_IRAM].pa & PRU_IRAM_ADDR_MASK) { 959 case TX_PRU0_IRAM_ADDR_MASK: 960 fallthrough; 961 case RTU0_IRAM_ADDR_MASK: 962 fallthrough; 963 case PRU0_IRAM_ADDR_MASK: 964 pru->id = PRUSS_PRU0; 965 break; 966 case TX_PRU1_IRAM_ADDR_MASK: 967 fallthrough; 968 case RTU1_IRAM_ADDR_MASK: 969 fallthrough; 970 case PRU1_IRAM_ADDR_MASK: 971 pru->id = PRUSS_PRU1; 972 break; 973 default: 974 ret = -EINVAL; 975 } 976 977 return ret; 978 } 979 980 static int pru_rproc_probe(struct platform_device *pdev) 981 { 982 struct device *dev = &pdev->dev; 983 struct device_node *np = dev->of_node; 984 struct platform_device *ppdev = to_platform_device(dev->parent); 985 struct pru_rproc *pru; 986 const char *fw_name; 987 struct rproc *rproc = NULL; 988 struct resource *res; 989 int i, ret; 990 const struct pru_private_data *data; 991 const char *mem_names[PRU_IOMEM_MAX] = { "iram", "control", "debug" }; 992 993 data = of_device_get_match_data(&pdev->dev); 994 if (!data) 995 return -ENODEV; 996 997 ret = of_property_read_string(np, "firmware-name", &fw_name); 998 if (ret) { 999 dev_err(dev, "unable to retrieve firmware-name %d\n", ret); 1000 return ret; 1001 } 1002 1003 rproc = devm_rproc_alloc(dev, pdev->name, &pru_rproc_ops, fw_name, 1004 sizeof(*pru)); 1005 if (!rproc) { 1006 dev_err(dev, "rproc_alloc failed\n"); 1007 return -ENOMEM; 1008 } 1009 /* use a custom load function to deal with PRU-specific quirks */ 1010 rproc->ops->load = pru_rproc_load_elf_segments; 1011 1012 /* use a custom parse function to deal with PRU-specific resources */ 1013 rproc->ops->parse_fw = pru_rproc_parse_fw; 1014 1015 /* error recovery is not supported for PRUs */ 1016 rproc->recovery_disabled = true; 1017 1018 /* 1019 * rproc_add will auto-boot the processor normally, but this is not 1020 * desired with PRU client driven boot-flow methodology. A PRU 1021 * application/client driver will boot the corresponding PRU 1022 * remote-processor as part of its state machine either through the 1023 * remoteproc sysfs interface or through the equivalent kernel API. 1024 */ 1025 rproc->auto_boot = false; 1026 1027 pru = rproc->priv; 1028 pru->dev = dev; 1029 pru->data = data; 1030 pru->pruss = platform_get_drvdata(ppdev); 1031 pru->rproc = rproc; 1032 pru->fw_name = fw_name; 1033 pru->client_np = NULL; 1034 spin_lock_init(&pru->rmw_lock); 1035 mutex_init(&pru->lock); 1036 1037 for (i = 0; i < ARRAY_SIZE(mem_names); i++) { 1038 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, 1039 mem_names[i]); 1040 pru->mem_regions[i].va = devm_ioremap_resource(dev, res); 1041 if (IS_ERR(pru->mem_regions[i].va)) { 1042 dev_err(dev, "failed to parse and map memory resource %d %s\n", 1043 i, mem_names[i]); 1044 ret = PTR_ERR(pru->mem_regions[i].va); 1045 return ret; 1046 } 1047 pru->mem_regions[i].pa = res->start; 1048 pru->mem_regions[i].size = resource_size(res); 1049 1050 dev_dbg(dev, "memory %8s: pa %pa size 0x%zx va %pK\n", 1051 mem_names[i], &pru->mem_regions[i].pa, 1052 pru->mem_regions[i].size, pru->mem_regions[i].va); 1053 } 1054 1055 ret = pru_rproc_set_id(pru); 1056 if (ret < 0) 1057 return ret; 1058 1059 platform_set_drvdata(pdev, rproc); 1060 1061 ret = devm_rproc_add(dev, pru->rproc); 1062 if (ret) { 1063 dev_err(dev, "rproc_add failed: %d\n", ret); 1064 return ret; 1065 } 1066 1067 pru_rproc_create_debug_entries(rproc); 1068 1069 dev_dbg(dev, "PRU rproc node %pOF probed successfully\n", np); 1070 1071 return 0; 1072 } 1073 1074 static int pru_rproc_remove(struct platform_device *pdev) 1075 { 1076 struct device *dev = &pdev->dev; 1077 struct rproc *rproc = platform_get_drvdata(pdev); 1078 1079 dev_dbg(dev, "%s: removing rproc %s\n", __func__, rproc->name); 1080 1081 return 0; 1082 } 1083 1084 static const struct pru_private_data pru_data = { 1085 .type = PRU_TYPE_PRU, 1086 }; 1087 1088 static const struct pru_private_data k3_pru_data = { 1089 .type = PRU_TYPE_PRU, 1090 .is_k3 = 1, 1091 }; 1092 1093 static const struct pru_private_data k3_rtu_data = { 1094 .type = PRU_TYPE_RTU, 1095 .is_k3 = 1, 1096 }; 1097 1098 static const struct pru_private_data k3_tx_pru_data = { 1099 .type = PRU_TYPE_TX_PRU, 1100 .is_k3 = 1, 1101 }; 1102 1103 static const struct of_device_id pru_rproc_match[] = { 1104 { .compatible = "ti,am3356-pru", .data = &pru_data }, 1105 { .compatible = "ti,am4376-pru", .data = &pru_data }, 1106 { .compatible = "ti,am5728-pru", .data = &pru_data }, 1107 { .compatible = "ti,am642-pru", .data = &k3_pru_data }, 1108 { .compatible = "ti,am642-rtu", .data = &k3_rtu_data }, 1109 { .compatible = "ti,am642-tx-pru", .data = &k3_tx_pru_data }, 1110 { .compatible = "ti,k2g-pru", .data = &pru_data }, 1111 { .compatible = "ti,am654-pru", .data = &k3_pru_data }, 1112 { .compatible = "ti,am654-rtu", .data = &k3_rtu_data }, 1113 { .compatible = "ti,am654-tx-pru", .data = &k3_tx_pru_data }, 1114 { .compatible = "ti,j721e-pru", .data = &k3_pru_data }, 1115 { .compatible = "ti,j721e-rtu", .data = &k3_rtu_data }, 1116 { .compatible = "ti,j721e-tx-pru", .data = &k3_tx_pru_data }, 1117 { .compatible = "ti,am625-pru", .data = &k3_pru_data }, 1118 {}, 1119 }; 1120 MODULE_DEVICE_TABLE(of, pru_rproc_match); 1121 1122 static struct platform_driver pru_rproc_driver = { 1123 .driver = { 1124 .name = PRU_RPROC_DRVNAME, 1125 .of_match_table = pru_rproc_match, 1126 .suppress_bind_attrs = true, 1127 }, 1128 .probe = pru_rproc_probe, 1129 .remove = pru_rproc_remove, 1130 }; 1131 module_platform_driver(pru_rproc_driver); 1132 1133 MODULE_AUTHOR("Suman Anna <s-anna@ti.com>"); 1134 MODULE_AUTHOR("Andrew F. Davis <afd@ti.com>"); 1135 MODULE_AUTHOR("Grzegorz Jaszczyk <grzegorz.jaszczyk@linaro.org>"); 1136 MODULE_AUTHOR("Puranjay Mohan <p-mohan@ti.com>"); 1137 MODULE_AUTHOR("Md Danish Anwar <danishanwar@ti.com>"); 1138 MODULE_DESCRIPTION("PRU-ICSS Remote Processor Driver"); 1139 MODULE_LICENSE("GPL v2"); 1140