bpf: Prevent memory disambiguation attack
[linux/fpc-iii.git] / drivers / firmware / tegra / bpmp.c
bloba7f461f2e650c3936959ff7c0c0cf5b9ef5198ab
1 /*
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
11 * more details.
14 #include <linux/clk/tegra.h>
15 #include <linux/genalloc.h>
16 #include <linux/mailbox_client.h>
17 #include <linux/of.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);
44 if (!np)
45 return ERR_PTR(-ENOENT);
47 pdev = of_find_device_by_node(np);
48 if (!pdev) {
49 bpmp = ERR_PTR(-ENODEV);
50 goto put;
53 bpmp = platform_get_drvdata(pdev);
54 if (!bpmp) {
55 bpmp = ERR_PTR(-EPROBE_DEFER);
56 put_device(&pdev->dev);
57 goto put;
60 put:
61 of_node_put(np);
62 return bpmp;
64 EXPORT_SYMBOL_GPL(tegra_bpmp_get);
66 void tegra_bpmp_put(struct tegra_bpmp *bpmp)
68 if (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;
78 static int
79 tegra_bpmp_channel_get_thread_index(struct tegra_bpmp_channel *channel)
81 struct tegra_bpmp *bpmp = channel->bpmp;
82 unsigned int offset, count;
83 int index;
85 offset = bpmp->soc->channels.thread.offset;
86 count = bpmp->soc->channels.thread.count;
88 index = tegra_bpmp_channel_get_index(channel);
89 if (index < 0)
90 return index;
92 if (index < offset || index >= offset + count)
93 return -EINVAL;
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;
104 if (index >= count)
105 return NULL;
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)
136 void *frame;
138 frame = tegra_ivc_read_get_next_frame(channel->ivc);
139 if (IS_ERR(frame)) {
140 channel->ib = NULL;
141 return false;
144 channel->ib = frame;
146 return true;
149 static int tegra_bpmp_wait_ack(struct tegra_bpmp_channel *channel)
151 unsigned long timeout = channel->bpmp->soc->channels.cpu_tx.timeout;
152 ktime_t end;
154 end = ktime_add_us(ktime_get(), timeout);
156 do {
157 if (tegra_bpmp_master_acked(channel))
158 return 0;
159 } while (ktime_before(ktime_get(), end));
161 return -ETIMEDOUT;
164 static bool tegra_bpmp_master_free(struct tegra_bpmp_channel *channel)
166 void *frame;
168 frame = tegra_ivc_write_get_next_frame(channel->ivc);
169 if (IS_ERR(frame)) {
170 channel->ob = NULL;
171 return false;
174 channel->ob = frame;
176 return true;
179 static int tegra_bpmp_wait_master_free(struct tegra_bpmp_channel *channel)
181 unsigned long timeout = channel->bpmp->soc->channels.cpu_tx.timeout;
182 ktime_t start, now;
184 start = ns_to_ktime(local_clock());
186 do {
187 if (tegra_bpmp_master_free(channel))
188 return 0;
190 now = ns_to_ktime(local_clock());
191 } while (ktime_us_delta(now, start) < timeout);
193 return -ETIMEDOUT;
196 static ssize_t __tegra_bpmp_channel_read(struct tegra_bpmp_channel *channel,
197 void *data, size_t size, int *ret)
199 int err;
201 if (data && size > 0)
202 memcpy(data, channel->ib->data, size);
204 err = tegra_ivc_read_advance(channel->ivc);
205 if (err < 0)
206 return err;
208 *ret = channel->ib->code;
210 return 0;
213 static ssize_t tegra_bpmp_channel_read(struct tegra_bpmp_channel *channel,
214 void *data, size_t size, int *ret)
216 struct tegra_bpmp *bpmp = channel->bpmp;
217 unsigned long flags;
218 ssize_t err;
219 int index;
221 index = tegra_bpmp_channel_get_thread_index(channel);
222 if (index < 0) {
223 err = index;
224 goto unlock;
227 spin_lock_irqsave(&bpmp->lock, flags);
228 err = __tegra_bpmp_channel_read(channel, data, size, ret);
229 clear_bit(index, bpmp->threaded.allocated);
230 spin_unlock_irqrestore(&bpmp->lock, flags);
232 unlock:
233 up(&bpmp->threaded.lock);
235 return err;
238 static ssize_t __tegra_bpmp_channel_write(struct tegra_bpmp_channel *channel,
239 unsigned int mrq, unsigned long flags,
240 const void *data, size_t size)
242 channel->ob->code = mrq;
243 channel->ob->flags = flags;
245 if (data && size > 0)
246 memcpy(channel->ob->data, data, size);
248 return tegra_ivc_write_advance(channel->ivc);
251 static struct tegra_bpmp_channel *
252 tegra_bpmp_write_threaded(struct tegra_bpmp *bpmp, unsigned int mrq,
253 const void *data, size_t size)
255 unsigned long timeout = bpmp->soc->channels.thread.timeout;
256 unsigned int count = bpmp->soc->channels.thread.count;
257 struct tegra_bpmp_channel *channel;
258 unsigned long flags;
259 unsigned int index;
260 int err;
262 err = down_timeout(&bpmp->threaded.lock, usecs_to_jiffies(timeout));
263 if (err < 0)
264 return ERR_PTR(err);
266 spin_lock_irqsave(&bpmp->lock, flags);
268 index = find_first_zero_bit(bpmp->threaded.allocated, count);
269 if (index == count) {
270 err = -EBUSY;
271 goto unlock;
274 channel = tegra_bpmp_channel_get_thread(bpmp, index);
275 if (!channel) {
276 err = -EINVAL;
277 goto unlock;
280 if (!tegra_bpmp_master_free(channel)) {
281 err = -EBUSY;
282 goto unlock;
285 set_bit(index, bpmp->threaded.allocated);
287 err = __tegra_bpmp_channel_write(channel, mrq, MSG_ACK | MSG_RING,
288 data, size);
289 if (err < 0)
290 goto clear_allocated;
292 set_bit(index, bpmp->threaded.busy);
294 spin_unlock_irqrestore(&bpmp->lock, flags);
295 return channel;
297 clear_allocated:
298 clear_bit(index, bpmp->threaded.allocated);
299 unlock:
300 spin_unlock_irqrestore(&bpmp->lock, flags);
301 up(&bpmp->threaded.lock);
303 return ERR_PTR(err);
306 static ssize_t tegra_bpmp_channel_write(struct tegra_bpmp_channel *channel,
307 unsigned int mrq, unsigned long flags,
308 const void *data, size_t size)
310 int err;
312 err = tegra_bpmp_wait_master_free(channel);
313 if (err < 0)
314 return err;
316 return __tegra_bpmp_channel_write(channel, mrq, flags, data, size);
319 int tegra_bpmp_transfer_atomic(struct tegra_bpmp *bpmp,
320 struct tegra_bpmp_message *msg)
322 struct tegra_bpmp_channel *channel;
323 int err;
325 if (WARN_ON(!irqs_disabled()))
326 return -EPERM;
328 if (!tegra_bpmp_message_valid(msg))
329 return -EINVAL;
331 channel = tegra_bpmp_channel_get_tx(bpmp);
333 err = tegra_bpmp_channel_write(channel, msg->mrq, MSG_ACK,
334 msg->tx.data, msg->tx.size);
335 if (err < 0)
336 return err;
338 err = mbox_send_message(bpmp->mbox.channel, NULL);
339 if (err < 0)
340 return err;
342 mbox_client_txdone(bpmp->mbox.channel, 0);
344 err = tegra_bpmp_wait_ack(channel);
345 if (err < 0)
346 return err;
348 return __tegra_bpmp_channel_read(channel, msg->rx.data, msg->rx.size,
349 &msg->rx.ret);
351 EXPORT_SYMBOL_GPL(tegra_bpmp_transfer_atomic);
353 int tegra_bpmp_transfer(struct tegra_bpmp *bpmp,
354 struct tegra_bpmp_message *msg)
356 struct tegra_bpmp_channel *channel;
357 unsigned long timeout;
358 int err;
360 if (WARN_ON(irqs_disabled()))
361 return -EPERM;
363 if (!tegra_bpmp_message_valid(msg))
364 return -EINVAL;
366 channel = tegra_bpmp_write_threaded(bpmp, msg->mrq, msg->tx.data,
367 msg->tx.size);
368 if (IS_ERR(channel))
369 return PTR_ERR(channel);
371 err = mbox_send_message(bpmp->mbox.channel, NULL);
372 if (err < 0)
373 return err;
375 mbox_client_txdone(bpmp->mbox.channel, 0);
377 timeout = usecs_to_jiffies(bpmp->soc->channels.thread.timeout);
379 err = wait_for_completion_timeout(&channel->completion, timeout);
380 if (err == 0)
381 return -ETIMEDOUT;
383 return tegra_bpmp_channel_read(channel, msg->rx.data, msg->rx.size,
384 &msg->rx.ret);
386 EXPORT_SYMBOL_GPL(tegra_bpmp_transfer);
388 static struct tegra_bpmp_mrq *tegra_bpmp_find_mrq(struct tegra_bpmp *bpmp,
389 unsigned int mrq)
391 struct tegra_bpmp_mrq *entry;
393 list_for_each_entry(entry, &bpmp->mrqs, list)
394 if (entry->mrq == mrq)
395 return entry;
397 return NULL;
400 void tegra_bpmp_mrq_return(struct tegra_bpmp_channel *channel, int code,
401 const void *data, size_t size)
403 unsigned long flags = channel->ib->flags;
404 struct tegra_bpmp *bpmp = channel->bpmp;
405 struct tegra_bpmp_mb_data *frame;
406 int err;
408 if (WARN_ON(size > MSG_DATA_MIN_SZ))
409 return;
411 err = tegra_ivc_read_advance(channel->ivc);
412 if (WARN_ON(err < 0))
413 return;
415 if ((flags & MSG_ACK) == 0)
416 return;
418 frame = tegra_ivc_write_get_next_frame(channel->ivc);
419 if (WARN_ON(IS_ERR(frame)))
420 return;
422 frame->code = code;
424 if (data && size > 0)
425 memcpy(frame->data, data, size);
427 err = tegra_ivc_write_advance(channel->ivc);
428 if (WARN_ON(err < 0))
429 return;
431 if (flags & MSG_RING) {
432 err = mbox_send_message(bpmp->mbox.channel, NULL);
433 if (WARN_ON(err < 0))
434 return;
436 mbox_client_txdone(bpmp->mbox.channel, 0);
439 EXPORT_SYMBOL_GPL(tegra_bpmp_mrq_return);
441 static void tegra_bpmp_handle_mrq(struct tegra_bpmp *bpmp,
442 unsigned int mrq,
443 struct tegra_bpmp_channel *channel)
445 struct tegra_bpmp_mrq *entry;
446 u32 zero = 0;
448 spin_lock(&bpmp->lock);
450 entry = tegra_bpmp_find_mrq(bpmp, mrq);
451 if (!entry) {
452 spin_unlock(&bpmp->lock);
453 tegra_bpmp_mrq_return(channel, -EINVAL, &zero, sizeof(zero));
454 return;
457 entry->handler(mrq, channel, entry->data);
459 spin_unlock(&bpmp->lock);
462 int tegra_bpmp_request_mrq(struct tegra_bpmp *bpmp, unsigned int mrq,
463 tegra_bpmp_mrq_handler_t handler, void *data)
465 struct tegra_bpmp_mrq *entry;
466 unsigned long flags;
468 if (!handler)
469 return -EINVAL;
471 entry = devm_kzalloc(bpmp->dev, sizeof(*entry), GFP_KERNEL);
472 if (!entry)
473 return -ENOMEM;
475 spin_lock_irqsave(&bpmp->lock, flags);
477 entry->mrq = mrq;
478 entry->handler = handler;
479 entry->data = data;
480 list_add(&entry->list, &bpmp->mrqs);
482 spin_unlock_irqrestore(&bpmp->lock, flags);
484 return 0;
486 EXPORT_SYMBOL_GPL(tegra_bpmp_request_mrq);
488 void tegra_bpmp_free_mrq(struct tegra_bpmp *bpmp, unsigned int mrq, void *data)
490 struct tegra_bpmp_mrq *entry;
491 unsigned long flags;
493 spin_lock_irqsave(&bpmp->lock, flags);
495 entry = tegra_bpmp_find_mrq(bpmp, mrq);
496 if (!entry)
497 goto unlock;
499 list_del(&entry->list);
500 devm_kfree(bpmp->dev, entry);
502 unlock:
503 spin_unlock_irqrestore(&bpmp->lock, flags);
505 EXPORT_SYMBOL_GPL(tegra_bpmp_free_mrq);
507 static void tegra_bpmp_mrq_handle_ping(unsigned int mrq,
508 struct tegra_bpmp_channel *channel,
509 void *data)
511 struct mrq_ping_request *request;
512 struct mrq_ping_response response;
514 request = (struct mrq_ping_request *)channel->ib->data;
516 memset(&response, 0, sizeof(response));
517 response.reply = request->challenge << 1;
519 tegra_bpmp_mrq_return(channel, 0, &response, sizeof(response));
522 static int tegra_bpmp_ping(struct tegra_bpmp *bpmp)
524 struct mrq_ping_response response;
525 struct mrq_ping_request request;
526 struct tegra_bpmp_message msg;
527 unsigned long flags;
528 ktime_t start, end;
529 int err;
531 memset(&request, 0, sizeof(request));
532 request.challenge = 1;
534 memset(&response, 0, sizeof(response));
536 memset(&msg, 0, sizeof(msg));
537 msg.mrq = MRQ_PING;
538 msg.tx.data = &request;
539 msg.tx.size = sizeof(request);
540 msg.rx.data = &response;
541 msg.rx.size = sizeof(response);
543 local_irq_save(flags);
544 start = ktime_get();
545 err = tegra_bpmp_transfer_atomic(bpmp, &msg);
546 end = ktime_get();
547 local_irq_restore(flags);
549 if (!err)
550 dev_dbg(bpmp->dev,
551 "ping ok: challenge: %u, response: %u, time: %lld\n",
552 request.challenge, response.reply,
553 ktime_to_us(ktime_sub(end, start)));
555 return err;
558 static int tegra_bpmp_get_firmware_tag(struct tegra_bpmp *bpmp, char *tag,
559 size_t size)
561 struct mrq_query_tag_request request;
562 struct tegra_bpmp_message msg;
563 unsigned long flags;
564 dma_addr_t phys;
565 void *virt;
566 int err;
568 virt = dma_alloc_coherent(bpmp->dev, MSG_DATA_MIN_SZ, &phys,
569 GFP_KERNEL | GFP_DMA32);
570 if (!virt)
571 return -ENOMEM;
573 memset(&request, 0, sizeof(request));
574 request.addr = phys;
576 memset(&msg, 0, sizeof(msg));
577 msg.mrq = MRQ_QUERY_TAG;
578 msg.tx.data = &request;
579 msg.tx.size = sizeof(request);
581 local_irq_save(flags);
582 err = tegra_bpmp_transfer_atomic(bpmp, &msg);
583 local_irq_restore(flags);
585 if (err == 0)
586 strlcpy(tag, virt, size);
588 dma_free_coherent(bpmp->dev, MSG_DATA_MIN_SZ, virt, phys);
590 return err;
593 static void tegra_bpmp_channel_signal(struct tegra_bpmp_channel *channel)
595 unsigned long flags = channel->ob->flags;
597 if ((flags & MSG_RING) == 0)
598 return;
600 complete(&channel->completion);
603 static void tegra_bpmp_handle_rx(struct mbox_client *client, void *data)
605 struct tegra_bpmp *bpmp = mbox_client_to_bpmp(client);
606 struct tegra_bpmp_channel *channel;
607 unsigned int i, count;
608 unsigned long *busy;
610 channel = tegra_bpmp_channel_get_rx(bpmp);
611 count = bpmp->soc->channels.thread.count;
612 busy = bpmp->threaded.busy;
614 if (tegra_bpmp_master_acked(channel))
615 tegra_bpmp_handle_mrq(bpmp, channel->ib->code, channel);
617 spin_lock(&bpmp->lock);
619 for_each_set_bit(i, busy, count) {
620 struct tegra_bpmp_channel *channel;
622 channel = tegra_bpmp_channel_get_thread(bpmp, i);
623 if (!channel)
624 continue;
626 if (tegra_bpmp_master_acked(channel)) {
627 tegra_bpmp_channel_signal(channel);
628 clear_bit(i, busy);
632 spin_unlock(&bpmp->lock);
635 static void tegra_bpmp_ivc_notify(struct tegra_ivc *ivc, void *data)
637 struct tegra_bpmp *bpmp = data;
638 int err;
640 if (WARN_ON(bpmp->mbox.channel == NULL))
641 return;
643 err = mbox_send_message(bpmp->mbox.channel, NULL);
644 if (err < 0)
645 return;
647 mbox_client_txdone(bpmp->mbox.channel, 0);
650 static int tegra_bpmp_channel_init(struct tegra_bpmp_channel *channel,
651 struct tegra_bpmp *bpmp,
652 unsigned int index)
654 size_t message_size, queue_size;
655 unsigned int offset;
656 int err;
658 channel->ivc = devm_kzalloc(bpmp->dev, sizeof(*channel->ivc),
659 GFP_KERNEL);
660 if (!channel->ivc)
661 return -ENOMEM;
663 message_size = tegra_ivc_align(MSG_MIN_SZ);
664 queue_size = tegra_ivc_total_queue_size(message_size);
665 offset = queue_size * index;
667 err = tegra_ivc_init(channel->ivc, NULL,
668 bpmp->rx.virt + offset, bpmp->rx.phys + offset,
669 bpmp->tx.virt + offset, bpmp->tx.phys + offset,
670 1, message_size, tegra_bpmp_ivc_notify,
671 bpmp);
672 if (err < 0) {
673 dev_err(bpmp->dev, "failed to setup IVC for channel %u: %d\n",
674 index, err);
675 return err;
678 init_completion(&channel->completion);
679 channel->bpmp = bpmp;
681 return 0;
684 static void tegra_bpmp_channel_reset(struct tegra_bpmp_channel *channel)
686 /* reset the channel state */
687 tegra_ivc_reset(channel->ivc);
689 /* sync the channel state with BPMP */
690 while (tegra_ivc_notified(channel->ivc))
694 static void tegra_bpmp_channel_cleanup(struct tegra_bpmp_channel *channel)
696 tegra_ivc_cleanup(channel->ivc);
699 static int tegra_bpmp_probe(struct platform_device *pdev)
701 struct tegra_bpmp_channel *channel;
702 struct tegra_bpmp *bpmp;
703 unsigned int i;
704 char tag[32];
705 size_t size;
706 int err;
708 bpmp = devm_kzalloc(&pdev->dev, sizeof(*bpmp), GFP_KERNEL);
709 if (!bpmp)
710 return -ENOMEM;
712 bpmp->soc = of_device_get_match_data(&pdev->dev);
713 bpmp->dev = &pdev->dev;
715 bpmp->tx.pool = of_gen_pool_get(pdev->dev.of_node, "shmem", 0);
716 if (!bpmp->tx.pool) {
717 dev_err(&pdev->dev, "TX shmem pool not found\n");
718 return -ENOMEM;
721 bpmp->tx.virt = gen_pool_dma_alloc(bpmp->tx.pool, 4096, &bpmp->tx.phys);
722 if (!bpmp->tx.virt) {
723 dev_err(&pdev->dev, "failed to allocate from TX pool\n");
724 return -ENOMEM;
727 bpmp->rx.pool = of_gen_pool_get(pdev->dev.of_node, "shmem", 1);
728 if (!bpmp->rx.pool) {
729 dev_err(&pdev->dev, "RX shmem pool not found\n");
730 err = -ENOMEM;
731 goto free_tx;
734 bpmp->rx.virt = gen_pool_dma_alloc(bpmp->rx.pool, 4096, &bpmp->rx.phys);
735 if (!bpmp->rx.pool) {
736 dev_err(&pdev->dev, "failed to allocate from RX pool\n");
737 err = -ENOMEM;
738 goto free_tx;
741 INIT_LIST_HEAD(&bpmp->mrqs);
742 spin_lock_init(&bpmp->lock);
744 bpmp->threaded.count = bpmp->soc->channels.thread.count;
745 sema_init(&bpmp->threaded.lock, bpmp->threaded.count);
747 size = BITS_TO_LONGS(bpmp->threaded.count) * sizeof(long);
749 bpmp->threaded.allocated = devm_kzalloc(&pdev->dev, size, GFP_KERNEL);
750 if (!bpmp->threaded.allocated) {
751 err = -ENOMEM;
752 goto free_rx;
755 bpmp->threaded.busy = devm_kzalloc(&pdev->dev, size, GFP_KERNEL);
756 if (!bpmp->threaded.busy) {
757 err = -ENOMEM;
758 goto free_rx;
761 bpmp->num_channels = bpmp->soc->channels.cpu_tx.count +
762 bpmp->soc->channels.thread.count +
763 bpmp->soc->channels.cpu_rx.count;
765 bpmp->channels = devm_kcalloc(&pdev->dev, bpmp->num_channels,
766 sizeof(*channel), GFP_KERNEL);
767 if (!bpmp->channels) {
768 err = -ENOMEM;
769 goto free_rx;
772 /* message channel initialization */
773 for (i = 0; i < bpmp->num_channels; i++) {
774 struct tegra_bpmp_channel *channel = &bpmp->channels[i];
776 err = tegra_bpmp_channel_init(channel, bpmp, i);
777 if (err < 0)
778 goto cleanup_channels;
781 /* mbox registration */
782 bpmp->mbox.client.dev = &pdev->dev;
783 bpmp->mbox.client.rx_callback = tegra_bpmp_handle_rx;
784 bpmp->mbox.client.tx_block = false;
785 bpmp->mbox.client.knows_txdone = false;
787 bpmp->mbox.channel = mbox_request_channel(&bpmp->mbox.client, 0);
788 if (IS_ERR(bpmp->mbox.channel)) {
789 err = PTR_ERR(bpmp->mbox.channel);
790 dev_err(&pdev->dev, "failed to get HSP mailbox: %d\n", err);
791 goto cleanup_channels;
794 /* reset message channels */
795 for (i = 0; i < bpmp->num_channels; i++) {
796 struct tegra_bpmp_channel *channel = &bpmp->channels[i];
798 tegra_bpmp_channel_reset(channel);
801 err = tegra_bpmp_request_mrq(bpmp, MRQ_PING,
802 tegra_bpmp_mrq_handle_ping, bpmp);
803 if (err < 0)
804 goto free_mbox;
806 err = tegra_bpmp_ping(bpmp);
807 if (err < 0) {
808 dev_err(&pdev->dev, "failed to ping BPMP: %d\n", err);
809 goto free_mrq;
812 err = tegra_bpmp_get_firmware_tag(bpmp, tag, sizeof(tag) - 1);
813 if (err < 0) {
814 dev_err(&pdev->dev, "failed to get firmware tag: %d\n", err);
815 goto free_mrq;
818 dev_info(&pdev->dev, "firmware: %s\n", tag);
820 platform_set_drvdata(pdev, bpmp);
822 err = of_platform_default_populate(pdev->dev.of_node, NULL, &pdev->dev);
823 if (err < 0)
824 goto free_mrq;
826 err = tegra_bpmp_init_clocks(bpmp);
827 if (err < 0)
828 goto free_mrq;
830 err = tegra_bpmp_init_resets(bpmp);
831 if (err < 0)
832 goto free_mrq;
834 err = tegra_bpmp_init_powergates(bpmp);
835 if (err < 0)
836 goto free_mrq;
838 err = tegra_bpmp_init_debugfs(bpmp);
839 if (err < 0)
840 dev_err(&pdev->dev, "debugfs initialization failed: %d\n", err);
842 return 0;
844 free_mrq:
845 tegra_bpmp_free_mrq(bpmp, MRQ_PING, bpmp);
846 free_mbox:
847 mbox_free_channel(bpmp->mbox.channel);
848 cleanup_channels:
849 while (i--)
850 tegra_bpmp_channel_cleanup(&bpmp->channels[i]);
851 free_rx:
852 gen_pool_free(bpmp->rx.pool, (unsigned long)bpmp->rx.virt, 4096);
853 free_tx:
854 gen_pool_free(bpmp->tx.pool, (unsigned long)bpmp->tx.virt, 4096);
855 return err;
858 static const struct tegra_bpmp_soc tegra186_soc = {
859 .channels = {
860 .cpu_tx = {
861 .offset = 0,
862 .count = 6,
863 .timeout = 60 * USEC_PER_SEC,
865 .thread = {
866 .offset = 6,
867 .count = 7,
868 .timeout = 600 * USEC_PER_SEC,
870 .cpu_rx = {
871 .offset = 13,
872 .count = 1,
873 .timeout = 0,
876 .num_resets = 193,
879 static const struct of_device_id tegra_bpmp_match[] = {
880 { .compatible = "nvidia,tegra186-bpmp", .data = &tegra186_soc },
884 static struct platform_driver tegra_bpmp_driver = {
885 .driver = {
886 .name = "tegra-bpmp",
887 .of_match_table = tegra_bpmp_match,
889 .probe = tegra_bpmp_probe,
892 static int __init tegra_bpmp_init(void)
894 return platform_driver_register(&tegra_bpmp_driver);
896 core_initcall(tegra_bpmp_init);