Files
linux-nv-oot/drivers/platform/tegra/dce/dce-mailbox.c
anupamg 2f69bee627 DCE-KMD: Update shared files licenses
- Updates licenses for DCE-KMD files that are shared with HVRTOS.
    - Update from GPL-2.0 to MIT as we use these files in both
      open source and closed source context.

JIRA TDS-16741

Change-Id: Icf7aeb737f2f3b294bb9ff9c36d5ed7220c13dea
Signed-off-by: anupamg <anupamg@nvidia.com>
Reviewed-on: https://git-master.nvidia.com/r/c/linux-nv-oot/+/3293404
GVS: buildbot_gerritrpt <buildbot_gerritrpt@nvidia.com>
Reviewed-by: svcacv <svcacv@nvidia.com>
Reviewed-by: Vinod Gopalakrishnakurup <vinodg@nvidia.com>
2025-07-24 10:19:14 +00:00

308 lines
7.2 KiB
C

// SPDX-License-Identifier: MIT
/*
* SPDX-FileCopyrightText: Copyright (c) 2019-2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
#include <dce.h>
#include <dce-mailbox.h>
#include <dce-os-utils.h>
#include <interface/dce-interface.h>
#include <interface/dce-boot-cmds.h>
#define CCPLEX_HSP_IE 1U /* TODO : Have an api to read from platform data */
#define DCE_MAILBOX_FULL_INT_SHIFT 8U
/**
* dce_hsp_get_irq_sources - gets the interrupt sources.
*
* @d : Pointer to tegra_dce struct.
*
* Return : bitmap for mailbox ids that triggered the irqs.
*/
static u32 dce_hsp_get_irq_sources(struct tegra_dce *d)
{
return (d->hsp.hsp_ie_read(d, d->hsp_id, CCPLEX_HSP_IE) &
d->hsp.hsp_ir_read(d, d->hsp_id));
}
/**
* dce_mailbox_isr - Isr for mailbox irqs.
*
* @d : Pointer to tegra_dce struct.
*
* Return : Void
*/
void dce_mailbox_isr(struct tegra_dce *d)
{
u8 i = 0;
u32 value;
struct dce_mailbox_interface *d_mb;
u32 irq_sources = dce_hsp_get_irq_sources(d);
do {
d_mb = &d->d_mb[i];
/**
* Get the mailbox on which the interrupt
* is received.
*/
if (irq_sources & (BIT(d_mb->r_mb)
<< DCE_MAILBOX_FULL_INT_SHIFT)) {
/**
* Read and store the value.
*
* TODO : Ignore the full interrupt
* bit before storing the result.
*
*/
value = d->hsp.smb_read(d, d->hsp_id, d_mb->r_mb);
d->hsp.smb_set(d, 0U, d->hsp_id, d_mb->r_mb);
dce_mailbox_store_interface_status(d, value, i);
d_mb->notify(d, d_mb->notify_data);
}
i++;
} while (i < DCE_MAILBOX_MAX_INTERFACES);
}
/**
* dce_mailbox_store_interface_status - stores the response
* received on a mailbox interface.
* @d : Pointer to tegra_dce struct.
* @v : Value to be stored.
* @id : interface id.
*
* Return :Void
*/
void dce_mailbox_store_interface_status(struct tegra_dce *d, u32 v, u8 id)
{
struct dce_mailbox_interface *d_mb = &d->d_mb[id];
dce_os_mutex_lock(&d_mb->lock);
d_mb->ack_value = v;
d_mb->valid = true;
dce_os_mutex_unlock(&d_mb->lock);
}
/**
* dce_mailbox_get_interface_status - gets the response
* received on mailbox interface.
* @d : Pointer to tegra_dce struct.
* @id : Interface id.
*
* Return : u32 value
*/
u32 dce_mailbox_get_interface_status(struct tegra_dce *d, u8 id)
{
struct dce_mailbox_interface *d_mb = &d->d_mb[id];
if (d_mb->valid)
return d_mb->ack_value;
else
return 0xffffffff;
}
/**
* dce_mailbox_invalidate_status - renders the response invalid.
*
* @d : Pointer to tegra_dce struct.
* @id : Interface id.
*
* Return : void
*/
void dce_mailbox_invalidate_status(struct tegra_dce *d, u8 id)
{
struct dce_mailbox_interface *d_mb = &d->d_mb[id];
dce_os_mutex_lock(&d_mb->lock);
d_mb->valid = false;
dce_os_mutex_unlock(&d_mb->lock);
}
/**
* dce_mailbox_write_safe - Checks if it's safe to write to
* a mailbox register.
*
* @d : Pointer to tegra_dce struct.
* @id : Mailbox ID
*
* Return : true if it's safe
*/
static bool dce_mailbox_write_safe(struct tegra_dce *d, u8 id)
{
unsigned long val;
val = d->hsp.smb_read(d, d->hsp_id, id);
return !(val & BIT(31));
}
/**
* dce_mailbox_set_full_interrupt - Sets the interrupt tag bit
* in the mailbox register
* @d : Pointer to tegra_dce struct.
* @id : Mailbox interface id.
*
* Return : Void
*/
void dce_mailbox_set_full_interrupt(struct tegra_dce *d, u8 id)
{
struct dce_mailbox_interface *d_mb;
d_mb = &d->d_mb[id];
dce_os_mutex_lock(&d_mb->lock);
if (!dce_mailbox_write_safe(d, d_mb->s_mb))
dce_os_info(d, "Intr bit set multiple times for MB : [0x%x] id:[0x%x]",
d_mb->s_mb, id);
d->hsp.smb_set(d, BIT(31), d->hsp_id, d_mb->s_mb);
dce_os_mutex_unlock(&d_mb->lock);
}
/**
* dce_handle_mailbox_send_cmd_sync - handler function for
* mailbox_send_cmd_sync
*
* @d : Pointer to tegra_dce struct.
* @cmd : The command to be sent.
* @interface : boot or admin interface
*
*
* Return : 0 if successful.
*/
int dce_handle_mailbox_send_cmd_sync(struct tegra_dce *d, u32 cmd, u32 interface)
{
int ret = 0;
struct dce_mailbox_interface *d_mb;
d_mb = &d->d_mb[interface];
dce_os_mutex_lock(&d_mb->lock);
if (!dce_mailbox_write_safe(d, d_mb->s_mb)) {
dce_os_err(d, "Previously sent message isn't synced");
return -1;
}
d->hsp.smb_set(d, cmd | BIT(31), d->hsp_id, d_mb->s_mb);
d_mb->valid = false;
dce_os_mutex_unlock(&d_mb->lock);
ret = d_mb->dce_mailbox_wait(d);
return ret;
}
/**
* dce_mailbox_send_cmd_sync - Sends command via mailbox and waits for ack.
*
* @d : Pointer to tegra_dce struct.
* @cmd : The command to be sent.
* @interface : boot or admin interface
*
* Return : 0 if successful.
*/
int dce_mailbox_send_cmd_sync(struct tegra_dce *d, u32 cmd, u32 interface)
{
int ret = 0;
struct dce_mailbox_send_cmd_params params;
params.cmd = cmd;
params.interface = interface;
ret = dce_fsm_post_event(d, EVENT_ID_DCE_BOOT_CMD_MSG_REQUESTED,
(void *)&params);
if (ret) {
dce_os_err(d, "Unable to send msg ret :%d", ret);
goto out;
}
out:
return ret;
}
/**
* dce_mailbox_init_interface - Initializes the mailbox interface.
*
* @d : Pointer to tegra_dce struct.
* @id : Mailbox interface id.
*
* Return : 0 if successful
*/
int dce_mailbox_init_interface(struct tegra_dce *d, u8 id, u8 s_mb,
u8 r_mb, int (*dce_mailbox_wait)(struct tegra_dce *),
void *notify_data, void (*notify)(struct tegra_dce *, void *))
{
int ret;
u64 ie_wr_val;
struct dce_mailbox_interface *d_mb;
d_mb = &d->d_mb[id];
ret = dce_os_mutex_init(&d_mb->lock);
if (ret) {
dce_os_err(d, "dce lock initialization failed for mailbox");
goto err_lock_init;
}
d_mb->valid = false;
d->hsp.smb_set_full_ie(d, true, d->hsp_id, r_mb);
ie_wr_val = BIT(r_mb) << 8U;
d->hsp.hsp_ie_write(d, ie_wr_val, d->hsp_id, CCPLEX_HSP_IE);
d_mb->s_mb = s_mb;
d_mb->r_mb = r_mb;
d_mb->notify = notify;
d_mb->notify_data = notify_data;
d_mb->dce_mailbox_wait
= dce_mailbox_wait;
return 0;
err_lock_init:
return ret;
}
/**
* dce_mailbox_deinit_interface - Releases the resources
* associated with boot interface.
*
* @d : Pointer to tegra_dce struct.
*
* Return : Void
*/
void dce_mailbox_deinit_interface(struct tegra_dce *d, u8 id)
{
struct dce_mailbox_interface *d_mb;
d_mb = &d->d_mb[id];
dce_os_mutex_destroy(&d_mb->lock);
}