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