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

+6
View File
@@ -0,0 +1,6 @@
config MTK_VDEC_FMT
bool "mtk vdec formatter"
help
This option enables mtk vdec formatter driver,
if yes, it means this platform supports VDEC-FMT(or miniMDP) HW,
if no, it means this platform not supports VDEC-FMT(or miniMDP) HW.
+28
View File
@@ -0,0 +1,28 @@
#
# SPDX-License-Identifier: GPL-2.0 */
#
# Copyright (C) 2016 MediaTek Inc.
#
# driver code for VDEC-FMT
ccflags-y += -I$(srctree)/drivers/misc/mediatek/mmdvfs/ \
-I$(srctree)/drivers/misc/mediatek/smi/ \
-I$(srctree)/drivers/misc/mediatek/m4u/$(MTK_PLATFORM) \
-I$(srctree)/drivers/iommu/ \
-I$(srctree)/drivers/staging/android/mtk_ion/ \
-I$(srctree)/drivers/staging/android/mtk_ion/mtk
ifeq ($(CONFIG_MACH_MT6781),y)
FMT_PLATFORM := mt6781
endif
obj-y += vdec_fmt_driver.o
obj-y += $(FMT_PLATFORM)/vdec_fmt_pm.o
obj-y += vdec_fmt_ion.o
obj-y += vdec_fmt_sync.o
ccflags-y += -I$(srctree)/drivers/misc/mediatek/vdec_fmt/
ccflags-y += -I$(srctree)/drivers/misc/mediatek/vdec_fmt/$(FMT_PLATFORM)
ccflags-y += -I$(srctree)/drivers/misc/mediatek/smi/$(FMT_PLATFORM)/
@@ -0,0 +1,215 @@
/* SPDX-License-Identifier: GPL-2.0 */
/*
* Copyright (C) 2016 MediaTek Inc.
*/
#include "vdec_fmt_pm.h"
#include "vdec_fmt_ion.h"
#include "vdec_fmt_utils.h"
#include <linux/clk.h>
#include <linux/soc/mediatek/mtk-pm-qos.h>
#include <mmdvfs_pmqos.h>
#include "smi_public.h"
static struct mtk_pm_qos_request fmt_qos_req_f;
static u32 fmt_freq_step_size;
static u64 fmt_freq_steps[MAX_FREQ_STEP];
static struct plist_head fmt_rlist[FMT_CORE_NUM];
static struct mm_qos_request fmt_rdma_request[FMT_CORE_NUM];
static struct mm_qos_request fmt_wdma_request[FMT_CORE_NUM];
#ifdef CONFIG_MTK_PSEUDO_M4U
#include <mach/mt_iommu.h>
#include "mach/pseudo_m4u.h"
#include "smi_port.h"
#endif
void fmt_get_module_clock_by_name(struct mtk_vdec_fmt *fmt,
const char *clkName, struct clk **clk_module)
{
*clk_module = of_clk_get_by_name(fmt->dev->of_node, clkName);
if (IS_ERR(*clk_module))
fmt_err("cannot get module clock:%s", clkName);
else
fmt_debug(0, "get module clock:%s", clkName);
}
/* Common Clock Framework */
void fmt_init_pm(struct mtk_vdec_fmt *fmt)
{
fmt_debug(0, "+");
fmt_get_module_clock_by_name(fmt, "MT_CG_VDEC",
&fmt->clk_VDEC);
}
int32_t fmt_clock_on(struct mtk_vdec_fmt *fmt)
{
int ret = 0;
#ifdef CONFIG_MTK_PSEUDO_M4U
int i, larb_id;
struct M4U_PORT_STRUCT port;
#endif
smi_bus_prepare_enable(SMI_LARB4, "FMT");
ret = clk_prepare_enable(fmt->clk_VDEC);
if (ret)
fmt_debug(0, "clk_prepare_enable VDEC_SOC failed %d", ret);
#ifdef CONFIG_MTK_PSEUDO_M4U
larb_id = 4;
//enable 34bits port configs
for (i = 12; i < 14; i++) {
port.ePortID = MTK_M4U_ID(larb_id, i);
port.Direction = 0;
port.Distance = 1;
port.domain = 0;
port.Security = 0;
port.Virtuality = 1;
m4u_config_port(&port);
fmt_debug(1, "port.ePortID %d m4u_config_port", port.ePortID);
}
#endif
return ret;
}
int32_t fmt_clock_off(struct mtk_vdec_fmt *fmt)
{
clk_disable_unprepare(fmt->clk_VDEC);
smi_bus_disable_unprepare(SMI_LARB4, "FMT");
return 0;
}
void fmt_prepare_dvfs_emi_bw(void)
{
int ret, i;
mtk_pm_qos_add_request(&fmt_qos_req_f, PM_QOS_VDEC_FREQ,
PM_QOS_DEFAULT_VALUE);
fmt_freq_step_size = 1;
ret = mmdvfs_qos_get_freq_steps(PM_QOS_VDEC_FREQ, &fmt_freq_steps[0],
&fmt_freq_step_size);
if (ret < 0)
fmt_err("Failed to get fmt freq steps (%d)", ret);
for (i = 0; i < fmt_freq_step_size; i++)
fmt_debug(1, "freq_steps[%d] %ld", i, fmt_freq_steps[i]);
plist_head_init(&fmt_rlist[0]);
mm_qos_add_request(&fmt_rlist[0],
&fmt_rdma_request[0], M4U_PORT_L4_MINI_MDP_R0_EXT);
mm_qos_add_request(&fmt_rlist[0],
&fmt_wdma_request[0], M4U_PORT_L4_MINI_MDP_W0_EXT);
}
void fmt_unprepare_dvfs_emi_bw(void)
{
int freq_idx = 0;
freq_idx = (fmt_freq_step_size == 0) ? 0 : (fmt_freq_step_size - 1);
mtk_pm_qos_update_request(&fmt_qos_req_f, fmt_freq_steps[freq_idx]);
mtk_pm_qos_remove_request(&fmt_qos_req_f);
mm_qos_remove_all_request(&fmt_rlist[0]);
}
void fmt_start_dvfs_emi_bw(struct fmt_pmqos pmqos_param)
{
u64 request_freq;
struct timeval curr_time;
s32 duration;
u32 bandwidth;
fmt_debug(1, "tv_sec %d tv_usec %d pixel_size %d rdma_datasize %d wdma_datasize %d",
pmqos_param.tv_sec,
pmqos_param.tv_usec,
pmqos_param.pixel_size,
pmqos_param.rdma_datasize,
pmqos_param.wdma_datasize);
do_gettimeofday(&curr_time);
fmt_debug(1, "curr time tv_sec %d tv_usec %d", curr_time.tv_sec, curr_time.tv_usec);
FMT_TIMER_GET_DURATION_IN_US(curr_time, pmqos_param, duration);
request_freq = pmqos_param.pixel_size / duration;
if (request_freq > fmt_freq_steps[0])
request_freq = fmt_freq_steps[0];
fmt_debug(1, "request_freq %d", request_freq);
mtk_pm_qos_update_request(&fmt_qos_req_f, request_freq);
FMT_BANDWIDTH(pmqos_param.rdma_datasize, pmqos_param.pixel_size, request_freq, bandwidth);
mm_qos_set_request(&fmt_rdma_request[0], bandwidth, 0, BW_COMP_NONE);
fmt_debug(1, "rdma bandwidth %d", bandwidth);
FMT_BANDWIDTH(pmqos_param.wdma_datasize, pmqos_param.pixel_size, request_freq, bandwidth);
mm_qos_set_request(&fmt_wdma_request[0], bandwidth, 0, BW_COMP_NONE);
fmt_debug(1, "wdma bandwidth %d", bandwidth);
mm_qos_update_all_request(&fmt_rlist[0]);
}
void fmt_end_dvfs_emi_bw(void)
{
mtk_pm_qos_update_request(&fmt_qos_req_f, 0);
mm_qos_set_request(&fmt_rdma_request[0], 0, 0, BW_COMP_NONE);
mm_qos_set_request(&fmt_wdma_request[0], 0, 0, BW_COMP_NONE);
mm_qos_update_all_request(&fmt_rlist[0]);
}
void fmt_dump_addr_reg(struct mtk_vdec_fmt *fmt, int port)
{
int i;
switch (port) {
case M4U_PORT_L4_MINI_MDP_R0_EXT:
for (i = 0; i < 313; i++)
fmt_debug(0, "FMT RDMA0(0x%x) 0x%x",
fmt->map_base[0].base + i*4,
FMT_GET32(fmt->map_base[0].va + i*4));
for (i = 960; i < 982; i++)
fmt_debug(0, "FMT RDMA0(0x%x) 0x%x",
fmt->map_base[0].base + i*4,
FMT_GET32(fmt->map_base[0].va + i*4));
break;
case M4U_PORT_L4_MINI_MDP_W0_EXT:
for (i = 0; i < 60; i++)
fmt_debug(0, "FMT WROT0(0x%x) 0x%x",
fmt->map_base[1].base + i*4,
FMT_GET32(fmt->map_base[1].va + i*4));
for (i = 960; i < 973; i++)
fmt_debug(0, "FMT WROT0(0x%x) 0x%x",
fmt->map_base[1].base + i*4,
FMT_GET32(fmt->map_base[1].va + i*4));
break;
default:
break;
}
}
#ifdef CONFIG_MTK_IOMMU_V2
enum mtk_iommu_callback_ret_t fmt_translation_fault_callback(
int port, unsigned long mva, void *data)
{
struct mtk_vdec_fmt *fmt = (struct mtk_vdec_fmt *) data;
fmt_debug(0, "TF callback, port:%d, mva:0x%x", port, mva);
fmt_dump_addr_reg(fmt, port);
return MTK_IOMMU_CALLBACK_HANDLED;
}
#endif
void fmt_translation_fault_callback_setting(struct mtk_vdec_fmt *fmt)
{
#ifdef CONFIG_MTK_IOMMU_V2
mtk_iommu_register_fault_callback(M4U_PORT_L4_MINI_MDP_R0_EXT,
(mtk_iommu_fault_callback_t)fmt_translation_fault_callback,
fmt);
mtk_iommu_register_fault_callback(M4U_PORT_L4_MINI_MDP_W0_EXT,
(mtk_iommu_fault_callback_t)fmt_translation_fault_callback,
fmt);
#endif
}
@@ -0,0 +1,19 @@
/* SPDX-License-Identifier: GPL-2.0 */
/*
* Copyright (C) 2016 MediaTek Inc.
*/
#ifndef MTK_FMT_PM_H
#define MTK_FMT_PM_H
#include "vdec_fmt_driver.h"
void fmt_init_pm(struct mtk_vdec_fmt *fmt);
int32_t fmt_clock_on(struct mtk_vdec_fmt *fmt);
int32_t fmt_clock_off(struct mtk_vdec_fmt *fmt);
void fmt_prepare_dvfs_emi_bw(void);
void fmt_unprepare_dvfs_emi_bw(void);
void fmt_start_dvfs_emi_bw(struct fmt_pmqos pmqos_param);
void fmt_end_dvfs_emi_bw(void);
void fmt_translation_fault_callback_setting(struct mtk_vdec_fmt *fmt);
#endif /* _MTK_FMT_PM_H */
@@ -0,0 +1,850 @@
/* SPDX-License-Identifier: GPL-2.0 */
/*
* Copyright (C) 2016 MediaTek Inc.
*/
#include <linux/module.h>
#include <linux/file.h>
#include <linux/delay.h>
#include <linux/cdev.h>
#include <linux/suspend.h>
#include <linux/semaphore.h>
#include <linux/of_address.h>
#include <linux/soc/mediatek/mtk-cmdq.h>
#include "vdec_fmt_driver.h"
#include "vdec_fmt_ion.h"
#include "vdec_fmt_pm.h"
#include "vdec_fmt_utils.h"
static struct mtk_vdec_fmt *fmt_mtkdev;
int fmt_dbg_level;
module_param(fmt_dbg_level, int, 0644);
static int fmt_check_reg_base(struct mtk_vdec_fmt *fmt, u64 addr, u64 length)
{
int i;
if (addr >= MAP_PA_BASE_1GB)
return -EINVAL;
for (i = 0; i < (int)FMT_MAP_HW_REG_NUM; i++)
if (addr >= (u64)fmt->map_base[i].base &&
addr + length <= (u64)fmt->map_base[i].base + fmt->map_base[i].len)
return 0;
fmt_err("addr %x length %x not found!", addr, length);
return -EINVAL;
}
static int fmt_lock(u32 id, bool sec)
{
unsigned int suspend_block_cnt = 0;
int ret = -1;
struct mtk_vdec_fmt *fmt = fmt_mtkdev;
enum fmt_gce_status gce_status = GCE_NONE;
if (id >= fmt->gce_th_num)
return ret;
while (fmt->is_entering_suspend == 1) {
suspend_block_cnt++;
if (suspend_block_cnt > SUSPEND_TIMEOUT_CNT) {
fmt_debug(0, "blocked by suspend");
suspend_block_cnt = 0;
}
usleep_range(10000, 20000);
}
if (sec != false)
gce_status = GCE_SECURE;
else
gce_status = GCE_NORMAL;
fmt_debug(1, "id %d gce_status %d cur gce_status %d",
id,
gce_status,
fmt->gce_status[id]);
// only need to trylock semaphore when gce status change
// NONE -> NORMAL/ NONE -> SECURE / NORMAL <-> SECURE
if (gce_status != fmt->gce_status[id])
ret = down_trylock(&fmt->fmt_sem[id]);
else
ret = 0;
if (ret == 0)
fmt->gce_status[id] = gce_status;
return ret;
}
static void fmt_unlock(u32 id)
{
struct mtk_vdec_fmt *fmt = fmt_mtkdev;
if (id >= fmt->gce_th_num)
return;
fmt_debug(1, "id %d cur gce_status", id, fmt->gce_status[id]);
up(&fmt->fmt_sem[id]);
fmt->gce_status[id] = GCE_NONE;
}
static int fmt_set_gce_task(struct cmdq_pkt *pkt_ptr, u32 id)
{
int i;
struct mtk_vdec_fmt *fmt = fmt_mtkdev;
mutex_lock(&fmt->mux_task);
for (i = 0; i < FMT_INST_MAX; i++) {
if (fmt->gce_task[i].used == 0 && fmt->gce_task[i].pkt_ptr == NULL) {
fmt->gce_task[i].used = 1;
fmt->gce_task[i].pkt_ptr = pkt_ptr;
fmt->gce_task[i].identifier = id;
mutex_unlock(&fmt->mux_task);
fmt_debug(1, "taskid %d pkt_ptr %p",
i, pkt_ptr);
return i;
}
}
mutex_unlock(&fmt->mux_task);
return -1;
}
static void fmt_clear_gce_task(unsigned int taskid)
{
struct mtk_vdec_fmt *fmt = fmt_mtkdev;
mutex_lock(&fmt->mux_task);
if (fmt->gce_task[taskid].used == 1 && fmt->gce_task[taskid].pkt_ptr != NULL) {
fmt_debug(1, "taskid %d pkt_ptr %p",
taskid, fmt->gce_task[taskid].pkt_ptr);
fmt->gce_task[taskid].used = 0;
fmt->gce_task[taskid].pkt_ptr = NULL;
fmt->gce_task[taskid].identifier = 0;
}
mutex_unlock(&fmt->mux_task);
}
static void fmt_set_gce_cmd(struct cmdq_pkt *pkt,
unsigned char cmd,
u64 addr, u64 data, u32 mask, u32 gpr, u32 dma_offset, u32 dma_size, struct ionmap map[])
{
struct mtk_vdec_fmt *fmt = fmt_mtkdev;
(void)dma_offset;
(void)dma_size;
switch (cmd) {
case CMD_READ:
fmt_debug(3, "CMD_READ addr 0x%x", addr);
if (fmt_check_reg_base(fmt, addr, 4) == 0)
cmdq_pkt_read_addr(pkt, addr, CMDQ_THR_SPR_IDX1);
else
fmt_err("CMD_READ wrong addr: 0x%x", addr);
break;
case CMD_WRITE:
fmt_debug(3, "CMD_WRITE addr 0x%x data 0x%x mask 0x%x", addr, data, mask);
if (fmt_check_reg_base(fmt, addr, 4) == 0)
cmdq_pkt_write(pkt, fmt->clt_base, addr, data, mask);
else
fmt_err("CMD_WRITE wrong addr: 0x%x 0x%x 0x%x",
addr, data, mask);
break;
case CMD_POLL_REG:
fmt_debug(3, "CMD_POLL_REG addr 0x%x data 0x%x mask 0x%x", addr, data, mask);
if (fmt_check_reg_base(fmt, addr, 4) == 0)
cmdq_pkt_poll_addr(pkt, data, addr, mask, gpr);
else
fmt_err("CMD_POLL_REG wrong addr: 0x%x 0x%x 0x%x",
addr, data, mask);
break;
case CMD_WAIT_EVENT:
if (data < GCE_EVENT_MAX) {
fmt_debug(3, "CMD_WAIT_EVENT eid %d", fmt->gce_codec_eid[data]);
cmdq_pkt_wfe(pkt, fmt->gce_codec_eid[data]);
} else
fmt_err("CMD_WAIT_EVENT got wrong eid %llu",
data);
break;
case CMD_CLEAR_EVENT:
if (data < GCE_EVENT_MAX) {
fmt_debug(3, "CMD_CLEAR_EVENT eid %d", fmt->gce_codec_eid[data]);
cmdq_pkt_clear_event(pkt, fmt->gce_codec_eid[data]);
} else
fmt_err("got wrong eid %llu",
data);
break;
case CMD_WRITE_FD:
fmt_debug(3, "CMD_WRITE_FD addr 0x%x fd 0x%x offset 0x%x", addr, data, mask);
if (fmt_check_reg_base(fmt, addr, 4) == 0)
cmdq_pkt_write(pkt, fmt->clt_base, addr,
fmt_translate_fd(data, mask, map), ~0);
else
fmt_err("CMD_WRITE_FD wrong addr: 0x%x 0x%x 0x%x",
addr, data, mask);
break;
default:
fmt_err("unknown GCE cmd %d", cmd);
break;
}
}
static void fmt_gce_flush_callback(struct cmdq_cb_data data)
{
struct cmdq_pkt *pkt_ptr;
pkt_ptr = (struct cmdq_pkt *)data.data;
if (data.err < 0)
fmt_err("pkt_ptr %p", pkt_ptr);
}
static int fmt_gce_cmd_flush(unsigned long arg)
{
int i, taskid, ret;
unsigned char *user_data_addr = NULL;
struct gce_cmdq_obj buff;
struct cmdq_pkt *pkt_ptr;
struct cmdq_client *cl;
struct gce_cmds *cmds;
unsigned int suspend_block_cnt = 0;
unsigned int identifier;
struct mtk_vdec_fmt *fmt = fmt_mtkdev;
int lock = -1;
struct ionmap map[FMT_FD_RESERVE];
fmt_debug(1, "+");
user_data_addr = (unsigned char *)arg;
ret = (long)copy_from_user(&buff, user_data_addr,
(unsigned long)sizeof(struct gce_cmdq_obj));
if (ret != 0L) {
fmt_err("gce_cmdq_obj copy_from_user failed! %d",
ret);
return -EINVAL;
}
identifier = buff.identifier;
if (identifier >= fmt->gce_th_num) {
fmt_err("invalid identifier %u",
identifier);
return -EINVAL;
}
cl = fmt->clt_fmt[identifier];
if (cl == NULL) {
fmt_err("gce thread is null id %d",
buff.identifier);
return -EINVAL;
}
while (fmt->is_entering_suspend == 1) {
suspend_block_cnt++;
if (suspend_block_cnt > 500) {
fmt_debug(0, "gce_flush blocked by suspend");
suspend_block_cnt = 0;
}
usleep_range(10000, 20000);
}
mutex_lock(&fmt->mux_gce_th[identifier]);
while (lock != 0) {
lock = fmt_lock(identifier,
(bool)buff.secure);
if (lock != 0) {
mutex_unlock(&fmt->mux_gce_th[identifier]);
usleep_range(1000, 2000);
mutex_lock(&fmt->mux_gce_th[identifier]);
}
}
cmds = fmt->gce_cmds[identifier];
user_data_addr = (unsigned char *)
(unsigned long)buff.cmds_user_ptr;
ret = (long)copy_from_user(cmds, user_data_addr,
(unsigned long)sizeof(struct gce_cmds));
if (ret != 0L) {
fmt_err("gce_cmds copy_from_user failed! %d",
ret);
mutex_unlock(&fmt->mux_gce_th[identifier]);
return -EINVAL;
}
if (cmds->cmd_cnt >= FMT_CMDQ_CMD_MAX) {
fmt_err("cmd_cnt (%d) overflow!!", cmds->cmd_cnt);
cmds->cmd_cnt = FMT_CMDQ_CMD_MAX;
ret = -EINVAL;
return ret;
}
buff.cmds_user_ptr = (u64)(unsigned long)cmds;
mutex_lock(&fmt->mux_fmt);
if ((atomic_read(&fmt->gce_job_cnt[0])
+ atomic_read(&fmt->gce_job_cnt[1])) == 0) {
// FMT cores share the same MTCMOS/CLK,
// pwr/clock on/off only when there's 0 job on both pipes
fmt_debug(1, "Both pipe job cnt = 0, pwr/clock on");
ret = fmt_clock_on(fmt);
if (ret != 0L) {
fmt_err("fmt_clock_on failed!%d",
ret);
mutex_unlock(&fmt->mux_gce_th[identifier]);
mutex_unlock(&fmt->mux_fmt);
return -EINVAL;
}
}
atomic_inc(&fmt->gce_job_cnt[identifier]);
mutex_unlock(&fmt->mux_fmt);
fmt_start_dvfs_emi_bw(buff.pmqos_param);
pkt_ptr = cmdq_pkt_create(cl);
if (IS_ERR_OR_NULL(pkt_ptr)) {
fmt_err("cmdq_pkt_create fail");
pkt_ptr = NULL;
}
for (i = 0; i < FMT_FD_RESERVE; i++) {
map[i].fd = -1;
map[i].iova = 0;
}
for (i = 0; i < cmds->cmd_cnt; i++) {
fmt_set_gce_cmd(pkt_ptr, cmds->cmd[i],
cmds->addr[i], cmds->data[i],
cmds->mask[i], fmt->gce_gpr[identifier],
cmds->dma_offset[i], cmds->dma_size[i], map);
}
mutex_unlock(&fmt->mux_gce_th[identifier]);
taskid = fmt_set_gce_task(pkt_ptr, identifier);
if (taskid < 0) {
fmt_err("failed to set task id");
ret = -EINVAL;
return ret;
}
memcpy(&fmt->gce_task[taskid].cmdq_buff, &buff, sizeof(buff));
// flush cmd async
cmdq_pkt_flush_async(pkt_ptr,
fmt_gce_flush_callback, (void *)fmt->gce_task[taskid].pkt_ptr);
if (copy_to_user(buff.taskid, &taskid, sizeof(unsigned int))) {
fmt_err("copy task id to user fail");
return -EFAULT;
}
fmt_debug(1, "-");
return ret;
}
static int fmt_gce_wait_callback(unsigned long arg)
{
int ret;
unsigned int identifier, taskid;
unsigned char *user_data_addr = NULL;
struct mtk_vdec_fmt *fmt = fmt_mtkdev;
user_data_addr = (unsigned char *)arg;
ret = (long)copy_from_user(&taskid, user_data_addr,
(unsigned long)sizeof(unsigned int));
if (ret != 0L) {
fmt_err("copy_from_user failed!%d",
ret);
return -EINVAL;
}
if (taskid >= FMT_INST_MAX) {
fmt_err("invalid taskid %u",
taskid);
return -EINVAL;
}
identifier = fmt->gce_task[taskid].identifier;
if (identifier >= fmt->gce_th_num) {
fmt_err("invalid identifier %u",
identifier);
return -EINVAL;
}
fmt_debug(1, "id %d taskid %d pkt_ptr %p",
identifier, taskid, fmt->gce_task[taskid].pkt_ptr);
cmdq_pkt_wait_complete(fmt->gce_task[taskid].pkt_ptr);
mutex_lock(&fmt->mux_gce_th[identifier]);
fmt_end_dvfs_emi_bw();
mutex_lock(&fmt->mux_fmt);
atomic_dec(&fmt->gce_job_cnt[identifier]);
if ((atomic_read(&fmt->gce_job_cnt[0])
+ atomic_read(&fmt->gce_job_cnt[1])) == 0) {
// FMT cores share the same MTCMOS/CLK,
// pwr/clock on/off only when there's 0 job on both pipes
fmt_debug(1, "Both pipe job cnt = 0, pwr/clock off");
ret = fmt_clock_off(fmt);
if (ret != 0L) {
fmt_err("fmt_clock_off failed!%d",
ret);
return -EINVAL;
}
}
mutex_unlock(&fmt->mux_fmt);
if (atomic_read(&fmt->gce_job_cnt[identifier]) == 0)
fmt_unlock(identifier);
mutex_unlock(&fmt->mux_gce_th[identifier]);
cmdq_pkt_destroy(fmt->gce_task[taskid].pkt_ptr);
fmt_clear_gce_task(taskid);
return ret;
}
static int vdec_fmt_open(struct inode *inode, struct file *file)
{
fmt_debug(1, "tid:%d", current->pid);
return 0;
}
static int vdec_fmt_release(struct inode *ignored, struct file *file)
{
fmt_debug(1, "tid:%d", current->pid);
return 0;
}
static long vdec_fmt_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
{
long ret = -1;
fmt_debug(1, "pid:%d tid:%d", current->group_leader->pid, current->pid);
switch (cmd) {
case FMT_GCE_SET_CMD_FLUSH:
ret = fmt_gce_cmd_flush(arg);
break;
case FMT_GCE_WAIT_CALLBACK:
ret = fmt_gce_wait_callback(arg);
break;
default:
fmt_err("Unknown cmd");
break;
}
return ret;
}
#ifdef MTK_VDEC_FMT_DVT_VERIFICATION
void vdec_fmt_vma_open(struct vm_area_struct *vma)
{
fmt_debug(1, "vdec fmt VMA open, virt %lx, phys %lx",
vma->vm_start, vma->vm_pgoff << PAGE_SHIFT);
}
void vdec_fmt_vma_close(struct vm_area_struct *vma)
{
fmt_debug(1, "vdec fmt VMA close, virt %lx, phys %lx",
vma->vm_start, vma->vm_pgoff << PAGE_SHIFT);
}
static const struct vm_operations_struct
vdec_fmt_remap_vm_ops = {
.open = vdec_fmt_vma_open,
.close = vdec_fmt_vma_close,
};
static int vdec_fmt_mmap(struct file *file, struct vm_area_struct *vma)
{
unsigned long length;
unsigned long pfn;
length = vma->vm_end - vma->vm_start;
pfn = vma->vm_pgoff<<PAGE_SHIFT;
if (fmt_check_reg_base(fmt_mtkdev, pfn, length) != 0) {
fmt_err("illegal mmap pfn 0x%lx, len 0x%lx",
pfn, length);
return -EAGAIN;
}
fmt_debug(1, "start 0x%lx, end 0x%lx, pgoff 0x%lx",
(unsigned long)vma->vm_start,
(unsigned long)vma->vm_end,
(unsigned long)vma->vm_pgoff);
vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
if (remap_pfn_range(vma, vma->vm_start, vma->vm_pgoff,
vma->vm_end - vma->vm_start, vma->vm_page_prot)) {
return -EAGAIN;
}
vma->vm_ops = &vdec_fmt_remap_vm_ops;
vdec_fmt_vma_open(vma);
return 0;
}
#endif
#ifdef CONFIG_COMPAT
static int compat_get_gce_cmdq_obj_data(
struct compat_gce_cmdq_obj __user *data32,
struct gce_cmdq_obj __user *data)
{
u64 u64t;
u32 u32t;
compat_uptr_t ptrt;
int err;
err = get_user(u64t, &data32->cmds_user_ptr);
err |= put_user(u64t, &data->cmds_user_ptr);
err |= get_user(u32t, &data32->identifier);
err |= put_user(u32t, &data->identifier);
err |= get_user(u32t, &data32->secure);
err |= put_user(u32t, &data->secure);
err |= get_user(ptrt, &data32->taskid);
err |= put_user(compat_ptr(ptrt), &data->taskid);
err |= get_user(u32t, &data32->pmqos_param.tv_sec);
err |= put_user(u32t, &data->pmqos_param.tv_sec);
err |= get_user(u32t, &data32->pmqos_param.tv_usec);
err |= put_user(u32t, &data->pmqos_param.tv_usec);
err |= get_user(u32t, &data32->pmqos_param.pixel_size);
err |= put_user(u32t, &data->pmqos_param.pixel_size);
err |= get_user(u32t, &data32->pmqos_param.rdma_datasize);
err |= put_user(u32t, &data->pmqos_param.rdma_datasize);
err |= get_user(u32t, &data32->pmqos_param.wdma_datasize);
err |= put_user(u32t, &data->pmqos_param.wdma_datasize);
return err;
}
static long compat_vdec_fmt_ioctl(struct file *file, unsigned int cmd,
unsigned long arg)
{
int err = 0;
fmt_debug(1, "pid:%d tid:%d", current->group_leader->pid, current->pid);
switch (cmd) {
case COMPAT_FMT_GCE_SET_CMD_FLUSH:
{
struct compat_gce_cmdq_obj __user *data32;
struct gce_cmdq_obj __user *data;
data32 = compat_ptr((uint32_t)arg);
data = compat_alloc_user_space(sizeof(struct gce_cmdq_obj));
if (data == NULL)
return -EFAULT;
err = compat_get_gce_cmdq_obj_data(data32, data);
if (err != 0)
return err;
return file->f_op->unlocked_ioctl(file, FMT_GCE_SET_CMD_FLUSH,
(unsigned long)data);
}
case FMT_GCE_WAIT_CALLBACK:
return file->f_op->unlocked_ioctl(file, cmd, arg);
default:
fmt_err("Unknown cmd");
return -EFAULT;
}
}
#endif
static const struct file_operations vdec_fmt_fops = {
.owner = THIS_MODULE,
.open = vdec_fmt_open,
.release = vdec_fmt_release,
.unlocked_ioctl = vdec_fmt_ioctl,
#ifdef MTK_VDEC_FMT_DVT_VERIFICATION
.mmap = vdec_fmt_mmap,
#endif
#ifdef CONFIG_COMPAT
.compat_ioctl = compat_vdec_fmt_ioctl,
#endif
};
static int vdec_fmt_suspend(struct device *pDev)
{
struct mtk_vdec_fmt *fmt = fmt_mtkdev;
fmt_debug(1, "+");
if (atomic_read(&fmt->gce_job_cnt[0]) > 0 ||
atomic_read(&fmt->gce_job_cnt[1]) > 0) {
fmt_debug(0, "waiting %d %d",
atomic_read(&fmt->gce_job_cnt[0]),
atomic_read(&fmt->gce_job_cnt[1]));
return -EBUSY;
}
fmt_debug(1, "-");
return 0;
}
static int vdec_fmt_resume(struct device *pDev)
{
fmt_debug(1, "resume done");
return 0;
}
static int vdec_fmt_suspend_notifier(struct notifier_block *nb,
unsigned long action, void *data)
{
int wait_cnt = 0;
struct mtk_vdec_fmt *fmt = fmt_mtkdev;
fmt_debug(1, "action %lu", action);
switch (action) {
case PM_SUSPEND_PREPARE:
fmt->is_entering_suspend = 1;
while (atomic_read(&fmt->gce_job_cnt[0]) > 0 ||
atomic_read(&fmt->gce_job_cnt[1]) > 0) {
wait_cnt++;
if (wait_cnt > 5) {
fmt_debug(0, "waiting %d %d",
atomic_read(&fmt->gce_job_cnt[0]),
atomic_read(&fmt->gce_job_cnt[1]));
/* Current task is still not finished, don't
* care, will check again in real suspend
*/
return NOTIFY_DONE;
}
usleep_range(10000, 20000);
}
return NOTIFY_OK;
case PM_POST_SUSPEND:
fmt->is_entering_suspend = 0;
return NOTIFY_OK;
default:
return NOTIFY_DONE;
}
return NOTIFY_DONE;
}
static int vdec_fmt_probe(struct platform_device *pdev)
{
struct mtk_vdec_fmt *fmt;
struct resource *res;
struct device *dev;
int ret = 0;
int i;
fmt_debug(0, "initialization");
dev = &pdev->dev;
fmt = devm_kzalloc(dev, sizeof(*fmt), GFP_KERNEL);
if (fmt == NULL)
return -ENOMEM;
fmt_mtkdev = fmt;
ret = of_property_read_string(dev->of_node, "mediatek,fmtname",
&fmt->fmtname);
fmt_debug(0, "name %s", fmt->fmtname);
if (ret != 0) {
fmt_err("failed to find mediatek,fmtname");
return ret;
}
fmt->dev = &pdev->dev;
platform_set_drvdata(pdev, fmt);
for (i = 0; i < (int)FMT_MAP_HW_REG_NUM; i++) {
res = platform_get_resource(pdev, IORESOURCE_MEM, i);
if (res == NULL)
break;
fmt->map_base[i].base = res->start;
fmt->map_base[i].len = resource_size(res);
fmt->map_base[i].va = (unsigned long)of_iomap(dev->of_node, i);
fmt_debug(0, "base[%d]: pa:0x%lx va:0x%lx 0x%lx",
i, fmt->map_base[i].base, fmt->map_base[i].va,
fmt->map_base[i].len);
}
ret = alloc_chrdev_region(&fmt->fmt_devno, 0, 1,
fmt->fmtname);
if (ret < 0) {
fmt_err(
"alloc_chrdev_region failed (%d)", ret);
goto err_alloc;
}
fmt->fmt_cdev = cdev_alloc();
fmt->fmt_cdev->owner = THIS_MODULE;
fmt->fmt_cdev->ops = &vdec_fmt_fops;
ret = cdev_add(fmt->fmt_cdev,
fmt->fmt_devno, 1);
if (ret < 0) {
fmt_err("cdev_add fail (ret=%d)", ret);
goto err_add;
}
fmt->fmt_class = class_create(THIS_MODULE,
fmt->fmtname);
if (IS_ERR_OR_NULL(fmt->fmt_class) == true) {
ret = (int)PTR_ERR(fmt->fmt_class);
fmt_err("class create fail (ret=%d)", ret);
goto err_add;
}
fmt->fmt_device =
device_create(fmt->fmt_class,
NULL,
fmt->fmt_devno,
NULL, fmt->fmtname);
if (IS_ERR_OR_NULL(fmt->fmt_device) == true) {
ret = (int)PTR_ERR(fmt->fmt_device);
fmt_err("device_create fail (ret=%d)", ret);
goto err_device;
}
ret = of_property_read_u32(dev->of_node, "mediatek,fmt_gce_th_num",
&fmt->gce_th_num);
fmt_debug(0, "gce_th_num %d", fmt->gce_th_num);
if (ret != 0 || fmt->gce_th_num > FMT_CORE_NUM)
fmt->gce_th_num = 1;
for (i = 0; i < ARRAY_SIZE(fmt->gce_gpr); i++) {
ret = of_property_read_u32_index(dev->of_node, "gce-gpr",
i, &fmt->gce_gpr[i]);
if (ret < 0)
break;
}
fmt->clt_base = cmdq_register_device(dev);
for (i = 0; i < fmt->gce_th_num; i++) {
fmt->clt_fmt[i] = cmdq_mbox_create(dev, i);
fmt_debug(0, "clt_fmt[%d] %p", i, fmt->clt_fmt[i]);
}
if (IS_ERR_OR_NULL(fmt->clt_fmt[0]))
goto err_device;
of_property_read_u16(dev->of_node,
"rdma0_sw_rst_done_eng",
&fmt->gce_codec_eid[FMT_RDMA0_SW_RST_DONE_ENG]);
of_property_read_u16(dev->of_node,
"rdma0_tile_done",
&fmt->gce_codec_eid[FMT_RDMA0_TILE_DONE]);
of_property_read_u16(dev->of_node,
"wdma0_sw_rst_done_eng",
&fmt->gce_codec_eid[FMT_WDMA0_SW_RST_DONE_ENG]);
of_property_read_u16(dev->of_node,
"wdma0_tile_done",
&fmt->gce_codec_eid[FMT_WDMA0_TILE_DONE]);
of_property_read_u16(dev->of_node,
"rdma1_sw_rst_done_eng",
&fmt->gce_codec_eid[FMT_RDMA1_SW_RST_DONE_ENG]);
of_property_read_u16(dev->of_node,
"rdma1_tile_done",
&fmt->gce_codec_eid[FMT_RDMA1_TILE_DONE]);
of_property_read_u16(dev->of_node,
"wdma1_sw_rst_done_eng",
&fmt->gce_codec_eid[FMT_WDMA1_SW_RST_DONE_ENG]);
of_property_read_u16(dev->of_node,
"wdma1_tile_done",
&fmt->gce_codec_eid[FMT_WDMA1_TILE_DONE]);
for (i = 0; i < (int)FMT_CORE_NUM; i++) {
fmt->gce_cmds[i] = devm_kzalloc(dev,
sizeof(struct gce_cmds), GFP_KERNEL);
if (fmt->gce_cmds[i] == NULL)
goto err_device;
}
for (i = 0; i < GCE_EVENT_MAX; i++)
fmt_debug(0, "gce event %d id %d", i, fmt->gce_codec_eid[i]);
for (i = 0; i < FMT_INST_MAX; i++) {
fmt->gce_task[i].pkt_ptr = NULL;
fmt->gce_task[i].used = 0;
fmt->gce_task[i].identifier = 0;
}
for (i = 0; i < fmt->gce_th_num; i++) {
sema_init(&fmt->fmt_sem[i], 1);
mutex_init(&fmt->mux_gce_th[i]);
}
mutex_init(&fmt->mux_fmt);
mutex_init(&fmt->mux_task);
for (i = 0; i < fmt->gce_th_num; i++)
fmt->gce_status[i] = GCE_NONE;
for (i = 0; i < fmt->gce_th_num; i++)
atomic_set(&fmt->gce_job_cnt[i], 0);
fmt_prepare_dvfs_emi_bw();
fmt->pm_notifier.notifier_call = vdec_fmt_suspend_notifier;
register_pm_notifier(&fmt->pm_notifier);
fmt->is_entering_suspend = 0;
fmt_ion_create("VDEC-FMT");
fmt_init_pm(fmt);
fmt_translation_fault_callback_setting(fmt);
fmt_debug(0, "initialization completed");
return 0;
err_device:
class_destroy(fmt->fmt_class);
err_add:
cdev_del(fmt->fmt_cdev);
err_alloc:
unregister_chrdev_region(fmt->fmt_devno, 1);
return ret;
}
static const struct of_device_id vdec_fmt_match[] = {
{.compatible = "mediatek-vdec-fmt",},
{}
};
static int vdec_fmt_remove(struct platform_device *pdev)
{
struct mtk_vdec_fmt *fmt = platform_get_drvdata(pdev);
fmt_unprepare_dvfs_emi_bw();
device_destroy(fmt->fmt_class, fmt->fmt_devno);
class_destroy(fmt->fmt_class);
cdev_del(fmt->fmt_cdev);
unregister_chrdev_region(fmt->fmt_devno, 1);
fmt_ion_destroy();
return 0;
}
static const struct dev_pm_ops vdec_fmt_pm_ops = {
.suspend = vdec_fmt_suspend,
.resume = vdec_fmt_resume,
};
static struct platform_driver vdec_fmt_driver = {
.probe = vdec_fmt_probe,
.remove = vdec_fmt_remove,
.driver = {
.name = VDEC_FMT_DEVNAME,
.owner = THIS_MODULE,
.pm = &vdec_fmt_pm_ops,
.of_match_table = vdec_fmt_match,
},
};
module_platform_driver(vdec_fmt_driver);
@@ -0,0 +1,137 @@
/* SPDX-License-Identifier: GPL-2.0 */
/*
* Copyright (C) 2016 MediaTek Inc.
*/
#ifndef MTK_FMT_H
#define MTK_FMT_H
#include <mt-plat/sync_write.h>
#include <linux/time.h>
#define FMT_CMDQ_CMD_MAX (2048)
#define VDEC_FMT_DEVNAME "vdec-fmt"
#define FMT_CORE_NUM 2
#define GCE_EVENT_MAX 8
#define FMT_MAP_HW_REG_NUM 6
#define FMT_INST_MAX 64
#define GCE_PENDING_CNT 10
#define MAP_PA_BASE_1GB 0x40000000 /* < 1GB registers */
#define SUSPEND_TIMEOUT_CNT 5000
#define FMT_REG_GET32(addr) (readl((void *)addr) & 0xFFFFFFFF)
#define FMT_REG_SET32(addr, val) mt_reg_sync_writel(val, (addr))
struct gce_cmds {
u8 cmd[FMT_CMDQ_CMD_MAX];
u64 addr[FMT_CMDQ_CMD_MAX];
u64 data[FMT_CMDQ_CMD_MAX];
u32 mask[FMT_CMDQ_CMD_MAX];
u32 dma_offset[FMT_CMDQ_CMD_MAX];
u32 dma_size[FMT_CMDQ_CMD_MAX];
u32 cmd_cnt;
};
struct map_hw_reg {
unsigned long base;
unsigned long len;
unsigned long va;
};
struct fmt_pmqos {
u32 tv_sec;
u32 tv_usec;
u32 pixel_size;
u32 rdma_datasize;
u32 wdma_datasize;
};
struct gce_cmdq_obj {
u64 cmds_user_ptr;
u32 identifier;
u32 secure;
u32 *taskid;
struct fmt_pmqos pmqos_param;
};
#ifdef CONFIG_COMPAT
struct compat_gce_cmdq_obj {
u64 cmds_user_ptr;
u32 identifier;
u32 secure;
compat_uptr_t taskid;
struct fmt_pmqos pmqos_param;
};
#endif
struct gce_cmdq_task {
struct gce_cmdq_obj cmdq_buff;
struct cmdq_pkt *pkt_ptr;
u32 identifier;
u32 used;
};
enum gce_cmd_id {
CMD_READ = 0, /* read register */
CMD_WRITE, /* write register */
/* polling register until get some value (no timeout, blocking wait) */
CMD_POLL_REG,
CMD_WAIT_EVENT, /* gce wait HW done event & clear */
CMD_CLEAR_EVENT, /* gce clear HW done event */
CMD_WRITE_FD, /* write file descriptor */
CMD_MAX
};
enum gce_event_id {
FMT_RDMA0_SW_RST_DONE_ENG,
FMT_RDMA0_TILE_DONE,
FMT_WDMA0_SW_RST_DONE_ENG,
FMT_WDMA0_TILE_DONE,
FMT_RDMA1_SW_RST_DONE_ENG,
FMT_RDMA1_TILE_DONE,
FMT_WDMA1_SW_RST_DONE_ENG,
FMT_WDMA1_TILE_DONE,
};
enum fmt_gce_status {
GCE_NONE,
GCE_NORMAL,
GCE_SECURE
};
struct mtk_vdec_fmt {
dev_t fmt_devno;
struct cdev *fmt_cdev;
struct class *fmt_class;
struct device *fmt_device;
struct device *dev;
const char *fmtname;
struct cmdq_base *clt_base;
struct cmdq_client *clt_fmt[FMT_CORE_NUM];
struct cmdq_client *clt_fmt_sec[FMT_CORE_NUM];
int gce_th_num;
u16 gce_codec_eid[GCE_EVENT_MAX];
struct gce_cmds *gce_cmds[FMT_CORE_NUM];
struct map_hw_reg map_base[FMT_MAP_HW_REG_NUM];
u32 gce_gpr[FMT_CORE_NUM];
bool is_entering_suspend;
struct mutex mux_fmt;
struct mutex mux_gce_th[FMT_CORE_NUM];
struct mutex mux_task;
struct gce_cmdq_task gce_task[FMT_INST_MAX];
struct semaphore fmt_sem[FMT_CORE_NUM];
struct notifier_block pm_notifier;
struct clk *clk_VDEC;
enum fmt_gce_status gce_status[FMT_CORE_NUM];
atomic_t gce_job_cnt[FMT_CORE_NUM];
};
#define FMT_GCE_SET_CMD_FLUSH _IOW('f', 0, struct gce_cmdq_obj)
#define FMT_GCE_WAIT_CALLBACK _IOW('f', 1, unsigned int)
#ifdef CONFIG_COMPAT
#define COMPAT_FMT_GCE_SET_CMD_FLUSH _IOW('f', 0, struct compat_gce_cmdq_obj)
#endif
#endif /* _MTK_FMT_H */
@@ -0,0 +1,174 @@
/* SPDX-License-Identifier: GPL-2.0 */
/*
* Copyright (C) 2016 MediaTek Inc.
*/
#include "vdec_fmt_ion.h"
#include "vdec_fmt_utils.h"
#include <ion_drv.h>
#ifdef CONFIG_MTK_IOMMU_V2
#include "mach/mt_iommu.h"
#include "mach/pseudo_m4u.h"
#include <soc/mediatek/smi.h>
#include "mtk_iommu_ext.h"
#endif
#if defined(CONFIG_MTK_IOMMU_V2)
static struct ion_client *g_fmt_ion_client;
#endif
int fmt_ion_get_iova(struct ion_handle *handle,
u64 *iova, int port)
{
#if defined(CONFIG_MTK_IOMMU_V2)
struct ion_mm_data mm_data;
size_t iova_size;
memset((void *)&mm_data, 0, sizeof(struct ion_mm_data));
mm_data.mm_cmd = ION_MM_GET_IOVA;
mm_data.get_phys_param.module_id = port;
mm_data.get_phys_param.kernel_handle = handle;
if (ion_kernel_ioctl(g_fmt_ion_client, ION_CMD_MULTIMEDIA,
(unsigned long)&mm_data) < 0) {
fmt_err("get iova failed.%p -%p\n",
g_fmt_ion_client, handle);
ion_free(g_fmt_ion_client, handle);
return -1;
}
*iova = mm_data.get_phys_param.phy_addr;
iova_size = mm_data.get_phys_param.len;
if (*iova == 0)
fmt_err("alloc mmu addr hnd=0x%p,iova=0x%08lx\n",
handle, *iova);
fmt_debug(1, "iova: %llu", *iova);
#endif
return 0;
}
struct ion_handle *fmt_ion_import_handle(int fd)
{
struct ion_handle *handle = NULL;
#if defined(CONFIG_MTK_IOMMU_V2)
/* If no need Ion support, do nothing! */
if (fd <= 0) {
fmt_err("NO NEED ion support, fd %d\n", fd);
return handle;
}
if (!g_fmt_ion_client) {
fmt_err("invalid ion client!");
return handle;
}
handle = ion_import_dma_buf_fd(g_fmt_ion_client, fd);
if (IS_ERR(handle)) {
fmt_err("import ion handle failed!");
return NULL;
}
fmt_debug(1, "import ion handle fd=%d, hnd=0x%p", fd, handle);
#endif
return handle;
}
void fmt_ion_free_handle(struct ion_handle *handle)
{
#if defined(CONFIG_MTK_IOMMU_V2)
if (!g_fmt_ion_client) {
fmt_err("invalid ion client!");
return;
}
if (!handle)
return;
ion_free(g_fmt_ion_client, handle);
fmt_debug(1, "handle 0x%p", handle);
#endif
}
void fmt_ion_cache_flush(struct ion_handle *handle)
{
#if defined(CONFIG_MTK_IOMMU_V2)
struct ion_sys_data sys_data;
void *buffer_va;
if (!g_fmt_ion_client || !handle)
return;
sys_data.sys_cmd = ION_SYS_CACHE_SYNC;
sys_data.cache_sync_param.kernel_handle = handle;
sys_data.cache_sync_param.sync_type = ION_CACHE_INVALID_BY_RANGE;
buffer_va = ion_map_kernel(g_fmt_ion_client, handle);
sys_data.cache_sync_param.va = buffer_va;
sys_data.cache_sync_param.size = handle->buffer->size;
if (ion_kernel_ioctl(g_fmt_ion_client, ION_CMD_SYSTEM,
(unsigned long)&sys_data))
fmt_err("ion cache flush failed!");
ion_unmap_kernel(g_fmt_ion_client, handle);
#endif
}
void fmt_ion_create(const char *name)
{
#if defined(CONFIG_MTK_IOMMU_V2)
if (g_ion_device)
g_fmt_ion_client = ion_client_create(g_ion_device, name);
else
fmt_err("invalid g_ion_device");
if (!g_fmt_ion_client)
fmt_err("create ion client failed!");
#endif
}
void fmt_ion_destroy(void)
{
#if defined(CONFIG_MTK_IOMMU_V2)
if (g_fmt_ion_client && g_ion_device)
ion_client_destroy(g_fmt_ion_client);
#endif
}
u64 fmt_translate_fd(u64 fd, u32 offset, struct ionmap map[])
{
int i;
struct ion_handle *ion_h;
u64 iova = 0;
for (i = 0; i < FMT_FD_RESERVE; i++) {
if (fd == map[i].fd) {
fmt_debug(1, "quick search iova 0x%x",
map[i].iova + offset);
return map[i].iova + offset;
}
}
/* need to map ion handle and iova */
ion_h = fmt_ion_import_handle(fd);
if (!ion_h)
return 0;
fmt_ion_get_iova(ion_h, &iova, M4U_PORT_L4_MINI_MDP_R0_EXT);
for (i = 0; i < FMT_FD_RESERVE; i++) {
if (map[i].fd == -1) {
map[i].fd = fd;
map[i].iova = iova;
break;
}
}
iova += offset;
fmt_debug(1, "iova 0x%x", iova);
return iova;
}
@@ -0,0 +1,30 @@
/* SPDX-License-Identifier: GPL-2.0 */
/*
* Copyright (C) 2016 MediaTek Inc.
*/
#ifndef __VDEC_FMT_ION_H__
#define __VDEC_FMT_ION_H__
#ifdef CONFIG_MTK_IOMMU_V2
#include "mach/mt_iommu.h"
#include <soc/mediatek/smi.h>
#endif
#include <ion_priv.h>
#define FMT_FD_RESERVE 3
struct ionmap {
int fd;
u64 iova;
};
void fmt_ion_create(const char *name);
void fmt_ion_destroy(void);
int fmt_ion_get_iova(struct ion_handle *handle,
u64 *iova, int port);
struct ion_handle *fmt_ion_import_handle(int fd);
void fmt_ion_free_handle(struct ion_handle *handle);
void fmt_ion_cache_flush(struct ion_handle *handle);
u64 fmt_translate_fd(u64 fd, u32 offset, struct ionmap map[]);
#endif /*__VDEC_FMT_ION_H__*/
@@ -0,0 +1,457 @@
/* SPDX-License-Identifier: GPL-2.0 */
/*
* Copyright (C) 2016 MediaTek Inc.
*/
#include <linux/file.h>
#include <linux/fs.h>
#include <linux/uaccess.h>
#include <linux/slab.h>
#include <linux/sync_file.h>
#include <linux/miscdevice.h>
/*
* FMT SYNC validation framework
*
* A sync object driver that uses a 32bit counter to coordinate
* synchronization. Useful when there is no hardware primitive backing
* the synchronization.
*
* To start the framework just open:
*
* dev/fmt_sync
*
* That will create a sync timeline, all fences created under this timeline
* file descriptor will belong to the this timeline.
*
* The 'fmt_sync' file can be opened many times as to create different
* timelines.
*
* Fences can be created with FMT_SYNC_IOC_CREATE_FENCE ioctl with struct
* fmt_sync_ioctl_create_fence as parameter.
*
* To increment the timeline counter, FMT_SYNC_IOC_INC ioctl should be used
* with the increment as u32. This will update the last signaled value
* from the timeline and signal any fence that has a seqno smaller or equal
* to it.
*
* struct fmt_sync_ioctl_create_fence
* @value: the seqno to initialise the fence with
* @name: the name of the new sync point
* @fence: return the fd of the new sync_file with the created fence
*/
struct fmt_sync_create_fence_data {
__u32 value;
char name[32];
__s32 dma_fence; /* fd of new fence */
};
#define FMT_SYNC_IOC_MAGIC 'f'
#define FMT_SYNC_IOC_CREATE_FENCE _IOWR(FMT_SYNC_IOC_MAGIC, 0,\
struct fmt_sync_create_fence_data)
#define FMT_SYNC_IOC_INC _IOW(FMT_SYNC_IOC_MAGIC, 1, __u32)
/**
* struct sync_timeline - sync object
* @kref: reference count on fence.
* @name: name of the sync_timeline. Useful for debugging
* @lock: lock protecting @pt_list and @value
* @pt_tree: rbtree of active (unsignaled/errored) sync_pts
* @pt_list: list of active (unsignaled/errored) sync_pts
* @sync_timeline_list: membership in global sync_timeline_list
*/
struct sync_timeline {
struct kref kref;
char name[32];
/* protected by lock */
u64 context;
int value;
struct rb_root pt_tree;
struct list_head pt_list;
spinlock_t lock;
struct list_head sync_timeline_list;
};
static inline struct sync_timeline *fence_parent(struct dma_fence *dma_fence)
{
return container_of(dma_fence->lock, struct sync_timeline, lock);
}
/**
* struct sync_pt - sync_pt object
* @base: base dma_fence object
* @link: link on the sync timeline's list
* @node: node in the sync timeline's tree
*/
struct sync_pt {
struct dma_fence base;
struct list_head link;
struct rb_node node;
};
static const struct dma_fence_ops timeline_dma_fence_ops;
static inline struct sync_pt *fence_to_sync_pt(struct dma_fence *dma_fence)
{
if (dma_fence->ops != &timeline_dma_fence_ops)
return NULL;
return container_of(dma_fence, struct sync_pt, base);
}
/**
* fmt_timeline_create() - creates a sync object
* @name: sync_timeline name
*
* Creates a new sync_timeline. Returns the sync_timeline object or NULL in
* case of error.
*/
struct sync_timeline *fmt_timeline_create(const char *name)
{
struct sync_timeline *obj;
obj = kzalloc(sizeof(*obj), GFP_KERNEL);
if (!obj)
return NULL;
kref_init(&obj->kref);
obj->context = dma_fence_context_alloc(1);
strlcpy(obj->name, name, sizeof(obj->name));
obj->pt_tree = RB_ROOT;
INIT_LIST_HEAD(&obj->pt_list);
spin_lock_init(&obj->lock);
//sync_timeline_debug_add(obj);
return obj;
}
static void sync_timeline_free(struct kref *kref)
{
struct sync_timeline *obj =
container_of(kref, struct sync_timeline, kref);
//sync_timeline_debug_remove(obj);
kfree(obj);
}
static void sync_timeline_get(struct sync_timeline *obj)
{
kref_get(&obj->kref);
}
static void sync_timeline_put(struct sync_timeline *obj)
{
kref_put(&obj->kref, sync_timeline_free);
}
static const char *timeline_fence_get_driver_name(struct dma_fence *dma_fence)
{
return "fmt_sync";
}
static const char *timeline_fence_get_timeline_name(struct dma_fence *dma_fence)
{
struct sync_timeline *parent = fence_parent(dma_fence);
return parent->name;
}
static void timeline_fence_release(struct dma_fence *dma_fence)
{
struct sync_pt *pt = fence_to_sync_pt(dma_fence);
struct sync_timeline *parent = fence_parent(dma_fence);
if (pt != NULL && !list_empty(&pt->link)) {
unsigned long flags;
spin_lock_irqsave(dma_fence->lock, flags);
if (!list_empty(&pt->link)) {
list_del(&pt->link);
rb_erase(&pt->node, &parent->pt_tree);
}
spin_unlock_irqrestore(dma_fence->lock, flags);
}
sync_timeline_put(parent);
dma_fence_free(dma_fence);
}
static bool timeline_fence_signaled(struct dma_fence *dma_fence)
{
struct sync_timeline *parent = fence_parent(dma_fence);
return !__dma_fence_is_later(dma_fence->seqno, parent->value);
}
static bool timeline_fence_enable_signaling(struct dma_fence *dma_fence)
{
return true;
}
static void timeline_fence_value_str(struct dma_fence *dma_fence,
char *str, int size)
{
snprintf(str, size, "%d", dma_fence->seqno);
}
static void timeline_fence_timeline_value_str(struct dma_fence *dma_fence,
char *str, int size)
{
struct sync_timeline *parent = fence_parent(dma_fence);
snprintf(str, size, "%d", parent->value);
}
static const struct dma_fence_ops timeline_dma_fence_ops = {
.get_driver_name = timeline_fence_get_driver_name,
.get_timeline_name = timeline_fence_get_timeline_name,
.enable_signaling = timeline_fence_enable_signaling,
.signaled = timeline_fence_signaled,
.wait = dma_fence_default_wait,
.release = timeline_fence_release,
.fence_value_str = timeline_fence_value_str,
.timeline_value_str = timeline_fence_timeline_value_str,
};
/**
* sync_timeline_signal() - signal a status change on a sync_timeline
* @obj: sync_timeline to signal
* @inc: num to increment on timeline->value
*
* A sync implementation should call this any time one of it's fences
* has signaled or has an error condition.
*/
static void sync_timeline_signal(struct sync_timeline *obj, unsigned int inc)
{
struct sync_pt *pt, *next;
//trace_sync_timeline(obj);
spin_lock_irq(&obj->lock);
obj->value += inc;
list_for_each_entry_safe(pt, next, &obj->pt_list, link) {
if (!timeline_fence_signaled(&pt->base))
break;
list_del_init(&pt->link);
rb_erase(&pt->node, &obj->pt_tree);
/*
* A signal callback may release the last reference to this
* dma_fence, causing it to be freed. That operation has to be
* last to avoid a use after free inside this loop, and must
* be after we remove the dma_fence from the timeline in order to
* prevent deadlocking on timeline->lock inside
* timeline_fence_release().
*/
dma_fence_signal_locked(&pt->base);
}
spin_unlock_irq(&obj->lock);
}
/**
* sync_pt_create() - creates a sync pt
* @parent: dma_fence's parent sync_timeline
* @inc: value of the dma_fence
*
* Creates a new sync_pt as a child of @parent. @size bytes will be
* allocated allowing for implementation specific data to be kept after
* the generic sync_timeline struct. Returns the sync_pt object or
* NULL in case of error.
*/
static struct sync_pt *sync_pt_create(struct sync_timeline *obj,
unsigned int value)
{
struct sync_pt *pt;
pt = kzalloc(sizeof(*pt), GFP_KERNEL);
if (!pt)
return NULL;
sync_timeline_get(obj);
dma_fence_init(&pt->base, &timeline_dma_fence_ops, &obj->lock,
obj->context, value);
INIT_LIST_HEAD(&pt->link);
spin_lock_irq(&obj->lock);
if (!dma_fence_is_signaled_locked(&pt->base)) {
struct rb_node **p = &obj->pt_tree.rb_node;
struct rb_node *parent = NULL;
while (*p) {
struct sync_pt *other;
int cmp;
parent = *p;
other = rb_entry(parent, typeof(*pt), node);
cmp = value - other->base.seqno;
if (cmp > 0) {
p = &parent->rb_right;
} else if (cmp < 0) {
p = &parent->rb_left;
} else {
if (dma_fence_get_rcu(&other->base)) {
dma_fence_put(&pt->base);
pt = other;
goto unlock;
}
p = &parent->rb_left;
}
}
rb_link_node(&pt->node, parent, p);
rb_insert_color(&pt->node, &obj->pt_tree);
parent = rb_next(&pt->node);
list_add_tail(&pt->link,
parent ? &rb_entry(parent, typeof(*pt),
node)->link : &obj->pt_list);
}
unlock:
spin_unlock_irq(&obj->lock);
return pt;
}
/*
* *WARNING*
*
* improper use of this can result in deadlocking kernel drivers from userspace.
*/
/* opening fmt_sync create a new sync obj */
static int fmt_sync_open(struct inode *inode, struct file *file)
{
struct sync_timeline *obj;
char task_comm[TASK_COMM_LEN];
get_task_comm(task_comm, current);
obj = fmt_timeline_create(task_comm);
if (!obj)
return -ENOMEM;
file->private_data = obj;
return 0;
}
static int fmt_sync_release(struct inode *inode, struct file *file)
{
struct sync_timeline *obj = file->private_data;
smp_wmb();/*memory barrier*/
sync_timeline_put(obj);
return 0;
}
static long fmt_sync_ioctl_create_fence(struct sync_timeline *obj,
unsigned long arg)
{
int fd = get_unused_fd_flags(O_CLOEXEC);
int err;
struct sync_pt *pt;
struct sync_file *sync_file;
struct fmt_sync_create_fence_data data;
if (fd < 0)
return fd;
if (copy_from_user(&data, (void __user *)arg, sizeof(data))) {
err = -EFAULT;
goto err;
}
pt = sync_pt_create(obj, data.value);
if (!pt) {
err = -ENOMEM;
goto err;
}
sync_file = sync_file_create(&pt->base);
dma_fence_put(&pt->base);
if (!sync_file) {
err = -ENOMEM;
goto err;
}
data.dma_fence = fd;
if (copy_to_user((void __user *)arg, &data, sizeof(data))) {
fput(sync_file->file);
err = -EFAULT;
goto err;
}
fd_install(fd, sync_file->file);
return 0;
err:
put_unused_fd(fd);
return err;
}
static long fmt_sync_ioctl_inc(struct sync_timeline *obj, unsigned long arg)
{
u32 value = 0, mod = 0, i = 0, loop = 0;
if (copy_from_user(&value, (void __user *)arg, sizeof(value)))
return -EFAULT;
if (value > INT_MAX) {
loop = value/INT_MAX;
mod = value%INT_MAX;
for (i = 0; i < loop; i++)
sync_timeline_signal(obj, INT_MAX);
value = mod;
}
sync_timeline_signal(obj, value);
return 0;
}
static long fmt_sync_ioctl(struct file *file, unsigned int cmd,
unsigned long arg)
{
struct sync_timeline *obj = file->private_data;
switch (cmd) {
case FMT_SYNC_IOC_CREATE_FENCE:
return fmt_sync_ioctl_create_fence(obj, arg);
case FMT_SYNC_IOC_INC:
return fmt_sync_ioctl_inc(obj, arg);
default:
return -ENOTTY;
}
}
const struct file_operations fmt_sync_fops = {
.open = fmt_sync_open,
.release = fmt_sync_release,
.unlocked_ioctl = fmt_sync_ioctl,
.compat_ioctl = fmt_sync_ioctl,
};
static struct miscdevice fmt_sync_dev = {
.minor = MISC_DYNAMIC_MINOR,
.name = "fmt_sync",
.fops = &fmt_sync_fops,
};
static int __init fmt_sync_device_init(void)
{
return misc_register(&fmt_sync_dev);
}
device_initcall(fmt_sync_device_init);
@@ -0,0 +1,53 @@
/* SPDX-License-Identifier: GPL-2.0 */
/*
* Copyright (C) 2016 MediaTek Inc.
*/
#ifndef MTK_FMT_UTILS_H
#define MTK_FMT_UTILS_H
extern int fmt_dbg_level;
#define fmt_debug(level, format, args...) \
do { \
if ((fmt_dbg_level & level) == level) \
pr_info("[VDEC-FMT] level=%d %s(),%d: " format "\n",\
level, __func__, __LINE__, ##args); \
} while (0)
#define fmt_err(format, args...) \
pr_info("[VDEC-FMT][ERROR] %s:%d: " format "\n", __func__, __LINE__, \
##args)
#define FMT_TIMER_GET_DURATION_IN_US(start, end, duration) \
do { \
u64 time1; \
u64 time2; \
\
time1 = (u64)(start.tv_sec) * 1000000 + \
(u64)(start.tv_usec); \
time2 = (u64)(end.tv_sec) * 1000000 + \
(u64)(end.tv_usec); \
\
duration = (s32)(time2 - time1); \
\
if (duration <= 0) \
duration = 1; \
} while (0)
#define FMT_BANDWIDTH(data, pixel, throughput, bandwidth) \
do { \
u64 numerator; \
u64 denominator; \
\
/* ocucpied bw efficiency is 1.33 while accessing DRAM */\
numerator = \
(u64)(div_u64((u64)(data) * 4 * (u64)(throughput), 3));\
denominator = (u64)(pixel); \
if (denominator == 0) \
denominator = 1; \
bandwidth = (u32)(div_u64(numerator, denominator));\
} while (0)
#define FMT_GET32(addr) (readl((void *)addr) & 0xFFFFFFFF)
#endif /* _MTK_FMT_UTILS_H */