1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * OMAP Remote Processor driver 4 * 5 * Copyright (C) 2011-2020 Texas Instruments Incorporated - http://www.ti.com/ 6 * Copyright (C) 2011 Google, Inc. 7 * 8 * Ohad Ben-Cohen <ohad@wizery.com> 9 * Brian Swetland <swetland@google.com> 10 * Fernando Guzman Lugo <fernando.lugo@ti.com> 11 * Mark Grosen <mgrosen@ti.com> 12 * Suman Anna <s-anna@ti.com> 13 * Hari Kanigeri <h-kanigeri2@ti.com> 14 */ 15 16 #include <linux/kernel.h> 17 #include <linux/module.h> 18 #include <linux/clk.h> 19 #include <linux/clk/ti.h> 20 #include <linux/err.h> 21 #include <linux/io.h> 22 #include <linux/of.h> 23 #include <linux/of_platform.h> 24 #include <linux/of_reserved_mem.h> 25 #include <linux/platform_device.h> 26 #include <linux/pm_runtime.h> 27 #include <linux/dma-mapping.h> 28 #include <linux/interrupt.h> 29 #include <linux/remoteproc.h> 30 #include <linux/mailbox_client.h> 31 #include <linux/omap-iommu.h> 32 #include <linux/omap-mailbox.h> 33 #include <linux/regmap.h> 34 #include <linux/mfd/syscon.h> 35 #include <linux/reset.h> 36 #include <clocksource/timer-ti-dm.h> 37 38 #include <linux/platform_data/dmtimer-omap.h> 39 40 #ifdef CONFIG_ARM_DMA_USE_IOMMU 41 #include <asm/dma-iommu.h> 42 #endif 43 44 #include "omap_remoteproc.h" 45 #include "remoteproc_internal.h" 46 47 /* default auto-suspend delay (ms) */ 48 #define DEFAULT_AUTOSUSPEND_DELAY 10000 49 50 /** 51 * struct omap_rproc_boot_data - boot data structure for the DSP omap rprocs 52 * @syscon: regmap handle for the system control configuration module 53 * @boot_reg: boot register offset within the @syscon regmap 54 * @boot_reg_shift: bit-field shift required for the boot address value in 55 * @boot_reg 56 */ 57 struct omap_rproc_boot_data { 58 struct regmap *syscon; 59 unsigned int boot_reg; 60 unsigned int boot_reg_shift; 61 }; 62 63 /** 64 * struct omap_rproc_mem - internal memory structure 65 * @cpu_addr: MPU virtual address of the memory region 66 * @bus_addr: bus address used to access the memory region 67 * @dev_addr: device address of the memory region from DSP view 68 * @size: size of the memory region 69 */ 70 struct omap_rproc_mem { 71 void __iomem *cpu_addr; 72 phys_addr_t bus_addr; 73 u32 dev_addr; 74 size_t size; 75 }; 76 77 /** 78 * struct omap_rproc_timer - data structure for a timer used by a omap rproc 79 * @odt: timer pointer 80 * @timer_ops: OMAP dmtimer ops for @odt timer 81 * @irq: timer irq 82 */ 83 struct omap_rproc_timer { 84 struct omap_dm_timer *odt; 85 const struct omap_dm_timer_ops *timer_ops; 86 int irq; 87 }; 88 89 /** 90 * struct omap_rproc - omap remote processor state 91 * @mbox: mailbox channel handle 92 * @client: mailbox client to request the mailbox channel 93 * @boot_data: boot data structure for setting processor boot address 94 * @mem: internal memory regions data 95 * @num_mems: number of internal memory regions 96 * @num_timers: number of rproc timer(s) 97 * @num_wd_timers: number of rproc watchdog timers 98 * @timers: timer(s) info used by rproc 99 * @autosuspend_delay: auto-suspend delay value to be used for runtime pm 100 * @need_resume: if true a resume is needed in the system resume callback 101 * @rproc: rproc handle 102 * @reset: reset handle 103 * @pm_comp: completion primitive to sync for suspend response 104 * @fck: functional clock for the remoteproc 105 * @suspend_acked: state machine flag to store the suspend request ack 106 */ 107 struct omap_rproc { 108 struct mbox_chan *mbox; 109 struct mbox_client client; 110 struct omap_rproc_boot_data *boot_data; 111 struct omap_rproc_mem *mem; 112 int num_mems; 113 int num_timers; 114 int num_wd_timers; 115 struct omap_rproc_timer *timers; 116 int autosuspend_delay; 117 bool need_resume; 118 struct rproc *rproc; 119 struct reset_control *reset; 120 struct completion pm_comp; 121 struct clk *fck; 122 bool suspend_acked; 123 }; 124 125 /** 126 * struct omap_rproc_mem_data - memory definitions for an omap remote processor 127 * @name: name for this memory entry 128 * @dev_addr: device address for the memory entry 129 */ 130 struct omap_rproc_mem_data { 131 const char *name; 132 const u32 dev_addr; 133 }; 134 135 /** 136 * struct omap_rproc_dev_data - device data for the omap remote processor 137 * @device_name: device name of the remote processor 138 * @mems: memory definitions for this remote processor 139 */ 140 struct omap_rproc_dev_data { 141 const char *device_name; 142 const struct omap_rproc_mem_data *mems; 143 }; 144 145 /** 146 * omap_rproc_request_timer() - request a timer for a remoteproc 147 * @dev: device requesting the timer 148 * @np: device node pointer to the desired timer 149 * @timer: handle to a struct omap_rproc_timer to return the timer handle 150 * 151 * This helper function is used primarily to request a timer associated with 152 * a remoteproc. The returned handle is stored in the .odt field of the 153 * @timer structure passed in, and is used to invoke other timer specific 154 * ops (like starting a timer either during device initialization or during 155 * a resume operation, or for stopping/freeing a timer). 156 * 157 * Return: 0 on success, otherwise an appropriate failure 158 */ 159 static int omap_rproc_request_timer(struct device *dev, struct device_node *np, 160 struct omap_rproc_timer *timer) 161 { 162 int ret; 163 164 timer->odt = timer->timer_ops->request_by_node(np); 165 if (!timer->odt) { 166 dev_err(dev, "request for timer node %p failed\n", np); 167 return -EBUSY; 168 } 169 170 ret = timer->timer_ops->set_source(timer->odt, OMAP_TIMER_SRC_SYS_CLK); 171 if (ret) { 172 dev_err(dev, "error setting OMAP_TIMER_SRC_SYS_CLK as source for timer node %p\n", 173 np); 174 timer->timer_ops->free(timer->odt); 175 return ret; 176 } 177 178 /* clean counter, remoteproc code will set the value */ 179 timer->timer_ops->set_load(timer->odt, 0); 180 181 return 0; 182 } 183 184 /** 185 * omap_rproc_start_timer() - start a timer for a remoteproc 186 * @timer: handle to a OMAP rproc timer 187 * 188 * This helper function is used to start a timer associated with a remoteproc, 189 * obtained using the request_timer ops. The helper function needs to be 190 * invoked by the driver to start the timer (during device initialization) 191 * or to just resume the timer. 192 * 193 * Return: 0 on success, otherwise a failure as returned by DMTimer ops 194 */ 195 static inline int omap_rproc_start_timer(struct omap_rproc_timer *timer) 196 { 197 return timer->timer_ops->start(timer->odt); 198 } 199 200 /** 201 * omap_rproc_stop_timer() - stop a timer for a remoteproc 202 * @timer: handle to a OMAP rproc timer 203 * 204 * This helper function is used to disable a timer associated with a 205 * remoteproc, and needs to be called either during a device shutdown 206 * or suspend operation. The separate helper function allows the driver 207 * to just stop a timer without having to release the timer during a 208 * suspend operation. 209 * 210 * Return: 0 on success, otherwise a failure as returned by DMTimer ops 211 */ 212 static inline int omap_rproc_stop_timer(struct omap_rproc_timer *timer) 213 { 214 return timer->timer_ops->stop(timer->odt); 215 } 216 217 /** 218 * omap_rproc_release_timer() - release a timer for a remoteproc 219 * @timer: handle to a OMAP rproc timer 220 * 221 * This helper function is used primarily to release a timer associated 222 * with a remoteproc. The dmtimer will be available for other clients to 223 * use once released. 224 * 225 * Return: 0 on success, otherwise a failure as returned by DMTimer ops 226 */ 227 static inline int omap_rproc_release_timer(struct omap_rproc_timer *timer) 228 { 229 return timer->timer_ops->free(timer->odt); 230 } 231 232 /** 233 * omap_rproc_get_timer_irq() - get the irq for a timer 234 * @timer: handle to a OMAP rproc timer 235 * 236 * This function is used to get the irq associated with a watchdog timer. The 237 * function is called by the OMAP remoteproc driver to register a interrupt 238 * handler to handle watchdog events on the remote processor. 239 * 240 * Return: irq id on success, otherwise a failure as returned by DMTimer ops 241 */ 242 static inline int omap_rproc_get_timer_irq(struct omap_rproc_timer *timer) 243 { 244 return timer->timer_ops->get_irq(timer->odt); 245 } 246 247 /** 248 * omap_rproc_ack_timer_irq() - acknowledge a timer irq 249 * @timer: handle to a OMAP rproc timer 250 * 251 * This function is used to clear the irq associated with a watchdog timer. 252 * The function is called by the OMAP remoteproc upon a watchdog event on the 253 * remote processor to clear the interrupt status of the watchdog timer. 254 */ 255 static inline void omap_rproc_ack_timer_irq(struct omap_rproc_timer *timer) 256 { 257 timer->timer_ops->write_status(timer->odt, OMAP_TIMER_INT_OVERFLOW); 258 } 259 260 /** 261 * omap_rproc_watchdog_isr() - Watchdog ISR handler for remoteproc device 262 * @irq: IRQ number associated with a watchdog timer 263 * @data: IRQ handler data 264 * 265 * This ISR routine executes the required necessary low-level code to 266 * acknowledge a watchdog timer interrupt. There can be multiple watchdog 267 * timers associated with a rproc (like IPUs which have 2 watchdog timers, 268 * one per Cortex M3/M4 core), so a lookup has to be performed to identify 269 * the timer to acknowledge its interrupt. 270 * 271 * The function also invokes rproc_report_crash to report the watchdog event 272 * to the remoteproc driver core, to trigger a recovery. 273 * 274 * Return: IRQ_HANDLED on success, otherwise IRQ_NONE 275 */ 276 static irqreturn_t omap_rproc_watchdog_isr(int irq, void *data) 277 { 278 struct rproc *rproc = data; 279 struct omap_rproc *oproc = rproc->priv; 280 struct device *dev = rproc->dev.parent; 281 struct omap_rproc_timer *timers = oproc->timers; 282 struct omap_rproc_timer *wd_timer = NULL; 283 int num_timers = oproc->num_timers + oproc->num_wd_timers; 284 int i; 285 286 for (i = oproc->num_timers; i < num_timers; i++) { 287 if (timers[i].irq > 0 && irq == timers[i].irq) { 288 wd_timer = &timers[i]; 289 break; 290 } 291 } 292 293 if (!wd_timer) { 294 dev_err(dev, "invalid timer\n"); 295 return IRQ_NONE; 296 } 297 298 omap_rproc_ack_timer_irq(wd_timer); 299 300 rproc_report_crash(rproc, RPROC_WATCHDOG); 301 302 return IRQ_HANDLED; 303 } 304 305 /** 306 * omap_rproc_enable_timers() - enable the timers for a remoteproc 307 * @rproc: handle of a remote processor 308 * @configure: boolean flag used to acquire and configure the timer handle 309 * 310 * This function is used primarily to enable the timers associated with 311 * a remoteproc. The configure flag is provided to allow the driver 312 * to either acquire and start a timer (during device initialization) or 313 * to just start a timer (during a resume operation). 314 * 315 * Return: 0 on success, otherwise an appropriate failure 316 */ 317 static int omap_rproc_enable_timers(struct rproc *rproc, bool configure) 318 { 319 int i; 320 int ret = 0; 321 struct platform_device *tpdev; 322 struct dmtimer_platform_data *tpdata; 323 const struct omap_dm_timer_ops *timer_ops; 324 struct omap_rproc *oproc = rproc->priv; 325 struct omap_rproc_timer *timers = oproc->timers; 326 struct device *dev = rproc->dev.parent; 327 struct device_node *np = NULL; 328 int num_timers = oproc->num_timers + oproc->num_wd_timers; 329 330 if (!num_timers) 331 return 0; 332 333 if (!configure) 334 goto start_timers; 335 336 for (i = 0; i < num_timers; i++) { 337 if (i < oproc->num_timers) 338 np = of_parse_phandle(dev->of_node, "ti,timers", i); 339 else 340 np = of_parse_phandle(dev->of_node, 341 "ti,watchdog-timers", 342 (i - oproc->num_timers)); 343 if (!np) { 344 ret = -ENXIO; 345 dev_err(dev, "device node lookup for timer at index %d failed: %d\n", 346 i < oproc->num_timers ? i : 347 i - oproc->num_timers, ret); 348 goto free_timers; 349 } 350 351 tpdev = of_find_device_by_node(np); 352 if (!tpdev) { 353 ret = -ENODEV; 354 dev_err(dev, "could not get timer platform device\n"); 355 goto put_node; 356 } 357 358 tpdata = dev_get_platdata(&tpdev->dev); 359 put_device(&tpdev->dev); 360 if (!tpdata) { 361 ret = -EINVAL; 362 dev_err(dev, "dmtimer pdata structure NULL\n"); 363 goto put_node; 364 } 365 366 timer_ops = tpdata->timer_ops; 367 if (!timer_ops || !timer_ops->request_by_node || 368 !timer_ops->set_source || !timer_ops->set_load || 369 !timer_ops->free || !timer_ops->start || 370 !timer_ops->stop || !timer_ops->get_irq || 371 !timer_ops->write_status) { 372 ret = -EINVAL; 373 dev_err(dev, "device does not have required timer ops\n"); 374 goto put_node; 375 } 376 377 timers[i].irq = -1; 378 timers[i].timer_ops = timer_ops; 379 ret = omap_rproc_request_timer(dev, np, &timers[i]); 380 if (ret) { 381 dev_err(dev, "request for timer %p failed: %d\n", np, 382 ret); 383 goto put_node; 384 } 385 of_node_put(np); 386 387 if (i >= oproc->num_timers) { 388 timers[i].irq = omap_rproc_get_timer_irq(&timers[i]); 389 if (timers[i].irq < 0) { 390 dev_err(dev, "get_irq for timer %p failed: %d\n", 391 np, timers[i].irq); 392 ret = -EBUSY; 393 goto free_timers; 394 } 395 396 ret = request_irq(timers[i].irq, 397 omap_rproc_watchdog_isr, IRQF_SHARED, 398 "rproc-wdt", rproc); 399 if (ret) { 400 dev_err(dev, "error requesting irq for timer %p\n", 401 np); 402 omap_rproc_release_timer(&timers[i]); 403 timers[i].odt = NULL; 404 timers[i].timer_ops = NULL; 405 timers[i].irq = -1; 406 goto free_timers; 407 } 408 } 409 } 410 411 start_timers: 412 for (i = 0; i < num_timers; i++) { 413 ret = omap_rproc_start_timer(&timers[i]); 414 if (ret) { 415 dev_err(dev, "start timer %p failed failed: %d\n", np, 416 ret); 417 break; 418 } 419 } 420 if (ret) { 421 while (i >= 0) { 422 omap_rproc_stop_timer(&timers[i]); 423 i--; 424 } 425 goto put_node; 426 } 427 return 0; 428 429 put_node: 430 if (configure) 431 of_node_put(np); 432 free_timers: 433 while (i--) { 434 if (i >= oproc->num_timers) 435 free_irq(timers[i].irq, rproc); 436 omap_rproc_release_timer(&timers[i]); 437 timers[i].odt = NULL; 438 timers[i].timer_ops = NULL; 439 timers[i].irq = -1; 440 } 441 442 return ret; 443 } 444 445 /** 446 * omap_rproc_disable_timers() - disable the timers for a remoteproc 447 * @rproc: handle of a remote processor 448 * @configure: boolean flag used to release the timer handle 449 * 450 * This function is used primarily to disable the timers associated with 451 * a remoteproc. The configure flag is provided to allow the driver 452 * to either stop and release a timer (during device shutdown) or to just 453 * stop a timer (during a suspend operation). 454 * 455 * Return: 0 on success or no timers 456 */ 457 static int omap_rproc_disable_timers(struct rproc *rproc, bool configure) 458 { 459 int i; 460 struct omap_rproc *oproc = rproc->priv; 461 struct omap_rproc_timer *timers = oproc->timers; 462 int num_timers = oproc->num_timers + oproc->num_wd_timers; 463 464 if (!num_timers) 465 return 0; 466 467 for (i = 0; i < num_timers; i++) { 468 omap_rproc_stop_timer(&timers[i]); 469 if (configure) { 470 if (i >= oproc->num_timers) 471 free_irq(timers[i].irq, rproc); 472 omap_rproc_release_timer(&timers[i]); 473 timers[i].odt = NULL; 474 timers[i].timer_ops = NULL; 475 timers[i].irq = -1; 476 } 477 } 478 479 return 0; 480 } 481 482 /** 483 * omap_rproc_mbox_callback() - inbound mailbox message handler 484 * @client: mailbox client pointer used for requesting the mailbox channel 485 * @data: mailbox payload 486 * 487 * This handler is invoked by omap's mailbox driver whenever a mailbox 488 * message is received. Usually, the mailbox payload simply contains 489 * the index of the virtqueue that is kicked by the remote processor, 490 * and we let remoteproc core handle it. 491 * 492 * In addition to virtqueue indices, we also have some out-of-band values 493 * that indicates different events. Those values are deliberately very 494 * big so they don't coincide with virtqueue indices. 495 */ 496 static void omap_rproc_mbox_callback(struct mbox_client *client, void *data) 497 { 498 struct omap_rproc *oproc = container_of(client, struct omap_rproc, 499 client); 500 struct device *dev = oproc->rproc->dev.parent; 501 const char *name = oproc->rproc->name; 502 u32 msg = (u32)data; 503 504 dev_dbg(dev, "mbox msg: 0x%x\n", msg); 505 506 switch (msg) { 507 case RP_MBOX_CRASH: 508 /* 509 * remoteproc detected an exception, notify the rproc core. 510 * The remoteproc core will handle the recovery. 511 */ 512 dev_err(dev, "omap rproc %s crashed\n", name); 513 rproc_report_crash(oproc->rproc, RPROC_FATAL_ERROR); 514 break; 515 case RP_MBOX_ECHO_REPLY: 516 dev_info(dev, "received echo reply from %s\n", name); 517 break; 518 case RP_MBOX_SUSPEND_ACK: 519 case RP_MBOX_SUSPEND_CANCEL: 520 oproc->suspend_acked = msg == RP_MBOX_SUSPEND_ACK; 521 complete(&oproc->pm_comp); 522 break; 523 default: 524 if (msg >= RP_MBOX_READY && msg < RP_MBOX_END_MSG) 525 return; 526 if (msg > oproc->rproc->max_notifyid) { 527 dev_dbg(dev, "dropping unknown message 0x%x", msg); 528 return; 529 } 530 /* msg contains the index of the triggered vring */ 531 if (rproc_vq_interrupt(oproc->rproc, msg) == IRQ_NONE) 532 dev_dbg(dev, "no message was found in vqid %d\n", msg); 533 } 534 } 535 536 /* kick a virtqueue */ 537 static void omap_rproc_kick(struct rproc *rproc, int vqid) 538 { 539 struct omap_rproc *oproc = rproc->priv; 540 struct device *dev = rproc->dev.parent; 541 int ret; 542 543 /* wake up the rproc before kicking it */ 544 ret = pm_runtime_get_sync(dev); 545 if (WARN_ON(ret < 0)) { 546 dev_err(dev, "pm_runtime_get_sync() failed during kick, ret = %d\n", 547 ret); 548 pm_runtime_put_noidle(dev); 549 return; 550 } 551 552 /* send the index of the triggered virtqueue in the mailbox payload */ 553 ret = mbox_send_message(oproc->mbox, (void *)vqid); 554 if (ret < 0) 555 dev_err(dev, "failed to send mailbox message, status = %d\n", 556 ret); 557 558 pm_runtime_put_autosuspend(dev); 559 } 560 561 /** 562 * omap_rproc_write_dsp_boot_addr() - set boot address for DSP remote processor 563 * @rproc: handle of a remote processor 564 * 565 * Set boot address for a supported DSP remote processor. 566 * 567 * Return: 0 on success, or -EINVAL if boot address is not aligned properly 568 */ 569 static int omap_rproc_write_dsp_boot_addr(struct rproc *rproc) 570 { 571 struct device *dev = rproc->dev.parent; 572 struct omap_rproc *oproc = rproc->priv; 573 struct omap_rproc_boot_data *bdata = oproc->boot_data; 574 u32 offset = bdata->boot_reg; 575 u32 value; 576 u32 mask; 577 578 if (rproc->bootaddr & (SZ_1K - 1)) { 579 dev_err(dev, "invalid boot address 0x%llx, must be aligned on a 1KB boundary\n", 580 rproc->bootaddr); 581 return -EINVAL; 582 } 583 584 value = rproc->bootaddr >> bdata->boot_reg_shift; 585 mask = ~(SZ_1K - 1) >> bdata->boot_reg_shift; 586 587 return regmap_update_bits(bdata->syscon, offset, mask, value); 588 } 589 590 /* 591 * Power up the remote processor. 592 * 593 * This function will be invoked only after the firmware for this rproc 594 * was loaded, parsed successfully, and all of its resource requirements 595 * were met. 596 */ 597 static int omap_rproc_start(struct rproc *rproc) 598 { 599 struct omap_rproc *oproc = rproc->priv; 600 struct device *dev = rproc->dev.parent; 601 int ret; 602 struct mbox_client *client = &oproc->client; 603 604 if (oproc->boot_data) { 605 ret = omap_rproc_write_dsp_boot_addr(rproc); 606 if (ret) 607 return ret; 608 } 609 610 client->dev = dev; 611 client->tx_done = NULL; 612 client->rx_callback = omap_rproc_mbox_callback; 613 client->tx_block = false; 614 client->knows_txdone = false; 615 616 oproc->mbox = mbox_request_channel(client, 0); 617 if (IS_ERR(oproc->mbox)) { 618 ret = -EBUSY; 619 dev_err(dev, "mbox_request_channel failed: %ld\n", 620 PTR_ERR(oproc->mbox)); 621 return ret; 622 } 623 624 /* 625 * Ping the remote processor. this is only for sanity-sake; 626 * there is no functional effect whatsoever. 627 * 628 * Note that the reply will _not_ arrive immediately: this message 629 * will wait in the mailbox fifo until the remote processor is booted. 630 */ 631 ret = mbox_send_message(oproc->mbox, (void *)RP_MBOX_ECHO_REQUEST); 632 if (ret < 0) { 633 dev_err(dev, "mbox_send_message failed: %d\n", ret); 634 goto put_mbox; 635 } 636 637 ret = omap_rproc_enable_timers(rproc, true); 638 if (ret) { 639 dev_err(dev, "omap_rproc_enable_timers failed: %d\n", ret); 640 goto put_mbox; 641 } 642 643 ret = reset_control_deassert(oproc->reset); 644 if (ret) { 645 dev_err(dev, "reset control deassert failed: %d\n", ret); 646 goto disable_timers; 647 } 648 649 /* 650 * remote processor is up, so update the runtime pm status and 651 * enable the auto-suspend. The device usage count is incremented 652 * manually for balancing it for auto-suspend 653 */ 654 pm_runtime_set_active(dev); 655 pm_runtime_use_autosuspend(dev); 656 pm_runtime_get_noresume(dev); 657 pm_runtime_enable(dev); 658 pm_runtime_put_autosuspend(dev); 659 660 return 0; 661 662 disable_timers: 663 omap_rproc_disable_timers(rproc, true); 664 put_mbox: 665 mbox_free_channel(oproc->mbox); 666 return ret; 667 } 668 669 /* power off the remote processor */ 670 static int omap_rproc_stop(struct rproc *rproc) 671 { 672 struct device *dev = rproc->dev.parent; 673 struct omap_rproc *oproc = rproc->priv; 674 int ret; 675 676 /* 677 * cancel any possible scheduled runtime suspend by incrementing 678 * the device usage count, and resuming the device. The remoteproc 679 * also needs to be woken up if suspended, to avoid the remoteproc 680 * OS to continue to remember any context that it has saved, and 681 * avoid potential issues in misindentifying a subsequent device 682 * reboot as a power restore boot 683 */ 684 ret = pm_runtime_get_sync(dev); 685 if (ret < 0) { 686 pm_runtime_put_noidle(dev); 687 return ret; 688 } 689 690 ret = reset_control_assert(oproc->reset); 691 if (ret) 692 goto out; 693 694 ret = omap_rproc_disable_timers(rproc, true); 695 if (ret) 696 goto enable_device; 697 698 mbox_free_channel(oproc->mbox); 699 700 /* 701 * update the runtime pm states and status now that the remoteproc 702 * has stopped 703 */ 704 pm_runtime_disable(dev); 705 pm_runtime_dont_use_autosuspend(dev); 706 pm_runtime_put_noidle(dev); 707 pm_runtime_set_suspended(dev); 708 709 return 0; 710 711 enable_device: 712 reset_control_deassert(oproc->reset); 713 out: 714 /* schedule the next auto-suspend */ 715 pm_runtime_put_autosuspend(dev); 716 return ret; 717 } 718 719 /** 720 * omap_rproc_da_to_va() - internal memory translation helper 721 * @rproc: remote processor to apply the address translation for 722 * @da: device address to translate 723 * @len: length of the memory buffer 724 * @is_iomem: pointer filled in to indicate if @da is iomapped memory 725 * 726 * Custom function implementing the rproc .da_to_va ops to provide address 727 * translation (device address to kernel virtual address) for internal RAMs 728 * present in a DSP or IPU device). The translated addresses can be used 729 * either by the remoteproc core for loading, or by any rpmsg bus drivers. 730 * 731 * Return: translated virtual address in kernel memory space on success, 732 * or NULL on failure. 733 */ 734 static void *omap_rproc_da_to_va(struct rproc *rproc, u64 da, size_t len, bool *is_iomem) 735 { 736 struct omap_rproc *oproc = rproc->priv; 737 int i; 738 u32 offset; 739 740 if (len <= 0) 741 return NULL; 742 743 if (!oproc->num_mems) 744 return NULL; 745 746 for (i = 0; i < oproc->num_mems; i++) { 747 if (da >= oproc->mem[i].dev_addr && da + len <= 748 oproc->mem[i].dev_addr + oproc->mem[i].size) { 749 offset = da - oproc->mem[i].dev_addr; 750 /* __force to make sparse happy with type conversion */ 751 return (__force void *)(oproc->mem[i].cpu_addr + 752 offset); 753 } 754 } 755 756 return NULL; 757 } 758 759 static const struct rproc_ops omap_rproc_ops = { 760 .start = omap_rproc_start, 761 .stop = omap_rproc_stop, 762 .kick = omap_rproc_kick, 763 .da_to_va = omap_rproc_da_to_va, 764 }; 765 766 #ifdef CONFIG_PM 767 static bool _is_rproc_in_standby(struct omap_rproc *oproc) 768 { 769 return ti_clk_is_in_standby(oproc->fck); 770 } 771 772 /* 1 sec is long enough time to let the remoteproc side suspend the device */ 773 #define DEF_SUSPEND_TIMEOUT 1000 774 static int _omap_rproc_suspend(struct rproc *rproc, bool auto_suspend) 775 { 776 struct device *dev = rproc->dev.parent; 777 struct omap_rproc *oproc = rproc->priv; 778 unsigned long to = msecs_to_jiffies(DEF_SUSPEND_TIMEOUT); 779 unsigned long ta = jiffies + to; 780 u32 suspend_msg = auto_suspend ? 781 RP_MBOX_SUSPEND_AUTO : RP_MBOX_SUSPEND_SYSTEM; 782 int ret; 783 784 reinit_completion(&oproc->pm_comp); 785 oproc->suspend_acked = false; 786 ret = mbox_send_message(oproc->mbox, (void *)suspend_msg); 787 if (ret < 0) { 788 dev_err(dev, "PM mbox_send_message failed: %d\n", ret); 789 return ret; 790 } 791 792 ret = wait_for_completion_timeout(&oproc->pm_comp, to); 793 if (!oproc->suspend_acked) 794 return -EBUSY; 795 796 /* 797 * The remoteproc side is returning the ACK message before saving the 798 * context, because the context saving is performed within a SYS/BIOS 799 * function, and it cannot have any inter-dependencies against the IPC 800 * layer. Also, as the SYS/BIOS needs to preserve properly the processor 801 * register set, sending this ACK or signalling the completion of the 802 * context save through a shared memory variable can never be the 803 * absolute last thing to be executed on the remoteproc side, and the 804 * MPU cannot use the ACK message as a sync point to put the remoteproc 805 * into reset. The only way to ensure that the remote processor has 806 * completed saving the context is to check that the module has reached 807 * STANDBY state (after saving the context, the SYS/BIOS executes the 808 * appropriate target-specific WFI instruction causing the module to 809 * enter STANDBY). 810 */ 811 while (!_is_rproc_in_standby(oproc)) { 812 if (time_after(jiffies, ta)) 813 return -ETIME; 814 schedule(); 815 } 816 817 ret = reset_control_assert(oproc->reset); 818 if (ret) { 819 dev_err(dev, "reset assert during suspend failed %d\n", ret); 820 return ret; 821 } 822 823 ret = omap_rproc_disable_timers(rproc, false); 824 if (ret) { 825 dev_err(dev, "disabling timers during suspend failed %d\n", 826 ret); 827 goto enable_device; 828 } 829 830 /* 831 * IOMMUs would have to be disabled specifically for runtime suspend. 832 * They are handled automatically through System PM callbacks for 833 * regular system suspend 834 */ 835 if (auto_suspend) { 836 ret = omap_iommu_domain_deactivate(rproc->domain); 837 if (ret) { 838 dev_err(dev, "iommu domain deactivate failed %d\n", 839 ret); 840 goto enable_timers; 841 } 842 } 843 844 return 0; 845 846 enable_timers: 847 /* ignore errors on re-enabling code */ 848 omap_rproc_enable_timers(rproc, false); 849 enable_device: 850 reset_control_deassert(oproc->reset); 851 return ret; 852 } 853 854 static int _omap_rproc_resume(struct rproc *rproc, bool auto_suspend) 855 { 856 struct device *dev = rproc->dev.parent; 857 struct omap_rproc *oproc = rproc->priv; 858 int ret; 859 860 /* 861 * IOMMUs would have to be enabled specifically for runtime resume. 862 * They would have been already enabled automatically through System 863 * PM callbacks for regular system resume 864 */ 865 if (auto_suspend) { 866 ret = omap_iommu_domain_activate(rproc->domain); 867 if (ret) { 868 dev_err(dev, "omap_iommu activate failed %d\n", ret); 869 goto out; 870 } 871 } 872 873 /* boot address could be lost after suspend, so restore it */ 874 if (oproc->boot_data) { 875 ret = omap_rproc_write_dsp_boot_addr(rproc); 876 if (ret) { 877 dev_err(dev, "boot address restore failed %d\n", ret); 878 goto suspend_iommu; 879 } 880 } 881 882 ret = omap_rproc_enable_timers(rproc, false); 883 if (ret) { 884 dev_err(dev, "enabling timers during resume failed %d\n", ret); 885 goto suspend_iommu; 886 } 887 888 ret = reset_control_deassert(oproc->reset); 889 if (ret) { 890 dev_err(dev, "reset deassert during resume failed %d\n", ret); 891 goto disable_timers; 892 } 893 894 return 0; 895 896 disable_timers: 897 omap_rproc_disable_timers(rproc, false); 898 suspend_iommu: 899 if (auto_suspend) 900 omap_iommu_domain_deactivate(rproc->domain); 901 out: 902 return ret; 903 } 904 905 static int __maybe_unused omap_rproc_suspend(struct device *dev) 906 { 907 struct rproc *rproc = dev_get_drvdata(dev); 908 struct omap_rproc *oproc = rproc->priv; 909 int ret = 0; 910 911 mutex_lock(&rproc->lock); 912 if (rproc->state == RPROC_OFFLINE) 913 goto out; 914 915 if (rproc->state == RPROC_SUSPENDED) 916 goto out; 917 918 if (rproc->state != RPROC_RUNNING) { 919 ret = -EBUSY; 920 goto out; 921 } 922 923 ret = _omap_rproc_suspend(rproc, false); 924 if (ret) { 925 dev_err(dev, "suspend failed %d\n", ret); 926 goto out; 927 } 928 929 /* 930 * remoteproc is running at the time of system suspend, so remember 931 * it so as to wake it up during system resume 932 */ 933 oproc->need_resume = true; 934 rproc->state = RPROC_SUSPENDED; 935 936 out: 937 mutex_unlock(&rproc->lock); 938 return ret; 939 } 940 941 static int __maybe_unused omap_rproc_resume(struct device *dev) 942 { 943 struct rproc *rproc = dev_get_drvdata(dev); 944 struct omap_rproc *oproc = rproc->priv; 945 int ret = 0; 946 947 mutex_lock(&rproc->lock); 948 if (rproc->state == RPROC_OFFLINE) 949 goto out; 950 951 if (rproc->state != RPROC_SUSPENDED) { 952 ret = -EBUSY; 953 goto out; 954 } 955 956 /* 957 * remoteproc was auto-suspended at the time of system suspend, 958 * so no need to wake-up the processor (leave it in suspended 959 * state, will be woken up during a subsequent runtime_resume) 960 */ 961 if (!oproc->need_resume) 962 goto out; 963 964 ret = _omap_rproc_resume(rproc, false); 965 if (ret) { 966 dev_err(dev, "resume failed %d\n", ret); 967 goto out; 968 } 969 970 oproc->need_resume = false; 971 rproc->state = RPROC_RUNNING; 972 973 pm_runtime_mark_last_busy(dev); 974 out: 975 mutex_unlock(&rproc->lock); 976 return ret; 977 } 978 979 static int omap_rproc_runtime_suspend(struct device *dev) 980 { 981 struct rproc *rproc = dev_get_drvdata(dev); 982 struct omap_rproc *oproc = rproc->priv; 983 int ret; 984 985 mutex_lock(&rproc->lock); 986 if (rproc->state == RPROC_CRASHED) { 987 dev_dbg(dev, "rproc cannot be runtime suspended when crashed!\n"); 988 ret = -EBUSY; 989 goto out; 990 } 991 992 if (WARN_ON(rproc->state != RPROC_RUNNING)) { 993 dev_err(dev, "rproc cannot be runtime suspended when not running!\n"); 994 ret = -EBUSY; 995 goto out; 996 } 997 998 /* 999 * do not even attempt suspend if the remote processor is not 1000 * idled for runtime auto-suspend 1001 */ 1002 if (!_is_rproc_in_standby(oproc)) { 1003 ret = -EBUSY; 1004 goto abort; 1005 } 1006 1007 ret = _omap_rproc_suspend(rproc, true); 1008 if (ret) 1009 goto abort; 1010 1011 rproc->state = RPROC_SUSPENDED; 1012 mutex_unlock(&rproc->lock); 1013 return 0; 1014 1015 abort: 1016 pm_runtime_mark_last_busy(dev); 1017 out: 1018 mutex_unlock(&rproc->lock); 1019 return ret; 1020 } 1021 1022 static int omap_rproc_runtime_resume(struct device *dev) 1023 { 1024 struct rproc *rproc = dev_get_drvdata(dev); 1025 int ret; 1026 1027 mutex_lock(&rproc->lock); 1028 if (WARN_ON(rproc->state != RPROC_SUSPENDED)) { 1029 dev_err(dev, "rproc cannot be runtime resumed if not suspended! state=%d\n", 1030 rproc->state); 1031 ret = -EBUSY; 1032 goto out; 1033 } 1034 1035 ret = _omap_rproc_resume(rproc, true); 1036 if (ret) { 1037 dev_err(dev, "runtime resume failed %d\n", ret); 1038 goto out; 1039 } 1040 1041 rproc->state = RPROC_RUNNING; 1042 out: 1043 mutex_unlock(&rproc->lock); 1044 return ret; 1045 } 1046 #endif /* CONFIG_PM */ 1047 1048 static const struct omap_rproc_mem_data ipu_mems[] = { 1049 { .name = "l2ram", .dev_addr = 0x20000000 }, 1050 { }, 1051 }; 1052 1053 static const struct omap_rproc_mem_data dra7_dsp_mems[] = { 1054 { .name = "l2ram", .dev_addr = 0x800000 }, 1055 { .name = "l1pram", .dev_addr = 0xe00000 }, 1056 { .name = "l1dram", .dev_addr = 0xf00000 }, 1057 { }, 1058 }; 1059 1060 static const struct omap_rproc_dev_data omap4_dsp_dev_data = { 1061 .device_name = "dsp", 1062 }; 1063 1064 static const struct omap_rproc_dev_data omap4_ipu_dev_data = { 1065 .device_name = "ipu", 1066 .mems = ipu_mems, 1067 }; 1068 1069 static const struct omap_rproc_dev_data omap5_dsp_dev_data = { 1070 .device_name = "dsp", 1071 }; 1072 1073 static const struct omap_rproc_dev_data omap5_ipu_dev_data = { 1074 .device_name = "ipu", 1075 .mems = ipu_mems, 1076 }; 1077 1078 static const struct omap_rproc_dev_data dra7_dsp_dev_data = { 1079 .device_name = "dsp", 1080 .mems = dra7_dsp_mems, 1081 }; 1082 1083 static const struct omap_rproc_dev_data dra7_ipu_dev_data = { 1084 .device_name = "ipu", 1085 .mems = ipu_mems, 1086 }; 1087 1088 static const struct of_device_id omap_rproc_of_match[] = { 1089 { 1090 .compatible = "ti,omap4-dsp", 1091 .data = &omap4_dsp_dev_data, 1092 }, 1093 { 1094 .compatible = "ti,omap4-ipu", 1095 .data = &omap4_ipu_dev_data, 1096 }, 1097 { 1098 .compatible = "ti,omap5-dsp", 1099 .data = &omap5_dsp_dev_data, 1100 }, 1101 { 1102 .compatible = "ti,omap5-ipu", 1103 .data = &omap5_ipu_dev_data, 1104 }, 1105 { 1106 .compatible = "ti,dra7-dsp", 1107 .data = &dra7_dsp_dev_data, 1108 }, 1109 { 1110 .compatible = "ti,dra7-ipu", 1111 .data = &dra7_ipu_dev_data, 1112 }, 1113 { 1114 /* end */ 1115 }, 1116 }; 1117 MODULE_DEVICE_TABLE(of, omap_rproc_of_match); 1118 1119 static const char *omap_rproc_get_firmware(struct platform_device *pdev) 1120 { 1121 const char *fw_name; 1122 int ret; 1123 1124 ret = of_property_read_string(pdev->dev.of_node, "firmware-name", 1125 &fw_name); 1126 if (ret) 1127 return ERR_PTR(ret); 1128 1129 return fw_name; 1130 } 1131 1132 static int omap_rproc_get_boot_data(struct platform_device *pdev, 1133 struct rproc *rproc) 1134 { 1135 struct device_node *np = pdev->dev.of_node; 1136 struct omap_rproc *oproc = rproc->priv; 1137 const struct omap_rproc_dev_data *data; 1138 1139 data = of_device_get_match_data(&pdev->dev); 1140 if (!data) 1141 return -ENODEV; 1142 1143 if (!of_property_read_bool(np, "ti,bootreg")) 1144 return 0; 1145 1146 oproc->boot_data = devm_kzalloc(&pdev->dev, sizeof(*oproc->boot_data), 1147 GFP_KERNEL); 1148 if (!oproc->boot_data) 1149 return -ENOMEM; 1150 1151 oproc->boot_data->syscon = 1152 syscon_regmap_lookup_by_phandle(np, "ti,bootreg"); 1153 if (IS_ERR(oproc->boot_data->syscon)) 1154 return PTR_ERR(oproc->boot_data->syscon); 1155 1156 if (of_property_read_u32_index(np, "ti,bootreg", 1, 1157 &oproc->boot_data->boot_reg)) { 1158 dev_err(&pdev->dev, "couldn't get the boot register\n"); 1159 return -EINVAL; 1160 } 1161 1162 of_property_read_u32_index(np, "ti,bootreg", 2, 1163 &oproc->boot_data->boot_reg_shift); 1164 1165 return 0; 1166 } 1167 1168 static int omap_rproc_of_get_internal_memories(struct platform_device *pdev, 1169 struct rproc *rproc) 1170 { 1171 struct omap_rproc *oproc = rproc->priv; 1172 struct device *dev = &pdev->dev; 1173 const struct omap_rproc_dev_data *data; 1174 struct resource *res; 1175 int num_mems; 1176 int i; 1177 1178 data = of_device_get_match_data(dev); 1179 if (!data) 1180 return -ENODEV; 1181 1182 if (!data->mems) 1183 return 0; 1184 1185 num_mems = of_property_count_elems_of_size(dev->of_node, "reg", 1186 sizeof(u32)) / 2; 1187 1188 oproc->mem = devm_kcalloc(dev, num_mems, sizeof(*oproc->mem), 1189 GFP_KERNEL); 1190 if (!oproc->mem) 1191 return -ENOMEM; 1192 1193 for (i = 0; data->mems[i].name; i++) { 1194 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, 1195 data->mems[i].name); 1196 if (!res) { 1197 dev_err(dev, "no memory defined for %s\n", 1198 data->mems[i].name); 1199 return -ENOMEM; 1200 } 1201 oproc->mem[i].cpu_addr = devm_ioremap_resource(dev, res); 1202 if (IS_ERR(oproc->mem[i].cpu_addr)) { 1203 dev_err(dev, "failed to parse and map %s memory\n", 1204 data->mems[i].name); 1205 return PTR_ERR(oproc->mem[i].cpu_addr); 1206 } 1207 oproc->mem[i].bus_addr = res->start; 1208 oproc->mem[i].dev_addr = data->mems[i].dev_addr; 1209 oproc->mem[i].size = resource_size(res); 1210 1211 dev_dbg(dev, "memory %8s: bus addr %pa size 0x%x va %p da 0x%x\n", 1212 data->mems[i].name, &oproc->mem[i].bus_addr, 1213 oproc->mem[i].size, oproc->mem[i].cpu_addr, 1214 oproc->mem[i].dev_addr); 1215 } 1216 oproc->num_mems = num_mems; 1217 1218 return 0; 1219 } 1220 1221 #ifdef CONFIG_OMAP_REMOTEPROC_WATCHDOG 1222 static int omap_rproc_count_wdog_timers(struct device *dev) 1223 { 1224 struct device_node *np = dev->of_node; 1225 int ret; 1226 1227 ret = of_count_phandle_with_args(np, "ti,watchdog-timers", NULL); 1228 if (ret <= 0) { 1229 dev_dbg(dev, "device does not have watchdog timers, status = %d\n", 1230 ret); 1231 ret = 0; 1232 } 1233 1234 return ret; 1235 } 1236 #else 1237 static int omap_rproc_count_wdog_timers(struct device *dev) 1238 { 1239 return 0; 1240 } 1241 #endif 1242 1243 static int omap_rproc_of_get_timers(struct platform_device *pdev, 1244 struct rproc *rproc) 1245 { 1246 struct device_node *np = pdev->dev.of_node; 1247 struct omap_rproc *oproc = rproc->priv; 1248 struct device *dev = &pdev->dev; 1249 int num_timers; 1250 1251 /* 1252 * Timer nodes are directly used in client nodes as phandles, so 1253 * retrieve the count using appropriate size 1254 */ 1255 oproc->num_timers = of_count_phandle_with_args(np, "ti,timers", NULL); 1256 if (oproc->num_timers <= 0) { 1257 dev_dbg(dev, "device does not have timers, status = %d\n", 1258 oproc->num_timers); 1259 oproc->num_timers = 0; 1260 } 1261 1262 oproc->num_wd_timers = omap_rproc_count_wdog_timers(dev); 1263 1264 num_timers = oproc->num_timers + oproc->num_wd_timers; 1265 if (num_timers) { 1266 oproc->timers = devm_kcalloc(dev, num_timers, 1267 sizeof(*oproc->timers), 1268 GFP_KERNEL); 1269 if (!oproc->timers) 1270 return -ENOMEM; 1271 1272 dev_dbg(dev, "device has %d tick timers and %d watchdog timers\n", 1273 oproc->num_timers, oproc->num_wd_timers); 1274 } 1275 1276 return 0; 1277 } 1278 1279 static void omap_rproc_mem_release(void *data) 1280 { 1281 struct device *dev = data; 1282 1283 of_reserved_mem_device_release(dev); 1284 } 1285 1286 static int omap_rproc_probe(struct platform_device *pdev) 1287 { 1288 struct device_node *np = pdev->dev.of_node; 1289 struct omap_rproc *oproc; 1290 struct rproc *rproc; 1291 const char *firmware; 1292 int ret; 1293 struct reset_control *reset; 1294 1295 if (!np) { 1296 dev_err(&pdev->dev, "only DT-based devices are supported\n"); 1297 return -ENODEV; 1298 } 1299 1300 reset = devm_reset_control_array_get_exclusive(&pdev->dev); 1301 if (IS_ERR(reset)) 1302 return PTR_ERR(reset); 1303 1304 firmware = omap_rproc_get_firmware(pdev); 1305 if (IS_ERR(firmware)) 1306 return PTR_ERR(firmware); 1307 1308 ret = dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(32)); 1309 if (ret) { 1310 dev_err(&pdev->dev, "dma_set_coherent_mask: %d\n", ret); 1311 return ret; 1312 } 1313 1314 rproc = devm_rproc_alloc(&pdev->dev, dev_name(&pdev->dev), &omap_rproc_ops, 1315 firmware, sizeof(*oproc)); 1316 if (!rproc) 1317 return -ENOMEM; 1318 1319 oproc = rproc->priv; 1320 oproc->rproc = rproc; 1321 oproc->reset = reset; 1322 /* All existing OMAP IPU and DSP processors have an MMU */ 1323 rproc->has_iommu = true; 1324 1325 #ifdef CONFIG_ARM_DMA_USE_IOMMU 1326 /* 1327 * Throw away the ARM DMA mapping that we'll never use, so it doesn't 1328 * interfere with the core rproc->domain and we get the right DMA ops. 1329 */ 1330 if (pdev->dev.archdata.mapping) { 1331 struct dma_iommu_mapping *mapping = to_dma_iommu_mapping(&pdev->dev); 1332 1333 arm_iommu_detach_device(&pdev->dev); 1334 arm_iommu_release_mapping(mapping); 1335 } 1336 #endif 1337 1338 ret = omap_rproc_of_get_internal_memories(pdev, rproc); 1339 if (ret) 1340 return ret; 1341 1342 ret = omap_rproc_get_boot_data(pdev, rproc); 1343 if (ret) 1344 return ret; 1345 1346 ret = omap_rproc_of_get_timers(pdev, rproc); 1347 if (ret) 1348 return ret; 1349 1350 init_completion(&oproc->pm_comp); 1351 oproc->autosuspend_delay = DEFAULT_AUTOSUSPEND_DELAY; 1352 1353 of_property_read_u32(pdev->dev.of_node, "ti,autosuspend-delay-ms", 1354 &oproc->autosuspend_delay); 1355 1356 pm_runtime_set_autosuspend_delay(&pdev->dev, oproc->autosuspend_delay); 1357 1358 oproc->fck = devm_clk_get(&pdev->dev, 0); 1359 if (IS_ERR(oproc->fck)) 1360 return PTR_ERR(oproc->fck); 1361 1362 ret = of_reserved_mem_device_init(&pdev->dev); 1363 if (ret) { 1364 dev_warn(&pdev->dev, "device does not have specific CMA pool.\n"); 1365 dev_warn(&pdev->dev, "Typically this should be provided,\n"); 1366 dev_warn(&pdev->dev, "only omit if you know what you are doing.\n"); 1367 } 1368 ret = devm_add_action_or_reset(&pdev->dev, omap_rproc_mem_release, &pdev->dev); 1369 if (ret) 1370 return ret; 1371 1372 platform_set_drvdata(pdev, rproc); 1373 1374 ret = devm_rproc_add(&pdev->dev, rproc); 1375 if (ret) 1376 return ret; 1377 1378 return 0; 1379 } 1380 1381 static const struct dev_pm_ops omap_rproc_pm_ops = { 1382 SET_SYSTEM_SLEEP_PM_OPS(omap_rproc_suspend, omap_rproc_resume) 1383 SET_RUNTIME_PM_OPS(omap_rproc_runtime_suspend, 1384 omap_rproc_runtime_resume, NULL) 1385 }; 1386 1387 static struct platform_driver omap_rproc_driver = { 1388 .probe = omap_rproc_probe, 1389 .driver = { 1390 .name = "omap-rproc", 1391 .pm = &omap_rproc_pm_ops, 1392 .of_match_table = omap_rproc_of_match, 1393 }, 1394 }; 1395 1396 module_platform_driver(omap_rproc_driver); 1397 1398 MODULE_LICENSE("GPL v2"); 1399 MODULE_DESCRIPTION("OMAP Remote Processor control driver"); 1400