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