2 * Copyright (c) 2016, NVIDIA CORPORATION. All rights reserved.
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms and conditions of the GNU General Public License,
6 * version 2, as published by the Free Software Foundation.
8 * This program is distributed in the hope it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 #include <linux/clk/tegra.h>
15 #include <linux/genalloc.h>
16 #include <linux/mailbox_client.h>
18 #include <linux/of_address.h>
19 #include <linux/of_device.h>
20 #include <linux/platform_device.h>
22 #include <linux/semaphore.h>
23 #include <linux/sched/clock.h>
25 #include <soc/tegra/bpmp.h>
26 #include <soc/tegra/bpmp-abi.h>
27 #include <soc/tegra/ivc.h>
29 #include "bpmp-private.h"
31 #define MSG_ACK BIT(0)
32 #define MSG_RING BIT(1)
35 static inline struct tegra_bpmp *
36 mbox_client_to_bpmp(struct mbox_client *client)
38 return container_of(client, struct tegra_bpmp, mbox.client);
41 static inline const struct tegra_bpmp_ops *
42 channel_to_ops(struct tegra_bpmp_channel *channel)
44 struct tegra_bpmp *bpmp = channel->bpmp;
46 return bpmp->soc->ops;
49 struct tegra_bpmp *tegra_bpmp_get(struct device *dev)
51 struct platform_device *pdev;
52 struct tegra_bpmp *bpmp;
53 struct device_node *np;
55 np = of_parse_phandle(dev->of_node, "nvidia,bpmp", 0);
57 return ERR_PTR(-ENOENT);
59 pdev = of_find_device_by_node(np);
61 bpmp = ERR_PTR(-ENODEV);
65 bpmp = platform_get_drvdata(pdev);
67 bpmp = ERR_PTR(-EPROBE_DEFER);
68 put_device(&pdev->dev);
76 EXPORT_SYMBOL_GPL(tegra_bpmp_get);
78 void tegra_bpmp_put(struct tegra_bpmp *bpmp)
81 put_device(bpmp->dev);
83 EXPORT_SYMBOL_GPL(tegra_bpmp_put);
86 tegra_bpmp_channel_get_thread_index(struct tegra_bpmp_channel *channel)
88 struct tegra_bpmp *bpmp = channel->bpmp;
92 count = bpmp->soc->channels.thread.count;
94 index = channel - channel->bpmp->threaded_channels;
95 if (index < 0 || index >= count)
101 static bool tegra_bpmp_message_valid(const struct tegra_bpmp_message *msg)
103 return (msg->tx.size <= MSG_DATA_MIN_SZ) &&
104 (msg->rx.size <= MSG_DATA_MIN_SZ) &&
105 (msg->tx.size == 0 || msg->tx.data) &&
106 (msg->rx.size == 0 || msg->rx.data);
109 static bool tegra_bpmp_is_response_ready(struct tegra_bpmp_channel *channel)
111 const struct tegra_bpmp_ops *ops = channel_to_ops(channel);
113 return ops->is_response_ready(channel);
116 static bool tegra_bpmp_is_request_ready(struct tegra_bpmp_channel *channel)
118 const struct tegra_bpmp_ops *ops = channel_to_ops(channel);
120 return ops->is_request_ready(channel);
123 static int tegra_bpmp_wait_response(struct tegra_bpmp_channel *channel)
125 unsigned long timeout = channel->bpmp->soc->channels.cpu_tx.timeout;
128 end = ktime_add_us(ktime_get(), timeout);
131 if (tegra_bpmp_is_response_ready(channel))
133 } while (ktime_before(ktime_get(), end));
138 static int tegra_bpmp_ack_response(struct tegra_bpmp_channel *channel)
140 const struct tegra_bpmp_ops *ops = channel_to_ops(channel);
142 return ops->ack_response(channel);
145 static int tegra_bpmp_ack_request(struct tegra_bpmp_channel *channel)
147 const struct tegra_bpmp_ops *ops = channel_to_ops(channel);
149 return ops->ack_request(channel);
153 tegra_bpmp_is_request_channel_free(struct tegra_bpmp_channel *channel)
155 const struct tegra_bpmp_ops *ops = channel_to_ops(channel);
157 return ops->is_request_channel_free(channel);
161 tegra_bpmp_is_response_channel_free(struct tegra_bpmp_channel *channel)
163 const struct tegra_bpmp_ops *ops = channel_to_ops(channel);
165 return ops->is_response_channel_free(channel);
169 tegra_bpmp_wait_request_channel_free(struct tegra_bpmp_channel *channel)
171 unsigned long timeout = channel->bpmp->soc->channels.cpu_tx.timeout;
174 start = ns_to_ktime(local_clock());
177 if (tegra_bpmp_is_request_channel_free(channel))
180 now = ns_to_ktime(local_clock());
181 } while (ktime_us_delta(now, start) < timeout);
186 static int tegra_bpmp_post_request(struct tegra_bpmp_channel *channel)
188 const struct tegra_bpmp_ops *ops = channel_to_ops(channel);
190 return ops->post_request(channel);
193 static int tegra_bpmp_post_response(struct tegra_bpmp_channel *channel)
195 const struct tegra_bpmp_ops *ops = channel_to_ops(channel);
197 return ops->post_response(channel);
200 static int tegra_bpmp_ring_doorbell(struct tegra_bpmp *bpmp)
202 return bpmp->soc->ops->ring_doorbell(bpmp);
205 static ssize_t __tegra_bpmp_channel_read(struct tegra_bpmp_channel *channel,
206 void *data, size_t size, int *ret)
210 if (data && size > 0)
211 memcpy(data, channel->ib->data, size);
213 err = tegra_bpmp_ack_response(channel);
217 *ret = channel->ib->code;
222 static ssize_t tegra_bpmp_channel_read(struct tegra_bpmp_channel *channel,
223 void *data, size_t size, int *ret)
225 struct tegra_bpmp *bpmp = channel->bpmp;
230 index = tegra_bpmp_channel_get_thread_index(channel);
236 spin_lock_irqsave(&bpmp->lock, flags);
237 err = __tegra_bpmp_channel_read(channel, data, size, ret);
238 clear_bit(index, bpmp->threaded.allocated);
239 spin_unlock_irqrestore(&bpmp->lock, flags);
242 up(&bpmp->threaded.lock);
247 static ssize_t __tegra_bpmp_channel_write(struct tegra_bpmp_channel *channel,
248 unsigned int mrq, unsigned long flags,
249 const void *data, size_t size)
251 channel->ob->code = mrq;
252 channel->ob->flags = flags;
254 if (data && size > 0)
255 memcpy(channel->ob->data, data, size);
257 return tegra_bpmp_post_request(channel);
260 static struct tegra_bpmp_channel *
261 tegra_bpmp_write_threaded(struct tegra_bpmp *bpmp, unsigned int mrq,
262 const void *data, size_t size)
264 unsigned long timeout = bpmp->soc->channels.thread.timeout;
265 unsigned int count = bpmp->soc->channels.thread.count;
266 struct tegra_bpmp_channel *channel;
271 err = down_timeout(&bpmp->threaded.lock, usecs_to_jiffies(timeout));
275 spin_lock_irqsave(&bpmp->lock, flags);
277 index = find_first_zero_bit(bpmp->threaded.allocated, count);
278 if (index == count) {
283 channel = &bpmp->threaded_channels[index];
285 if (!tegra_bpmp_is_request_channel_free(channel)) {
290 set_bit(index, bpmp->threaded.allocated);
292 err = __tegra_bpmp_channel_write(channel, mrq, MSG_ACK | MSG_RING,
295 goto clear_allocated;
297 set_bit(index, bpmp->threaded.busy);
299 spin_unlock_irqrestore(&bpmp->lock, flags);
303 clear_bit(index, bpmp->threaded.allocated);
305 spin_unlock_irqrestore(&bpmp->lock, flags);
306 up(&bpmp->threaded.lock);
311 static ssize_t tegra_bpmp_channel_write(struct tegra_bpmp_channel *channel,
312 unsigned int mrq, unsigned long flags,
313 const void *data, size_t size)
317 err = tegra_bpmp_wait_request_channel_free(channel);
321 return __tegra_bpmp_channel_write(channel, mrq, flags, data, size);
324 int tegra_bpmp_transfer_atomic(struct tegra_bpmp *bpmp,
325 struct tegra_bpmp_message *msg)
327 struct tegra_bpmp_channel *channel;
330 if (WARN_ON(!irqs_disabled()))
333 if (!tegra_bpmp_message_valid(msg))
336 channel = bpmp->tx_channel;
338 spin_lock(&bpmp->atomic_tx_lock);
340 err = tegra_bpmp_channel_write(channel, msg->mrq, MSG_ACK,
341 msg->tx.data, msg->tx.size);
343 spin_unlock(&bpmp->atomic_tx_lock);
347 spin_unlock(&bpmp->atomic_tx_lock);
349 err = tegra_bpmp_ring_doorbell(bpmp);
353 err = tegra_bpmp_wait_response(channel);
357 return __tegra_bpmp_channel_read(channel, msg->rx.data, msg->rx.size,
360 EXPORT_SYMBOL_GPL(tegra_bpmp_transfer_atomic);
362 int tegra_bpmp_transfer(struct tegra_bpmp *bpmp,
363 struct tegra_bpmp_message *msg)
365 struct tegra_bpmp_channel *channel;
366 unsigned long timeout;
369 if (WARN_ON(irqs_disabled()))
372 if (!tegra_bpmp_message_valid(msg))
375 channel = tegra_bpmp_write_threaded(bpmp, msg->mrq, msg->tx.data,
378 return PTR_ERR(channel);
380 err = tegra_bpmp_ring_doorbell(bpmp);
384 timeout = usecs_to_jiffies(bpmp->soc->channels.thread.timeout);
386 err = wait_for_completion_timeout(&channel->completion, timeout);
390 return tegra_bpmp_channel_read(channel, msg->rx.data, msg->rx.size,
393 EXPORT_SYMBOL_GPL(tegra_bpmp_transfer);
395 static struct tegra_bpmp_mrq *tegra_bpmp_find_mrq(struct tegra_bpmp *bpmp,
398 struct tegra_bpmp_mrq *entry;
400 list_for_each_entry(entry, &bpmp->mrqs, list)
401 if (entry->mrq == mrq)
407 void tegra_bpmp_mrq_return(struct tegra_bpmp_channel *channel, int code,
408 const void *data, size_t size)
410 unsigned long flags = channel->ib->flags;
411 struct tegra_bpmp *bpmp = channel->bpmp;
414 if (WARN_ON(size > MSG_DATA_MIN_SZ))
417 err = tegra_bpmp_ack_request(channel);
418 if (WARN_ON(err < 0))
421 if ((flags & MSG_ACK) == 0)
424 if (WARN_ON(!tegra_bpmp_is_response_channel_free(channel)))
427 channel->ob->code = code;
429 if (data && size > 0)
430 memcpy(channel->ob->data, data, size);
432 err = tegra_bpmp_post_response(channel);
433 if (WARN_ON(err < 0))
436 if (flags & MSG_RING) {
437 err = tegra_bpmp_ring_doorbell(bpmp);
438 if (WARN_ON(err < 0))
442 EXPORT_SYMBOL_GPL(tegra_bpmp_mrq_return);
444 static void tegra_bpmp_handle_mrq(struct tegra_bpmp *bpmp,
446 struct tegra_bpmp_channel *channel)
448 struct tegra_bpmp_mrq *entry;
451 spin_lock(&bpmp->lock);
453 entry = tegra_bpmp_find_mrq(bpmp, mrq);
455 spin_unlock(&bpmp->lock);
456 tegra_bpmp_mrq_return(channel, -EINVAL, &zero, sizeof(zero));
460 entry->handler(mrq, channel, entry->data);
462 spin_unlock(&bpmp->lock);
465 int tegra_bpmp_request_mrq(struct tegra_bpmp *bpmp, unsigned int mrq,
466 tegra_bpmp_mrq_handler_t handler, void *data)
468 struct tegra_bpmp_mrq *entry;
474 entry = devm_kzalloc(bpmp->dev, sizeof(*entry), GFP_KERNEL);
478 spin_lock_irqsave(&bpmp->lock, flags);
481 entry->handler = handler;
483 list_add(&entry->list, &bpmp->mrqs);
485 spin_unlock_irqrestore(&bpmp->lock, flags);
489 EXPORT_SYMBOL_GPL(tegra_bpmp_request_mrq);
491 void tegra_bpmp_free_mrq(struct tegra_bpmp *bpmp, unsigned int mrq, void *data)
493 struct tegra_bpmp_mrq *entry;
496 spin_lock_irqsave(&bpmp->lock, flags);
498 entry = tegra_bpmp_find_mrq(bpmp, mrq);
502 list_del(&entry->list);
503 devm_kfree(bpmp->dev, entry);
506 spin_unlock_irqrestore(&bpmp->lock, flags);
508 EXPORT_SYMBOL_GPL(tegra_bpmp_free_mrq);
510 bool tegra_bpmp_mrq_is_supported(struct tegra_bpmp *bpmp, unsigned int mrq)
512 struct mrq_query_abi_request req = { .mrq = cpu_to_le32(mrq) };
513 struct mrq_query_abi_response resp;
514 struct tegra_bpmp_message msg = {
515 .mrq = MRQ_QUERY_ABI,
522 .size = sizeof(resp),
527 ret = tegra_bpmp_transfer(bpmp, &msg);
528 if (ret || msg.rx.ret)
531 return resp.status == 0;
533 EXPORT_SYMBOL_GPL(tegra_bpmp_mrq_is_supported);
535 static void tegra_bpmp_mrq_handle_ping(unsigned int mrq,
536 struct tegra_bpmp_channel *channel,
539 struct mrq_ping_request *request;
540 struct mrq_ping_response response;
542 request = (struct mrq_ping_request *)channel->ib->data;
544 memset(&response, 0, sizeof(response));
545 response.reply = request->challenge << 1;
547 tegra_bpmp_mrq_return(channel, 0, &response, sizeof(response));
550 static int tegra_bpmp_ping(struct tegra_bpmp *bpmp)
552 struct mrq_ping_response response;
553 struct mrq_ping_request request;
554 struct tegra_bpmp_message msg;
559 memset(&request, 0, sizeof(request));
560 request.challenge = 1;
562 memset(&response, 0, sizeof(response));
564 memset(&msg, 0, sizeof(msg));
566 msg.tx.data = &request;
567 msg.tx.size = sizeof(request);
568 msg.rx.data = &response;
569 msg.rx.size = sizeof(response);
571 local_irq_save(flags);
573 err = tegra_bpmp_transfer_atomic(bpmp, &msg);
575 local_irq_restore(flags);
579 "ping ok: challenge: %u, response: %u, time: %lld\n",
580 request.challenge, response.reply,
581 ktime_to_us(ktime_sub(end, start)));
586 /* deprecated version of tag query */
587 static int tegra_bpmp_get_firmware_tag_old(struct tegra_bpmp *bpmp, char *tag,
590 struct mrq_query_tag_request request;
591 struct tegra_bpmp_message msg;
600 virt = dma_alloc_coherent(bpmp->dev, TAG_SZ, &phys,
601 GFP_KERNEL | GFP_DMA32);
605 memset(&request, 0, sizeof(request));
608 memset(&msg, 0, sizeof(msg));
609 msg.mrq = MRQ_QUERY_TAG;
610 msg.tx.data = &request;
611 msg.tx.size = sizeof(request);
613 local_irq_save(flags);
614 err = tegra_bpmp_transfer_atomic(bpmp, &msg);
615 local_irq_restore(flags);
618 memcpy(tag, virt, TAG_SZ);
620 dma_free_coherent(bpmp->dev, TAG_SZ, virt, phys);
625 static int tegra_bpmp_get_firmware_tag(struct tegra_bpmp *bpmp, char *tag,
628 if (tegra_bpmp_mrq_is_supported(bpmp, MRQ_QUERY_FW_TAG)) {
629 struct mrq_query_fw_tag_response resp;
630 struct tegra_bpmp_message msg = {
631 .mrq = MRQ_QUERY_FW_TAG,
634 .size = sizeof(resp),
639 if (size != sizeof(resp.tag))
642 err = tegra_bpmp_transfer(bpmp, &msg);
649 memcpy(tag, resp.tag, sizeof(resp.tag));
653 return tegra_bpmp_get_firmware_tag_old(bpmp, tag, size);
656 static void tegra_bpmp_channel_signal(struct tegra_bpmp_channel *channel)
658 unsigned long flags = channel->ob->flags;
660 if ((flags & MSG_RING) == 0)
663 complete(&channel->completion);
666 void tegra_bpmp_handle_rx(struct tegra_bpmp *bpmp)
668 struct tegra_bpmp_channel *channel;
669 unsigned int i, count;
672 channel = bpmp->rx_channel;
673 count = bpmp->soc->channels.thread.count;
674 busy = bpmp->threaded.busy;
676 if (tegra_bpmp_is_request_ready(channel))
677 tegra_bpmp_handle_mrq(bpmp, channel->ib->code, channel);
679 spin_lock(&bpmp->lock);
681 for_each_set_bit(i, busy, count) {
682 struct tegra_bpmp_channel *channel;
684 channel = &bpmp->threaded_channels[i];
686 if (tegra_bpmp_is_response_ready(channel)) {
687 tegra_bpmp_channel_signal(channel);
692 spin_unlock(&bpmp->lock);
695 static int tegra_bpmp_probe(struct platform_device *pdev)
697 struct tegra_bpmp *bpmp;
702 bpmp = devm_kzalloc(&pdev->dev, sizeof(*bpmp), GFP_KERNEL);
706 bpmp->soc = of_device_get_match_data(&pdev->dev);
707 bpmp->dev = &pdev->dev;
709 INIT_LIST_HEAD(&bpmp->mrqs);
710 spin_lock_init(&bpmp->lock);
712 bpmp->threaded.count = bpmp->soc->channels.thread.count;
713 sema_init(&bpmp->threaded.lock, bpmp->threaded.count);
715 size = BITS_TO_LONGS(bpmp->threaded.count) * sizeof(long);
717 bpmp->threaded.allocated = devm_kzalloc(&pdev->dev, size, GFP_KERNEL);
718 if (!bpmp->threaded.allocated)
721 bpmp->threaded.busy = devm_kzalloc(&pdev->dev, size, GFP_KERNEL);
722 if (!bpmp->threaded.busy)
725 spin_lock_init(&bpmp->atomic_tx_lock);
726 bpmp->tx_channel = devm_kzalloc(&pdev->dev, sizeof(*bpmp->tx_channel),
728 if (!bpmp->tx_channel)
731 bpmp->rx_channel = devm_kzalloc(&pdev->dev, sizeof(*bpmp->rx_channel),
733 if (!bpmp->rx_channel)
736 bpmp->threaded_channels = devm_kcalloc(&pdev->dev, bpmp->threaded.count,
737 sizeof(*bpmp->threaded_channels),
739 if (!bpmp->threaded_channels)
742 err = bpmp->soc->ops->init(bpmp);
746 err = tegra_bpmp_request_mrq(bpmp, MRQ_PING,
747 tegra_bpmp_mrq_handle_ping, bpmp);
751 err = tegra_bpmp_ping(bpmp);
753 dev_err(&pdev->dev, "failed to ping BPMP: %d\n", err);
757 err = tegra_bpmp_get_firmware_tag(bpmp, tag, sizeof(tag));
759 dev_err(&pdev->dev, "failed to get firmware tag: %d\n", err);
763 dev_info(&pdev->dev, "firmware: %.*s\n", (int)sizeof(tag), tag);
765 platform_set_drvdata(pdev, bpmp);
767 err = of_platform_default_populate(pdev->dev.of_node, NULL, &pdev->dev);
771 if (of_find_property(pdev->dev.of_node, "#clock-cells", NULL)) {
772 err = tegra_bpmp_init_clocks(bpmp);
777 if (of_find_property(pdev->dev.of_node, "#reset-cells", NULL)) {
778 err = tegra_bpmp_init_resets(bpmp);
783 if (of_find_property(pdev->dev.of_node, "#power-domain-cells", NULL)) {
784 err = tegra_bpmp_init_powergates(bpmp);
789 err = tegra_bpmp_init_debugfs(bpmp);
791 dev_err(&pdev->dev, "debugfs initialization failed: %d\n", err);
796 tegra_bpmp_free_mrq(bpmp, MRQ_PING, bpmp);
798 if (bpmp->soc->ops->deinit)
799 bpmp->soc->ops->deinit(bpmp);
804 static int __maybe_unused tegra_bpmp_resume(struct device *dev)
806 struct tegra_bpmp *bpmp = dev_get_drvdata(dev);
808 if (bpmp->soc->ops->resume)
809 return bpmp->soc->ops->resume(bpmp);
814 static SIMPLE_DEV_PM_OPS(tegra_bpmp_pm_ops, NULL, tegra_bpmp_resume);
816 #if IS_ENABLED(CONFIG_ARCH_TEGRA_186_SOC) || \
817 IS_ENABLED(CONFIG_ARCH_TEGRA_194_SOC)
818 static const struct tegra_bpmp_soc tegra186_soc = {
822 .timeout = 60 * USEC_PER_SEC,
827 .timeout = 600 * USEC_PER_SEC,
834 .ops = &tegra186_bpmp_ops,
839 #if IS_ENABLED(CONFIG_ARCH_TEGRA_210_SOC)
840 static const struct tegra_bpmp_soc tegra210_soc = {
845 .timeout = 60 * USEC_PER_SEC,
850 .timeout = 600 * USEC_PER_SEC,
858 .ops = &tegra210_bpmp_ops,
862 static const struct of_device_id tegra_bpmp_match[] = {
863 #if IS_ENABLED(CONFIG_ARCH_TEGRA_186_SOC) || \
864 IS_ENABLED(CONFIG_ARCH_TEGRA_194_SOC)
865 { .compatible = "nvidia,tegra186-bpmp", .data = &tegra186_soc },
867 #if IS_ENABLED(CONFIG_ARCH_TEGRA_210_SOC)
868 { .compatible = "nvidia,tegra210-bpmp", .data = &tegra210_soc },
873 static struct platform_driver tegra_bpmp_driver = {
875 .name = "tegra-bpmp",
876 .of_match_table = tegra_bpmp_match,
877 .pm = &tegra_bpmp_pm_ops,
879 .probe = tegra_bpmp_probe,
882 static int __init tegra_bpmp_init(void)
884 return platform_driver_register(&tegra_bpmp_driver);
886 core_initcall(tegra_bpmp_init);