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 return index; 216 217 spin_lock_irqsave(&bpmp->lock, flags); 218 err = __tegra_bpmp_channel_read(channel, data, size); 219 clear_bit(index, bpmp->threaded.allocated); 220 spin_unlock_irqrestore(&bpmp->lock, flags); 221 222 up(&bpmp->threaded.lock); 223 224 return err; 225 } 226 227 static ssize_t __tegra_bpmp_channel_write(struct tegra_bpmp_channel *channel, 228 unsigned int mrq, unsigned long flags, 229 const void *data, size_t size) 230 { 231 channel->ob->code = mrq; 232 channel->ob->flags = flags; 233 234 if (data && size > 0) 235 memcpy(channel->ob->data, data, size); 236 237 return tegra_ivc_write_advance(channel->ivc); 238 } 239 240 static struct tegra_bpmp_channel * 241 tegra_bpmp_write_threaded(struct tegra_bpmp *bpmp, unsigned int mrq, 242 const void *data, size_t size) 243 { 244 unsigned long timeout = bpmp->soc->channels.thread.timeout; 245 unsigned int count = bpmp->soc->channels.thread.count; 246 struct tegra_bpmp_channel *channel; 247 unsigned long flags; 248 unsigned int index; 249 int err; 250 251 err = down_timeout(&bpmp->threaded.lock, usecs_to_jiffies(timeout)); 252 if (err < 0) 253 return ERR_PTR(err); 254 255 spin_lock_irqsave(&bpmp->lock, flags); 256 257 index = find_first_zero_bit(bpmp->threaded.allocated, count); 258 if (index == count) { 259 channel = ERR_PTR(-EBUSY); 260 goto unlock; 261 } 262 263 channel = tegra_bpmp_channel_get_thread(bpmp, index); 264 if (!channel) { 265 channel = ERR_PTR(-EINVAL); 266 goto unlock; 267 } 268 269 if (!tegra_bpmp_master_free(channel)) { 270 channel = ERR_PTR(-EBUSY); 271 goto unlock; 272 } 273 274 set_bit(index, bpmp->threaded.allocated); 275 276 err = __tegra_bpmp_channel_write(channel, mrq, MSG_ACK | MSG_RING, 277 data, size); 278 if (err < 0) { 279 clear_bit(index, bpmp->threaded.allocated); 280 goto unlock; 281 } 282 283 set_bit(index, bpmp->threaded.busy); 284 285 unlock: 286 spin_unlock_irqrestore(&bpmp->lock, flags); 287 return channel; 288 } 289 290 static ssize_t tegra_bpmp_channel_write(struct tegra_bpmp_channel *channel, 291 unsigned int mrq, unsigned long flags, 292 const void *data, size_t size) 293 { 294 int err; 295 296 err = tegra_bpmp_wait_master_free(channel); 297 if (err < 0) 298 return err; 299 300 return __tegra_bpmp_channel_write(channel, mrq, flags, data, size); 301 } 302 303 int tegra_bpmp_transfer_atomic(struct tegra_bpmp *bpmp, 304 struct tegra_bpmp_message *msg) 305 { 306 struct tegra_bpmp_channel *channel; 307 int err; 308 309 if (WARN_ON(!irqs_disabled())) 310 return -EPERM; 311 312 if (!tegra_bpmp_message_valid(msg)) 313 return -EINVAL; 314 315 channel = tegra_bpmp_channel_get_tx(bpmp); 316 317 err = tegra_bpmp_channel_write(channel, msg->mrq, MSG_ACK, 318 msg->tx.data, msg->tx.size); 319 if (err < 0) 320 return err; 321 322 err = mbox_send_message(bpmp->mbox.channel, NULL); 323 if (err < 0) 324 return err; 325 326 mbox_client_txdone(bpmp->mbox.channel, 0); 327 328 err = tegra_bpmp_wait_ack(channel); 329 if (err < 0) 330 return err; 331 332 return __tegra_bpmp_channel_read(channel, msg->rx.data, msg->rx.size); 333 } 334 EXPORT_SYMBOL_GPL(tegra_bpmp_transfer_atomic); 335 336 int tegra_bpmp_transfer(struct tegra_bpmp *bpmp, 337 struct tegra_bpmp_message *msg) 338 { 339 struct tegra_bpmp_channel *channel; 340 unsigned long timeout; 341 int err; 342 343 if (WARN_ON(irqs_disabled())) 344 return -EPERM; 345 346 if (!tegra_bpmp_message_valid(msg)) 347 return -EINVAL; 348 349 channel = tegra_bpmp_write_threaded(bpmp, msg->mrq, msg->tx.data, 350 msg->tx.size); 351 if (IS_ERR(channel)) 352 return PTR_ERR(channel); 353 354 err = mbox_send_message(bpmp->mbox.channel, NULL); 355 if (err < 0) 356 return err; 357 358 mbox_client_txdone(bpmp->mbox.channel, 0); 359 360 timeout = usecs_to_jiffies(bpmp->soc->channels.thread.timeout); 361 362 err = wait_for_completion_timeout(&channel->completion, timeout); 363 if (err == 0) 364 return -ETIMEDOUT; 365 366 return tegra_bpmp_channel_read(channel, msg->rx.data, msg->rx.size); 367 } 368 EXPORT_SYMBOL_GPL(tegra_bpmp_transfer); 369 370 static struct tegra_bpmp_mrq *tegra_bpmp_find_mrq(struct tegra_bpmp *bpmp, 371 unsigned int mrq) 372 { 373 struct tegra_bpmp_mrq *entry; 374 375 list_for_each_entry(entry, &bpmp->mrqs, list) 376 if (entry->mrq == mrq) 377 return entry; 378 379 return NULL; 380 } 381 382 static void tegra_bpmp_mrq_return(struct tegra_bpmp_channel *channel, 383 int code, const void *data, size_t size) 384 { 385 unsigned long flags = channel->ib->flags; 386 struct tegra_bpmp *bpmp = channel->bpmp; 387 struct tegra_bpmp_mb_data *frame; 388 int err; 389 390 if (WARN_ON(size > MSG_DATA_MIN_SZ)) 391 return; 392 393 err = tegra_ivc_read_advance(channel->ivc); 394 if (WARN_ON(err < 0)) 395 return; 396 397 if ((flags & MSG_ACK) == 0) 398 return; 399 400 frame = tegra_ivc_write_get_next_frame(channel->ivc); 401 if (WARN_ON(IS_ERR(frame))) 402 return; 403 404 frame->code = code; 405 406 if (data && size > 0) 407 memcpy(frame->data, data, size); 408 409 err = tegra_ivc_write_advance(channel->ivc); 410 if (WARN_ON(err < 0)) 411 return; 412 413 if (flags & MSG_RING) { 414 err = mbox_send_message(bpmp->mbox.channel, NULL); 415 if (WARN_ON(err < 0)) 416 return; 417 418 mbox_client_txdone(bpmp->mbox.channel, 0); 419 } 420 } 421 422 static void tegra_bpmp_handle_mrq(struct tegra_bpmp *bpmp, 423 unsigned int mrq, 424 struct tegra_bpmp_channel *channel) 425 { 426 struct tegra_bpmp_mrq *entry; 427 u32 zero = 0; 428 429 spin_lock(&bpmp->lock); 430 431 entry = tegra_bpmp_find_mrq(bpmp, mrq); 432 if (!entry) { 433 spin_unlock(&bpmp->lock); 434 tegra_bpmp_mrq_return(channel, -EINVAL, &zero, sizeof(zero)); 435 return; 436 } 437 438 entry->handler(mrq, channel, entry->data); 439 440 spin_unlock(&bpmp->lock); 441 } 442 443 int tegra_bpmp_request_mrq(struct tegra_bpmp *bpmp, unsigned int mrq, 444 tegra_bpmp_mrq_handler_t handler, void *data) 445 { 446 struct tegra_bpmp_mrq *entry; 447 unsigned long flags; 448 449 if (!handler) 450 return -EINVAL; 451 452 entry = devm_kzalloc(bpmp->dev, sizeof(*entry), GFP_KERNEL); 453 if (!entry) 454 return -ENOMEM; 455 456 spin_lock_irqsave(&bpmp->lock, flags); 457 458 entry->mrq = mrq; 459 entry->handler = handler; 460 entry->data = data; 461 list_add(&entry->list, &bpmp->mrqs); 462 463 spin_unlock_irqrestore(&bpmp->lock, flags); 464 465 return 0; 466 } 467 EXPORT_SYMBOL_GPL(tegra_bpmp_request_mrq); 468 469 void tegra_bpmp_free_mrq(struct tegra_bpmp *bpmp, unsigned int mrq, void *data) 470 { 471 struct tegra_bpmp_mrq *entry; 472 unsigned long flags; 473 474 spin_lock_irqsave(&bpmp->lock, flags); 475 476 entry = tegra_bpmp_find_mrq(bpmp, mrq); 477 if (!entry) 478 goto unlock; 479 480 list_del(&entry->list); 481 devm_kfree(bpmp->dev, entry); 482 483 unlock: 484 spin_unlock_irqrestore(&bpmp->lock, flags); 485 } 486 EXPORT_SYMBOL_GPL(tegra_bpmp_free_mrq); 487 488 static void tegra_bpmp_mrq_handle_ping(unsigned int mrq, 489 struct tegra_bpmp_channel *channel, 490 void *data) 491 { 492 struct mrq_ping_request *request; 493 struct mrq_ping_response response; 494 495 request = (struct mrq_ping_request *)channel->ib->data; 496 497 memset(&response, 0, sizeof(response)); 498 response.reply = request->challenge << 1; 499 500 tegra_bpmp_mrq_return(channel, 0, &response, sizeof(response)); 501 } 502 503 static int tegra_bpmp_ping(struct tegra_bpmp *bpmp) 504 { 505 struct mrq_ping_response response; 506 struct mrq_ping_request request; 507 struct tegra_bpmp_message msg; 508 unsigned long flags; 509 ktime_t start, end; 510 int err; 511 512 memset(&request, 0, sizeof(request)); 513 request.challenge = 1; 514 515 memset(&response, 0, sizeof(response)); 516 517 memset(&msg, 0, sizeof(msg)); 518 msg.mrq = MRQ_PING; 519 msg.tx.data = &request; 520 msg.tx.size = sizeof(request); 521 msg.rx.data = &response; 522 msg.rx.size = sizeof(response); 523 524 local_irq_save(flags); 525 start = ktime_get(); 526 err = tegra_bpmp_transfer_atomic(bpmp, &msg); 527 end = ktime_get(); 528 local_irq_restore(flags); 529 530 if (!err) 531 dev_dbg(bpmp->dev, 532 "ping ok: challenge: %u, response: %u, time: %lld\n", 533 request.challenge, response.reply, 534 ktime_to_us(ktime_sub(end, start))); 535 536 return err; 537 } 538 539 static int tegra_bpmp_get_firmware_tag(struct tegra_bpmp *bpmp, char *tag, 540 size_t size) 541 { 542 struct mrq_query_tag_request request; 543 struct tegra_bpmp_message msg; 544 unsigned long flags; 545 dma_addr_t phys; 546 void *virt; 547 int err; 548 549 virt = dma_alloc_coherent(bpmp->dev, MSG_DATA_MIN_SZ, &phys, 550 GFP_KERNEL | GFP_DMA32); 551 if (!virt) 552 return -ENOMEM; 553 554 memset(&request, 0, sizeof(request)); 555 request.addr = phys; 556 557 memset(&msg, 0, sizeof(msg)); 558 msg.mrq = MRQ_QUERY_TAG; 559 msg.tx.data = &request; 560 msg.tx.size = sizeof(request); 561 562 local_irq_save(flags); 563 err = tegra_bpmp_transfer_atomic(bpmp, &msg); 564 local_irq_restore(flags); 565 566 if (err == 0) 567 strlcpy(tag, virt, size); 568 569 dma_free_coherent(bpmp->dev, MSG_DATA_MIN_SZ, virt, phys); 570 571 return err; 572 } 573 574 static void tegra_bpmp_channel_signal(struct tegra_bpmp_channel *channel) 575 { 576 unsigned long flags = channel->ob->flags; 577 578 if ((flags & MSG_RING) == 0) 579 return; 580 581 complete(&channel->completion); 582 } 583 584 static void tegra_bpmp_handle_rx(struct mbox_client *client, void *data) 585 { 586 struct tegra_bpmp *bpmp = mbox_client_to_bpmp(client); 587 struct tegra_bpmp_channel *channel; 588 unsigned int i, count; 589 unsigned long *busy; 590 591 channel = tegra_bpmp_channel_get_rx(bpmp); 592 count = bpmp->soc->channels.thread.count; 593 busy = bpmp->threaded.busy; 594 595 if (tegra_bpmp_master_acked(channel)) 596 tegra_bpmp_handle_mrq(bpmp, channel->ib->code, channel); 597 598 spin_lock(&bpmp->lock); 599 600 for_each_set_bit(i, busy, count) { 601 struct tegra_bpmp_channel *channel; 602 603 channel = tegra_bpmp_channel_get_thread(bpmp, i); 604 if (!channel) 605 continue; 606 607 if (tegra_bpmp_master_acked(channel)) { 608 tegra_bpmp_channel_signal(channel); 609 clear_bit(i, busy); 610 } 611 } 612 613 spin_unlock(&bpmp->lock); 614 } 615 616 static void tegra_bpmp_ivc_notify(struct tegra_ivc *ivc, void *data) 617 { 618 struct tegra_bpmp *bpmp = data; 619 int err; 620 621 if (WARN_ON(bpmp->mbox.channel == NULL)) 622 return; 623 624 err = mbox_send_message(bpmp->mbox.channel, NULL); 625 if (err < 0) 626 return; 627 628 mbox_client_txdone(bpmp->mbox.channel, 0); 629 } 630 631 static int tegra_bpmp_channel_init(struct tegra_bpmp_channel *channel, 632 struct tegra_bpmp *bpmp, 633 unsigned int index) 634 { 635 size_t message_size, queue_size; 636 unsigned int offset; 637 int err; 638 639 channel->ivc = devm_kzalloc(bpmp->dev, sizeof(*channel->ivc), 640 GFP_KERNEL); 641 if (!channel->ivc) 642 return -ENOMEM; 643 644 message_size = tegra_ivc_align(MSG_MIN_SZ); 645 queue_size = tegra_ivc_total_queue_size(message_size); 646 offset = queue_size * index; 647 648 err = tegra_ivc_init(channel->ivc, NULL, 649 bpmp->rx.virt + offset, bpmp->rx.phys + offset, 650 bpmp->tx.virt + offset, bpmp->tx.phys + offset, 651 1, message_size, tegra_bpmp_ivc_notify, 652 bpmp); 653 if (err < 0) { 654 dev_err(bpmp->dev, "failed to setup IVC for channel %u: %d\n", 655 index, err); 656 return err; 657 } 658 659 init_completion(&channel->completion); 660 channel->bpmp = bpmp; 661 662 return 0; 663 } 664 665 static void tegra_bpmp_channel_reset(struct tegra_bpmp_channel *channel) 666 { 667 /* reset the channel state */ 668 tegra_ivc_reset(channel->ivc); 669 670 /* sync the channel state with BPMP */ 671 while (tegra_ivc_notified(channel->ivc)) 672 ; 673 } 674 675 static void tegra_bpmp_channel_cleanup(struct tegra_bpmp_channel *channel) 676 { 677 tegra_ivc_cleanup(channel->ivc); 678 } 679 680 static int tegra_bpmp_probe(struct platform_device *pdev) 681 { 682 struct tegra_bpmp_channel *channel; 683 struct tegra_bpmp *bpmp; 684 unsigned int i; 685 char tag[32]; 686 size_t size; 687 int err; 688 689 bpmp = devm_kzalloc(&pdev->dev, sizeof(*bpmp), GFP_KERNEL); 690 if (!bpmp) 691 return -ENOMEM; 692 693 bpmp->soc = of_device_get_match_data(&pdev->dev); 694 bpmp->dev = &pdev->dev; 695 696 bpmp->tx.pool = of_gen_pool_get(pdev->dev.of_node, "shmem", 0); 697 if (!bpmp->tx.pool) { 698 dev_err(&pdev->dev, "TX shmem pool not found\n"); 699 return -ENOMEM; 700 } 701 702 bpmp->tx.virt = gen_pool_dma_alloc(bpmp->tx.pool, 4096, &bpmp->tx.phys); 703 if (!bpmp->tx.virt) { 704 dev_err(&pdev->dev, "failed to allocate from TX pool\n"); 705 return -ENOMEM; 706 } 707 708 bpmp->rx.pool = of_gen_pool_get(pdev->dev.of_node, "shmem", 1); 709 if (!bpmp->rx.pool) { 710 dev_err(&pdev->dev, "RX shmem pool not found\n"); 711 err = -ENOMEM; 712 goto free_tx; 713 } 714 715 bpmp->rx.virt = gen_pool_dma_alloc(bpmp->rx.pool, 4096, &bpmp->rx.phys); 716 if (!bpmp->rx.pool) { 717 dev_err(&pdev->dev, "failed to allocate from RX pool\n"); 718 err = -ENOMEM; 719 goto free_tx; 720 } 721 722 INIT_LIST_HEAD(&bpmp->mrqs); 723 spin_lock_init(&bpmp->lock); 724 725 bpmp->threaded.count = bpmp->soc->channels.thread.count; 726 sema_init(&bpmp->threaded.lock, bpmp->threaded.count); 727 728 size = BITS_TO_LONGS(bpmp->threaded.count) * sizeof(long); 729 730 bpmp->threaded.allocated = devm_kzalloc(&pdev->dev, size, GFP_KERNEL); 731 if (!bpmp->threaded.allocated) { 732 err = -ENOMEM; 733 goto free_rx; 734 } 735 736 bpmp->threaded.busy = devm_kzalloc(&pdev->dev, size, GFP_KERNEL); 737 if (!bpmp->threaded.busy) { 738 err = -ENOMEM; 739 goto free_rx; 740 } 741 742 bpmp->num_channels = bpmp->soc->channels.cpu_tx.count + 743 bpmp->soc->channels.thread.count + 744 bpmp->soc->channels.cpu_rx.count; 745 746 bpmp->channels = devm_kcalloc(&pdev->dev, bpmp->num_channels, 747 sizeof(*channel), GFP_KERNEL); 748 if (!bpmp->channels) { 749 err = -ENOMEM; 750 goto free_rx; 751 } 752 753 /* message channel initialization */ 754 for (i = 0; i < bpmp->num_channels; i++) { 755 struct tegra_bpmp_channel *channel = &bpmp->channels[i]; 756 757 err = tegra_bpmp_channel_init(channel, bpmp, i); 758 if (err < 0) 759 goto cleanup_channels; 760 } 761 762 /* mbox registration */ 763 bpmp->mbox.client.dev = &pdev->dev; 764 bpmp->mbox.client.rx_callback = tegra_bpmp_handle_rx; 765 bpmp->mbox.client.tx_block = false; 766 bpmp->mbox.client.knows_txdone = false; 767 768 bpmp->mbox.channel = mbox_request_channel(&bpmp->mbox.client, 0); 769 if (IS_ERR(bpmp->mbox.channel)) { 770 err = PTR_ERR(bpmp->mbox.channel); 771 dev_err(&pdev->dev, "failed to get HSP mailbox: %d\n", err); 772 goto cleanup_channels; 773 } 774 775 /* reset message channels */ 776 for (i = 0; i < bpmp->num_channels; i++) { 777 struct tegra_bpmp_channel *channel = &bpmp->channels[i]; 778 779 tegra_bpmp_channel_reset(channel); 780 } 781 782 err = tegra_bpmp_request_mrq(bpmp, MRQ_PING, 783 tegra_bpmp_mrq_handle_ping, bpmp); 784 if (err < 0) 785 goto free_mbox; 786 787 err = tegra_bpmp_ping(bpmp); 788 if (err < 0) { 789 dev_err(&pdev->dev, "failed to ping BPMP: %d\n", err); 790 goto free_mrq; 791 } 792 793 err = tegra_bpmp_get_firmware_tag(bpmp, tag, sizeof(tag) - 1); 794 if (err < 0) { 795 dev_err(&pdev->dev, "failed to get firmware tag: %d\n", err); 796 goto free_mrq; 797 } 798 799 dev_info(&pdev->dev, "firmware: %s\n", tag); 800 801 err = of_platform_default_populate(pdev->dev.of_node, NULL, &pdev->dev); 802 if (err < 0) 803 goto free_mrq; 804 805 err = tegra_bpmp_init_clocks(bpmp); 806 if (err < 0) 807 goto free_mrq; 808 809 err = tegra_bpmp_init_resets(bpmp); 810 if (err < 0) 811 goto free_mrq; 812 813 platform_set_drvdata(pdev, bpmp); 814 815 return 0; 816 817 free_mrq: 818 tegra_bpmp_free_mrq(bpmp, MRQ_PING, bpmp); 819 free_mbox: 820 mbox_free_channel(bpmp->mbox.channel); 821 cleanup_channels: 822 while (i--) 823 tegra_bpmp_channel_cleanup(&bpmp->channels[i]); 824 free_rx: 825 gen_pool_free(bpmp->rx.pool, (unsigned long)bpmp->rx.virt, 4096); 826 free_tx: 827 gen_pool_free(bpmp->tx.pool, (unsigned long)bpmp->tx.virt, 4096); 828 return err; 829 } 830 831 static const struct tegra_bpmp_soc tegra186_soc = { 832 .channels = { 833 .cpu_tx = { 834 .offset = 0, 835 .count = 6, 836 .timeout = 60 * USEC_PER_SEC, 837 }, 838 .thread = { 839 .offset = 6, 840 .count = 7, 841 .timeout = 600 * USEC_PER_SEC, 842 }, 843 .cpu_rx = { 844 .offset = 13, 845 .count = 1, 846 .timeout = 0, 847 }, 848 }, 849 .num_resets = 193, 850 }; 851 852 static const struct of_device_id tegra_bpmp_match[] = { 853 { .compatible = "nvidia,tegra186-bpmp", .data = &tegra186_soc }, 854 { } 855 }; 856 857 static struct platform_driver tegra_bpmp_driver = { 858 .driver = { 859 .name = "tegra-bpmp", 860 .of_match_table = tegra_bpmp_match, 861 }, 862 .probe = tegra_bpmp_probe, 863 }; 864 865 static int __init tegra_bpmp_init(void) 866 { 867 return platform_driver_register(&tegra_bpmp_driver); 868 } 869 core_initcall(tegra_bpmp_init); 870