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>
21 #include <linux/semaphore.h>
22 #include <linux/sched/clock.h>
24 #include <soc/tegra/bpmp.h>
25 #include <soc/tegra/bpmp-abi.h>
26 #include <soc/tegra/ivc.h>
28 #define MSG_ACK BIT(0)
29 #define MSG_RING BIT(1)
31 static inline struct tegra_bpmp *
32 mbox_client_to_bpmp(struct mbox_client *client)
34 return container_of(client, struct tegra_bpmp, mbox.client);
37 struct tegra_bpmp *tegra_bpmp_get(struct device *dev)
39 struct platform_device *pdev;
40 struct tegra_bpmp *bpmp;
41 struct device_node *np;
43 np = of_parse_phandle(dev->of_node, "nvidia,bpmp", 0);
45 return ERR_PTR(-ENOENT);
47 pdev = of_find_device_by_node(np);
49 bpmp = ERR_PTR(-ENODEV);
53 bpmp = platform_get_drvdata(pdev);
55 bpmp = ERR_PTR(-EPROBE_DEFER);
56 put_device(&pdev->dev);
64 EXPORT_SYMBOL_GPL(tegra_bpmp_get);
66 void tegra_bpmp_put(struct tegra_bpmp *bpmp)
69 put_device(bpmp->dev);
71 EXPORT_SYMBOL_GPL(tegra_bpmp_put);
74 tegra_bpmp_channel_get_thread_index(struct tegra_bpmp_channel *channel)
76 struct tegra_bpmp *bpmp = channel->bpmp;
80 count = bpmp->soc->channels.thread.count;
82 index = channel - channel->bpmp->threaded_channels;
83 if (index < 0 || index >= count)
89 static bool tegra_bpmp_message_valid(const struct tegra_bpmp_message *msg)
91 return (msg->tx.size <= MSG_DATA_MIN_SZ) &&
92 (msg->rx.size <= MSG_DATA_MIN_SZ) &&
93 (msg->tx.size == 0 || msg->tx.data) &&
94 (msg->rx.size == 0 || msg->rx.data);
97 static bool tegra_bpmp_master_acked(struct tegra_bpmp_channel *channel)
101 frame = tegra_ivc_read_get_next_frame(channel->ivc);
112 static int tegra_bpmp_wait_ack(struct tegra_bpmp_channel *channel)
114 unsigned long timeout = channel->bpmp->soc->channels.cpu_tx.timeout;
117 end = ktime_add_us(ktime_get(), timeout);
120 if (tegra_bpmp_master_acked(channel))
122 } while (ktime_before(ktime_get(), end));
127 static bool tegra_bpmp_master_free(struct tegra_bpmp_channel *channel)
131 frame = tegra_ivc_write_get_next_frame(channel->ivc);
142 static int tegra_bpmp_wait_master_free(struct tegra_bpmp_channel *channel)
144 unsigned long timeout = channel->bpmp->soc->channels.cpu_tx.timeout;
147 start = ns_to_ktime(local_clock());
150 if (tegra_bpmp_master_free(channel))
153 now = ns_to_ktime(local_clock());
154 } while (ktime_us_delta(now, start) < timeout);
159 static ssize_t __tegra_bpmp_channel_read(struct tegra_bpmp_channel *channel,
160 void *data, size_t size, int *ret)
164 if (data && size > 0)
165 memcpy(data, channel->ib->data, size);
167 err = tegra_ivc_read_advance(channel->ivc);
171 *ret = channel->ib->code;
176 static ssize_t tegra_bpmp_channel_read(struct tegra_bpmp_channel *channel,
177 void *data, size_t size, int *ret)
179 struct tegra_bpmp *bpmp = channel->bpmp;
184 index = tegra_bpmp_channel_get_thread_index(channel);
190 spin_lock_irqsave(&bpmp->lock, flags);
191 err = __tegra_bpmp_channel_read(channel, data, size, ret);
192 clear_bit(index, bpmp->threaded.allocated);
193 spin_unlock_irqrestore(&bpmp->lock, flags);
196 up(&bpmp->threaded.lock);
201 static ssize_t __tegra_bpmp_channel_write(struct tegra_bpmp_channel *channel,
202 unsigned int mrq, unsigned long flags,
203 const void *data, size_t size)
205 channel->ob->code = mrq;
206 channel->ob->flags = flags;
208 if (data && size > 0)
209 memcpy(channel->ob->data, data, size);
211 return tegra_ivc_write_advance(channel->ivc);
214 static struct tegra_bpmp_channel *
215 tegra_bpmp_write_threaded(struct tegra_bpmp *bpmp, unsigned int mrq,
216 const void *data, size_t size)
218 unsigned long timeout = bpmp->soc->channels.thread.timeout;
219 unsigned int count = bpmp->soc->channels.thread.count;
220 struct tegra_bpmp_channel *channel;
225 err = down_timeout(&bpmp->threaded.lock, usecs_to_jiffies(timeout));
229 spin_lock_irqsave(&bpmp->lock, flags);
231 index = find_first_zero_bit(bpmp->threaded.allocated, count);
232 if (index == count) {
237 channel = &bpmp->threaded_channels[index];
239 if (!tegra_bpmp_master_free(channel)) {
244 set_bit(index, bpmp->threaded.allocated);
246 err = __tegra_bpmp_channel_write(channel, mrq, MSG_ACK | MSG_RING,
249 goto clear_allocated;
251 set_bit(index, bpmp->threaded.busy);
253 spin_unlock_irqrestore(&bpmp->lock, flags);
257 clear_bit(index, bpmp->threaded.allocated);
259 spin_unlock_irqrestore(&bpmp->lock, flags);
260 up(&bpmp->threaded.lock);
265 static ssize_t tegra_bpmp_channel_write(struct tegra_bpmp_channel *channel,
266 unsigned int mrq, unsigned long flags,
267 const void *data, size_t size)
271 err = tegra_bpmp_wait_master_free(channel);
275 return __tegra_bpmp_channel_write(channel, mrq, flags, data, size);
278 int tegra_bpmp_transfer_atomic(struct tegra_bpmp *bpmp,
279 struct tegra_bpmp_message *msg)
281 struct tegra_bpmp_channel *channel;
284 if (WARN_ON(!irqs_disabled()))
287 if (!tegra_bpmp_message_valid(msg))
290 channel = bpmp->tx_channel;
292 spin_lock(&bpmp->atomic_tx_lock);
294 err = tegra_bpmp_channel_write(channel, msg->mrq, MSG_ACK,
295 msg->tx.data, msg->tx.size);
297 spin_unlock(&bpmp->atomic_tx_lock);
301 spin_unlock(&bpmp->atomic_tx_lock);
303 err = mbox_send_message(bpmp->mbox.channel, NULL);
307 mbox_client_txdone(bpmp->mbox.channel, 0);
309 err = tegra_bpmp_wait_ack(channel);
313 return __tegra_bpmp_channel_read(channel, msg->rx.data, msg->rx.size,
316 EXPORT_SYMBOL_GPL(tegra_bpmp_transfer_atomic);
318 int tegra_bpmp_transfer(struct tegra_bpmp *bpmp,
319 struct tegra_bpmp_message *msg)
321 struct tegra_bpmp_channel *channel;
322 unsigned long timeout;
325 if (WARN_ON(irqs_disabled()))
328 if (!tegra_bpmp_message_valid(msg))
331 channel = tegra_bpmp_write_threaded(bpmp, msg->mrq, msg->tx.data,
334 return PTR_ERR(channel);
336 err = mbox_send_message(bpmp->mbox.channel, NULL);
340 mbox_client_txdone(bpmp->mbox.channel, 0);
342 timeout = usecs_to_jiffies(bpmp->soc->channels.thread.timeout);
344 err = wait_for_completion_timeout(&channel->completion, timeout);
348 return tegra_bpmp_channel_read(channel, msg->rx.data, msg->rx.size,
351 EXPORT_SYMBOL_GPL(tegra_bpmp_transfer);
353 static struct tegra_bpmp_mrq *tegra_bpmp_find_mrq(struct tegra_bpmp *bpmp,
356 struct tegra_bpmp_mrq *entry;
358 list_for_each_entry(entry, &bpmp->mrqs, list)
359 if (entry->mrq == mrq)
365 void tegra_bpmp_mrq_return(struct tegra_bpmp_channel *channel, int code,
366 const void *data, size_t size)
368 unsigned long flags = channel->ib->flags;
369 struct tegra_bpmp *bpmp = channel->bpmp;
370 struct tegra_bpmp_mb_data *frame;
373 if (WARN_ON(size > MSG_DATA_MIN_SZ))
376 err = tegra_ivc_read_advance(channel->ivc);
377 if (WARN_ON(err < 0))
380 if ((flags & MSG_ACK) == 0)
383 frame = tegra_ivc_write_get_next_frame(channel->ivc);
384 if (WARN_ON(IS_ERR(frame)))
389 if (data && size > 0)
390 memcpy(frame->data, data, size);
392 err = tegra_ivc_write_advance(channel->ivc);
393 if (WARN_ON(err < 0))
396 if (flags & MSG_RING) {
397 err = mbox_send_message(bpmp->mbox.channel, NULL);
398 if (WARN_ON(err < 0))
401 mbox_client_txdone(bpmp->mbox.channel, 0);
404 EXPORT_SYMBOL_GPL(tegra_bpmp_mrq_return);
406 static void tegra_bpmp_handle_mrq(struct tegra_bpmp *bpmp,
408 struct tegra_bpmp_channel *channel)
410 struct tegra_bpmp_mrq *entry;
413 spin_lock(&bpmp->lock);
415 entry = tegra_bpmp_find_mrq(bpmp, mrq);
417 spin_unlock(&bpmp->lock);
418 tegra_bpmp_mrq_return(channel, -EINVAL, &zero, sizeof(zero));
422 entry->handler(mrq, channel, entry->data);
424 spin_unlock(&bpmp->lock);
427 int tegra_bpmp_request_mrq(struct tegra_bpmp *bpmp, unsigned int mrq,
428 tegra_bpmp_mrq_handler_t handler, void *data)
430 struct tegra_bpmp_mrq *entry;
436 entry = devm_kzalloc(bpmp->dev, sizeof(*entry), GFP_KERNEL);
440 spin_lock_irqsave(&bpmp->lock, flags);
443 entry->handler = handler;
445 list_add(&entry->list, &bpmp->mrqs);
447 spin_unlock_irqrestore(&bpmp->lock, flags);
451 EXPORT_SYMBOL_GPL(tegra_bpmp_request_mrq);
453 void tegra_bpmp_free_mrq(struct tegra_bpmp *bpmp, unsigned int mrq, void *data)
455 struct tegra_bpmp_mrq *entry;
458 spin_lock_irqsave(&bpmp->lock, flags);
460 entry = tegra_bpmp_find_mrq(bpmp, mrq);
464 list_del(&entry->list);
465 devm_kfree(bpmp->dev, entry);
468 spin_unlock_irqrestore(&bpmp->lock, flags);
470 EXPORT_SYMBOL_GPL(tegra_bpmp_free_mrq);
472 static void tegra_bpmp_mrq_handle_ping(unsigned int mrq,
473 struct tegra_bpmp_channel *channel,
476 struct mrq_ping_request *request;
477 struct mrq_ping_response response;
479 request = (struct mrq_ping_request *)channel->ib->data;
481 memset(&response, 0, sizeof(response));
482 response.reply = request->challenge << 1;
484 tegra_bpmp_mrq_return(channel, 0, &response, sizeof(response));
487 static int tegra_bpmp_ping(struct tegra_bpmp *bpmp)
489 struct mrq_ping_response response;
490 struct mrq_ping_request request;
491 struct tegra_bpmp_message msg;
496 memset(&request, 0, sizeof(request));
497 request.challenge = 1;
499 memset(&response, 0, sizeof(response));
501 memset(&msg, 0, sizeof(msg));
503 msg.tx.data = &request;
504 msg.tx.size = sizeof(request);
505 msg.rx.data = &response;
506 msg.rx.size = sizeof(response);
508 local_irq_save(flags);
510 err = tegra_bpmp_transfer_atomic(bpmp, &msg);
512 local_irq_restore(flags);
516 "ping ok: challenge: %u, response: %u, time: %lld\n",
517 request.challenge, response.reply,
518 ktime_to_us(ktime_sub(end, start)));
523 static int tegra_bpmp_get_firmware_tag(struct tegra_bpmp *bpmp, char *tag,
526 struct mrq_query_tag_request request;
527 struct tegra_bpmp_message msg;
533 virt = dma_alloc_coherent(bpmp->dev, MSG_DATA_MIN_SZ, &phys,
534 GFP_KERNEL | GFP_DMA32);
538 memset(&request, 0, sizeof(request));
541 memset(&msg, 0, sizeof(msg));
542 msg.mrq = MRQ_QUERY_TAG;
543 msg.tx.data = &request;
544 msg.tx.size = sizeof(request);
546 local_irq_save(flags);
547 err = tegra_bpmp_transfer_atomic(bpmp, &msg);
548 local_irq_restore(flags);
551 strlcpy(tag, virt, size);
553 dma_free_coherent(bpmp->dev, MSG_DATA_MIN_SZ, virt, phys);
558 static void tegra_bpmp_channel_signal(struct tegra_bpmp_channel *channel)
560 unsigned long flags = channel->ob->flags;
562 if ((flags & MSG_RING) == 0)
565 complete(&channel->completion);
568 static void tegra_bpmp_handle_rx(struct mbox_client *client, void *data)
570 struct tegra_bpmp *bpmp = mbox_client_to_bpmp(client);
571 struct tegra_bpmp_channel *channel;
572 unsigned int i, count;
575 channel = bpmp->rx_channel;
576 count = bpmp->soc->channels.thread.count;
577 busy = bpmp->threaded.busy;
579 if (tegra_bpmp_master_acked(channel))
580 tegra_bpmp_handle_mrq(bpmp, channel->ib->code, channel);
582 spin_lock(&bpmp->lock);
584 for_each_set_bit(i, busy, count) {
585 struct tegra_bpmp_channel *channel;
587 channel = &bpmp->threaded_channels[i];
589 if (tegra_bpmp_master_acked(channel)) {
590 tegra_bpmp_channel_signal(channel);
595 spin_unlock(&bpmp->lock);
598 static void tegra_bpmp_ivc_notify(struct tegra_ivc *ivc, void *data)
600 struct tegra_bpmp *bpmp = data;
603 if (WARN_ON(bpmp->mbox.channel == NULL))
606 err = mbox_send_message(bpmp->mbox.channel, NULL);
610 mbox_client_txdone(bpmp->mbox.channel, 0);
613 static int tegra_bpmp_channel_init(struct tegra_bpmp_channel *channel,
614 struct tegra_bpmp *bpmp,
617 size_t message_size, queue_size;
621 channel->ivc = devm_kzalloc(bpmp->dev, sizeof(*channel->ivc),
626 message_size = tegra_ivc_align(MSG_MIN_SZ);
627 queue_size = tegra_ivc_total_queue_size(message_size);
628 offset = queue_size * index;
630 err = tegra_ivc_init(channel->ivc, NULL,
631 bpmp->rx.virt + offset, bpmp->rx.phys + offset,
632 bpmp->tx.virt + offset, bpmp->tx.phys + offset,
633 1, message_size, tegra_bpmp_ivc_notify,
636 dev_err(bpmp->dev, "failed to setup IVC for channel %u: %d\n",
641 init_completion(&channel->completion);
642 channel->bpmp = bpmp;
647 static void tegra_bpmp_channel_reset(struct tegra_bpmp_channel *channel)
649 /* reset the channel state */
650 tegra_ivc_reset(channel->ivc);
652 /* sync the channel state with BPMP */
653 while (tegra_ivc_notified(channel->ivc))
657 static void tegra_bpmp_channel_cleanup(struct tegra_bpmp_channel *channel)
659 tegra_ivc_cleanup(channel->ivc);
662 static int tegra_bpmp_probe(struct platform_device *pdev)
664 struct tegra_bpmp *bpmp;
670 bpmp = devm_kzalloc(&pdev->dev, sizeof(*bpmp), GFP_KERNEL);
674 bpmp->soc = of_device_get_match_data(&pdev->dev);
675 bpmp->dev = &pdev->dev;
677 bpmp->tx.pool = of_gen_pool_get(pdev->dev.of_node, "shmem", 0);
678 if (!bpmp->tx.pool) {
679 dev_err(&pdev->dev, "TX shmem pool not found\n");
683 bpmp->tx.virt = gen_pool_dma_alloc(bpmp->tx.pool, 4096, &bpmp->tx.phys);
684 if (!bpmp->tx.virt) {
685 dev_err(&pdev->dev, "failed to allocate from TX pool\n");
689 bpmp->rx.pool = of_gen_pool_get(pdev->dev.of_node, "shmem", 1);
690 if (!bpmp->rx.pool) {
691 dev_err(&pdev->dev, "RX shmem pool not found\n");
696 bpmp->rx.virt = gen_pool_dma_alloc(bpmp->rx.pool, 4096, &bpmp->rx.phys);
697 if (!bpmp->rx.virt) {
698 dev_err(&pdev->dev, "failed to allocate from RX pool\n");
703 INIT_LIST_HEAD(&bpmp->mrqs);
704 spin_lock_init(&bpmp->lock);
706 bpmp->threaded.count = bpmp->soc->channels.thread.count;
707 sema_init(&bpmp->threaded.lock, bpmp->threaded.count);
709 size = BITS_TO_LONGS(bpmp->threaded.count) * sizeof(long);
711 bpmp->threaded.allocated = devm_kzalloc(&pdev->dev, size, GFP_KERNEL);
712 if (!bpmp->threaded.allocated) {
717 bpmp->threaded.busy = devm_kzalloc(&pdev->dev, size, GFP_KERNEL);
718 if (!bpmp->threaded.busy) {
723 spin_lock_init(&bpmp->atomic_tx_lock);
724 bpmp->tx_channel = devm_kzalloc(&pdev->dev, sizeof(*bpmp->tx_channel),
726 if (!bpmp->tx_channel) {
731 bpmp->rx_channel = devm_kzalloc(&pdev->dev, sizeof(*bpmp->rx_channel),
733 if (!bpmp->rx_channel) {
738 bpmp->threaded_channels = devm_kcalloc(&pdev->dev, bpmp->threaded.count,
739 sizeof(*bpmp->threaded_channels),
741 if (!bpmp->threaded_channels) {
746 err = tegra_bpmp_channel_init(bpmp->tx_channel, bpmp,
747 bpmp->soc->channels.cpu_tx.offset);
751 err = tegra_bpmp_channel_init(bpmp->rx_channel, bpmp,
752 bpmp->soc->channels.cpu_rx.offset);
754 goto cleanup_tx_channel;
756 for (i = 0; i < bpmp->threaded.count; i++) {
757 err = tegra_bpmp_channel_init(
758 &bpmp->threaded_channels[i], bpmp,
759 bpmp->soc->channels.thread.offset + i);
761 goto cleanup_threaded_channels;
764 /* mbox registration */
765 bpmp->mbox.client.dev = &pdev->dev;
766 bpmp->mbox.client.rx_callback = tegra_bpmp_handle_rx;
767 bpmp->mbox.client.tx_block = false;
768 bpmp->mbox.client.knows_txdone = false;
770 bpmp->mbox.channel = mbox_request_channel(&bpmp->mbox.client, 0);
771 if (IS_ERR(bpmp->mbox.channel)) {
772 err = PTR_ERR(bpmp->mbox.channel);
773 dev_err(&pdev->dev, "failed to get HSP mailbox: %d\n", err);
774 goto cleanup_threaded_channels;
777 /* reset message channels */
778 tegra_bpmp_channel_reset(bpmp->tx_channel);
779 tegra_bpmp_channel_reset(bpmp->rx_channel);
780 for (i = 0; i < bpmp->threaded.count; i++)
781 tegra_bpmp_channel_reset(&bpmp->threaded_channels[i]);
783 err = tegra_bpmp_request_mrq(bpmp, MRQ_PING,
784 tegra_bpmp_mrq_handle_ping, bpmp);
788 err = tegra_bpmp_ping(bpmp);
790 dev_err(&pdev->dev, "failed to ping BPMP: %d\n", err);
794 err = tegra_bpmp_get_firmware_tag(bpmp, tag, sizeof(tag) - 1);
796 dev_err(&pdev->dev, "failed to get firmware tag: %d\n", err);
800 dev_info(&pdev->dev, "firmware: %s\n", tag);
802 platform_set_drvdata(pdev, bpmp);
804 err = of_platform_default_populate(pdev->dev.of_node, NULL, &pdev->dev);
808 err = tegra_bpmp_init_clocks(bpmp);
812 err = tegra_bpmp_init_resets(bpmp);
816 err = tegra_bpmp_init_powergates(bpmp);
820 err = tegra_bpmp_init_debugfs(bpmp);
822 dev_err(&pdev->dev, "debugfs initialization failed: %d\n", err);
827 tegra_bpmp_free_mrq(bpmp, MRQ_PING, bpmp);
829 mbox_free_channel(bpmp->mbox.channel);
830 cleanup_threaded_channels:
831 for (i = 0; i < bpmp->threaded.count; i++) {
832 if (bpmp->threaded_channels[i].bpmp)
833 tegra_bpmp_channel_cleanup(&bpmp->threaded_channels[i]);
836 tegra_bpmp_channel_cleanup(bpmp->rx_channel);
838 tegra_bpmp_channel_cleanup(bpmp->tx_channel);
840 gen_pool_free(bpmp->rx.pool, (unsigned long)bpmp->rx.virt, 4096);
842 gen_pool_free(bpmp->tx.pool, (unsigned long)bpmp->tx.virt, 4096);
846 static const struct tegra_bpmp_soc tegra186_soc = {
850 .timeout = 60 * USEC_PER_SEC,
855 .timeout = 600 * USEC_PER_SEC,
865 static const struct of_device_id tegra_bpmp_match[] = {
866 { .compatible = "nvidia,tegra186-bpmp", .data = &tegra186_soc },
870 static struct platform_driver tegra_bpmp_driver = {
872 .name = "tegra-bpmp",
873 .of_match_table = tegra_bpmp_match,
875 .probe = tegra_bpmp_probe,
878 static int __init tegra_bpmp_init(void)
880 return platform_driver_register(&tegra_bpmp_driver);
882 core_initcall(tegra_bpmp_init);