Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * Copyright (c) 2016, NVIDIA CORPORATION.  All rights reserved.
0004  */
0005 
0006 #include <linux/clk/tegra.h>
0007 #include <linux/genalloc.h>
0008 #include <linux/mailbox_client.h>
0009 #include <linux/module.h>
0010 #include <linux/of.h>
0011 #include <linux/of_address.h>
0012 #include <linux/of_device.h>
0013 #include <linux/platform_device.h>
0014 #include <linux/pm.h>
0015 #include <linux/semaphore.h>
0016 #include <linux/sched/clock.h>
0017 
0018 #include <soc/tegra/bpmp.h>
0019 #include <soc/tegra/bpmp-abi.h>
0020 #include <soc/tegra/ivc.h>
0021 
0022 #include "bpmp-private.h"
0023 
0024 #define MSG_ACK     BIT(0)
0025 #define MSG_RING    BIT(1)
0026 #define TAG_SZ      32
0027 
0028 static inline struct tegra_bpmp *
0029 mbox_client_to_bpmp(struct mbox_client *client)
0030 {
0031     return container_of(client, struct tegra_bpmp, mbox.client);
0032 }
0033 
0034 static inline const struct tegra_bpmp_ops *
0035 channel_to_ops(struct tegra_bpmp_channel *channel)
0036 {
0037     struct tegra_bpmp *bpmp = channel->bpmp;
0038 
0039     return bpmp->soc->ops;
0040 }
0041 
0042 struct tegra_bpmp *tegra_bpmp_get(struct device *dev)
0043 {
0044     struct platform_device *pdev;
0045     struct tegra_bpmp *bpmp;
0046     struct device_node *np;
0047 
0048     np = of_parse_phandle(dev->of_node, "nvidia,bpmp", 0);
0049     if (!np)
0050         return ERR_PTR(-ENOENT);
0051 
0052     pdev = of_find_device_by_node(np);
0053     if (!pdev) {
0054         bpmp = ERR_PTR(-ENODEV);
0055         goto put;
0056     }
0057 
0058     bpmp = platform_get_drvdata(pdev);
0059     if (!bpmp) {
0060         bpmp = ERR_PTR(-EPROBE_DEFER);
0061         put_device(&pdev->dev);
0062         goto put;
0063     }
0064 
0065 put:
0066     of_node_put(np);
0067     return bpmp;
0068 }
0069 EXPORT_SYMBOL_GPL(tegra_bpmp_get);
0070 
0071 void tegra_bpmp_put(struct tegra_bpmp *bpmp)
0072 {
0073     if (bpmp)
0074         put_device(bpmp->dev);
0075 }
0076 EXPORT_SYMBOL_GPL(tegra_bpmp_put);
0077 
0078 static int
0079 tegra_bpmp_channel_get_thread_index(struct tegra_bpmp_channel *channel)
0080 {
0081     struct tegra_bpmp *bpmp = channel->bpmp;
0082     unsigned int count;
0083     int index;
0084 
0085     count = bpmp->soc->channels.thread.count;
0086 
0087     index = channel - channel->bpmp->threaded_channels;
0088     if (index < 0 || index >= count)
0089         return -EINVAL;
0090 
0091     return index;
0092 }
0093 
0094 static bool tegra_bpmp_message_valid(const struct tegra_bpmp_message *msg)
0095 {
0096     return (msg->tx.size <= MSG_DATA_MIN_SZ) &&
0097            (msg->rx.size <= MSG_DATA_MIN_SZ) &&
0098            (msg->tx.size == 0 || msg->tx.data) &&
0099            (msg->rx.size == 0 || msg->rx.data);
0100 }
0101 
0102 static bool tegra_bpmp_is_response_ready(struct tegra_bpmp_channel *channel)
0103 {
0104     const struct tegra_bpmp_ops *ops = channel_to_ops(channel);
0105 
0106     return ops->is_response_ready(channel);
0107 }
0108 
0109 static bool tegra_bpmp_is_request_ready(struct tegra_bpmp_channel *channel)
0110 {
0111     const struct tegra_bpmp_ops *ops = channel_to_ops(channel);
0112 
0113     return ops->is_request_ready(channel);
0114 }
0115 
0116 static int tegra_bpmp_wait_response(struct tegra_bpmp_channel *channel)
0117 {
0118     unsigned long timeout = channel->bpmp->soc->channels.cpu_tx.timeout;
0119     ktime_t end;
0120 
0121     end = ktime_add_us(ktime_get(), timeout);
0122 
0123     do {
0124         if (tegra_bpmp_is_response_ready(channel))
0125             return 0;
0126     } while (ktime_before(ktime_get(), end));
0127 
0128     return -ETIMEDOUT;
0129 }
0130 
0131 static int tegra_bpmp_ack_response(struct tegra_bpmp_channel *channel)
0132 {
0133     const struct tegra_bpmp_ops *ops = channel_to_ops(channel);
0134 
0135     return ops->ack_response(channel);
0136 }
0137 
0138 static int tegra_bpmp_ack_request(struct tegra_bpmp_channel *channel)
0139 {
0140     const struct tegra_bpmp_ops *ops = channel_to_ops(channel);
0141 
0142     return ops->ack_request(channel);
0143 }
0144 
0145 static bool
0146 tegra_bpmp_is_request_channel_free(struct tegra_bpmp_channel *channel)
0147 {
0148     const struct tegra_bpmp_ops *ops = channel_to_ops(channel);
0149 
0150     return ops->is_request_channel_free(channel);
0151 }
0152 
0153 static bool
0154 tegra_bpmp_is_response_channel_free(struct tegra_bpmp_channel *channel)
0155 {
0156     const struct tegra_bpmp_ops *ops = channel_to_ops(channel);
0157 
0158     return ops->is_response_channel_free(channel);
0159 }
0160 
0161 static int
0162 tegra_bpmp_wait_request_channel_free(struct tegra_bpmp_channel *channel)
0163 {
0164     unsigned long timeout = channel->bpmp->soc->channels.cpu_tx.timeout;
0165     ktime_t start, now;
0166 
0167     start = ns_to_ktime(local_clock());
0168 
0169     do {
0170         if (tegra_bpmp_is_request_channel_free(channel))
0171             return 0;
0172 
0173         now = ns_to_ktime(local_clock());
0174     } while (ktime_us_delta(now, start) < timeout);
0175 
0176     return -ETIMEDOUT;
0177 }
0178 
0179 static int tegra_bpmp_post_request(struct tegra_bpmp_channel *channel)
0180 {
0181     const struct tegra_bpmp_ops *ops = channel_to_ops(channel);
0182 
0183     return ops->post_request(channel);
0184 }
0185 
0186 static int tegra_bpmp_post_response(struct tegra_bpmp_channel *channel)
0187 {
0188     const struct tegra_bpmp_ops *ops = channel_to_ops(channel);
0189 
0190     return ops->post_response(channel);
0191 }
0192 
0193 static int tegra_bpmp_ring_doorbell(struct tegra_bpmp *bpmp)
0194 {
0195     return bpmp->soc->ops->ring_doorbell(bpmp);
0196 }
0197 
0198 static ssize_t __tegra_bpmp_channel_read(struct tegra_bpmp_channel *channel,
0199                      void *data, size_t size, int *ret)
0200 {
0201     int err;
0202 
0203     if (data && size > 0)
0204         memcpy_fromio(data, channel->ib->data, size);
0205 
0206     err = tegra_bpmp_ack_response(channel);
0207     if (err < 0)
0208         return err;
0209 
0210     *ret = channel->ib->code;
0211 
0212     return 0;
0213 }
0214 
0215 static ssize_t tegra_bpmp_channel_read(struct tegra_bpmp_channel *channel,
0216                        void *data, size_t size, int *ret)
0217 {
0218     struct tegra_bpmp *bpmp = channel->bpmp;
0219     unsigned long flags;
0220     ssize_t err;
0221     int index;
0222 
0223     index = tegra_bpmp_channel_get_thread_index(channel);
0224     if (index < 0) {
0225         err = index;
0226         goto unlock;
0227     }
0228 
0229     spin_lock_irqsave(&bpmp->lock, flags);
0230     err = __tegra_bpmp_channel_read(channel, data, size, ret);
0231     clear_bit(index, bpmp->threaded.allocated);
0232     spin_unlock_irqrestore(&bpmp->lock, flags);
0233 
0234 unlock:
0235     up(&bpmp->threaded.lock);
0236 
0237     return err;
0238 }
0239 
0240 static ssize_t __tegra_bpmp_channel_write(struct tegra_bpmp_channel *channel,
0241                       unsigned int mrq, unsigned long flags,
0242                       const void *data, size_t size)
0243 {
0244     channel->ob->code = mrq;
0245     channel->ob->flags = flags;
0246 
0247     if (data && size > 0)
0248         memcpy_toio(channel->ob->data, data, size);
0249 
0250     return tegra_bpmp_post_request(channel);
0251 }
0252 
0253 static struct tegra_bpmp_channel *
0254 tegra_bpmp_write_threaded(struct tegra_bpmp *bpmp, unsigned int mrq,
0255               const void *data, size_t size)
0256 {
0257     unsigned long timeout = bpmp->soc->channels.thread.timeout;
0258     unsigned int count = bpmp->soc->channels.thread.count;
0259     struct tegra_bpmp_channel *channel;
0260     unsigned long flags;
0261     unsigned int index;
0262     int err;
0263 
0264     err = down_timeout(&bpmp->threaded.lock, usecs_to_jiffies(timeout));
0265     if (err < 0)
0266         return ERR_PTR(err);
0267 
0268     spin_lock_irqsave(&bpmp->lock, flags);
0269 
0270     index = find_first_zero_bit(bpmp->threaded.allocated, count);
0271     if (index == count) {
0272         err = -EBUSY;
0273         goto unlock;
0274     }
0275 
0276     channel = &bpmp->threaded_channels[index];
0277 
0278     if (!tegra_bpmp_is_request_channel_free(channel)) {
0279         err = -EBUSY;
0280         goto unlock;
0281     }
0282 
0283     set_bit(index, bpmp->threaded.allocated);
0284 
0285     err = __tegra_bpmp_channel_write(channel, mrq, MSG_ACK | MSG_RING,
0286                      data, size);
0287     if (err < 0)
0288         goto clear_allocated;
0289 
0290     set_bit(index, bpmp->threaded.busy);
0291 
0292     spin_unlock_irqrestore(&bpmp->lock, flags);
0293     return channel;
0294 
0295 clear_allocated:
0296     clear_bit(index, bpmp->threaded.allocated);
0297 unlock:
0298     spin_unlock_irqrestore(&bpmp->lock, flags);
0299     up(&bpmp->threaded.lock);
0300 
0301     return ERR_PTR(err);
0302 }
0303 
0304 static ssize_t tegra_bpmp_channel_write(struct tegra_bpmp_channel *channel,
0305                     unsigned int mrq, unsigned long flags,
0306                     const void *data, size_t size)
0307 {
0308     int err;
0309 
0310     err = tegra_bpmp_wait_request_channel_free(channel);
0311     if (err < 0)
0312         return err;
0313 
0314     return __tegra_bpmp_channel_write(channel, mrq, flags, data, size);
0315 }
0316 
0317 int tegra_bpmp_transfer_atomic(struct tegra_bpmp *bpmp,
0318                    struct tegra_bpmp_message *msg)
0319 {
0320     struct tegra_bpmp_channel *channel;
0321     int err;
0322 
0323     if (WARN_ON(!irqs_disabled()))
0324         return -EPERM;
0325 
0326     if (!tegra_bpmp_message_valid(msg))
0327         return -EINVAL;
0328 
0329     channel = bpmp->tx_channel;
0330 
0331     spin_lock(&bpmp->atomic_tx_lock);
0332 
0333     err = tegra_bpmp_channel_write(channel, msg->mrq, MSG_ACK,
0334                        msg->tx.data, msg->tx.size);
0335     if (err < 0) {
0336         spin_unlock(&bpmp->atomic_tx_lock);
0337         return err;
0338     }
0339 
0340     spin_unlock(&bpmp->atomic_tx_lock);
0341 
0342     err = tegra_bpmp_ring_doorbell(bpmp);
0343     if (err < 0)
0344         return err;
0345 
0346     err = tegra_bpmp_wait_response(channel);
0347     if (err < 0)
0348         return err;
0349 
0350     return __tegra_bpmp_channel_read(channel, msg->rx.data, msg->rx.size,
0351                      &msg->rx.ret);
0352 }
0353 EXPORT_SYMBOL_GPL(tegra_bpmp_transfer_atomic);
0354 
0355 int tegra_bpmp_transfer(struct tegra_bpmp *bpmp,
0356             struct tegra_bpmp_message *msg)
0357 {
0358     struct tegra_bpmp_channel *channel;
0359     unsigned long timeout;
0360     int err;
0361 
0362     if (WARN_ON(irqs_disabled()))
0363         return -EPERM;
0364 
0365     if (!tegra_bpmp_message_valid(msg))
0366         return -EINVAL;
0367 
0368     channel = tegra_bpmp_write_threaded(bpmp, msg->mrq, msg->tx.data,
0369                         msg->tx.size);
0370     if (IS_ERR(channel))
0371         return PTR_ERR(channel);
0372 
0373     err = tegra_bpmp_ring_doorbell(bpmp);
0374     if (err < 0)
0375         return err;
0376 
0377     timeout = usecs_to_jiffies(bpmp->soc->channels.thread.timeout);
0378 
0379     err = wait_for_completion_timeout(&channel->completion, timeout);
0380     if (err == 0)
0381         return -ETIMEDOUT;
0382 
0383     return tegra_bpmp_channel_read(channel, msg->rx.data, msg->rx.size,
0384                        &msg->rx.ret);
0385 }
0386 EXPORT_SYMBOL_GPL(tegra_bpmp_transfer);
0387 
0388 static struct tegra_bpmp_mrq *tegra_bpmp_find_mrq(struct tegra_bpmp *bpmp,
0389                           unsigned int mrq)
0390 {
0391     struct tegra_bpmp_mrq *entry;
0392 
0393     list_for_each_entry(entry, &bpmp->mrqs, list)
0394         if (entry->mrq == mrq)
0395             return entry;
0396 
0397     return NULL;
0398 }
0399 
0400 void tegra_bpmp_mrq_return(struct tegra_bpmp_channel *channel, int code,
0401                const void *data, size_t size)
0402 {
0403     unsigned long flags = channel->ib->flags;
0404     struct tegra_bpmp *bpmp = channel->bpmp;
0405     int err;
0406 
0407     if (WARN_ON(size > MSG_DATA_MIN_SZ))
0408         return;
0409 
0410     err = tegra_bpmp_ack_request(channel);
0411     if (WARN_ON(err < 0))
0412         return;
0413 
0414     if ((flags & MSG_ACK) == 0)
0415         return;
0416 
0417     if (WARN_ON(!tegra_bpmp_is_response_channel_free(channel)))
0418         return;
0419 
0420     channel->ob->code = code;
0421 
0422     if (data && size > 0)
0423         memcpy_toio(channel->ob->data, data, size);
0424 
0425     err = tegra_bpmp_post_response(channel);
0426     if (WARN_ON(err < 0))
0427         return;
0428 
0429     if (flags & MSG_RING) {
0430         err = tegra_bpmp_ring_doorbell(bpmp);
0431         if (WARN_ON(err < 0))
0432             return;
0433     }
0434 }
0435 EXPORT_SYMBOL_GPL(tegra_bpmp_mrq_return);
0436 
0437 static void tegra_bpmp_handle_mrq(struct tegra_bpmp *bpmp,
0438                   unsigned int mrq,
0439                   struct tegra_bpmp_channel *channel)
0440 {
0441     struct tegra_bpmp_mrq *entry;
0442     u32 zero = 0;
0443 
0444     spin_lock(&bpmp->lock);
0445 
0446     entry = tegra_bpmp_find_mrq(bpmp, mrq);
0447     if (!entry) {
0448         spin_unlock(&bpmp->lock);
0449         tegra_bpmp_mrq_return(channel, -EINVAL, &zero, sizeof(zero));
0450         return;
0451     }
0452 
0453     entry->handler(mrq, channel, entry->data);
0454 
0455     spin_unlock(&bpmp->lock);
0456 }
0457 
0458 int tegra_bpmp_request_mrq(struct tegra_bpmp *bpmp, unsigned int mrq,
0459                tegra_bpmp_mrq_handler_t handler, void *data)
0460 {
0461     struct tegra_bpmp_mrq *entry;
0462     unsigned long flags;
0463 
0464     if (!handler)
0465         return -EINVAL;
0466 
0467     entry = devm_kzalloc(bpmp->dev, sizeof(*entry), GFP_KERNEL);
0468     if (!entry)
0469         return -ENOMEM;
0470 
0471     spin_lock_irqsave(&bpmp->lock, flags);
0472 
0473     entry->mrq = mrq;
0474     entry->handler = handler;
0475     entry->data = data;
0476     list_add(&entry->list, &bpmp->mrqs);
0477 
0478     spin_unlock_irqrestore(&bpmp->lock, flags);
0479 
0480     return 0;
0481 }
0482 EXPORT_SYMBOL_GPL(tegra_bpmp_request_mrq);
0483 
0484 void tegra_bpmp_free_mrq(struct tegra_bpmp *bpmp, unsigned int mrq, void *data)
0485 {
0486     struct tegra_bpmp_mrq *entry;
0487     unsigned long flags;
0488 
0489     spin_lock_irqsave(&bpmp->lock, flags);
0490 
0491     entry = tegra_bpmp_find_mrq(bpmp, mrq);
0492     if (!entry)
0493         goto unlock;
0494 
0495     list_del(&entry->list);
0496     devm_kfree(bpmp->dev, entry);
0497 
0498 unlock:
0499     spin_unlock_irqrestore(&bpmp->lock, flags);
0500 }
0501 EXPORT_SYMBOL_GPL(tegra_bpmp_free_mrq);
0502 
0503 bool tegra_bpmp_mrq_is_supported(struct tegra_bpmp *bpmp, unsigned int mrq)
0504 {
0505     struct mrq_query_abi_request req = { .mrq = cpu_to_le32(mrq) };
0506     struct mrq_query_abi_response resp;
0507     struct tegra_bpmp_message msg = {
0508         .mrq = MRQ_QUERY_ABI,
0509         .tx = {
0510             .data = &req,
0511             .size = sizeof(req),
0512         },
0513         .rx = {
0514             .data = &resp,
0515             .size = sizeof(resp),
0516         },
0517     };
0518     int err;
0519 
0520     err = tegra_bpmp_transfer(bpmp, &msg);
0521     if (err || msg.rx.ret)
0522         return false;
0523 
0524     return resp.status == 0;
0525 }
0526 EXPORT_SYMBOL_GPL(tegra_bpmp_mrq_is_supported);
0527 
0528 static void tegra_bpmp_mrq_handle_ping(unsigned int mrq,
0529                        struct tegra_bpmp_channel *channel,
0530                        void *data)
0531 {
0532     struct mrq_ping_request *request;
0533     struct mrq_ping_response response;
0534 
0535     request = (struct mrq_ping_request *)channel->ib->data;
0536 
0537     memset(&response, 0, sizeof(response));
0538     response.reply = request->challenge << 1;
0539 
0540     tegra_bpmp_mrq_return(channel, 0, &response, sizeof(response));
0541 }
0542 
0543 static int tegra_bpmp_ping(struct tegra_bpmp *bpmp)
0544 {
0545     struct mrq_ping_response response;
0546     struct mrq_ping_request request;
0547     struct tegra_bpmp_message msg;
0548     unsigned long flags;
0549     ktime_t start, end;
0550     int err;
0551 
0552     memset(&request, 0, sizeof(request));
0553     request.challenge = 1;
0554 
0555     memset(&response, 0, sizeof(response));
0556 
0557     memset(&msg, 0, sizeof(msg));
0558     msg.mrq = MRQ_PING;
0559     msg.tx.data = &request;
0560     msg.tx.size = sizeof(request);
0561     msg.rx.data = &response;
0562     msg.rx.size = sizeof(response);
0563 
0564     local_irq_save(flags);
0565     start = ktime_get();
0566     err = tegra_bpmp_transfer_atomic(bpmp, &msg);
0567     end = ktime_get();
0568     local_irq_restore(flags);
0569 
0570     if (!err)
0571         dev_dbg(bpmp->dev,
0572             "ping ok: challenge: %u, response: %u, time: %lld\n",
0573             request.challenge, response.reply,
0574             ktime_to_us(ktime_sub(end, start)));
0575 
0576     return err;
0577 }
0578 
0579 /* deprecated version of tag query */
0580 static int tegra_bpmp_get_firmware_tag_old(struct tegra_bpmp *bpmp, char *tag,
0581                        size_t size)
0582 {
0583     struct mrq_query_tag_request request;
0584     struct tegra_bpmp_message msg;
0585     unsigned long flags;
0586     dma_addr_t phys;
0587     void *virt;
0588     int err;
0589 
0590     if (size != TAG_SZ)
0591         return -EINVAL;
0592 
0593     virt = dma_alloc_coherent(bpmp->dev, TAG_SZ, &phys,
0594                   GFP_KERNEL | GFP_DMA32);
0595     if (!virt)
0596         return -ENOMEM;
0597 
0598     memset(&request, 0, sizeof(request));
0599     request.addr = phys;
0600 
0601     memset(&msg, 0, sizeof(msg));
0602     msg.mrq = MRQ_QUERY_TAG;
0603     msg.tx.data = &request;
0604     msg.tx.size = sizeof(request);
0605 
0606     local_irq_save(flags);
0607     err = tegra_bpmp_transfer_atomic(bpmp, &msg);
0608     local_irq_restore(flags);
0609 
0610     if (err == 0)
0611         memcpy(tag, virt, TAG_SZ);
0612 
0613     dma_free_coherent(bpmp->dev, TAG_SZ, virt, phys);
0614 
0615     return err;
0616 }
0617 
0618 static int tegra_bpmp_get_firmware_tag(struct tegra_bpmp *bpmp, char *tag,
0619                        size_t size)
0620 {
0621     if (tegra_bpmp_mrq_is_supported(bpmp, MRQ_QUERY_FW_TAG)) {
0622         struct mrq_query_fw_tag_response resp;
0623         struct tegra_bpmp_message msg = {
0624             .mrq = MRQ_QUERY_FW_TAG,
0625             .rx = {
0626                 .data = &resp,
0627                 .size = sizeof(resp),
0628             },
0629         };
0630         int err;
0631 
0632         if (size != sizeof(resp.tag))
0633             return -EINVAL;
0634 
0635         err = tegra_bpmp_transfer(bpmp, &msg);
0636 
0637         if (err)
0638             return err;
0639         if (msg.rx.ret < 0)
0640             return -EINVAL;
0641 
0642         memcpy(tag, resp.tag, sizeof(resp.tag));
0643         return 0;
0644     }
0645 
0646     return tegra_bpmp_get_firmware_tag_old(bpmp, tag, size);
0647 }
0648 
0649 static void tegra_bpmp_channel_signal(struct tegra_bpmp_channel *channel)
0650 {
0651     unsigned long flags = channel->ob->flags;
0652 
0653     if ((flags & MSG_RING) == 0)
0654         return;
0655 
0656     complete(&channel->completion);
0657 }
0658 
0659 void tegra_bpmp_handle_rx(struct tegra_bpmp *bpmp)
0660 {
0661     struct tegra_bpmp_channel *channel;
0662     unsigned int i, count;
0663     unsigned long *busy;
0664 
0665     channel = bpmp->rx_channel;
0666     count = bpmp->soc->channels.thread.count;
0667     busy = bpmp->threaded.busy;
0668 
0669     if (tegra_bpmp_is_request_ready(channel))
0670         tegra_bpmp_handle_mrq(bpmp, channel->ib->code, channel);
0671 
0672     spin_lock(&bpmp->lock);
0673 
0674     for_each_set_bit(i, busy, count) {
0675         struct tegra_bpmp_channel *channel;
0676 
0677         channel = &bpmp->threaded_channels[i];
0678 
0679         if (tegra_bpmp_is_response_ready(channel)) {
0680             tegra_bpmp_channel_signal(channel);
0681             clear_bit(i, busy);
0682         }
0683     }
0684 
0685     spin_unlock(&bpmp->lock);
0686 }
0687 
0688 static int tegra_bpmp_probe(struct platform_device *pdev)
0689 {
0690     struct tegra_bpmp *bpmp;
0691     char tag[TAG_SZ];
0692     size_t size;
0693     int err;
0694 
0695     bpmp = devm_kzalloc(&pdev->dev, sizeof(*bpmp), GFP_KERNEL);
0696     if (!bpmp)
0697         return -ENOMEM;
0698 
0699     bpmp->soc = of_device_get_match_data(&pdev->dev);
0700     bpmp->dev = &pdev->dev;
0701 
0702     INIT_LIST_HEAD(&bpmp->mrqs);
0703     spin_lock_init(&bpmp->lock);
0704 
0705     bpmp->threaded.count = bpmp->soc->channels.thread.count;
0706     sema_init(&bpmp->threaded.lock, bpmp->threaded.count);
0707 
0708     size = BITS_TO_LONGS(bpmp->threaded.count) * sizeof(long);
0709 
0710     bpmp->threaded.allocated = devm_kzalloc(&pdev->dev, size, GFP_KERNEL);
0711     if (!bpmp->threaded.allocated)
0712         return -ENOMEM;
0713 
0714     bpmp->threaded.busy = devm_kzalloc(&pdev->dev, size, GFP_KERNEL);
0715     if (!bpmp->threaded.busy)
0716         return -ENOMEM;
0717 
0718     spin_lock_init(&bpmp->atomic_tx_lock);
0719     bpmp->tx_channel = devm_kzalloc(&pdev->dev, sizeof(*bpmp->tx_channel),
0720                     GFP_KERNEL);
0721     if (!bpmp->tx_channel)
0722         return -ENOMEM;
0723 
0724     bpmp->rx_channel = devm_kzalloc(&pdev->dev, sizeof(*bpmp->rx_channel),
0725                                     GFP_KERNEL);
0726     if (!bpmp->rx_channel)
0727         return -ENOMEM;
0728 
0729     bpmp->threaded_channels = devm_kcalloc(&pdev->dev, bpmp->threaded.count,
0730                            sizeof(*bpmp->threaded_channels),
0731                            GFP_KERNEL);
0732     if (!bpmp->threaded_channels)
0733         return -ENOMEM;
0734 
0735     err = bpmp->soc->ops->init(bpmp);
0736     if (err < 0)
0737         return err;
0738 
0739     err = tegra_bpmp_request_mrq(bpmp, MRQ_PING,
0740                      tegra_bpmp_mrq_handle_ping, bpmp);
0741     if (err < 0)
0742         goto deinit;
0743 
0744     err = tegra_bpmp_ping(bpmp);
0745     if (err < 0) {
0746         dev_err(&pdev->dev, "failed to ping BPMP: %d\n", err);
0747         goto free_mrq;
0748     }
0749 
0750     err = tegra_bpmp_get_firmware_tag(bpmp, tag, sizeof(tag));
0751     if (err < 0) {
0752         dev_err(&pdev->dev, "failed to get firmware tag: %d\n", err);
0753         goto free_mrq;
0754     }
0755 
0756     dev_info(&pdev->dev, "firmware: %.*s\n", (int)sizeof(tag), tag);
0757 
0758     platform_set_drvdata(pdev, bpmp);
0759 
0760     err = of_platform_default_populate(pdev->dev.of_node, NULL, &pdev->dev);
0761     if (err < 0)
0762         goto free_mrq;
0763 
0764     if (of_find_property(pdev->dev.of_node, "#clock-cells", NULL)) {
0765         err = tegra_bpmp_init_clocks(bpmp);
0766         if (err < 0)
0767             goto free_mrq;
0768     }
0769 
0770     if (of_find_property(pdev->dev.of_node, "#reset-cells", NULL)) {
0771         err = tegra_bpmp_init_resets(bpmp);
0772         if (err < 0)
0773             goto free_mrq;
0774     }
0775 
0776     if (of_find_property(pdev->dev.of_node, "#power-domain-cells", NULL)) {
0777         err = tegra_bpmp_init_powergates(bpmp);
0778         if (err < 0)
0779             goto free_mrq;
0780     }
0781 
0782     err = tegra_bpmp_init_debugfs(bpmp);
0783     if (err < 0)
0784         dev_err(&pdev->dev, "debugfs initialization failed: %d\n", err);
0785 
0786     return 0;
0787 
0788 free_mrq:
0789     tegra_bpmp_free_mrq(bpmp, MRQ_PING, bpmp);
0790 deinit:
0791     if (bpmp->soc->ops->deinit)
0792         bpmp->soc->ops->deinit(bpmp);
0793 
0794     return err;
0795 }
0796 
0797 static int __maybe_unused tegra_bpmp_resume(struct device *dev)
0798 {
0799     struct tegra_bpmp *bpmp = dev_get_drvdata(dev);
0800 
0801     if (bpmp->soc->ops->resume)
0802         return bpmp->soc->ops->resume(bpmp);
0803     else
0804         return 0;
0805 }
0806 
0807 static const struct dev_pm_ops tegra_bpmp_pm_ops = {
0808     .resume_noirq = tegra_bpmp_resume,
0809 };
0810 
0811 #if IS_ENABLED(CONFIG_ARCH_TEGRA_186_SOC) || \
0812     IS_ENABLED(CONFIG_ARCH_TEGRA_194_SOC) || \
0813     IS_ENABLED(CONFIG_ARCH_TEGRA_234_SOC)
0814 static const struct tegra_bpmp_soc tegra186_soc = {
0815     .channels = {
0816         .cpu_tx = {
0817             .offset = 3,
0818             .timeout = 60 * USEC_PER_SEC,
0819         },
0820         .thread = {
0821             .offset = 0,
0822             .count = 3,
0823             .timeout = 600 * USEC_PER_SEC,
0824         },
0825         .cpu_rx = {
0826             .offset = 13,
0827             .timeout = 0,
0828         },
0829     },
0830     .ops = &tegra186_bpmp_ops,
0831     .num_resets = 193,
0832 };
0833 #endif
0834 
0835 #if IS_ENABLED(CONFIG_ARCH_TEGRA_210_SOC)
0836 static const struct tegra_bpmp_soc tegra210_soc = {
0837     .channels = {
0838         .cpu_tx = {
0839             .offset = 0,
0840             .count = 1,
0841             .timeout = 60 * USEC_PER_SEC,
0842         },
0843         .thread = {
0844             .offset = 4,
0845             .count = 1,
0846             .timeout = 600 * USEC_PER_SEC,
0847         },
0848         .cpu_rx = {
0849             .offset = 8,
0850             .count = 1,
0851             .timeout = 0,
0852         },
0853     },
0854     .ops = &tegra210_bpmp_ops,
0855 };
0856 #endif
0857 
0858 static const struct of_device_id tegra_bpmp_match[] = {
0859 #if IS_ENABLED(CONFIG_ARCH_TEGRA_186_SOC) || \
0860     IS_ENABLED(CONFIG_ARCH_TEGRA_194_SOC) || \
0861     IS_ENABLED(CONFIG_ARCH_TEGRA_234_SOC)
0862     { .compatible = "nvidia,tegra186-bpmp", .data = &tegra186_soc },
0863 #endif
0864 #if IS_ENABLED(CONFIG_ARCH_TEGRA_210_SOC)
0865     { .compatible = "nvidia,tegra210-bpmp", .data = &tegra210_soc },
0866 #endif
0867     { }
0868 };
0869 
0870 static struct platform_driver tegra_bpmp_driver = {
0871     .driver = {
0872         .name = "tegra-bpmp",
0873         .of_match_table = tegra_bpmp_match,
0874         .pm = &tegra_bpmp_pm_ops,
0875         .suppress_bind_attrs = true,
0876     },
0877     .probe = tegra_bpmp_probe,
0878 };
0879 builtin_platform_driver(tegra_bpmp_driver);