Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * Copyright (c) 2016-2018, NVIDIA CORPORATION.  All rights reserved.
0004  */
0005 
0006 #include <linux/delay.h>
0007 #include <linux/interrupt.h>
0008 #include <linux/io.h>
0009 #include <linux/mailbox_controller.h>
0010 #include <linux/of.h>
0011 #include <linux/of_device.h>
0012 #include <linux/platform_device.h>
0013 #include <linux/pm.h>
0014 #include <linux/slab.h>
0015 
0016 #include <soc/tegra/fuse.h>
0017 
0018 #include <dt-bindings/mailbox/tegra186-hsp.h>
0019 
0020 #include "mailbox.h"
0021 
0022 #define HSP_INT_IE(x)       (0x100 + ((x) * 4))
0023 #define HSP_INT_IV      0x300
0024 #define HSP_INT_IR      0x304
0025 
0026 #define HSP_INT_EMPTY_SHIFT 0
0027 #define HSP_INT_EMPTY_MASK  0xff
0028 #define HSP_INT_FULL_SHIFT  8
0029 #define HSP_INT_FULL_MASK   0xff
0030 
0031 #define HSP_INT_DIMENSIONING    0x380
0032 #define HSP_nSM_SHIFT       0
0033 #define HSP_nSS_SHIFT       4
0034 #define HSP_nAS_SHIFT       8
0035 #define HSP_nDB_SHIFT       12
0036 #define HSP_nSI_SHIFT       16
0037 #define HSP_nINT_MASK       0xf
0038 
0039 #define HSP_DB_TRIGGER  0x0
0040 #define HSP_DB_ENABLE   0x4
0041 #define HSP_DB_RAW  0x8
0042 #define HSP_DB_PENDING  0xc
0043 
0044 #define HSP_SM_SHRD_MBOX    0x0
0045 #define HSP_SM_SHRD_MBOX_FULL   BIT(31)
0046 #define HSP_SM_SHRD_MBOX_FULL_INT_IE    0x04
0047 #define HSP_SM_SHRD_MBOX_EMPTY_INT_IE   0x08
0048 
0049 #define HSP_SHRD_MBOX_TYPE1_TAG     0x40
0050 #define HSP_SHRD_MBOX_TYPE1_DATA0   0x48
0051 #define HSP_SHRD_MBOX_TYPE1_DATA1   0x4c
0052 #define HSP_SHRD_MBOX_TYPE1_DATA2   0x50
0053 #define HSP_SHRD_MBOX_TYPE1_DATA3   0x54
0054 
0055 #define HSP_DB_CCPLEX       1
0056 #define HSP_DB_BPMP     3
0057 #define HSP_DB_MAX      7
0058 
0059 #define HSP_MBOX_TYPE_MASK  0xff
0060 
0061 struct tegra_hsp_channel;
0062 struct tegra_hsp;
0063 
0064 struct tegra_hsp_channel {
0065     struct tegra_hsp *hsp;
0066     struct mbox_chan *chan;
0067     void __iomem *regs;
0068 };
0069 
0070 struct tegra_hsp_doorbell {
0071     struct tegra_hsp_channel channel;
0072     struct list_head list;
0073     const char *name;
0074     unsigned int master;
0075     unsigned int index;
0076 };
0077 
0078 struct tegra_hsp_sm_ops {
0079     void (*send)(struct tegra_hsp_channel *channel, void *data);
0080     void (*recv)(struct tegra_hsp_channel *channel);
0081 };
0082 
0083 struct tegra_hsp_mailbox {
0084     struct tegra_hsp_channel channel;
0085     const struct tegra_hsp_sm_ops *ops;
0086     unsigned int index;
0087     bool producer;
0088 };
0089 
0090 struct tegra_hsp_db_map {
0091     const char *name;
0092     unsigned int master;
0093     unsigned int index;
0094 };
0095 
0096 struct tegra_hsp_soc {
0097     const struct tegra_hsp_db_map *map;
0098     bool has_per_mb_ie;
0099     bool has_128_bit_mb;
0100 };
0101 
0102 struct tegra_hsp {
0103     struct device *dev;
0104     const struct tegra_hsp_soc *soc;
0105     struct mbox_controller mbox_db;
0106     struct mbox_controller mbox_sm;
0107     void __iomem *regs;
0108     unsigned int doorbell_irq;
0109     unsigned int *shared_irqs;
0110     unsigned int shared_irq;
0111     unsigned int num_sm;
0112     unsigned int num_as;
0113     unsigned int num_ss;
0114     unsigned int num_db;
0115     unsigned int num_si;
0116 
0117     spinlock_t lock;
0118     struct lock_class_key lock_key;
0119 
0120     struct list_head doorbells;
0121     struct tegra_hsp_mailbox *mailboxes;
0122 
0123     unsigned long mask;
0124 };
0125 
0126 static inline u32 tegra_hsp_readl(struct tegra_hsp *hsp, unsigned int offset)
0127 {
0128     return readl(hsp->regs + offset);
0129 }
0130 
0131 static inline void tegra_hsp_writel(struct tegra_hsp *hsp, u32 value,
0132                     unsigned int offset)
0133 {
0134     writel(value, hsp->regs + offset);
0135 }
0136 
0137 static inline u32 tegra_hsp_channel_readl(struct tegra_hsp_channel *channel,
0138                       unsigned int offset)
0139 {
0140     return readl(channel->regs + offset);
0141 }
0142 
0143 static inline void tegra_hsp_channel_writel(struct tegra_hsp_channel *channel,
0144                         u32 value, unsigned int offset)
0145 {
0146     writel(value, channel->regs + offset);
0147 }
0148 
0149 static bool tegra_hsp_doorbell_can_ring(struct tegra_hsp_doorbell *db)
0150 {
0151     u32 value;
0152 
0153     value = tegra_hsp_channel_readl(&db->channel, HSP_DB_ENABLE);
0154 
0155     return (value & BIT(TEGRA_HSP_DB_MASTER_CCPLEX)) != 0;
0156 }
0157 
0158 static struct tegra_hsp_doorbell *
0159 __tegra_hsp_doorbell_get(struct tegra_hsp *hsp, unsigned int master)
0160 {
0161     struct tegra_hsp_doorbell *entry;
0162 
0163     list_for_each_entry(entry, &hsp->doorbells, list)
0164         if (entry->master == master)
0165             return entry;
0166 
0167     return NULL;
0168 }
0169 
0170 static struct tegra_hsp_doorbell *
0171 tegra_hsp_doorbell_get(struct tegra_hsp *hsp, unsigned int master)
0172 {
0173     struct tegra_hsp_doorbell *db;
0174     unsigned long flags;
0175 
0176     spin_lock_irqsave(&hsp->lock, flags);
0177     db = __tegra_hsp_doorbell_get(hsp, master);
0178     spin_unlock_irqrestore(&hsp->lock, flags);
0179 
0180     return db;
0181 }
0182 
0183 static irqreturn_t tegra_hsp_doorbell_irq(int irq, void *data)
0184 {
0185     struct tegra_hsp *hsp = data;
0186     struct tegra_hsp_doorbell *db;
0187     unsigned long master, value;
0188 
0189     db = tegra_hsp_doorbell_get(hsp, TEGRA_HSP_DB_MASTER_CCPLEX);
0190     if (!db)
0191         return IRQ_NONE;
0192 
0193     value = tegra_hsp_channel_readl(&db->channel, HSP_DB_PENDING);
0194     tegra_hsp_channel_writel(&db->channel, value, HSP_DB_PENDING);
0195 
0196     spin_lock(&hsp->lock);
0197 
0198     for_each_set_bit(master, &value, hsp->mbox_db.num_chans) {
0199         struct tegra_hsp_doorbell *db;
0200 
0201         db = __tegra_hsp_doorbell_get(hsp, master);
0202         /*
0203          * Depending on the bootloader chain, the CCPLEX doorbell will
0204          * have some doorbells enabled, which means that requesting an
0205          * interrupt will immediately fire.
0206          *
0207          * In that case, db->channel.chan will still be NULL here and
0208          * cause a crash if not properly guarded.
0209          *
0210          * It remains to be seen if ignoring the doorbell in that case
0211          * is the correct solution.
0212          */
0213         if (db && db->channel.chan)
0214             mbox_chan_received_data(db->channel.chan, NULL);
0215     }
0216 
0217     spin_unlock(&hsp->lock);
0218 
0219     return IRQ_HANDLED;
0220 }
0221 
0222 static irqreturn_t tegra_hsp_shared_irq(int irq, void *data)
0223 {
0224     struct tegra_hsp *hsp = data;
0225     unsigned long bit, mask;
0226     u32 status;
0227 
0228     status = tegra_hsp_readl(hsp, HSP_INT_IR) & hsp->mask;
0229 
0230     /* process EMPTY interrupts first */
0231     mask = (status >> HSP_INT_EMPTY_SHIFT) & HSP_INT_EMPTY_MASK;
0232 
0233     for_each_set_bit(bit, &mask, hsp->num_sm) {
0234         struct tegra_hsp_mailbox *mb = &hsp->mailboxes[bit];
0235 
0236         if (mb->producer) {
0237             /*
0238              * Disable EMPTY interrupts until data is sent with
0239              * the next message. These interrupts are level-
0240              * triggered, so if we kept them enabled they would
0241              * constantly trigger until we next write data into
0242              * the message.
0243              */
0244             spin_lock(&hsp->lock);
0245 
0246             hsp->mask &= ~BIT(HSP_INT_EMPTY_SHIFT + mb->index);
0247             tegra_hsp_writel(hsp, hsp->mask,
0248                      HSP_INT_IE(hsp->shared_irq));
0249 
0250             spin_unlock(&hsp->lock);
0251 
0252             mbox_chan_txdone(mb->channel.chan, 0);
0253         }
0254     }
0255 
0256     /* process FULL interrupts */
0257     mask = (status >> HSP_INT_FULL_SHIFT) & HSP_INT_FULL_MASK;
0258 
0259     for_each_set_bit(bit, &mask, hsp->num_sm) {
0260         struct tegra_hsp_mailbox *mb = &hsp->mailboxes[bit];
0261 
0262         if (!mb->producer)
0263             mb->ops->recv(&mb->channel);
0264     }
0265 
0266     return IRQ_HANDLED;
0267 }
0268 
0269 static struct tegra_hsp_channel *
0270 tegra_hsp_doorbell_create(struct tegra_hsp *hsp, const char *name,
0271               unsigned int master, unsigned int index)
0272 {
0273     struct tegra_hsp_doorbell *db;
0274     unsigned int offset;
0275     unsigned long flags;
0276 
0277     db = devm_kzalloc(hsp->dev, sizeof(*db), GFP_KERNEL);
0278     if (!db)
0279         return ERR_PTR(-ENOMEM);
0280 
0281     offset = (1 + (hsp->num_sm / 2) + hsp->num_ss + hsp->num_as) * SZ_64K;
0282     offset += index * 0x100;
0283 
0284     db->channel.regs = hsp->regs + offset;
0285     db->channel.hsp = hsp;
0286 
0287     db->name = devm_kstrdup_const(hsp->dev, name, GFP_KERNEL);
0288     db->master = master;
0289     db->index = index;
0290 
0291     spin_lock_irqsave(&hsp->lock, flags);
0292     list_add_tail(&db->list, &hsp->doorbells);
0293     spin_unlock_irqrestore(&hsp->lock, flags);
0294 
0295     return &db->channel;
0296 }
0297 
0298 static int tegra_hsp_doorbell_send_data(struct mbox_chan *chan, void *data)
0299 {
0300     struct tegra_hsp_doorbell *db = chan->con_priv;
0301 
0302     tegra_hsp_channel_writel(&db->channel, 1, HSP_DB_TRIGGER);
0303 
0304     return 0;
0305 }
0306 
0307 static int tegra_hsp_doorbell_startup(struct mbox_chan *chan)
0308 {
0309     struct tegra_hsp_doorbell *db = chan->con_priv;
0310     struct tegra_hsp *hsp = db->channel.hsp;
0311     struct tegra_hsp_doorbell *ccplex;
0312     unsigned long flags;
0313     u32 value;
0314 
0315     if (db->master >= chan->mbox->num_chans) {
0316         dev_err(chan->mbox->dev,
0317             "invalid master ID %u for HSP channel\n",
0318             db->master);
0319         return -EINVAL;
0320     }
0321 
0322     ccplex = tegra_hsp_doorbell_get(hsp, TEGRA_HSP_DB_MASTER_CCPLEX);
0323     if (!ccplex)
0324         return -ENODEV;
0325 
0326     /*
0327      * On simulation platforms the BPMP hasn't had a chance yet to mark
0328      * the doorbell as ringable by the CCPLEX, so we want to skip extra
0329      * checks here.
0330      */
0331     if (tegra_is_silicon() && !tegra_hsp_doorbell_can_ring(db))
0332         return -ENODEV;
0333 
0334     spin_lock_irqsave(&hsp->lock, flags);
0335 
0336     value = tegra_hsp_channel_readl(&ccplex->channel, HSP_DB_ENABLE);
0337     value |= BIT(db->master);
0338     tegra_hsp_channel_writel(&ccplex->channel, value, HSP_DB_ENABLE);
0339 
0340     spin_unlock_irqrestore(&hsp->lock, flags);
0341 
0342     return 0;
0343 }
0344 
0345 static void tegra_hsp_doorbell_shutdown(struct mbox_chan *chan)
0346 {
0347     struct tegra_hsp_doorbell *db = chan->con_priv;
0348     struct tegra_hsp *hsp = db->channel.hsp;
0349     struct tegra_hsp_doorbell *ccplex;
0350     unsigned long flags;
0351     u32 value;
0352 
0353     ccplex = tegra_hsp_doorbell_get(hsp, TEGRA_HSP_DB_MASTER_CCPLEX);
0354     if (!ccplex)
0355         return;
0356 
0357     spin_lock_irqsave(&hsp->lock, flags);
0358 
0359     value = tegra_hsp_channel_readl(&ccplex->channel, HSP_DB_ENABLE);
0360     value &= ~BIT(db->master);
0361     tegra_hsp_channel_writel(&ccplex->channel, value, HSP_DB_ENABLE);
0362 
0363     spin_unlock_irqrestore(&hsp->lock, flags);
0364 }
0365 
0366 static const struct mbox_chan_ops tegra_hsp_db_ops = {
0367     .send_data = tegra_hsp_doorbell_send_data,
0368     .startup = tegra_hsp_doorbell_startup,
0369     .shutdown = tegra_hsp_doorbell_shutdown,
0370 };
0371 
0372 static void tegra_hsp_sm_send32(struct tegra_hsp_channel *channel, void *data)
0373 {
0374     u32 value;
0375 
0376     /* copy data and mark mailbox full */
0377     value = (u32)(unsigned long)data;
0378     value |= HSP_SM_SHRD_MBOX_FULL;
0379 
0380     tegra_hsp_channel_writel(channel, value, HSP_SM_SHRD_MBOX);
0381 }
0382 
0383 static void tegra_hsp_sm_recv32(struct tegra_hsp_channel *channel)
0384 {
0385     u32 value;
0386     void *msg;
0387 
0388     value = tegra_hsp_channel_readl(channel, HSP_SM_SHRD_MBOX);
0389     value &= ~HSP_SM_SHRD_MBOX_FULL;
0390     msg = (void *)(unsigned long)value;
0391     mbox_chan_received_data(channel->chan, msg);
0392 
0393     /*
0394      * Need to clear all bits here since some producers, such as TCU, depend
0395      * on fields in the register getting cleared by the consumer.
0396      *
0397      * The mailbox API doesn't give the consumers a way of doing that
0398      * explicitly, so we have to make sure we cover all possible cases.
0399      */
0400     tegra_hsp_channel_writel(channel, 0x0, HSP_SM_SHRD_MBOX);
0401 }
0402 
0403 static const struct tegra_hsp_sm_ops tegra_hsp_sm_32bit_ops = {
0404     .send = tegra_hsp_sm_send32,
0405     .recv = tegra_hsp_sm_recv32,
0406 };
0407 
0408 static void tegra_hsp_sm_send128(struct tegra_hsp_channel *channel, void *data)
0409 {
0410     u32 value[4];
0411 
0412     memcpy(value, data, sizeof(value));
0413 
0414     /* Copy data */
0415     tegra_hsp_channel_writel(channel, value[0], HSP_SHRD_MBOX_TYPE1_DATA0);
0416     tegra_hsp_channel_writel(channel, value[1], HSP_SHRD_MBOX_TYPE1_DATA1);
0417     tegra_hsp_channel_writel(channel, value[2], HSP_SHRD_MBOX_TYPE1_DATA2);
0418     tegra_hsp_channel_writel(channel, value[3], HSP_SHRD_MBOX_TYPE1_DATA3);
0419 
0420     /* Update tag to mark mailbox full */
0421     tegra_hsp_channel_writel(channel, HSP_SM_SHRD_MBOX_FULL,
0422                  HSP_SHRD_MBOX_TYPE1_TAG);
0423 }
0424 
0425 static void tegra_hsp_sm_recv128(struct tegra_hsp_channel *channel)
0426 {
0427     u32 value[4];
0428     void *msg;
0429 
0430     value[0] = tegra_hsp_channel_readl(channel, HSP_SHRD_MBOX_TYPE1_DATA0);
0431     value[1] = tegra_hsp_channel_readl(channel, HSP_SHRD_MBOX_TYPE1_DATA1);
0432     value[2] = tegra_hsp_channel_readl(channel, HSP_SHRD_MBOX_TYPE1_DATA2);
0433     value[3] = tegra_hsp_channel_readl(channel, HSP_SHRD_MBOX_TYPE1_DATA3);
0434 
0435     msg = (void *)(unsigned long)value;
0436     mbox_chan_received_data(channel->chan, msg);
0437 
0438     /*
0439      * Clear data registers and tag.
0440      */
0441     tegra_hsp_channel_writel(channel, 0x0, HSP_SHRD_MBOX_TYPE1_DATA0);
0442     tegra_hsp_channel_writel(channel, 0x0, HSP_SHRD_MBOX_TYPE1_DATA1);
0443     tegra_hsp_channel_writel(channel, 0x0, HSP_SHRD_MBOX_TYPE1_DATA2);
0444     tegra_hsp_channel_writel(channel, 0x0, HSP_SHRD_MBOX_TYPE1_DATA3);
0445     tegra_hsp_channel_writel(channel, 0x0, HSP_SHRD_MBOX_TYPE1_TAG);
0446 }
0447 
0448 static const struct tegra_hsp_sm_ops tegra_hsp_sm_128bit_ops = {
0449     .send = tegra_hsp_sm_send128,
0450     .recv = tegra_hsp_sm_recv128,
0451 };
0452 
0453 static int tegra_hsp_mailbox_send_data(struct mbox_chan *chan, void *data)
0454 {
0455     struct tegra_hsp_mailbox *mb = chan->con_priv;
0456     struct tegra_hsp *hsp = mb->channel.hsp;
0457     unsigned long flags;
0458 
0459     if (WARN_ON(!mb->producer))
0460         return -EPERM;
0461 
0462     mb->ops->send(&mb->channel, data);
0463 
0464     /* enable EMPTY interrupt for the shared mailbox */
0465     spin_lock_irqsave(&hsp->lock, flags);
0466 
0467     hsp->mask |= BIT(HSP_INT_EMPTY_SHIFT + mb->index);
0468     tegra_hsp_writel(hsp, hsp->mask, HSP_INT_IE(hsp->shared_irq));
0469 
0470     spin_unlock_irqrestore(&hsp->lock, flags);
0471 
0472     return 0;
0473 }
0474 
0475 static int tegra_hsp_mailbox_flush(struct mbox_chan *chan,
0476                    unsigned long timeout)
0477 {
0478     struct tegra_hsp_mailbox *mb = chan->con_priv;
0479     struct tegra_hsp_channel *ch = &mb->channel;
0480     u32 value;
0481 
0482     timeout = jiffies + msecs_to_jiffies(timeout);
0483 
0484     while (time_before(jiffies, timeout)) {
0485         value = tegra_hsp_channel_readl(ch, HSP_SM_SHRD_MBOX);
0486         if ((value & HSP_SM_SHRD_MBOX_FULL) == 0) {
0487             mbox_chan_txdone(chan, 0);
0488 
0489             /* Wait until channel is empty */
0490             if (chan->active_req != NULL)
0491                 continue;
0492 
0493             return 0;
0494         }
0495 
0496         udelay(1);
0497     }
0498 
0499     return -ETIME;
0500 }
0501 
0502 static int tegra_hsp_mailbox_startup(struct mbox_chan *chan)
0503 {
0504     struct tegra_hsp_mailbox *mb = chan->con_priv;
0505     struct tegra_hsp_channel *ch = &mb->channel;
0506     struct tegra_hsp *hsp = mb->channel.hsp;
0507     unsigned long flags;
0508 
0509     chan->txdone_method = TXDONE_BY_IRQ;
0510 
0511     /*
0512      * Shared mailboxes start out as consumers by default. FULL and EMPTY
0513      * interrupts are coalesced at the same shared interrupt.
0514      *
0515      * Keep EMPTY interrupts disabled at startup and only enable them when
0516      * the mailbox is actually full. This is required because the FULL and
0517      * EMPTY interrupts are level-triggered, so keeping EMPTY interrupts
0518      * enabled all the time would cause an interrupt storm while mailboxes
0519      * are idle.
0520      */
0521 
0522     spin_lock_irqsave(&hsp->lock, flags);
0523 
0524     if (mb->producer)
0525         hsp->mask &= ~BIT(HSP_INT_EMPTY_SHIFT + mb->index);
0526     else
0527         hsp->mask |= BIT(HSP_INT_FULL_SHIFT + mb->index);
0528 
0529     tegra_hsp_writel(hsp, hsp->mask, HSP_INT_IE(hsp->shared_irq));
0530 
0531     spin_unlock_irqrestore(&hsp->lock, flags);
0532 
0533     if (hsp->soc->has_per_mb_ie) {
0534         if (mb->producer)
0535             tegra_hsp_channel_writel(ch, 0x0,
0536                          HSP_SM_SHRD_MBOX_EMPTY_INT_IE);
0537         else
0538             tegra_hsp_channel_writel(ch, 0x1,
0539                          HSP_SM_SHRD_MBOX_FULL_INT_IE);
0540     }
0541 
0542     return 0;
0543 }
0544 
0545 static void tegra_hsp_mailbox_shutdown(struct mbox_chan *chan)
0546 {
0547     struct tegra_hsp_mailbox *mb = chan->con_priv;
0548     struct tegra_hsp_channel *ch = &mb->channel;
0549     struct tegra_hsp *hsp = mb->channel.hsp;
0550     unsigned long flags;
0551 
0552     if (hsp->soc->has_per_mb_ie) {
0553         if (mb->producer)
0554             tegra_hsp_channel_writel(ch, 0x0,
0555                          HSP_SM_SHRD_MBOX_EMPTY_INT_IE);
0556         else
0557             tegra_hsp_channel_writel(ch, 0x0,
0558                          HSP_SM_SHRD_MBOX_FULL_INT_IE);
0559     }
0560 
0561     spin_lock_irqsave(&hsp->lock, flags);
0562 
0563     if (mb->producer)
0564         hsp->mask &= ~BIT(HSP_INT_EMPTY_SHIFT + mb->index);
0565     else
0566         hsp->mask &= ~BIT(HSP_INT_FULL_SHIFT + mb->index);
0567 
0568     tegra_hsp_writel(hsp, hsp->mask, HSP_INT_IE(hsp->shared_irq));
0569 
0570     spin_unlock_irqrestore(&hsp->lock, flags);
0571 }
0572 
0573 static const struct mbox_chan_ops tegra_hsp_sm_ops = {
0574     .send_data = tegra_hsp_mailbox_send_data,
0575     .flush = tegra_hsp_mailbox_flush,
0576     .startup = tegra_hsp_mailbox_startup,
0577     .shutdown = tegra_hsp_mailbox_shutdown,
0578 };
0579 
0580 static struct mbox_chan *tegra_hsp_db_xlate(struct mbox_controller *mbox,
0581                         const struct of_phandle_args *args)
0582 {
0583     struct tegra_hsp *hsp = container_of(mbox, struct tegra_hsp, mbox_db);
0584     unsigned int type = args->args[0], master = args->args[1];
0585     struct tegra_hsp_channel *channel = ERR_PTR(-ENODEV);
0586     struct tegra_hsp_doorbell *db;
0587     struct mbox_chan *chan;
0588     unsigned long flags;
0589     unsigned int i;
0590 
0591     if (type != TEGRA_HSP_MBOX_TYPE_DB || !hsp->doorbell_irq)
0592         return ERR_PTR(-ENODEV);
0593 
0594     db = tegra_hsp_doorbell_get(hsp, master);
0595     if (db)
0596         channel = &db->channel;
0597 
0598     if (IS_ERR(channel))
0599         return ERR_CAST(channel);
0600 
0601     spin_lock_irqsave(&hsp->lock, flags);
0602 
0603     for (i = 0; i < mbox->num_chans; i++) {
0604         chan = &mbox->chans[i];
0605         if (!chan->con_priv) {
0606             channel->chan = chan;
0607             chan->con_priv = db;
0608             break;
0609         }
0610 
0611         chan = NULL;
0612     }
0613 
0614     spin_unlock_irqrestore(&hsp->lock, flags);
0615 
0616     return chan ?: ERR_PTR(-EBUSY);
0617 }
0618 
0619 static struct mbox_chan *tegra_hsp_sm_xlate(struct mbox_controller *mbox,
0620                         const struct of_phandle_args *args)
0621 {
0622     struct tegra_hsp *hsp = container_of(mbox, struct tegra_hsp, mbox_sm);
0623     unsigned int type = args->args[0], index;
0624     struct tegra_hsp_mailbox *mb;
0625 
0626     index = args->args[1] & TEGRA_HSP_SM_MASK;
0627 
0628     if ((type & HSP_MBOX_TYPE_MASK) != TEGRA_HSP_MBOX_TYPE_SM ||
0629         !hsp->shared_irqs || index >= hsp->num_sm)
0630         return ERR_PTR(-ENODEV);
0631 
0632     mb = &hsp->mailboxes[index];
0633 
0634     if (type & TEGRA_HSP_MBOX_TYPE_SM_128BIT) {
0635         if (!hsp->soc->has_128_bit_mb)
0636             return ERR_PTR(-ENODEV);
0637 
0638         mb->ops = &tegra_hsp_sm_128bit_ops;
0639     } else {
0640         mb->ops = &tegra_hsp_sm_32bit_ops;
0641     }
0642 
0643     if ((args->args[1] & TEGRA_HSP_SM_FLAG_TX) == 0)
0644         mb->producer = false;
0645     else
0646         mb->producer = true;
0647 
0648     return mb->channel.chan;
0649 }
0650 
0651 static int tegra_hsp_add_doorbells(struct tegra_hsp *hsp)
0652 {
0653     const struct tegra_hsp_db_map *map = hsp->soc->map;
0654     struct tegra_hsp_channel *channel;
0655 
0656     while (map->name) {
0657         channel = tegra_hsp_doorbell_create(hsp, map->name,
0658                             map->master, map->index);
0659         if (IS_ERR(channel))
0660             return PTR_ERR(channel);
0661 
0662         map++;
0663     }
0664 
0665     return 0;
0666 }
0667 
0668 static int tegra_hsp_add_mailboxes(struct tegra_hsp *hsp, struct device *dev)
0669 {
0670     int i;
0671 
0672     hsp->mailboxes = devm_kcalloc(dev, hsp->num_sm, sizeof(*hsp->mailboxes),
0673                       GFP_KERNEL);
0674     if (!hsp->mailboxes)
0675         return -ENOMEM;
0676 
0677     for (i = 0; i < hsp->num_sm; i++) {
0678         struct tegra_hsp_mailbox *mb = &hsp->mailboxes[i];
0679 
0680         mb->index = i;
0681 
0682         mb->channel.hsp = hsp;
0683         mb->channel.regs = hsp->regs + SZ_64K + i * SZ_32K;
0684         mb->channel.chan = &hsp->mbox_sm.chans[i];
0685         mb->channel.chan->con_priv = mb;
0686     }
0687 
0688     return 0;
0689 }
0690 
0691 static int tegra_hsp_request_shared_irq(struct tegra_hsp *hsp)
0692 {
0693     unsigned int i, irq = 0;
0694     int err;
0695 
0696     for (i = 0; i < hsp->num_si; i++) {
0697         irq = hsp->shared_irqs[i];
0698         if (irq <= 0)
0699             continue;
0700 
0701         err = devm_request_irq(hsp->dev, irq, tegra_hsp_shared_irq, 0,
0702                        dev_name(hsp->dev), hsp);
0703         if (err < 0) {
0704             dev_err(hsp->dev, "failed to request interrupt: %d\n",
0705                 err);
0706             continue;
0707         }
0708 
0709         hsp->shared_irq = i;
0710 
0711         /* disable all interrupts */
0712         tegra_hsp_writel(hsp, 0, HSP_INT_IE(hsp->shared_irq));
0713 
0714         dev_dbg(hsp->dev, "interrupt requested: %u\n", irq);
0715 
0716         break;
0717     }
0718 
0719     if (i == hsp->num_si) {
0720         dev_err(hsp->dev, "failed to find available interrupt\n");
0721         return -ENOENT;
0722     }
0723 
0724     return 0;
0725 }
0726 
0727 static int tegra_hsp_probe(struct platform_device *pdev)
0728 {
0729     struct tegra_hsp *hsp;
0730     struct resource *res;
0731     unsigned int i;
0732     u32 value;
0733     int err;
0734 
0735     hsp = devm_kzalloc(&pdev->dev, sizeof(*hsp), GFP_KERNEL);
0736     if (!hsp)
0737         return -ENOMEM;
0738 
0739     hsp->dev = &pdev->dev;
0740     hsp->soc = of_device_get_match_data(&pdev->dev);
0741     INIT_LIST_HEAD(&hsp->doorbells);
0742     spin_lock_init(&hsp->lock);
0743 
0744     res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
0745     hsp->regs = devm_ioremap_resource(&pdev->dev, res);
0746     if (IS_ERR(hsp->regs))
0747         return PTR_ERR(hsp->regs);
0748 
0749     value = tegra_hsp_readl(hsp, HSP_INT_DIMENSIONING);
0750     hsp->num_sm = (value >> HSP_nSM_SHIFT) & HSP_nINT_MASK;
0751     hsp->num_ss = (value >> HSP_nSS_SHIFT) & HSP_nINT_MASK;
0752     hsp->num_as = (value >> HSP_nAS_SHIFT) & HSP_nINT_MASK;
0753     hsp->num_db = (value >> HSP_nDB_SHIFT) & HSP_nINT_MASK;
0754     hsp->num_si = (value >> HSP_nSI_SHIFT) & HSP_nINT_MASK;
0755 
0756     err = platform_get_irq_byname_optional(pdev, "doorbell");
0757     if (err >= 0)
0758         hsp->doorbell_irq = err;
0759 
0760     if (hsp->num_si > 0) {
0761         unsigned int count = 0;
0762 
0763         hsp->shared_irqs = devm_kcalloc(&pdev->dev, hsp->num_si,
0764                         sizeof(*hsp->shared_irqs),
0765                         GFP_KERNEL);
0766         if (!hsp->shared_irqs)
0767             return -ENOMEM;
0768 
0769         for (i = 0; i < hsp->num_si; i++) {
0770             char *name;
0771 
0772             name = kasprintf(GFP_KERNEL, "shared%u", i);
0773             if (!name)
0774                 return -ENOMEM;
0775 
0776             err = platform_get_irq_byname_optional(pdev, name);
0777             if (err >= 0) {
0778                 hsp->shared_irqs[i] = err;
0779                 count++;
0780             }
0781 
0782             kfree(name);
0783         }
0784 
0785         if (count == 0) {
0786             devm_kfree(&pdev->dev, hsp->shared_irqs);
0787             hsp->shared_irqs = NULL;
0788         }
0789     }
0790 
0791     /* setup the doorbell controller */
0792     hsp->mbox_db.of_xlate = tegra_hsp_db_xlate;
0793     hsp->mbox_db.num_chans = 32;
0794     hsp->mbox_db.dev = &pdev->dev;
0795     hsp->mbox_db.ops = &tegra_hsp_db_ops;
0796 
0797     hsp->mbox_db.chans = devm_kcalloc(&pdev->dev, hsp->mbox_db.num_chans,
0798                       sizeof(*hsp->mbox_db.chans),
0799                       GFP_KERNEL);
0800     if (!hsp->mbox_db.chans)
0801         return -ENOMEM;
0802 
0803     if (hsp->doorbell_irq) {
0804         err = tegra_hsp_add_doorbells(hsp);
0805         if (err < 0) {
0806             dev_err(&pdev->dev, "failed to add doorbells: %d\n",
0807                     err);
0808             return err;
0809         }
0810     }
0811 
0812     err = devm_mbox_controller_register(&pdev->dev, &hsp->mbox_db);
0813     if (err < 0) {
0814         dev_err(&pdev->dev, "failed to register doorbell mailbox: %d\n",
0815             err);
0816         return err;
0817     }
0818 
0819     /* setup the shared mailbox controller */
0820     hsp->mbox_sm.of_xlate = tegra_hsp_sm_xlate;
0821     hsp->mbox_sm.num_chans = hsp->num_sm;
0822     hsp->mbox_sm.dev = &pdev->dev;
0823     hsp->mbox_sm.ops = &tegra_hsp_sm_ops;
0824 
0825     hsp->mbox_sm.chans = devm_kcalloc(&pdev->dev, hsp->mbox_sm.num_chans,
0826                       sizeof(*hsp->mbox_sm.chans),
0827                       GFP_KERNEL);
0828     if (!hsp->mbox_sm.chans)
0829         return -ENOMEM;
0830 
0831     if (hsp->shared_irqs) {
0832         err = tegra_hsp_add_mailboxes(hsp, &pdev->dev);
0833         if (err < 0) {
0834             dev_err(&pdev->dev, "failed to add mailboxes: %d\n",
0835                     err);
0836             return err;
0837         }
0838     }
0839 
0840     err = devm_mbox_controller_register(&pdev->dev, &hsp->mbox_sm);
0841     if (err < 0) {
0842         dev_err(&pdev->dev, "failed to register shared mailbox: %d\n",
0843             err);
0844         return err;
0845     }
0846 
0847     platform_set_drvdata(pdev, hsp);
0848 
0849     if (hsp->doorbell_irq) {
0850         err = devm_request_irq(&pdev->dev, hsp->doorbell_irq,
0851                        tegra_hsp_doorbell_irq, IRQF_NO_SUSPEND,
0852                        dev_name(&pdev->dev), hsp);
0853         if (err < 0) {
0854             dev_err(&pdev->dev,
0855                     "failed to request doorbell IRQ#%u: %d\n",
0856                 hsp->doorbell_irq, err);
0857             return err;
0858         }
0859     }
0860 
0861     if (hsp->shared_irqs) {
0862         err = tegra_hsp_request_shared_irq(hsp);
0863         if (err < 0)
0864             return err;
0865     }
0866 
0867     lockdep_register_key(&hsp->lock_key);
0868     lockdep_set_class(&hsp->lock, &hsp->lock_key);
0869 
0870     return 0;
0871 }
0872 
0873 static int tegra_hsp_remove(struct platform_device *pdev)
0874 {
0875     struct tegra_hsp *hsp = platform_get_drvdata(pdev);
0876 
0877     lockdep_unregister_key(&hsp->lock_key);
0878 
0879     return 0;
0880 }
0881 
0882 static int __maybe_unused tegra_hsp_resume(struct device *dev)
0883 {
0884     struct tegra_hsp *hsp = dev_get_drvdata(dev);
0885     unsigned int i;
0886     struct tegra_hsp_doorbell *db;
0887 
0888     list_for_each_entry(db, &hsp->doorbells, list) {
0889         if (db->channel.chan)
0890             tegra_hsp_doorbell_startup(db->channel.chan);
0891     }
0892 
0893     if (hsp->mailboxes) {
0894         for (i = 0; i < hsp->num_sm; i++) {
0895             struct tegra_hsp_mailbox *mb = &hsp->mailboxes[i];
0896 
0897             if (mb->channel.chan->cl)
0898                 tegra_hsp_mailbox_startup(mb->channel.chan);
0899         }
0900     }
0901 
0902     return 0;
0903 }
0904 
0905 static const struct dev_pm_ops tegra_hsp_pm_ops = {
0906     .resume_noirq = tegra_hsp_resume,
0907 };
0908 
0909 static const struct tegra_hsp_db_map tegra186_hsp_db_map[] = {
0910     { "ccplex", TEGRA_HSP_DB_MASTER_CCPLEX, HSP_DB_CCPLEX, },
0911     { "bpmp",   TEGRA_HSP_DB_MASTER_BPMP,   HSP_DB_BPMP,   },
0912     { /* sentinel */ }
0913 };
0914 
0915 static const struct tegra_hsp_soc tegra186_hsp_soc = {
0916     .map = tegra186_hsp_db_map,
0917     .has_per_mb_ie = false,
0918     .has_128_bit_mb = false,
0919 };
0920 
0921 static const struct tegra_hsp_soc tegra194_hsp_soc = {
0922     .map = tegra186_hsp_db_map,
0923     .has_per_mb_ie = true,
0924     .has_128_bit_mb = false,
0925 };
0926 
0927 static const struct tegra_hsp_soc tegra234_hsp_soc = {
0928     .map = tegra186_hsp_db_map,
0929     .has_per_mb_ie = false,
0930     .has_128_bit_mb = true,
0931 };
0932 
0933 static const struct of_device_id tegra_hsp_match[] = {
0934     { .compatible = "nvidia,tegra186-hsp", .data = &tegra186_hsp_soc },
0935     { .compatible = "nvidia,tegra194-hsp", .data = &tegra194_hsp_soc },
0936     { .compatible = "nvidia,tegra234-hsp", .data = &tegra234_hsp_soc },
0937     { }
0938 };
0939 
0940 static struct platform_driver tegra_hsp_driver = {
0941     .driver = {
0942         .name = "tegra-hsp",
0943         .of_match_table = tegra_hsp_match,
0944         .pm = &tegra_hsp_pm_ops,
0945     },
0946     .probe = tegra_hsp_probe,
0947     .remove = tegra_hsp_remove,
0948 };
0949 
0950 static int __init tegra_hsp_init(void)
0951 {
0952     return platform_driver_register(&tegra_hsp_driver);
0953 }
0954 core_initcall(tegra_hsp_init);