Initial commit: Linux kernel 4.19 for Unplugged OS

This commit is contained in:
ziv committed 2025-10-06 13:52:42 +00:00
commit d30235b04c
80519 files changed
+35800681

No files matched your search

@@ -0,0 +1,65 @@
#
# Copyright (C) 2016 MediaTek Inc.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# See http://www.gnu.org/licenses/gpl-2.0.html for more details.
#
### include
ccflags-y += \
-I$(srctree)/drivers/misc/mediatek/base/power/include \
-I$(srctree)/drivers/misc/mediatek/scp/include \
-I$(srctree)/drivers/misc/mediatek/scp/cm4 \
-I$(srctree)/drivers/misc/mediatek/adsp/common_v2 \
-I$(srctree)/drivers/misc/mediatek/adsp/$(MTK_PLATFORM) \
-I$(srctree)/drivers/misc/mediatek/audio_ipi/$(CONFIG_MTK_PLATFORM) \
-I$(srctree)/drivers/misc/mediatek/audio_ipi/common_v2/utility
### DO
ifeq ($(CONFIG_MTK_DO),y)
ccflags-y += -I$(srctree)/drivers/staging/dyn_obj/$(MTK_PLATFORM)
endif
### call
ifeq ($(CONFIG_MTK_AURISYS_PHONE_CALL_SUPPORT),y)
ccflags-y += -I$(srctree)/drivers/misc/mediatek/audio_ipi/common_v2/tasks/call
endif
### mp3
ifeq ($(CONFIG_MTK_AUDIO_TUNNELING_SUPPORT),y)
ccflags-y += -I$(srctree)/drivers/misc/mediatek/audio_ipi/common_v2/tasks/mp3
endif
### spkprotect for headfile include path
ccflags-y += -I$(srctree)/drivers/misc/mediatek/audio_ipi/common_v2/tasks/spkprotect
ccflags-y += -I$(srctree)/drivers/misc/mediatek/audio_ipi/common_v2/tasks/audplayback
### c files
obj-y += audio_ringbuf.o
obj-y += audio_ipi_dma.o
obj-y += audio_ipi_driver.o
obj-y += audio_ipi_queue.o
obj-y += audio_messenger_ipi.o
obj-y += audio_task_manager.o
obj-y += adsp_ipi_queue.o
ifeq ($(CONFIG_MT_ENG_BUILD),y)
CFLAGS_audio_ringbuf.o += -DDEBUG
CFLAGS_audio_ipi_dma.o += -DDEBUG
CFLAGS_audio_ipi_driver.o += -DDEBUG
CFLAGS_audio_ipi_queue.o += -DDEBUG
CFLAGS_audio_messenger_ipi.o += -DDEBUG
CFLAGS_audio_task_manager.o += -DDEBUG
CFLAGS_adsp_ipi_queue.o += -DDEBUG
endif
File diff suppressed because it is too large. Load diff
@@ -0,0 +1,40 @@
// SPDX-License-Identifier: GPL-2.0
//
// adsp_ipi_queue.h
//
// Copyright (c) 2018 MediaTek Inc.
#ifndef MTK_ADSP_IPI_QUEUE_H
#define MTK_ADSP_IPI_QUEUE_H
#include <linux/types.h>
#define ADSP_IPI_QUEUE_DEFAULT_WAIT_MS (20)
int scp_ipi_queue_init(uint32_t dsp_id); /* enum dsp_id */
bool is_scp_ipi_queue_init(const uint32_t dsp_id);
int scp_flush_msg_queue(uint32_t dsp_id);
int scp_send_msg_to_queue(
uint32_t dsp_id, /* enum dsp_id */
uint32_t ipi_id, /* enum adsp_ipi_id */
void *buf,
uint32_t len,
uint32_t wait_ms);
int scp_dispatch_ipi_hanlder_to_queue(
uint32_t dsp_id, /* enum dsp_id */
uint32_t ipi_id, /* enum adsp_ipi_id */
void *buf,
uint32_t len,
void (*ipi_handler)(int ipi_id, void *buf, unsigned int len));
#endif /* end of MTK_ADSP_IPI_QUEUE_H */
@@ -0,0 +1,48 @@
// SPDX-License-Identifier: GPL-2.0
//
// adsp_controller_msg_id.h
//
// Copyright (c) 2018 MediaTek Inc.
#ifndef AUDIO_CONTROLLER_MSG_ID_H
#define AUDIO_CONTROLLER_MSG_ID_H
#define AUD_CTL_MSG_A2D_BASE (0xAD00)
#define AUD_CTL_MSG_D2A_BASE (0xDA00)
#define AUD_CTL_MSG_D2D_BASE (0xDD00)
/* NOTE: all ack behaviors are rely on audio_ipi_msg_ack_t */
enum AUD_CTL_MSG_id_call_t {
/* Aurisys control msg, 0xADA- */
AUD_CTL_MSG_A2D_AURISYS_CFG = AUD_CTL_MSG_A2D_BASE + 0xA0,
AUD_CTL_MSG_A2D_AURISYS_SET_PARAM = AUD_CTL_MSG_A2D_BASE + 0xA1,
AUD_CTL_MSG_A2D_AURISYS_GET_PARAM = AUD_CTL_MSG_A2D_BASE + 0xA2,
AUD_CTL_MSG_A2D_AURISYS_ENABLE = AUD_CTL_MSG_A2D_BASE + 0xA3,
/* Boot & recovery */
AUD_CTL_MSG_A2D_HAL_REBOOT = AUD_CTL_MSG_A2D_BASE + 0xB0,
/* DMA control msg, 0xADD- */
AUD_CTL_MSG_A2D_DMA_INIT = AUD_CTL_MSG_A2D_BASE + 0xD0,
AUD_CTL_MSG_A2D_DMA_UPDATE_REGION = AUD_CTL_MSG_A2D_BASE + 0xD1,
/* Aurisys dump msg, 0xDAA- */
AUD_CTL_MSG_D2A_AURISYS_DUMP = AUD_CTL_MSG_D2A_BASE + 0xA0,
/* IRQ, DSP to DSP, 0xDDD- */
AUD_CTL_MSG_D2D_IRQ = AUD_CTL_MSG_D2D_BASE + 0x00,
/* uint16_t */
AUD_CTL_MSG_MAX = 0xFFFF
};
#endif /* end of AUDIO_CONTROLLER_MSG_ID_H */
File diff suppressed because it is too large. Load diff
@@ -0,0 +1,135 @@
// SPDX-License-Identifier: GPL-2.0
//
// audio_ipi_dma.h
//
// Copyright (c) 2018 MediaTek Inc.
#ifndef AUDUI_IPI_DMA_H
#define AUDUI_IPI_DMA_H
#include <linux/types.h>
/*
* =============================================================================
* MACRO
* =============================================================================
*/
/*
* =============================================================================
* struct def
* =============================================================================
*/
enum { /* audio_ipi_dma_path_t */
AUDIO_IPI_DMA_AP_TO_SCP = 0,
AUDIO_IPI_DMA_SCP_TO_AP = 1,
NUM_AUDIO_IPI_DMA_PATH
};
struct aud_ptr_t {
union {
uint8_t *addr;
uint64_t addr_val; /* the value of address */
uint32_t dummy[2]; /* work between 32/64 bit environment */
};
};
/*
* =============================================================================
* ref struct
* =============================================================================
*/
struct ipi_msg_t;
/*
* =============================================================================
* init
* =============================================================================
*/
/* kernel */
int init_audio_ipi_dma(const uint32_t dsp_id);
int deinit_audio_ipi_dma(const uint32_t dsp_id);
/* dsp */
int audio_ipi_dma_init_dsp(const uint32_t dsp_id);
/*
* =============================================================================
* alloc/free
* =============================================================================
*/
int audio_ipi_dma_alloc(const uint8_t task,
phys_addr_t *phy_addr,
void **virt_addr,
const uint32_t size);
int audio_ipi_dma_free(const uint8_t task,
phys_addr_t phy_addr,
const uint32_t size);
/*
* =============================================================================
* data retion
* =============================================================================
*/
int audio_ipi_dma_alloc_region(const uint8_t task,
const uint32_t ap_to_dsp_size,
const uint32_t dsp_to_ap_size);
int audio_ipi_dma_free_region(const uint8_t task);
int audio_ipi_dma_free_region_all_task(uint32_t dsp_id);
int audio_ipi_dma_write_region(const uint8_t task,
const void *data_buf,
uint32_t data_size,
uint32_t *write_idx);
int audio_ipi_dma_read_region(const uint8_t task,
void *data_buf,
uint32_t data_size,
uint32_t read_idx);
int audio_ipi_dma_drop_region(const uint8_t task,
uint32_t data_size,
uint32_t read_idx);
/*
* =============================================================================
* data retion
* =============================================================================
*/
int audio_ipi_dma_msg_to_hal(struct ipi_msg_t *p_ipi_msg);
size_t audio_ipi_dma_msg_read(void __user *buf, size_t count);
#endif /* end of AUDUI_IPI_DMA_H */
@@ -0,0 +1,790 @@
// SPDX-License-Identifier: GPL-2.0
//
// audio_ipi_driver.c
//
// Copyright (c) 2018 MediaTek Inc.
#include <linux/module.h> /* needed by all modules */
#include <linux/init.h> /* needed by module macros */
#include <linux/fs.h> /* needed by file_operations* */
#include <linux/miscdevice.h> /* needed by miscdevice* */
#include <linux/sysfs.h>
#include <linux/device.h> /* needed by device_* */
#include <linux/vmalloc.h> /* needed by vmalloc */
#include <linux/uaccess.h> /* needed by copy_to_user */
#include <linux/poll.h> /* needed by poll */
#include <linux/mutex.h>
#include <linux/sched.h>
#include <linux/interrupt.h>
#include <linux/suspend.h>
#include <linux/timer.h>
#include <linux/notifier.h>
#include <linux/of.h>
#include <linux/of_address.h>
#include <linux/of_irq.h>
#include <linux/of_fdt.h>
#include <linux/ioport.h>
#include <linux/io.h>
#include <audio_ipi_queue.h>
#ifdef CONFIG_MTK_AUDIO_CM4_SUPPORT
#include <scp_helper.h>
#include <scp_ipi.h>
#include <scp_feature_define.h>
#endif
#ifdef CONFIG_MTK_AUDIODSP_SUPPORT
#include <adsp_helper.h>
#endif
#include "audio_log.h"
#include "audio_assert.h"
#include <audio_controller_msg_id.h>
#include <audio_messenger_ipi.h>
#include <audio_task_manager.h>
#include <audio_ipi_dma.h>
#include <audio_ipi_platform.h>
#include <adsp_ipi_queue.h>
#ifdef CONFIG_MTK_AURISYS_PHONE_CALL_SUPPORT
#include <audio_ipi_client_phone_call.h>
#endif
/*
* =============================================================================
* log
* =============================================================================
*/
#ifdef ipi_dbg
#undef ipi_dbg
#endif
#if 0
#define ipi_dbg(x...) pr_info(x)
#else
#define ipi_dbg(x...)
#endif
#ifdef pr_fmt
#undef pr_fmt
#endif
#define pr_fmt(fmt) "[IPI][DRV] %s(), " fmt "\n", __func__
/*
* =============================================================================
* MACRO
* =============================================================================
*/
#define AUDIO_IPI_DEVICE_NAME "audio_ipi"
#define AUDIO_IPI_IOC_MAGIC 'i'
#define AUDIO_IPI_IOCTL_SEND_MSG_ONLY _IOW(AUDIO_IPI_IOC_MAGIC, 0, unsigned int)
#define AUDIO_IPI_IOCTL_SEND_PAYLOAD _IOW(AUDIO_IPI_IOC_MAGIC, 1, unsigned int)
#define AUDIO_IPI_IOCTL_SEND_DRAM _IOW(AUDIO_IPI_IOC_MAGIC, 2, unsigned int)
#define AUDIO_IPI_IOCTL_LOAD_SCENE _IOW(AUDIO_IPI_IOC_MAGIC, 10, unsigned int)
#define AUDIO_IPI_IOCTL_INIT_DSP _IOW(AUDIO_IPI_IOC_MAGIC, 20, unsigned int)
#define AUDIO_IPI_IOCTL_REG_DMA _IOW(AUDIO_IPI_IOC_MAGIC, 21, unsigned int)
/*
* =============================================================================
* struct
* =============================================================================
*/
struct audio_ipi_reg_dma_t {
uint32_t magic_header;
uint8_t task;
uint8_t reg_flag; /* 1: register, 0: unregister */
uint16_t __reserved;
uint32_t a2d_size;
uint32_t d2a_size;
uint32_t magic_footer;
};
struct audio_ipi_reg_feature_t {
uint16_t reg_flag;
uint16_t feature_id;
};
struct audio_task_info_t {
uint32_t dsp_id; /* dsp_id_t */
uint8_t is_dsp_support; /* dsp_id supported or not */
uint8_t is_adsp; /* adsp(HiFi) or not */
uint8_t is_scp; /* scp(CM4) or not */
uint8_t task_ctrl; /* task controller scene # */
};
/*
* =============================================================================
* global var
* =============================================================================
*/
static struct audio_task_info_t g_audio_task_info[TASK_SCENE_SIZE];
static DEFINE_MUTEX(init_dsp_lock);
static DEFINE_MUTEX(reg_dma_lock);
/*
* =============================================================================
* implementation
* =============================================================================
*/
inline uint32_t msg_len_of_type(const uint8_t data_type)
{
uint32_t msg_len = 0;
switch (data_type) {
case AUDIO_IPI_MSG_ONLY:
msg_len = IPI_MSG_HEADER_SIZE;
break;
case AUDIO_IPI_PAYLOAD:
msg_len = MAX_IPI_MSG_BUF_SIZE;
break;
case AUDIO_IPI_DMA:
msg_len = IPI_MSG_HEADER_SIZE + IPI_MSG_DMA_INFO_SIZE;
break;
default:
pr_info("%d not support!!", data_type);
msg_len = IPI_MSG_HEADER_SIZE;
}
return msg_len;
}
static int parsing_ipi_msg_from_user_space(
void __user *user_data_ptr,
uint8_t data_type)
{
struct ipi_msg_t ipi_msg;
struct ipi_msg_dma_info_t *dma_info = NULL;
struct aud_data_t *wb_dram = NULL;
uint32_t msg_len = 0;
uint32_t hal_data_size = 0;
void *copy_hal_data = NULL;
uint32_t hal_wb_buf_size = 0;
void __user *hal_wb_buf_addr = NULL;
phys_addr_t dram_buf = 0;
void *dram_buf_virt = NULL;
int retval = 0;
/* get message size to read */
msg_len = msg_len_of_type(data_type);
if (msg_len > sizeof(struct ipi_msg_t)) {
pr_notice("msg_len %u > %zu!!",
msg_len, sizeof(struct ipi_msg_t));
retval = -1;
goto parsing_exit;
}
memset(&ipi_msg, 0, sizeof(struct ipi_msg_t));
retval = copy_from_user(&ipi_msg, user_data_ptr, msg_len);
if (retval != 0) {
pr_notice("msg copy_from_user retval %d", retval);
goto parsing_exit;
}
if (ipi_msg.data_type != data_type) { /* double check */
pr_notice("data_type %d != %d", ipi_msg.data_type, data_type);
retval = -1;
goto parsing_exit;
}
if (ipi_msg.source_layer != AUDIO_IPI_LAYER_FROM_HAL) {
pr_notice("source_layer %d != %d", ipi_msg.source_layer, AUDIO_IPI_LAYER_FROM_HAL);
retval = -1;
goto parsing_exit;
}
msg_len = get_message_buf_size(&ipi_msg);
retval = check_msg_format(&ipi_msg, msg_len);
if (retval != 0)
goto parsing_exit;
ipi_dbg("task %d msg 0x%x", ipi_msg.task_scene, ipi_msg.msg_id);
/* get dma buf if need */
dma_info = &ipi_msg.dma_info;
wb_dram = &dma_info->wb_dram;
if (data_type == AUDIO_IPI_DMA) {
/* check DMA & Write-Back size */
hal_data_size = dma_info->hal_buf.data_size;
if (hal_data_size > MAX_DSP_DMA_WRITE_SIZE) {
DUMP_IPI_MSG("hal_data_size error!!", &ipi_msg);
retval = -1;
goto parsing_exit;
}
hal_wb_buf_size = dma_info->hal_buf.memory_size;
if (hal_wb_buf_size > MAX_DSP_DMA_WRITE_SIZE) {
DUMP_IPI_MSG("hal_wb_buf_size error!!", &ipi_msg);
retval = -1;
goto parsing_exit;
}
/* get hal data & write hal data to DRAM */
copy_hal_data = vmalloc(hal_data_size);
if (copy_hal_data == NULL) {
retval = -ENOMEM;
goto parsing_exit;
}
retval = copy_from_user(
copy_hal_data,
(void __user *)dma_info->hal_buf.addr,
hal_data_size);
if (retval != 0) {
pr_notice("dma copy_from_user retval %d", retval);
goto parsing_exit;
}
dma_info->data_size = hal_data_size;
retval = audio_ipi_dma_write_region(
ipi_msg.task_scene,
copy_hal_data,
hal_data_size,
&dma_info->rw_idx);
if (retval != 0) {
pr_notice("dma write region error!!");
goto parsing_exit;
}
/* write back result to hal later, like get parameter */
hal_wb_buf_addr = (void __user *)dma_info->hal_buf.addr;
ipi_dbg(
"write region copy_hal_data(%p), hal_data_size %d, hal_wb_buf_size %d"
, copy_hal_data, hal_data_size, hal_wb_buf_size);
if (hal_wb_buf_size != 0 && hal_wb_buf_addr != NULL) {
/* alloc a dma for wb */
audio_ipi_dma_alloc(ipi_msg.task_scene,
&dram_buf,
&dram_buf_virt,
hal_wb_buf_size);
wb_dram->memory_size = hal_wb_buf_size;
wb_dram->data_size = 0;
wb_dram->addr_val = dram_buf;
ipi_dbg(
"hal_wb_buf_addr(%p), wb dram_buf(%u), hal_wb_buf_size(%d)"
, hal_wb_buf_addr, dram_buf, hal_wb_buf_size);
/* force need ack to get scp info */
if (ipi_msg.ack_type != AUDIO_IPI_MSG_NEED_ACK) {
pr_notice("task %d msg 0x%x need ack!!",
ipi_msg.task_scene, ipi_msg.msg_id);
ipi_msg.ack_type = AUDIO_IPI_MSG_NEED_ACK;
}
}
#if 0 /* debug only */
DUMP_IPI_MSG("dma", &ipi_msg);
#endif
}
/* sent message */
retval = audio_send_ipi_filled_msg(&ipi_msg);
if (retval != 0) {
pr_notice("audio_send_ipi_filled_msg error!!");
goto parsing_exit;
}
/* write back data to hal */
if (data_type == AUDIO_IPI_DMA &&
hal_wb_buf_size != 0 &&
hal_wb_buf_addr != NULL &&
wb_dram != NULL &&
wb_dram->addr_val != 0 &&
ipi_msg.scp_ret == 1) {
if (wb_dram->data_size > hal_wb_buf_size) {
pr_notice("wb_dram->data_size %u > hal_wb_buf_size %u!!",
wb_dram->data_size,
hal_wb_buf_size);
ipi_msg.scp_ret = 0;
} else if (wb_dram->data_size == 0) {
pr_notice("ipi wb data sz = 0!! check adsp write");
ipi_msg.scp_ret = 0;
} else {
retval = copy_to_user(
hal_wb_buf_addr,
dram_buf_virt,
wb_dram->data_size);
if (retval) {
pr_info("copy_to_user dma err, id = 0x%x",
ipi_msg.msg_id);
ipi_msg.scp_ret = 0;
}
}
}
/* write back ipi msg to hal */
if (data_type == AUDIO_IPI_DMA) /* clear sensitive addr info */
memset(&ipi_msg.dma_info, 0, IPI_MSG_DMA_INFO_SIZE);
retval = copy_to_user(user_data_ptr,
&ipi_msg,
sizeof(struct ipi_msg_t));
if (retval) {
pr_info("copy_to_user err, id = 0x%x", ipi_msg.msg_id);
retval = -EFAULT;
}
parsing_exit:
if (copy_hal_data != NULL) {
vfree(copy_hal_data);
copy_hal_data = NULL;
}
if (dram_buf != 0) {
ipi_dbg(
"task %d msg 0x%x, free wb buffer %u, hal_wb_buf_size 0x%x"
, ipi_msg.task_scene, ipi_msg.msg_id,
dram_buf, hal_wb_buf_size);
audio_ipi_dma_free(ipi_msg.task_scene,
dram_buf, hal_wb_buf_size);
}
ipi_dbg("task %d msg 0x%x, retval %d",
ipi_msg.task_scene, ipi_msg.msg_id, retval);
return retval;
}
#if defined(CONFIG_MTK_AUDIODSP_SUPPORT)
/* ADSP reboot */
#ifdef CFG_RECOVERY_SUPPORT
static int audio_ctrl_event_receive(
struct notifier_block *this,
unsigned long event,
void *ptr)
{
struct ipi_queue_handler_t *handler = NULL;
uint8_t scene = 0;
uint32_t dsp_id = 0;
switch (event) {
case ADSP_EVENT_STOP:
for (scene = 0; scene < TASK_SCENE_SIZE; scene++) {
if (is_audio_use_adsp(audio_get_dsp_id(scene))) {
handler = get_ipi_queue_handler(scene);
if (handler != NULL)
flush_ipi_queue_handler(handler);
}
}
break;
case ADSP_EVENT_READY:
for (dsp_id = 0; dsp_id < NUM_OPENDSP_TYPE; dsp_id++) {
if (is_audio_use_adsp(dsp_id))
audio_ipi_dma_init_dsp(dsp_id);
}
break;
default:
pr_info("event %lu err", event);
}
return 0;
}
static struct notifier_block audio_ctrl_notifier = {
.notifier_call = audio_ctrl_event_receive,
.priority = AUDIO_CONTROLLER_FEATURE_PRI,
};
#endif /* end of CFG_RECOVERY_SUPPORT */
/* HAL reboot */
static int audio_ipi_init_dsp_hifi3(const uint32_t dsp_id)
{
static bool init_flag[NUM_OPENDSP_TYPE];
uint8_t task = TASK_SCENE_INVALID;
struct ipi_msg_t ipi_msg;
int ret = 0;
if (dsp_id >= NUM_OPENDSP_TYPE) {
pr_info("dsp_id(%u) invalid!!!", dsp_id);
return -ENODEV;
}
if (!is_audio_use_adsp(dsp_id)) {
pr_info("dsp_id(%u) not for adsp!!!", dsp_id);
return -ENODEV;
}
task = get_audio_controller_task(dsp_id);
if (task == TASK_SCENE_INVALID) {
pr_info("task(%d) invalid!!!", task);
return -ENODEV;
}
/* check phone boot */
if (init_flag[dsp_id] == false) {
init_flag[dsp_id] = true;
pr_info("phone init");
audio_ipi_dma_init_dsp(dsp_id);
return 0;
}
/* wake up adsp */
adsp_register_feature(AUDIO_CONTROLLER_FEATURE_ID);
/* not first init => HAL reboot */
pr_info("audio hal reinit");
/* TODO: do something before ADSP process hal reboot here */
/* ADSP process hal reboot here */
ret = audio_send_ipi_msg(
&ipi_msg,
task,
AUDIO_IPI_LAYER_TO_DSP,
AUDIO_IPI_MSG_ONLY,
AUDIO_IPI_MSG_NEED_ACK,
AUD_CTL_MSG_A2D_HAL_REBOOT,
0, /* 0: audio HAL */
0,
NULL);
/* release DMA */
audio_ipi_dma_free_region_all_task(dsp_id);
/* allow adsp to sleep */
adsp_deregister_feature(AUDIO_CONTROLLER_FEATURE_ID);
/* TODO: clear feature register count here? */
reset_hal_feature_table();
return ret;
}
#endif
#if defined(CONFIG_MTK_AUDIO_CM4_SUPPORT)
/* SCP reboot */
#if defined(SCP_RECOVERY_SUPPORT)
static int audio_ctrl_event_receive_scp(
struct notifier_block *this,
unsigned long event,
void *ptr)
{
struct ipi_queue_handler_t *handler = NULL;
uint8_t scene = 0;
switch (event) {
case SCP_EVENT_STOP:
for (scene = 0; scene < TASK_SCENE_SIZE; scene++) {
if (audio_get_dsp_id(scene) ==
AUDIO_OPENDSP_USE_CM4_A) {
handler = get_ipi_queue_handler(scene);
if (handler != NULL)
flush_ipi_queue_handler(handler);
}
}
break;
#if defined(CONFIG_MTK_AUDIO_CM4_DMA_SUPPORT)
case SCP_EVENT_READY:
audio_ipi_dma_init_dsp(AUDIO_OPENDSP_USE_CM4_A);
break;
#endif
default:
pr_info("event %lu err", event);
}
return 0;
}
static struct notifier_block audio_ctrl_notifier_scp = {
.notifier_call = audio_ctrl_event_receive_scp,
};
#endif /* end of SCP_RECOVERY_SUPPORT */
static int audio_ipi_init_dsp_cm4(void)
{
int ret = 0;
#if defined(CONFIG_MTK_AUDIO_CM4_DMA_SUPPORT)
struct ipi_msg_t ipi_msg;
static bool init_flag;
/* check phone boot */
if (init_flag == false) {
init_flag = true;
pr_info("phone init");
audio_ipi_dma_init_dsp(AUDIO_OPENDSP_USE_CM4_A);
return 0;
}
/* not first init => HAL reboot */
pr_info("audio hal reinit");
/* TODO: do something before SCP process hal reboot here */
/* SCP process hal reboot here */
ret = audio_send_ipi_msg(
&ipi_msg,
TASK_SCENE_AUDIO_CONTROLLER_CM4,
AUDIO_IPI_LAYER_TO_DSP,
AUDIO_IPI_MSG_ONLY,
AUDIO_IPI_MSG_NEED_ACK,
AUD_CTL_MSG_A2D_HAL_REBOOT,
0, /* 0: audio HAL */
0,
NULL);
/* release DMA */
audio_ipi_dma_free_region_all_task(AUDIO_OPENDSP_USE_CM4_A);
#endif
return ret;
}
#endif /* end of CONFIG_MTK_AUDIO_CM4_SUPPORT */
static long audio_ipi_driver_ioctl(
struct file *file, unsigned int cmd, unsigned long arg)
{
#if defined(CONFIG_MTK_AUDIODSP_SUPPORT)
uint32_t dsp_id = 0;
#endif
struct audio_ipi_reg_dma_t dma_reg;
int retval = 0;
uint32_t check_sum = 0;
AUD_LOG_V("cmd = %u, arg = %lu", cmd, arg);
switch (cmd) {
case AUDIO_IPI_IOCTL_SEND_MSG_ONLY: {
retval = parsing_ipi_msg_from_user_space(
(void __user *)arg, AUDIO_IPI_MSG_ONLY);
break;
}
case AUDIO_IPI_IOCTL_SEND_PAYLOAD: {
retval = parsing_ipi_msg_from_user_space(
(void __user *)arg, AUDIO_IPI_PAYLOAD);
break;
}
case AUDIO_IPI_IOCTL_SEND_DRAM: {
retval = parsing_ipi_msg_from_user_space(
(void __user *)arg, AUDIO_IPI_DMA);
break;
}
case AUDIO_IPI_IOCTL_LOAD_SCENE: {
pr_debug("AUDIO_IPI_IOCTL_LOAD_SCENE(%d)", (uint8_t)arg);
audio_load_task((uint8_t)arg);
break;
}
case AUDIO_IPI_IOCTL_INIT_DSP: {
pr_debug("AUDIO_IPI_IOCTL_INIT_DSP");
mutex_lock(&init_dsp_lock);
#if defined(CONFIG_MTK_AUDIODSP_SUPPORT)
for (dsp_id = 0; dsp_id < NUM_OPENDSP_TYPE; dsp_id++) {
if (is_audio_use_adsp(dsp_id))
audio_ipi_init_dsp_hifi3(dsp_id);
}
#endif
#if defined(CONFIG_MTK_AUDIO_CM4_SUPPORT)
audio_ipi_init_dsp_cm4();
#endif
/* copy g_audio_task_info to HAL */
if (((void __user *)arg) == NULL)
retval = -EINVAL;
else {
retval = copy_to_user((void __user *)arg,
g_audio_task_info,
sizeof(g_audio_task_info));
}
if (retval) {
pr_info("task info copy_to_user err");
retval = -EFAULT;
}
mutex_unlock(&init_dsp_lock);
break;
}
case AUDIO_IPI_IOCTL_REG_DMA: {
if (((void __user *)arg) == NULL) {
retval = -1;
break;
}
retval = copy_from_user(
&dma_reg,
(void __user *)arg,
sizeof(struct audio_ipi_reg_dma_t));
if (retval != 0) {
pr_notice("dma reg copy_from_user retval %d", retval);
break;
}
check_sum = dma_reg.magic_footer + dma_reg.magic_header;
if (check_sum != 0xFFFFFFFF) {
pr_notice("dma reg check fail! header(0x%x) footer(0x%x)",
dma_reg.magic_header,
dma_reg.magic_footer);
retval = -1;
break;
}
mutex_lock(&reg_dma_lock);
if (dma_reg.reg_flag)
retval = audio_ipi_dma_alloc_region(dma_reg.task,
dma_reg.a2d_size,
dma_reg.d2a_size);
else
retval = audio_ipi_dma_free_region(dma_reg.task);
mutex_unlock(&reg_dma_lock);
break;
}
default:
retval = -ENOIOCTLCMD;
break;
}
return retval;
}
#ifdef CONFIG_COMPAT
static long audio_ipi_driver_compat_ioctl(
struct file *file, unsigned int cmd, unsigned long arg)
{
if (!file->f_op || !file->f_op->unlocked_ioctl) {
pr_notice("op null");
return -ENOTTY;
}
return file->f_op->unlocked_ioctl(file, cmd, arg);
}
#endif
static ssize_t audio_ipi_driver_read(
struct file *file, char __user *buf, size_t count, loff_t *ppos)
{
return audio_ipi_dma_msg_read(buf, count);
}
static const struct file_operations audio_ipi_driver_ops = {
.owner = THIS_MODULE,
.read = audio_ipi_driver_read,
.unlocked_ioctl = audio_ipi_driver_ioctl,
#ifdef CONFIG_COMPAT
.compat_ioctl = audio_ipi_driver_compat_ioctl,
#endif
};
static struct miscdevice audio_ipi_device = {
.minor = MISC_DYNAMIC_MINOR,
.name = AUDIO_IPI_DEVICE_NAME,
.fops = &audio_ipi_driver_ops
};
static int __init audio_ipi_driver_init(void)
{
int ret = 0;
uint32_t dsp_id = 0;
struct audio_task_info_t *task_info = NULL;
uint8_t task_id = 0;
for (dsp_id = 0; dsp_id < NUM_OPENDSP_TYPE; dsp_id++) {
if (is_audio_dsp_support(dsp_id))
scp_ipi_queue_init(dsp_id);
}
audio_task_manager_init();
audio_messenger_ipi_init();
for (dsp_id = 0; dsp_id < NUM_OPENDSP_TYPE; dsp_id++) {
if (is_audio_dsp_support(dsp_id))
init_audio_ipi_dma(dsp_id);
}
#if defined(CONFIG_MTK_AUDIODSP_SUPPORT) && defined(CFG_RECOVERY_SUPPORT)
adsp_register_notify(&audio_ctrl_notifier);
#endif
#if defined(CONFIG_MTK_AUDIO_CM4_SUPPORT) && defined(SCP_RECOVERY_SUPPORT)
scp_A_register_notify(&audio_ctrl_notifier_scp);
#endif
#if defined(CONFIG_MTK_AURISYS_PHONE_CALL_SUPPORT)
audio_ipi_client_phone_call_init();
#endif
for (task_id = 0; task_id < TASK_SCENE_SIZE; task_id++) {
task_info = &g_audio_task_info[task_id];
dsp_id = audio_get_dsp_id(task_id);
task_info->dsp_id = dsp_id;
task_info->is_dsp_support = is_audio_dsp_support(dsp_id);
task_info->is_adsp = is_audio_use_adsp(dsp_id);
task_info->is_scp = is_audio_use_scp(dsp_id);
task_info->task_ctrl = get_audio_controller_task(dsp_id);
}
ret = misc_register(&audio_ipi_device);
if (unlikely(ret != 0)) {
pr_notice("misc register failed");
return ret;
}
return ret;
}
static void __exit audio_ipi_driver_exit(void)
{
int dsp_id = 0;
#if defined(CONFIG_MTK_AURISYS_PHONE_CALL_SUPPORT)
audio_ipi_client_phone_call_deinit();
#endif
audio_task_manager_deinit();
for (dsp_id = 0; dsp_id < NUM_OPENDSP_TYPE; dsp_id++) {
if (is_audio_dsp_support(dsp_id))
deinit_audio_ipi_dma(dsp_id);
}
misc_deregister(&audio_ipi_device);
}
module_init(audio_ipi_driver_init);
module_exit(audio_ipi_driver_exit);
@@ -0,0 +1,48 @@
// SPDX-License-Identifier: GPL-2.0
//
// adsp_ipi_platform.h
//
// Copyright (c) 2018 MediaTek Inc.
#ifndef __AUDIO_IPI_PLATFORM_H__
#define __AUDIO_IPI_PLATFORM_H__
#include <linux/types.h>
enum dsp_id_t {
AUDIO_OPENDSP_USE_CM4_A, /* => SCP_A_ID */
AUDIO_OPENDSP_USE_CM4_B, /* => SCP_B_ID */
AUDIO_OPENDSP_USE_HIFI3_A, /* => ADSP_A_ID */
AUDIO_OPENDSP_USE_HIFI3_B, /* => ADSP_B_ID */
NUM_OPENDSP_TYPE,
AUDIO_OPENDSP_ID_INVALID
};
bool is_audio_use_adsp(const uint32_t dsp_id);
bool is_audio_use_scp(const uint32_t dsp_id);
bool is_audio_dsp_ready(const uint32_t dsp_id);
bool is_audio_task_dsp_ready(const uint8_t task);
uint32_t audio_get_dsp_id(const uint8_t task);
uint32_t audio_get_ipi_id(const uint8_t task);
bool is_audio_dsp_support(const uint32_t dsp_id);
uint8_t get_audio_controller_task(const uint32_t dsp_id);
int get_reserve_mem_size(const uint32_t dsp_id,
uint32_t *mem_id, uint32_t *size);
void *get_reserve_mem_virt(const uint32_t dsp_id, const uint32_t mem_id);
phys_addr_t get_reserve_mem_phys(const uint32_t dsp_id, const uint32_t mem_id);
uint8_t get_cache_aligned_order(const uint32_t dsp_id);
uint8_t get_cache_aligned_mask(const uint32_t dsp_id);
#endif /*__AUDIO_IPI_PLATFORM_H__ */
@@ -0,0 +1,827 @@
// SPDX-License-Identifier: GPL-2.0
//
// adsp_ipi_queue.c
//
// Copyright (c) 2018 MediaTek Inc.
#include "audio_ipi_queue.h"
#include <linux/vmalloc.h>
#include <linux/kthread.h>
#include <linux/wait.h>
#include <linux/spinlock.h>
#include <linux/delay.h>
#ifdef CONFIG_MTK_AUDIO_CM4_SUPPORT
#include <scp_ipi.h>
#endif
#include "audio_log.h"
#include "audio_assert.h"
#include "audio_ipi_platform.h"
#include "audio_messenger_ipi.h"
#include "audio_task.h"
/*
* =============================================================================
* log
* =============================================================================
*/
#ifdef pr_fmt
#undef pr_fmt
#endif
#define pr_fmt(fmt) "[IPI][TASK_Q] %s(), " fmt "\n", __func__
/*
* =============================================================================
* MACRO
* =============================================================================
*/
#define MAX_IPI_MSG_QUEUE_SIZE (16)
/*
* =============================================================================
* typedef
* =============================================================================
*/
struct queue_element_t {
struct ipi_msg_t *msg;
wait_queue_head_t wq;
};
struct msg_queue_t {
uint8_t k_element_size;
struct queue_element_t element[MAX_IPI_MSG_QUEUE_SIZE];
uint8_t task_scene; /* task_scene_t */
uint8_t idx_r;
uint8_t idx_w;
spinlock_t rw_lock;
struct ipi_msg_t ipi_msg_ack;
spinlock_t ack_lock;
bool enable;
};
/*
* =============================================================================
* private global members
* =============================================================================
*/
static struct ipi_queue_handler_t g_ipi_queue_handler[TASK_SCENE_SIZE];
/*
* =============================================================================
* private function declaration
* =============================================================================
*/
static struct msg_queue_t *create_msg_queue(const uint8_t task_scene);
static void destroy_msg_queue(struct msg_queue_t *msg_queue);
static int process_message_in_queue(
struct msg_queue_t *msg_queue,
struct ipi_msg_t *p_ipi_msg,
int idx_msg);
/*
* =============================================================================
* utilities
* =============================================================================
*/
inline bool check_queue_empty(const struct msg_queue_t *msg_queue);
inline bool check_queue_to_be_full(const struct msg_queue_t *msg_queue);
inline uint8_t get_num_messages_in_queue(const struct msg_queue_t *msg_queue);
inline int push_msg(
struct msg_queue_t *msg_queue,
struct ipi_msg_t *p_ipi_msg);
inline int pop_msg(
struct msg_queue_t *msg_queue,
struct ipi_msg_t **pp_ipi_msg);
inline bool check_idx_msg_valid(
struct msg_queue_t *msg_queue,
int idx_msg);
inline bool check_ack_msg_valid(
const struct ipi_msg_t *p_ipi_msg,
const struct ipi_msg_t *p_ipi_msg_ack);
/*
* =============================================================================
* create/destroy/init/deinit functions
* =============================================================================
*/
struct ipi_queue_handler_t *create_ipi_queue_handler(const uint8_t task_scene)
{
struct ipi_queue_handler_t *handler = NULL;
/* error handling */
if (task_scene >= TASK_SCENE_SIZE) {
pr_info("task_scene %d invalid!! return NULL", task_scene);
return NULL;
}
/* create handler */
handler = &g_ipi_queue_handler[task_scene];
if (handler->msg_queue == NULL) {
handler->msg_queue = (void *)create_msg_queue(task_scene);
if (handler->msg_queue == NULL) {
pr_notice("task_scene %d create fail!!", task_scene);
return NULL;
}
}
return handler;
}
static struct msg_queue_t *create_msg_queue(const uint8_t task_scene)
{
struct msg_queue_t *msg_queue = NULL;
int i = 0;
/* malloc */
msg_queue = vmalloc(sizeof(struct msg_queue_t));
if (msg_queue == NULL)
return NULL;
/* init var */
msg_queue->k_element_size = MAX_IPI_MSG_QUEUE_SIZE;
for (i = 0; i < msg_queue->k_element_size; i++) {
msg_queue->element[i].msg = NULL;
init_waitqueue_head(&msg_queue->element[i].wq);
}
msg_queue->task_scene = task_scene;
msg_queue->idx_r = 0;
msg_queue->idx_w = 0;
spin_lock_init(&msg_queue->rw_lock);
spin_lock_init(&msg_queue->ack_lock);
memset(&msg_queue->ipi_msg_ack, 0, sizeof(struct ipi_msg_t));
msg_queue->enable = true;
return msg_queue;
}
void destroy_msg_queue(struct msg_queue_t *msg_queue)
{
/* error handling */
if (msg_queue == NULL) {
pr_info("msg_queue == NULL!! return");
return;
}
AUD_ASSERT(check_queue_empty(msg_queue));
/* free */
vfree(msg_queue);
msg_queue = NULL;
}
void destroy_ipi_queue_handler(struct ipi_queue_handler_t *handler)
{
/* error handling */
if (handler == NULL) {
pr_info("handler == NULL!! return");
return;
}
/* destroy handler */
destroy_msg_queue((struct msg_queue_t *)handler->msg_queue);
handler->msg_queue = NULL;
}
struct ipi_queue_handler_t *get_ipi_queue_handler(const uint8_t task_scene)
{
/* error handling */
if (task_scene >= TASK_SCENE_SIZE) {
pr_info("task_scene %d invalid!! return NULL", task_scene);
return NULL;
}
/* TODO: get/create refine */
return create_ipi_queue_handler(task_scene);
}
void disable_ipi_queue_handler(struct ipi_queue_handler_t *handler)
{
struct msg_queue_t *msg_queue = NULL;
/* error handling */
if (handler == NULL) {
pr_info("handler == NULL!! return");
return;
}
msg_queue = (struct msg_queue_t *)handler->msg_queue;
msg_queue->enable = false;
}
int flush_ipi_queue_handler(struct ipi_queue_handler_t *handler)
{
struct msg_queue_t *msg_queue = NULL;
struct ipi_msg_t *p_ipi_msg = NULL;
const uint16_t k_max_wait_times = 100;
uint16_t i = 0;
unsigned long flags = 0;
/* error handling */
if (handler == NULL) {
pr_info("handler == NULL!! return");
return -1;
}
msg_queue = (struct msg_queue_t *)handler->msg_queue;
spin_lock_irqsave(&msg_queue->rw_lock, flags);
if (msg_queue->idx_r >= MAX_IPI_MSG_QUEUE_SIZE) {
pr_info("idx_r %d >= %d(%d)!!", msg_queue->idx_r,
MAX_IPI_MSG_QUEUE_SIZE, msg_queue->k_element_size);
} else if (check_queue_empty(msg_queue) == false) {
p_ipi_msg = msg_queue->element[msg_queue->idx_r].msg;
if (p_ipi_msg->ack_type == AUDIO_IPI_MSG_NEED_ACK) {
DUMP_IPI_MSG("fake ack return", p_ipi_msg);
dsb(SY);
wake_up_interruptible(
&msg_queue->element[msg_queue->idx_r].wq);
}
}
spin_unlock_irqrestore(&msg_queue->rw_lock, flags); /* TODO: check */
for (i = 0; i < k_max_wait_times; i++) {
if (check_queue_empty(msg_queue))
break;
usleep_range(500, 600);
}
return 0;
}
/*
* =============================================================================
* main functions
* =============================================================================
*/
int send_message(
struct ipi_queue_handler_t *handler,
struct ipi_msg_t *p_ipi_msg)
{
struct msg_queue_t *msg_queue = NULL;
bool is_queue_empty = false;
unsigned long flags = 0;
int idx_msg = -1;
int retval = 0;
uint32_t try_cnt = 0;
const uint32_t k_max_try_cnt = 100; /* retry 1 sec for -ERESTARTSYS */
const uint32_t k_restart_sleep_min_us = 10 * 1000; /* 10 ms */
const uint32_t k_restart_sleep_max_us = (k_restart_sleep_min_us + 200);
/* error handling */
if (handler == NULL) {
pr_info("handler == NULL!! return");
return -1;
}
if (p_ipi_msg == NULL) {
pr_info("p_ipi_msg == NULL!! return");
return -1;
}
if (is_audio_task_dsp_ready(p_ipi_msg->task_scene) == false) {
pr_info("dsp not ready!! return");
return -1;
}
/* send to scp directly (bypass audio queue, but still in IPC queue) */
if (p_ipi_msg->ack_type == AUDIO_IPI_MSG_DIRECT_SEND)
return send_message_to_scp(p_ipi_msg);
/* send message in queue */
msg_queue = (struct msg_queue_t *)handler->msg_queue;
if (msg_queue->enable == false) {
pr_info("queue disabled!! return");
return -1;
}
spin_lock_irqsave(&msg_queue->rw_lock, flags);
is_queue_empty = check_queue_empty(msg_queue);
idx_msg = push_msg(msg_queue, p_ipi_msg);
spin_unlock_irqrestore(&msg_queue->rw_lock, flags);
if (check_idx_msg_valid(msg_queue, idx_msg) == false) {
pr_info("idx_msg %d is invalid!! return", idx_msg);
return -1;
}
/* process queue */
if (is_queue_empty == true) { /* just send message to scp */
/* no other working msg ack */
if (msg_queue->ipi_msg_ack.magic != 0) {
DUMP_IPI_MSG("ack not clean", &msg_queue->ipi_msg_ack);
memset(&msg_queue->ipi_msg_ack,
0,
sizeof(struct ipi_msg_t));
}
retval = process_message_in_queue(
msg_queue, p_ipi_msg, idx_msg);
} else { /* wait until processed, and then send message to scp */
for (try_cnt = 0; try_cnt < k_max_try_cnt; try_cnt++) {
retval = wait_event_interruptible(
msg_queue->element[idx_msg].wq,
(msg_queue->idx_r == idx_msg ||
msg_queue->enable == false));
if (msg_queue->enable == false) {
DUMP_IPI_MSG("enable == false", p_ipi_msg);
retval = 0;
break;
}
if (!is_audio_task_dsp_ready(p_ipi_msg->task_scene)) {
DUMP_IPI_MSG("dsp not ready", p_ipi_msg);
return 0;
}
if (msg_queue->idx_r == idx_msg) {
retval = 0;
break;
}
if (retval == 0) {
pr_notice("wait ret 0, idx %d/%d, enable %d",
msg_queue->idx_r,
idx_msg,
msg_queue->enable);
break;
}
if (retval == -ERESTARTSYS) {
pr_info("-ERESTARTSYS, #%u, sleep us: %u",
try_cnt, k_restart_sleep_min_us);
DUMP_IPI_MSG("wait queue head", p_ipi_msg);
retval = -EINTR;
usleep_range(k_restart_sleep_min_us,
k_restart_sleep_max_us);
continue;
}
pr_notice("retval: %d not handle!!", retval);
}
if (retval != 0) {
DUMP_IPI_MSG("task queue timeout", p_ipi_msg);
pr_notice("retval: %d!!", retval);
} else if (msg_queue->idx_r == idx_msg) {
retval = process_message_in_queue(
msg_queue, p_ipi_msg, idx_msg);
} else {
pr_notice("idx %d/%d, enable %d",
msg_queue->idx_r,
idx_msg,
msg_queue->enable);
DUMP_IPI_MSG("drop msg", p_ipi_msg);
retval = -1;
}
}
return retval;
}
int send_message_ack(
struct ipi_queue_handler_t *handler,
struct ipi_msg_t *p_ipi_msg_ack)
{
struct msg_queue_t *msg_queue = NULL;
uint8_t task_scene = 0xFF;
unsigned long flags = 0;
/* error handling */
if (handler == NULL) {
pr_info("handler == NULL!! return");
return -1;
}
if (p_ipi_msg_ack == NULL) {
pr_notice("p_ipi_msg_ack = NULL, return");
return -1;
}
if (p_ipi_msg_ack->ack_type != AUDIO_IPI_MSG_ACK_BACK) {
pr_notice("ack_type %d invalid, return",
p_ipi_msg_ack->ack_type);
return -1;
}
/* get info */
msg_queue = (struct msg_queue_t *)handler->msg_queue;
task_scene = msg_queue->task_scene;
if (msg_queue->enable == false) {
pr_info("queue disabled!! return");
return -1;
}
/* get msg ack & wake up queue */
spin_lock_irqsave(&msg_queue->ack_lock, flags);
if (msg_queue->ipi_msg_ack.magic != 0) {
DUMP_IPI_MSG("previous ack not clean", &msg_queue->ipi_msg_ack);
DUMP_IPI_MSG("new ack", p_ipi_msg_ack);
}
memcpy(&msg_queue->ipi_msg_ack,
p_ipi_msg_ack,
sizeof(struct ipi_msg_t));
dsb(SY);
wake_up_interruptible(&msg_queue->element[msg_queue->idx_r].wq);
spin_unlock_irqrestore(&msg_queue->ack_lock, flags);
return 0;
}
static int process_message_in_queue(
struct msg_queue_t *msg_queue,
struct ipi_msg_t *p_ipi_msg,
int idx_msg)
{
struct ipi_msg_t *p_ipi_msg_pop = NULL;
bool is_queue_empty = false;
unsigned long flags = 0;
int retval = 0;
uint32_t try_cnt = 0;
const uint32_t k_wait_ms = 10; /* 10 ms */
const uint32_t k_max_try_cnt = 100; /* retry 1 sec for -ERESTARTSYS */
const uint32_t k_restart_sleep_min_us = k_wait_ms * 1000;
const uint32_t k_restart_sleep_max_us = (k_restart_sleep_min_us + 200);
struct queue_element_t *p_element = NULL;
struct ipi_msg_t *p_ack = NULL;
bool timeout_flag = false;
/* error handling */
if (msg_queue == NULL) {
pr_info("msg_queue == NULL!! return");
return -1;
}
if (p_ipi_msg == NULL) {
pr_info("p_ipi_msg == NULL!! return");
return -1;
}
if (check_idx_msg_valid(msg_queue, idx_msg) == false) {
pr_info("idx_msg %d is invalid!! return", idx_msg);
return -1;
}
/* process message */
AUD_ASSERT(idx_msg == msg_queue->idx_r);
p_element = &msg_queue->element[msg_queue->idx_r];
p_ack = &msg_queue->ipi_msg_ack;
switch (p_ipi_msg->ack_type) {
case AUDIO_IPI_MSG_BYPASS_ACK: {
/* no need ack, send directly and then just return */
if (msg_queue->enable == false) {
DUMP_IPI_MSG("queue disable", p_ipi_msg);
retval = 0;
break;
}
retval = send_message_to_scp(p_ipi_msg);
break;
}
case AUDIO_IPI_MSG_NEED_ACK: {
/* need ack, send and then wait until ack back */
if (msg_queue->enable == false) {
DUMP_IPI_MSG("queue disable", p_ipi_msg);
retval = 0;
break;
}
retval = send_message_to_scp(p_ipi_msg);
if (retval != 0) {
p_ipi_msg->ack_type = AUDIO_IPI_MSG_CANCELED;
break;
}
/* send to scp succeed, wait ack */
for (try_cnt = 0; try_cnt < k_max_try_cnt; try_cnt++) {
retval = wait_event_interruptible_timeout(
p_element->wq,
p_ack->magic == IPI_MSG_MAGIC_NUMBER ||
msg_queue->enable == false,
msecs_to_jiffies(k_wait_ms));
if (msg_queue->enable == false) {
DUMP_IPI_MSG("queue disable", p_ipi_msg);
retval = -ENODEV;
break;
}
if (p_ack->magic == IPI_MSG_MAGIC_NUMBER) {
retval = 0;
break;
}
if (!is_audio_task_dsp_ready(p_ipi_msg->task_scene)) {
DUMP_IPI_MSG("dsp not ready", p_ipi_msg);
retval = -ENODEV;
break;
}
if (retval > 0) {
pr_notice("wait ret %d, enable %d",
retval,
msg_queue->enable);
DUMP_IPI_MSG("ack signal?", p_ipi_msg);
retval = 0;
break;
}
if (retval == 0) { /* 10 ms timeout */
if (timeout_flag == false) {
DUMP_IPI_MSG("wait ack", p_ipi_msg);
timeout_flag = true;
}
retval = -ETIMEDOUT;
continue;
}
if (retval == -ERESTARTSYS) {
DUMP_IPI_MSG("-ERESTARTSYS", p_ipi_msg);
retval = -EINTR;
usleep_range(k_restart_sleep_min_us,
k_restart_sleep_max_us);
continue;
}
pr_notice("retval: %d not handle!!", retval);
}
if (timeout_flag == true)
pr_info("wait %u x %u ms, retval %d",
k_wait_ms, try_cnt, retval);
if (retval == -ENODEV)
break;
if (retval == -EINTR) {
DUMP_IPI_MSG("-EINTR!!", p_ipi_msg);
DUMP_IPI_MSG("-EINTR!!", p_ack);
memset(p_ack, 0, sizeof(struct ipi_msg_t));
break;
}
if (retval == -ETIMEDOUT) {
DUMP_IPI_MSG("timeout!! msg", p_ipi_msg);
DUMP_IPI_MSG("timeout!! ack", p_ack);
break;
}
if (check_print_msg_info(p_ack) == true)
DUMP_IPI_MSG("ack back", p_ipi_msg);
/* should be in pair */
spin_lock_irqsave(&msg_queue->ack_lock, flags);
if (!check_ack_msg_valid(p_ipi_msg, p_ack)) {
DUMP_IPI_MSG("ack not pair", p_ipi_msg);
DUMP_IPI_MSG("ack not pair", p_ack);
memset(p_ack, 0, sizeof(struct ipi_msg_t));
retval = -1;
spin_unlock_irqrestore(&msg_queue->ack_lock, flags);
break;
}
memcpy(p_ipi_msg, p_ack, sizeof(struct ipi_msg_t));
memset(p_ack, 0, sizeof(struct ipi_msg_t));
spin_unlock_irqrestore(&msg_queue->ack_lock, flags);
retval = 0;
break;
}
default:
DUMP_IPI_MSG("invalid ack_type", p_ipi_msg);
retval = -1;
WARN_ON(1);
break;
}
/* pop message from queue & wake up next message */
spin_lock_irqsave(&msg_queue->rw_lock, flags);
pop_msg(msg_queue, &p_ipi_msg_pop);
is_queue_empty = check_queue_empty(msg_queue);
spin_unlock_irqrestore(&msg_queue->rw_lock, flags);
AUD_ASSERT(p_ipi_msg_pop == p_ipi_msg);
if (is_queue_empty == false) {
dsb(SY);
wake_up_interruptible(&msg_queue->element[msg_queue->idx_r].wq);
}
return retval;
}
/*
* =============================================================================
* utilities
* =============================================================================
*/
inline bool check_queue_empty(const struct msg_queue_t *msg_queue)
{
/* error handling */
if (msg_queue == NULL) {
pr_info("msg_queue == NULL!! return");
return false;
}
return (msg_queue->idx_r == msg_queue->idx_w);
}
inline bool check_queue_to_be_full(const struct msg_queue_t *msg_queue)
{
uint8_t idx_w_to_be = 0;
/* error handling */
if (msg_queue == NULL) {
pr_info("msg_queue == NULL!! return");
return false;
}
idx_w_to_be = msg_queue->idx_w + 1;
if (idx_w_to_be == msg_queue->k_element_size)
idx_w_to_be = 0;
return (idx_w_to_be == msg_queue->idx_r) ? true : false;
}
inline uint8_t get_num_messages_in_queue(const struct msg_queue_t *msg_queue)
{
/* error handling */
if (msg_queue == NULL) {
pr_info("msg_queue == NULL!! return");
return 0;
}
return (msg_queue->idx_w >= msg_queue->idx_r)
? (msg_queue->idx_w - msg_queue->idx_r)
: ((msg_queue->k_element_size - msg_queue->idx_r) +
msg_queue->idx_w);
}
inline int push_msg(struct msg_queue_t *msg_queue, struct ipi_msg_t *p_ipi_msg)
{
int idx_msg = -1;
/* error handling */
if (msg_queue == NULL) {
pr_info("msg_queue == NULL!! return");
return -1;
}
if (p_ipi_msg == NULL) {
pr_notice("p_ipi_msg = NULL, return");
return -1;
}
/* check queue full */
if (check_queue_to_be_full(msg_queue) == true) {
pr_info("task: %d, queue overflow, idx_w = %d, idx_r = %d",
p_ipi_msg->task_scene,
msg_queue->idx_w,
msg_queue->idx_r);
DUMP_IPI_MSG("drop msg", p_ipi_msg);
return -1;
}
/* push */
msg_queue->element[msg_queue->idx_w].msg = p_ipi_msg;
idx_msg = msg_queue->idx_w;
msg_queue->idx_w++;
if (msg_queue->idx_w >= msg_queue->k_element_size &&
msg_queue->k_element_size != 0)
msg_queue->idx_w %= msg_queue->k_element_size;
AUD_LOG_V(
"task %d, push msg: 0x%x, idx_msg = %d, idx_r = %d, idx_w = %d",
p_ipi_msg->task_scene, p_ipi_msg->msg_id,
idx_msg, msg_queue->idx_r, msg_queue->idx_w);
AUD_LOG_V(
"=> queue status(%d/%d)",
get_num_messages_in_queue(msg_queue),
msg_queue->k_element_size);
return idx_msg;
}
inline int pop_msg(struct msg_queue_t *msg_queue, struct ipi_msg_t **pp_ipi_msg)
{
/* error handling */
if (msg_queue == NULL) {
pr_info("msg_queue == NULL!! return");
return -1;
}
if (pp_ipi_msg == NULL) {
pr_info("pp_ipi_msg == NULL!! return");
return -1;
}
/* check queue empty */
if (check_queue_empty(msg_queue) == true) {
pr_info("task: %d, queue is empty, idx_r = %d",
msg_queue->task_scene, msg_queue->idx_r);
return -1;
}
/* pop */
*pp_ipi_msg = msg_queue->element[msg_queue->idx_r].msg;
msg_queue->idx_r++;
if (msg_queue->idx_r >= msg_queue->k_element_size &&
msg_queue->k_element_size != 0)
msg_queue->idx_r %= msg_queue->k_element_size;
if (*pp_ipi_msg == NULL) {
pr_notice("p_ipi_msg = NULL, return");
return -1;
}
AUD_LOG_V("task %d, pop msg: 0x%x, idx_r = %d, idx_w = %d",
(*pp_ipi_msg)->task_scene, (*pp_ipi_msg)->msg_id,
msg_queue->idx_r, msg_queue->idx_w);
AUD_LOG_V("=> queue status(%d/%d)",
get_num_messages_in_queue(msg_queue),
msg_queue->k_element_size);
return msg_queue->idx_r;
}
inline bool check_idx_msg_valid(struct msg_queue_t *msg_queue, int idx_msg)
{
return (idx_msg >= 0 &&
idx_msg < msg_queue->k_element_size)
? true : false;
}
inline bool check_ack_msg_valid(
const struct ipi_msg_t *p_ipi_msg,
const struct ipi_msg_t *p_ipi_msg_ack)
{
return (p_ipi_msg->task_scene == p_ipi_msg_ack->task_scene &&
p_ipi_msg->msg_id == p_ipi_msg_ack->msg_id) ? true : false;
}
@@ -0,0 +1,56 @@
// SPDX-License-Identifier: GPL-2.0
//
// audio_ipi_queue.h
//
// Copyright (c) 2018 MediaTek Inc.
#ifndef AUDIO_IPI_MSG_QUEUE_H
#define AUDIO_IPI_MSG_QUEUE_H
#include <linux/types.h>
/*
* =============================================================================
* ref struct
* =============================================================================
*/
struct ipi_msg_t;
/*
* =============================================================================
* struct definition
* =============================================================================
*/
struct ipi_queue_handler_t {
/* set void to prevent get/set attributes from outside */
void *msg_queue; /* struct msg_queue_t */
};
/*
* =============================================================================
* public function
* =============================================================================
*/
struct ipi_queue_handler_t *create_ipi_queue_handler(const uint8_t task_scene);
void destroy_ipi_queue_handler(struct ipi_queue_handler_t *handler);
struct ipi_queue_handler_t *get_ipi_queue_handler(const uint8_t task_scene);
void disable_ipi_queue_handler(struct ipi_queue_handler_t *handler);
int flush_ipi_queue_handler(struct ipi_queue_handler_t *handler);
int send_message(struct ipi_queue_handler_t *handler,
struct ipi_msg_t *p_ipi_msg);
int send_message_ack(struct ipi_queue_handler_t *handler,
struct ipi_msg_t *p_ipi_msg_ack);
#endif /* end of AUDIO_IPI_MSG_QUEUE_H */
@@ -0,0 +1,580 @@
// SPDX-License-Identifier: GPL-2.0
//
// adsp_messenge_ipi.c
//
// Copyright (c) 2018 MediaTek Inc.
#include "audio_messenger_ipi.h"
#include <linux/string.h>
#include <linux/spinlock.h>
#include <linux/errno.h>
#ifdef CONFIG_MTK_AUDIO_CM4_SUPPORT
#include <scp.h>
#include <scp_ipi.h>
#endif
#ifdef CONFIG_MTK_AUDIODSP_SUPPORT
#include <adsp_ipi.h>
#include <adsp_helper.h>
#endif
#include <adsp_ipi_queue.h>
#include "audio_log.h"
#include "audio_assert.h"
#include "audio_task.h"
#include "audio_ipi_queue.h"
#include "audio_ipi_platform.h"
#ifdef CONFIG_SND_SOC_MTK_AUDIO_DSP
#include <audio_playback_msg_id.h>
#endif
/*
* =============================================================================
* log
* =============================================================================
*/
#ifdef pr_fmt
#undef pr_fmt
#endif
#define pr_fmt(fmt) "[IPI][MSG] %s(), " fmt "\n", __func__
/*
* =============================================================================
* MACRO
* =============================================================================
*/
#ifdef ipi_dbg
#undef ipi_dbg
#endif
#if 0
#define ipi_dbg(x...) pr_info(x)
#else
#define ipi_dbg(x...)
#endif
/*
* =============================================================================
* private global members
* =============================================================================
*/
recv_message_t recv_message_array[TASK_SCENE_SIZE];
/*
* =============================================================================
* private functions - declaration
* =============================================================================
*/
/* queue related */
static void audio_ipi_msg_dispatcher(int id, void *data, unsigned int len);
static uint16_t current_idx;
/*
* =============================================================================
* utilities
* =============================================================================
*/
uint16_t get_message_buf_size(const struct ipi_msg_t *p_ipi_msg)
{
if (p_ipi_msg->data_type == AUDIO_IPI_MSG_ONLY)
return IPI_MSG_HEADER_SIZE;
else if (p_ipi_msg->data_type == AUDIO_IPI_PAYLOAD)
return (IPI_MSG_HEADER_SIZE + p_ipi_msg->payload_size);
else if (p_ipi_msg->data_type == AUDIO_IPI_DMA)
return (IPI_MSG_HEADER_SIZE + IPI_MSG_DMA_INFO_SIZE);
else
return 0;
}
int check_msg_format(const struct ipi_msg_t *p_ipi_msg, unsigned int len)
{
if (p_ipi_msg->magic != IPI_MSG_MAGIC_NUMBER) {
pr_notice("magic 0x%x error!!", p_ipi_msg->magic);
return -1;
}
if (p_ipi_msg->task_scene >= TASK_SCENE_SIZE) {
pr_notice("task_scene %d error!!", p_ipi_msg->task_scene);
return -1;
}
if (p_ipi_msg->source_layer >= AUDIO_IPI_LAYER_FROM_SIZE) {
pr_notice("source_layer %d error!!", p_ipi_msg->source_layer);
return -1;
}
if (p_ipi_msg->target_layer >= AUDIO_IPI_LAYER_TO_SIZE) {
pr_notice("target_layer %d error!!", p_ipi_msg->target_layer);
return -1;
}
if (p_ipi_msg->data_type >= AUDIO_IPI_TYPE_SIZE) {
pr_notice("data_type %d error!!", p_ipi_msg->data_type);
return -1;
}
if (p_ipi_msg->ack_type > AUDIO_IPI_MSG_DIRECT_SEND &&
p_ipi_msg->ack_type != AUDIO_IPI_MSG_CANCELED) {
pr_notice("ack_type %d error!!", p_ipi_msg->ack_type);
return -1;
}
if (get_message_buf_size(p_ipi_msg) > len) {
pr_notice("len 0x%x error!!", len);
return -1;
}
if (p_ipi_msg->data_type == AUDIO_IPI_PAYLOAD) {
if (p_ipi_msg->payload_size == 0 ||
p_ipi_msg->payload_size > MAX_IPI_MSG_PAYLOAD_SIZE) {
DUMP_IPI_MSG("payload_size error!!", p_ipi_msg);
return -1;
}
}
if (p_ipi_msg->data_type == AUDIO_IPI_DMA) {
if (p_ipi_msg->dma_addr == NULL) {
DUMP_IPI_MSG("dma addr null!!", p_ipi_msg);
return -1;
}
if (p_ipi_msg->dma_info.data_size == 0 &&
p_ipi_msg->dma_info.hal_buf.data_size == 0) {
DUMP_IPI_MSG("dma data_size error!!", p_ipi_msg);
return -1;
}
}
return 0;
}
bool check_print_msg_info(const struct ipi_msg_t *p_ipi_msg)
{
if (p_ipi_msg == NULL)
return false;
#ifdef CONFIG_SND_SOC_MTK_AUDIO_DSP
if (p_ipi_msg->msg_id == AUDIO_DSP_TASK_OPEN ||
p_ipi_msg->msg_id == AUDIO_DSP_TASK_CLOSE ||
p_ipi_msg->msg_id == AUDIO_DSP_TASK_HWPARAM ||
p_ipi_msg->msg_id == AUDIO_DSP_TASK_PCM_HWPARAM ||
p_ipi_msg->msg_id == AUDIO_DSP_TASK_HWFREE ||
p_ipi_msg->msg_id == AUDIO_DSP_TASK_PCM_HWFREE ||
p_ipi_msg->msg_id == AUDIO_DSP_TASK_PREPARE ||
p_ipi_msg->msg_id == AUDIO_DSP_TASK_PCM_PREPARE ||
p_ipi_msg->msg_id == AUDIO_DSP_TASK_START ||
p_ipi_msg->msg_id == AUDIO_DSP_TASK_STOP)
return false;
if (p_ipi_msg->task_scene == TASK_SCENE_PRIMARY &&
p_ipi_msg->msg_id == AUDIO_DSP_TASK_DLCOPY)
return false;
if (p_ipi_msg->task_scene == TASK_SCENE_DEEPBUFFER &&
p_ipi_msg->msg_id == AUDIO_DSP_TASK_DLCOPY)
return false;
if (p_ipi_msg->task_scene == TASK_SCENE_VOIP &&
p_ipi_msg->msg_id == AUDIO_DSP_TASK_DLCOPY)
return false;
if (p_ipi_msg->task_scene == TASK_SCENE_CAPTURE_UL1 &&
p_ipi_msg->msg_id == AUDIO_DSP_TASK_ULCOPY)
return false;
if (p_ipi_msg->task_scene == TASK_SCENE_CAPTURE_RAW &&
p_ipi_msg->msg_id == AUDIO_DSP_TASK_ULCOPY)
return false;
if (p_ipi_msg->task_scene == TASK_SCENE_FAST &&
p_ipi_msg->msg_id == AUDIO_DSP_TASK_DLCOPY)
return false;
#endif
if (p_ipi_msg->ack_type == AUDIO_IPI_MSG_NEED_ACK ||
p_ipi_msg->ack_type == AUDIO_IPI_MSG_ACK_BACK)
return true;
return false;
}
/*
* =============================================================================
* private functions - implementation
* =============================================================================
*/
static void audio_ipi_msg_dispatcher(int id, void *data, unsigned int len)
{
struct ipi_msg_t *p_ipi_msg = NULL;
if (data == NULL)
return;
if (len < IPI_MSG_HEADER_SIZE || len > MAX_IPI_MSG_BUF_SIZE)
return;
p_ipi_msg = (struct ipi_msg_t *)data;
if (check_msg_format(p_ipi_msg, len) != 0)
return;
if (p_ipi_msg->ack_type == AUDIO_IPI_MSG_ACK_BACK) {
send_message_ack(get_ipi_queue_handler(p_ipi_msg->task_scene),
p_ipi_msg);
} else if (p_ipi_msg->data_type == AUDIO_IPI_DMA &&
p_ipi_msg->target_layer == AUDIO_IPI_LAYER_TO_HAL)
audio_ipi_dma_msg_to_hal(p_ipi_msg);
else {
if (recv_message_array[p_ipi_msg->task_scene] != NULL)
recv_message_array[p_ipi_msg->task_scene](p_ipi_msg);
}
}
/*
* =============================================================================
* public functions - implementation
* =============================================================================
*/
void audio_messenger_ipi_init(void)
{
int i = 0;
#ifdef CONFIG_MTK_AUDIO_CM4_SUPPORT
int ret_scp = 0;
#endif
#ifdef CONFIG_MTK_AUDIODSP_SUPPORT
int ret_adsp = 0;
#endif
current_idx = 0;
#ifdef CONFIG_MTK_AUDIO_CM4_SUPPORT
ret_scp = scp_ipi_registration(
IPI_AUDIO,
audio_ipi_msg_dispatcher,
"audio");
if (ret_scp != 0)
pr_notice("scp_ipi_registration fail!!");
#endif
#ifdef CONFIG_MTK_AUDIODSP_SUPPORT
ret_adsp = adsp_ipi_registration(
ADSP_IPI_AUDIO,
audio_ipi_msg_dispatcher,
"audio");
if (ret_adsp != 0)
pr_notice("adsp_ipi_registration fail!!");
#endif
for (i = 0; i < TASK_SCENE_SIZE; i++)
recv_message_array[i] = NULL;
}
void audio_reg_recv_message(uint8_t task_scene, recv_message_t recv_message)
{
if (task_scene >= TASK_SCENE_SIZE) {
pr_info("not support task_scene %d!!", task_scene);
return;
}
recv_message_array[task_scene] = recv_message;
}
int audio_send_ipi_msg(
struct ipi_msg_t *p_ipi_msg,
uint8_t task_scene, /* task_scene_t */
uint8_t target_layer, /* audio_ipi_msg_target_layer_t */
uint8_t data_type, /* audio_ipi_msg_data_t */
uint8_t ack_type, /* audio_ipi_msg_ack_t */
uint16_t msg_id,
uint32_t param1, /* data_size for payload & dma */
uint32_t param2,
void *data_buffer) /* buffer for payload & dma */
{
struct ipi_queue_handler_t *handler = NULL;
struct ipi_msg_dma_info_t *dma_info = NULL;
uint32_t ipi_msg_len = 0;
int ret = 0;
if (p_ipi_msg == NULL) {
pr_notice("p_ipi_msg = NULL, return");
return -1;
}
if (target_layer != AUDIO_IPI_LAYER_TO_DSP) {
pr_notice("target_layer %d in kernel", target_layer);
return -1;
}
memset(p_ipi_msg, 0, MAX_IPI_MSG_BUF_SIZE);
p_ipi_msg->magic = IPI_MSG_MAGIC_NUMBER;
p_ipi_msg->task_scene = task_scene;
p_ipi_msg->source_layer = AUDIO_IPI_LAYER_FROM_KERNEL;
p_ipi_msg->target_layer = target_layer;
p_ipi_msg->data_type = data_type;
p_ipi_msg->ack_type = ack_type;
p_ipi_msg->msg_id = msg_id;
p_ipi_msg->param1 = param1;
p_ipi_msg->param2 = param2;
if (p_ipi_msg->data_type == AUDIO_IPI_PAYLOAD) {
if (data_buffer == NULL) {
pr_notice("payload data_buffer NULL, return");
return -1;
}
if (p_ipi_msg->payload_size > MAX_IPI_MSG_PAYLOAD_SIZE) {
pr_notice("payload_size %u error!!",
p_ipi_msg->payload_size);
return -1;
}
memcpy(p_ipi_msg->payload,
data_buffer,
p_ipi_msg->payload_size);
} else if (p_ipi_msg->data_type == AUDIO_IPI_DMA) {
if (data_buffer == NULL) {
pr_notice("dma data_buffer NULL, return");
return -1;
}
p_ipi_msg->dma_addr = (char *)data_buffer;
if (param1 > 1) {
dma_info = &p_ipi_msg->dma_info;
dma_info->data_size = param1;
ret = audio_ipi_dma_write_region(
p_ipi_msg->task_scene,
data_buffer,
dma_info->data_size,
&dma_info->rw_idx);
}
}
if (ret != 0) {
DUMP_IPI_MSG("dma fail!!", p_ipi_msg);
return ret;
}
ipi_msg_len = get_message_buf_size(p_ipi_msg);
if (check_msg_format(p_ipi_msg, ipi_msg_len) != 0) {
pr_info("drop msg due to ipi fmt err");
return -1;
}
handler = get_ipi_queue_handler(p_ipi_msg->task_scene);
if (handler == NULL) {
pr_notice("handler = NULL, return");
return -1;
}
return send_message(handler, p_ipi_msg);
}
int audio_send_ipi_filled_msg(struct ipi_msg_t *p_ipi_msg)
{
struct ipi_queue_handler_t *handler = NULL;
if (p_ipi_msg == NULL) {
pr_notice("p_ipi_msg = NULL, return");
return -1;
}
if (check_msg_format(p_ipi_msg, get_message_buf_size(p_ipi_msg)) != 0) {
pr_info("drop msg due to ipi fmt err");
return -1;
}
handler = get_ipi_queue_handler(p_ipi_msg->task_scene);
if (handler == NULL) {
AUD_LOG_E("handler = NULL, return");
return -1;
}
return send_message(handler, p_ipi_msg);
}
int send_message_to_scp(const struct ipi_msg_t *p_ipi_msg)
{
int send_status = 0;
uint32_t wait_ms = 0;
uint32_t dsp_id = 0;
uint32_t ipi_id = 0;
/* error handling */
if (p_ipi_msg == NULL) {
pr_notice("p_ipi_msg = NULL, return");
return -1;
}
/* wait until IPC done */
wait_ms = (p_ipi_msg->ack_type == AUDIO_IPI_MSG_DIRECT_SEND)
? 0
: ADSP_IPI_QUEUE_DEFAULT_WAIT_MS;
dsp_id = audio_get_dsp_id(p_ipi_msg->task_scene);
ipi_id = audio_get_ipi_id(p_ipi_msg->task_scene);
send_status = scp_send_msg_to_queue(
dsp_id,
ipi_id,
(void *)p_ipi_msg,
get_message_buf_size(p_ipi_msg),
wait_ms);
if (send_status != 0) {
pr_notice("scp_ipi_send error %d", send_status);
DUMP_IPI_MSG("fail", p_ipi_msg);
}
return send_status;
}
int audio_send_ipi_buf_to_dsp(
struct ipi_msg_t *p_ipi_msg,
uint8_t task_scene, /* task_scene_t */
uint16_t msg_id,
void *data_buffer,
uint32_t data_size)
{
return audio_send_ipi_msg(
p_ipi_msg,
task_scene,
AUDIO_IPI_LAYER_TO_DSP,
AUDIO_IPI_DMA,
AUDIO_IPI_MSG_NEED_ACK,
msg_id,
data_size,
0,
data_buffer);
}
int audio_recv_ipi_buf_from_dsp(
struct ipi_msg_t *p_ipi_msg,
uint8_t task_scene,
uint16_t msg_id,
void *data_buffer,
uint32_t max_data_size,
uint32_t *data_size)
{
phys_addr_t share_buf_phy = 0;
void *share_buf_virt = NULL;
struct aud_data_t *share_buf_info = NULL;
int ret = 0;
if (task_scene >= TASK_SCENE_SIZE) {
pr_notice("task_scene %u err!!", task_scene);
return -EINVAL;
}
if (p_ipi_msg == NULL) {
pr_notice("p_ipi_msg = NULL!!");
return -EINVAL;
}
if (data_buffer == NULL || max_data_size == 0) {
pr_notice("ptr %p size %u err!!", data_buffer, max_data_size);
return -EINVAL;
}
if (data_size == NULL) {
pr_notice("data_size = NULL!!");
return -EINVAL;
}
memset(p_ipi_msg, 0, MAX_IPI_MSG_BUF_SIZE);
p_ipi_msg->magic = IPI_MSG_MAGIC_NUMBER;
p_ipi_msg->task_scene = task_scene;
p_ipi_msg->source_layer = AUDIO_IPI_LAYER_FROM_KERNEL;
p_ipi_msg->target_layer = AUDIO_IPI_LAYER_TO_DSP;
p_ipi_msg->data_type = AUDIO_IPI_PAYLOAD;
p_ipi_msg->ack_type = AUDIO_IPI_MSG_NEED_ACK;
p_ipi_msg->msg_id = msg_id;
p_ipi_msg->payload_size = sizeof(struct aud_data_t);
/* alloc shared DRAM & put the addr info into payload */
if (p_ipi_msg->payload_size > MAX_IPI_MSG_PAYLOAD_SIZE) {
pr_notice("payload_size %u, max sz %u not enough",
p_ipi_msg->payload_size, MAX_IPI_MSG_PAYLOAD_SIZE);
goto RECV_BUF_EXIT;
}
ret = audio_ipi_dma_alloc(
task_scene,
&share_buf_phy,
&share_buf_virt,
max_data_size);
if (ret != 0 || share_buf_phy == 0 || share_buf_virt == NULL) {
pr_notice("dma_alloc fail!!");
goto RECV_BUF_EXIT;
}
share_buf_info = (struct aud_data_t *)p_ipi_msg->payload;
share_buf_info->memory_size = max_data_size;
share_buf_info->data_size = 0;
share_buf_info->addr_val = share_buf_phy;
/* sent message to dsp */
ret = audio_send_ipi_filled_msg(p_ipi_msg);
if (ret != 0) {
pr_notice("audio_send_ipi_filled_msg error!!");
goto RECV_BUF_EXIT;
}
/* copy shared data to user buffer */
if (share_buf_info->data_size > max_data_size) {
pr_notice("share_buf_info->data_size %u > max_data_size %u!!",
share_buf_info->data_size,
max_data_size);
ret = -1;
goto RECV_BUF_EXIT;
}
if (share_buf_info->data_size == 0) {
pr_notice("share_buf_info->data_size = 0!! check adsp write");
*data_size = 0;
goto RECV_BUF_EXIT;
}
memcpy(data_buffer, share_buf_virt, share_buf_info->data_size);
*data_size = share_buf_info->data_size;
RECV_BUF_EXIT:
audio_ipi_dma_free(task_scene, share_buf_phy, max_data_size);
return ret;
}
@@ -0,0 +1,251 @@
// SPDX-License-Identifier: GPL-2.0
//
// audio_messenger_ipi.h
//
// Copyright (c) 2018 MediaTek Inc.
#ifndef AUDIO_MESSENGER_IPI_H
#define AUDIO_MESSENGER_IPI_H
#include <linux/types.h>
#include <audio_ipi_dma.h>
/*
* =============================================================================
* MACRO
* =============================================================================
*/
#define MAX_IPI_MSG_BUF_SIZE (112) /* SHARE_BUF_SIZE - 16 */
#define IPI_MSG_HEADER_SIZE (16)
#define MAX_IPI_MSG_PAYLOAD_SIZE (MAX_IPI_MSG_BUF_SIZE - IPI_MSG_HEADER_SIZE)
#define MAX_DSP_DMA_WRITE_SIZE (0x20000)
#define IPI_MSG_MAGIC_NUMBER (0x8888)
/*
* =============================================================================
* typedef
* =============================================================================
*/
enum { /* audio_ipi_msg_source_layer_t */
AUDIO_IPI_LAYER_FROM_HAL,
AUDIO_IPI_LAYER_FROM_KERNEL,
AUDIO_IPI_LAYER_FROM_DSP,
AUDIO_IPI_LAYER_FROM_SIZE,
AUDIO_IPI_LAYER_FROM_MAX = 15 /* 4-bit only */
};
enum { /* audio_ipi_msg_target_layer_t */
AUDIO_IPI_LAYER_TO_HAL,
AUDIO_IPI_LAYER_TO_KERNEL,
AUDIO_IPI_LAYER_TO_DSP,
AUDIO_IPI_LAYER_TO_SIZE,
AUDIO_IPI_LAYER_TO_MAX = 15 /* 4-bit only */
};
enum { /* audio_ipi_msg_data_t */
AUDIO_IPI_MSG_ONLY,
AUDIO_IPI_PAYLOAD,
AUDIO_IPI_DMA,
AUDIO_IPI_TYPE_SIZE
};
enum { /* audio_ipi_msg_ack_t */
/* bypass ack, but still send to audio queue */
AUDIO_IPI_MSG_BYPASS_ACK = 0,
/* need ack, and block in audio queue until ack back */
AUDIO_IPI_MSG_NEED_ACK = 1,
AUDIO_IPI_MSG_ACK_BACK = 2,
/* bypass audio queue, but still send to ipc queue */
AUDIO_IPI_MSG_DIRECT_SEND = 3,
AUDIO_IPI_MSG_CANCELED = 8
};
/*
* =============================================================================
* struct definition
* =============================================================================
*/
struct aud_data_t {
uint32_t memory_size; /* buffer size (memory) */
uint32_t data_size; /* 0 <= data_size <= memory_size */
union {
void *addr; /* memory address */
uint64_t addr_val; /* the value of address */
uint32_t dummy[2]; /* work between 32/64 bit environment */
};
};
struct ipi_msg_dma_info_t {
struct aud_data_t reserve; /* TODO: remove later */
struct aud_data_t hal_buf; /* source data buffer */
uint32_t rw_idx; /* region r/w index */
uint32_t data_size; /* region data size */
struct aud_data_t wb_dram; /* allow target to write data back */
};
#define IPI_MSG_DMA_INFO_SIZE (sizeof(struct ipi_msg_dma_info_t))
struct ipi_msg_t {
/* header: 16 bytes */
uint16_t magic; /* IPI_MSG_MAGIC_NUMBER */
uint8_t task_scene; /* see task_scene_t */
uint8_t source_layer: 4; /* see audio_ipi_msg_source_layer_t */
uint8_t target_layer: 4; /* see audio_ipi_msg_target_layer_t */
uint8_t data_type; /* see audio_ipi_msg_data_t */
uint8_t ack_type; /* see audio_ipi_msg_ack_t */
uint16_t msg_id; /* defined by user */
union {
uint32_t param1;
uint32_t payload_size; /* payload */
uint32_t scp_ret;
};
uint32_t param2;
/* data: 256 bytes */
union {
char payload[MAX_IPI_MSG_PAYLOAD_SIZE]; /* payload only */
struct ipi_msg_dma_info_t dma_info; /* dma only */
char *dma_addr; /* TODO: remove later */
};
};
/*
* =============================================================================
* hook function
* =============================================================================
*/
typedef void (*recv_message_t)(struct ipi_msg_t *p_ipi_msg);
/*
* =============================================================================
* public function
* =============================================================================
*/
uint16_t get_message_buf_size(const struct ipi_msg_t *p_ipi_msg);
int check_msg_format(const struct ipi_msg_t *p_ipi_msg, unsigned int len);
bool check_print_msg_info(const struct ipi_msg_t *p_ipi_msg);
#define DUMP_IPI_MSG(description, p_ipi_msg) \
do { \
if (description == NULL || (p_ipi_msg) == NULL) \
break; \
if ((p_ipi_msg)->magic != IPI_MSG_MAGIC_NUMBER) { \
pr_info("%s, but magic 0x%x fail", \
description, \
(unsigned int)(p_ipi_msg)->magic); \
break; \
} \
if ((p_ipi_msg)->data_type == AUDIO_IPI_MSG_ONLY) { \
pr_info("%s, task: %d, msg_id: 0x%x, ack_type: %d, " \
"p1: 0x%x, p2: 0x%x", \
description, \
(p_ipi_msg)->task_scene, \
(unsigned int)(p_ipi_msg)->msg_id, \
(p_ipi_msg)->ack_type, \
(unsigned int)(p_ipi_msg)->param1, \
(unsigned int)(p_ipi_msg)->param2); \
} else if ((p_ipi_msg)->data_type == AUDIO_IPI_PAYLOAD) { \
pr_info("%s, task: %d, msg_id: 0x%x, ack_type: %d, " \
"payload_size: 0x%x, p2: 0x%x", \
description, \
(p_ipi_msg)->task_scene, \
(unsigned int)(p_ipi_msg)->msg_id, \
(p_ipi_msg)->ack_type, \
(unsigned int)(p_ipi_msg)->payload_size, \
(unsigned int)(p_ipi_msg)->param2); \
} else if ((p_ipi_msg)->data_type == AUDIO_IPI_DMA) { \
pr_info("%s, task: %d, msg_id: 0x%x, ack_type: %d, " \
"p1: 0x%x, p2: 0x%x, dma sz: %u, idx: %u, " \
"hal sz: %u, wb sz: %u", \
description, \
(p_ipi_msg)->task_scene, \
(unsigned int)(p_ipi_msg)->msg_id, \
(p_ipi_msg)->ack_type, \
(unsigned int)(p_ipi_msg)->param1, \
(unsigned int)(p_ipi_msg)->param2, \
(p_ipi_msg)->dma_info.data_size, \
(p_ipi_msg)->dma_info.rw_idx, \
(p_ipi_msg)->dma_info.hal_buf.data_size, \
(p_ipi_msg)->dma_info.wb_dram.data_size); \
} \
} while (0)
/*
* =============================================================================
* public functions - declaration
* =============================================================================
*/
void audio_messenger_ipi_init(void);
void audio_reg_recv_message(uint8_t task_scene, recv_message_t recv_message);
int audio_send_ipi_msg(
struct ipi_msg_t *p_ipi_msg,
uint8_t task_scene, /* task_scene_t */
uint8_t target_layer, /* audio_ipi_msg_target_layer_t */
uint8_t data_type, /* audio_ipi_msg_data_t */
uint8_t ack_type, /* audio_ipi_msg_ack_t */
uint16_t msg_id,
uint32_t param1, /* data_size for payload & dma */
uint32_t param2,
void *data_buffer); /* buffer for payload & dma */
int audio_send_ipi_filled_msg(struct ipi_msg_t *p_ipi_msg);
int send_message_to_scp(const struct ipi_msg_t *p_ipi_msg);
int audio_send_ipi_buf_to_dsp(
struct ipi_msg_t *p_ipi_msg,
uint8_t task_scene, /* task_scene_t */
uint16_t msg_id,
void *data_buffer,
uint32_t data_size);
int audio_recv_ipi_buf_from_dsp(
struct ipi_msg_t *p_ipi_msg,
uint8_t task_scene,
uint16_t msg_id,
void *data_buffer,
uint32_t max_data_size,
uint32_t *data_size);
#endif /* end of AUDIO_MESSENGER_IPI_H */
@@ -0,0 +1,610 @@
// SPDX-License-Identifier: GPL-2.0
//
// audio_ringbuf.c
//
// Copyright (c) 2018 MediaTek Inc.
#include <audio_ringbuf.h>
#include <linux/string.h>
#include <linux/vmalloc.h>
#include <audio_assert.h>
#ifdef LOG_TAG
#undef LOG_TAG
#endif
#define LOG_TAG "audio_ringbuf"
#define MAX_SIZE_OF_ONE_FRAME (16) /* 32-bits * 4ch */
#define DUMP_RINGBUF(LOG_F, description, rb, count) \
do { \
if (rb && description) { \
LOG_F("%s(), %s, base %p, read %p, write %p, size %u" \
", data %u, free %u, count %u\n", \
__func__, \
description, \
(rb)->base, \
(rb)->read, \
(rb)->write, \
(rb)->size, \
audio_ringbuf_count(rb), \
audio_ringbuf_free_space(rb), \
count); \
} else { \
pr_notice("%s(), %uL, %p %p\n", \
__func__, __LINE__, rb, description); \
} \
} while (0)
uint32_t audio_ringbuf_count(const struct audio_ringbuf_t *rb)
{
uint32_t count = 0;
uint32_t w2r = 0;
if (!rb) {
AUD_WARNING("null");
return 0;
}
if (!rb->base || !rb->size)
return 0;
if (rb->write >= rb->read)
count = rb->write - rb->read;
else {
w2r = rb->read - rb->write;
if (rb->size > w2r)
count = rb->size - w2r;
else
count = 0;
}
return count;
}
uint32_t audio_ringbuf_free_space(const struct audio_ringbuf_t *rb)
{
uint32_t count = 0;
uint32_t free_spece = 0;
if (!rb) {
AUD_WARNING("null");
return 0;
}
if (!rb->base || !rb->size)
return 0;
count = audio_ringbuf_count(rb);
free_spece = (rb->size > count) ? (rb->size - count) : 0;
/* avoid to copy data s.t. read == write */
if (free_spece > MAX_SIZE_OF_ONE_FRAME)
free_spece -= MAX_SIZE_OF_ONE_FRAME;
else
free_spece = 0;
return free_spece;
}
void audio_ringbuf_copy_to_linear(
char *linear,
struct audio_ringbuf_t *rb,
uint32_t count)
{
char *end = NULL;
uint32_t r2e = 0;
if (!count)
return;
if (!linear || !rb) {
AUD_WARNING("null");
return;
}
if (!rb->base || !rb->size) {
DUMP_RINGBUF(pr_notice, "no init", rb, count);
AUD_WARNING("no init");
return;
}
if (count > audio_ringbuf_count(rb)) {
DUMP_RINGBUF(pr_notice, "underflow", rb, count);
AUD_WARNING("underflow");
return;
}
if (rb->read <= rb->write) {
memcpy(linear, rb->read, count);
rb->read += count;
} else {
end = rb->base + rb->size;
r2e = end - rb->read;
if (count <= r2e) {
memcpy(linear, rb->read, count);
rb->read += count;
if (rb->read == end)
rb->read = rb->base;
} else {
memcpy(linear, rb->read, r2e);
memcpy(linear + r2e, rb->base, count - r2e);
rb->read = rb->base + count - r2e;
}
}
}
void audio_ringbuf_copy_from_linear_impl(
struct audio_ringbuf_t *rb,
const char *linear,
uint32_t count)
{
char *end = NULL;
uint32_t w2e = 0;
if (!count)
return;
if (!rb || !linear) {
AUD_WARNING("null");
return;
}
if (!rb->base || !rb->size) {
DUMP_RINGBUF(pr_notice, "no init", rb, count);
AUD_WARNING("no init");
return;
}
if (count > audio_ringbuf_free_space(rb)) {
DUMP_RINGBUF(pr_notice, "overflow", rb, count);
AUD_WARNING("overflow");
return;
}
end = rb->base + rb->size;
if (rb->read <= rb->write) {
w2e = end - rb->write;
if (count <= w2e) {
memcpy(rb->write, linear, count);
rb->write += count;
if (rb->write == end)
rb->write = rb->base;
} else {
memcpy(rb->write, linear, w2e);
memcpy(rb->base, linear + w2e, count - w2e);
rb->write = rb->base + count - w2e;
}
} else {
memcpy(rb->write, linear, count);
rb->write += count;
}
}
void audio_ringbuf_copy_from_linear(
struct audio_ringbuf_t *rb,
const char *linear,
uint32_t count)
{
if (!count)
return;
if (!rb || !linear) {
AUD_WARNING("null");
return;
}
dynamic_change_ring_buf_size(rb, count);
audio_ringbuf_copy_from_linear_impl(rb, linear, count);
}
void audio_ringbuf_copy_from_ringbuf_impl(
struct audio_ringbuf_t *rb_des,
struct audio_ringbuf_t *rb_src,
uint32_t count)
{
char *end = NULL;
uint32_t r2e = 0;
if (!count)
return;
if (!rb_des || !rb_src) {
AUD_WARNING("null");
return;
}
if (!rb_src->base || !rb_src->size) {
DUMP_RINGBUF(pr_notice, "no init", rb_src, count);
AUD_WARNING("no init");
return;
}
if (count > audio_ringbuf_count(rb_src)) {
DUMP_RINGBUF(pr_notice, "underflow", rb_src, count);
AUD_WARNING("underflow");
return;
}
if (count > audio_ringbuf_free_space(rb_des)) {
DUMP_RINGBUF(pr_notice, "overflow", rb_des, count);
AUD_WARNING("overflow");
return;
}
if (rb_src->read <= rb_src->write) {
audio_ringbuf_copy_from_linear_impl(
rb_des,
rb_src->read,
count);
rb_src->read += count;
} else {
end = rb_src->base + rb_src->size;
r2e = end - rb_src->read;
if (r2e >= count) {
audio_ringbuf_copy_from_linear_impl(
rb_des,
rb_src->read,
count);
rb_src->read += count;
if (rb_src->read == end)
rb_src->read = rb_src->base;
} else {
audio_ringbuf_copy_from_linear_impl(
rb_des,
rb_src->read,
r2e);
audio_ringbuf_copy_from_linear_impl(
rb_des,
rb_src->base,
count - r2e);
rb_src->read = rb_src->base + count - r2e;
}
}
}
void audio_ringbuf_copy_from_ringbuf(
struct audio_ringbuf_t *rb_des,
struct audio_ringbuf_t *rb_src,
uint32_t count)
{
if (!count)
return;
if (!rb_des || !rb_src) {
AUD_WARNING("null");
return;
}
if (!rb_src->base || !rb_src->size) {
DUMP_RINGBUF(pr_notice, "no init", rb_src, count);
AUD_WARNING("no init");
return;
}
if (count > audio_ringbuf_count(rb_src)) {
DUMP_RINGBUF(pr_notice, "underflow", rb_src, count);
AUD_WARNING("underflow");
return;
}
dynamic_change_ring_buf_size(rb_des, count);
audio_ringbuf_copy_from_ringbuf_impl(rb_des, rb_src, count);
}
void audio_ringbuf_copy_from_ringbuf_all(
struct audio_ringbuf_t *rb_des,
struct audio_ringbuf_t *rb_src)
{
if (!rb_des || !rb_src) {
AUD_WARNING("null");
return;
}
audio_ringbuf_copy_from_ringbuf(
rb_des,
rb_src,
audio_ringbuf_count(rb_src));
}
void audio_ringbuf_write_value(
struct audio_ringbuf_t *rb,
const uint8_t value,
const uint32_t count)
{
char *end = NULL;
uint32_t w2e = 0;
if (!count)
return;
if (!rb) {
AUD_WARNING("null");
return;
}
dynamic_change_ring_buf_size(rb, count);
if (!rb->base || !rb->size) {
DUMP_RINGBUF(pr_notice, "no init", rb, count);
AUD_WARNING("no init");
return;
}
if (count > audio_ringbuf_free_space(rb)) {
DUMP_RINGBUF(pr_notice, "overflow", rb, count);
AUD_WARNING("overflow");
return;
}
end = rb->base + rb->size;
if (rb->read <= rb->write) {
w2e = end - rb->write;
if (count <= w2e) {
memset(rb->write, value, count);
rb->write += count;
if (rb->write == end)
rb->write = rb->base;
} else {
memset(rb->write, value, w2e);
memset(rb->base, value, count - w2e);
rb->write = rb->base + count - w2e;
}
} else {
memset(rb->write, value, count);
rb->write += count;
}
}
void audio_ringbuf_write_zero(struct audio_ringbuf_t *rb, uint32_t count)
{
if (!count)
return;
if (!rb) {
AUD_WARNING("null");
return;
}
audio_ringbuf_write_value(rb, 0, count);
}
void audio_ringbuf_drop_data(struct audio_ringbuf_t *rb, const uint32_t count)
{
char *end = NULL;
uint32_t r2e = 0;
if (!count)
return;
if (!rb) {
AUD_WARNING("null");
return;
}
if (count > audio_ringbuf_count(rb)) {
DUMP_RINGBUF(pr_notice, "underflow", rb, count);
AUD_WARNING("underflow");
return;
}
if (rb->read <= rb->write)
rb->read += count;
else {
end = rb->base + rb->size;
r2e = end - rb->read;
if (count <= r2e) {
rb->read += count;
if (rb->read == end)
rb->read = rb->base;
} else
rb->read = rb->base + count - r2e;
}
}
void audio_ringbuf_drop_all(struct audio_ringbuf_t *rb)
{
if (!rb) {
AUD_WARNING("null");
return;
}
rb->read = 0;
rb->write = 0;
}
void audio_ringbuf_compensate_value_impl(
struct audio_ringbuf_t *rb,
const uint8_t value,
const uint32_t count)
{
char *end = NULL;
uint32_t b2r = 0;
uint32_t left_data = 0;
if (!count)
return;
if (!rb) {
AUD_WARNING("null");
return;
}
if (!rb->base || !rb->size) {
DUMP_RINGBUF(pr_notice, "no init", rb, count);
AUD_WARNING("no init");
return;
}
if (count > audio_ringbuf_free_space(rb)) {
DUMP_RINGBUF(pr_notice, "overflow", rb, count);
AUD_WARNING("overflow");
return;
}
end = rb->base + rb->size;
if (rb->read <= rb->write) {
b2r = rb->read - rb->base;
if (b2r >= count) {
rb->read -= count;
memset(rb->read, value, count);
} else {
if (b2r > 0) { /* in case read == base */
memset(rb->base, value, b2r);
left_data = count - b2r;
rb->read = end - left_data;
memset(rb->read, value, left_data);
} else {
rb->read = end - count;
memset(rb->read, value, count);
}
}
} else {
rb->read -= count;
memset(rb->read, value, count);
}
}
void audio_ringbuf_compensate_value(
struct audio_ringbuf_t *rb,
const uint8_t value,
const uint32_t count)
{
if (!count)
return;
if (!rb) {
AUD_WARNING("null");
return;
}
dynamic_change_ring_buf_size(rb, count);
audio_ringbuf_compensate_value_impl(rb, value, count);
}
void audio_ringbuf_rollback(struct audio_ringbuf_t *rb, const uint32_t count)
{
char *end = NULL;
uint32_t b2r = 0;
uint32_t left_data = 0;
if (!count)
return;
if (!rb) {
AUD_WARNING("null");
return;
}
if (count > audio_ringbuf_free_space(rb)) {
AUD_WARNING("overflow");
return;
}
b2r = rb->read - rb->base;
end = rb->base + rb->size;
if (rb->read <= rb->write) {
b2r = rb->read - rb->base;
if (b2r >= count)
rb->read -= count;
else {
if (b2r > 0) { /* in case read == base */
left_data = count - b2r;
rb->read = end - left_data;
} else
rb->read = end - count;
}
} else
rb->read -= count;
}
void dynamic_change_ring_buf_size(
struct audio_ringbuf_t *rb,
uint32_t write_size)
{
uint32_t data_count = 0;
uint32_t free_space = 0;
uint32_t change_size = 0;
struct audio_ringbuf_t new_ringbuf;
if (!rb) {
AUD_WARNING("null");
return;
}
if (!write_size)
return;
memset(&new_ringbuf, 0, sizeof(struct audio_ringbuf_t));
if (!rb->base || !rb->size) { /* init */
change_size = (2 * write_size);
change_size += MAX_SIZE_OF_ONE_FRAME;
rb->base = vmalloc(change_size);
rb->read = rb->base;
rb->write = rb->base;
rb->size = change_size;
} else { /* update size */
data_count = audio_ringbuf_count(rb);
free_space = audio_ringbuf_free_space(rb);
if (free_space < write_size) {
change_size = data_count + free_space;
while ((change_size - data_count) < write_size)
change_size *= 2;
} else if (free_space > (8 * (data_count + write_size))) {
change_size = data_count + free_space;
while ((change_size - data_count) >
(8 * (data_count + write_size)))
change_size /= 2;
}
if (change_size) {
change_size += MAX_SIZE_OF_ONE_FRAME;
pr_info("%s(), %p: %u -> %u, data_count %u, write_size %u, free_space %u\n",
__func__,
rb->base,
rb->size,
change_size,
data_count,
write_size,
free_space);
new_ringbuf.base = vmalloc(change_size);
new_ringbuf.read = new_ringbuf.base;
new_ringbuf.write = new_ringbuf.base;
new_ringbuf.size = change_size;
/* copy old data */
audio_ringbuf_copy_from_ringbuf_impl(
&new_ringbuf,
rb,
data_count);
/* delete old ringbuf */
vfree(rb->base);
/* update info */
rb->base = new_ringbuf.base;
rb->read = new_ringbuf.read;
rb->write = new_ringbuf.write;
rb->size = new_ringbuf.size;
memset(&new_ringbuf, 0, sizeof(struct audio_ringbuf_t));
}
}
}
@@ -0,0 +1,100 @@
// SPDX-License-Identifier: GPL-2.0
//
// adsp_ringbuf.h
//
// Copyright (c) 2018 MediaTek Inc.
#ifndef AUDIO_RING_BUF_H
#define AUDIO_RING_BUF_H
#include <linux/types.h>
#ifdef __cplusplus
extern "C" {
#endif
struct audio_ringbuf_t {
char *base;
char *read;
char *write;
uint32_t size;
};
uint32_t audio_ringbuf_count(const struct audio_ringbuf_t *rb);
uint32_t audio_ringbuf_free_space(const struct audio_ringbuf_t *rb);
void audio_ringbuf_copy_to_linear(
char *linear,
struct audio_ringbuf_t *rb,
uint32_t count);
void audio_ringbuf_copy_from_linear(
struct audio_ringbuf_t *rb,
const char *linear,
uint32_t count);
void audio_ringbuf_copy_from_linear_impl(
struct audio_ringbuf_t *rb,
const char *linear,
uint32_t count);
void audio_ringbuf_copy_from_ringbuf(
struct audio_ringbuf_t *rb_des,
struct audio_ringbuf_t *rb_src,
uint32_t count);
void audio_ringbuf_copy_from_ringbuf_impl(
struct audio_ringbuf_t *rb_des,
struct audio_ringbuf_t *rb_src,
uint32_t count);
void audio_ringbuf_copy_from_ringbuf_all(
struct audio_ringbuf_t *rb_des,
struct audio_ringbuf_t *rb_src);
void audio_ringbuf_write_value(
struct audio_ringbuf_t *rb,
const uint8_t value,
const uint32_t count);
void audio_ringbuf_write_zero(struct audio_ringbuf_t *rb, uint32_t count);
void audio_ringbuf_drop_data(struct audio_ringbuf_t *rb, const uint32_t count);
void audio_ringbuf_drop_all(struct audio_ringbuf_t *rb);
void audio_ringbuf_compensate_value(
struct audio_ringbuf_t *rb,
const uint8_t value,
const uint32_t count);
void audio_ringbuf_compensate_value_impl(
struct audio_ringbuf_t *rb,
const uint8_t value,
const uint32_t count);
void audio_ringbuf_rollback(struct audio_ringbuf_t *rb, const uint32_t count);
void dynamic_change_ring_buf_size(
struct audio_ringbuf_t *rb,
uint32_t write_size);
#ifdef __cplusplus
} /* extern "C" */
#endif
#endif /* end of AUDIO_RING_BUF_H */
@@ -0,0 +1,56 @@
// SPDX-License-Identifier: GPL-2.0
//
// adsp_task.h
//
// Copyright (c) 2018 MediaTek Inc.
#ifndef AUDIO_TASK_H
#define AUDIO_TASK_H
#include <linux/types.h>
enum {
/* scene for library */
TASK_SCENE_PHONE_CALL = 0,
TASK_SCENE_VOICE_ULTRASOUND = 1,
TASK_SCENE_PLAYBACK_MP3 = 2,
TASK_SCENE_RECORD = 3,
TASK_SCENE_VOIP = 4,
TASK_SCENE_SPEAKER_PROTECTION = 5,
TASK_SCENE_VOW = 6,
TASK_SCENE_PRIMARY = 7,
TASK_SCENE_DEEPBUFFER = 8,
TASK_SCENE_AUDPLAYBACK = 9,
TASK_SCENE_CAPTURE_UL1 = 10,
TASK_SCENE_A2DP = 11,
TASK_SCENE_DATAPROVIDER = 12,
TASK_SCENE_MUSIC = 13,
TASK_SCENE_CALL_FINAL = 14,
TASK_SCENE_FAST = 15,
TASK_SCENE_KTV = 16,
TASK_SCENE_CAPTURE_RAW = 17,
TASK_SCENE_FM_ADSP = 18,
TASK_SCENE_PHONE_CALL_SUB = 19,
TASK_SCENE_BLEDL = 20,
TASK_SCENE_BLEUL = 21,
TASK_SCENE_BLEDEC = 22,
TASK_SCENE_BLEENC = 23,
TASK_SCENE_BLECALLDL = 24,
TASK_SCENE_BLECALLUL = 25,
TASK_SCENE_RV_SPK_PROCESS = 26,
/* control for driver */
TASK_SCENE_AUDIO_CONTROLLER_HIFI3_A,
TASK_SCENE_AUDIO_CONTROLLER_HIFI3_B,
TASK_SCENE_AUD_DAEMON_A,
TASK_SCENE_AUD_DAEMON_B,
TASK_SCENE_AUDIO_CONTROLLER_CM4,
TASK_SCENE_AUDIO_CONTROLLER_RV,
TASK_SCENE_SIZE,
TASK_SCENE_INVALID
};
#endif /* end of AUDIO_TASK_H */
@@ -0,0 +1,322 @@
// SPDX-License-Identifier: GPL-2.0
//
// adsp_task_manager.c
//
// Copyright (c) 2018 MediaTek Inc.
#include "audio_task_manager.h"
#include <linux/string.h>
#include <linux/kthread.h>
#include <linux/wait.h>
#include <linux/spinlock.h>
#include <linux/io.h>
#include <linux/mutex.h>
#ifdef CONFIG_MTK_AUDIO_CM4_SUPPORT
#include <scp_ipi.h>
#endif
#ifdef CONFIG_MTK_DO /* TODO: check only once in one file */
#include <do.h>
#endif
#include "audio_log.h"
#include "audio_assert.h"
#include <audio_ipi_dma.h>
#include "audio_ipi_queue.h"
#include "audio_messenger_ipi.h"
#ifdef CONFIG_MTK_DO /* with DO */
static DEFINE_MUTEX(audio_load_task_mutex);
#define DO_FEATURE_NAME_CALL "FEATURE_MTK_AURISYS_PHONE_CALL"
#define DO_FEATURE_NAME_MP3 "FEATURE_MTK_AUDIO_PLAYBACK_MP3"
#define DO_FEATURE_NAME_VOW "FEATURE_MTK_VOW"
#define DO_SET_NAME_CALL "AUDIO_CALL"
#define DO_SET_NAME_MP3_VOW "AUDIO_MP3_VOW"
#endif
struct audio_task_t {
#ifdef CONFIG_MTK_DO /* with DO */
char *feature_name;
char *do_name;
bool is_do_loaded;
task_unloaded_t task_unloaded;
#endif
struct ipi_queue_handler_t *ipi_queue_handler;
};
static struct audio_task_t g_audio_task_array[TASK_SCENE_SIZE];
static char *g_current_do_name;
#ifdef CONFIG_MTK_DO /* with DO */
static char *get_feature_name(const uint8_t task_scene)
{
/*
* feature name defined in:
* repo: alps/vendor/mediatek/proprietary/tinysys/freertos/source
* file: project/CM4_A/mt6797/platform/platform.mk
*/
char *feature_name = NULL;
switch (task_scene) {
case TASK_SCENE_PHONE_CALL:
feature_name = DO_FEATURE_NAME_CALL;
break;
case TASK_SCENE_PLAYBACK_MP3:
feature_name = DO_FEATURE_NAME_MP3;
break;
case TASK_SCENE_VOW:
feature_name = DO_FEATURE_NAME_VOW;
break;
case TASK_SCENE_VOICE_ULTRASOUND:
case TASK_SCENE_RECORD:
case TASK_SCENE_VOIP:
case TASK_SCENE_SPEAKER_PROTECTION:
default: {
pr_notice("%s not support task %d", __func__, task_scene);
break;
}
}
return feature_name;
}
static char *get_do_name(const uint8_t task_scene)
{
#if 1
/*
* DO name defined in:
* repo: alps/vendor/mediatek/proprietary/tinysys/freertos/source
* file: project/CM4_A/mt6797/platform/dos.mk
*/
char *do_name = NULL;
switch (task_scene) {
case TASK_SCENE_PHONE_CALL:
do_name = DO_SET_NAME_CALL;
break;
case TASK_SCENE_PLAYBACK_MP3:
case TASK_SCENE_VOW:
do_name = DO_SET_NAME_MP3_VOW;
break;
case TASK_SCENE_VOICE_ULTRASOUND:
case TASK_SCENE_RECORD:
case TASK_SCENE_VOIP:
case TASK_SCENE_SPEAKER_PROTECTION:
default: {
pr_notice("%s not support task %d", __func__, task_scene);
break;
}
}
return do_name;
#else
char *feature_name = g_audio_task_array[task_scene].feature_name;
struct do_list_node *all_do_info = mt_do_get_do_infos();
struct do_list_node *do_node = NULL;
struct do_list_node *feature_node = NULL;
char *do_name = NULL;
do_node = all_do_info;
while (do_node != NULL) {
feature_node = do_node->features;
while (feature_node != NULL) {
if (!strcmp(feature_node->name, feature_name)) {
do_name = do_node->name;
break;
}
feature_node = feature_node->next;
}
if (do_name != NULL)
break;
do_node = do_node->next;
}
if (do_name != NULL)
pr_debug("get feature %s in DO set %s\n",
feature_name, do_name);
return do_name;
#endif
}
#endif /* end of CONFIG_MTK_DO */
int audio_task_register_callback(
const uint8_t task_scene,
recv_message_t recv_message,
task_unloaded_t task_unloaded)
{
struct audio_task_t *task = &g_audio_task_array[task_scene];
#ifdef CONFIG_MTK_DO /* with DO */
task->feature_name = get_feature_name(task_scene);
AUD_ASSERT(task->feature_name != NULL);
task->do_name = get_do_name(task_scene);
AUD_ASSERT(task->do_name != NULL);
task->is_do_loaded = false;
task->task_unloaded = task_unloaded;
/* with DO, create hanlder until do loaded */
task->ipi_queue_handler = NULL;
#else
/* without DO, create hanlder when registering */
task->ipi_queue_handler = create_ipi_queue_handler(task_scene);
#endif
audio_reg_recv_message(task_scene, recv_message);
return 0;
}
#ifdef CONFIG_MTK_DO /* with DO */
static void load_target_tasks(char *target_do_name)
{
struct audio_task_t *task = NULL;
int i = 0;
for (i = 0; i < TASK_SCENE_SIZE; i++) {
task = &g_audio_task_array[i];
if (task->do_name == NULL)
continue;
if (!strcmp(task->do_name, target_do_name)) {
task->is_do_loaded = true;
task->ipi_queue_handler = create_ipi_queue_handler(i);
}
}
}
static void unload_current_tasks(void)
{
struct audio_task_t *task = NULL;
int i = 0;
for (i = 0; i < TASK_SCENE_SIZE; i++) {
task = &g_audio_task_array[i];
if (task->is_do_loaded == true) {
task->is_do_loaded = false;
/* notify user the task is going to be unloaded */
if (task->task_unloaded != NULL)
task->task_unloaded();
disable_ipi_queue_handler(task->ipi_queue_handler);
flush_ipi_queue_handler(task->ipi_queue_handler);
destroy_ipi_queue_handler(task->ipi_queue_handler);
task->ipi_queue_handler = NULL;
}
}
}
#endif
int audio_load_task(const uint8_t task_scene)
{
#ifndef CONFIG_MTK_DO /* without DO, do nothing */
return -1;
#else
struct do_list_node *current_do = NULL;
char *target_do_name = NULL;
int retval = 0;
pr_debug("%s(+), task_scene: %d", __func__, task_scene);
if (task_scene >= TASK_SCENE_SIZE)
return -1;
mutex_lock(&audio_load_task_mutex);
target_do_name = get_do_name(task_scene);
AUD_ASSERT(target_do_name != NULL);
/* already loaded, do nothing */
if (g_current_do_name != NULL &&
!strcmp(target_do_name, g_current_do_name))
goto audio_load_task_exit;
/* unload current */
if (g_current_do_name == NULL) { /* scp might load a default DO */
current_do = mt_do_get_loaded_do(SCP_B);
if (current_do != NULL &&
current_do->name != NULL &&
strcmp(target_do_name, current_do->name) != 0)
mt_do_unload_do(current_do->name);
} else { /* another DO is loaded, unload it */
unload_current_tasks();
mt_do_unload_do(g_current_do_name);
}
/* load target */
mt_do_load_do(target_do_name);
load_target_tasks(target_do_name);
g_current_do_name = target_do_name;
audio_load_task_exit:
mutex_unlock(&audio_load_task_mutex);
pr_debug("%s(-), task_scene: %d\n", __func__, task_scene);
return retval;
#endif /* end of CONFIG_MTK_DO */
}
void audio_task_manager_init(void)
{
g_current_do_name = NULL;
memset(g_audio_task_array, 0, sizeof(g_audio_task_array));
}
void audio_task_manager_deinit(void)
{
struct audio_task_t *task = NULL;
int i = 0;
for (i = 0; i < TASK_SCENE_SIZE; i++) {
task = &g_audio_task_array[i];
if (task->ipi_queue_handler != NULL) {
disable_ipi_queue_handler(task->ipi_queue_handler);
flush_ipi_queue_handler(task->ipi_queue_handler);
destroy_ipi_queue_handler(task->ipi_queue_handler);
task->ipi_queue_handler = NULL;
}
}
}
@@ -0,0 +1,51 @@
// SPDX-License-Identifier: GPL-2.0
//
// adsp_task_manager.h
//
// Copyright (c) 2018 MediaTek Inc.
#ifndef AUDIO_TASK_MANAGER_H
#define AUDIO_TASK_MANAGER_H
#include <linux/types.h>
#include <audio_task.h>
#include <audio_messenger_ipi.h>
/*
* =============================================================================
* hook function
* =============================================================================
*/
typedef void (*task_unloaded_t)(void);
/*
* =============================================================================
* public function
* =============================================================================
*/
void audio_task_manager_init(void);
void audio_task_manager_deinit(void);
int audio_task_register_callback(
const uint8_t task_scene,
recv_message_t recv_message,
task_unloaded_t task_unloaded_callback);
int audio_load_task(const uint8_t task_scene);
#endif /* end of AUDIO_TASK_MANAGER_H */