xref: /openbmc/linux/drivers/firmware/tegra/bpmp.c (revision 9b93eb47)
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/pm.h>
22 #include <linux/semaphore.h>
23 #include <linux/sched/clock.h>
24 
25 #include <soc/tegra/bpmp.h>
26 #include <soc/tegra/bpmp-abi.h>
27 #include <soc/tegra/ivc.h>
28 
29 #include "bpmp-private.h"
30 
31 #define MSG_ACK		BIT(0)
32 #define MSG_RING	BIT(1)
33 #define TAG_SZ		32
34 
35 static inline struct tegra_bpmp *
36 mbox_client_to_bpmp(struct mbox_client *client)
37 {
38 	return container_of(client, struct tegra_bpmp, mbox.client);
39 }
40 
41 static inline const struct tegra_bpmp_ops *
42 channel_to_ops(struct tegra_bpmp_channel *channel)
43 {
44 	struct tegra_bpmp *bpmp = channel->bpmp;
45 
46 	return bpmp->soc->ops;
47 }
48 
49 struct tegra_bpmp *tegra_bpmp_get(struct device *dev)
50 {
51 	struct platform_device *pdev;
52 	struct tegra_bpmp *bpmp;
53 	struct device_node *np;
54 
55 	np = of_parse_phandle(dev->of_node, "nvidia,bpmp", 0);
56 	if (!np)
57 		return ERR_PTR(-ENOENT);
58 
59 	pdev = of_find_device_by_node(np);
60 	if (!pdev) {
61 		bpmp = ERR_PTR(-ENODEV);
62 		goto put;
63 	}
64 
65 	bpmp = platform_get_drvdata(pdev);
66 	if (!bpmp) {
67 		bpmp = ERR_PTR(-EPROBE_DEFER);
68 		put_device(&pdev->dev);
69 		goto put;
70 	}
71 
72 put:
73 	of_node_put(np);
74 	return bpmp;
75 }
76 EXPORT_SYMBOL_GPL(tegra_bpmp_get);
77 
78 void tegra_bpmp_put(struct tegra_bpmp *bpmp)
79 {
80 	if (bpmp)
81 		put_device(bpmp->dev);
82 }
83 EXPORT_SYMBOL_GPL(tegra_bpmp_put);
84 
85 static int
86 tegra_bpmp_channel_get_thread_index(struct tegra_bpmp_channel *channel)
87 {
88 	struct tegra_bpmp *bpmp = channel->bpmp;
89 	unsigned int count;
90 	int index;
91 
92 	count = bpmp->soc->channels.thread.count;
93 
94 	index = channel - channel->bpmp->threaded_channels;
95 	if (index < 0 || index >= count)
96 		return -EINVAL;
97 
98 	return index;
99 }
100 
101 static bool tegra_bpmp_message_valid(const struct tegra_bpmp_message *msg)
102 {
103 	return (msg->tx.size <= MSG_DATA_MIN_SZ) &&
104 	       (msg->rx.size <= MSG_DATA_MIN_SZ) &&
105 	       (msg->tx.size == 0 || msg->tx.data) &&
106 	       (msg->rx.size == 0 || msg->rx.data);
107 }
108 
109 static bool tegra_bpmp_is_response_ready(struct tegra_bpmp_channel *channel)
110 {
111 	const struct tegra_bpmp_ops *ops = channel_to_ops(channel);
112 
113 	return ops->is_response_ready(channel);
114 }
115 
116 static bool tegra_bpmp_is_request_ready(struct tegra_bpmp_channel *channel)
117 {
118 	const struct tegra_bpmp_ops *ops = channel_to_ops(channel);
119 
120 	return ops->is_request_ready(channel);
121 }
122 
123 static int tegra_bpmp_wait_response(struct tegra_bpmp_channel *channel)
124 {
125 	unsigned long timeout = channel->bpmp->soc->channels.cpu_tx.timeout;
126 	ktime_t end;
127 
128 	end = ktime_add_us(ktime_get(), timeout);
129 
130 	do {
131 		if (tegra_bpmp_is_response_ready(channel))
132 			return 0;
133 	} while (ktime_before(ktime_get(), end));
134 
135 	return -ETIMEDOUT;
136 }
137 
138 static int tegra_bpmp_ack_response(struct tegra_bpmp_channel *channel)
139 {
140 	const struct tegra_bpmp_ops *ops = channel_to_ops(channel);
141 
142 	return ops->ack_response(channel);
143 }
144 
145 static int tegra_bpmp_ack_request(struct tegra_bpmp_channel *channel)
146 {
147 	const struct tegra_bpmp_ops *ops = channel_to_ops(channel);
148 
149 	return ops->ack_request(channel);
150 }
151 
152 static bool
153 tegra_bpmp_is_request_channel_free(struct tegra_bpmp_channel *channel)
154 {
155 	const struct tegra_bpmp_ops *ops = channel_to_ops(channel);
156 
157 	return ops->is_request_channel_free(channel);
158 }
159 
160 static bool
161 tegra_bpmp_is_response_channel_free(struct tegra_bpmp_channel *channel)
162 {
163 	const struct tegra_bpmp_ops *ops = channel_to_ops(channel);
164 
165 	return ops->is_response_channel_free(channel);
166 }
167 
168 static int
169 tegra_bpmp_wait_request_channel_free(struct tegra_bpmp_channel *channel)
170 {
171 	unsigned long timeout = channel->bpmp->soc->channels.cpu_tx.timeout;
172 	ktime_t start, now;
173 
174 	start = ns_to_ktime(local_clock());
175 
176 	do {
177 		if (tegra_bpmp_is_request_channel_free(channel))
178 			return 0;
179 
180 		now = ns_to_ktime(local_clock());
181 	} while (ktime_us_delta(now, start) < timeout);
182 
183 	return -ETIMEDOUT;
184 }
185 
186 static int tegra_bpmp_post_request(struct tegra_bpmp_channel *channel)
187 {
188 	const struct tegra_bpmp_ops *ops = channel_to_ops(channel);
189 
190 	return ops->post_request(channel);
191 }
192 
193 static int tegra_bpmp_post_response(struct tegra_bpmp_channel *channel)
194 {
195 	const struct tegra_bpmp_ops *ops = channel_to_ops(channel);
196 
197 	return ops->post_response(channel);
198 }
199 
200 static int tegra_bpmp_ring_doorbell(struct tegra_bpmp *bpmp)
201 {
202 	return bpmp->soc->ops->ring_doorbell(bpmp);
203 }
204 
205 static ssize_t __tegra_bpmp_channel_read(struct tegra_bpmp_channel *channel,
206 					 void *data, size_t size, int *ret)
207 {
208 	int err;
209 
210 	if (data && size > 0)
211 		memcpy(data, channel->ib->data, size);
212 
213 	err = tegra_bpmp_ack_response(channel);
214 	if (err < 0)
215 		return err;
216 
217 	*ret = channel->ib->code;
218 
219 	return 0;
220 }
221 
222 static ssize_t tegra_bpmp_channel_read(struct tegra_bpmp_channel *channel,
223 				       void *data, size_t size, int *ret)
224 {
225 	struct tegra_bpmp *bpmp = channel->bpmp;
226 	unsigned long flags;
227 	ssize_t err;
228 	int index;
229 
230 	index = tegra_bpmp_channel_get_thread_index(channel);
231 	if (index < 0) {
232 		err = index;
233 		goto unlock;
234 	}
235 
236 	spin_lock_irqsave(&bpmp->lock, flags);
237 	err = __tegra_bpmp_channel_read(channel, data, size, ret);
238 	clear_bit(index, bpmp->threaded.allocated);
239 	spin_unlock_irqrestore(&bpmp->lock, flags);
240 
241 unlock:
242 	up(&bpmp->threaded.lock);
243 
244 	return err;
245 }
246 
247 static ssize_t __tegra_bpmp_channel_write(struct tegra_bpmp_channel *channel,
248 					  unsigned int mrq, unsigned long flags,
249 					  const void *data, size_t size)
250 {
251 	channel->ob->code = mrq;
252 	channel->ob->flags = flags;
253 
254 	if (data && size > 0)
255 		memcpy(channel->ob->data, data, size);
256 
257 	return tegra_bpmp_post_request(channel);
258 }
259 
260 static struct tegra_bpmp_channel *
261 tegra_bpmp_write_threaded(struct tegra_bpmp *bpmp, unsigned int mrq,
262 			  const void *data, size_t size)
263 {
264 	unsigned long timeout = bpmp->soc->channels.thread.timeout;
265 	unsigned int count = bpmp->soc->channels.thread.count;
266 	struct tegra_bpmp_channel *channel;
267 	unsigned long flags;
268 	unsigned int index;
269 	int err;
270 
271 	err = down_timeout(&bpmp->threaded.lock, usecs_to_jiffies(timeout));
272 	if (err < 0)
273 		return ERR_PTR(err);
274 
275 	spin_lock_irqsave(&bpmp->lock, flags);
276 
277 	index = find_first_zero_bit(bpmp->threaded.allocated, count);
278 	if (index == count) {
279 		err = -EBUSY;
280 		goto unlock;
281 	}
282 
283 	channel = &bpmp->threaded_channels[index];
284 
285 	if (!tegra_bpmp_is_request_channel_free(channel)) {
286 		err = -EBUSY;
287 		goto unlock;
288 	}
289 
290 	set_bit(index, bpmp->threaded.allocated);
291 
292 	err = __tegra_bpmp_channel_write(channel, mrq, MSG_ACK | MSG_RING,
293 					 data, size);
294 	if (err < 0)
295 		goto clear_allocated;
296 
297 	set_bit(index, bpmp->threaded.busy);
298 
299 	spin_unlock_irqrestore(&bpmp->lock, flags);
300 	return channel;
301 
302 clear_allocated:
303 	clear_bit(index, bpmp->threaded.allocated);
304 unlock:
305 	spin_unlock_irqrestore(&bpmp->lock, flags);
306 	up(&bpmp->threaded.lock);
307 
308 	return ERR_PTR(err);
309 }
310 
311 static ssize_t tegra_bpmp_channel_write(struct tegra_bpmp_channel *channel,
312 					unsigned int mrq, unsigned long flags,
313 					const void *data, size_t size)
314 {
315 	int err;
316 
317 	err = tegra_bpmp_wait_request_channel_free(channel);
318 	if (err < 0)
319 		return err;
320 
321 	return __tegra_bpmp_channel_write(channel, mrq, flags, data, size);
322 }
323 
324 int tegra_bpmp_transfer_atomic(struct tegra_bpmp *bpmp,
325 			       struct tegra_bpmp_message *msg)
326 {
327 	struct tegra_bpmp_channel *channel;
328 	int err;
329 
330 	if (WARN_ON(!irqs_disabled()))
331 		return -EPERM;
332 
333 	if (!tegra_bpmp_message_valid(msg))
334 		return -EINVAL;
335 
336 	channel = bpmp->tx_channel;
337 
338 	spin_lock(&bpmp->atomic_tx_lock);
339 
340 	err = tegra_bpmp_channel_write(channel, msg->mrq, MSG_ACK,
341 				       msg->tx.data, msg->tx.size);
342 	if (err < 0) {
343 		spin_unlock(&bpmp->atomic_tx_lock);
344 		return err;
345 	}
346 
347 	spin_unlock(&bpmp->atomic_tx_lock);
348 
349 	err = tegra_bpmp_ring_doorbell(bpmp);
350 	if (err < 0)
351 		return err;
352 
353 	err = tegra_bpmp_wait_response(channel);
354 	if (err < 0)
355 		return err;
356 
357 	return __tegra_bpmp_channel_read(channel, msg->rx.data, msg->rx.size,
358 					 &msg->rx.ret);
359 }
360 EXPORT_SYMBOL_GPL(tegra_bpmp_transfer_atomic);
361 
362 int tegra_bpmp_transfer(struct tegra_bpmp *bpmp,
363 			struct tegra_bpmp_message *msg)
364 {
365 	struct tegra_bpmp_channel *channel;
366 	unsigned long timeout;
367 	int err;
368 
369 	if (WARN_ON(irqs_disabled()))
370 		return -EPERM;
371 
372 	if (!tegra_bpmp_message_valid(msg))
373 		return -EINVAL;
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 = 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 	channel->ob->code = code;
428 
429 	if (data && size > 0)
430 		memcpy(channel->ob->data, 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 = cpu_to_le32(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 ret;
526 
527 	ret = tegra_bpmp_transfer(bpmp, &msg);
528 	if (ret || 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 	request = (struct mrq_ping_request *)channel->ib->data;
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 = 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 		tegra_bpmp_handle_mrq(bpmp, channel->ib->code, channel);
678 
679 	spin_lock(&bpmp->lock);
680 
681 	for_each_set_bit(i, busy, count) {
682 		struct tegra_bpmp_channel *channel;
683 
684 		channel = &bpmp->threaded_channels[i];
685 
686 		if (tegra_bpmp_is_response_ready(channel)) {
687 			tegra_bpmp_channel_signal(channel);
688 			clear_bit(i, busy);
689 		}
690 	}
691 
692 	spin_unlock(&bpmp->lock);
693 }
694 
695 static int tegra_bpmp_probe(struct platform_device *pdev)
696 {
697 	struct tegra_bpmp *bpmp;
698 	char tag[TAG_SZ];
699 	size_t size;
700 	int err;
701 
702 	bpmp = devm_kzalloc(&pdev->dev, sizeof(*bpmp), GFP_KERNEL);
703 	if (!bpmp)
704 		return -ENOMEM;
705 
706 	bpmp->soc = of_device_get_match_data(&pdev->dev);
707 	bpmp->dev = &pdev->dev;
708 
709 	INIT_LIST_HEAD(&bpmp->mrqs);
710 	spin_lock_init(&bpmp->lock);
711 
712 	bpmp->threaded.count = bpmp->soc->channels.thread.count;
713 	sema_init(&bpmp->threaded.lock, bpmp->threaded.count);
714 
715 	size = BITS_TO_LONGS(bpmp->threaded.count) * sizeof(long);
716 
717 	bpmp->threaded.allocated = devm_kzalloc(&pdev->dev, size, GFP_KERNEL);
718 	if (!bpmp->threaded.allocated)
719 		return -ENOMEM;
720 
721 	bpmp->threaded.busy = devm_kzalloc(&pdev->dev, size, GFP_KERNEL);
722 	if (!bpmp->threaded.busy)
723 		return -ENOMEM;
724 
725 	spin_lock_init(&bpmp->atomic_tx_lock);
726 	bpmp->tx_channel = devm_kzalloc(&pdev->dev, sizeof(*bpmp->tx_channel),
727 					GFP_KERNEL);
728 	if (!bpmp->tx_channel)
729 		return -ENOMEM;
730 
731 	bpmp->rx_channel = devm_kzalloc(&pdev->dev, sizeof(*bpmp->rx_channel),
732 	                                GFP_KERNEL);
733 	if (!bpmp->rx_channel)
734 		return -ENOMEM;
735 
736 	bpmp->threaded_channels = devm_kcalloc(&pdev->dev, bpmp->threaded.count,
737 					       sizeof(*bpmp->threaded_channels),
738 					       GFP_KERNEL);
739 	if (!bpmp->threaded_channels)
740 		return -ENOMEM;
741 
742 	err = bpmp->soc->ops->init(bpmp);
743 	if (err < 0)
744 		return err;
745 
746 	err = tegra_bpmp_request_mrq(bpmp, MRQ_PING,
747 				     tegra_bpmp_mrq_handle_ping, bpmp);
748 	if (err < 0)
749 		goto deinit;
750 
751 	err = tegra_bpmp_ping(bpmp);
752 	if (err < 0) {
753 		dev_err(&pdev->dev, "failed to ping BPMP: %d\n", err);
754 		goto free_mrq;
755 	}
756 
757 	err = tegra_bpmp_get_firmware_tag(bpmp, tag, sizeof(tag));
758 	if (err < 0) {
759 		dev_err(&pdev->dev, "failed to get firmware tag: %d\n", err);
760 		goto free_mrq;
761 	}
762 
763 	dev_info(&pdev->dev, "firmware: %.*s\n", (int)sizeof(tag), tag);
764 
765 	platform_set_drvdata(pdev, bpmp);
766 
767 	err = of_platform_default_populate(pdev->dev.of_node, NULL, &pdev->dev);
768 	if (err < 0)
769 		goto free_mrq;
770 
771 	if (of_find_property(pdev->dev.of_node, "#clock-cells", NULL)) {
772 		err = tegra_bpmp_init_clocks(bpmp);
773 		if (err < 0)
774 			goto free_mrq;
775 	}
776 
777 	if (of_find_property(pdev->dev.of_node, "#reset-cells", NULL)) {
778 		err = tegra_bpmp_init_resets(bpmp);
779 		if (err < 0)
780 			goto free_mrq;
781 	}
782 
783 	if (of_find_property(pdev->dev.of_node, "#power-domain-cells", NULL)) {
784 		err = tegra_bpmp_init_powergates(bpmp);
785 		if (err < 0)
786 			goto free_mrq;
787 	}
788 
789 	err = tegra_bpmp_init_debugfs(bpmp);
790 	if (err < 0)
791 		dev_err(&pdev->dev, "debugfs initialization failed: %d\n", err);
792 
793 	return 0;
794 
795 free_mrq:
796 	tegra_bpmp_free_mrq(bpmp, MRQ_PING, bpmp);
797 deinit:
798 	if (bpmp->soc->ops->deinit)
799 		bpmp->soc->ops->deinit(bpmp);
800 
801 	return err;
802 }
803 
804 static int __maybe_unused tegra_bpmp_resume(struct device *dev)
805 {
806 	struct tegra_bpmp *bpmp = dev_get_drvdata(dev);
807 
808 	if (bpmp->soc->ops->resume)
809 		return bpmp->soc->ops->resume(bpmp);
810 	else
811 		return 0;
812 }
813 
814 static SIMPLE_DEV_PM_OPS(tegra_bpmp_pm_ops, NULL, tegra_bpmp_resume);
815 
816 #if IS_ENABLED(CONFIG_ARCH_TEGRA_186_SOC) || \
817     IS_ENABLED(CONFIG_ARCH_TEGRA_194_SOC)
818 static const struct tegra_bpmp_soc tegra186_soc = {
819 	.channels = {
820 		.cpu_tx = {
821 			.offset = 3,
822 			.timeout = 60 * USEC_PER_SEC,
823 		},
824 		.thread = {
825 			.offset = 0,
826 			.count = 3,
827 			.timeout = 600 * USEC_PER_SEC,
828 		},
829 		.cpu_rx = {
830 			.offset = 13,
831 			.timeout = 0,
832 		},
833 	},
834 	.ops = &tegra186_bpmp_ops,
835 	.num_resets = 193,
836 };
837 #endif
838 
839 #if IS_ENABLED(CONFIG_ARCH_TEGRA_210_SOC)
840 static const struct tegra_bpmp_soc tegra210_soc = {
841 	.channels = {
842 		.cpu_tx = {
843 			.offset = 0,
844 			.count = 1,
845 			.timeout = 60 * USEC_PER_SEC,
846 		},
847 		.thread = {
848 			.offset = 4,
849 			.count = 1,
850 			.timeout = 600 * USEC_PER_SEC,
851 		},
852 		.cpu_rx = {
853 			.offset = 8,
854 			.count = 1,
855 			.timeout = 0,
856 		},
857 	},
858 	.ops = &tegra210_bpmp_ops,
859 };
860 #endif
861 
862 static const struct of_device_id tegra_bpmp_match[] = {
863 #if IS_ENABLED(CONFIG_ARCH_TEGRA_186_SOC) || \
864     IS_ENABLED(CONFIG_ARCH_TEGRA_194_SOC)
865 	{ .compatible = "nvidia,tegra186-bpmp", .data = &tegra186_soc },
866 #endif
867 #if IS_ENABLED(CONFIG_ARCH_TEGRA_210_SOC)
868 	{ .compatible = "nvidia,tegra210-bpmp", .data = &tegra210_soc },
869 #endif
870 	{ }
871 };
872 
873 static struct platform_driver tegra_bpmp_driver = {
874 	.driver = {
875 		.name = "tegra-bpmp",
876 		.of_match_table = tegra_bpmp_match,
877 		.pm = &tegra_bpmp_pm_ops,
878 	},
879 	.probe = tegra_bpmp_probe,
880 };
881 
882 static int __init tegra_bpmp_init(void)
883 {
884 	return platform_driver_register(&tegra_bpmp_driver);
885 }
886 core_initcall(tegra_bpmp_init);
887