xref: /openbmc/linux/drivers/firmware/tegra/bpmp.c (revision 7aacf86b)
1 /*
2  * Copyright (c) 2016, NVIDIA CORPORATION.  All rights reserved.
3  *
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.
7  *
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.
12  */
13 
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>
23 
24 #include <soc/tegra/bpmp.h>
25 #include <soc/tegra/bpmp-abi.h>
26 #include <soc/tegra/ivc.h>
27 
28 #define MSG_ACK		BIT(0)
29 #define MSG_RING	BIT(1)
30 
31 static inline struct tegra_bpmp *
32 mbox_client_to_bpmp(struct mbox_client *client)
33 {
34 	return container_of(client, struct tegra_bpmp, mbox.client);
35 }
36 
37 struct tegra_bpmp *tegra_bpmp_get(struct device *dev)
38 {
39 	struct platform_device *pdev;
40 	struct tegra_bpmp *bpmp;
41 	struct device_node *np;
42 
43 	np = of_parse_phandle(dev->of_node, "nvidia,bpmp", 0);
44 	if (!np)
45 		return ERR_PTR(-ENOENT);
46 
47 	pdev = of_find_device_by_node(np);
48 	if (!pdev) {
49 		bpmp = ERR_PTR(-ENODEV);
50 		goto put;
51 	}
52 
53 	bpmp = platform_get_drvdata(pdev);
54 	if (!bpmp) {
55 		bpmp = ERR_PTR(-EPROBE_DEFER);
56 		put_device(&pdev->dev);
57 		goto put;
58 	}
59 
60 put:
61 	of_node_put(np);
62 	return bpmp;
63 }
64 EXPORT_SYMBOL_GPL(tegra_bpmp_get);
65 
66 void tegra_bpmp_put(struct tegra_bpmp *bpmp)
67 {
68 	if (bpmp)
69 		put_device(bpmp->dev);
70 }
71 EXPORT_SYMBOL_GPL(tegra_bpmp_put);
72 
73 static int tegra_bpmp_channel_get_index(struct tegra_bpmp_channel *channel)
74 {
75 	return channel - channel->bpmp->channels;
76 }
77 
78 static int
79 tegra_bpmp_channel_get_thread_index(struct tegra_bpmp_channel *channel)
80 {
81 	struct tegra_bpmp *bpmp = channel->bpmp;
82 	unsigned int offset, count;
83 	int index;
84 
85 	offset = bpmp->soc->channels.thread.offset;
86 	count = bpmp->soc->channels.thread.count;
87 
88 	index = tegra_bpmp_channel_get_index(channel);
89 	if (index < 0)
90 		return index;
91 
92 	if (index < offset || index >= offset + count)
93 		return -EINVAL;
94 
95 	return index - offset;
96 }
97 
98 static struct tegra_bpmp_channel *
99 tegra_bpmp_channel_get_thread(struct tegra_bpmp *bpmp, unsigned int index)
100 {
101 	unsigned int offset = bpmp->soc->channels.thread.offset;
102 	unsigned int count = bpmp->soc->channels.thread.count;
103 
104 	if (index >= count)
105 		return NULL;
106 
107 	return &bpmp->channels[offset + index];
108 }
109 
110 static struct tegra_bpmp_channel *
111 tegra_bpmp_channel_get_tx(struct tegra_bpmp *bpmp)
112 {
113 	unsigned int offset = bpmp->soc->channels.cpu_tx.offset;
114 
115 	return &bpmp->channels[offset + smp_processor_id()];
116 }
117 
118 static struct tegra_bpmp_channel *
119 tegra_bpmp_channel_get_rx(struct tegra_bpmp *bpmp)
120 {
121 	unsigned int offset = bpmp->soc->channels.cpu_rx.offset;
122 
123 	return &bpmp->channels[offset];
124 }
125 
126 static bool tegra_bpmp_message_valid(const struct tegra_bpmp_message *msg)
127 {
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);
132 }
133 
134 static bool tegra_bpmp_master_acked(struct tegra_bpmp_channel *channel)
135 {
136 	void *frame;
137 
138 	frame = tegra_ivc_read_get_next_frame(channel->ivc);
139 	if (IS_ERR(frame)) {
140 		channel->ib = NULL;
141 		return false;
142 	}
143 
144 	channel->ib = frame;
145 
146 	return true;
147 }
148 
149 static int tegra_bpmp_wait_ack(struct tegra_bpmp_channel *channel)
150 {
151 	unsigned long timeout = channel->bpmp->soc->channels.cpu_tx.timeout;
152 	ktime_t end;
153 
154 	end = ktime_add_us(ktime_get(), timeout);
155 
156 	do {
157 		if (tegra_bpmp_master_acked(channel))
158 			return 0;
159 	} while (ktime_before(ktime_get(), end));
160 
161 	return -ETIMEDOUT;
162 }
163 
164 static bool tegra_bpmp_master_free(struct tegra_bpmp_channel *channel)
165 {
166 	void *frame;
167 
168 	frame = tegra_ivc_write_get_next_frame(channel->ivc);
169 	if (IS_ERR(frame)) {
170 		channel->ob = NULL;
171 		return false;
172 	}
173 
174 	channel->ob = frame;
175 
176 	return true;
177 }
178 
179 static int tegra_bpmp_wait_master_free(struct tegra_bpmp_channel *channel)
180 {
181 	unsigned long timeout = channel->bpmp->soc->channels.cpu_tx.timeout;
182 	ktime_t start, now;
183 
184 	start = ns_to_ktime(local_clock());
185 
186 	do {
187 		if (tegra_bpmp_master_free(channel))
188 			return 0;
189 
190 		now = ns_to_ktime(local_clock());
191 	} while (ktime_us_delta(now, start) < timeout);
192 
193 	return -ETIMEDOUT;
194 }
195 
196 static ssize_t __tegra_bpmp_channel_read(struct tegra_bpmp_channel *channel,
197 					 void *data, size_t size)
198 {
199 	if (data && size > 0)
200 		memcpy(data, channel->ib->data, size);
201 
202 	return tegra_ivc_read_advance(channel->ivc);
203 }
204 
205 static ssize_t tegra_bpmp_channel_read(struct tegra_bpmp_channel *channel,
206 				       void *data, size_t size)
207 {
208 	struct tegra_bpmp *bpmp = channel->bpmp;
209 	unsigned long flags;
210 	ssize_t err;
211 	int index;
212 
213 	index = tegra_bpmp_channel_get_thread_index(channel);
214 	if (index < 0) {
215 		err = index;
216 		goto unlock;
217 	}
218 
219 	spin_lock_irqsave(&bpmp->lock, flags);
220 	err = __tegra_bpmp_channel_read(channel, data, size);
221 	clear_bit(index, bpmp->threaded.allocated);
222 	spin_unlock_irqrestore(&bpmp->lock, flags);
223 
224 unlock:
225 	up(&bpmp->threaded.lock);
226 
227 	return err;
228 }
229 
230 static ssize_t __tegra_bpmp_channel_write(struct tegra_bpmp_channel *channel,
231 					  unsigned int mrq, unsigned long flags,
232 					  const void *data, size_t size)
233 {
234 	channel->ob->code = mrq;
235 	channel->ob->flags = flags;
236 
237 	if (data && size > 0)
238 		memcpy(channel->ob->data, data, size);
239 
240 	return tegra_ivc_write_advance(channel->ivc);
241 }
242 
243 static struct tegra_bpmp_channel *
244 tegra_bpmp_write_threaded(struct tegra_bpmp *bpmp, unsigned int mrq,
245 			  const void *data, size_t size)
246 {
247 	unsigned long timeout = bpmp->soc->channels.thread.timeout;
248 	unsigned int count = bpmp->soc->channels.thread.count;
249 	struct tegra_bpmp_channel *channel;
250 	unsigned long flags;
251 	unsigned int index;
252 	int err;
253 
254 	err = down_timeout(&bpmp->threaded.lock, usecs_to_jiffies(timeout));
255 	if (err < 0)
256 		return ERR_PTR(err);
257 
258 	spin_lock_irqsave(&bpmp->lock, flags);
259 
260 	index = find_first_zero_bit(bpmp->threaded.allocated, count);
261 	if (index == count) {
262 		err = -EBUSY;
263 		goto unlock;
264 	}
265 
266 	channel = tegra_bpmp_channel_get_thread(bpmp, index);
267 	if (!channel) {
268 		err = -EINVAL;
269 		goto unlock;
270 	}
271 
272 	if (!tegra_bpmp_master_free(channel)) {
273 		err = -EBUSY;
274 		goto unlock;
275 	}
276 
277 	set_bit(index, bpmp->threaded.allocated);
278 
279 	err = __tegra_bpmp_channel_write(channel, mrq, MSG_ACK | MSG_RING,
280 					 data, size);
281 	if (err < 0)
282 		goto clear_allocated;
283 
284 	set_bit(index, bpmp->threaded.busy);
285 
286 	spin_unlock_irqrestore(&bpmp->lock, flags);
287 	return channel;
288 
289 clear_allocated:
290 	clear_bit(index, bpmp->threaded.allocated);
291 unlock:
292 	spin_unlock_irqrestore(&bpmp->lock, flags);
293 	up(&bpmp->threaded.lock);
294 
295 	return ERR_PTR(err);
296 }
297 
298 static ssize_t tegra_bpmp_channel_write(struct tegra_bpmp_channel *channel,
299 					unsigned int mrq, unsigned long flags,
300 					const void *data, size_t size)
301 {
302 	int err;
303 
304 	err = tegra_bpmp_wait_master_free(channel);
305 	if (err < 0)
306 		return err;
307 
308 	return __tegra_bpmp_channel_write(channel, mrq, flags, data, size);
309 }
310 
311 int tegra_bpmp_transfer_atomic(struct tegra_bpmp *bpmp,
312 			       struct tegra_bpmp_message *msg)
313 {
314 	struct tegra_bpmp_channel *channel;
315 	int err;
316 
317 	if (WARN_ON(!irqs_disabled()))
318 		return -EPERM;
319 
320 	if (!tegra_bpmp_message_valid(msg))
321 		return -EINVAL;
322 
323 	channel = tegra_bpmp_channel_get_tx(bpmp);
324 
325 	err = tegra_bpmp_channel_write(channel, msg->mrq, MSG_ACK,
326 				       msg->tx.data, msg->tx.size);
327 	if (err < 0)
328 		return err;
329 
330 	err = mbox_send_message(bpmp->mbox.channel, NULL);
331 	if (err < 0)
332 		return err;
333 
334 	mbox_client_txdone(bpmp->mbox.channel, 0);
335 
336 	err = tegra_bpmp_wait_ack(channel);
337 	if (err < 0)
338 		return err;
339 
340 	return __tegra_bpmp_channel_read(channel, msg->rx.data, msg->rx.size);
341 }
342 EXPORT_SYMBOL_GPL(tegra_bpmp_transfer_atomic);
343 
344 int tegra_bpmp_transfer(struct tegra_bpmp *bpmp,
345 			struct tegra_bpmp_message *msg)
346 {
347 	struct tegra_bpmp_channel *channel;
348 	unsigned long timeout;
349 	int err;
350 
351 	if (WARN_ON(irqs_disabled()))
352 		return -EPERM;
353 
354 	if (!tegra_bpmp_message_valid(msg))
355 		return -EINVAL;
356 
357 	channel = tegra_bpmp_write_threaded(bpmp, msg->mrq, msg->tx.data,
358 					    msg->tx.size);
359 	if (IS_ERR(channel))
360 		return PTR_ERR(channel);
361 
362 	err = mbox_send_message(bpmp->mbox.channel, NULL);
363 	if (err < 0)
364 		return err;
365 
366 	mbox_client_txdone(bpmp->mbox.channel, 0);
367 
368 	timeout = usecs_to_jiffies(bpmp->soc->channels.thread.timeout);
369 
370 	err = wait_for_completion_timeout(&channel->completion, timeout);
371 	if (err == 0)
372 		return -ETIMEDOUT;
373 
374 	return tegra_bpmp_channel_read(channel, msg->rx.data, msg->rx.size);
375 }
376 EXPORT_SYMBOL_GPL(tegra_bpmp_transfer);
377 
378 static struct tegra_bpmp_mrq *tegra_bpmp_find_mrq(struct tegra_bpmp *bpmp,
379 						  unsigned int mrq)
380 {
381 	struct tegra_bpmp_mrq *entry;
382 
383 	list_for_each_entry(entry, &bpmp->mrqs, list)
384 		if (entry->mrq == mrq)
385 			return entry;
386 
387 	return NULL;
388 }
389 
390 static void tegra_bpmp_mrq_return(struct tegra_bpmp_channel *channel,
391 				  int code, const void *data, size_t size)
392 {
393 	unsigned long flags = channel->ib->flags;
394 	struct tegra_bpmp *bpmp = channel->bpmp;
395 	struct tegra_bpmp_mb_data *frame;
396 	int err;
397 
398 	if (WARN_ON(size > MSG_DATA_MIN_SZ))
399 		return;
400 
401 	err = tegra_ivc_read_advance(channel->ivc);
402 	if (WARN_ON(err < 0))
403 		return;
404 
405 	if ((flags & MSG_ACK) == 0)
406 		return;
407 
408 	frame = tegra_ivc_write_get_next_frame(channel->ivc);
409 	if (WARN_ON(IS_ERR(frame)))
410 		return;
411 
412 	frame->code = code;
413 
414 	if (data && size > 0)
415 		memcpy(frame->data, data, size);
416 
417 	err = tegra_ivc_write_advance(channel->ivc);
418 	if (WARN_ON(err < 0))
419 		return;
420 
421 	if (flags & MSG_RING) {
422 		err = mbox_send_message(bpmp->mbox.channel, NULL);
423 		if (WARN_ON(err < 0))
424 			return;
425 
426 		mbox_client_txdone(bpmp->mbox.channel, 0);
427 	}
428 }
429 
430 static void tegra_bpmp_handle_mrq(struct tegra_bpmp *bpmp,
431 				  unsigned int mrq,
432 				  struct tegra_bpmp_channel *channel)
433 {
434 	struct tegra_bpmp_mrq *entry;
435 	u32 zero = 0;
436 
437 	spin_lock(&bpmp->lock);
438 
439 	entry = tegra_bpmp_find_mrq(bpmp, mrq);
440 	if (!entry) {
441 		spin_unlock(&bpmp->lock);
442 		tegra_bpmp_mrq_return(channel, -EINVAL, &zero, sizeof(zero));
443 		return;
444 	}
445 
446 	entry->handler(mrq, channel, entry->data);
447 
448 	spin_unlock(&bpmp->lock);
449 }
450 
451 int tegra_bpmp_request_mrq(struct tegra_bpmp *bpmp, unsigned int mrq,
452 			   tegra_bpmp_mrq_handler_t handler, void *data)
453 {
454 	struct tegra_bpmp_mrq *entry;
455 	unsigned long flags;
456 
457 	if (!handler)
458 		return -EINVAL;
459 
460 	entry = devm_kzalloc(bpmp->dev, sizeof(*entry), GFP_KERNEL);
461 	if (!entry)
462 		return -ENOMEM;
463 
464 	spin_lock_irqsave(&bpmp->lock, flags);
465 
466 	entry->mrq = mrq;
467 	entry->handler = handler;
468 	entry->data = data;
469 	list_add(&entry->list, &bpmp->mrqs);
470 
471 	spin_unlock_irqrestore(&bpmp->lock, flags);
472 
473 	return 0;
474 }
475 EXPORT_SYMBOL_GPL(tegra_bpmp_request_mrq);
476 
477 void tegra_bpmp_free_mrq(struct tegra_bpmp *bpmp, unsigned int mrq, void *data)
478 {
479 	struct tegra_bpmp_mrq *entry;
480 	unsigned long flags;
481 
482 	spin_lock_irqsave(&bpmp->lock, flags);
483 
484 	entry = tegra_bpmp_find_mrq(bpmp, mrq);
485 	if (!entry)
486 		goto unlock;
487 
488 	list_del(&entry->list);
489 	devm_kfree(bpmp->dev, entry);
490 
491 unlock:
492 	spin_unlock_irqrestore(&bpmp->lock, flags);
493 }
494 EXPORT_SYMBOL_GPL(tegra_bpmp_free_mrq);
495 
496 static void tegra_bpmp_mrq_handle_ping(unsigned int mrq,
497 				       struct tegra_bpmp_channel *channel,
498 				       void *data)
499 {
500 	struct mrq_ping_request *request;
501 	struct mrq_ping_response response;
502 
503 	request = (struct mrq_ping_request *)channel->ib->data;
504 
505 	memset(&response, 0, sizeof(response));
506 	response.reply = request->challenge << 1;
507 
508 	tegra_bpmp_mrq_return(channel, 0, &response, sizeof(response));
509 }
510 
511 static int tegra_bpmp_ping(struct tegra_bpmp *bpmp)
512 {
513 	struct mrq_ping_response response;
514 	struct mrq_ping_request request;
515 	struct tegra_bpmp_message msg;
516 	unsigned long flags;
517 	ktime_t start, end;
518 	int err;
519 
520 	memset(&request, 0, sizeof(request));
521 	request.challenge = 1;
522 
523 	memset(&response, 0, sizeof(response));
524 
525 	memset(&msg, 0, sizeof(msg));
526 	msg.mrq = MRQ_PING;
527 	msg.tx.data = &request;
528 	msg.tx.size = sizeof(request);
529 	msg.rx.data = &response;
530 	msg.rx.size = sizeof(response);
531 
532 	local_irq_save(flags);
533 	start = ktime_get();
534 	err = tegra_bpmp_transfer_atomic(bpmp, &msg);
535 	end = ktime_get();
536 	local_irq_restore(flags);
537 
538 	if (!err)
539 		dev_dbg(bpmp->dev,
540 			"ping ok: challenge: %u, response: %u, time: %lld\n",
541 			request.challenge, response.reply,
542 			ktime_to_us(ktime_sub(end, start)));
543 
544 	return err;
545 }
546 
547 static int tegra_bpmp_get_firmware_tag(struct tegra_bpmp *bpmp, char *tag,
548 				       size_t size)
549 {
550 	struct mrq_query_tag_request request;
551 	struct tegra_bpmp_message msg;
552 	unsigned long flags;
553 	dma_addr_t phys;
554 	void *virt;
555 	int err;
556 
557 	virt = dma_alloc_coherent(bpmp->dev, MSG_DATA_MIN_SZ, &phys,
558 				  GFP_KERNEL | GFP_DMA32);
559 	if (!virt)
560 		return -ENOMEM;
561 
562 	memset(&request, 0, sizeof(request));
563 	request.addr = phys;
564 
565 	memset(&msg, 0, sizeof(msg));
566 	msg.mrq = MRQ_QUERY_TAG;
567 	msg.tx.data = &request;
568 	msg.tx.size = sizeof(request);
569 
570 	local_irq_save(flags);
571 	err = tegra_bpmp_transfer_atomic(bpmp, &msg);
572 	local_irq_restore(flags);
573 
574 	if (err == 0)
575 		strlcpy(tag, virt, size);
576 
577 	dma_free_coherent(bpmp->dev, MSG_DATA_MIN_SZ, virt, phys);
578 
579 	return err;
580 }
581 
582 static void tegra_bpmp_channel_signal(struct tegra_bpmp_channel *channel)
583 {
584 	unsigned long flags = channel->ob->flags;
585 
586 	if ((flags & MSG_RING) == 0)
587 		return;
588 
589 	complete(&channel->completion);
590 }
591 
592 static void tegra_bpmp_handle_rx(struct mbox_client *client, void *data)
593 {
594 	struct tegra_bpmp *bpmp = mbox_client_to_bpmp(client);
595 	struct tegra_bpmp_channel *channel;
596 	unsigned int i, count;
597 	unsigned long *busy;
598 
599 	channel = tegra_bpmp_channel_get_rx(bpmp);
600 	count = bpmp->soc->channels.thread.count;
601 	busy = bpmp->threaded.busy;
602 
603 	if (tegra_bpmp_master_acked(channel))
604 		tegra_bpmp_handle_mrq(bpmp, channel->ib->code, channel);
605 
606 	spin_lock(&bpmp->lock);
607 
608 	for_each_set_bit(i, busy, count) {
609 		struct tegra_bpmp_channel *channel;
610 
611 		channel = tegra_bpmp_channel_get_thread(bpmp, i);
612 		if (!channel)
613 			continue;
614 
615 		if (tegra_bpmp_master_acked(channel)) {
616 			tegra_bpmp_channel_signal(channel);
617 			clear_bit(i, busy);
618 		}
619 	}
620 
621 	spin_unlock(&bpmp->lock);
622 }
623 
624 static void tegra_bpmp_ivc_notify(struct tegra_ivc *ivc, void *data)
625 {
626 	struct tegra_bpmp *bpmp = data;
627 	int err;
628 
629 	if (WARN_ON(bpmp->mbox.channel == NULL))
630 		return;
631 
632 	err = mbox_send_message(bpmp->mbox.channel, NULL);
633 	if (err < 0)
634 		return;
635 
636 	mbox_client_txdone(bpmp->mbox.channel, 0);
637 }
638 
639 static int tegra_bpmp_channel_init(struct tegra_bpmp_channel *channel,
640 				   struct tegra_bpmp *bpmp,
641 				   unsigned int index)
642 {
643 	size_t message_size, queue_size;
644 	unsigned int offset;
645 	int err;
646 
647 	channel->ivc = devm_kzalloc(bpmp->dev, sizeof(*channel->ivc),
648 				    GFP_KERNEL);
649 	if (!channel->ivc)
650 		return -ENOMEM;
651 
652 	message_size = tegra_ivc_align(MSG_MIN_SZ);
653 	queue_size = tegra_ivc_total_queue_size(message_size);
654 	offset = queue_size * index;
655 
656 	err = tegra_ivc_init(channel->ivc, NULL,
657 			     bpmp->rx.virt + offset, bpmp->rx.phys + offset,
658 			     bpmp->tx.virt + offset, bpmp->tx.phys + offset,
659 			     1, message_size, tegra_bpmp_ivc_notify,
660 			     bpmp);
661 	if (err < 0) {
662 		dev_err(bpmp->dev, "failed to setup IVC for channel %u: %d\n",
663 			index, err);
664 		return err;
665 	}
666 
667 	init_completion(&channel->completion);
668 	channel->bpmp = bpmp;
669 
670 	return 0;
671 }
672 
673 static void tegra_bpmp_channel_reset(struct tegra_bpmp_channel *channel)
674 {
675 	/* reset the channel state */
676 	tegra_ivc_reset(channel->ivc);
677 
678 	/* sync the channel state with BPMP */
679 	while (tegra_ivc_notified(channel->ivc))
680 		;
681 }
682 
683 static void tegra_bpmp_channel_cleanup(struct tegra_bpmp_channel *channel)
684 {
685 	tegra_ivc_cleanup(channel->ivc);
686 }
687 
688 static int tegra_bpmp_probe(struct platform_device *pdev)
689 {
690 	struct tegra_bpmp_channel *channel;
691 	struct tegra_bpmp *bpmp;
692 	unsigned int i;
693 	char tag[32];
694 	size_t size;
695 	int err;
696 
697 	bpmp = devm_kzalloc(&pdev->dev, sizeof(*bpmp), GFP_KERNEL);
698 	if (!bpmp)
699 		return -ENOMEM;
700 
701 	bpmp->soc = of_device_get_match_data(&pdev->dev);
702 	bpmp->dev = &pdev->dev;
703 
704 	bpmp->tx.pool = of_gen_pool_get(pdev->dev.of_node, "shmem", 0);
705 	if (!bpmp->tx.pool) {
706 		dev_err(&pdev->dev, "TX shmem pool not found\n");
707 		return -ENOMEM;
708 	}
709 
710 	bpmp->tx.virt = gen_pool_dma_alloc(bpmp->tx.pool, 4096, &bpmp->tx.phys);
711 	if (!bpmp->tx.virt) {
712 		dev_err(&pdev->dev, "failed to allocate from TX pool\n");
713 		return -ENOMEM;
714 	}
715 
716 	bpmp->rx.pool = of_gen_pool_get(pdev->dev.of_node, "shmem", 1);
717 	if (!bpmp->rx.pool) {
718 		dev_err(&pdev->dev, "RX shmem pool not found\n");
719 		err = -ENOMEM;
720 		goto free_tx;
721 	}
722 
723 	bpmp->rx.virt = gen_pool_dma_alloc(bpmp->rx.pool, 4096, &bpmp->rx.phys);
724 	if (!bpmp->rx.pool) {
725 		dev_err(&pdev->dev, "failed to allocate from RX pool\n");
726 		err = -ENOMEM;
727 		goto free_tx;
728 	}
729 
730 	INIT_LIST_HEAD(&bpmp->mrqs);
731 	spin_lock_init(&bpmp->lock);
732 
733 	bpmp->threaded.count = bpmp->soc->channels.thread.count;
734 	sema_init(&bpmp->threaded.lock, bpmp->threaded.count);
735 
736 	size = BITS_TO_LONGS(bpmp->threaded.count) * sizeof(long);
737 
738 	bpmp->threaded.allocated = devm_kzalloc(&pdev->dev, size, GFP_KERNEL);
739 	if (!bpmp->threaded.allocated) {
740 		err = -ENOMEM;
741 		goto free_rx;
742 	}
743 
744 	bpmp->threaded.busy = devm_kzalloc(&pdev->dev, size, GFP_KERNEL);
745 	if (!bpmp->threaded.busy) {
746 		err = -ENOMEM;
747 		goto free_rx;
748 	}
749 
750 	bpmp->num_channels = bpmp->soc->channels.cpu_tx.count +
751 			     bpmp->soc->channels.thread.count +
752 			     bpmp->soc->channels.cpu_rx.count;
753 
754 	bpmp->channels = devm_kcalloc(&pdev->dev, bpmp->num_channels,
755 				      sizeof(*channel), GFP_KERNEL);
756 	if (!bpmp->channels) {
757 		err = -ENOMEM;
758 		goto free_rx;
759 	}
760 
761 	/* message channel initialization */
762 	for (i = 0; i < bpmp->num_channels; i++) {
763 		struct tegra_bpmp_channel *channel = &bpmp->channels[i];
764 
765 		err = tegra_bpmp_channel_init(channel, bpmp, i);
766 		if (err < 0)
767 			goto cleanup_channels;
768 	}
769 
770 	/* mbox registration */
771 	bpmp->mbox.client.dev = &pdev->dev;
772 	bpmp->mbox.client.rx_callback = tegra_bpmp_handle_rx;
773 	bpmp->mbox.client.tx_block = false;
774 	bpmp->mbox.client.knows_txdone = false;
775 
776 	bpmp->mbox.channel = mbox_request_channel(&bpmp->mbox.client, 0);
777 	if (IS_ERR(bpmp->mbox.channel)) {
778 		err = PTR_ERR(bpmp->mbox.channel);
779 		dev_err(&pdev->dev, "failed to get HSP mailbox: %d\n", err);
780 		goto cleanup_channels;
781 	}
782 
783 	/* reset message channels */
784 	for (i = 0; i < bpmp->num_channels; i++) {
785 		struct tegra_bpmp_channel *channel = &bpmp->channels[i];
786 
787 		tegra_bpmp_channel_reset(channel);
788 	}
789 
790 	err = tegra_bpmp_request_mrq(bpmp, MRQ_PING,
791 				     tegra_bpmp_mrq_handle_ping, bpmp);
792 	if (err < 0)
793 		goto free_mbox;
794 
795 	err = tegra_bpmp_ping(bpmp);
796 	if (err < 0) {
797 		dev_err(&pdev->dev, "failed to ping BPMP: %d\n", err);
798 		goto free_mrq;
799 	}
800 
801 	err = tegra_bpmp_get_firmware_tag(bpmp, tag, sizeof(tag) - 1);
802 	if (err < 0) {
803 		dev_err(&pdev->dev, "failed to get firmware tag: %d\n", err);
804 		goto free_mrq;
805 	}
806 
807 	dev_info(&pdev->dev, "firmware: %s\n", tag);
808 
809 	err = of_platform_default_populate(pdev->dev.of_node, NULL, &pdev->dev);
810 	if (err < 0)
811 		goto free_mrq;
812 
813 	err = tegra_bpmp_init_clocks(bpmp);
814 	if (err < 0)
815 		goto free_mrq;
816 
817 	err = tegra_bpmp_init_resets(bpmp);
818 	if (err < 0)
819 		goto free_mrq;
820 
821 	err = tegra_bpmp_init_powergates(bpmp);
822 	if (err < 0)
823 		goto free_mrq;
824 
825 	platform_set_drvdata(pdev, bpmp);
826 
827 	return 0;
828 
829 free_mrq:
830 	tegra_bpmp_free_mrq(bpmp, MRQ_PING, bpmp);
831 free_mbox:
832 	mbox_free_channel(bpmp->mbox.channel);
833 cleanup_channels:
834 	while (i--)
835 		tegra_bpmp_channel_cleanup(&bpmp->channels[i]);
836 free_rx:
837 	gen_pool_free(bpmp->rx.pool, (unsigned long)bpmp->rx.virt, 4096);
838 free_tx:
839 	gen_pool_free(bpmp->tx.pool, (unsigned long)bpmp->tx.virt, 4096);
840 	return err;
841 }
842 
843 static const struct tegra_bpmp_soc tegra186_soc = {
844 	.channels = {
845 		.cpu_tx = {
846 			.offset = 0,
847 			.count = 6,
848 			.timeout = 60 * USEC_PER_SEC,
849 		},
850 		.thread = {
851 			.offset = 6,
852 			.count = 7,
853 			.timeout = 600 * USEC_PER_SEC,
854 		},
855 		.cpu_rx = {
856 			.offset = 13,
857 			.count = 1,
858 			.timeout = 0,
859 		},
860 	},
861 	.num_resets = 193,
862 };
863 
864 static const struct of_device_id tegra_bpmp_match[] = {
865 	{ .compatible = "nvidia,tegra186-bpmp", .data = &tegra186_soc },
866 	{ }
867 };
868 
869 static struct platform_driver tegra_bpmp_driver = {
870 	.driver = {
871 		.name = "tegra-bpmp",
872 		.of_match_table = tegra_bpmp_match,
873 	},
874 	.probe = tegra_bpmp_probe,
875 };
876 
877 static int __init tegra_bpmp_init(void)
878 {
879 	return platform_driver_register(&tegra_bpmp_driver);
880 }
881 core_initcall(tegra_bpmp_init);
882