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);
73 static int tegra_bpmp_channel_get_index(struct tegra_bpmp_channel *channel)
75 return channel - channel->bpmp->channels;
79 tegra_bpmp_channel_get_thread_index(struct tegra_bpmp_channel *channel)
81 struct tegra_bpmp *bpmp = channel->bpmp;
82 unsigned int offset, count;
85 offset = bpmp->soc->channels.thread.offset;
86 count = bpmp->soc->channels.thread.count;
88 index = tegra_bpmp_channel_get_index(channel);
92 if (index < offset || index >= offset + count)
95 return index - offset;
98 static struct tegra_bpmp_channel *
99 tegra_bpmp_channel_get_thread(struct tegra_bpmp *bpmp, unsigned int index)
101 unsigned int offset = bpmp->soc->channels.thread.offset;
102 unsigned int count = bpmp->soc->channels.thread.count;
107 return &bpmp->channels[offset + index];
110 static struct tegra_bpmp_channel *
111 tegra_bpmp_channel_get_tx(struct tegra_bpmp *bpmp)
113 unsigned int offset = bpmp->soc->channels.cpu_tx.offset;
115 return &bpmp->channels[offset + smp_processor_id()];
118 static struct tegra_bpmp_channel *
119 tegra_bpmp_channel_get_rx(struct tegra_bpmp *bpmp)
121 unsigned int offset = bpmp->soc->channels.cpu_rx.offset;
123 return &bpmp->channels[offset];
126 static bool tegra_bpmp_message_valid(const struct tegra_bpmp_message *msg)
128 return (msg->tx.size <= MSG_DATA_MIN_SZ) &&
129 (msg->rx.size <= MSG_DATA_MIN_SZ) &&
130 (msg->tx.size == 0 || msg->tx.data) &&
131 (msg->rx.size == 0 || msg->rx.data);
134 static bool tegra_bpmp_master_acked(struct tegra_bpmp_channel *channel)
138 frame = tegra_ivc_read_get_next_frame(channel->ivc);
149 static int tegra_bpmp_wait_ack(struct tegra_bpmp_channel *channel)
151 unsigned long timeout = channel->bpmp->soc->channels.cpu_tx.timeout;
154 end = ktime_add_us(ktime_get(), timeout);
157 if (tegra_bpmp_master_acked(channel))
159 } while (ktime_before(ktime_get(), end));
164 static bool tegra_bpmp_master_free(struct tegra_bpmp_channel *channel)
168 frame = tegra_ivc_write_get_next_frame(channel->ivc);
179 static int tegra_bpmp_wait_master_free(struct tegra_bpmp_channel *channel)
181 unsigned long timeout = channel->bpmp->soc->channels.cpu_tx.timeout;
184 start = ns_to_ktime(local_clock());
187 if (tegra_bpmp_master_free(channel))
190 now = ns_to_ktime(local_clock());
191 } while (ktime_us_delta(now, start) < timeout);
196 static ssize_t __tegra_bpmp_channel_read(struct tegra_bpmp_channel *channel,
197 void *data, size_t size)
199 if (data && size > 0)
200 memcpy(data, channel->ib->data, size);
202 return tegra_ivc_read_advance(channel->ivc);
205 static ssize_t tegra_bpmp_channel_read(struct tegra_bpmp_channel *channel,
206 void *data, size_t size)
208 struct tegra_bpmp *bpmp = channel->bpmp;
213 index = tegra_bpmp_channel_get_thread_index(channel);
217 spin_lock_irqsave(&bpmp->lock, flags);
218 err = __tegra_bpmp_channel_read(channel, data, size);
219 clear_bit(index, bpmp->threaded.allocated);
220 spin_unlock_irqrestore(&bpmp->lock, flags);
222 up(&bpmp->threaded.lock);
227 static ssize_t __tegra_bpmp_channel_write(struct tegra_bpmp_channel *channel,
228 unsigned int mrq, unsigned long flags,
229 const void *data, size_t size)
231 channel->ob->code = mrq;
232 channel->ob->flags = flags;
234 if (data && size > 0)
235 memcpy(channel->ob->data, data, size);
237 return tegra_ivc_write_advance(channel->ivc);
240 static struct tegra_bpmp_channel *
241 tegra_bpmp_write_threaded(struct tegra_bpmp *bpmp, unsigned int mrq,
242 const void *data, size_t size)
244 unsigned long timeout = bpmp->soc->channels.thread.timeout;
245 unsigned int count = bpmp->soc->channels.thread.count;
246 struct tegra_bpmp_channel *channel;
251 err = down_timeout(&bpmp->threaded.lock, usecs_to_jiffies(timeout));
255 spin_lock_irqsave(&bpmp->lock, flags);
257 index = find_first_zero_bit(bpmp->threaded.allocated, count);
258 if (index == count) {
259 channel = ERR_PTR(-EBUSY);
263 channel = tegra_bpmp_channel_get_thread(bpmp, index);
265 channel = ERR_PTR(-EINVAL);
269 if (!tegra_bpmp_master_free(channel)) {
270 channel = ERR_PTR(-EBUSY);
274 set_bit(index, bpmp->threaded.allocated);
276 err = __tegra_bpmp_channel_write(channel, mrq, MSG_ACK | MSG_RING,
279 clear_bit(index, bpmp->threaded.allocated);
283 set_bit(index, bpmp->threaded.busy);
286 spin_unlock_irqrestore(&bpmp->lock, flags);
290 static ssize_t tegra_bpmp_channel_write(struct tegra_bpmp_channel *channel,
291 unsigned int mrq, unsigned long flags,
292 const void *data, size_t size)
296 err = tegra_bpmp_wait_master_free(channel);
300 return __tegra_bpmp_channel_write(channel, mrq, flags, data, size);
303 int tegra_bpmp_transfer_atomic(struct tegra_bpmp *bpmp,
304 struct tegra_bpmp_message *msg)
306 struct tegra_bpmp_channel *channel;
309 if (WARN_ON(!irqs_disabled()))
312 if (!tegra_bpmp_message_valid(msg))
315 channel = tegra_bpmp_channel_get_tx(bpmp);
317 err = tegra_bpmp_channel_write(channel, msg->mrq, MSG_ACK,
318 msg->tx.data, msg->tx.size);
322 err = mbox_send_message(bpmp->mbox.channel, NULL);
326 mbox_client_txdone(bpmp->mbox.channel, 0);
328 err = tegra_bpmp_wait_ack(channel);
332 return __tegra_bpmp_channel_read(channel, msg->rx.data, msg->rx.size);
334 EXPORT_SYMBOL_GPL(tegra_bpmp_transfer_atomic);
336 int tegra_bpmp_transfer(struct tegra_bpmp *bpmp,
337 struct tegra_bpmp_message *msg)
339 struct tegra_bpmp_channel *channel;
340 unsigned long timeout;
343 if (WARN_ON(irqs_disabled()))
346 if (!tegra_bpmp_message_valid(msg))
349 channel = tegra_bpmp_write_threaded(bpmp, msg->mrq, msg->tx.data,
352 return PTR_ERR(channel);
354 err = mbox_send_message(bpmp->mbox.channel, NULL);
358 mbox_client_txdone(bpmp->mbox.channel, 0);
360 timeout = usecs_to_jiffies(bpmp->soc->channels.thread.timeout);
362 err = wait_for_completion_timeout(&channel->completion, timeout);
366 return tegra_bpmp_channel_read(channel, msg->rx.data, msg->rx.size);
368 EXPORT_SYMBOL_GPL(tegra_bpmp_transfer);
370 static struct tegra_bpmp_mrq *tegra_bpmp_find_mrq(struct tegra_bpmp *bpmp,
373 struct tegra_bpmp_mrq *entry;
375 list_for_each_entry(entry, &bpmp->mrqs, list)
376 if (entry->mrq == mrq)
382 static void tegra_bpmp_mrq_return(struct tegra_bpmp_channel *channel,
383 int code, const void *data, size_t size)
385 unsigned long flags = channel->ib->flags;
386 struct tegra_bpmp *bpmp = channel->bpmp;
387 struct tegra_bpmp_mb_data *frame;
390 if (WARN_ON(size > MSG_DATA_MIN_SZ))
393 err = tegra_ivc_read_advance(channel->ivc);
394 if (WARN_ON(err < 0))
397 if ((flags & MSG_ACK) == 0)
400 frame = tegra_ivc_write_get_next_frame(channel->ivc);
401 if (WARN_ON(IS_ERR(frame)))
406 if (data && size > 0)
407 memcpy(frame->data, data, size);
409 err = tegra_ivc_write_advance(channel->ivc);
410 if (WARN_ON(err < 0))
413 if (flags & MSG_RING) {
414 err = mbox_send_message(bpmp->mbox.channel, NULL);
415 if (WARN_ON(err < 0))
418 mbox_client_txdone(bpmp->mbox.channel, 0);
422 static void tegra_bpmp_handle_mrq(struct tegra_bpmp *bpmp,
424 struct tegra_bpmp_channel *channel)
426 struct tegra_bpmp_mrq *entry;
429 spin_lock(&bpmp->lock);
431 entry = tegra_bpmp_find_mrq(bpmp, mrq);
433 spin_unlock(&bpmp->lock);
434 tegra_bpmp_mrq_return(channel, -EINVAL, &zero, sizeof(zero));
438 entry->handler(mrq, channel, entry->data);
440 spin_unlock(&bpmp->lock);
443 int tegra_bpmp_request_mrq(struct tegra_bpmp *bpmp, unsigned int mrq,
444 tegra_bpmp_mrq_handler_t handler, void *data)
446 struct tegra_bpmp_mrq *entry;
452 entry = devm_kzalloc(bpmp->dev, sizeof(*entry), GFP_KERNEL);
456 spin_lock_irqsave(&bpmp->lock, flags);
459 entry->handler = handler;
461 list_add(&entry->list, &bpmp->mrqs);
463 spin_unlock_irqrestore(&bpmp->lock, flags);
467 EXPORT_SYMBOL_GPL(tegra_bpmp_request_mrq);
469 void tegra_bpmp_free_mrq(struct tegra_bpmp *bpmp, unsigned int mrq, void *data)
471 struct tegra_bpmp_mrq *entry;
474 spin_lock_irqsave(&bpmp->lock, flags);
476 entry = tegra_bpmp_find_mrq(bpmp, mrq);
480 list_del(&entry->list);
481 devm_kfree(bpmp->dev, entry);
484 spin_unlock_irqrestore(&bpmp->lock, flags);
486 EXPORT_SYMBOL_GPL(tegra_bpmp_free_mrq);
488 static void tegra_bpmp_mrq_handle_ping(unsigned int mrq,
489 struct tegra_bpmp_channel *channel,
492 struct mrq_ping_request *request;
493 struct mrq_ping_response response;
495 request = (struct mrq_ping_request *)channel->ib->data;
497 memset(&response, 0, sizeof(response));
498 response.reply = request->challenge << 1;
500 tegra_bpmp_mrq_return(channel, 0, &response, sizeof(response));
503 static int tegra_bpmp_ping(struct tegra_bpmp *bpmp)
505 struct mrq_ping_response response;
506 struct mrq_ping_request request;
507 struct tegra_bpmp_message msg;
512 memset(&request, 0, sizeof(request));
513 request.challenge = 1;
515 memset(&response, 0, sizeof(response));
517 memset(&msg, 0, sizeof(msg));
519 msg.tx.data = &request;
520 msg.tx.size = sizeof(request);
521 msg.rx.data = &response;
522 msg.rx.size = sizeof(response);
524 local_irq_save(flags);
526 err = tegra_bpmp_transfer_atomic(bpmp, &msg);
528 local_irq_restore(flags);
532 "ping ok: challenge: %u, response: %u, time: %lld\n",
533 request.challenge, response.reply,
534 ktime_to_us(ktime_sub(end, start)));
539 static int tegra_bpmp_get_firmware_tag(struct tegra_bpmp *bpmp, char *tag,
542 struct mrq_query_tag_request request;
543 struct tegra_bpmp_message msg;
549 virt = dma_alloc_coherent(bpmp->dev, MSG_DATA_MIN_SZ, &phys,
550 GFP_KERNEL | GFP_DMA32);
554 memset(&request, 0, sizeof(request));
557 memset(&msg, 0, sizeof(msg));
558 msg.mrq = MRQ_QUERY_TAG;
559 msg.tx.data = &request;
560 msg.tx.size = sizeof(request);
562 local_irq_save(flags);
563 err = tegra_bpmp_transfer_atomic(bpmp, &msg);
564 local_irq_restore(flags);
567 strlcpy(tag, virt, size);
569 dma_free_coherent(bpmp->dev, MSG_DATA_MIN_SZ, virt, phys);
574 static void tegra_bpmp_channel_signal(struct tegra_bpmp_channel *channel)
576 unsigned long flags = channel->ob->flags;
578 if ((flags & MSG_RING) == 0)
581 complete(&channel->completion);
584 static void tegra_bpmp_handle_rx(struct mbox_client *client, void *data)
586 struct tegra_bpmp *bpmp = mbox_client_to_bpmp(client);
587 struct tegra_bpmp_channel *channel;
588 unsigned int i, count;
591 channel = tegra_bpmp_channel_get_rx(bpmp);
592 count = bpmp->soc->channels.thread.count;
593 busy = bpmp->threaded.busy;
595 if (tegra_bpmp_master_acked(channel))
596 tegra_bpmp_handle_mrq(bpmp, channel->ib->code, channel);
598 spin_lock(&bpmp->lock);
600 for_each_set_bit(i, busy, count) {
601 struct tegra_bpmp_channel *channel;
603 channel = tegra_bpmp_channel_get_thread(bpmp, i);
607 if (tegra_bpmp_master_acked(channel)) {
608 tegra_bpmp_channel_signal(channel);
613 spin_unlock(&bpmp->lock);
616 static void tegra_bpmp_ivc_notify(struct tegra_ivc *ivc, void *data)
618 struct tegra_bpmp *bpmp = data;
621 if (WARN_ON(bpmp->mbox.channel == NULL))
624 err = mbox_send_message(bpmp->mbox.channel, NULL);
628 mbox_client_txdone(bpmp->mbox.channel, 0);
631 static int tegra_bpmp_channel_init(struct tegra_bpmp_channel *channel,
632 struct tegra_bpmp *bpmp,
635 size_t message_size, queue_size;
639 channel->ivc = devm_kzalloc(bpmp->dev, sizeof(*channel->ivc),
644 message_size = tegra_ivc_align(MSG_MIN_SZ);
645 queue_size = tegra_ivc_total_queue_size(message_size);
646 offset = queue_size * index;
648 err = tegra_ivc_init(channel->ivc, NULL,
649 bpmp->rx.virt + offset, bpmp->rx.phys + offset,
650 bpmp->tx.virt + offset, bpmp->tx.phys + offset,
651 1, message_size, tegra_bpmp_ivc_notify,
654 dev_err(bpmp->dev, "failed to setup IVC for channel %u: %d\n",
659 init_completion(&channel->completion);
660 channel->bpmp = bpmp;
665 static void tegra_bpmp_channel_reset(struct tegra_bpmp_channel *channel)
667 /* reset the channel state */
668 tegra_ivc_reset(channel->ivc);
670 /* sync the channel state with BPMP */
671 while (tegra_ivc_notified(channel->ivc))
675 static void tegra_bpmp_channel_cleanup(struct tegra_bpmp_channel *channel)
677 tegra_ivc_cleanup(channel->ivc);
680 static int tegra_bpmp_probe(struct platform_device *pdev)
682 struct tegra_bpmp_channel *channel;
683 struct tegra_bpmp *bpmp;
689 bpmp = devm_kzalloc(&pdev->dev, sizeof(*bpmp), GFP_KERNEL);
693 bpmp->soc = of_device_get_match_data(&pdev->dev);
694 bpmp->dev = &pdev->dev;
696 bpmp->tx.pool = of_gen_pool_get(pdev->dev.of_node, "shmem", 0);
697 if (!bpmp->tx.pool) {
698 dev_err(&pdev->dev, "TX shmem pool not found\n");
702 bpmp->tx.virt = gen_pool_dma_alloc(bpmp->tx.pool, 4096, &bpmp->tx.phys);
703 if (!bpmp->tx.virt) {
704 dev_err(&pdev->dev, "failed to allocate from TX pool\n");
708 bpmp->rx.pool = of_gen_pool_get(pdev->dev.of_node, "shmem", 1);
709 if (!bpmp->rx.pool) {
710 dev_err(&pdev->dev, "RX shmem pool not found\n");
715 bpmp->rx.virt = gen_pool_dma_alloc(bpmp->rx.pool, 4096, &bpmp->rx.phys);
716 if (!bpmp->rx.pool) {
717 dev_err(&pdev->dev, "failed to allocate from RX pool\n");
722 INIT_LIST_HEAD(&bpmp->mrqs);
723 spin_lock_init(&bpmp->lock);
725 bpmp->threaded.count = bpmp->soc->channels.thread.count;
726 sema_init(&bpmp->threaded.lock, bpmp->threaded.count);
728 size = BITS_TO_LONGS(bpmp->threaded.count) * sizeof(long);
730 bpmp->threaded.allocated = devm_kzalloc(&pdev->dev, size, GFP_KERNEL);
731 if (!bpmp->threaded.allocated) {
736 bpmp->threaded.busy = devm_kzalloc(&pdev->dev, size, GFP_KERNEL);
737 if (!bpmp->threaded.busy) {
742 bpmp->num_channels = bpmp->soc->channels.cpu_tx.count +
743 bpmp->soc->channels.thread.count +
744 bpmp->soc->channels.cpu_rx.count;
746 bpmp->channels = devm_kcalloc(&pdev->dev, bpmp->num_channels,
747 sizeof(*channel), GFP_KERNEL);
748 if (!bpmp->channels) {
753 /* message channel initialization */
754 for (i = 0; i < bpmp->num_channels; i++) {
755 struct tegra_bpmp_channel *channel = &bpmp->channels[i];
757 err = tegra_bpmp_channel_init(channel, bpmp, i);
759 goto cleanup_channels;
762 /* mbox registration */
763 bpmp->mbox.client.dev = &pdev->dev;
764 bpmp->mbox.client.rx_callback = tegra_bpmp_handle_rx;
765 bpmp->mbox.client.tx_block = false;
766 bpmp->mbox.client.knows_txdone = false;
768 bpmp->mbox.channel = mbox_request_channel(&bpmp->mbox.client, 0);
769 if (IS_ERR(bpmp->mbox.channel)) {
770 err = PTR_ERR(bpmp->mbox.channel);
771 dev_err(&pdev->dev, "failed to get HSP mailbox: %d\n", err);
772 goto cleanup_channels;
775 /* reset message channels */
776 for (i = 0; i < bpmp->num_channels; i++) {
777 struct tegra_bpmp_channel *channel = &bpmp->channels[i];
779 tegra_bpmp_channel_reset(channel);
782 err = tegra_bpmp_request_mrq(bpmp, MRQ_PING,
783 tegra_bpmp_mrq_handle_ping, bpmp);
787 err = tegra_bpmp_ping(bpmp);
789 dev_err(&pdev->dev, "failed to ping BPMP: %d\n", err);
793 err = tegra_bpmp_get_firmware_tag(bpmp, tag, sizeof(tag) - 1);
795 dev_err(&pdev->dev, "failed to get firmware tag: %d\n", err);
799 dev_info(&pdev->dev, "firmware: %s\n", tag);
801 err = of_platform_default_populate(pdev->dev.of_node, NULL, &pdev->dev);
805 err = tegra_bpmp_init_clocks(bpmp);
809 err = tegra_bpmp_init_resets(bpmp);
813 platform_set_drvdata(pdev, bpmp);
818 tegra_bpmp_free_mrq(bpmp, MRQ_PING, bpmp);
820 mbox_free_channel(bpmp->mbox.channel);
823 tegra_bpmp_channel_cleanup(&bpmp->channels[i]);
825 gen_pool_free(bpmp->rx.pool, (unsigned long)bpmp->rx.virt, 4096);
827 gen_pool_free(bpmp->tx.pool, (unsigned long)bpmp->tx.virt, 4096);
831 static const struct tegra_bpmp_soc tegra186_soc = {
836 .timeout = 60 * USEC_PER_SEC,
841 .timeout = 600 * USEC_PER_SEC,
852 static const struct of_device_id tegra_bpmp_match[] = {
853 { .compatible = "nvidia,tegra186-bpmp", .data = &tegra186_soc },
857 static struct platform_driver tegra_bpmp_driver = {
859 .name = "tegra-bpmp",
860 .of_match_table = tegra_bpmp_match,
862 .probe = tegra_bpmp_probe,
865 static int __init tegra_bpmp_init(void)
867 return platform_driver_register(&tegra_bpmp_driver);
869 core_initcall(tegra_bpmp_init);