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 * struct pru_private_data - device data for a PRU core 86 * @type: type of the PRU core (PRU, RTU, Tx_PRU) 87 * @is_k3: flag used to identify the need for special load handling 88 */ 89 struct pru_private_data { 90 enum pru_type type; 91 unsigned int is_k3 : 1; 92 }; 93 94 /** 95 * struct pru_rproc - PRU remoteproc structure 96 * @id: id of the PRU core within the PRUSS 97 * @dev: PRU core device pointer 98 * @pruss: back-reference to parent PRUSS structure 99 * @rproc: remoteproc pointer for this PRU core 100 * @data: PRU core specific data 101 * @mem_regions: data for each of the PRU memory regions 102 * @client_np: client device node 103 * @lock: mutex to protect client usage 104 * @fw_name: name of firmware image used during loading 105 * @mapped_irq: virtual interrupt numbers of created fw specific mapping 106 * @pru_interrupt_map: pointer to interrupt mapping description (firmware) 107 * @pru_interrupt_map_sz: pru_interrupt_map size 108 * @rmw_lock: lock for read, modify, write operations on registers 109 * @dbg_single_step: debug state variable to set PRU into single step mode 110 * @dbg_continuous: debug state variable to restore PRU execution mode 111 * @evt_count: number of mapped events 112 */ 113 struct pru_rproc { 114 int id; 115 struct device *dev; 116 struct pruss *pruss; 117 struct rproc *rproc; 118 const struct pru_private_data *data; 119 struct pruss_mem_region mem_regions[PRU_IOMEM_MAX]; 120 struct device_node *client_np; 121 struct mutex lock; 122 const char *fw_name; 123 unsigned int *mapped_irq; 124 struct pru_irq_rsc *pru_interrupt_map; 125 size_t pru_interrupt_map_sz; 126 spinlock_t rmw_lock; 127 u32 dbg_single_step; 128 u32 dbg_continuous; 129 u8 evt_count; 130 }; 131 132 static inline u32 pru_control_read_reg(struct pru_rproc *pru, unsigned int reg) 133 { 134 return readl_relaxed(pru->mem_regions[PRU_IOMEM_CTRL].va + reg); 135 } 136 137 static inline 138 void pru_control_write_reg(struct pru_rproc *pru, unsigned int reg, u32 val) 139 { 140 writel_relaxed(val, pru->mem_regions[PRU_IOMEM_CTRL].va + reg); 141 } 142 143 static inline 144 void pru_control_set_reg(struct pru_rproc *pru, unsigned int reg, 145 u32 mask, u32 set) 146 { 147 u32 val; 148 unsigned long flags; 149 150 spin_lock_irqsave(&pru->rmw_lock, flags); 151 152 val = pru_control_read_reg(pru, reg); 153 val &= ~mask; 154 val |= (set & mask); 155 pru_control_write_reg(pru, reg, val); 156 157 spin_unlock_irqrestore(&pru->rmw_lock, flags); 158 } 159 160 /** 161 * pru_rproc_set_firmware() - set firmware for a PRU core 162 * @rproc: the rproc instance of the PRU 163 * @fw_name: the new firmware name, or NULL if default is desired 164 * 165 * Return: 0 on success, or errno in error case. 166 */ 167 static int pru_rproc_set_firmware(struct rproc *rproc, const char *fw_name) 168 { 169 struct pru_rproc *pru = rproc->priv; 170 171 if (!fw_name) 172 fw_name = pru->fw_name; 173 174 return rproc_set_firmware(rproc, fw_name); 175 } 176 177 static struct rproc *__pru_rproc_get(struct device_node *np, int index) 178 { 179 struct rproc *rproc; 180 phandle rproc_phandle; 181 int ret; 182 183 ret = of_property_read_u32_index(np, "ti,prus", index, &rproc_phandle); 184 if (ret) 185 return ERR_PTR(ret); 186 187 rproc = rproc_get_by_phandle(rproc_phandle); 188 if (!rproc) { 189 ret = -EPROBE_DEFER; 190 return ERR_PTR(ret); 191 } 192 193 /* make sure it is PRU rproc */ 194 if (!is_pru_rproc(rproc->dev.parent)) { 195 rproc_put(rproc); 196 return ERR_PTR(-ENODEV); 197 } 198 199 return rproc; 200 } 201 202 /** 203 * pru_rproc_get() - get the PRU rproc instance from a device node 204 * @np: the user/client device node 205 * @index: index to use for the ti,prus property 206 * @pru_id: optional pointer to return the PRU remoteproc processor id 207 * 208 * This function looks through a client device node's "ti,prus" property at 209 * index @index and returns the rproc handle for a valid PRU remote processor if 210 * found. The function allows only one user to own the PRU rproc resource at a 211 * time. Caller must call pru_rproc_put() when done with using the rproc, not 212 * required if the function returns a failure. 213 * 214 * When optional @pru_id pointer is passed the PRU remoteproc processor id is 215 * returned. 216 * 217 * Return: rproc handle on success, and an ERR_PTR on failure using one 218 * of the following error values 219 * -ENODEV if device is not found 220 * -EBUSY if PRU is already acquired by anyone 221 * -EPROBE_DEFER is PRU device is not probed yet 222 */ 223 struct rproc *pru_rproc_get(struct device_node *np, int index, 224 enum pruss_pru_id *pru_id) 225 { 226 struct rproc *rproc; 227 struct pru_rproc *pru; 228 struct device *dev; 229 const char *fw_name; 230 int ret; 231 232 rproc = __pru_rproc_get(np, index); 233 if (IS_ERR(rproc)) 234 return rproc; 235 236 pru = rproc->priv; 237 dev = &rproc->dev; 238 239 mutex_lock(&pru->lock); 240 241 if (pru->client_np) { 242 mutex_unlock(&pru->lock); 243 ret = -EBUSY; 244 goto err_no_rproc_handle; 245 } 246 247 pru->client_np = np; 248 rproc->sysfs_read_only = true; 249 250 mutex_unlock(&pru->lock); 251 252 if (pru_id) 253 *pru_id = pru->id; 254 255 ret = of_property_read_string_index(np, "firmware-name", index, 256 &fw_name); 257 if (!ret) { 258 ret = pru_rproc_set_firmware(rproc, fw_name); 259 if (ret) { 260 dev_err(dev, "failed to set firmware: %d\n", ret); 261 goto err; 262 } 263 } 264 265 return rproc; 266 267 err_no_rproc_handle: 268 rproc_put(rproc); 269 return ERR_PTR(ret); 270 271 err: 272 pru_rproc_put(rproc); 273 return ERR_PTR(ret); 274 } 275 EXPORT_SYMBOL_GPL(pru_rproc_get); 276 277 /** 278 * pru_rproc_put() - release the PRU rproc resource 279 * @rproc: the rproc resource to release 280 * 281 * Releases the PRU rproc resource and makes it available to other 282 * users. 283 */ 284 void pru_rproc_put(struct rproc *rproc) 285 { 286 struct pru_rproc *pru; 287 288 if (IS_ERR_OR_NULL(rproc) || !is_pru_rproc(rproc->dev.parent)) 289 return; 290 291 pru = rproc->priv; 292 293 pru_rproc_set_firmware(rproc, NULL); 294 295 mutex_lock(&pru->lock); 296 297 if (!pru->client_np) { 298 mutex_unlock(&pru->lock); 299 return; 300 } 301 302 pru->client_np = NULL; 303 rproc->sysfs_read_only = false; 304 mutex_unlock(&pru->lock); 305 306 rproc_put(rproc); 307 } 308 EXPORT_SYMBOL_GPL(pru_rproc_put); 309 310 /** 311 * pru_rproc_set_ctable() - set the constant table index for the PRU 312 * @rproc: the rproc instance of the PRU 313 * @c: constant table index to set 314 * @addr: physical address to set it to 315 * 316 * Return: 0 on success, or errno in error case. 317 */ 318 int pru_rproc_set_ctable(struct rproc *rproc, enum pru_ctable_idx c, u32 addr) 319 { 320 struct pru_rproc *pru = rproc->priv; 321 unsigned int reg; 322 u32 mask, set; 323 u16 idx; 324 u16 idx_mask; 325 326 if (IS_ERR_OR_NULL(rproc)) 327 return -EINVAL; 328 329 if (!rproc->dev.parent || !is_pru_rproc(rproc->dev.parent)) 330 return -ENODEV; 331 332 /* pointer is 16 bit and index is 8-bit so mask out the rest */ 333 idx_mask = (c >= PRU_C28) ? 0xFFFF : 0xFF; 334 335 /* ctable uses bit 8 and upwards only */ 336 idx = (addr >> 8) & idx_mask; 337 338 /* configurable ctable (i.e. C24) starts at PRU_CTRL_CTBIR0 */ 339 reg = PRU_CTRL_CTBIR0 + 4 * (c >> 1); 340 mask = idx_mask << (16 * (c & 1)); 341 set = idx << (16 * (c & 1)); 342 343 pru_control_set_reg(pru, reg, mask, set); 344 345 return 0; 346 } 347 EXPORT_SYMBOL_GPL(pru_rproc_set_ctable); 348 349 static inline u32 pru_debug_read_reg(struct pru_rproc *pru, unsigned int reg) 350 { 351 return readl_relaxed(pru->mem_regions[PRU_IOMEM_DEBUG].va + reg); 352 } 353 354 static int regs_show(struct seq_file *s, void *data) 355 { 356 struct rproc *rproc = s->private; 357 struct pru_rproc *pru = rproc->priv; 358 int i, nregs = 32; 359 u32 pru_sts; 360 int pru_is_running; 361 362 seq_puts(s, "============== Control Registers ==============\n"); 363 seq_printf(s, "CTRL := 0x%08x\n", 364 pru_control_read_reg(pru, PRU_CTRL_CTRL)); 365 pru_sts = pru_control_read_reg(pru, PRU_CTRL_STS); 366 seq_printf(s, "STS (PC) := 0x%08x (0x%08x)\n", pru_sts, pru_sts << 2); 367 seq_printf(s, "WAKEUP_EN := 0x%08x\n", 368 pru_control_read_reg(pru, PRU_CTRL_WAKEUP_EN)); 369 seq_printf(s, "CYCLE := 0x%08x\n", 370 pru_control_read_reg(pru, PRU_CTRL_CYCLE)); 371 seq_printf(s, "STALL := 0x%08x\n", 372 pru_control_read_reg(pru, PRU_CTRL_STALL)); 373 seq_printf(s, "CTBIR0 := 0x%08x\n", 374 pru_control_read_reg(pru, PRU_CTRL_CTBIR0)); 375 seq_printf(s, "CTBIR1 := 0x%08x\n", 376 pru_control_read_reg(pru, PRU_CTRL_CTBIR1)); 377 seq_printf(s, "CTPPR0 := 0x%08x\n", 378 pru_control_read_reg(pru, PRU_CTRL_CTPPR0)); 379 seq_printf(s, "CTPPR1 := 0x%08x\n", 380 pru_control_read_reg(pru, PRU_CTRL_CTPPR1)); 381 382 seq_puts(s, "=============== Debug Registers ===============\n"); 383 pru_is_running = pru_control_read_reg(pru, PRU_CTRL_CTRL) & 384 CTRL_CTRL_RUNSTATE; 385 if (pru_is_running) { 386 seq_puts(s, "PRU is executing, cannot print/access debug registers.\n"); 387 return 0; 388 } 389 390 for (i = 0; i < nregs; i++) { 391 seq_printf(s, "GPREG%-2d := 0x%08x\tCT_REG%-2d := 0x%08x\n", 392 i, pru_debug_read_reg(pru, PRU_DEBUG_GPREG(i)), 393 i, pru_debug_read_reg(pru, PRU_DEBUG_CT_REG(i))); 394 } 395 396 return 0; 397 } 398 DEFINE_SHOW_ATTRIBUTE(regs); 399 400 /* 401 * Control PRU single-step mode 402 * 403 * This is a debug helper function used for controlling the single-step 404 * mode of the PRU. The PRU Debug registers are not accessible when the 405 * PRU is in RUNNING state. 406 * 407 * Writing a non-zero value sets the PRU into single-step mode irrespective 408 * of its previous state. The PRU mode is saved only on the first set into 409 * a single-step mode. Writing a zero value will restore the PRU into its 410 * original mode. 411 */ 412 static int pru_rproc_debug_ss_set(void *data, u64 val) 413 { 414 struct rproc *rproc = data; 415 struct pru_rproc *pru = rproc->priv; 416 u32 reg_val; 417 418 val = val ? 1 : 0; 419 if (!val && !pru->dbg_single_step) 420 return 0; 421 422 reg_val = pru_control_read_reg(pru, PRU_CTRL_CTRL); 423 424 if (val && !pru->dbg_single_step) 425 pru->dbg_continuous = reg_val; 426 427 if (val) 428 reg_val |= CTRL_CTRL_SINGLE_STEP | CTRL_CTRL_EN; 429 else 430 reg_val = pru->dbg_continuous; 431 432 pru->dbg_single_step = val; 433 pru_control_write_reg(pru, PRU_CTRL_CTRL, reg_val); 434 435 return 0; 436 } 437 438 static int pru_rproc_debug_ss_get(void *data, u64 *val) 439 { 440 struct rproc *rproc = data; 441 struct pru_rproc *pru = rproc->priv; 442 443 *val = pru->dbg_single_step; 444 445 return 0; 446 } 447 DEFINE_DEBUGFS_ATTRIBUTE(pru_rproc_debug_ss_fops, pru_rproc_debug_ss_get, 448 pru_rproc_debug_ss_set, "%llu\n"); 449 450 /* 451 * Create PRU-specific debugfs entries 452 * 453 * The entries are created only if the parent remoteproc debugfs directory 454 * exists, and will be cleaned up by the remoteproc core. 455 */ 456 static void pru_rproc_create_debug_entries(struct rproc *rproc) 457 { 458 if (!rproc->dbg_dir) 459 return; 460 461 debugfs_create_file("regs", 0400, rproc->dbg_dir, 462 rproc, ®s_fops); 463 debugfs_create_file("single_step", 0600, rproc->dbg_dir, 464 rproc, &pru_rproc_debug_ss_fops); 465 } 466 467 static void pru_dispose_irq_mapping(struct pru_rproc *pru) 468 { 469 if (!pru->mapped_irq) 470 return; 471 472 while (pru->evt_count) { 473 pru->evt_count--; 474 if (pru->mapped_irq[pru->evt_count] > 0) 475 irq_dispose_mapping(pru->mapped_irq[pru->evt_count]); 476 } 477 478 kfree(pru->mapped_irq); 479 pru->mapped_irq = NULL; 480 } 481 482 /* 483 * Parse the custom PRU interrupt map resource and configure the INTC 484 * appropriately. 485 */ 486 static int pru_handle_intrmap(struct rproc *rproc) 487 { 488 struct device *dev = rproc->dev.parent; 489 struct pru_rproc *pru = rproc->priv; 490 struct pru_irq_rsc *rsc = pru->pru_interrupt_map; 491 struct irq_fwspec fwspec; 492 struct device_node *parent, *irq_parent; 493 int i, ret = 0; 494 495 /* not having pru_interrupt_map is not an error */ 496 if (!rsc) 497 return 0; 498 499 /* currently supporting only type 0 */ 500 if (rsc->type != 0) { 501 dev_err(dev, "unsupported rsc type: %d\n", rsc->type); 502 return -EINVAL; 503 } 504 505 if (rsc->num_evts > MAX_PRU_SYS_EVENTS) 506 return -EINVAL; 507 508 if (sizeof(*rsc) + rsc->num_evts * sizeof(struct pruss_int_map) != 509 pru->pru_interrupt_map_sz) 510 return -EINVAL; 511 512 pru->evt_count = rsc->num_evts; 513 pru->mapped_irq = kcalloc(pru->evt_count, sizeof(unsigned int), 514 GFP_KERNEL); 515 if (!pru->mapped_irq) { 516 pru->evt_count = 0; 517 return -ENOMEM; 518 } 519 520 /* 521 * parse and fill in system event to interrupt channel and 522 * channel-to-host mapping. The interrupt controller to be used 523 * for these mappings for a given PRU remoteproc is always its 524 * corresponding sibling PRUSS INTC node. 525 */ 526 parent = of_get_parent(dev_of_node(pru->dev)); 527 if (!parent) { 528 kfree(pru->mapped_irq); 529 pru->mapped_irq = NULL; 530 pru->evt_count = 0; 531 return -ENODEV; 532 } 533 534 irq_parent = of_get_child_by_name(parent, "interrupt-controller"); 535 of_node_put(parent); 536 if (!irq_parent) { 537 kfree(pru->mapped_irq); 538 pru->mapped_irq = NULL; 539 pru->evt_count = 0; 540 return -ENODEV; 541 } 542 543 fwspec.fwnode = of_node_to_fwnode(irq_parent); 544 fwspec.param_count = 3; 545 for (i = 0; i < pru->evt_count; i++) { 546 fwspec.param[0] = rsc->pru_intc_map[i].event; 547 fwspec.param[1] = rsc->pru_intc_map[i].chnl; 548 fwspec.param[2] = rsc->pru_intc_map[i].host; 549 550 dev_dbg(dev, "mapping%d: event %d, chnl %d, host %d\n", 551 i, fwspec.param[0], fwspec.param[1], fwspec.param[2]); 552 553 pru->mapped_irq[i] = irq_create_fwspec_mapping(&fwspec); 554 if (!pru->mapped_irq[i]) { 555 dev_err(dev, "failed to get virq for fw mapping %d: event %d chnl %d host %d\n", 556 i, fwspec.param[0], fwspec.param[1], 557 fwspec.param[2]); 558 ret = -EINVAL; 559 goto map_fail; 560 } 561 } 562 of_node_put(irq_parent); 563 564 return ret; 565 566 map_fail: 567 pru_dispose_irq_mapping(pru); 568 of_node_put(irq_parent); 569 570 return ret; 571 } 572 573 static int pru_rproc_start(struct rproc *rproc) 574 { 575 struct device *dev = &rproc->dev; 576 struct pru_rproc *pru = rproc->priv; 577 const char *names[PRU_TYPE_MAX] = { "PRU", "RTU", "Tx_PRU" }; 578 u32 val; 579 int ret; 580 581 dev_dbg(dev, "starting %s%d: entry-point = 0x%llx\n", 582 names[pru->data->type], pru->id, (rproc->bootaddr >> 2)); 583 584 ret = pru_handle_intrmap(rproc); 585 /* 586 * reset references to pru interrupt map - they will stop being valid 587 * after rproc_start returns 588 */ 589 pru->pru_interrupt_map = NULL; 590 pru->pru_interrupt_map_sz = 0; 591 if (ret) 592 return ret; 593 594 val = CTRL_CTRL_EN | ((rproc->bootaddr >> 2) << 16); 595 pru_control_write_reg(pru, PRU_CTRL_CTRL, val); 596 597 return 0; 598 } 599 600 static int pru_rproc_stop(struct rproc *rproc) 601 { 602 struct device *dev = &rproc->dev; 603 struct pru_rproc *pru = rproc->priv; 604 const char *names[PRU_TYPE_MAX] = { "PRU", "RTU", "Tx_PRU" }; 605 u32 val; 606 607 dev_dbg(dev, "stopping %s%d\n", names[pru->data->type], pru->id); 608 609 val = pru_control_read_reg(pru, PRU_CTRL_CTRL); 610 val &= ~CTRL_CTRL_EN; 611 pru_control_write_reg(pru, PRU_CTRL_CTRL, val); 612 613 /* dispose irq mapping - new firmware can provide new mapping */ 614 pru_dispose_irq_mapping(pru); 615 616 return 0; 617 } 618 619 /* 620 * Convert PRU device address (data spaces only) to kernel virtual address. 621 * 622 * Each PRU has access to all data memories within the PRUSS, accessible at 623 * different ranges. So, look through both its primary and secondary Data 624 * RAMs as well as any shared Data RAM to convert a PRU device address to 625 * kernel virtual address. Data RAM0 is primary Data RAM for PRU0 and Data 626 * RAM1 is primary Data RAM for PRU1. 627 */ 628 static void *pru_d_da_to_va(struct pru_rproc *pru, u32 da, size_t len) 629 { 630 struct pruss_mem_region dram0, dram1, shrd_ram; 631 struct pruss *pruss = pru->pruss; 632 u32 offset; 633 void *va = NULL; 634 635 if (len == 0) 636 return NULL; 637 638 dram0 = pruss->mem_regions[PRUSS_MEM_DRAM0]; 639 dram1 = pruss->mem_regions[PRUSS_MEM_DRAM1]; 640 /* PRU1 has its local RAM addresses reversed */ 641 if (pru->id == PRUSS_PRU1) 642 swap(dram0, dram1); 643 shrd_ram = pruss->mem_regions[PRUSS_MEM_SHRD_RAM2]; 644 645 if (da + len <= PRU_PDRAM_DA + dram0.size) { 646 offset = da - PRU_PDRAM_DA; 647 va = (__force void *)(dram0.va + offset); 648 } else if (da >= PRU_SDRAM_DA && 649 da + len <= PRU_SDRAM_DA + dram1.size) { 650 offset = da - PRU_SDRAM_DA; 651 va = (__force void *)(dram1.va + offset); 652 } else if (da >= PRU_SHRDRAM_DA && 653 da + len <= PRU_SHRDRAM_DA + shrd_ram.size) { 654 offset = da - PRU_SHRDRAM_DA; 655 va = (__force void *)(shrd_ram.va + offset); 656 } 657 658 return va; 659 } 660 661 /* 662 * Convert PRU device address (instruction space) to kernel virtual address. 663 * 664 * A PRU does not have an unified address space. Each PRU has its very own 665 * private Instruction RAM, and its device address is identical to that of 666 * its primary Data RAM device address. 667 */ 668 static void *pru_i_da_to_va(struct pru_rproc *pru, u32 da, size_t len) 669 { 670 u32 offset; 671 void *va = NULL; 672 673 if (len == 0) 674 return NULL; 675 676 /* 677 * GNU binutils do not support multiple address spaces. The GNU 678 * linker's default linker script places IRAM at an arbitrary high 679 * offset, in order to differentiate it from DRAM. Hence we need to 680 * strip the artificial offset in the IRAM addresses coming from the 681 * ELF file. 682 * 683 * The TI proprietary linker would never set those higher IRAM address 684 * bits anyway. PRU architecture limits the program counter to 16-bit 685 * word-address range. This in turn corresponds to 18-bit IRAM 686 * byte-address range for ELF. 687 * 688 * Two more bits are added just in case to make the final 20-bit mask. 689 * Idea is to have a safeguard in case TI decides to add banking 690 * in future SoCs. 691 */ 692 da &= 0xfffff; 693 694 if (da + len <= PRU_IRAM_DA + pru->mem_regions[PRU_IOMEM_IRAM].size) { 695 offset = da - PRU_IRAM_DA; 696 va = (__force void *)(pru->mem_regions[PRU_IOMEM_IRAM].va + 697 offset); 698 } 699 700 return va; 701 } 702 703 /* 704 * Provide address translations for only PRU Data RAMs through the remoteproc 705 * core for any PRU client drivers. The PRU Instruction RAM access is restricted 706 * only to the PRU loader code. 707 */ 708 static void *pru_rproc_da_to_va(struct rproc *rproc, u64 da, size_t len, bool *is_iomem) 709 { 710 struct pru_rproc *pru = rproc->priv; 711 712 return pru_d_da_to_va(pru, da, len); 713 } 714 715 /* PRU-specific address translator used by PRU loader. */ 716 static void *pru_da_to_va(struct rproc *rproc, u64 da, size_t len, bool is_iram) 717 { 718 struct pru_rproc *pru = rproc->priv; 719 void *va; 720 721 if (is_iram) 722 va = pru_i_da_to_va(pru, da, len); 723 else 724 va = pru_d_da_to_va(pru, da, len); 725 726 return va; 727 } 728 729 static struct rproc_ops pru_rproc_ops = { 730 .start = pru_rproc_start, 731 .stop = pru_rproc_stop, 732 .da_to_va = pru_rproc_da_to_va, 733 }; 734 735 /* 736 * Custom memory copy implementation for ICSSG PRU/RTU/Tx_PRU Cores 737 * 738 * The ICSSG PRU/RTU/Tx_PRU cores have a memory copying issue with IRAM 739 * memories, that is not seen on previous generation SoCs. The data is reflected 740 * properly in the IRAM memories only for integer (4-byte) copies. Any unaligned 741 * copies result in all the other pre-existing bytes zeroed out within that 742 * 4-byte boundary, thereby resulting in wrong text/code in the IRAMs. Also, the 743 * IRAM memory port interface does not allow any 8-byte copies (as commonly used 744 * by ARM64 memcpy implementation) and throws an exception. The DRAM memory 745 * ports do not show this behavior. 746 */ 747 static int pru_rproc_memcpy(void *dest, const void *src, size_t count) 748 { 749 const u32 *s = src; 750 u32 *d = dest; 751 size_t size = count / 4; 752 u32 *tmp_src = NULL; 753 754 /* 755 * TODO: relax limitation of 4-byte aligned dest addresses and copy 756 * sizes 757 */ 758 if ((long)dest % 4 || count % 4) 759 return -EINVAL; 760 761 /* src offsets in ELF firmware image can be non-aligned */ 762 if ((long)src % 4) { 763 tmp_src = kmemdup(src, count, GFP_KERNEL); 764 if (!tmp_src) 765 return -ENOMEM; 766 s = tmp_src; 767 } 768 769 while (size--) 770 *d++ = *s++; 771 772 kfree(tmp_src); 773 774 return 0; 775 } 776 777 static int 778 pru_rproc_load_elf_segments(struct rproc *rproc, const struct firmware *fw) 779 { 780 struct pru_rproc *pru = rproc->priv; 781 struct device *dev = &rproc->dev; 782 struct elf32_hdr *ehdr; 783 struct elf32_phdr *phdr; 784 int i, ret = 0; 785 const u8 *elf_data = fw->data; 786 787 ehdr = (struct elf32_hdr *)elf_data; 788 phdr = (struct elf32_phdr *)(elf_data + ehdr->e_phoff); 789 790 /* go through the available ELF segments */ 791 for (i = 0; i < ehdr->e_phnum; i++, phdr++) { 792 u32 da = phdr->p_paddr; 793 u32 memsz = phdr->p_memsz; 794 u32 filesz = phdr->p_filesz; 795 u32 offset = phdr->p_offset; 796 bool is_iram; 797 void *ptr; 798 799 if (phdr->p_type != PT_LOAD || !filesz) 800 continue; 801 802 dev_dbg(dev, "phdr: type %d da 0x%x memsz 0x%x filesz 0x%x\n", 803 phdr->p_type, da, memsz, filesz); 804 805 if (filesz > memsz) { 806 dev_err(dev, "bad phdr filesz 0x%x memsz 0x%x\n", 807 filesz, memsz); 808 ret = -EINVAL; 809 break; 810 } 811 812 if (offset + filesz > fw->size) { 813 dev_err(dev, "truncated fw: need 0x%x avail 0x%zx\n", 814 offset + filesz, fw->size); 815 ret = -EINVAL; 816 break; 817 } 818 819 /* grab the kernel address for this device address */ 820 is_iram = phdr->p_flags & PF_X; 821 ptr = pru_da_to_va(rproc, da, memsz, is_iram); 822 if (!ptr) { 823 dev_err(dev, "bad phdr da 0x%x mem 0x%x\n", da, memsz); 824 ret = -EINVAL; 825 break; 826 } 827 828 if (pru->data->is_k3) { 829 ret = pru_rproc_memcpy(ptr, elf_data + phdr->p_offset, 830 filesz); 831 if (ret) { 832 dev_err(dev, "PRU memory copy failed for da 0x%x memsz 0x%x\n", 833 da, memsz); 834 break; 835 } 836 } else { 837 memcpy(ptr, elf_data + phdr->p_offset, filesz); 838 } 839 840 /* skip the memzero logic performed by remoteproc ELF loader */ 841 } 842 843 return ret; 844 } 845 846 static const void * 847 pru_rproc_find_interrupt_map(struct device *dev, const struct firmware *fw) 848 { 849 struct elf32_shdr *shdr, *name_table_shdr; 850 const char *name_table; 851 const u8 *elf_data = fw->data; 852 struct elf32_hdr *ehdr = (struct elf32_hdr *)elf_data; 853 u16 shnum = ehdr->e_shnum; 854 u16 shstrndx = ehdr->e_shstrndx; 855 int i; 856 857 /* first, get the section header */ 858 shdr = (struct elf32_shdr *)(elf_data + ehdr->e_shoff); 859 /* compute name table section header entry in shdr array */ 860 name_table_shdr = shdr + shstrndx; 861 /* finally, compute the name table section address in elf */ 862 name_table = elf_data + name_table_shdr->sh_offset; 863 864 for (i = 0; i < shnum; i++, shdr++) { 865 u32 size = shdr->sh_size; 866 u32 offset = shdr->sh_offset; 867 u32 name = shdr->sh_name; 868 869 if (strcmp(name_table + name, ".pru_irq_map")) 870 continue; 871 872 /* make sure we have the entire irq map */ 873 if (offset + size > fw->size || offset + size < size) { 874 dev_err(dev, ".pru_irq_map section truncated\n"); 875 return ERR_PTR(-EINVAL); 876 } 877 878 /* make sure irq map has at least the header */ 879 if (sizeof(struct pru_irq_rsc) > size) { 880 dev_err(dev, "header-less .pru_irq_map section\n"); 881 return ERR_PTR(-EINVAL); 882 } 883 884 return shdr; 885 } 886 887 dev_dbg(dev, "no .pru_irq_map section found for this fw\n"); 888 889 return NULL; 890 } 891 892 /* 893 * Use a custom parse_fw callback function for dealing with PRU firmware 894 * specific sections. 895 * 896 * The firmware blob can contain optional ELF sections: .resource_table section 897 * and .pru_irq_map one. The second one contains the PRUSS interrupt mapping 898 * description, which needs to be setup before powering on the PRU core. To 899 * avoid RAM wastage this ELF section is not mapped to any ELF segment (by the 900 * firmware linker) and therefore is not loaded to PRU memory. 901 */ 902 static int pru_rproc_parse_fw(struct rproc *rproc, const struct firmware *fw) 903 { 904 struct device *dev = &rproc->dev; 905 struct pru_rproc *pru = rproc->priv; 906 const u8 *elf_data = fw->data; 907 const void *shdr; 908 u8 class = fw_elf_get_class(fw); 909 u64 sh_offset; 910 int ret; 911 912 /* load optional rsc table */ 913 ret = rproc_elf_load_rsc_table(rproc, fw); 914 if (ret == -EINVAL) 915 dev_dbg(&rproc->dev, "no resource table found for this fw\n"); 916 else if (ret) 917 return ret; 918 919 /* find .pru_interrupt_map section, not having it is not an error */ 920 shdr = pru_rproc_find_interrupt_map(dev, fw); 921 if (IS_ERR(shdr)) 922 return PTR_ERR(shdr); 923 924 if (!shdr) 925 return 0; 926 927 /* preserve pointer to PRU interrupt map together with it size */ 928 sh_offset = elf_shdr_get_sh_offset(class, shdr); 929 pru->pru_interrupt_map = (struct pru_irq_rsc *)(elf_data + sh_offset); 930 pru->pru_interrupt_map_sz = elf_shdr_get_sh_size(class, shdr); 931 932 return 0; 933 } 934 935 /* 936 * Compute PRU id based on the IRAM addresses. The PRU IRAMs are 937 * always at a particular offset within the PRUSS address space. 938 */ 939 static int pru_rproc_set_id(struct pru_rproc *pru) 940 { 941 int ret = 0; 942 943 switch (pru->mem_regions[PRU_IOMEM_IRAM].pa & PRU_IRAM_ADDR_MASK) { 944 case TX_PRU0_IRAM_ADDR_MASK: 945 fallthrough; 946 case RTU0_IRAM_ADDR_MASK: 947 fallthrough; 948 case PRU0_IRAM_ADDR_MASK: 949 pru->id = PRUSS_PRU0; 950 break; 951 case TX_PRU1_IRAM_ADDR_MASK: 952 fallthrough; 953 case RTU1_IRAM_ADDR_MASK: 954 fallthrough; 955 case PRU1_IRAM_ADDR_MASK: 956 pru->id = PRUSS_PRU1; 957 break; 958 default: 959 ret = -EINVAL; 960 } 961 962 return ret; 963 } 964 965 static int pru_rproc_probe(struct platform_device *pdev) 966 { 967 struct device *dev = &pdev->dev; 968 struct device_node *np = dev->of_node; 969 struct platform_device *ppdev = to_platform_device(dev->parent); 970 struct pru_rproc *pru; 971 const char *fw_name; 972 struct rproc *rproc = NULL; 973 struct resource *res; 974 int i, ret; 975 const struct pru_private_data *data; 976 const char *mem_names[PRU_IOMEM_MAX] = { "iram", "control", "debug" }; 977 978 data = of_device_get_match_data(&pdev->dev); 979 if (!data) 980 return -ENODEV; 981 982 ret = of_property_read_string(np, "firmware-name", &fw_name); 983 if (ret) { 984 dev_err(dev, "unable to retrieve firmware-name %d\n", ret); 985 return ret; 986 } 987 988 rproc = devm_rproc_alloc(dev, pdev->name, &pru_rproc_ops, fw_name, 989 sizeof(*pru)); 990 if (!rproc) { 991 dev_err(dev, "rproc_alloc failed\n"); 992 return -ENOMEM; 993 } 994 /* use a custom load function to deal with PRU-specific quirks */ 995 rproc->ops->load = pru_rproc_load_elf_segments; 996 997 /* use a custom parse function to deal with PRU-specific resources */ 998 rproc->ops->parse_fw = pru_rproc_parse_fw; 999 1000 /* error recovery is not supported for PRUs */ 1001 rproc->recovery_disabled = true; 1002 1003 /* 1004 * rproc_add will auto-boot the processor normally, but this is not 1005 * desired with PRU client driven boot-flow methodology. A PRU 1006 * application/client driver will boot the corresponding PRU 1007 * remote-processor as part of its state machine either through the 1008 * remoteproc sysfs interface or through the equivalent kernel API. 1009 */ 1010 rproc->auto_boot = false; 1011 1012 pru = rproc->priv; 1013 pru->dev = dev; 1014 pru->data = data; 1015 pru->pruss = platform_get_drvdata(ppdev); 1016 pru->rproc = rproc; 1017 pru->fw_name = fw_name; 1018 pru->client_np = NULL; 1019 spin_lock_init(&pru->rmw_lock); 1020 mutex_init(&pru->lock); 1021 1022 for (i = 0; i < ARRAY_SIZE(mem_names); i++) { 1023 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, 1024 mem_names[i]); 1025 pru->mem_regions[i].va = devm_ioremap_resource(dev, res); 1026 if (IS_ERR(pru->mem_regions[i].va)) { 1027 dev_err(dev, "failed to parse and map memory resource %d %s\n", 1028 i, mem_names[i]); 1029 ret = PTR_ERR(pru->mem_regions[i].va); 1030 return ret; 1031 } 1032 pru->mem_regions[i].pa = res->start; 1033 pru->mem_regions[i].size = resource_size(res); 1034 1035 dev_dbg(dev, "memory %8s: pa %pa size 0x%zx va %pK\n", 1036 mem_names[i], &pru->mem_regions[i].pa, 1037 pru->mem_regions[i].size, pru->mem_regions[i].va); 1038 } 1039 1040 ret = pru_rproc_set_id(pru); 1041 if (ret < 0) 1042 return ret; 1043 1044 platform_set_drvdata(pdev, rproc); 1045 1046 ret = devm_rproc_add(dev, pru->rproc); 1047 if (ret) { 1048 dev_err(dev, "rproc_add failed: %d\n", ret); 1049 return ret; 1050 } 1051 1052 pru_rproc_create_debug_entries(rproc); 1053 1054 dev_dbg(dev, "PRU rproc node %pOF probed successfully\n", np); 1055 1056 return 0; 1057 } 1058 1059 static void pru_rproc_remove(struct platform_device *pdev) 1060 { 1061 struct device *dev = &pdev->dev; 1062 struct rproc *rproc = platform_get_drvdata(pdev); 1063 1064 dev_dbg(dev, "%s: removing rproc %s\n", __func__, rproc->name); 1065 } 1066 1067 static const struct pru_private_data pru_data = { 1068 .type = PRU_TYPE_PRU, 1069 }; 1070 1071 static const struct pru_private_data k3_pru_data = { 1072 .type = PRU_TYPE_PRU, 1073 .is_k3 = 1, 1074 }; 1075 1076 static const struct pru_private_data k3_rtu_data = { 1077 .type = PRU_TYPE_RTU, 1078 .is_k3 = 1, 1079 }; 1080 1081 static const struct pru_private_data k3_tx_pru_data = { 1082 .type = PRU_TYPE_TX_PRU, 1083 .is_k3 = 1, 1084 }; 1085 1086 static const struct of_device_id pru_rproc_match[] = { 1087 { .compatible = "ti,am3356-pru", .data = &pru_data }, 1088 { .compatible = "ti,am4376-pru", .data = &pru_data }, 1089 { .compatible = "ti,am5728-pru", .data = &pru_data }, 1090 { .compatible = "ti,am642-pru", .data = &k3_pru_data }, 1091 { .compatible = "ti,am642-rtu", .data = &k3_rtu_data }, 1092 { .compatible = "ti,am642-tx-pru", .data = &k3_tx_pru_data }, 1093 { .compatible = "ti,k2g-pru", .data = &pru_data }, 1094 { .compatible = "ti,am654-pru", .data = &k3_pru_data }, 1095 { .compatible = "ti,am654-rtu", .data = &k3_rtu_data }, 1096 { .compatible = "ti,am654-tx-pru", .data = &k3_tx_pru_data }, 1097 { .compatible = "ti,j721e-pru", .data = &k3_pru_data }, 1098 { .compatible = "ti,j721e-rtu", .data = &k3_rtu_data }, 1099 { .compatible = "ti,j721e-tx-pru", .data = &k3_tx_pru_data }, 1100 { .compatible = "ti,am625-pru", .data = &k3_pru_data }, 1101 {}, 1102 }; 1103 MODULE_DEVICE_TABLE(of, pru_rproc_match); 1104 1105 static struct platform_driver pru_rproc_driver = { 1106 .driver = { 1107 .name = PRU_RPROC_DRVNAME, 1108 .of_match_table = pru_rproc_match, 1109 .suppress_bind_attrs = true, 1110 }, 1111 .probe = pru_rproc_probe, 1112 .remove_new = pru_rproc_remove, 1113 }; 1114 module_platform_driver(pru_rproc_driver); 1115 1116 MODULE_AUTHOR("Suman Anna <s-anna@ti.com>"); 1117 MODULE_AUTHOR("Andrew F. Davis <afd@ti.com>"); 1118 MODULE_AUTHOR("Grzegorz Jaszczyk <grzegorz.jaszczyk@linaro.org>"); 1119 MODULE_AUTHOR("Puranjay Mohan <p-mohan@ti.com>"); 1120 MODULE_AUTHOR("Md Danish Anwar <danishanwar@ti.com>"); 1121 MODULE_DESCRIPTION("PRU-ICSS Remote Processor Driver"); 1122 MODULE_LICENSE("GPL v2"); 1123