| .. | .. |
|---|
| 1 | +// SPDX-License-Identifier: GPL-2.0-only |
|---|
| 1 | 2 | /* |
|---|
| 2 | 3 | * 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 | 4 | */ |
|---|
| 13 | 5 | |
|---|
| 14 | 6 | #include <linux/clk/tegra.h> |
|---|
| 15 | 7 | #include <linux/genalloc.h> |
|---|
| 16 | 8 | #include <linux/mailbox_client.h> |
|---|
| 9 | +#include <linux/module.h> |
|---|
| 17 | 10 | #include <linux/of.h> |
|---|
| 18 | 11 | #include <linux/of_address.h> |
|---|
| 19 | 12 | #include <linux/of_device.h> |
|---|
| 20 | 13 | #include <linux/platform_device.h> |
|---|
| 14 | +#include <linux/pm.h> |
|---|
| 21 | 15 | #include <linux/semaphore.h> |
|---|
| 22 | 16 | #include <linux/sched/clock.h> |
|---|
| 23 | 17 | |
|---|
| .. | .. |
|---|
| 25 | 19 | #include <soc/tegra/bpmp-abi.h> |
|---|
| 26 | 20 | #include <soc/tegra/ivc.h> |
|---|
| 27 | 21 | |
|---|
| 22 | +#include "bpmp-private.h" |
|---|
| 23 | + |
|---|
| 28 | 24 | #define MSG_ACK BIT(0) |
|---|
| 29 | 25 | #define MSG_RING BIT(1) |
|---|
| 26 | +#define TAG_SZ 32 |
|---|
| 30 | 27 | |
|---|
| 31 | 28 | static inline struct tegra_bpmp * |
|---|
| 32 | 29 | mbox_client_to_bpmp(struct mbox_client *client) |
|---|
| 33 | 30 | { |
|---|
| 34 | 31 | return container_of(client, struct tegra_bpmp, mbox.client); |
|---|
| 32 | +} |
|---|
| 33 | + |
|---|
| 34 | +static inline const struct tegra_bpmp_ops * |
|---|
| 35 | +channel_to_ops(struct tegra_bpmp_channel *channel) |
|---|
| 36 | +{ |
|---|
| 37 | + struct tegra_bpmp *bpmp = channel->bpmp; |
|---|
| 38 | + |
|---|
| 39 | + return bpmp->soc->ops; |
|---|
| 35 | 40 | } |
|---|
| 36 | 41 | |
|---|
| 37 | 42 | struct tegra_bpmp *tegra_bpmp_get(struct device *dev) |
|---|
| .. | .. |
|---|
| 94 | 99 | (msg->rx.size == 0 || msg->rx.data); |
|---|
| 95 | 100 | } |
|---|
| 96 | 101 | |
|---|
| 97 | | -static bool tegra_bpmp_master_acked(struct tegra_bpmp_channel *channel) |
|---|
| 102 | +static bool tegra_bpmp_is_response_ready(struct tegra_bpmp_channel *channel) |
|---|
| 98 | 103 | { |
|---|
| 99 | | - void *frame; |
|---|
| 104 | + const struct tegra_bpmp_ops *ops = channel_to_ops(channel); |
|---|
| 100 | 105 | |
|---|
| 101 | | - frame = tegra_ivc_read_get_next_frame(channel->ivc); |
|---|
| 102 | | - if (IS_ERR(frame)) { |
|---|
| 103 | | - channel->ib = NULL; |
|---|
| 104 | | - return false; |
|---|
| 105 | | - } |
|---|
| 106 | | - |
|---|
| 107 | | - channel->ib = frame; |
|---|
| 108 | | - |
|---|
| 109 | | - return true; |
|---|
| 106 | + return ops->is_response_ready(channel); |
|---|
| 110 | 107 | } |
|---|
| 111 | 108 | |
|---|
| 112 | | -static int tegra_bpmp_wait_ack(struct tegra_bpmp_channel *channel) |
|---|
| 109 | +static bool tegra_bpmp_is_request_ready(struct tegra_bpmp_channel *channel) |
|---|
| 110 | +{ |
|---|
| 111 | + const struct tegra_bpmp_ops *ops = channel_to_ops(channel); |
|---|
| 112 | + |
|---|
| 113 | + return ops->is_request_ready(channel); |
|---|
| 114 | +} |
|---|
| 115 | + |
|---|
| 116 | +static int tegra_bpmp_wait_response(struct tegra_bpmp_channel *channel) |
|---|
| 113 | 117 | { |
|---|
| 114 | 118 | unsigned long timeout = channel->bpmp->soc->channels.cpu_tx.timeout; |
|---|
| 115 | 119 | ktime_t end; |
|---|
| .. | .. |
|---|
| 117 | 121 | end = ktime_add_us(ktime_get(), timeout); |
|---|
| 118 | 122 | |
|---|
| 119 | 123 | do { |
|---|
| 120 | | - if (tegra_bpmp_master_acked(channel)) |
|---|
| 124 | + if (tegra_bpmp_is_response_ready(channel)) |
|---|
| 121 | 125 | return 0; |
|---|
| 122 | 126 | } while (ktime_before(ktime_get(), end)); |
|---|
| 123 | 127 | |
|---|
| 124 | 128 | return -ETIMEDOUT; |
|---|
| 125 | 129 | } |
|---|
| 126 | 130 | |
|---|
| 127 | | -static bool tegra_bpmp_master_free(struct tegra_bpmp_channel *channel) |
|---|
| 131 | +static int tegra_bpmp_ack_response(struct tegra_bpmp_channel *channel) |
|---|
| 128 | 132 | { |
|---|
| 129 | | - void *frame; |
|---|
| 133 | + const struct tegra_bpmp_ops *ops = channel_to_ops(channel); |
|---|
| 130 | 134 | |
|---|
| 131 | | - frame = tegra_ivc_write_get_next_frame(channel->ivc); |
|---|
| 132 | | - if (IS_ERR(frame)) { |
|---|
| 133 | | - channel->ob = NULL; |
|---|
| 134 | | - return false; |
|---|
| 135 | | - } |
|---|
| 136 | | - |
|---|
| 137 | | - channel->ob = frame; |
|---|
| 138 | | - |
|---|
| 139 | | - return true; |
|---|
| 135 | + return ops->ack_response(channel); |
|---|
| 140 | 136 | } |
|---|
| 141 | 137 | |
|---|
| 142 | | -static int tegra_bpmp_wait_master_free(struct tegra_bpmp_channel *channel) |
|---|
| 138 | +static int tegra_bpmp_ack_request(struct tegra_bpmp_channel *channel) |
|---|
| 139 | +{ |
|---|
| 140 | + const struct tegra_bpmp_ops *ops = channel_to_ops(channel); |
|---|
| 141 | + |
|---|
| 142 | + return ops->ack_request(channel); |
|---|
| 143 | +} |
|---|
| 144 | + |
|---|
| 145 | +static bool |
|---|
| 146 | +tegra_bpmp_is_request_channel_free(struct tegra_bpmp_channel *channel) |
|---|
| 147 | +{ |
|---|
| 148 | + const struct tegra_bpmp_ops *ops = channel_to_ops(channel); |
|---|
| 149 | + |
|---|
| 150 | + return ops->is_request_channel_free(channel); |
|---|
| 151 | +} |
|---|
| 152 | + |
|---|
| 153 | +static bool |
|---|
| 154 | +tegra_bpmp_is_response_channel_free(struct tegra_bpmp_channel *channel) |
|---|
| 155 | +{ |
|---|
| 156 | + const struct tegra_bpmp_ops *ops = channel_to_ops(channel); |
|---|
| 157 | + |
|---|
| 158 | + return ops->is_response_channel_free(channel); |
|---|
| 159 | +} |
|---|
| 160 | + |
|---|
| 161 | +static int |
|---|
| 162 | +tegra_bpmp_wait_request_channel_free(struct tegra_bpmp_channel *channel) |
|---|
| 143 | 163 | { |
|---|
| 144 | 164 | unsigned long timeout = channel->bpmp->soc->channels.cpu_tx.timeout; |
|---|
| 145 | 165 | ktime_t start, now; |
|---|
| .. | .. |
|---|
| 147 | 167 | start = ns_to_ktime(local_clock()); |
|---|
| 148 | 168 | |
|---|
| 149 | 169 | do { |
|---|
| 150 | | - if (tegra_bpmp_master_free(channel)) |
|---|
| 170 | + if (tegra_bpmp_is_request_channel_free(channel)) |
|---|
| 151 | 171 | return 0; |
|---|
| 152 | 172 | |
|---|
| 153 | 173 | now = ns_to_ktime(local_clock()); |
|---|
| 154 | 174 | } while (ktime_us_delta(now, start) < timeout); |
|---|
| 155 | 175 | |
|---|
| 156 | 176 | return -ETIMEDOUT; |
|---|
| 177 | +} |
|---|
| 178 | + |
|---|
| 179 | +static int tegra_bpmp_post_request(struct tegra_bpmp_channel *channel) |
|---|
| 180 | +{ |
|---|
| 181 | + const struct tegra_bpmp_ops *ops = channel_to_ops(channel); |
|---|
| 182 | + |
|---|
| 183 | + return ops->post_request(channel); |
|---|
| 184 | +} |
|---|
| 185 | + |
|---|
| 186 | +static int tegra_bpmp_post_response(struct tegra_bpmp_channel *channel) |
|---|
| 187 | +{ |
|---|
| 188 | + const struct tegra_bpmp_ops *ops = channel_to_ops(channel); |
|---|
| 189 | + |
|---|
| 190 | + return ops->post_response(channel); |
|---|
| 191 | +} |
|---|
| 192 | + |
|---|
| 193 | +static int tegra_bpmp_ring_doorbell(struct tegra_bpmp *bpmp) |
|---|
| 194 | +{ |
|---|
| 195 | + return bpmp->soc->ops->ring_doorbell(bpmp); |
|---|
| 157 | 196 | } |
|---|
| 158 | 197 | |
|---|
| 159 | 198 | static ssize_t __tegra_bpmp_channel_read(struct tegra_bpmp_channel *channel, |
|---|
| .. | .. |
|---|
| 164 | 203 | if (data && size > 0) |
|---|
| 165 | 204 | memcpy(data, channel->ib->data, size); |
|---|
| 166 | 205 | |
|---|
| 167 | | - err = tegra_ivc_read_advance(channel->ivc); |
|---|
| 206 | + err = tegra_bpmp_ack_response(channel); |
|---|
| 168 | 207 | if (err < 0) |
|---|
| 169 | 208 | return err; |
|---|
| 170 | 209 | |
|---|
| .. | .. |
|---|
| 208 | 247 | if (data && size > 0) |
|---|
| 209 | 248 | memcpy(channel->ob->data, data, size); |
|---|
| 210 | 249 | |
|---|
| 211 | | - return tegra_ivc_write_advance(channel->ivc); |
|---|
| 250 | + return tegra_bpmp_post_request(channel); |
|---|
| 212 | 251 | } |
|---|
| 213 | 252 | |
|---|
| 214 | 253 | static struct tegra_bpmp_channel * |
|---|
| .. | .. |
|---|
| 236 | 275 | |
|---|
| 237 | 276 | channel = &bpmp->threaded_channels[index]; |
|---|
| 238 | 277 | |
|---|
| 239 | | - if (!tegra_bpmp_master_free(channel)) { |
|---|
| 278 | + if (!tegra_bpmp_is_request_channel_free(channel)) { |
|---|
| 240 | 279 | err = -EBUSY; |
|---|
| 241 | 280 | goto unlock; |
|---|
| 242 | 281 | } |
|---|
| .. | .. |
|---|
| 268 | 307 | { |
|---|
| 269 | 308 | int err; |
|---|
| 270 | 309 | |
|---|
| 271 | | - err = tegra_bpmp_wait_master_free(channel); |
|---|
| 310 | + err = tegra_bpmp_wait_request_channel_free(channel); |
|---|
| 272 | 311 | if (err < 0) |
|---|
| 273 | 312 | return err; |
|---|
| 274 | 313 | |
|---|
| .. | .. |
|---|
| 300 | 339 | |
|---|
| 301 | 340 | spin_unlock(&bpmp->atomic_tx_lock); |
|---|
| 302 | 341 | |
|---|
| 303 | | - err = mbox_send_message(bpmp->mbox.channel, NULL); |
|---|
| 342 | + err = tegra_bpmp_ring_doorbell(bpmp); |
|---|
| 304 | 343 | if (err < 0) |
|---|
| 305 | 344 | return err; |
|---|
| 306 | 345 | |
|---|
| 307 | | - mbox_client_txdone(bpmp->mbox.channel, 0); |
|---|
| 308 | | - |
|---|
| 309 | | - err = tegra_bpmp_wait_ack(channel); |
|---|
| 346 | + err = tegra_bpmp_wait_response(channel); |
|---|
| 310 | 347 | if (err < 0) |
|---|
| 311 | 348 | return err; |
|---|
| 312 | 349 | |
|---|
| .. | .. |
|---|
| 333 | 370 | if (IS_ERR(channel)) |
|---|
| 334 | 371 | return PTR_ERR(channel); |
|---|
| 335 | 372 | |
|---|
| 336 | | - err = mbox_send_message(bpmp->mbox.channel, NULL); |
|---|
| 373 | + err = tegra_bpmp_ring_doorbell(bpmp); |
|---|
| 337 | 374 | if (err < 0) |
|---|
| 338 | 375 | return err; |
|---|
| 339 | | - |
|---|
| 340 | | - mbox_client_txdone(bpmp->mbox.channel, 0); |
|---|
| 341 | 376 | |
|---|
| 342 | 377 | timeout = usecs_to_jiffies(bpmp->soc->channels.thread.timeout); |
|---|
| 343 | 378 | |
|---|
| .. | .. |
|---|
| 367 | 402 | { |
|---|
| 368 | 403 | unsigned long flags = channel->ib->flags; |
|---|
| 369 | 404 | struct tegra_bpmp *bpmp = channel->bpmp; |
|---|
| 370 | | - struct tegra_bpmp_mb_data *frame; |
|---|
| 371 | 405 | int err; |
|---|
| 372 | 406 | |
|---|
| 373 | 407 | if (WARN_ON(size > MSG_DATA_MIN_SZ)) |
|---|
| 374 | 408 | return; |
|---|
| 375 | 409 | |
|---|
| 376 | | - err = tegra_ivc_read_advance(channel->ivc); |
|---|
| 410 | + err = tegra_bpmp_ack_request(channel); |
|---|
| 377 | 411 | if (WARN_ON(err < 0)) |
|---|
| 378 | 412 | return; |
|---|
| 379 | 413 | |
|---|
| 380 | 414 | if ((flags & MSG_ACK) == 0) |
|---|
| 381 | 415 | return; |
|---|
| 382 | 416 | |
|---|
| 383 | | - frame = tegra_ivc_write_get_next_frame(channel->ivc); |
|---|
| 384 | | - if (WARN_ON(IS_ERR(frame))) |
|---|
| 417 | + if (WARN_ON(!tegra_bpmp_is_response_channel_free(channel))) |
|---|
| 385 | 418 | return; |
|---|
| 386 | 419 | |
|---|
| 387 | | - frame->code = code; |
|---|
| 420 | + channel->ob->code = code; |
|---|
| 388 | 421 | |
|---|
| 389 | 422 | if (data && size > 0) |
|---|
| 390 | | - memcpy(frame->data, data, size); |
|---|
| 423 | + memcpy(channel->ob->data, data, size); |
|---|
| 391 | 424 | |
|---|
| 392 | | - err = tegra_ivc_write_advance(channel->ivc); |
|---|
| 425 | + err = tegra_bpmp_post_response(channel); |
|---|
| 393 | 426 | if (WARN_ON(err < 0)) |
|---|
| 394 | 427 | return; |
|---|
| 395 | 428 | |
|---|
| 396 | 429 | if (flags & MSG_RING) { |
|---|
| 397 | | - err = mbox_send_message(bpmp->mbox.channel, NULL); |
|---|
| 430 | + err = tegra_bpmp_ring_doorbell(bpmp); |
|---|
| 398 | 431 | if (WARN_ON(err < 0)) |
|---|
| 399 | 432 | return; |
|---|
| 400 | | - |
|---|
| 401 | | - mbox_client_txdone(bpmp->mbox.channel, 0); |
|---|
| 402 | 433 | } |
|---|
| 403 | 434 | } |
|---|
| 404 | 435 | EXPORT_SYMBOL_GPL(tegra_bpmp_mrq_return); |
|---|
| .. | .. |
|---|
| 469 | 500 | } |
|---|
| 470 | 501 | EXPORT_SYMBOL_GPL(tegra_bpmp_free_mrq); |
|---|
| 471 | 502 | |
|---|
| 503 | +bool tegra_bpmp_mrq_is_supported(struct tegra_bpmp *bpmp, unsigned int mrq) |
|---|
| 504 | +{ |
|---|
| 505 | + struct mrq_query_abi_request req = { .mrq = cpu_to_le32(mrq) }; |
|---|
| 506 | + struct mrq_query_abi_response resp; |
|---|
| 507 | + struct tegra_bpmp_message msg = { |
|---|
| 508 | + .mrq = MRQ_QUERY_ABI, |
|---|
| 509 | + .tx = { |
|---|
| 510 | + .data = &req, |
|---|
| 511 | + .size = sizeof(req), |
|---|
| 512 | + }, |
|---|
| 513 | + .rx = { |
|---|
| 514 | + .data = &resp, |
|---|
| 515 | + .size = sizeof(resp), |
|---|
| 516 | + }, |
|---|
| 517 | + }; |
|---|
| 518 | + int err; |
|---|
| 519 | + |
|---|
| 520 | + err = tegra_bpmp_transfer(bpmp, &msg); |
|---|
| 521 | + if (err || msg.rx.ret) |
|---|
| 522 | + return false; |
|---|
| 523 | + |
|---|
| 524 | + return resp.status == 0; |
|---|
| 525 | +} |
|---|
| 526 | +EXPORT_SYMBOL_GPL(tegra_bpmp_mrq_is_supported); |
|---|
| 527 | + |
|---|
| 472 | 528 | static void tegra_bpmp_mrq_handle_ping(unsigned int mrq, |
|---|
| 473 | 529 | struct tegra_bpmp_channel *channel, |
|---|
| 474 | 530 | void *data) |
|---|
| .. | .. |
|---|
| 520 | 576 | return err; |
|---|
| 521 | 577 | } |
|---|
| 522 | 578 | |
|---|
| 523 | | -static int tegra_bpmp_get_firmware_tag(struct tegra_bpmp *bpmp, char *tag, |
|---|
| 524 | | - size_t size) |
|---|
| 579 | +/* deprecated version of tag query */ |
|---|
| 580 | +static int tegra_bpmp_get_firmware_tag_old(struct tegra_bpmp *bpmp, char *tag, |
|---|
| 581 | + size_t size) |
|---|
| 525 | 582 | { |
|---|
| 526 | 583 | struct mrq_query_tag_request request; |
|---|
| 527 | 584 | struct tegra_bpmp_message msg; |
|---|
| .. | .. |
|---|
| 530 | 587 | void *virt; |
|---|
| 531 | 588 | int err; |
|---|
| 532 | 589 | |
|---|
| 533 | | - virt = dma_alloc_coherent(bpmp->dev, MSG_DATA_MIN_SZ, &phys, |
|---|
| 590 | + if (size != TAG_SZ) |
|---|
| 591 | + return -EINVAL; |
|---|
| 592 | + |
|---|
| 593 | + virt = dma_alloc_coherent(bpmp->dev, TAG_SZ, &phys, |
|---|
| 534 | 594 | GFP_KERNEL | GFP_DMA32); |
|---|
| 535 | 595 | if (!virt) |
|---|
| 536 | 596 | return -ENOMEM; |
|---|
| .. | .. |
|---|
| 548 | 608 | local_irq_restore(flags); |
|---|
| 549 | 609 | |
|---|
| 550 | 610 | if (err == 0) |
|---|
| 551 | | - strlcpy(tag, virt, size); |
|---|
| 611 | + memcpy(tag, virt, TAG_SZ); |
|---|
| 552 | 612 | |
|---|
| 553 | | - dma_free_coherent(bpmp->dev, MSG_DATA_MIN_SZ, virt, phys); |
|---|
| 613 | + dma_free_coherent(bpmp->dev, TAG_SZ, virt, phys); |
|---|
| 554 | 614 | |
|---|
| 555 | 615 | return err; |
|---|
| 616 | +} |
|---|
| 617 | + |
|---|
| 618 | +static int tegra_bpmp_get_firmware_tag(struct tegra_bpmp *bpmp, char *tag, |
|---|
| 619 | + size_t size) |
|---|
| 620 | +{ |
|---|
| 621 | + if (tegra_bpmp_mrq_is_supported(bpmp, MRQ_QUERY_FW_TAG)) { |
|---|
| 622 | + struct mrq_query_fw_tag_response resp; |
|---|
| 623 | + struct tegra_bpmp_message msg = { |
|---|
| 624 | + .mrq = MRQ_QUERY_FW_TAG, |
|---|
| 625 | + .rx = { |
|---|
| 626 | + .data = &resp, |
|---|
| 627 | + .size = sizeof(resp), |
|---|
| 628 | + }, |
|---|
| 629 | + }; |
|---|
| 630 | + int err; |
|---|
| 631 | + |
|---|
| 632 | + if (size != sizeof(resp.tag)) |
|---|
| 633 | + return -EINVAL; |
|---|
| 634 | + |
|---|
| 635 | + err = tegra_bpmp_transfer(bpmp, &msg); |
|---|
| 636 | + |
|---|
| 637 | + if (err) |
|---|
| 638 | + return err; |
|---|
| 639 | + if (msg.rx.ret < 0) |
|---|
| 640 | + return -EINVAL; |
|---|
| 641 | + |
|---|
| 642 | + memcpy(tag, resp.tag, sizeof(resp.tag)); |
|---|
| 643 | + return 0; |
|---|
| 644 | + } |
|---|
| 645 | + |
|---|
| 646 | + return tegra_bpmp_get_firmware_tag_old(bpmp, tag, size); |
|---|
| 556 | 647 | } |
|---|
| 557 | 648 | |
|---|
| 558 | 649 | static void tegra_bpmp_channel_signal(struct tegra_bpmp_channel *channel) |
|---|
| .. | .. |
|---|
| 565 | 656 | complete(&channel->completion); |
|---|
| 566 | 657 | } |
|---|
| 567 | 658 | |
|---|
| 568 | | -static void tegra_bpmp_handle_rx(struct mbox_client *client, void *data) |
|---|
| 659 | +void tegra_bpmp_handle_rx(struct tegra_bpmp *bpmp) |
|---|
| 569 | 660 | { |
|---|
| 570 | | - struct tegra_bpmp *bpmp = mbox_client_to_bpmp(client); |
|---|
| 571 | 661 | struct tegra_bpmp_channel *channel; |
|---|
| 572 | 662 | unsigned int i, count; |
|---|
| 573 | 663 | unsigned long *busy; |
|---|
| .. | .. |
|---|
| 576 | 666 | count = bpmp->soc->channels.thread.count; |
|---|
| 577 | 667 | busy = bpmp->threaded.busy; |
|---|
| 578 | 668 | |
|---|
| 579 | | - if (tegra_bpmp_master_acked(channel)) |
|---|
| 669 | + if (tegra_bpmp_is_request_ready(channel)) |
|---|
| 580 | 670 | tegra_bpmp_handle_mrq(bpmp, channel->ib->code, channel); |
|---|
| 581 | 671 | |
|---|
| 582 | 672 | spin_lock(&bpmp->lock); |
|---|
| .. | .. |
|---|
| 586 | 676 | |
|---|
| 587 | 677 | channel = &bpmp->threaded_channels[i]; |
|---|
| 588 | 678 | |
|---|
| 589 | | - if (tegra_bpmp_master_acked(channel)) { |
|---|
| 679 | + if (tegra_bpmp_is_response_ready(channel)) { |
|---|
| 590 | 680 | tegra_bpmp_channel_signal(channel); |
|---|
| 591 | 681 | clear_bit(i, busy); |
|---|
| 592 | 682 | } |
|---|
| .. | .. |
|---|
| 595 | 685 | spin_unlock(&bpmp->lock); |
|---|
| 596 | 686 | } |
|---|
| 597 | 687 | |
|---|
| 598 | | -static void tegra_bpmp_ivc_notify(struct tegra_ivc *ivc, void *data) |
|---|
| 599 | | -{ |
|---|
| 600 | | - struct tegra_bpmp *bpmp = data; |
|---|
| 601 | | - int err; |
|---|
| 602 | | - |
|---|
| 603 | | - if (WARN_ON(bpmp->mbox.channel == NULL)) |
|---|
| 604 | | - return; |
|---|
| 605 | | - |
|---|
| 606 | | - err = mbox_send_message(bpmp->mbox.channel, NULL); |
|---|
| 607 | | - if (err < 0) |
|---|
| 608 | | - return; |
|---|
| 609 | | - |
|---|
| 610 | | - mbox_client_txdone(bpmp->mbox.channel, 0); |
|---|
| 611 | | -} |
|---|
| 612 | | - |
|---|
| 613 | | -static int tegra_bpmp_channel_init(struct tegra_bpmp_channel *channel, |
|---|
| 614 | | - struct tegra_bpmp *bpmp, |
|---|
| 615 | | - unsigned int index) |
|---|
| 616 | | -{ |
|---|
| 617 | | - size_t message_size, queue_size; |
|---|
| 618 | | - unsigned int offset; |
|---|
| 619 | | - int err; |
|---|
| 620 | | - |
|---|
| 621 | | - channel->ivc = devm_kzalloc(bpmp->dev, sizeof(*channel->ivc), |
|---|
| 622 | | - GFP_KERNEL); |
|---|
| 623 | | - if (!channel->ivc) |
|---|
| 624 | | - return -ENOMEM; |
|---|
| 625 | | - |
|---|
| 626 | | - message_size = tegra_ivc_align(MSG_MIN_SZ); |
|---|
| 627 | | - queue_size = tegra_ivc_total_queue_size(message_size); |
|---|
| 628 | | - offset = queue_size * index; |
|---|
| 629 | | - |
|---|
| 630 | | - err = tegra_ivc_init(channel->ivc, NULL, |
|---|
| 631 | | - bpmp->rx.virt + offset, bpmp->rx.phys + offset, |
|---|
| 632 | | - bpmp->tx.virt + offset, bpmp->tx.phys + offset, |
|---|
| 633 | | - 1, message_size, tegra_bpmp_ivc_notify, |
|---|
| 634 | | - bpmp); |
|---|
| 635 | | - if (err < 0) { |
|---|
| 636 | | - dev_err(bpmp->dev, "failed to setup IVC for channel %u: %d\n", |
|---|
| 637 | | - index, err); |
|---|
| 638 | | - return err; |
|---|
| 639 | | - } |
|---|
| 640 | | - |
|---|
| 641 | | - init_completion(&channel->completion); |
|---|
| 642 | | - channel->bpmp = bpmp; |
|---|
| 643 | | - |
|---|
| 644 | | - return 0; |
|---|
| 645 | | -} |
|---|
| 646 | | - |
|---|
| 647 | | -static void tegra_bpmp_channel_reset(struct tegra_bpmp_channel *channel) |
|---|
| 648 | | -{ |
|---|
| 649 | | - /* reset the channel state */ |
|---|
| 650 | | - tegra_ivc_reset(channel->ivc); |
|---|
| 651 | | - |
|---|
| 652 | | - /* sync the channel state with BPMP */ |
|---|
| 653 | | - while (tegra_ivc_notified(channel->ivc)) |
|---|
| 654 | | - ; |
|---|
| 655 | | -} |
|---|
| 656 | | - |
|---|
| 657 | | -static void tegra_bpmp_channel_cleanup(struct tegra_bpmp_channel *channel) |
|---|
| 658 | | -{ |
|---|
| 659 | | - tegra_ivc_cleanup(channel->ivc); |
|---|
| 660 | | -} |
|---|
| 661 | | - |
|---|
| 662 | 688 | static int tegra_bpmp_probe(struct platform_device *pdev) |
|---|
| 663 | 689 | { |
|---|
| 664 | 690 | struct tegra_bpmp *bpmp; |
|---|
| 665 | | - unsigned int i; |
|---|
| 666 | | - char tag[32]; |
|---|
| 691 | + char tag[TAG_SZ]; |
|---|
| 667 | 692 | size_t size; |
|---|
| 668 | 693 | int err; |
|---|
| 669 | 694 | |
|---|
| .. | .. |
|---|
| 674 | 699 | bpmp->soc = of_device_get_match_data(&pdev->dev); |
|---|
| 675 | 700 | bpmp->dev = &pdev->dev; |
|---|
| 676 | 701 | |
|---|
| 677 | | - bpmp->tx.pool = of_gen_pool_get(pdev->dev.of_node, "shmem", 0); |
|---|
| 678 | | - if (!bpmp->tx.pool) { |
|---|
| 679 | | - dev_err(&pdev->dev, "TX shmem pool not found\n"); |
|---|
| 680 | | - return -ENOMEM; |
|---|
| 681 | | - } |
|---|
| 682 | | - |
|---|
| 683 | | - bpmp->tx.virt = gen_pool_dma_alloc(bpmp->tx.pool, 4096, &bpmp->tx.phys); |
|---|
| 684 | | - if (!bpmp->tx.virt) { |
|---|
| 685 | | - dev_err(&pdev->dev, "failed to allocate from TX pool\n"); |
|---|
| 686 | | - return -ENOMEM; |
|---|
| 687 | | - } |
|---|
| 688 | | - |
|---|
| 689 | | - bpmp->rx.pool = of_gen_pool_get(pdev->dev.of_node, "shmem", 1); |
|---|
| 690 | | - if (!bpmp->rx.pool) { |
|---|
| 691 | | - dev_err(&pdev->dev, "RX shmem pool not found\n"); |
|---|
| 692 | | - err = -ENOMEM; |
|---|
| 693 | | - goto free_tx; |
|---|
| 694 | | - } |
|---|
| 695 | | - |
|---|
| 696 | | - bpmp->rx.virt = gen_pool_dma_alloc(bpmp->rx.pool, 4096, &bpmp->rx.phys); |
|---|
| 697 | | - if (!bpmp->rx.virt) { |
|---|
| 698 | | - dev_err(&pdev->dev, "failed to allocate from RX pool\n"); |
|---|
| 699 | | - err = -ENOMEM; |
|---|
| 700 | | - goto free_tx; |
|---|
| 701 | | - } |
|---|
| 702 | | - |
|---|
| 703 | 702 | INIT_LIST_HEAD(&bpmp->mrqs); |
|---|
| 704 | 703 | spin_lock_init(&bpmp->lock); |
|---|
| 705 | 704 | |
|---|
| .. | .. |
|---|
| 709 | 708 | size = BITS_TO_LONGS(bpmp->threaded.count) * sizeof(long); |
|---|
| 710 | 709 | |
|---|
| 711 | 710 | bpmp->threaded.allocated = devm_kzalloc(&pdev->dev, size, GFP_KERNEL); |
|---|
| 712 | | - if (!bpmp->threaded.allocated) { |
|---|
| 713 | | - err = -ENOMEM; |
|---|
| 714 | | - goto free_rx; |
|---|
| 715 | | - } |
|---|
| 711 | + if (!bpmp->threaded.allocated) |
|---|
| 712 | + return -ENOMEM; |
|---|
| 716 | 713 | |
|---|
| 717 | 714 | bpmp->threaded.busy = devm_kzalloc(&pdev->dev, size, GFP_KERNEL); |
|---|
| 718 | | - if (!bpmp->threaded.busy) { |
|---|
| 719 | | - err = -ENOMEM; |
|---|
| 720 | | - goto free_rx; |
|---|
| 721 | | - } |
|---|
| 715 | + if (!bpmp->threaded.busy) |
|---|
| 716 | + return -ENOMEM; |
|---|
| 722 | 717 | |
|---|
| 723 | 718 | spin_lock_init(&bpmp->atomic_tx_lock); |
|---|
| 724 | 719 | bpmp->tx_channel = devm_kzalloc(&pdev->dev, sizeof(*bpmp->tx_channel), |
|---|
| 725 | 720 | GFP_KERNEL); |
|---|
| 726 | | - if (!bpmp->tx_channel) { |
|---|
| 727 | | - err = -ENOMEM; |
|---|
| 728 | | - goto free_rx; |
|---|
| 729 | | - } |
|---|
| 721 | + if (!bpmp->tx_channel) |
|---|
| 722 | + return -ENOMEM; |
|---|
| 730 | 723 | |
|---|
| 731 | 724 | bpmp->rx_channel = devm_kzalloc(&pdev->dev, sizeof(*bpmp->rx_channel), |
|---|
| 732 | 725 | GFP_KERNEL); |
|---|
| 733 | | - if (!bpmp->rx_channel) { |
|---|
| 734 | | - err = -ENOMEM; |
|---|
| 735 | | - goto free_rx; |
|---|
| 736 | | - } |
|---|
| 726 | + if (!bpmp->rx_channel) |
|---|
| 727 | + return -ENOMEM; |
|---|
| 737 | 728 | |
|---|
| 738 | 729 | bpmp->threaded_channels = devm_kcalloc(&pdev->dev, bpmp->threaded.count, |
|---|
| 739 | 730 | sizeof(*bpmp->threaded_channels), |
|---|
| 740 | 731 | GFP_KERNEL); |
|---|
| 741 | | - if (!bpmp->threaded_channels) { |
|---|
| 742 | | - err = -ENOMEM; |
|---|
| 743 | | - goto free_rx; |
|---|
| 744 | | - } |
|---|
| 732 | + if (!bpmp->threaded_channels) |
|---|
| 733 | + return -ENOMEM; |
|---|
| 745 | 734 | |
|---|
| 746 | | - err = tegra_bpmp_channel_init(bpmp->tx_channel, bpmp, |
|---|
| 747 | | - bpmp->soc->channels.cpu_tx.offset); |
|---|
| 735 | + err = bpmp->soc->ops->init(bpmp); |
|---|
| 748 | 736 | if (err < 0) |
|---|
| 749 | | - goto free_rx; |
|---|
| 750 | | - |
|---|
| 751 | | - err = tegra_bpmp_channel_init(bpmp->rx_channel, bpmp, |
|---|
| 752 | | - bpmp->soc->channels.cpu_rx.offset); |
|---|
| 753 | | - if (err < 0) |
|---|
| 754 | | - goto cleanup_tx_channel; |
|---|
| 755 | | - |
|---|
| 756 | | - for (i = 0; i < bpmp->threaded.count; i++) { |
|---|
| 757 | | - err = tegra_bpmp_channel_init( |
|---|
| 758 | | - &bpmp->threaded_channels[i], bpmp, |
|---|
| 759 | | - bpmp->soc->channels.thread.offset + i); |
|---|
| 760 | | - if (err < 0) |
|---|
| 761 | | - goto cleanup_threaded_channels; |
|---|
| 762 | | - } |
|---|
| 763 | | - |
|---|
| 764 | | - /* mbox registration */ |
|---|
| 765 | | - bpmp->mbox.client.dev = &pdev->dev; |
|---|
| 766 | | - bpmp->mbox.client.rx_callback = tegra_bpmp_handle_rx; |
|---|
| 767 | | - bpmp->mbox.client.tx_block = false; |
|---|
| 768 | | - bpmp->mbox.client.knows_txdone = false; |
|---|
| 769 | | - |
|---|
| 770 | | - bpmp->mbox.channel = mbox_request_channel(&bpmp->mbox.client, 0); |
|---|
| 771 | | - if (IS_ERR(bpmp->mbox.channel)) { |
|---|
| 772 | | - err = PTR_ERR(bpmp->mbox.channel); |
|---|
| 773 | | - dev_err(&pdev->dev, "failed to get HSP mailbox: %d\n", err); |
|---|
| 774 | | - goto cleanup_threaded_channels; |
|---|
| 775 | | - } |
|---|
| 776 | | - |
|---|
| 777 | | - /* reset message channels */ |
|---|
| 778 | | - tegra_bpmp_channel_reset(bpmp->tx_channel); |
|---|
| 779 | | - tegra_bpmp_channel_reset(bpmp->rx_channel); |
|---|
| 780 | | - for (i = 0; i < bpmp->threaded.count; i++) |
|---|
| 781 | | - tegra_bpmp_channel_reset(&bpmp->threaded_channels[i]); |
|---|
| 737 | + return err; |
|---|
| 782 | 738 | |
|---|
| 783 | 739 | err = tegra_bpmp_request_mrq(bpmp, MRQ_PING, |
|---|
| 784 | 740 | tegra_bpmp_mrq_handle_ping, bpmp); |
|---|
| 785 | 741 | if (err < 0) |
|---|
| 786 | | - goto free_mbox; |
|---|
| 742 | + goto deinit; |
|---|
| 787 | 743 | |
|---|
| 788 | 744 | err = tegra_bpmp_ping(bpmp); |
|---|
| 789 | 745 | if (err < 0) { |
|---|
| .. | .. |
|---|
| 791 | 747 | goto free_mrq; |
|---|
| 792 | 748 | } |
|---|
| 793 | 749 | |
|---|
| 794 | | - err = tegra_bpmp_get_firmware_tag(bpmp, tag, sizeof(tag) - 1); |
|---|
| 750 | + err = tegra_bpmp_get_firmware_tag(bpmp, tag, sizeof(tag)); |
|---|
| 795 | 751 | if (err < 0) { |
|---|
| 796 | 752 | dev_err(&pdev->dev, "failed to get firmware tag: %d\n", err); |
|---|
| 797 | 753 | goto free_mrq; |
|---|
| 798 | 754 | } |
|---|
| 799 | 755 | |
|---|
| 800 | | - dev_info(&pdev->dev, "firmware: %s\n", tag); |
|---|
| 756 | + dev_info(&pdev->dev, "firmware: %.*s\n", (int)sizeof(tag), tag); |
|---|
| 801 | 757 | |
|---|
| 802 | 758 | platform_set_drvdata(pdev, bpmp); |
|---|
| 803 | 759 | |
|---|
| .. | .. |
|---|
| 805 | 761 | if (err < 0) |
|---|
| 806 | 762 | goto free_mrq; |
|---|
| 807 | 763 | |
|---|
| 808 | | - err = tegra_bpmp_init_clocks(bpmp); |
|---|
| 809 | | - if (err < 0) |
|---|
| 810 | | - goto free_mrq; |
|---|
| 764 | + if (of_find_property(pdev->dev.of_node, "#clock-cells", NULL)) { |
|---|
| 765 | + err = tegra_bpmp_init_clocks(bpmp); |
|---|
| 766 | + if (err < 0) |
|---|
| 767 | + goto free_mrq; |
|---|
| 768 | + } |
|---|
| 811 | 769 | |
|---|
| 812 | | - err = tegra_bpmp_init_resets(bpmp); |
|---|
| 813 | | - if (err < 0) |
|---|
| 814 | | - goto free_mrq; |
|---|
| 770 | + if (of_find_property(pdev->dev.of_node, "#reset-cells", NULL)) { |
|---|
| 771 | + err = tegra_bpmp_init_resets(bpmp); |
|---|
| 772 | + if (err < 0) |
|---|
| 773 | + goto free_mrq; |
|---|
| 774 | + } |
|---|
| 815 | 775 | |
|---|
| 816 | | - err = tegra_bpmp_init_powergates(bpmp); |
|---|
| 817 | | - if (err < 0) |
|---|
| 818 | | - goto free_mrq; |
|---|
| 776 | + if (of_find_property(pdev->dev.of_node, "#power-domain-cells", NULL)) { |
|---|
| 777 | + err = tegra_bpmp_init_powergates(bpmp); |
|---|
| 778 | + if (err < 0) |
|---|
| 779 | + goto free_mrq; |
|---|
| 780 | + } |
|---|
| 819 | 781 | |
|---|
| 820 | 782 | err = tegra_bpmp_init_debugfs(bpmp); |
|---|
| 821 | 783 | if (err < 0) |
|---|
| .. | .. |
|---|
| 825 | 787 | |
|---|
| 826 | 788 | free_mrq: |
|---|
| 827 | 789 | tegra_bpmp_free_mrq(bpmp, MRQ_PING, bpmp); |
|---|
| 828 | | -free_mbox: |
|---|
| 829 | | - mbox_free_channel(bpmp->mbox.channel); |
|---|
| 830 | | -cleanup_threaded_channels: |
|---|
| 831 | | - for (i = 0; i < bpmp->threaded.count; i++) { |
|---|
| 832 | | - if (bpmp->threaded_channels[i].bpmp) |
|---|
| 833 | | - tegra_bpmp_channel_cleanup(&bpmp->threaded_channels[i]); |
|---|
| 834 | | - } |
|---|
| 790 | +deinit: |
|---|
| 791 | + if (bpmp->soc->ops->deinit) |
|---|
| 792 | + bpmp->soc->ops->deinit(bpmp); |
|---|
| 835 | 793 | |
|---|
| 836 | | - tegra_bpmp_channel_cleanup(bpmp->rx_channel); |
|---|
| 837 | | -cleanup_tx_channel: |
|---|
| 838 | | - tegra_bpmp_channel_cleanup(bpmp->tx_channel); |
|---|
| 839 | | -free_rx: |
|---|
| 840 | | - gen_pool_free(bpmp->rx.pool, (unsigned long)bpmp->rx.virt, 4096); |
|---|
| 841 | | -free_tx: |
|---|
| 842 | | - gen_pool_free(bpmp->tx.pool, (unsigned long)bpmp->tx.virt, 4096); |
|---|
| 843 | 794 | return err; |
|---|
| 844 | 795 | } |
|---|
| 845 | 796 | |
|---|
| 797 | +static int __maybe_unused tegra_bpmp_resume(struct device *dev) |
|---|
| 798 | +{ |
|---|
| 799 | + struct tegra_bpmp *bpmp = dev_get_drvdata(dev); |
|---|
| 800 | + |
|---|
| 801 | + if (bpmp->soc->ops->resume) |
|---|
| 802 | + return bpmp->soc->ops->resume(bpmp); |
|---|
| 803 | + else |
|---|
| 804 | + return 0; |
|---|
| 805 | +} |
|---|
| 806 | + |
|---|
| 807 | +static const struct dev_pm_ops tegra_bpmp_pm_ops = { |
|---|
| 808 | + .resume_noirq = tegra_bpmp_resume, |
|---|
| 809 | +}; |
|---|
| 810 | + |
|---|
| 811 | +#if IS_ENABLED(CONFIG_ARCH_TEGRA_186_SOC) || \ |
|---|
| 812 | + IS_ENABLED(CONFIG_ARCH_TEGRA_194_SOC) || \ |
|---|
| 813 | + IS_ENABLED(CONFIG_ARCH_TEGRA_234_SOC) |
|---|
| 846 | 814 | static const struct tegra_bpmp_soc tegra186_soc = { |
|---|
| 847 | 815 | .channels = { |
|---|
| 848 | 816 | .cpu_tx = { |
|---|
| .. | .. |
|---|
| 859 | 827 | .timeout = 0, |
|---|
| 860 | 828 | }, |
|---|
| 861 | 829 | }, |
|---|
| 830 | + .ops = &tegra186_bpmp_ops, |
|---|
| 862 | 831 | .num_resets = 193, |
|---|
| 863 | 832 | }; |
|---|
| 833 | +#endif |
|---|
| 834 | + |
|---|
| 835 | +#if IS_ENABLED(CONFIG_ARCH_TEGRA_210_SOC) |
|---|
| 836 | +static const struct tegra_bpmp_soc tegra210_soc = { |
|---|
| 837 | + .channels = { |
|---|
| 838 | + .cpu_tx = { |
|---|
| 839 | + .offset = 0, |
|---|
| 840 | + .count = 1, |
|---|
| 841 | + .timeout = 60 * USEC_PER_SEC, |
|---|
| 842 | + }, |
|---|
| 843 | + .thread = { |
|---|
| 844 | + .offset = 4, |
|---|
| 845 | + .count = 1, |
|---|
| 846 | + .timeout = 600 * USEC_PER_SEC, |
|---|
| 847 | + }, |
|---|
| 848 | + .cpu_rx = { |
|---|
| 849 | + .offset = 8, |
|---|
| 850 | + .count = 1, |
|---|
| 851 | + .timeout = 0, |
|---|
| 852 | + }, |
|---|
| 853 | + }, |
|---|
| 854 | + .ops = &tegra210_bpmp_ops, |
|---|
| 855 | +}; |
|---|
| 856 | +#endif |
|---|
| 864 | 857 | |
|---|
| 865 | 858 | static const struct of_device_id tegra_bpmp_match[] = { |
|---|
| 859 | +#if IS_ENABLED(CONFIG_ARCH_TEGRA_186_SOC) || \ |
|---|
| 860 | + IS_ENABLED(CONFIG_ARCH_TEGRA_194_SOC) || \ |
|---|
| 861 | + IS_ENABLED(CONFIG_ARCH_TEGRA_234_SOC) |
|---|
| 866 | 862 | { .compatible = "nvidia,tegra186-bpmp", .data = &tegra186_soc }, |
|---|
| 863 | +#endif |
|---|
| 864 | +#if IS_ENABLED(CONFIG_ARCH_TEGRA_210_SOC) |
|---|
| 865 | + { .compatible = "nvidia,tegra210-bpmp", .data = &tegra210_soc }, |
|---|
| 866 | +#endif |
|---|
| 867 | 867 | { } |
|---|
| 868 | 868 | }; |
|---|
| 869 | 869 | |
|---|
| .. | .. |
|---|
| 871 | 871 | .driver = { |
|---|
| 872 | 872 | .name = "tegra-bpmp", |
|---|
| 873 | 873 | .of_match_table = tegra_bpmp_match, |
|---|
| 874 | + .pm = &tegra_bpmp_pm_ops, |
|---|
| 875 | + .suppress_bind_attrs = true, |
|---|
| 874 | 876 | }, |
|---|
| 875 | 877 | .probe = tegra_bpmp_probe, |
|---|
| 876 | 878 | }; |
|---|
| 877 | | - |
|---|
| 878 | | -static int __init tegra_bpmp_init(void) |
|---|
| 879 | | -{ |
|---|
| 880 | | - return platform_driver_register(&tegra_bpmp_driver); |
|---|
| 881 | | -} |
|---|
| 882 | | -core_initcall(tegra_bpmp_init); |
|---|
| 879 | +builtin_platform_driver(tegra_bpmp_driver); |
|---|