1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2016-2023, NVIDIA CORPORATION. All rights reserved.
6 #include <linux/delay.h>
7 #include <linux/interrupt.h>
9 #include <linux/mailbox_controller.h>
11 #include <linux/platform_device.h>
13 #include <linux/slab.h>
15 #include <soc/tegra/fuse.h>
17 #include <dt-bindings/mailbox/tegra186-hsp.h>
21 #define HSP_INT_IE(x) (0x100 + ((x) * 4))
22 #define HSP_INT_IV 0x300
23 #define HSP_INT_IR 0x304
25 #define HSP_INT_EMPTY_SHIFT 0
26 #define HSP_INT_EMPTY_MASK 0xff
27 #define HSP_INT_FULL_SHIFT 8
28 #define HSP_INT_FULL_MASK 0xff
30 #define HSP_INT_DIMENSIONING 0x380
31 #define HSP_nSM_SHIFT 0
32 #define HSP_nSS_SHIFT 4
33 #define HSP_nAS_SHIFT 8
34 #define HSP_nDB_SHIFT 12
35 #define HSP_nSI_SHIFT 16
36 #define HSP_nINT_MASK 0xf
38 #define HSP_DB_TRIGGER 0x0
39 #define HSP_DB_ENABLE 0x4
40 #define HSP_DB_RAW 0x8
41 #define HSP_DB_PENDING 0xc
43 #define HSP_SM_SHRD_MBOX 0x0
44 #define HSP_SM_SHRD_MBOX_FULL BIT(31)
45 #define HSP_SM_SHRD_MBOX_FULL_INT_IE 0x04
46 #define HSP_SM_SHRD_MBOX_EMPTY_INT_IE 0x08
48 #define HSP_SHRD_MBOX_TYPE1_TAG 0x40
49 #define HSP_SHRD_MBOX_TYPE1_DATA0 0x48
50 #define HSP_SHRD_MBOX_TYPE1_DATA1 0x4c
51 #define HSP_SHRD_MBOX_TYPE1_DATA2 0x50
52 #define HSP_SHRD_MBOX_TYPE1_DATA3 0x54
54 #define HSP_DB_CCPLEX 1
58 #define HSP_MBOX_TYPE_MASK 0xff
60 struct tegra_hsp_channel
;
63 struct tegra_hsp_channel
{
64 struct tegra_hsp
*hsp
;
65 struct mbox_chan
*chan
;
69 struct tegra_hsp_doorbell
{
70 struct tegra_hsp_channel channel
;
71 struct list_head list
;
77 struct tegra_hsp_sm_ops
{
78 void (*send
)(struct tegra_hsp_channel
*channel
, void *data
);
79 void (*recv
)(struct tegra_hsp_channel
*channel
);
82 struct tegra_hsp_mailbox
{
83 struct tegra_hsp_channel channel
;
84 const struct tegra_hsp_sm_ops
*ops
;
89 struct tegra_hsp_db_map
{
95 struct tegra_hsp_soc
{
96 const struct tegra_hsp_db_map
*map
;
99 unsigned int reg_stride
;
104 const struct tegra_hsp_soc
*soc
;
105 struct mbox_controller mbox_db
;
106 struct mbox_controller mbox_sm
;
108 unsigned int doorbell_irq
;
109 unsigned int *shared_irqs
;
110 unsigned int shared_irq
;
118 struct lock_class_key lock_key
;
120 struct list_head doorbells
;
121 struct tegra_hsp_mailbox
*mailboxes
;
126 static inline u32
tegra_hsp_readl(struct tegra_hsp
*hsp
, unsigned int offset
)
128 return readl(hsp
->regs
+ offset
);
131 static inline void tegra_hsp_writel(struct tegra_hsp
*hsp
, u32 value
,
134 writel(value
, hsp
->regs
+ offset
);
137 static inline u32
tegra_hsp_channel_readl(struct tegra_hsp_channel
*channel
,
140 return readl(channel
->regs
+ offset
);
143 static inline void tegra_hsp_channel_writel(struct tegra_hsp_channel
*channel
,
144 u32 value
, unsigned int offset
)
146 writel(value
, channel
->regs
+ offset
);
149 static bool tegra_hsp_doorbell_can_ring(struct tegra_hsp_doorbell
*db
)
153 value
= tegra_hsp_channel_readl(&db
->channel
, HSP_DB_ENABLE
);
155 return (value
& BIT(TEGRA_HSP_DB_MASTER_CCPLEX
)) != 0;
158 static struct tegra_hsp_doorbell
*
159 __tegra_hsp_doorbell_get(struct tegra_hsp
*hsp
, unsigned int master
)
161 struct tegra_hsp_doorbell
*entry
;
163 list_for_each_entry(entry
, &hsp
->doorbells
, list
)
164 if (entry
->master
== master
)
170 static struct tegra_hsp_doorbell
*
171 tegra_hsp_doorbell_get(struct tegra_hsp
*hsp
, unsigned int master
)
173 struct tegra_hsp_doorbell
*db
;
176 spin_lock_irqsave(&hsp
->lock
, flags
);
177 db
= __tegra_hsp_doorbell_get(hsp
, master
);
178 spin_unlock_irqrestore(&hsp
->lock
, flags
);
183 static irqreturn_t
tegra_hsp_doorbell_irq(int irq
, void *data
)
185 struct tegra_hsp
*hsp
= data
;
186 struct tegra_hsp_doorbell
*db
;
187 unsigned long master
, value
;
189 db
= tegra_hsp_doorbell_get(hsp
, TEGRA_HSP_DB_MASTER_CCPLEX
);
193 value
= tegra_hsp_channel_readl(&db
->channel
, HSP_DB_PENDING
);
194 tegra_hsp_channel_writel(&db
->channel
, value
, HSP_DB_PENDING
);
196 spin_lock(&hsp
->lock
);
198 for_each_set_bit(master
, &value
, hsp
->mbox_db
.num_chans
) {
199 struct tegra_hsp_doorbell
*db
;
201 db
= __tegra_hsp_doorbell_get(hsp
, master
);
203 * Depending on the bootloader chain, the CCPLEX doorbell will
204 * have some doorbells enabled, which means that requesting an
205 * interrupt will immediately fire.
207 * In that case, db->channel.chan will still be NULL here and
208 * cause a crash if not properly guarded.
210 * It remains to be seen if ignoring the doorbell in that case
211 * is the correct solution.
213 if (db
&& db
->channel
.chan
)
214 mbox_chan_received_data(db
->channel
.chan
, NULL
);
217 spin_unlock(&hsp
->lock
);
222 static irqreturn_t
tegra_hsp_shared_irq(int irq
, void *data
)
224 struct tegra_hsp
*hsp
= data
;
225 unsigned long bit
, mask
;
228 status
= tegra_hsp_readl(hsp
, HSP_INT_IR
) & hsp
->mask
;
230 /* process EMPTY interrupts first */
231 mask
= (status
>> HSP_INT_EMPTY_SHIFT
) & HSP_INT_EMPTY_MASK
;
233 for_each_set_bit(bit
, &mask
, hsp
->num_sm
) {
234 struct tegra_hsp_mailbox
*mb
= &hsp
->mailboxes
[bit
];
238 * Disable EMPTY interrupts until data is sent with
239 * the next message. These interrupts are level-
240 * triggered, so if we kept them enabled they would
241 * constantly trigger until we next write data into
244 spin_lock(&hsp
->lock
);
246 hsp
->mask
&= ~BIT(HSP_INT_EMPTY_SHIFT
+ mb
->index
);
247 tegra_hsp_writel(hsp
, hsp
->mask
,
248 HSP_INT_IE(hsp
->shared_irq
));
250 spin_unlock(&hsp
->lock
);
252 mbox_chan_txdone(mb
->channel
.chan
, 0);
256 /* process FULL interrupts */
257 mask
= (status
>> HSP_INT_FULL_SHIFT
) & HSP_INT_FULL_MASK
;
259 for_each_set_bit(bit
, &mask
, hsp
->num_sm
) {
260 struct tegra_hsp_mailbox
*mb
= &hsp
->mailboxes
[bit
];
263 mb
->ops
->recv(&mb
->channel
);
269 static struct tegra_hsp_channel
*
270 tegra_hsp_doorbell_create(struct tegra_hsp
*hsp
, const char *name
,
271 unsigned int master
, unsigned int index
)
273 struct tegra_hsp_doorbell
*db
;
277 db
= devm_kzalloc(hsp
->dev
, sizeof(*db
), GFP_KERNEL
);
279 return ERR_PTR(-ENOMEM
);
281 offset
= (1 + (hsp
->num_sm
/ 2) + hsp
->num_ss
+ hsp
->num_as
) * SZ_64K
;
282 offset
+= index
* hsp
->soc
->reg_stride
;
284 db
->channel
.regs
= hsp
->regs
+ offset
;
285 db
->channel
.hsp
= hsp
;
287 db
->name
= devm_kstrdup_const(hsp
->dev
, name
, GFP_KERNEL
);
291 spin_lock_irqsave(&hsp
->lock
, flags
);
292 list_add_tail(&db
->list
, &hsp
->doorbells
);
293 spin_unlock_irqrestore(&hsp
->lock
, flags
);
298 static int tegra_hsp_doorbell_send_data(struct mbox_chan
*chan
, void *data
)
300 struct tegra_hsp_doorbell
*db
= chan
->con_priv
;
302 tegra_hsp_channel_writel(&db
->channel
, 1, HSP_DB_TRIGGER
);
307 static int tegra_hsp_doorbell_startup(struct mbox_chan
*chan
)
309 struct tegra_hsp_doorbell
*db
= chan
->con_priv
;
310 struct tegra_hsp
*hsp
= db
->channel
.hsp
;
311 struct tegra_hsp_doorbell
*ccplex
;
315 if (db
->master
>= chan
->mbox
->num_chans
) {
316 dev_err(chan
->mbox
->dev
,
317 "invalid master ID %u for HSP channel\n",
322 ccplex
= tegra_hsp_doorbell_get(hsp
, TEGRA_HSP_DB_MASTER_CCPLEX
);
327 * On simulation platforms the BPMP hasn't had a chance yet to mark
328 * the doorbell as ringable by the CCPLEX, so we want to skip extra
331 if (tegra_is_silicon() && !tegra_hsp_doorbell_can_ring(db
))
334 spin_lock_irqsave(&hsp
->lock
, flags
);
336 value
= tegra_hsp_channel_readl(&ccplex
->channel
, HSP_DB_ENABLE
);
337 value
|= BIT(db
->master
);
338 tegra_hsp_channel_writel(&ccplex
->channel
, value
, HSP_DB_ENABLE
);
340 spin_unlock_irqrestore(&hsp
->lock
, flags
);
345 static void tegra_hsp_doorbell_shutdown(struct mbox_chan
*chan
)
347 struct tegra_hsp_doorbell
*db
= chan
->con_priv
;
348 struct tegra_hsp
*hsp
= db
->channel
.hsp
;
349 struct tegra_hsp_doorbell
*ccplex
;
353 ccplex
= tegra_hsp_doorbell_get(hsp
, TEGRA_HSP_DB_MASTER_CCPLEX
);
357 spin_lock_irqsave(&hsp
->lock
, flags
);
359 value
= tegra_hsp_channel_readl(&ccplex
->channel
, HSP_DB_ENABLE
);
360 value
&= ~BIT(db
->master
);
361 tegra_hsp_channel_writel(&ccplex
->channel
, value
, HSP_DB_ENABLE
);
363 spin_unlock_irqrestore(&hsp
->lock
, flags
);
366 static const struct mbox_chan_ops tegra_hsp_db_ops
= {
367 .send_data
= tegra_hsp_doorbell_send_data
,
368 .startup
= tegra_hsp_doorbell_startup
,
369 .shutdown
= tegra_hsp_doorbell_shutdown
,
372 static void tegra_hsp_sm_send32(struct tegra_hsp_channel
*channel
, void *data
)
376 /* copy data and mark mailbox full */
377 value
= (u32
)(unsigned long)data
;
378 value
|= HSP_SM_SHRD_MBOX_FULL
;
380 tegra_hsp_channel_writel(channel
, value
, HSP_SM_SHRD_MBOX
);
383 static void tegra_hsp_sm_recv32(struct tegra_hsp_channel
*channel
)
388 value
= tegra_hsp_channel_readl(channel
, HSP_SM_SHRD_MBOX
);
389 value
&= ~HSP_SM_SHRD_MBOX_FULL
;
390 msg
= (void *)(unsigned long)value
;
393 * Need to clear all bits here since some producers, such as TCU, depend
394 * on fields in the register getting cleared by the consumer.
396 * The mailbox API doesn't give the consumers a way of doing that
397 * explicitly, so we have to make sure we cover all possible cases.
399 tegra_hsp_channel_writel(channel
, 0x0, HSP_SM_SHRD_MBOX
);
401 mbox_chan_received_data(channel
->chan
, msg
);
404 static const struct tegra_hsp_sm_ops tegra_hsp_sm_32bit_ops
= {
405 .send
= tegra_hsp_sm_send32
,
406 .recv
= tegra_hsp_sm_recv32
,
409 static void tegra_hsp_sm_send128(struct tegra_hsp_channel
*channel
, void *data
)
413 memcpy(value
, data
, sizeof(value
));
416 tegra_hsp_channel_writel(channel
, value
[0], HSP_SHRD_MBOX_TYPE1_DATA0
);
417 tegra_hsp_channel_writel(channel
, value
[1], HSP_SHRD_MBOX_TYPE1_DATA1
);
418 tegra_hsp_channel_writel(channel
, value
[2], HSP_SHRD_MBOX_TYPE1_DATA2
);
419 tegra_hsp_channel_writel(channel
, value
[3], HSP_SHRD_MBOX_TYPE1_DATA3
);
421 /* Update tag to mark mailbox full */
422 tegra_hsp_channel_writel(channel
, HSP_SM_SHRD_MBOX_FULL
,
423 HSP_SHRD_MBOX_TYPE1_TAG
);
426 static void tegra_hsp_sm_recv128(struct tegra_hsp_channel
*channel
)
431 value
[0] = tegra_hsp_channel_readl(channel
, HSP_SHRD_MBOX_TYPE1_DATA0
);
432 value
[1] = tegra_hsp_channel_readl(channel
, HSP_SHRD_MBOX_TYPE1_DATA1
);
433 value
[2] = tegra_hsp_channel_readl(channel
, HSP_SHRD_MBOX_TYPE1_DATA2
);
434 value
[3] = tegra_hsp_channel_readl(channel
, HSP_SHRD_MBOX_TYPE1_DATA3
);
436 msg
= (void *)(unsigned long)value
;
439 * Clear data registers and tag.
441 tegra_hsp_channel_writel(channel
, 0x0, HSP_SHRD_MBOX_TYPE1_DATA0
);
442 tegra_hsp_channel_writel(channel
, 0x0, HSP_SHRD_MBOX_TYPE1_DATA1
);
443 tegra_hsp_channel_writel(channel
, 0x0, HSP_SHRD_MBOX_TYPE1_DATA2
);
444 tegra_hsp_channel_writel(channel
, 0x0, HSP_SHRD_MBOX_TYPE1_DATA3
);
445 tegra_hsp_channel_writel(channel
, 0x0, HSP_SHRD_MBOX_TYPE1_TAG
);
447 mbox_chan_received_data(channel
->chan
, msg
);
450 static const struct tegra_hsp_sm_ops tegra_hsp_sm_128bit_ops
= {
451 .send
= tegra_hsp_sm_send128
,
452 .recv
= tegra_hsp_sm_recv128
,
455 static int tegra_hsp_mailbox_send_data(struct mbox_chan
*chan
, void *data
)
457 struct tegra_hsp_mailbox
*mb
= chan
->con_priv
;
458 struct tegra_hsp
*hsp
= mb
->channel
.hsp
;
461 if (WARN_ON(!mb
->producer
))
464 mb
->ops
->send(&mb
->channel
, data
);
466 /* enable EMPTY interrupt for the shared mailbox */
467 spin_lock_irqsave(&hsp
->lock
, flags
);
469 hsp
->mask
|= BIT(HSP_INT_EMPTY_SHIFT
+ mb
->index
);
470 tegra_hsp_writel(hsp
, hsp
->mask
, HSP_INT_IE(hsp
->shared_irq
));
472 spin_unlock_irqrestore(&hsp
->lock
, flags
);
477 static int tegra_hsp_mailbox_flush(struct mbox_chan
*chan
,
478 unsigned long timeout
)
480 struct tegra_hsp_mailbox
*mb
= chan
->con_priv
;
481 struct tegra_hsp_channel
*ch
= &mb
->channel
;
484 timeout
= jiffies
+ msecs_to_jiffies(timeout
);
486 while (time_before(jiffies
, timeout
)) {
487 value
= tegra_hsp_channel_readl(ch
, HSP_SM_SHRD_MBOX
);
488 if ((value
& HSP_SM_SHRD_MBOX_FULL
) == 0) {
489 mbox_chan_txdone(chan
, 0);
491 /* Wait until channel is empty */
492 if (chan
->active_req
!= NULL
)
504 static int tegra_hsp_mailbox_startup(struct mbox_chan
*chan
)
506 struct tegra_hsp_mailbox
*mb
= chan
->con_priv
;
507 struct tegra_hsp_channel
*ch
= &mb
->channel
;
508 struct tegra_hsp
*hsp
= mb
->channel
.hsp
;
511 chan
->txdone_method
= TXDONE_BY_IRQ
;
514 * Shared mailboxes start out as consumers by default. FULL and EMPTY
515 * interrupts are coalesced at the same shared interrupt.
517 * Keep EMPTY interrupts disabled at startup and only enable them when
518 * the mailbox is actually full. This is required because the FULL and
519 * EMPTY interrupts are level-triggered, so keeping EMPTY interrupts
520 * enabled all the time would cause an interrupt storm while mailboxes
524 spin_lock_irqsave(&hsp
->lock
, flags
);
527 hsp
->mask
&= ~BIT(HSP_INT_EMPTY_SHIFT
+ mb
->index
);
529 hsp
->mask
|= BIT(HSP_INT_FULL_SHIFT
+ mb
->index
);
531 tegra_hsp_writel(hsp
, hsp
->mask
, HSP_INT_IE(hsp
->shared_irq
));
533 spin_unlock_irqrestore(&hsp
->lock
, flags
);
535 if (hsp
->soc
->has_per_mb_ie
) {
537 tegra_hsp_channel_writel(ch
, 0x0,
538 HSP_SM_SHRD_MBOX_EMPTY_INT_IE
);
540 tegra_hsp_channel_writel(ch
, 0x1,
541 HSP_SM_SHRD_MBOX_FULL_INT_IE
);
547 static void tegra_hsp_mailbox_shutdown(struct mbox_chan
*chan
)
549 struct tegra_hsp_mailbox
*mb
= chan
->con_priv
;
550 struct tegra_hsp_channel
*ch
= &mb
->channel
;
551 struct tegra_hsp
*hsp
= mb
->channel
.hsp
;
554 if (hsp
->soc
->has_per_mb_ie
) {
556 tegra_hsp_channel_writel(ch
, 0x0,
557 HSP_SM_SHRD_MBOX_EMPTY_INT_IE
);
559 tegra_hsp_channel_writel(ch
, 0x0,
560 HSP_SM_SHRD_MBOX_FULL_INT_IE
);
563 spin_lock_irqsave(&hsp
->lock
, flags
);
566 hsp
->mask
&= ~BIT(HSP_INT_EMPTY_SHIFT
+ mb
->index
);
568 hsp
->mask
&= ~BIT(HSP_INT_FULL_SHIFT
+ mb
->index
);
570 tegra_hsp_writel(hsp
, hsp
->mask
, HSP_INT_IE(hsp
->shared_irq
));
572 spin_unlock_irqrestore(&hsp
->lock
, flags
);
575 static const struct mbox_chan_ops tegra_hsp_sm_ops
= {
576 .send_data
= tegra_hsp_mailbox_send_data
,
577 .flush
= tegra_hsp_mailbox_flush
,
578 .startup
= tegra_hsp_mailbox_startup
,
579 .shutdown
= tegra_hsp_mailbox_shutdown
,
582 static struct mbox_chan
*tegra_hsp_db_xlate(struct mbox_controller
*mbox
,
583 const struct of_phandle_args
*args
)
585 struct tegra_hsp
*hsp
= container_of(mbox
, struct tegra_hsp
, mbox_db
);
586 unsigned int type
= args
->args
[0], master
= args
->args
[1];
587 struct tegra_hsp_channel
*channel
= ERR_PTR(-ENODEV
);
588 struct tegra_hsp_doorbell
*db
;
589 struct mbox_chan
*chan
;
593 if (type
!= TEGRA_HSP_MBOX_TYPE_DB
|| !hsp
->doorbell_irq
)
594 return ERR_PTR(-ENODEV
);
596 db
= tegra_hsp_doorbell_get(hsp
, master
);
598 channel
= &db
->channel
;
601 return ERR_CAST(channel
);
603 spin_lock_irqsave(&hsp
->lock
, flags
);
605 for (i
= 0; i
< mbox
->num_chans
; i
++) {
606 chan
= &mbox
->chans
[i
];
607 if (!chan
->con_priv
) {
608 channel
->chan
= chan
;
616 spin_unlock_irqrestore(&hsp
->lock
, flags
);
618 return chan
?: ERR_PTR(-EBUSY
);
621 static struct mbox_chan
*tegra_hsp_sm_xlate(struct mbox_controller
*mbox
,
622 const struct of_phandle_args
*args
)
624 struct tegra_hsp
*hsp
= container_of(mbox
, struct tegra_hsp
, mbox_sm
);
625 unsigned int type
= args
->args
[0], index
;
626 struct tegra_hsp_mailbox
*mb
;
628 index
= args
->args
[1] & TEGRA_HSP_SM_MASK
;
630 if ((type
& HSP_MBOX_TYPE_MASK
) != TEGRA_HSP_MBOX_TYPE_SM
||
631 !hsp
->shared_irqs
|| index
>= hsp
->num_sm
)
632 return ERR_PTR(-ENODEV
);
634 mb
= &hsp
->mailboxes
[index
];
636 if (type
& TEGRA_HSP_MBOX_TYPE_SM_128BIT
) {
637 if (!hsp
->soc
->has_128_bit_mb
)
638 return ERR_PTR(-ENODEV
);
640 mb
->ops
= &tegra_hsp_sm_128bit_ops
;
642 mb
->ops
= &tegra_hsp_sm_32bit_ops
;
645 if ((args
->args
[1] & TEGRA_HSP_SM_FLAG_TX
) == 0)
646 mb
->producer
= false;
650 return mb
->channel
.chan
;
653 static int tegra_hsp_add_doorbells(struct tegra_hsp
*hsp
)
655 const struct tegra_hsp_db_map
*map
= hsp
->soc
->map
;
656 struct tegra_hsp_channel
*channel
;
659 channel
= tegra_hsp_doorbell_create(hsp
, map
->name
,
660 map
->master
, map
->index
);
662 return PTR_ERR(channel
);
670 static int tegra_hsp_add_mailboxes(struct tegra_hsp
*hsp
, struct device
*dev
)
674 hsp
->mailboxes
= devm_kcalloc(dev
, hsp
->num_sm
, sizeof(*hsp
->mailboxes
),
679 for (i
= 0; i
< hsp
->num_sm
; i
++) {
680 struct tegra_hsp_mailbox
*mb
= &hsp
->mailboxes
[i
];
684 mb
->channel
.hsp
= hsp
;
685 mb
->channel
.regs
= hsp
->regs
+ SZ_64K
+ i
* SZ_32K
;
686 mb
->channel
.chan
= &hsp
->mbox_sm
.chans
[i
];
687 mb
->channel
.chan
->con_priv
= mb
;
693 static int tegra_hsp_request_shared_irq(struct tegra_hsp
*hsp
)
695 unsigned int i
, irq
= 0;
698 for (i
= 0; i
< hsp
->num_si
; i
++) {
699 irq
= hsp
->shared_irqs
[i
];
703 err
= devm_request_irq(hsp
->dev
, irq
, tegra_hsp_shared_irq
, 0,
704 dev_name(hsp
->dev
), hsp
);
706 dev_err(hsp
->dev
, "failed to request interrupt: %d\n",
713 /* disable all interrupts */
714 tegra_hsp_writel(hsp
, 0, HSP_INT_IE(hsp
->shared_irq
));
716 dev_dbg(hsp
->dev
, "interrupt requested: %u\n", irq
);
721 if (i
== hsp
->num_si
) {
722 dev_err(hsp
->dev
, "failed to find available interrupt\n");
729 static int tegra_hsp_probe(struct platform_device
*pdev
)
731 struct tegra_hsp
*hsp
;
736 hsp
= devm_kzalloc(&pdev
->dev
, sizeof(*hsp
), GFP_KERNEL
);
740 hsp
->dev
= &pdev
->dev
;
741 hsp
->soc
= of_device_get_match_data(&pdev
->dev
);
742 INIT_LIST_HEAD(&hsp
->doorbells
);
743 spin_lock_init(&hsp
->lock
);
745 hsp
->regs
= devm_platform_ioremap_resource(pdev
, 0);
746 if (IS_ERR(hsp
->regs
))
747 return PTR_ERR(hsp
->regs
);
749 value
= tegra_hsp_readl(hsp
, HSP_INT_DIMENSIONING
);
750 hsp
->num_sm
= (value
>> HSP_nSM_SHIFT
) & HSP_nINT_MASK
;
751 hsp
->num_ss
= (value
>> HSP_nSS_SHIFT
) & HSP_nINT_MASK
;
752 hsp
->num_as
= (value
>> HSP_nAS_SHIFT
) & HSP_nINT_MASK
;
753 hsp
->num_db
= (value
>> HSP_nDB_SHIFT
) & HSP_nINT_MASK
;
754 hsp
->num_si
= (value
>> HSP_nSI_SHIFT
) & HSP_nINT_MASK
;
756 err
= platform_get_irq_byname_optional(pdev
, "doorbell");
758 hsp
->doorbell_irq
= err
;
760 if (hsp
->num_si
> 0) {
761 unsigned int count
= 0;
763 hsp
->shared_irqs
= devm_kcalloc(&pdev
->dev
, hsp
->num_si
,
764 sizeof(*hsp
->shared_irqs
),
766 if (!hsp
->shared_irqs
)
769 for (i
= 0; i
< hsp
->num_si
; i
++) {
772 name
= kasprintf(GFP_KERNEL
, "shared%u", i
);
776 err
= platform_get_irq_byname_optional(pdev
, name
);
778 hsp
->shared_irqs
[i
] = err
;
786 devm_kfree(&pdev
->dev
, hsp
->shared_irqs
);
787 hsp
->shared_irqs
= NULL
;
791 /* setup the doorbell controller */
792 hsp
->mbox_db
.of_xlate
= tegra_hsp_db_xlate
;
793 hsp
->mbox_db
.num_chans
= 32;
794 hsp
->mbox_db
.dev
= &pdev
->dev
;
795 hsp
->mbox_db
.ops
= &tegra_hsp_db_ops
;
797 hsp
->mbox_db
.chans
= devm_kcalloc(&pdev
->dev
, hsp
->mbox_db
.num_chans
,
798 sizeof(*hsp
->mbox_db
.chans
),
800 if (!hsp
->mbox_db
.chans
)
803 if (hsp
->doorbell_irq
) {
804 err
= tegra_hsp_add_doorbells(hsp
);
806 dev_err(&pdev
->dev
, "failed to add doorbells: %d\n",
812 err
= devm_mbox_controller_register(&pdev
->dev
, &hsp
->mbox_db
);
814 dev_err(&pdev
->dev
, "failed to register doorbell mailbox: %d\n",
819 /* setup the shared mailbox controller */
820 hsp
->mbox_sm
.of_xlate
= tegra_hsp_sm_xlate
;
821 hsp
->mbox_sm
.num_chans
= hsp
->num_sm
;
822 hsp
->mbox_sm
.dev
= &pdev
->dev
;
823 hsp
->mbox_sm
.ops
= &tegra_hsp_sm_ops
;
825 hsp
->mbox_sm
.chans
= devm_kcalloc(&pdev
->dev
, hsp
->mbox_sm
.num_chans
,
826 sizeof(*hsp
->mbox_sm
.chans
),
828 if (!hsp
->mbox_sm
.chans
)
831 if (hsp
->shared_irqs
) {
832 err
= tegra_hsp_add_mailboxes(hsp
, &pdev
->dev
);
834 dev_err(&pdev
->dev
, "failed to add mailboxes: %d\n",
840 err
= devm_mbox_controller_register(&pdev
->dev
, &hsp
->mbox_sm
);
842 dev_err(&pdev
->dev
, "failed to register shared mailbox: %d\n",
847 platform_set_drvdata(pdev
, hsp
);
849 if (hsp
->doorbell_irq
) {
850 err
= devm_request_irq(&pdev
->dev
, hsp
->doorbell_irq
,
851 tegra_hsp_doorbell_irq
, IRQF_NO_SUSPEND
,
852 dev_name(&pdev
->dev
), hsp
);
855 "failed to request doorbell IRQ#%u: %d\n",
856 hsp
->doorbell_irq
, err
);
861 if (hsp
->shared_irqs
) {
862 err
= tegra_hsp_request_shared_irq(hsp
);
867 lockdep_register_key(&hsp
->lock_key
);
868 lockdep_set_class(&hsp
->lock
, &hsp
->lock_key
);
873 static void tegra_hsp_remove(struct platform_device
*pdev
)
875 struct tegra_hsp
*hsp
= platform_get_drvdata(pdev
);
877 lockdep_unregister_key(&hsp
->lock_key
);
880 static int __maybe_unused
tegra_hsp_resume(struct device
*dev
)
882 struct tegra_hsp
*hsp
= dev_get_drvdata(dev
);
884 struct tegra_hsp_doorbell
*db
;
886 list_for_each_entry(db
, &hsp
->doorbells
, list
) {
887 if (db
->channel
.chan
)
888 tegra_hsp_doorbell_startup(db
->channel
.chan
);
891 if (hsp
->mailboxes
) {
892 for (i
= 0; i
< hsp
->num_sm
; i
++) {
893 struct tegra_hsp_mailbox
*mb
= &hsp
->mailboxes
[i
];
895 if (mb
->channel
.chan
->cl
)
896 tegra_hsp_mailbox_startup(mb
->channel
.chan
);
903 static const struct dev_pm_ops tegra_hsp_pm_ops
= {
904 .resume_noirq
= tegra_hsp_resume
,
907 static const struct tegra_hsp_db_map tegra186_hsp_db_map
[] = {
908 { "ccplex", TEGRA_HSP_DB_MASTER_CCPLEX
, HSP_DB_CCPLEX
, },
909 { "bpmp", TEGRA_HSP_DB_MASTER_BPMP
, HSP_DB_BPMP
, },
913 static const struct tegra_hsp_soc tegra186_hsp_soc
= {
914 .map
= tegra186_hsp_db_map
,
915 .has_per_mb_ie
= false,
916 .has_128_bit_mb
= false,
920 static const struct tegra_hsp_soc tegra194_hsp_soc
= {
921 .map
= tegra186_hsp_db_map
,
922 .has_per_mb_ie
= true,
923 .has_128_bit_mb
= false,
927 static const struct tegra_hsp_soc tegra234_hsp_soc
= {
928 .map
= tegra186_hsp_db_map
,
929 .has_per_mb_ie
= false,
930 .has_128_bit_mb
= true,
934 static const struct tegra_hsp_soc tegra264_hsp_soc
= {
935 .map
= tegra186_hsp_db_map
,
936 .has_per_mb_ie
= false,
937 .has_128_bit_mb
= true,
938 .reg_stride
= 0x1000,
941 static const struct of_device_id tegra_hsp_match
[] = {
942 { .compatible
= "nvidia,tegra186-hsp", .data
= &tegra186_hsp_soc
},
943 { .compatible
= "nvidia,tegra194-hsp", .data
= &tegra194_hsp_soc
},
944 { .compatible
= "nvidia,tegra234-hsp", .data
= &tegra234_hsp_soc
},
945 { .compatible
= "nvidia,tegra264-hsp", .data
= &tegra264_hsp_soc
},
949 static struct platform_driver tegra_hsp_driver
= {
952 .of_match_table
= tegra_hsp_match
,
953 .pm
= &tegra_hsp_pm_ops
,
955 .probe
= tegra_hsp_probe
,
956 .remove
= tegra_hsp_remove
,
959 static int __init
tegra_hsp_init(void)
961 return platform_driver_register(&tegra_hsp_driver
);
963 core_initcall(tegra_hsp_init
);