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

+51
View File
@@ -0,0 +1,51 @@
config MTK_GPU_SUPPORT
tristate "Support MTK GPU"
help
config supports MTK GPU driver
CONFIG_MTK_GPU_SUPPORT defines
The gpu driver would be built-in(y), as kernel module(m) or not supported(n)
MTK_GPU_VERSION would define the target HW IP
config MTK_GPU_VERSION
string "Support MTK GPU Driver Version"
default ""
help
CONFIG_MTK_GPU_VERSION define which gpu ddk version will be used
It has three parts
1. vendor name
2. GPU architecture name
3. driver version
config MTK_GPU_COMMON_DVFS_SUPPORT
bool "Support GPU VSync-Based DVFS Policy"
default n
help
config supports to enable GPU DVFS policy
This enables MTK intenral GPU DVFS governor
It is nice method to save power
It is recommended to enable it when MTK_GPU_SUPPORT is enabled
config SW_SYNC64
bool "Enable SW 64bit sync counter"
depends on SW_SYNC
help
A sync object driver that uses a 64bit counter to coordinate
syncrhronization. Useful when there is no hardware
primitive backing the synchronization.
config MTK_GED_SUPPORT
bool "Support MTK GED"
default n
choice
prompt "GPU_VERSION"
optional
config GPU_VERSION_MALI_MIDGARD_R26P0
bool "MIDGARD_R26P0"
select MALI_MIDGARD
config GPU_VERSION_IMG_M1_10ED5173305
bool "IMG_M1_10ED5173305"
endchoice
+50
View File
@@ -0,0 +1,50 @@
#
# Copyright (C) 2015 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 the
# GNU General Public License for more details.
#
MTK_GPU_VERSION := $(subst ",,$(CONFIG_MTK_GPU_VERSION))
export MTK_GPU_VERSION
MTK_PLATFORM := $(subst ",,$(CONFIG_MTK_PLATFORM))
export MTK_PLATFORM
ifneq ($(CONFIG_MTK_GPU_SUPPORT),)
obj-y += hal/
obj-y += gpufreq/
obj-y += ged/
ifneq (, $(findstring $(CONFIG_MTK_PLATFORM), "mt6779" "mt6785"))
obj-y += gpu_bm/
$(info *GPU defined GPU BM QoS 2.0-419)
endif
ifneq (, $(findstring $(CONFIG_MTK_PLATFORM), "mt6885" "mt6873" "mt6853"))
obj-y += gpu_bm_k414/
$(info *GPU defined GPU BM QoS 2.0-414)
endif
# GPU R5P0
ifeq ($(CONFIG_MTK_GPU_R5P0),y)
obj-y += $(subst ",,$(CONFIG_MTK_PLATFORM))/
else ifeq ($(CONFIG_MACH_MT6781),y)
$(info *direct build mali-r32p1 for CONFIG_MACH_MT6781)
obj-y += gpu_mali/mali_valhall/mali-r32p1/drivers/gpu/arm/midgard/
else ifneq ($(word 1,$(MTK_GPU_VERSION)),)
obj-y += gpu_$(word 1,$(MTK_GPU_VERSION))/
else
obj-y += $(subst ",,$(CONFIG_MTK_PLATFORM))/
endif
endif
ifeq ($(CONFIG_GPU_VERSION_MALI_MIDGARD_R26P0),y)
ifneq ($(CONFIG_MTK_GPU_SUPPORT),m)
obj-y += hal/
obj-y += gpu_mali/
endif
endif
+613
View File
@@ -0,0 +1,613 @@
#
# Copyright (C) 2015 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 the
# GNU General Public License for more details.
#
GED_TOP = $(srctree)/drivers/gpu/mediatek/ged
MTK_TOP = $(srctree)/drivers/misc/mediatek/
GPU_TOP = $(srctree)/drivers/gpu/mediatek/
subdir-ccflags-y += -Wno-error
#
# mt6781 config
#
ifeq ($(CONFIG_MACH_MT6781),y)
ccflags-y += -DMTK_GPUFREQ_V1
#ccflags-y += -DGED_KPI_MET_DEBUG
ccflags-y += -DMTK_GED_KPI
ccflags-y += -DGED_KPI_CPU_BOOST
ifeq ($(CONFIG_MTK_GPU_COMMON_DVFS_SUPPORT),y)
ccflags-y += -DGED_ENABLE_FB_DVFS
ccflags-y += -DGED_ENABLE_DYNAMIC_DVFS_MARGIN
ccflags-y += -DGED_CONFIGURE_LOADING_BASE_DVFS_STEP
ccflags-y += -DGED_ENABLE_TIMER_BASED_DVFS_MARGIN
ccflags-y += -DGED_ENABLE_DVFS_LOADING_MODE
endif
ccflags-y += -I$(srctree)/drivers/misc/mediatek/cmdq/v3
ccflags-y += -I$(srctree)/drivers/misc/mediatek/cmdq/v3/mt6781
ccflags-y += \
-I$(MTK_TOP)/base/power/mt6781/ \
-I$(srctree)/drivers/misc/mediatek/video/mt6781/videox \
-I$(srctree)/drivers/misc/mediatek/video/mt6781/dispsys \
-I$(srctree)/drivers/misc/mediatek/sspm/mt6781
endif
$(info $$GED: CONFIG_MTK_PLATFORM is [${CONFIG_MTK_PLATFORM}])
#
# mt6853 family config (mt6833)
#
ifneq (,$(filter $(CONFIG_MTK_PLATFORM), "mt6853"))
ccflags-y += -DMTK_GPUFREQ_V1
#ccflags-y += -DGED_KPI_MET_DEBUG
ccflags-y += -DMTK_GED_KPI
ccflags-y += -DGED_KPI_CPU_BOOST
$(info $$GED: CONFIG_MTK_PLATFORM is defined [${CONFIG_MTK_PLATFORM}])
ifeq ($(CONFIG_MTK_GPU_COMMON_DVFS_SUPPORT),y)
ccflags-y += -DGED_ENABLE_FB_DVFS
ccflags-y += -DGED_ENABLE_DYNAMIC_DVFS_MARGIN
ccflags-y += -DGED_CONFIGURE_LOADING_BASE_DVFS_STEP
ccflags-y += -DGED_ENABLE_TIMER_BASED_DVFS_MARGIN
ccflags-y += -DGED_ENABLE_DVFS_LOADING_MODE
$(info $$GED: CONFIG_MTK_GPU_COMMON_DVFS_SUPPORT is defined [${CONFIG_MTK_GPU_COMMON_DVFS_SUPPORT}])
endif
ccflags-y += -I$(srctree)/drivers/misc/mediatek/cmdq/v3
ifeq ($(CONFIG_MACH_MT6833),y)
ccflags-y += -I$(srctree)/drivers/misc/mediatek/cmdq/v3/mt6833
ccflags-y += \
-I$(MTK_TOP)/base/power/mt6833/ \
-I$(srctree)/drivers/misc/mediatek/video/mt6833/videox \
-I$(srctree)/drivers/misc/mediatek/video/mt6833/dispsys \
-I$(srctree)/drivers/misc/mediatek/sspm/mt6833
endif
ifeq ($(CONFIG_MACH_MT6877),y)
ccflags-y += -I$(srctree)/drivers/misc/mediatek/cmdq/v3/mt6877
ccflags-y += \
-I$(MTK_TOP)/base/power/mt6877/ \
-I$(srctree)/drivers/misc/mediatek/video/mt6877/videox \
-I$(srctree)/drivers/misc/mediatek/video/mt6877/dispsys \
-I$(srctree)/drivers/misc/mediatek/sspm/mt6877
endif
ccflags-y += -I$(srctree)/drivers/misc/mediatek/cmdq/v3/$(subst ",,$(CONFIG_MTK_PLATFORM))
ccflags-y += \
-I$(MTK_TOP)/base/power/$(subst ",,$(CONFIG_MTK_PLATFORM))/ \
-I$(srctree)/drivers/misc/mediatek/video/$(subst ",,$(CONFIG_MTK_PLATFORM))/videox \
-I$(srctree)/drivers/misc/mediatek/video/$(subst ",,$(CONFIG_MTK_PLATFORM))/dispsys \
-I$(srctree)/drivers/misc/mediatek/sspm/$(subst ",,$(CONFIG_MTK_PLATFORM))
endif
#
# mt6885 family config (mt6893)
#
ifneq (,$(filter $(CONFIG_MTK_PLATFORM), "mt6885"))
ccflags-y += -DMTK_GPUFREQ_V1
#ccflags-y += -DGED_KPI_MET_DEBUG
ccflags-y += -DMTK_GED_KPI
ccflags-y += -DGED_KPI_CPU_BOOST
ifeq ($(CONFIG_MTK_GPU_COMMON_DVFS_SUPPORT),y)
ccflags-y += -DGED_ENABLE_FB_DVFS
ccflags-y += -DGED_ENABLE_DYNAMIC_DVFS_MARGIN
ccflags-y += -DGED_CONFIGURE_LOADING_BASE_DVFS_STEP
ccflags-y += -DGED_ENABLE_TIMER_BASED_DVFS_MARGIN
ccflags-y += -DGED_ENABLE_DVFS_LOADING_MODE
endif
ccflags-y += -I$(srctree)/drivers/misc/mediatek/cmdq/v3
ifeq ($(CONFIG_MACH_MT6893),y)
ccflags-y += -I$(srctree)/drivers/misc/mediatek/cmdq/v3/mt6893
ccflags-y += \
-I$(MTK_TOP)/base/power/mt6893/ \
-I$(srctree)/drivers/misc/mediatek/video/mt6893/videox \
-I$(srctree)/drivers/misc/mediatek/video/mt6893/dispsys \
-I$(srctree)/drivers/misc/mediatek/sspm/mt6893
endif
ccflags-y += -I$(srctree)/drivers/misc/mediatek/cmdq/v3/$(subst ",,$(CONFIG_MTK_PLATFORM))
ccflags-y += \
-I$(MTK_TOP)/base/power/$(subst ",,$(CONFIG_MTK_PLATFORM))/ \
-I$(srctree)/drivers/misc/mediatek/video/$(subst ",,$(CONFIG_MTK_PLATFORM))/videox \
-I$(srctree)/drivers/misc/mediatek/video/$(subst ",,$(CONFIG_MTK_PLATFORM))/dispsys \
-I$(srctree)/drivers/misc/mediatek/sspm/$(subst ",,$(CONFIG_MTK_PLATFORM))
endif
#
# mt6873 config
#
ifneq (,$(filter $(CONFIG_MTK_PLATFORM), "mt6873"))
ccflags-y += -DMTK_GPUFREQ_V1
#ccflags-y += -DGED_KPI_MET_DEBUG
ccflags-y += -DMTK_GED_KPI
ccflags-y += -DGED_KPI_CPU_BOOST
ifeq ($(CONFIG_MTK_GPU_COMMON_DVFS_SUPPORT),y)
ccflags-y += -DGED_ENABLE_FB_DVFS
ccflags-y += -DGED_ENABLE_DYNAMIC_DVFS_MARGIN
ccflags-y += -DGED_CONFIGURE_LOADING_BASE_DVFS_STEP
ccflags-y += -DGED_ENABLE_TIMER_BASED_DVFS_MARGIN
ccflags-y += -DGED_ENABLE_DVFS_LOADING_MODE
endif
ccflags-y += -I$(srctree)/drivers/misc/mediatek/cmdq/v3
ccflags-y += -I$(srctree)/drivers/misc/mediatek/cmdq/v3/$(subst ",,$(CONFIG_MTK_PLATFORM))
ccflags-y += \
-I$(MTK_TOP)/base/power/$(subst ",,$(CONFIG_MTK_PLATFORM))/ \
-I$(srctree)/drivers/misc/mediatek/video/$(subst ",,$(CONFIG_MTK_PLATFORM))/videox \
-I$(srctree)/drivers/misc/mediatek/video/$(subst ",,$(CONFIG_MTK_PLATFORM))/dispsys \
-I$(srctree)/drivers/misc/mediatek/sspm/$(subst ",,$(CONFIG_MTK_PLATFORM))
endif
#
# mt6785 config
#
ifeq ($(CONFIG_MACH_MT6785),y)
#ccflags-y += -DGED_KPI_MET_DEBUG
ccflags-y += -DMTK_GED_KPI
ccflags-y += -DGED_KPI_CPU_BOOST
ifeq ($(CONFIG_MTK_GPU_COMMON_DVFS_SUPPORT),y)
ccflags-y += -DGED_ENABLE_FB_DVFS
ccflags-y += -DGED_ENABLE_DYNAMIC_DVFS_MARGIN
ccflags-y += -DGED_CONFIGURE_LOADING_BASE_DVFS_STEP
ccflags-y += -DGED_ENABLE_TIMER_BASED_DVFS_MARGIN
endif
ccflags-y += -I$(srctree)/drivers/misc/mediatek/cmdq/v3
ccflags-y += -I$(srctree)/drivers/misc/mediatek/cmdq/v3/$(subst ",,$(CONFIG_MTK_PLATFORM))
ccflags-y += \
-I$(MTK_TOP)/base/power/$(subst ",,$(CONFIG_MTK_PLATFORM))/ \
-I$(srctree)/drivers/misc/mediatek/video/$(subst ",,$(CONFIG_MTK_PLATFORM))/videox \
-I$(srctree)/drivers/misc/mediatek/video/$(subst ",,$(CONFIG_MTK_PLATFORM))/dispsys \
-I$(srctree)/drivers/misc/mediatek/sspm/$(subst ",,$(CONFIG_MTK_PLATFORM))
endif
#
# mt6768 config
#
ifneq (,$(filter $(CONFIG_MTK_PLATFORM), "mt6768"))
#ccflags-y += -DGED_KPI_MET_DEBUG
ccflags-y += -DMTK_GED_KPI
ccflags-y += -DGED_KPI_CPU_BOOST
ifeq ($(CONFIG_MTK_GPU_COMMON_DVFS_SUPPORT),y)
ccflags-y += -DGED_ENABLE_FB_DVFS
ccflags-y += -DGED_ENABLE_DYNAMIC_DVFS_MARGIN
ccflags-y += -DGED_CONFIGURE_LOADING_BASE_DVFS_STEP
ccflags-y += -DGED_ENABLE_TIMER_BASED_DVFS_MARGIN
endif
#ccflags-y += -DCONFIG_MTK_QOS_V1_SUPPORT
ccflags-y += -I$(srctree)/drivers/misc/mediatek/cmdq/v3
ccflags-y += -I$(srctree)/drivers/misc/mediatek/cmdq/v3/$(subst ",,$(CONFIG_MTK_PLATFORM))
ccflags-y += -I$(srctree)/drivers/gpu/mediatek/gpufreq/$(subst ",,$(CONFIG_MTK_PLATFORM))
ccflags-y += \
-I$(MTK_TOP)/base/power/$(subst ",,$(CONFIG_MTK_PLATFORM))/ \
-I$(srctree)/drivers/misc/mediatek/video/$(subst ",,$(CONFIG_MTK_PLATFORM))/videox \
-I$(srctree)/drivers/misc/mediatek/video/$(subst ",,$(CONFIG_MTK_PLATFORM))/dispsys \
-I$(srctree)/drivers/misc/mediatek/sspm/$(subst ",,$(CONFIG_MTK_PLATFORM))
endif
#
# mt6771 config
#
ifneq (,$(filter $(CONFIG_MTK_PLATFORM), "mt6771"))
#ccflags-y += -DGED_KPI_MET_DEBUG
ccflags-y += -DMTK_GED_KPI
ccflags-y += -DGED_KPI_CPU_BOOST
ifeq ($(CONFIG_MTK_GPU_COMMON_DVFS_SUPPORT),y)
ccflags-y += -DGED_ENABLE_FB_DVFS
ccflags-y += -DGED_ENABLE_DYNAMIC_DVFS_MARGIN
ccflags-y += -DGED_CONFIGURE_LOADING_BASE_DVFS_STEP
ccflags-y += -DGED_ENABLE_TIMER_BASED_DVFS_MARGIN
endif
#ccflags-y += -DGED_KPI_CPU_INFO
#ccflags-y += -DGED_KPI_DFRC
#ccflags-y += -DCONFIG_MTK_QOS_V1_SUPPORT
ccflags-y += -I$(srctree)/drivers/misc/mediatek/cmdq/v3
ccflags-y += -I$(srctree)/drivers/misc/mediatek/cmdq/v3/$(subst ",,$(CONFIG_MTK_PLATFORM))
ccflags-y += -I$(srctree)/drivers/gpu/mediatek/gpufreq/$(subst ",,$(CONFIG_MTK_PLATFORM))
ccflags-y += \
-I$(MTK_TOP)/base/power/$(subst ",,$(CONFIG_MTK_PLATFORM))/ \
-I$(srctree)/drivers/misc/mediatek/video/$(subst ",,$(CONFIG_MTK_PLATFORM))/videox \
-I$(srctree)/drivers/misc/mediatek/video/$(subst ",,$(CONFIG_MTK_PLATFORM))/dispsys \
-I$(srctree)/drivers/misc/mediatek/sspm/$(subst ",,$(CONFIG_MTK_PLATFORM))
endif
#
# mt6763 config
#
ifneq (,$(filter $(CONFIG_MTK_PLATFORM), "mt6763"))
#ccflags-y += -DGED_KPI_MET_DEBUG
ccflags-y += -DMTK_GED_KPI
ccflags-y += -DGED_KPI_CPU_BOOST
ifeq ($(CONFIG_MTK_GPU_COMMON_DVFS_SUPPORT),y)
ccflags-y += -DGED_ENABLE_FB_DVFS
endif
#ccflags-y += -DGED_KPI_DFRC
ccflags-y += -I$(srctree)/drivers/misc/mediatek/cmdq/v3
ccflags-y += -I$(srctree)/drivers/misc/mediatek/cmdq/v3/$(subst ",,$(CONFIG_MTK_PLATFORM))
ccflags-y += \
-I$(MTK_TOP)/base/power/$(subst ",,$(CONFIG_MTK_PLATFORM))/ \
-I$(srctree)/drivers/misc/mediatek/video/$(subst ",,$(CONFIG_MTK_PLATFORM))/videox \
-I$(srctree)/drivers/misc/mediatek/video/$(subst ",,$(CONFIG_MTK_PLATFORM))/dispsys \
-I$(srctree)/drivers/misc/mediatek/sspm/$(subst ",,$(CONFIG_MTK_PLATFORM))
endif
#
# mt6739 config
#
ifneq (,$(filter $(CONFIG_MTK_PLATFORM), "mt6739"))
#ccflags-y += -DGED_KPI_MET_DEBUG
ccflags-y += -DMTK_GED_KPI
ccflags-y += -DGED_KPI_CPU_BOOST
ifeq ($(CONFIG_MTK_GPU_COMMON_DVFS_SUPPORT),y)
ccflags-y += -DGED_ENABLE_FB_DVFS
endif
#ccflags-y += -DGED_KPI_CPU_SINGLE_CLUSTER
ccflags-y += -I$(srctree)/drivers/misc/mediatek/cmdq/v3
ccflags-y += -I$(srctree)/drivers/misc/mediatek/cmdq/v3/$(subst ",,$(CONFIG_MTK_PLATFORM))
ccflags-y += \
-I$(MTK_TOP)/base/power/$(subst ",,$(CONFIG_MTK_PLATFORM))/ \
-I$(srctree)/drivers/misc/mediatek/video/$(subst ",,$(CONFIG_MTK_PLATFORM))/videox \
-I$(srctree)/drivers/misc/mediatek/video/$(subst ",,$(CONFIG_MTK_PLATFORM))/dispsys \
-I$(srctree)/drivers/misc/mediatek/sspm/$(subst ",,$(CONFIG_MTK_PLATFORM))
endif
#
# mt6775 config
#
ifneq (,$(filter $(CONFIG_MTK_PLATFORM), "mt6775"))
#ccflags-y += -DGED_KPI_MET_DEBUG
ccflags-y += -DMTK_GED_KPI
ccflags-y += -DGED_KPI_CPU_BOOST
ifeq ($(CONFIG_MTK_GPU_COMMON_DVFS_SUPPORT),y)
ccflags-y += -DGED_ENABLE_FB_DVFS
endif
#ccflags-y += -DGED_KPI_CPU_INFO
#ccflags-y += -DGED_KPI_DFRC
ccflags-y += -I$(srctree)/drivers/misc/mediatek/cmdq/v3
ccflags-y += -I$(srctree)/drivers/misc/mediatek/cmdq/v3/$(subst ",,$(CONFIG_MTK_PLATFORM))
ccflags-y += \
-I$(MTK_TOP)/base/power/$(subst ",,$(CONFIG_MTK_PLATFORM))/ \
-I$(srctree)/drivers/misc/mediatek/video/$(subst ",,$(CONFIG_MTK_PLATFORM))/videox \
-I$(srctree)/drivers/misc/mediatek/video/$(subst ",,$(CONFIG_MTK_PLATFORM))/dispsys \
-I$(srctree)/drivers/misc/mediatek/sspm/$(subst ",,$(CONFIG_MTK_PLATFORM))
endif
#
# mt6799 config
#
ifneq (,$(filter $(CONFIG_MTK_PLATFORM), "mt6799"))
#ccflags-y += -DGED_KPI_MET_DEBUG
ccflags-y += -DMTK_GED_KPI
ccflags-y += -DGED_KPI_CPU_BOOST
#ccflags-y += -DGED_KPI_DFRC
#ccflags-y += -DGED_KPI_CPU_TRI_CLUSTER
ccflags-y += -I$(srctree)/drivers/misc/mediatek/cmdq/v3
ccflags-y += -I$(srctree)/drivers/misc/mediatek/cmdq/v3/$(subst ",,$(CONFIG_MTK_PLATFORM))
ccflags-y += \
-I$(MTK_TOP)/base/power/$(subst ",,$(CONFIG_MTK_PLATFORM))/ \
-I$(srctree)/drivers/misc/mediatek/video/$(subst ",,$(CONFIG_MTK_PLATFORM))/videox \
-I$(srctree)/drivers/misc/mediatek/video/$(subst ",,$(CONFIG_MTK_PLATFORM))/dispsys \
-I$(srctree)/drivers/misc/mediatek/sspm/$(subst ",,$(CONFIG_MTK_PLATFORM))
endif
#
# mt6759 config
#
ifneq (,$(filter $(CONFIG_MTK_PLATFORM), "mt6759"))
#ccflags-y += -DGED_KPI_MET_DEBUG
ccflags-y += -DMTK_GED_KPI
ccflags-y += -DGED_KPI_CPU_BOOST
#ccflags-y += -DGED_KPI_DFRC
#ccflags-y += -DGED_KPI_CPU_TRI_CLUSTER
ccflags-y += -I$(srctree)/drivers/misc/mediatek/cmdq/v3
ccflags-y += -I$(srctree)/drivers/misc/mediatek/cmdq/v3/$(subst ",,$(CONFIG_MTK_PLATFORM))
ccflags-y += \
-I$(MTK_TOP)/base/power/$(subst ",,$(CONFIG_MTK_PLATFORM))/ \
-I$(srctree)/drivers/misc/mediatek/video/$(subst ",,$(CONFIG_MTK_PLATFORM))/videox \
-I$(srctree)/drivers/misc/mediatek/video/$(subst ",,$(CONFIG_MTK_PLATFORM))/dispsys \
-I$(srctree)/drivers/misc/mediatek/sspm/$(subst ",,$(CONFIG_MTK_PLATFORM))
endif
#
# mt6758 config
#
ifneq (,$(filter $(CONFIG_MTK_PLATFORM), "mt6758"))
#ccflags-y += -DGED_KPI_MET_DEBUG
ccflags-y += -DMTK_GED_KPI
ccflags-y += -DGED_KPI_CPU_BOOST
ccflags-y += -I$(srctree)/drivers/misc/mediatek/cmdq/v3
ccflags-y += -I$(srctree)/drivers/misc/mediatek/cmdq/v3/$(subst ",,$(CONFIG_MTK_PLATFORM))
ccflags-y += \
-I$(MTK_TOP)/base/power/$(subst ",,$(CONFIG_MTK_PLATFORM))/ \
-I$(srctree)/drivers/misc/mediatek/video/$(subst ",,$(CONFIG_MTK_PLATFORM))/videox \
-I$(srctree)/drivers/misc/mediatek/video/$(subst ",,$(CONFIG_MTK_PLATFORM))/dispsys \
-I$(srctree)/drivers/misc/mediatek/sspm/$(subst ",,$(CONFIG_MTK_PLATFORM))
endif
#
# mt8168 config
#
ifneq (,$(filter $(CONFIG_MTK_PLATFORM), "mt8168"))
#ccflags-y += -DGED_KPI_MET_DEBUG
ccflags-y += -DMTK_GED_KPI
ccflags-y += -DGED_KPI_CPU_BOOST
ifeq ($(CONFIG_MTK_GPU_COMMON_DVFS_SUPPORT),y)
ccflags-y += -DGED_ENABLE_FB_DVFS
endif
ccflags-y += -I$(srctree)/drivers/misc/mediatek/cmdq/v3
ccflags-y += -I$(srctree)/drivers/misc/mediatek/cmdq/v3/$(subst ",,$(CONFIG_MTK_PLATFORM))
ccflags-y += \
-I$(MTK_TOP)/base/power/$(subst ",,$(CONFIG_MTK_PLATFORM))/ \
-I$(srctree)/drivers/misc/mediatek/video/$(subst ",,$(CONFIG_MTK_PLATFORM))/videox \
-I$(srctree)/drivers/misc/mediatek/video/$(subst ",,$(CONFIG_MTK_PLATFORM))/dispsys \
-I$(srctree)/drivers/misc/mediatek/sspm/$(subst ",,$(CONFIG_MTK_PLATFORM))
endif
#
# mt3967 config
#
ifneq (,$(filter $(CONFIG_MTK_PLATFORM), "mt3967"))
#ccflags-y += -DGED_KPI_MET_DEBUG
ccflags-y += -DMTK_GED_KPI
ccflags-y += -DGED_KPI_CPU_BOOST
ccflags-y += -I$(srctree)/drivers/misc/mediatek/cmdq/v3
ccflags-y += -I$(srctree)/drivers/misc/mediatek/cmdq/v3/$(subst ",,$(CONFIG_MTK_PLATFORM))
ccflags-y += \
-I$(MTK_TOP)/base/power/$(subst ",,$(CONFIG_MTK_PLATFORM))/ \
-I$(srctree)/drivers/misc/mediatek/video/$(subst ",,$(CONFIG_MTK_PLATFORM))/videox \
-I$(srctree)/drivers/misc/mediatek/video/$(subst ",,$(CONFIG_MTK_PLATFORM))/dispsys \
-I$(srctree)/drivers/misc/mediatek/sspm/$(subst ",,$(CONFIG_MTK_PLATFORM))
endif
#
# mt6757 config
#
ifneq (,$(filter $(CONFIG_MTK_PLATFORM), "mt6757"))
ccflags-y += \
-I$(srctree)/drivers/misc/mediatek/cmdq/v2 \
-I$(srctree)/drivers/misc/mediatek/cmdq/v2/$(subst ",,$(CONFIG_MTK_PLATFORM))
ccflags-y += \
-I$(MTK_TOP)/base/power/$(subst ",,$(CONFIG_MTK_PLATFORM))/ \
-I$(srctree)/drivers/misc/mediatek/video/$(subst ",,$(CONFIG_MTK_PLATFORM))/videox \
-I$(srctree)/drivers/misc/mediatek/video/$(subst ",,$(CONFIG_MTK_PLATFORM))/dispsys \
-I$(srctree)/drivers/misc/mediatek/sspm/$(subst ",,$(CONFIG_MTK_PLATFORM))
endif
#
# mt8167 config
#
ifneq (,$(filter $(CONFIG_MTK_PLATFORM), "mt8167"))
ccflags-y += \
-I$(srctree)/drivers/misc/mediatek/cmdq/v2 \
-I$(srctree)/drivers/misc/mediatek/cmdq/v2/$(subst ",,$(CONFIG_MTK_PLATFORM))
ccflags-y += \
-I$(MTK_TOP)/base/power/$(subst ",,$(CONFIG_MTK_PLATFORM))/ \
-I$(srctree)/drivers/misc/mediatek/video/$(subst ",,$(CONFIG_MTK_PLATFORM))/videox \
-I$(srctree)/drivers/misc/mediatek/video/$(subst ",,$(CONFIG_MTK_PLATFORM))/dispsys \
-I$(srctree)/drivers/misc/mediatek/sspm/$(subst ",,$(CONFIG_MTK_PLATFORM))
endif
#
# mt6761 config
#
ifneq (,$(filter $(CONFIG_MTK_PLATFORM), "mt6761"))
#ccflags-y += -DGED_KPI_MET_DEBUG
ccflags-y += -DMTK_GED_KPI
ccflags-y += -DGED_KPI_CPU_BOOST
ifeq ($(CONFIG_MTK_GPU_COMMON_DVFS_SUPPORT),y)
ccflags-y += -DGED_ENABLE_FB_DVFS
ccflags-y += -DGED_ENABLE_DYNAMIC_DVFS_MARGIN
ccflags-y += -DGED_CONFIGURE_LOADING_BASE_DVFS_STEP
ccflags-y += -DGED_ENABLE_TIMER_BASED_DVFS_MARGIN
endif
ccflags-y += -I$(srctree)/drivers/misc/mediatek/cmdq/v3
ccflags-y += -I$(srctree)/drivers/misc/mediatek/cmdq/v3/$(subst ",,$(CONFIG_MTK_PLATFORM))
ccflags-y += \
-I$(MTK_TOP)/base/power/$(subst ",,$(CONFIG_MTK_PLATFORM))/ \
-I$(srctree)/drivers/misc/mediatek/video/$(subst ",,$(CONFIG_MTK_PLATFORM))/videox \
-I$(srctree)/drivers/misc/mediatek/video/$(subst ",,$(CONFIG_MTK_PLATFORM))/dispsys \
-I$(srctree)/drivers/misc/mediatek/sspm/$(subst ",,$(CONFIG_MTK_PLATFORM))
endif
#
# mt6765 config
#
ifneq (,$(filter $(CONFIG_MTK_PLATFORM), "mt6765"))
#ccflags-y += -DGED_KPI_MET_DEBUG
ccflags-y += -DMTK_GED_KPI
ccflags-y += -DGED_KPI_CPU_BOOST
ifeq ($(CONFIG_MTK_GPU_COMMON_DVFS_SUPPORT),y)
ccflags-y += -DGED_ENABLE_FB_DVFS
ccflags-y += -DGED_ENABLE_DYNAMIC_DVFS_MARGIN
ccflags-y += -DGED_CONFIGURE_LOADING_BASE_DVFS_STEP
ccflags-y += -DGED_ENABLE_TIMER_BASED_DVFS_MARGIN
endif
ccflags-y += -I$(srctree)/drivers/misc/mediatek/cmdq/v3
ccflags-y += -I$(srctree)/drivers/misc/mediatek/cmdq/v3/$(subst ",,$(CONFIG_MTK_PLATFORM))
ccflags-y += \
-I$(MTK_TOP)/base/power/$(subst ",,$(CONFIG_MTK_PLATFORM))/ \
-I$(srctree)/drivers/misc/mediatek/video/$(subst ",,$(CONFIG_MTK_PLATFORM))/videox \
-I$(srctree)/drivers/misc/mediatek/video/$(subst ",,$(CONFIG_MTK_PLATFORM))/dispsys \
-I$(srctree)/drivers/misc/mediatek/sspm/$(subst ",,$(CONFIG_MTK_PLATFORM))
endif
#
# mt6779 config
#
ifneq (,$(filter $(CONFIG_MTK_PLATFORM), "mt6779"))
#ccflags-y += -DGED_KPI_MET_DEBUG
ccflags-y += -DMTK_GED_KPI
ccflags-y += -DGED_KPI_CPU_BOOST
ifeq ($(CONFIG_MTK_GPU_COMMON_DVFS_SUPPORT),y)
ccflags-y += -DGED_ENABLE_FB_DVFS
ccflags-y += -DGED_ENABLE_DYNAMIC_DVFS_MARGIN
ccflags-y += -DGED_CONFIGURE_LOADING_BASE_DVFS_STEP
ccflags-y += -DGED_ENABLE_TIMER_BASED_DVFS_MARGIN
ccflags-y += -DGED_MODULE_LATE_INIT
endif
ccflags-y += -I$(srctree)/drivers/misc/mediatek/cmdq/v3
ccflags-y += -I$(srctree)/drivers/misc/mediatek/cmdq/v3/$(subst ",,$(CONFIG_MTK_PLATFORM))
ccflags-y += \
-I$(MTK_TOP)/base/power/$(subst ",,$(CONFIG_MTK_PLATFORM))/ \
-I$(srctree)/drivers/misc/mediatek/video/$(subst ",,$(CONFIG_MTK_PLATFORM))/videox \
-I$(srctree)/drivers/misc/mediatek/video/$(subst ",,$(CONFIG_MTK_PLATFORM))/dispsys \
-I$(srctree)/drivers/misc/mediatek/sspm/$(subst ",,$(CONFIG_MTK_PLATFORM))
endif
#
# kiboplus config
#
ifneq (,$(filter $(CONFIG_MTK_PLATFORM), "kiboplus"))
ccflags-y += \
-I$(srctree)/drivers/misc/mediatek/cmdq/v2 \
-I$(srctree)/drivers/misc/mediatek/cmdq/v2/$(subst ",,$(CONFIG_MTK_PLATFORM))
ccflags-y += \
-I$(MTK_TOP)/base/power/$(subst ",,$(CONFIG_MTK_PLATFORM))/ \
-I$(srctree)/drivers/misc/mediatek/video/$(subst ",,$(CONFIG_MTK_PLATFORM))/videox \
-I$(srctree)/drivers/misc/mediatek/video/$(subst ",,$(CONFIG_MTK_PLATFORM))/dispsys \
-I$(srctree)/drivers/misc/mediatek/sspm/$(subst ",,$(CONFIG_MTK_PLATFORM))
endif
#
# common config
#
ccflags-y += -DMET_USER_EVENT_SUPPORT -DMTK_GPU_DVFS
ccflags-y += -DGED_DVFS_DEBUG_BUF
ccflags-y += -DCONFIG_MTK_GPU_OPP_STATS_SUPPORT
# ccflags-y += -DGED_KPI_DEBUG
VER := eng
ifneq ($(CONFIG_MTK_ENG_BUILD),y)
VER := user
ifeq ($(CONFIG_MTK_ENABLE_GMO), y)
ccflags-y += -DGED_LOG_SIZE_LIMITED
ccflags-y += -DGED_BUFFER_LOG_DISABLE
endif
endif
ifeq ($(VER),eng)
ccflags-y += -DGED_DEBUG_IOCTL_LOCK
endif
ifeq ($(CONFIG_MTK_FTRACE_DEFAULT_ENABLE),y)
ccflags-y += -DENABLE_GED_SYSTRACE_UTIL
endif
ifeq ($(CONFIG_MTK_TINYSYS_SSPM_SUPPORT),y)
#ccflags-y += -DGED_SSPM
endif
#$(info $$CONFIG_MTK_GPU_COMMON_DVFS_SUPPORT is [${CONFIG_MTK_GPU_COMMON_DVFS_SUPPORT}])
ifneq ($(CONFIG_FPGA_EARLY_PORTING), y)
ifeq ($(CONFIG_MTK_GPU_COMMON_DVFS_SUPPORT),y)
ccflags-y += -DENABLE_COMMON_DVFS \
-DENABLE_TIMER_BACKUP \
-DGED_DVFS_ENABLE
else
ccflags-y += -DGED_FDVFS_ENABLE
endif
else
$(info $[GED]CONFIG_FPGA_EARLY_PORTING is [${CONFIG_FPGA_EARLY_PORTING}])
endif
ccflags-y += \
-include $(GED_TOP)/include/config_kernel_$(VER).h
ccflags-y += \
-I$(srctree)/include/ \
-I$(MTK_TOP)/include/ \
-I$(MTK_TOP)/include/mt-plat/ \
-I$(GED_TOP)/include/ \
-I$(GPU_TOP)/hal/ \
-I$(GPU_TOP)/ \
-I$(MTK_TOP)/base/power/include/ \
-I$(srctree)/drivers/misc/mediatek/video/include \
-I$(srctree)/drivers/misc/mediatek/lcm/inc/ \
-I$(srctree)/drivers/misc/mediatek/dfrc/ \
-I$(srctree)/drivers/gpu/mediatek/gpufreq/ \
-I$(srctree)/drivers/misc/mediatek/sspm/
obj-$(CONFIG_MTK_GPU_SUPPORT) += ged.o
ifeq ($(CONFIG_DEBUG_FS),y)
ccflags-y += -DGED_DEBUG_FS
ged-y += \
src/ged_debugFS.o
endif
ged-y += \
src/ged.o \
src/ged_base.o \
src/ged_main.o \
src/ged_sysfs.o \
src/ged_hal.o \
src/ged_log.o \
src/ged_bridge.o \
src/ged_monitor_3D_fence.o \
src/ged_notify_sw_vsync.o \
src/ged_hashtable.o \
src/ged_thread.o \
src/ged_ge.o \
src/ged_dvfs.o \
src/ged_gpu_tuner.o \
src/ged_kpi.o
@@ -0,0 +1,6 @@
/* SPDX-License-Identifier: GPL-2.0 */
/*
* Copyright (C) 2015 MediaTek Inc.
*/
#define GED_DEBUG
@@ -0,0 +1,4 @@
/* SPDX-License-Identifier: GPL-2.0 */
/*
* Copyright (c) 2019 MediaTek Inc.
*/
+23
View File
@@ -0,0 +1,23 @@
/* SPDX-License-Identifier: GPL-2.0 */
/*
* Copyright (c) 2019 MediaTek Inc.
*/
#ifndef __GED_H__
#define __GED_H__
#include "ged_type.h"
#define GED_NOTIFICATION_TYPE int
#define GED_NOTIFICATION_TYPE_SW_VSYNC 0
#define GED_NOTIFICATION_TYPE_HW_VSYNC_PRIMARY_DISPLAY 1
void ged_notification(GED_NOTIFICATION_TYPE eType);
int ged_set_target_fps(unsigned int target_fps, int mode);
unsigned int ged_get_cur_fps(void);
void ged_get_latest_perf_state(long long *t_cpu_remained,
long long *t_gpu_remained,
long *t_cpu_target,
long *t_gpu_target);
#endif
@@ -0,0 +1,43 @@
/* SPDX-License-Identifier: GPL-2.0 */
/*
* Copyright (c) 2019 MediaTek Inc.
*/
#ifndef __GED_BASE_H__
#define __GED_BASE_H__
#include <linux/compiler.h>
#include "ged_type.h"
#define GED_TAG "[GPU/GED]"
#ifdef GED_DEBUG
#define GED_LOGD(...) pr_debug(GED_TAG"[DEBUG]" __VA_ARGS__)
#else
#define GED_LOGD(...)
#endif
#define GED_LOGI(...) pr_info(GED_TAG"[INFO]" __VA_ARGS__)
#define GED_LOGE(...) pr_err(GED_TAG"[ERROR]" __VA_ARGS__)
#define GED_CONTAINER_OF(ptr, type, member) \
((type *)(((char *)ptr) - offsetof(type, member)))
unsigned long ged_copy_to_user(void __user *pvTo,
const void *pvFrom, unsigned long ulBytes);
unsigned long ged_copy_from_user(void *pvTo,
const void __user *pvFrom, unsigned long ulBytes);
void *ged_alloc(int i32Size);
void *ged_alloc_atomic(int i32Size);
void ged_free(void *pvBuf, int i32Size);
long ged_get_pid(void);
unsigned long long ged_get_time(void);
struct GED_FILE_PRIVATE_BASE {
void (*free_func)(void *f);
};
#endif
@@ -0,0 +1,106 @@
/* SPDX-License-Identifier: GPL-2.0 */
/*
* Copyright (c) 2019 MediaTek Inc.
*/
#ifndef __GED_BRIDGE_H__
#define __GED_BRIDGE_H__
#include "ged_base.h"
#include "ged_log.h"
#include "ged_type.h"
#include "ged_bridge_id.h"
/*****************************************************************************
* BRIDGE FUNCTIONS
*****************************************************************************/
int ged_bridge_log_buf_get(
struct GED_BRIDGE_IN_LOGBUFGET *psLogBufGetIN,
struct GED_BRIDGE_OUT_LOGBUFGET *psLogBufGetOUT);
int ged_bridge_log_buf_write(
struct GED_BRIDGE_IN_LOGBUFWRITE *psLogBufWriteIN,
struct GED_BRIDGE_OUT_LOGBUFWRITE *psLogBufWriteOUT);
int ged_bridge_log_buf_reset(
struct GED_BRIDGE_IN_LOGBUFRESET *psLogBufResetIn,
struct GED_BRIDGE_OUT_LOGBUFRESET *psLogBufResetOUT);
int ged_bridge_boost_gpu_freq(
struct GED_BRIDGE_IN_BOOSTGPUFREQ *psBoostGpuFreqIN,
struct GED_BRIDGE_OUT_BOOSTGPUFREQ *psBoostGpuFreqOUT);
int ged_bridge_monitor_3D_fence(
struct GED_BRIDGE_IN_MONITOR3DFENCE *psMonitor3DFenceINT,
struct GED_BRIDGE_OUT_MONITOR3DFENCE *psMonitor3DFenceOUT);
int ged_bridge_query_info(
struct GED_BRIDGE_IN_QUERY_INFO *psQueryInfoINT,
struct GED_BRIDGE_OUT_QUERY_INFO *psQueryInfoOUT);
int ged_bridge_notify_vsync(
struct GED_BRIDGE_IN_NOTIFY_VSYNC *psNotifyVsyncINT,
struct GED_BRIDGE_OUT_NOTIFY_VSYNC *psNotifyVsyncOUT);
int ged_bridge_dvfs_probe(
struct GED_BRIDGE_IN_DVFS_PROBE *psDVFSProbeINT,
struct GED_BRIDGE_OUT_DVFS_PROBE *psDVFSProbeOUT);
int ged_bridge_dvfs_um_retrun(
struct GED_BRIDGE_IN_DVFS_UM_RETURN *psDVFS_UM_returnINT,
struct GED_BRIDGE_OUT_DVFS_UM_RETURN *psDVFS_UM_returnOUT);
int ged_bridge_event_notify(
struct GED_BRIDGE_IN_EVENT_NOTIFY *psEVENT_NOTIFYINT,
struct GED_BRIDGE_OUT_EVENT_NOTIFY *psEVENT_NOTIFYOUT);
int ged_bridge_gpu_hint_to_cpu(
struct GED_BRIDGE_IN_GPU_HINT_TO_CPU *in,
struct GED_BRIDGE_OUT_GPU_HINT_TO_CPU *out);
int ged_bridge_hint_force_mdp(
struct GED_BRIDGE_IN_HINT_FORCE_MDP *psHintForceMdpIn,
struct GED_BRIDGE_OUT_HINT_FORCE_MDP *psHintForceMdpOut);
int ged_bridge_gpu_timestamp(
struct GED_BRIDGE_IN_GPU_TIMESTAMP *psGpuBeginINT,
struct GED_BRIDGE_OUT_GPU_TIMESTAMP *psGpuBeginOUT);
int ged_bridge_query_dvfs_freq_pred(
struct GED_BRIDGE_IN_QUERY_DVFS_FREQ_PRED *QueryDVFSFreqPredIn,
struct GED_BRIDGE_OUT_QUERY_DVFS_FREQ_PRED *QueryDVFSFreqPredOut);
int ged_bridge_query_gpu_dvfs_info(
struct GED_BRIDGE_IN_QUERY_GPU_DVFS_INFO *QueryGPUDVFSInfoIn,
struct GED_BRIDGE_OUT_QUERY_GPU_DVFS_INFO *QueryGPUDVFSInfoOut);
int ged_bridge_ge_alloc(
struct GED_BRIDGE_IN_GE_ALLOC *psALLOC_IN,
struct GED_BRIDGE_OUT_GE_ALLOC *psALLOC_OUT);
int ged_bridge_ge_get(
struct GED_BRIDGE_IN_GE_GET *psGET_IN,
struct GED_BRIDGE_OUT_GE_GET *psGET_OUT,
int output_buffer_size);
int ged_bridge_ge_set(
struct GED_BRIDGE_IN_GE_SET *psSET_IN,
struct GED_BRIDGE_OUT_GE_SET *psSET_OUT,
int input_buffer_size);
int ged_bridge_ge_info(
struct GED_BRIDGE_IN_GE_INFO *psINFO_IN,
struct GED_BRIDGE_OUT_GE_INFO *psINFO_OUT);
int ged_bridge_gpu_tuner_status(
struct GED_BRIDGE_IN_GPU_TUNER_STATUS *in,
struct GED_BRIDGE_OUT_GPU_TUNER_STATUS *out);
#ifdef ENABLE_FRR_FOR_MT6XXX_PLATFORM
int ged_bridge_vsync_wait(void *IN, void *OUT);
#endif
#endif
@@ -0,0 +1,459 @@
/* SPDX-License-Identifier: GPL-2.0 */
/*
* Copyright (c) 2019 MediaTek Inc.
*/
#ifndef __GED_BRIDGE_ID_H__
#define __GED_BRIDGE_ID_H__
#include "ged_type.h"
struct GED_BRIDGE_PACKAGE {
uint32_t ui32FunctionID;
int32_t i32Size;
void *pvParamIn;
int32_t i32InBufferSize;
void *pvParamOut;
int32_t i32OutBufferSize;
};
struct GED_BRIDGE_PACKAGE_32 {
uint32_t ui32FunctionID;
int32_t i32Size;
uint32_t ui32ParamIn;
int32_t i32InBufferSize;
uint32_t ui32ParamOut;
int32_t i32OutBufferSize;
};
#define GPU_TUNER_BUF_NAME_LEN 128
/*****************************************************************************
* IOCTL values.
*****************************************************************************/
#define GED_MAGIC 'g'
#define GED_IO(INDEX) _IO(GED_MAGIC, INDEX, struct GED_BRIDGE_PACKAGE)
#define GED_IOW(INDEX) _IOW(GED_MAGIC, INDEX, struct GED_BRIDGE_PACKAGE)
#define GED_IOR(INDEX) _IOR(GED_MAGIC, INDEX, struct GED_BRIDGE_PACKAGE)
#define GED_IOWR(INDEX) _IOWR(GED_MAGIC, INDEX, struct GED_BRIDGE_PACKAGE)
#define GED_IOWR_32(INDEX) _IOWR(GED_MAGIC, INDEX, struct GED_BRIDGE_PACKAGE_32)
#define GED_GET_BRIDGE_ID(X) _IOC_NR(X)
/******************************************************************************
* IOCTL Commands
******************************************************************************/
#define GED_BRIDGE_COMMAND_LOG_BUF_GET 0
#define GED_BRIDGE_COMMAND_LOG_BUF_WRITE 1
#define GED_BRIDGE_COMMAND_LOG_BUF_RESET 2
#define GED_BRIDGE_COMMAND_BOOST_GPU_FREQ 3
#define GED_BRIDGE_COMMAND_MONITOR_3D_FENCE 4
#define GED_BRIDGE_COMMAND_QUERY_INFO 5
#define GED_BRIDGE_COMMAND_NOTIFY_VSYNC 6
#define GED_BRIDGE_COMMAND_DVFS_PROBE 7
#define GED_BRIDGE_COMMAND_DVFS_UM_RETURN 8
#define GED_BRIDGE_COMMAND_EVENT_NOTIFY 9
#define GED_BRIDGE_COMMAND_WAIT_HW_VSYNC 10
#define GED_BRIDGE_COMMAND_QUERY_TARGET_FPS 11
#define GED_BRIDGE_COMMAND_VSYNC_WAIT 12
#define GED_BRIDGE_COMMAND_GPU_HINT_TO_CPU 13
#define GED_BRIDGE_COMMAND_HINT_FORCE_MDP 14
#define GED_BRIDGE_COMMAND_QUERY_DVFS_FREQ_PRED 15
#define GED_BRIDGE_COMMAND_QUERY_GPU_DVFS_INFO 16
#define GED_BRIDGE_COMMAND_GE_ALLOC 100
#define GED_BRIDGE_COMMAND_GE_GET 101
#define GED_BRIDGE_COMMAND_GE_SET 102
#define GED_BRIDGE_COMMAND_GPU_TIMESTAMP 103
#define GED_BRIDGE_COMMAND_TARGET_FPS 104
#define GED_BRIDGE_COMMAND_GE_INFO 105
#define GED_BRIDGE_COMMAND_GPU_TUNER_STATUS 106
#define GED_BRIDGE_COMMAND_ID int
#define GED_BRIDGE_IO_LOG_BUF_GET \
GED_IOWR(GED_BRIDGE_COMMAND_LOG_BUF_GET)
#define GED_BRIDGE_IO_LOG_BUF_WRITE \
GED_IOWR(GED_BRIDGE_COMMAND_LOG_BUF_WRITE)
#define GED_BRIDGE_IO_LOG_BUF_RESET \
GED_IOWR(GED_BRIDGE_COMMAND_LOG_BUF_RESET)
#define GED_BRIDGE_IO_BOOST_GPU_FREQ \
GED_IOWR(GED_BRIDGE_COMMAND_BOOST_GPU_FREQ)
#define GED_BRIDGE_IO_MONITOR_3D_FENCE \
GED_IOWR(GED_BRIDGE_COMMAND_MONITOR_3D_FENCE)
#define GED_BRIDGE_IO_QUERY_INFO \
GED_IOWR(GED_BRIDGE_COMMAND_QUERY_INFO)
#define GED_BRIDGE_IO_NOTIFY_VSYNC \
GED_IOWR(GED_BRIDGE_COMMAND_NOTIFY_VSYNC)
#define GED_BRIDGE_IO_DVFS_PROBE \
GED_IOWR(GED_BRIDGE_COMMAND_DVFS_PROBE)
#define GED_BRIDGE_IO_DVFS_UM_RETURN \
GED_IOWR(GED_BRIDGE_COMMAND_DVFS_UM_RETURN)
#define GED_BRIDGE_IO_EVENT_NOTIFY \
GED_IOWR(GED_BRIDGE_COMMAND_EVENT_NOTIFY)
#define GED_BRIDGE_IO_WAIT_HW_VSYNC \
GED_IOWR(GED_BRIDGE_COMMAND_WAIT_HW_VSYNC)
#define GED_BRIDGE_IO_VSYNC_WAIT \
GED_IOWR(GED_BRIDGE_COMMAND_VSYNC_WAIT)
#define GED_BRIDGE_IO_GPU_HINT_TO_CPU \
GED_IOWR(GED_BRIDGE_COMMAND_GPU_HINT_TO_CPU)
#define GED_BRIDGE_IO_HINT_FORCE_MDP \
GED_IOWR(GED_BRIDGE_COMMAND_HINT_FORCE_MDP)
#define GED_BRIDGE_IO_QUERY_DVFS_FREQ_PRED \
GED_IOWR(GED_BRIDGE_COMMAND_QUERY_DVFS_FREQ_PRED)
#define GED_BRIDGE_IO_QUERY_GPU_DVFS_INFO \
GED_IOWR(GED_BRIDGE_COMMAND_QUERY_GPU_DVFS_INFO)
#define GED_BRIDGE_IO_GE_ALLOC \
GED_IOWR(GED_BRIDGE_COMMAND_GE_ALLOC)
#define GED_BRIDGE_IO_GE_GET \
GED_IOWR(GED_BRIDGE_COMMAND_GE_GET)
#define GED_BRIDGE_IO_GE_SET \
GED_IOWR(GED_BRIDGE_COMMAND_GE_SET)
#define GED_BRIDGE_IO_QUERY_TARGET_FPS \
GED_IOWR(GED_BRIDGE_COMMAND_QUERY_TARGET_FPS)
#define GED_BRIDGE_IO_GPU_TIMESTAMP \
GED_IOWR(GED_BRIDGE_COMMAND_GPU_TIMESTAMP)
#define GED_BRIDGE_IO_GE_INFO \
GED_IOWR(GED_BRIDGE_COMMAND_GE_INFO)
#define GED_BRIDGE_IO_GPU_TUNER_STATUS \
GED_IOWR(GED_BRIDGE_COMMAND_GPU_TUNER_STATUS)
/******************************************************************************
* LOG_BUF_GET
******************************************************************************/
#define GED_LOG_BUF_NAME_LENGTH 64
/* Bridge in structure for LOG_BUF_GET */
struct GED_BRIDGE_IN_LOGBUFGET {
char acName[GED_LOG_BUF_NAME_LENGTH];
};
/* Bridge out structure for LOG_BUF_GETC */
struct GED_BRIDGE_OUT_LOGBUFGET {
GED_ERROR eError;
GED_LOG_BUF_HANDLE hLogBuf;
};
/******************************************************************************
* LOG_BUF_WRITE
******************************************************************************/
/* Bridge in structure for LOG_BUF_WRITE */
struct GED_BRIDGE_IN_LOGBUFWRITE {
GED_LOG_BUF_HANDLE hLogBuf;
int attrs;
char acLogBuf[GED_BRIDGE_IN_LOGBUF_SIZE];
};
/* Bridge out structure for LOG_BUF_WRITE */
struct GED_BRIDGE_OUT_LOGBUFWRITE {
GED_ERROR eError;
};
/******************************************************************************
* LOG_BUF_RESET
******************************************************************************/
/* Bridge in structure for LOG_BUF_RESET */
struct GED_BRIDGE_IN_LOGBUFRESET {
GED_LOG_BUF_HANDLE hLogBuf;
};
/* Bridge out structure for LOG_BUF_RESET */
struct GED_BRIDGE_OUT_LOGBUFRESET {
GED_ERROR eError;
};
/******************************************************************************
* BOOST GPU FREQ
******************************************************************************/
/* Bridge in structure for LOG_BUF_WRITE */
struct GED_BRIDGE_IN_BOOSTGPUFREQ {
GED_BOOST_GPU_FREQ_LEVEL eGPUFreqLevel;
};
/* Bridge out structure for LOG_BUF_WRITE */
struct GED_BRIDGE_OUT_BOOSTGPUFREQ {
GED_ERROR eError;
};
/*****************************************************************************
* MONITOR 3D FENCE
*****************************************************************************/
/* Bridge in structure for MONITOR3DFENCE */
struct GED_BRIDGE_IN_MONITOR3DFENCE {
int fd;
};
/* Bridge out structure for MONITOR3DFENCE */
struct GED_BRIDGE_OUT_MONITOR3DFENCE {
GED_ERROR eError;
};
/*****************************************************************************
* QUERY INFO
*****************************************************************************/
/* Bridge in structure for QUERY INFO*/
struct GED_BRIDGE_IN_QUERY_INFO {
GED_INFO eType;
};
/* Bridge out structure for QUERY INFO*/
struct GED_BRIDGE_OUT_QUERY_INFO {
uint64_t retrieve;
};
/*****************************************************************************
* NOTIFY VSYNC
*****************************************************************************/
/* Bridge in structure for VSYNCEVENT */
struct GED_BRIDGE_IN_NOTIFY_VSYNC {
GED_VSYNC_TYPE eType;
};
/* Bridge out structure for VSYNCEVENT */
struct GED_BRIDGE_OUT_NOTIFY_VSYNC {
struct GED_DVFS_UM_QUERY_PACK sQueryData;
GED_ERROR eError;
};
/*****************************************************************************
* DVFS PROBE
*****************************************************************************/
/* Bridge in structure for DVFS_PROBE */
struct GED_BRIDGE_IN_DVFS_PROBE {
int pid;
};
/* Bridge out structure for DVFS_PROBE */
struct GED_BRIDGE_OUT_DVFS_PROBE {
GED_ERROR eError;
};
/*****************************************************************************
* DVFS UM RETURN
*****************************************************************************/
/* Bridge in structure for DVFS_UM_RETURN */
struct GED_BRIDGE_IN_DVFS_UM_RETURN {
uint64_t gpu_tar_freq;
bool bFallback;
};
/* Bridge out structure for DVFS_UM_RETURN */
struct GED_BRIDGE_OUT_DVFS_UM_RETURN {
GED_ERROR eError;
};
/*****************************************************************************
* EVENT NOTIFY
*****************************************************************************/
/* Bridge in structure for EVENT_NOTIFY */
struct GED_BRIDGE_IN_EVENT_NOTIFY {
GED_DVFS_VSYNC_OFFSET_SWITCH_CMD eEvent;
bool bSwitch;
};
/* Bridge out structure for EVENT_NOTIFY */
struct GED_BRIDGE_OUT_EVENT_NOTIFY {
GED_ERROR eError;
};
/*****************************************************************************
* WAIT HW VSync
*****************************************************************************/
/* Bridge in structure for creation */
struct GED_BRIDGE_IN_WAIT_HW_VSYNC {
int tid;
};
/* Bridge out structure for creation */
struct GED_BRIDGE_OUT_WAIT_HW_VSYNC {
GED_ERROR eError;
};
/*****************************************************************************
* GPU_TIMESTAMP
*****************************************************************************/
/* Bridge in structure for creation */
struct GED_BRIDGE_IN_GPU_TIMESTAMP {
int pid;
uint64_t ullWnd;
int32_t i32FrameID;
int fence_fd;
int QedBuffer_length;
int isSF;
};
/* Bridge out structure for creation */
struct GED_BRIDGE_OUT_GPU_TIMESTAMP {
GED_ERROR eError;
int is_ged_kpi_enabled;
};
/*****************************************************************************
* QUERY_TARGET_FPS (for FRR20)
*****************************************************************************/
struct GED_BRIDGE_IN_QUERY_TARGET_FPS {
int pid;
uint64_t cid;
int fenceFd;
};
struct GED_BRIDGE_OUT_QUERY_TARGET_FPS {
int fps;
};
/******************************************************************************
* BOOST GPU FREQ
******************************************************************************/
struct GED_BRIDGE_IN_GPU_HINT_TO_CPU {
int32_t i32BridgeFD;
int32_t tid;
int32_t hint;
};
struct GED_BRIDGE_OUT_GPU_HINT_TO_CPU {
GED_ERROR eError;
int32_t boost_flag; // 1:boost 0:not_boost
int32_t boost_value;
};
/******************************************************************************
* HINT VIDEO CODEC FORCE MDP
******************************************************************************/
struct GED_BRIDGE_IN_HINT_FORCE_MDP {
int32_t i32BridgeFD;
int32_t hint; /* 1: Do MDP, 0: No MDP, -1: No overwrite */
};
struct GED_BRIDGE_OUT_HINT_FORCE_MDP {
GED_ERROR eError;
int32_t mdp_flag; /* 1: Do MDP, 0: No MDP */
};
/******************************************************************************
* QEURY DVFS GPU_FREQ PREDICTION
******************************************************************************/
struct GED_BRIDGE_IN_QUERY_DVFS_FREQ_PRED {
int32_t pid;
int32_t hint;
};
/*****************************************************************************
* Hint frequency calculated by DVFS to MEOW
*****************************************************************************/
struct GED_BRIDGE_OUT_QUERY_DVFS_FREQ_PRED {
GED_ERROR eError;
int gpu_freq_cur;
int gpu_freq_max;
int gpu_freq_dvfs_pred;
};
/******************************************************************************
* QEURY DVFS GPU_FREQ PREDICTION
******************************************************************************/
struct GED_BRIDGE_IN_QUERY_GPU_DVFS_INFO {
int32_t pid;
int32_t hint;
int32_t gift_ratio;
};
/*****************************************************************************
* Hint DVFS related INFOs to MEOW
*****************************************************************************/
struct GED_BRIDGE_OUT_QUERY_GPU_DVFS_INFO {
GED_ERROR eError;
int gpu_freq_cur;
int gpu_freq_max;
int gpu_freq_dvfs_pred;
int target_fps;
int target_fps_margin;
int eara_fps_margin;
int gpu_time;
};
/*****************************************************************************
* GE - gralloc_extra functions
*****************************************************************************/
/* Bridge in structure for GE_ALLOC */
struct GED_BRIDGE_IN_GE_ALLOC {
int region_num;
uint32_t region_sizes[0];
};
/* Bridge out structure for GE_ALLOC */
struct GED_BRIDGE_OUT_GE_ALLOC {
int ge_fd;
GED_ERROR eError;
};
/* Bridge in structure for GE_GET */
struct GED_BRIDGE_IN_GE_GET {
int ge_fd;
int region_id;
/* Here uint32_* means that the unit is 32bit.
* For example: uint32_offset 1 => offset 4 bytes
*/
int uint32_offset;
int uint32_size;
};
/* Bridge out structure for GE_GET */
struct GED_BRIDGE_OUT_GE_GET {
GED_ERROR eError;
uint32_t data[0];
};
/* Bridge in structure for GE_SET */
struct GED_BRIDGE_IN_GE_SET {
int ge_fd;
int region_id;
/* Here uint32_* means that the unit is 32bit.
* For example: uint32_offset 1 => offset 4 bytes
*/
int uint32_offset;
int uint32_size;
uint32_t data[0];
};
/* Bridge out structure for GE_SET */
struct GED_BRIDGE_OUT_GE_SET {
GED_ERROR eError;
};
/* Bridge in structure for GE_INFO */
struct GED_BRIDGE_IN_GE_INFO {
int ge_fd;
};
/* Bridge out structure for GE_INFO */
struct GED_BRIDGE_OUT_GE_INFO {
uint64_t unique_id;
GED_ERROR eError;
};
/* Bridge in structure for GPU_TUNER_STATUS */
struct GED_BRIDGE_IN_GPU_TUNER_STATUS {
char name[GPU_TUNER_BUF_NAME_LEN];
};
/* Bridge out structure for GPU_TUNER_STATUS */
struct GED_BRIDGE_OUT_GPU_TUNER_STATUS {
int feature;
};
#endif
@@ -0,0 +1,36 @@
/* SPDX-License-Identifier: GPL-2.0 */
/*
* Copyright (c) 2019 MediaTek Inc.
*/
#ifndef __GED_DEBUG_FS_H__
#define __GED_DEBUG_FS_H__
#include <linux/seq_file.h>
#include "ged_type.h"
GED_ERROR ged_debugFS_create_entry(
const char *pszName,
void *pvDir,
const struct seq_operations *psReadOps,
ssize_t (*pfnWrite)(const char __user *pszBuffer, size_t uiCount,
loff_t uiPosition, void *pvData),
void *pvData,
struct dentry **ppsEntry);
void ged_debugFS_remove_entry(
struct dentry *psEntry);
GED_ERROR ged_debugFS_create_entry_dir(
const char *pszName,
struct dentry *psParentDir,
struct dentry **ppsDir);
void ged_debugFS_remove_entry_dir(
struct dentry *psDir);
GED_ERROR ged_debugFS_init(void);
void ged_debugFS_exit(void);
#endif
+219
View File
@@ -0,0 +1,219 @@
/* SPDX-License-Identifier: GPL-2.0 */
/*
* Copyright (c) 2019 MediaTek Inc.
*/
#ifndef __GED_DVFS_H__
#define __GED_DVFS_H__
#include <linux/types.h>
#include "ged_type.h"
#define GED_DVFS_UM_CAL 1
#define GED_DVFS_PROBE_TO_UM 1
#define GED_DVFS_PROBE_IN_KM 0
#define GED_NO_UM_SERVICE -1
#define GED_DVFS_VSYNC_OFFSET_SIGNAL_EVENT 44
#define GED_FPS_CHANGE_SIGNAL_EVENT 45
#define GED_SRV_SUICIDE_EVENT 46
#define GED_LOW_POWER_MODE_SIGNAL_EVENT 47
#define GED_MHL4K_VID_SIGNAL_EVENT 48
#define GED_GAS_SIGNAL_EVENT 49
#define GED_SIGNAL_BOOST_HOST_EVENT 50
#define GED_VILTE_VID_SIGNAL_EVENT 51
#define GED_LOW_LATENCY_MODE_SIGNAL_EVENT 52
/* GED_DVFS_DIFF_THRESHOLD (us) */
#define GED_DVFS_DIFF_THRESHOLD 500
#define GED_DVFS_TIMER_BACKUP 0x5566dead
#define GED_DVFS_DEFAULT_COMMIT 0
#define GED_DVFS_CUSTOM_CEIL_COMMIT 1
#define GED_DVFS_CUSTOM_BOOST_COMMIT 2
#define GED_DVFS_SET_BOTTOM_COMMIT 3
#define GED_DVFS_SET_LIMIT_COMMIT 4
#define GED_DVFS_INPUT_BOOST_COMMIT 5
#define GED_DVFS_COMMIT_TYPE int
#define GED_DVFS_DEFAULT 0
#define GED_DVFS_LP 1
#define GED_DVFS_JUST_MAKE 2
#define GED_DVFS_PERFORMANCE 3
#define GED_DVFS_TUNING_MODE int
#define GED_EVENT_TOUCH (1 << 0)
#define GED_EVENT_THERMAL (1 << 1)
#define GED_EVENT_WFD (1 << 2)
#define GED_EVENT_MHL (1 << 3)
#define GED_EVENT_GAS (1 << 4)
#define GED_EVENT_LOW_POWER_MODE (1 << 5)
#define GED_EVENT_MHL4K_VID (1 << 6)
#define GED_EVENT_BOOST_HOST (1 << 7)
#define GED_EVENT_VR (1 << 8)
#define GED_EVENT_VILTE_VID (1 << 9)
#define GED_EVENT_LCD (1 << 10)
#define GED_EVENT_LOW_LATENCY_MODE (1 << 13)
#define GED_EVENT_DHWC (1 << 14)
typedef void (*ged_event_change_fp)(void *private_data, int events);
bool mtk_register_ged_event_change(
const char *name, void (*pfn_callback)(void *private_data, int events),
void *private_data);
bool mtk_unregister_ged_event_change(const char *name);
void mtk_ged_event_notify(int events);
#define GED_EVENT_FORCE_ON (1 << 0)
#define GED_EVENT_FORCE_OFF (1 << 1)
#define GED_EVENT_NOT_SYNC (1 << 2)
#define GED_VSYNC_OFFSET_NOT_SYNC -2
#define GED_VSYNC_OFFSET_SYNC -3
struct GED_DVFS_FREQ_DATA {
unsigned int ui32Idx;
unsigned long ulFreq;
};
struct GED_DVFS_BW_DATA {
unsigned int ui32MaxBW;
unsigned int ui32AvgBW;
};
#define MAX_BW_PROFILE 5
#ifdef CONFIG_MTK_GPU_OPP_STATS_SUPPORT
/* unit 1us */
struct GED_DVFS_OPP_STAT {
union {
uint32_t *aTrans;
uint32_t ui32Freq;
} uMem;
uint64_t ui64Active;
uint64_t ui64Idle;
};
#endif /* CONFIG_MTK_GPU_OPP_STATS_SUPPORT */
#ifdef GED_ENABLE_DVFS_LOADING_MODE
struct GpuUtilization_Ex {
unsigned int util_active;
unsigned int util_3d;
unsigned int util_ta;
unsigned int util_compute;
};
bool ged_dvfs_cal_gpu_utilization_ex(unsigned int *pui32Loading,
unsigned int *pui32Block, unsigned int *pui32Idle,
struct GpuUtilization_Ex *Util_Ex);
#else
bool ged_dvfs_cal_gpu_utilization(unsigned int *pui32Loading,
unsigned int *pui32Block, unsigned int *pui32Idle);
#endif
void ged_dvfs_run(unsigned long t, long phase, unsigned long ul3DFenceDoneTime);
void ged_dvfs_set_tuning_mode(GED_DVFS_TUNING_MODE eMode);
GED_DVFS_TUNING_MODE ged_dvfs_get_tuning_mode(void);
GED_ERROR ged_dvfs_vsync_offset_event_switch(
GED_DVFS_VSYNC_OFFSET_SWITCH_CMD eEvent, bool bSwitch);
void ged_dvfs_vsync_offset_level_set(int i32level);
int ged_dvfs_vsync_offset_level_get(void);
unsigned int ged_dvfs_get_gpu_loading(void);
unsigned int ged_dvfs_get_gpu_blocking(void);
unsigned int ged_dvfs_get_gpu_idle(void);
unsigned long ged_query_info(GED_INFO eType);
void ged_dvfs_get_gpu_cur_freq(struct GED_DVFS_FREQ_DATA *psData);
void ged_dvfs_get_gpu_pre_freq(struct GED_DVFS_FREQ_DATA *psData);
void ged_dvfs_sw_vsync_query_data(struct GED_DVFS_UM_QUERY_PACK *psQueryData);
void ged_dvfs_boost_gpu_freq(void);
GED_ERROR ged_dvfs_probe(int pid);
GED_ERROR ged_dvfs_um_commit(unsigned long gpu_tar_freq, bool bFallback);
GED_ERROR ged_dvfs_probe_signal(int signo);
void ged_dvfs_gpu_clock_switch_notify(bool bSwitch);
#ifdef CONFIG_MTK_GPU_OPP_STATS_SUPPORT
void ged_dvfs_reset_opp_cost(int oppsize);
void ged_dvfs_update_opp_cost(unsigned int loading,
unsigned int TSDiff_us, unsigned long long cur_us, unsigned int idx);
#define OPP_STAT_DEINIT 0xdead5566
#define OPP_STAT_QUERY 0x0
int ged_dvfs_query_opp_cost(struct GED_DVFS_OPP_STAT *psReport,
int i32NumOpp, bool bStript);
int ged_dvfs_init_opp_cost(void);
#endif /* CONFIG_MTK_GPU_OPP_STATS_SUPPORT */
GED_ERROR ged_dvfs_system_init(void);
void ged_dvfs_system_exit(void);
unsigned long ged_dvfs_get_last_commit_idx(void);
extern void (*ged_kpi_set_gpu_dvfs_hint_fp)(int t_gpu_target,
int boost_accum_gpu);
#ifdef GED_ENABLE_FB_DVFS
extern int (*ged_kpi_gpu_dvfs_fp)(int t_gpu, int t_gpu_target,
int target_fps_margin, unsigned int force_fallback);
extern void (*ged_kpi_trigger_fb_dvfs_fp)(void);
extern int (*ged_kpi_check_if_fallback_mode_fp)(void);
#endif
extern void (*mtk_get_gpu_dvfs_cal_freq_fp)(unsigned long *pulGpu_tar_freq,
int *pmode);
extern void mtk_gpu_ged_hint(int a, int b);
int ged_dvfs_boost_value(void);
#ifdef MTK_GPUFREQ_V1
extern unsigned int mt_gpufreq_get_power_by_idx(int idx);
extern int mt_gpufreq_get_opp_idx_by_freq(unsigned int freq);
#endif /* MTK_GPUFREQ_V1 */
#if (defined(GED_ENABLE_FB_DVFS) && defined(GED_ENABLE_DYNAMIC_DVFS_MARGIN))
extern void (*mtk_dvfs_margin_value_fp)(int i32MarginValue);
extern int (*mtk_get_dvfs_margin_value_fp)(void);
extern int ged_get_dvfs_margin(void);
extern unsigned int ged_get_dvfs_margin_mode(void);
#endif
#ifdef GED_CONFIGURE_LOADING_BASE_DVFS_STEP
extern void (*mtk_loading_base_dvfs_step_fp)(int i32MarginValue);
extern int (*mtk_get_loading_base_dvfs_step_fp)(void);
#endif
#ifdef GED_ENABLE_TIMER_BASED_DVFS_MARGIN
extern void (*mtk_timer_base_dvfs_margin_fp)(int i32MarginValue);
extern int (*mtk_get_timer_base_dvfs_margin_fp)(void);
/* it is not good that ged_dvfs extern API of ged_kpi, need to refactor */
extern GED_ERROR ged_kpi_timer_based_pick_riskyBQ(int *pT_gpu_real,
int *pT_gpu_pipe, int *pT_gpu_target, unsigned long long *pullWnd);
int ged_dvfs_get_tb_dvfs_margin_cur(void);
unsigned int ged_dvfs_get_tb_dvfs_margin_mode(void);
#endif
#ifdef GED_ENABLE_DVFS_LOADING_MODE
#define LOADING_ACTIVE 0
#define LOADING_MAX_3DTA_COM 1
#define LOADING_MAX_3DTA 2
extern void (*mtk_dvfs_loading_mode_fp)(int i32LoadingMode);
extern int (*mtk_get_dvfs_loading_mode_fp)(void);
extern void ged_get_gpu_utli_ex(struct GpuUtilization_Ex *util_ex);
#define MAX(x, y) ((x) < (y) ? (y) : (x))
#endif
extern unsigned int ged_log_perf_trace_enable;
#endif
@@ -0,0 +1,71 @@
/* SPDX-License-Identifier: GPL-2.0 */
/*
* Copyright (c) 2019 MediaTek Inc.
*/
#ifndef __GED_GLOBAL_H__
#define __GED_GLOBAL_H__
#include "ged_dvfs.h"
extern GED_LOG_BUF_HANDLE ghLogBuf_DVFS;
extern void (*mtk_boost_gpu_freq_fp)(void);
extern void (*mtk_set_bottom_gpu_freq_fp)(unsigned int idx);
extern unsigned int (*mtk_get_bottom_gpu_freq_fp)(void);
extern unsigned int (*mtk_custom_get_gpu_freq_level_count_fp)(void);
extern void (*mtk_custom_boost_gpu_freq_fp)(unsigned int ui32FreqLevel);
extern void (*mtk_custom_upbound_gpu_freq_fp)(unsigned int ui32FreqLevel);
extern unsigned int (*mtk_get_custom_boost_gpu_freq_fp)(void);
extern unsigned int (*mtk_get_custom_upbound_gpu_freq_fp)(void);
extern unsigned int (*mtk_get_gpu_loading_fp)(void);
extern unsigned int (*mtk_get_gpu_loading2_fp)(int load);
extern unsigned int (*mtk_get_gpu_block_fp)(void);
extern unsigned int (*mtk_get_gpu_idle_fp)(void);
extern void (*mtk_do_gpu_dvfs_fp)(unsigned long t, long phase,
unsigned long ul3DFenceDoneTime);
extern void (*mtk_gpu_dvfs_set_mode_fp)(int eMode);
extern unsigned int (*mtk_get_gpu_sub_loading_fp)(void);
extern unsigned long (*mtk_get_vsync_based_target_freq_fp)(void);
extern void (*mtk_get_gpu_dvfs_from_fp)(enum MTK_GPU_DVFS_TYPE *peType,
unsigned long *pulFreq);
extern unsigned long (*mtk_get_gpu_bottom_freq_fp)(void);
extern unsigned long (*mtk_get_gpu_custom_boost_freq_fp)(void);
extern unsigned long (*mtk_get_gpu_custom_upbound_freq_fp)(void);
extern void ged_monitor_3D_fence_set_enable(GED_BOOL bEnable);
extern unsigned int g_ui32EventStatus;
extern unsigned int g_ui32EventDebugStatus;
#ifdef GED_ENABLE_DVFS_LOADING_MODE
extern void (*ged_dvfs_cal_gpu_utilization_ex_fp)(
unsigned int *pui32Loading,
unsigned int *pui32Block,
unsigned int *pui32Idle,
void *Util_Ex);
#else
extern void (*ged_dvfs_cal_gpu_utilization_fp)(unsigned int *pui32Loading,
unsigned int *pui32Block, unsigned int *pui32Idle);
#endif
extern void (*ged_dvfs_gpu_freq_commit_fp)(unsigned long ui32NewFreqID,
GED_DVFS_COMMIT_TYPE eCommitType, int *pbCommited);
extern bool ged_gpu_power_on_notified;
extern bool ged_gpu_power_off_notified;
extern bool mtk_get_bottom_gpu_freq(unsigned int *pui32FreqLevel);
extern GED_LOG_BUF_HANDLE ghLogBuf_GED;
extern void (*mtk_gpu_sodi_entry_fp)(void);
extern void (*mtk_gpu_sodi_exit_fp)(void);
extern unsigned int g_gpu_timer_based_emu;
// calculate loading reset time stamp
extern unsigned long g_ulCalResetTS_us;
// previous calculate loading reset time stamp
extern unsigned long g_ulPreCalResetTS_us;
// last frame half, t0
extern unsigned long g_ulWorkingPeriod_us;
#endif
@@ -0,0 +1,47 @@
/* SPDX-License-Identifier: GPL-2.0 */
/*
* Copyright (c) 2019 MediaTek Inc.
*/
#ifndef __GED_GPU_TUNER_H__
#define __GED_GPU_TUNER_H__
#include <linux/list.h>
#include <linux/kernel.h>
#include <ged_bridge.h>
#include "ged_type.h"
#include <mt-plat/mtk_gpu_utility.h>
#if defined(__cplusplus)
extern "C" {
#endif
#define BUF_LEN 128
struct GED_GPU_TUNER_HINT {
char packagename[BUF_LEN];
char cmd[BUF_LEN];
int feature;
int value;
};
struct GED_GPU_TUNER_ITEM {
struct GED_GPU_TUNER_HINT status;
struct list_head List;
};
GED_ERROR ged_gpu_tuner_hint_set(char *packagename,
enum GPU_TUNER_FEATURE eFeature);
GED_ERROR ged_gpu_tuner_hint_restore(char *packagename,
enum GPU_TUNER_FEATURE eFeature);
GED_ERROR ged_gpu_get_stauts_by_packagename(char *packagename,
struct GED_GPU_TUNER_ITEM *status);
GED_ERROR ged_gpu_tuner_init(void);
GED_ERROR ged_gpu_tuner_exit(void);
#if defined(__cplusplus)
}
#endif
#endif
@@ -0,0 +1,60 @@
/* SPDX-License-Identifier: GPL-2.0 */
/*
* Copyright (c) 2019 MediaTek Inc.
*/
#ifndef _MT_GPUFREQ_H_
#define _MT_GPUFREQ_H_
#include <linux/module.h>
#include <linux/clk.h>
#define BUCK_ON 1
#define BUCK_OFF 0
#define BUCK_ENFORCE_OFF 4
#define DRV_Reg32(addr) readl(addr)
#define DRV_WriteReg32(addr, val) writel(val, addr)
#define DRV_SetReg32(addr, val) DRV_WriteReg32(addr, DRV_Reg32(addr) | (val))
#define DRV_ClrReg32(addr, val) DRV_WriteReg32(addr, DRV_Reg32(addr) & ~(val))
struct mt_gpufreq_power_table_info {
unsigned int gpufreq_khz;
unsigned int gpufreq_volt;
unsigned int gpufreq_power;
};
typedef void (*gpufreq_power_limit_notify)(unsigned int); /* legacy */
/****************************
* MTK GPUFREQ API
****************************/
extern unsigned int mt_gpufreq_get_cur_freq_index(void);
extern unsigned int mt_gpufreq_get_cur_freq(void);
extern unsigned int mt_gpufreq_get_cur_volt(void);
extern unsigned int mt_gpufreq_get_cur_vsram(void);
extern unsigned int mt_gpufreq_get_dvfs_table_num(void);
extern unsigned int mt_gpufreq_target(unsigned int idx);
extern unsigned int mt_gpufreq_voltage_enable_set(unsigned int enable);
extern unsigned int
mt_gpufreq_update_volt(unsigned int pmic_volt[], unsigned int array_size);
extern unsigned int mt_gpufreq_get_freq_by_idx(unsigned int idx);
extern unsigned int mt_gpufreq_get_volt_by_idx(unsigned int idx);
extern void mt_gpufreq_thermal_protect(unsigned int limited_power);
extern void mt_gpufreq_restore_default_volt(void);
extern void mt_gpufreq_enable_by_ptpod(void);
extern void mt_gpufreq_disable_by_ptpod(void);
extern unsigned int mt_gpufreq_get_max_power(void);
extern unsigned int mt_gpufreq_get_min_power(void);
extern unsigned int mt_gpufreq_get_thermal_limit_index(void);
extern unsigned int mt_gpufreq_get_thermal_limit_freq(void);
extern void mt_gpufreq_set_power_limit_by_pbm(unsigned int limited_power);
extern unsigned int mt_gpufreq_get_leakage_mw(void);
extern int mt_gpufreq_get_cur_ceiling_idx(void);
extern void mt_gpufreq_enable_CG(void);
extern void mt_gpufreq_disable_CG(void);
extern void mt_gpufreq_enable_MTCMOS(bool bEnableHWAPM);
extern void mt_gpufreq_disable_MTCMOS(bool bEnableHWAPM);
extern void mt_gpufreq_set_loading(unsigned int gpu_loading); /* legacy */
#endif /* _MT_GPUFREQ_H_ */
@@ -0,0 +1,19 @@
/* SPDX-License-Identifier: GPL-2.0 */
/*
* Copyright (c) 2019 MediaTek Inc.
*/
#ifndef __GED_HAL_H__
#define __GED_HAL_H__
#include "ged_type.h"
#define GED_GPU_INFO_CAPABILITY 20
#define GED_GPU_INFO_RUNTIME 21
GED_ERROR ged_hal_init(void);
void ged_hal_exit(void);
#endif
@@ -0,0 +1,44 @@
/* SPDX-License-Identifier: GPL-2.0 */
/*
* Copyright (c) 2019 MediaTek Inc.
*/
#ifndef __GED_HASH_TABLE_H__
#define __GED_HASH_TABLE_H__
#include "ged_type.h"
//typedef void *GED_HASHTABLE_HANDLE;
#define GED_HASHTABLE_HANDLE void*
GED_HASHTABLE_HANDLE ged_hashtable_create(unsigned int ui32Bits);
void ged_hashtable_destroy(GED_HASHTABLE_HANDLE hHashTable);
GED_ERROR ged_hashtable_insert(GED_HASHTABLE_HANDLE hHashTable, void *pvoid,
unsigned long *pulID);
void ged_hashtable_remove(GED_HASHTABLE_HANDLE hHashTable, unsigned long ulID);
void *ged_hashtable_find(GED_HASHTABLE_HANDLE hHashTable, unsigned long ulID);
GED_ERROR ged_hashtable_set(GED_HASHTABLE_HANDLE hHashTable,
unsigned long ulID, void *pvoid);
void ged_hashtable_iterator(GED_HASHTABLE_HANDLE hHashTable,
GED_BOOL (*iterator)(unsigned long ulID, void *pvoid, void *pvParam),
void *pvParam);
void *ged_hashtable_search(GED_HASHTABLE_HANDLE hHashTable,
void* (*pFunc)(unsigned long ulID, void *pvoid, void *pvParam),
void *pvParam);
void ged_hashtable_iterator_delete(GED_HASHTABLE_HANDLE hHashTable,
GED_BOOL (*pFunc)(unsigned long ulID, void *pvoid, void *pvParam,
GED_BOOL *pbDeleted),
void *pvParam);
unsigned long ged_hashtable_get_count(GED_HASHTABLE_HANDLE hHashTable);
#endif
@@ -0,0 +1,74 @@
/* SPDX-License-Identifier: GPL-2.0 */
/*
* Copyright (c) 2019 MediaTek Inc.
*/
#ifndef __GED_KPI_H__
#define __GED_KPI_H__
#include "ged_type.h"
#include "ged_bridge_id.h"
/* To-Do: EAS*/
/*#include "eas_ctrl.h"*/
#include <linux/sched.h>
#include <linux/cpufreq.h>
#include <linux/topology.h>
GED_ERROR ged_kpi_dequeue_buffer_ts(int pid,
u64 ullWdnd,
int i32FrameID,
int fence_fd,
int isSF);
GED_ERROR ged_kpi_queue_buffer_ts(int pid,
u64 ullWdnd,
int i32FrameID,
int fence,
int QedBuffer_length);
GED_ERROR ged_kpi_acquire_buffer_ts(int pid, u64 ullWdnd, int i32FrameID);
GED_ERROR ged_kpi_sw_vsync(void);
GED_ERROR ged_kpi_hw_vsync(void);
int ged_kpi_get_uncompleted_count(void);
unsigned int ged_kpi_get_cur_fps(void);
unsigned int ged_kpi_get_cur_avg_cpu_time(void);
unsigned int ged_kpi_get_cur_avg_gpu_time(void);
unsigned int ged_kpi_get_cur_avg_response_time(void);
unsigned int ged_kpi_get_cur_avg_gpu_remained_time(void);
unsigned int ged_kpi_get_cur_avg_cpu_remained_time(void);
unsigned int ged_kpi_get_cur_avg_gpu_freq(void);
void ged_kpi_get_latest_perf_state(long long *t_cpu_remained,
long long *t_gpu_remained,
long *t_cpu_target,
long *t_gpu_target);
GED_ERROR ged_kpi_system_init(void);
void ged_kpi_system_exit(void);
bool ged_kpi_set_cpu_remained_time(long long t_cpu_remained,
int QedBuffer_length);
bool ged_kpi_set_gpu_dvfs_hint(int t_gpu_target, int t_gpu_cur);
void ged_kpi_set_game_hint(int mode);
unsigned int ged_kpi_enabled(void);
void ged_kpi_set_target_FPS(u64 ulID, int target_FPS);
void ged_kpi_set_target_FPS_margin(u64 ulID, int target_FPS,
int target_FPS_margin, int cpu_time);
#ifdef GED_ENABLE_TIMER_BASED_DVFS_MARGIN
GED_ERROR ged_kpi_timer_based_pick_riskyBQ(int *pT_gpu_real, int *pT_gpu_pipe,
int *pT_gpu_target, unsigned long long *pullWnd);
#endif
GED_ERROR ged_kpi_query_dvfs_freq_pred(int *gpu_freq_cur
, int *gpu_freq_max, int *gpu_freq_pred);
GED_ERROR ged_kpi_query_gpu_dvfs_info(struct GED_BRIDGE_OUT_QUERY_GPU_DVFS_INFO *out);
GED_ERROR ged_kpi_set_gift_status(int mode);
GED_ERROR ged_kpi_set_gift_target_pid(int pid);
extern int linear_real_boost(int linear_boost);
#ifdef GED_KPI_CPU_INFO
extern unsigned int sched_get_cpu_load(int cpu);
extern unsigned long cpufreq_scale_freq_capacity(struct sched_domain *sd
, int cpu);
extern unsigned long arch_scale_get_max_freq(int cpu);
#endif
#ifdef GED_ENABLE_FB_DVFS
extern spinlock_t gsGpuUtilLock;
#endif
#endif
@@ -0,0 +1,82 @@
/* SPDX-License-Identifier: GPL-2.0 */
/*
* Copyright (c) 2019 MediaTek Inc.
*/
#ifndef __GED_LOG_H__
#define __GED_LOG_H__
#include "ged_type.h"
#if defined(__GNUC__)
#define GED_LOG_BUF_FORMAT_PRINTF(x, y) __attribute__((format(printf, x, y)))
#else
#define GED_LOG_BUF_FORMAT_PRINTF(x, y)
#endif
#define GED_LOG_BUF_NAME_LENGTH 64
#define GED_LOG_BUF_NODE_NAME_LENGTH 64
#define GED_LOG_BUF_TYPE_RINGBUFFER 0
#define GED_LOG_BUF_TYPE_QUEUEBUFFER 1
#define GED_LOG_BUF_TYPE_QUEUEBUFFER_AUTO_INCREASE 2
#define GED_LOG_BUF_TYPE int
GED_LOG_BUF_HANDLE ged_log_buf_alloc(int i32MaxLineCount,
int i32MaxBufferSizeByte, GED_LOG_BUF_TYPE eType, const char *pszName,
const char *pszNodeName);
GED_ERROR ged_log_buf_resize(GED_LOG_BUF_HANDLE hLogBuf, int i32NewMaxLineCount,
int i32NewMaxBufferSizeByte);
GED_ERROR ged_log_buf_ignore_lines(GED_LOG_BUF_HANDLE hLogBuf,
int i32LineCount);
GED_ERROR ged_log_buf_reset(GED_LOG_BUF_HANDLE hLogBuf);
void ged_log_buf_free(GED_LOG_BUF_HANDLE hLogBuf);
/* query by Name, return NULL if not found */
GED_LOG_BUF_HANDLE ged_log_buf_get(const char *pszName);
/* register a pointer,
* it will be set after the corresponding buffer is allcated.
*/
int ged_log_buf_get_early(const char *pszName,
GED_LOG_BUF_HANDLE *callback_set_handle);
GED_ERROR ged_log_buf_print(GED_LOG_BUF_HANDLE hLogBuf,
const char *fmt, ...) GED_LOG_BUF_FORMAT_PRINTF(2, 3);
GED_ERROR
ged_log_buf_print2(GED_LOG_BUF_HANDLE hLogBuf, int i32LogAttrs,
const char *fmt, ...) GED_LOG_BUF_FORMAT_PRINTF(3, 4);
GED_ERROR ged_log_system_init(void);
void ged_log_system_exit(void);
int ged_log_buf_write(GED_LOG_BUF_HANDLE hLogBuf,
const char __user *pszBuffer, int i32Count);
void ged_log_trace_begin(char *name);
void ged_log_trace_end(void);
void ged_log_trace_counter(char *name, int count);
void ged_log_perf_trace_counter(char *name, long long count, int pid,
unsigned long frameID, u64 BQID);
void ged_log_dump(GED_LOG_BUF_HANDLE hLogBuf);
#if defined(CONFIG_MACH_MT8167) || defined(CONFIG_MACH_MT8173) ||\
defined(CONFIG_MACH_MT6739) || defined(CONFIG_MACH_MT6761)\
|| defined(CONFIG_MACH_MT6765)
extern void ged_dump_fw(void);
#endif
unsigned int is_gpu_ged_log_enable(void);
#endif
@@ -0,0 +1,16 @@
/* SPDX-License-Identifier: GPL-2.0 */
/*
* Copyright (c) 2019 MediaTek Inc.
*/
#ifndef __GED_MONITOR_3D_FENCE_H__
#define __GED_MONITOR_3D_FENCE_H__
#include "ged_type.h"
GED_ERROR ged_monitor_3D_fence_add(int fence_fd);
void ged_monitor_3D_fence_notify(void);
unsigned long ged_monitor_3D_fence_done_time(void);
int ged_monitor_3D_fence_get_count(void);
#endif
@@ -0,0 +1,36 @@
/* SPDX-License-Identifier: GPL-2.0 */
/*
* Copyright (c) 2019 MediaTek Inc.
*/
#ifndef __GED_NOTIFY_SW_VSYNC_H__
#define __GED_NOTIFY_SW_VSYNC_H__
#include "ged_type.h"
extern unsigned int gpu_block;
extern unsigned int gpu_idle;
extern unsigned int gpu_av_loading;
extern unsigned long long g_ns_gpu_on_ts;
GED_ERROR ged_notify_sw_vsync(GED_VSYNC_TYPE eType,
struct GED_DVFS_UM_QUERY_PACK *psQueryData);
GED_ERROR ged_notify_sw_vsync_system_init(void);
void ged_notify_sw_vsync_system_exit(void);
#ifdef GED_ENABLE_FB_DVFS
void ged_set_backup_timer_timeout(u64 time_out);
void ged_cancel_backup_timer(void);
#endif
void ged_sodi_start(void);
void ged_sodi_stop(void);
#if defined(CONFIG_MACH_MT8167) || defined(CONFIG_MACH_MT8173) ||\
defined(CONFIG_MACH_MT6739) || defined(CONFIG_MACH_MT6761) ||\
defined(CONFIG_MACH_MT6765)
extern void MTKFWDump(void);
#endif
#endif
@@ -0,0 +1,36 @@
/* SPDX-License-Identifier: GPL-2.0 */
/*
* Copyright (C) 2020 MediaTek Inc.
*/
#ifndef __GED_SYSFS_H__
#define __GED_SYSFS_H__
#include <linux/kobject.h>
#include "ged_type.h"
#define GED_SYSFS_MAX_BUFF_SIZE 128
#define KOBJ_ATTR_RW(_name) \
struct kobj_attribute kobj_attr_##_name = \
__ATTR(_name, 0660, \
_name##_show, _name##_store)
#define KOBJ_ATTR_RO(_name) \
struct kobj_attribute kobj_attr_##_name = \
__ATTR(_name, 0440, \
_name##_show, NULL)
GED_ERROR ged_sysfs_create_dir(struct kobject *parent,
const char *name, struct kobject **ppsKobj);
void ged_sysfs_remove_dir(struct kobject **ppsKobj);
GED_ERROR ged_sysfs_create_file(struct kobject *parent,
struct kobj_attribute *kobj_attr);
void ged_sysfs_remove_file(struct kobject *parent,
struct kobj_attribute *kobj_attr);
GED_ERROR ged_sysfs_init(void);
void ged_sysfs_exit(void);
extern struct kobject *kernel_kobj;
#endif
@@ -0,0 +1,20 @@
/* SPDX-License-Identifier: GPL-2.0 */
/*
* Copyright (c) 2019 MediaTek Inc.
*/
#ifndef __GED_THREAD_H__
#define __GED_THREAD_H__
#include "ged_type.h"
#define GED_THREAD_HANDLE void*
//typedef void (*GED_THREAD_FUNC)(void *);
GED_ERROR ged_thread_create(GED_THREAD_HANDLE *phThread,
const char *szThreadName, void (*pFunc)(void *), void *pvData);
void ged_thread_destroy(GED_THREAD_HANDLE hThread);
#endif
+111
View File
@@ -0,0 +1,111 @@
/* SPDX-License-Identifier: GPL-2.0 */
/*
* Copyright (c) 2019 MediaTek Inc.
*/
#ifndef __GED_TYPE_H__
#define __GED_TYPE_H__
#define GED_OK 0
#define GED_ERROR_FAIL 1
#define GED_ERROR_OOM 2
#define GED_ERROR_OUT_OF_FD 3
#define GED_ERROR_FAIL_WITH_LIMIT 4
#define GED_ERROR_TIMEOUT 5
#define GED_ERROR_CMD_NOT_PROCESSED 6
#define GED_ERROR_INVALID_PARAMS 7
#define GED_ERROR_INTENTIONAL_BLOCK 8
#define GED_ERROR int
#define GED_HANDLE void*
#define GED_FRR_HANDLE void*
#define GED_GLES_HANDLE void*
#define GED_SWD_HANDLE void*
#define GED_LOG_HANDLE void*
#define GED_KPI_HANDLE void*
#define GED_LOG_BUF_HANDLE unsigned int
#define GED_FALSE 0
#define GED_TRUE 1
#define GED_BOOL int
#define GED_LOADING 0
#define GED_IDLE 1
#define GED_BLOCKING 2
#define GED_PRE_FREQ 3
#define GED_PRE_FREQ_IDX 4
#define GED_CUR_FREQ 5
#define GED_CUR_FREQ_IDX 6
#define GED_MAX_FREQ_IDX 7
#define GED_MAX_FREQ_IDX_FREQ 8
#define GED_MIN_FREQ_IDX 9
#define GED_MIN_FREQ_IDX_FREQ 10
#define GED_3D_FENCE_DONE_TIME 11
#define GED_VSYNC_OFFSET 12
#define GED_EVENT_STATUS 13
#define GED_EVENT_DEBUG_STATUS 14
#define GED_EVENT_GAS_MODE 15
#define GED_SRV_SUICIDE 16
#define GED_PRE_HALF_PERIOD 17
#define GED_LATEST_START 18
#define GED_FPS 19
#define GED_INFO_SIZE 20
#define GED_INFO int
#define GED_DVFS_VSYNC_OFFSET_DEBUG_CLEAR_EVENT 0
#define GED_DVFS_VSYNC_OFFSET_FORCE_ON 1
#define GED_DVFS_VSYNC_OFFSET_FORCE_OFF 2
#define GED_DVFS_VSYNC_OFFSET_TOUCH_EVENT 3
#define GED_DVFS_VSYNC_OFFSET_THERMAL_EVENT 4
#define GED_DVFS_VSYNC_OFFSET_WFD_EVENT 5
#define GED_DVFS_VSYNC_OFFSET_MHL_EVENT 6
#define GED_DVFS_VSYNC_OFFSET_GAS_EVENT 7
#define GED_DVFS_VSYNC_OFFSET_LOW_POWER_MODE_EVENT 8
#define GED_DVFS_VSYNC_OFFSET_MHL4K_VID_EVENT 9
#define GED_DVFS_VSYNC_OFFSET_VR_EVENT 10
#define GED_DVFS_BOOST_HOST_EVENT 11
#define GED_DVFS_VSYNC_OFFSET_VILTE_VID_EVENT 12
#define GED_DVFS_VSYNC_OFFSET_LOW_LATENCY_MODE_EVENT 13
#define GED_DVFS_VSYNC_OFFSET_DHWC_EVENT 14
#define GED_DVFS_VSYNC_OFFSET_SWITCH_CMD int
#define GED_VSYNC_SW_EVENT 0
#define GED_VSYNC_HW_EVENT 1
#define GED_VSYNC_TYPE int
enum {
GAS_CATEGORY_GAME,
GAS_CATEGORY_OTHERS,
};
#define GED_BOOST_GPU_FREQ_LEVEL_MAX 100
#define GED_BOOST_GPU_FREQ_LEVEL int
#define GED_BRIDGE_IN_LOGBUF_SIZE 320
/* bit 0~7 reserved for internal used */
#define GED_RESVERED 0xFF
/* log with a prefix kernel time */
#define GED_LOG_ATTR_TIME 0x100
/* log with a prefix user time, pid, tid */
#define GED_LOG_ATTR_TIME_TPT 0x200
struct GED_DVFS_UM_QUERY_PACK {
char bFirstBorn;
unsigned int ui32GPULoading;
unsigned int ui32GPUFreqID;
unsigned int gpu_cur_freq;
unsigned int gpu_pre_freq;
long long usT;
long long nsOffset;
unsigned long long ul3DFenceDoneTime;
unsigned long long ulPreCalResetTS_us;
unsigned long long ulWorkingPeriod_us;
unsigned int ui32TargetPeriod_us;
unsigned int ui32BoostValue;
};
#endif
+191
View File
@@ -0,0 +1,191 @@
// SPDX-License-Identifier: GPL-2.0
/*
* Copyright (c) 2019 MediaTek Inc.
*/
#include <linux/kernel.h>
#include <linux/module.h>
#include "ged.h"
#include "ged_kpi.h"
void (*ged_vsync_notifier_fp)(void);
EXPORT_SYMBOL(ged_vsync_notifier_fp);
void ged_notification(GED_NOTIFICATION_TYPE eType)
{
switch (eType) {
case GED_NOTIFICATION_TYPE_SW_VSYNC:
ged_kpi_sw_vsync();
if (ged_vsync_notifier_fp)
ged_vsync_notifier_fp();
break;
case GED_NOTIFICATION_TYPE_HW_VSYNC_PRIMARY_DISPLAY:
ged_kpi_hw_vsync();
break;
}
}
EXPORT_SYMBOL(ged_notification);
int ged_set_target_fps(unsigned int target_fps, int mode)
{
return 0;
}
EXPORT_SYMBOL(ged_set_target_fps);
void ged_get_latest_perf_state(long long *t_cpu_remained,
long long *t_gpu_remained,
long *t_cpu_target,
long *t_gpu_target)
{
ged_kpi_get_latest_perf_state(t_cpu_remained,
t_gpu_remained, t_cpu_target, t_gpu_target);
}
unsigned int ged_get_cur_fps(void)
{
return ged_kpi_get_cur_fps();
}
#ifndef MTK_GPU_DVFS
/*
* Below code segment are fake gpufreq API set
*/
#include "ged_gpufreq.h"
/****************************
* MTK GPUFREQ API
****************************/
unsigned int mt_gpufreq_get_cur_freq_index(void)
{
return 0;
}
unsigned int mt_gpufreq_get_cur_freq(void)
{
return 0;
}
unsigned int mt_gpufreq_get_cur_volt(void)
{
return 0;
}
unsigned int mt_gpufreq_get_cur_vsram(void)
{
return 0;
}
unsigned int mt_gpufreq_get_dvfs_table_num(void)
{
return 0;
}
unsigned int mt_gpufreq_target(unsigned int idx)
{
return 0;
}
unsigned int mt_gpufreq_voltage_enable_set(unsigned int enable)
{
return 0;
}
unsigned int
mt_gpufreq_update_volt(unsigned int pmic_volt[], unsigned int array_size)
{
return 0;
}
unsigned int mt_gpufreq_get_freq_by_idx(unsigned int idx)
{
return 0;
}
unsigned int mt_gpufreq_get_volt_by_idx(unsigned int idx)
{
return 0;
}
unsigned int mt_gpufreq_get_ori_opp_idx(unsigned int idx)
{
return 0;
}
void mt_gpufreq_thermal_protect(unsigned int limited_power)
{
;
}
void mt_gpufreq_restore_default_volt(void)
{
;
}
void mt_gpufreq_enable_by_ptpod(void)
{
;
}
void mt_gpufreq_disable_by_ptpod(void)
{
;
}
unsigned int mt_gpufreq_get_max_power(void)
{
return 0;
}
unsigned int mt_gpufreq_get_min_power(void)
{
return 0;
}
unsigned int mt_gpufreq_get_thermal_limit_index(void)
{
return 0;
}
unsigned int mt_gpufreq_get_thermal_limit_freq(void)
{
return 0;
}
void mt_gpufreq_set_power_limit_by_pbm(unsigned int limited_power)
{
;
}
unsigned int mt_gpufreq_get_leakage_mw(void)
{
return 0;
}
int mt_gpufreq_get_cur_ceiling_idx(void)
{
return 0;
}
void mt_gpufreq_enable_CG(void)
{
;
}
void mt_gpufreq_disable_CG(void)
{
;
}
void mt_gpufreq_enable_MTCMOS(bool bEnableHWAPM)
{
;
}
void mt_gpufreq_disable_MTCMOS(bool bEnableHWAPM)
{
;
}
void mt_gpufreq_set_loading(unsigned int gpu_loading)
{
;
}
#endif
+83
View File
@@ -0,0 +1,83 @@
// SPDX-License-Identifier: GPL-2.0
/*
* Copyright (c) 2019 MediaTek Inc.
*/
#include "ged_base.h"
#include <asm/page.h>
#include <linux/version.h>
#include <linux/vmalloc.h>
#include <linux/slab.h>
#include <linux/sched/clock.h>
#include <linux/interrupt.h>
#include <linux/uaccess.h>
unsigned long ged_copy_to_user(void __user *pvTo, const void *pvFrom,
unsigned long ulBytes)
{
if (access_ok(VERIFY_WRITE, pvTo, ulBytes))
return __copy_to_user(pvTo, pvFrom, ulBytes);
return ulBytes;
}
unsigned long ged_copy_from_user(void *pvTo, const void __user *pvFrom,
unsigned long ulBytes)
{
if (access_ok(VERIFY_READ, pvFrom, ulBytes))
return __copy_from_user(pvTo, pvFrom, ulBytes);
return ulBytes;
}
void *ged_alloc(int i32Size)
{
void *pvBuf;
if (i32Size <= PAGE_SIZE)
pvBuf = kmalloc(i32Size, GFP_KERNEL);
else
pvBuf = vmalloc(i32Size);
return pvBuf;
}
void *ged_alloc_atomic(int i32Size)
{
void *pvBuf;
if (i32Size <= PAGE_SIZE)
pvBuf = kmalloc(i32Size, GFP_ATOMIC);
else
pvBuf = vmalloc(i32Size);
return pvBuf;
}
void ged_free(void *pvBuf, int i32Size)
{
if (pvBuf) {
if (i32Size <= PAGE_SIZE)
kfree(pvBuf);
else
vfree(pvBuf);
}
}
long ged_get_pid(void)
{
if (in_interrupt())
return 0xffffffffL;
return (long)task_tgid_nr(current);
}
unsigned long long ged_get_time(void)
{
unsigned long long temp;
preempt_disable();
temp = cpu_clock(smp_processor_id());
preempt_enable();
return temp;
}
+246
View File
@@ -0,0 +1,246 @@
// SPDX-License-Identifier: GPL-2.0
/*
* Copyright (c) 2019 MediaTek Inc.
*/
#include <linux/kernel.h>
#include <mt-plat/mtk_gpu_utility.h>
#ifdef CONFIG_MTK_FPSGO_V3
#include <mt-plat/fpsgo_common.h>
#endif
#include "ged_base.h"
#include "ged_bridge.h"
#include "ged_log.h"
#include "ged_monitor_3D_fence.h"
#include "ged_notify_sw_vsync.h"
#include "ged_dvfs.h"
#include <linux/module.h>
#include "ged_kpi.h"
#include "ged.h"
static unsigned int ged_boost_enable = 1;
//-----------------------------------------------------------------------------
int ged_bridge_log_buf_get(
struct GED_BRIDGE_IN_LOGBUFGET *psLogBufGetIN,
struct GED_BRIDGE_OUT_LOGBUFGET *psLogBufGetOUT)
{
psLogBufGetOUT->hLogBuf = ged_log_buf_get(psLogBufGetIN->acName);
psLogBufGetOUT->eError =
psLogBufGetOUT->hLogBuf ? GED_OK : GED_ERROR_FAIL;
return 0;
}
//-----------------------------------------------------------------------------
int ged_bridge_log_buf_write(
struct GED_BRIDGE_IN_LOGBUFWRITE *psLogBufWriteIN,
struct GED_BRIDGE_OUT_LOGBUFWRITE *psLogBufWriteOUT)
{
psLogBufWriteIN->acLogBuf[GED_BRIDGE_IN_LOGBUF_SIZE - 1] = '\0';
psLogBufWriteOUT->eError =
ged_log_buf_print2(psLogBufWriteIN->hLogBuf,
psLogBufWriteIN->attrs, "%s", psLogBufWriteIN->acLogBuf);
return 0;
}
//-----------------------------------------------------------------------------
int ged_bridge_log_buf_reset(
struct GED_BRIDGE_IN_LOGBUFRESET *psLogBufResetIn,
struct GED_BRIDGE_OUT_LOGBUFRESET *psLogBufResetOUT)
{
psLogBufResetOUT->eError = ged_log_buf_reset(psLogBufResetIn->hLogBuf);
return 0;
}
//-----------------------------------------------------------------------------
int ged_bridge_boost_gpu_freq(
struct GED_BRIDGE_IN_BOOSTGPUFREQ *psBoostGpuFreqIN,
struct GED_BRIDGE_OUT_BOOSTGPUFREQ *psBoostGpuFreqOUT)
{
psBoostGpuFreqOUT->eError = (mtk_set_bottom_gpu_freq(
psBoostGpuFreqIN->eGPUFreqLevel) == true) ?
GED_OK : GED_ERROR_FAIL;
return 0;
}
//-----------------------------------------------------------------------------
int ged_bridge_monitor_3D_fence(
struct GED_BRIDGE_IN_MONITOR3DFENCE *psMonitor3DFenceINT,
struct GED_BRIDGE_OUT_MONITOR3DFENCE *psMonitor3DFenceOUT)
{
psMonitor3DFenceOUT->eError =
ged_monitor_3D_fence_add(psMonitor3DFenceINT->fd);
return 0;
}
//-----------------------------------------------------------------------------
int ged_bridge_query_info(
struct GED_BRIDGE_IN_QUERY_INFO *psQueryInfoINT,
struct GED_BRIDGE_OUT_QUERY_INFO *psQueryInfoOUT)
{
psQueryInfoOUT->retrieve = ged_query_info(psQueryInfoINT->eType);
return 0;
}
//-----------------------------------------------------------------------------
int ged_bridge_notify_vsync(
struct GED_BRIDGE_IN_NOTIFY_VSYNC *psNotifyVsyncINT,
struct GED_BRIDGE_OUT_NOTIFY_VSYNC *psNotifyVsyncOUT)
{
psNotifyVsyncOUT->eError =
ged_notify_sw_vsync(psNotifyVsyncINT->eType,
&psNotifyVsyncOUT->sQueryData);
return 0;
}
//-----------------------------------------------------------------------------
int ged_bridge_dvfs_probe(
struct GED_BRIDGE_IN_DVFS_PROBE *psDVFSProbeINT,
struct GED_BRIDGE_OUT_DVFS_PROBE *psDVFSProbeOUT)
{
psDVFSProbeOUT->eError = ged_dvfs_probe(psDVFSProbeINT->pid);
return 0;
}
//-----------------------------------------------------------------------------
int ged_bridge_dvfs_um_retrun(
struct GED_BRIDGE_IN_DVFS_UM_RETURN *psDVFS_UM_returnINT,
struct GED_BRIDGE_OUT_DVFS_UM_RETURN *psDVFS_UM_returnOUT)
{
psDVFS_UM_returnOUT->eError =
ged_dvfs_um_commit(psDVFS_UM_returnINT->gpu_tar_freq,
psDVFS_UM_returnINT->bFallback);
return 0;
}
//-----------------------------------------------------------------------------
int ged_bridge_event_notify(
struct GED_BRIDGE_IN_EVENT_NOTIFY *psEVENT_NOTIFYINT,
struct GED_BRIDGE_OUT_EVENT_NOTIFY *psEVENT_NOTIFYOUT)
{
if (ged_boost_enable) {
psEVENT_NOTIFYOUT->eError =
ged_dvfs_vsync_offset_event_switch(
psEVENT_NOTIFYINT->eEvent, psEVENT_NOTIFYINT->bSwitch);
} else {
psEVENT_NOTIFYOUT->eError = GED_OK;
}
return 0;
}
/* -------------------------------------------------------------------------- */
int ged_bridge_gpu_timestamp(
struct GED_BRIDGE_IN_GPU_TIMESTAMP *psGpuBeginINT,
struct GED_BRIDGE_OUT_GPU_TIMESTAMP *psGpuBeginOUT)
{
if (ged_kpi_enabled() == 1) {
if (psGpuBeginINT->QedBuffer_length == -2) {
psGpuBeginOUT->eError =
ged_kpi_dequeue_buffer_ts(psGpuBeginINT->pid,
psGpuBeginINT->ullWnd,
psGpuBeginINT->i32FrameID,
psGpuBeginINT->fence_fd,
psGpuBeginINT->isSF);
} else if (psGpuBeginINT->QedBuffer_length != -1) {
psGpuBeginOUT->eError =
ged_kpi_queue_buffer_ts(psGpuBeginINT->pid,
psGpuBeginINT->ullWnd,
psGpuBeginINT->i32FrameID,
psGpuBeginINT->fence_fd,
psGpuBeginINT->QedBuffer_length);
} else {
psGpuBeginOUT->eError =
ged_kpi_acquire_buffer_ts(psGpuBeginINT->pid,
psGpuBeginINT->ullWnd,
psGpuBeginINT->i32FrameID);
}
psGpuBeginOUT->is_ged_kpi_enabled = 1;
} else {
psGpuBeginOUT->is_ged_kpi_enabled = 0;
psGpuBeginOUT->eError = GED_OK;
}
return 0;
}
//-----------------------------------------------------------------------------
int ged_bridge_gpu_hint_to_cpu(
struct GED_BRIDGE_IN_GPU_HINT_TO_CPU *in,
struct GED_BRIDGE_OUT_GPU_HINT_TO_CPU *out)
{
int ret = 0;
#ifdef CONFIG_MTK_FPSGO_V3
ret = fpsgo_notify_gpu_block(in->tid, in->i32BridgeFD, in->hint);
#endif
out->eError = GED_OK;
out->boost_flag = ret;
out->boost_value = ged_dvfs_boost_value();
return 0;
}
//-----------------------------------------------------------------------------
static int ged_force_mdp_enable;
int ged_bridge_hint_force_mdp(
struct GED_BRIDGE_IN_HINT_FORCE_MDP *psHintForceMdpIn,
struct GED_BRIDGE_OUT_HINT_FORCE_MDP *psHintForceMdpOut)
{
/* Set flag */
if (psHintForceMdpIn->hint != -1) {
ged_force_mdp_enable = psHintForceMdpIn->hint;
psHintForceMdpOut->eError =
(ged_force_mdp_enable == psHintForceMdpIn->hint) ?
GED_OK : GED_ERROR_FAIL;
}
/* Get flag */
else {
psHintForceMdpOut->mdp_flag = ged_force_mdp_enable;
psHintForceMdpOut->eError =
(ged_force_mdp_enable == psHintForceMdpOut->mdp_flag) ?
GED_OK : GED_ERROR_FAIL;
}
return 0;
}
//-----------------------------------------------------------------------------
int ged_bridge_query_dvfs_freq_pred(
struct GED_BRIDGE_IN_QUERY_DVFS_FREQ_PRED *QueryDVFSFreqPredIn,
struct GED_BRIDGE_OUT_QUERY_DVFS_FREQ_PRED *QueryDVFSFreqPredOut)
{
/* GiFT hint status to GED */
if (QueryDVFSFreqPredIn->hint) {
QueryDVFSFreqPredOut->eError =
ged_kpi_set_gift_status(QueryDVFSFreqPredIn->hint);
}
/* GiFT query gpu_freq info from GED */
else {
QueryDVFSFreqPredOut->eError = ged_kpi_query_dvfs_freq_pred(
&QueryDVFSFreqPredOut->gpu_freq_cur,
&QueryDVFSFreqPredOut->gpu_freq_max,
&QueryDVFSFreqPredOut->gpu_freq_dvfs_pred);
}
return 0;
}
//-----------------------------------------------------------------------------
int ged_bridge_query_gpu_dvfs_info(
struct GED_BRIDGE_IN_QUERY_GPU_DVFS_INFO *QueryGPUDVFSInfoIn,
struct GED_BRIDGE_OUT_QUERY_GPU_DVFS_INFO *QueryGPUDVFSInfoOut)
{
/* GiFT hint PID status to GED */
if (QueryGPUDVFSInfoIn->pid)
ged_kpi_set_gift_target_pid(QueryGPUDVFSInfoIn->pid);
/* GiFT hint status to GED */
if (QueryGPUDVFSInfoIn->hint) {
if (QueryGPUDVFSInfoIn->gift_ratio)
QueryGPUDVFSInfoOut->eError =
ged_kpi_set_gift_status(QueryGPUDVFSInfoIn->gift_ratio);
else
QueryGPUDVFSInfoOut->eError =
ged_kpi_set_gift_status(QueryGPUDVFSInfoIn->hint);
}
QueryGPUDVFSInfoOut->eError = ged_kpi_query_gpu_dvfs_info(
QueryGPUDVFSInfoOut);
return 0;
}
//-----------------------------------------------------------------------------
module_param(ged_boost_enable, uint, 0644);
module_param(ged_force_mdp_enable, int, 0644);
+173
View File
@@ -0,0 +1,173 @@
// SPDX-License-Identifier: GPL-2.0
/*
* Copyright (c) 2019 MediaTek Inc.
*/
#include <linux/module.h>
#include <linux/slab.h>
#include <linux/debugfs.h>
#include "ged_base.h"
#include "ged_debugFS.h"
#define GED_DEBUGFS_DIR_NAME "ged"
static struct dentry *gpsDebugFSEntryDir;
struct GED_DEBUGFS_PRIV_DATA {
const struct seq_operations *psReadOps;
ssize_t (*pfnWrite)(const char __user *pszBuffer, size_t uiCount,
loff_t uiPosition, void *pvData);
void *pvData;
};
//-----------------------------------------------------------------------------
static int ged_debugFS_open(struct inode *psINode, struct file *psFile)
{
struct GED_DEBUGFS_PRIV_DATA *psPrivData =
(struct GED_DEBUGFS_PRIV_DATA *)psINode->i_private;
int iResult;
iResult = seq_open(psFile, psPrivData->psReadOps);
if (iResult == 0) {
struct seq_file *psSeqFile = psFile->private_data;
psSeqFile->private = psPrivData->pvData;
return GED_OK;
}
return GED_ERROR_FAIL;
}
//-----------------------------------------------------------------------------
static ssize_t ged_debugFS_write(
struct file *psFile,
const char __user *pszBuffer,
size_t uiCount,
loff_t *puiPosition)
{
struct inode *psINode = psFile->f_path.dentry->d_inode;
struct GED_DEBUGFS_PRIV_DATA *psPrivData =
(struct GED_DEBUGFS_PRIV_DATA *)psINode->i_private;
if (psPrivData->pfnWrite == NULL)
return -EIO;
return psPrivData->pfnWrite(pszBuffer, uiCount,
*puiPosition, psPrivData->pvData);
}
//-----------------------------------------------------------------------------
static const struct file_operations gsGEDDebugFSFileOps = {
.owner = THIS_MODULE,
.open = ged_debugFS_open,
.read = seq_read,
.write = ged_debugFS_write,
.llseek = seq_lseek,
.release = seq_release,
};
//-----------------------------------------------------------------------------
GED_ERROR ged_debugFS_create_entry(
const char *pszName,
void *pvDir,
const struct seq_operations *psReadOps,
ssize_t (*pfnWrite)(const char __user *pszBuffer,
size_t uiCount, loff_t uiPosition, void *pvData),
void *pvData,
struct dentry **ppsEntry)
{
struct GED_DEBUGFS_PRIV_DATA *psPrivData;
struct dentry *psEntry;
umode_t uiMode;
psPrivData = ged_alloc(sizeof(struct GED_DEBUGFS_PRIV_DATA));
if (psPrivData == NULL)
return GED_ERROR_OOM;
psPrivData->psReadOps = psReadOps;
psPrivData->pfnWrite = pfnWrite;
psPrivData->pvData = pvData;
uiMode = S_IFREG;
if (psReadOps != NULL)
uiMode |= 0440;
if (pfnWrite != NULL)
uiMode |= 0220;
psEntry = debugfs_create_file(pszName,
uiMode,
(pvDir != NULL) ? (struct dentry *)pvDir : gpsDebugFSEntryDir,
psPrivData,
&gsGEDDebugFSFileOps);
if (IS_ERR(psEntry)) {
GED_LOGE("Failed to create '%s' debugfs entry\n", pszName);
return GED_ERROR_FAIL;
}
*ppsEntry = psEntry;
return GED_OK;
}
//-----------------------------------------------------------------------------
void ged_debugFS_remove_entry(struct dentry *psEntry)
{
if (psEntry != NULL && psEntry->d_inode->i_private != NULL)
ged_free(psEntry->d_inode->i_private,
sizeof(struct GED_DEBUGFS_PRIV_DATA));
debugfs_remove(psEntry);
}
//-----------------------------------------------------------------------------
GED_ERROR ged_debugFS_create_entry_dir(
const char *pszName,
struct dentry *psParentDir,
struct dentry **ppsDir)
{
struct dentry *psDir;
if (pszName == NULL || ppsDir == NULL)
return GED_ERROR_INVALID_PARAMS;
psDir = debugfs_create_dir(pszName,
(psParentDir) ? psParentDir : gpsDebugFSEntryDir);
if (psDir == NULL) {
GED_LOGE("Failed to create '%s' debugfs directory\n", pszName);
return GED_ERROR_OOM;
}
*ppsDir = psDir;
return GED_OK;
}
//-----------------------------------------------------------------------------
void ged_debugFS_remove_entry_dir(struct dentry *psDir)
{
debugfs_remove(psDir);
}
//-----------------------------------------------------------------------------
GED_ERROR ged_debugFS_init(void)
{
//assert(gpkDebugFSEntryDir == NULL);
gpsDebugFSEntryDir = debugfs_create_dir(GED_DEBUGFS_DIR_NAME, NULL);
if (gpsDebugFSEntryDir == NULL) {
GED_LOGE("Failed to create '%s' debugfs root directory\n",
GED_DEBUGFS_DIR_NAME);
return GED_ERROR_OOM;
}
return GED_OK;
}
//-----------------------------------------------------------------------------
void ged_debugFS_exit(void)
{
//assert(gpkDebugFSEntryDir != NULL);
debugfs_remove(gpsDebugFSEntryDir);
gpsDebugFSEntryDir = NULL;
}
//-----------------------------------------------------------------------------
File diff suppressed because it is too large. Load diff
+436
View File
@@ -0,0 +1,436 @@
// SPDX-License-Identifier: GPL-2.0
/*
* Copyright (c) 2019 MediaTek Inc.
*/
#include "ged_ge.h"
#include <ged_bridge.h>
#include <linux/file.h>
#include <linux/fs.h>
#include <linux/list.h>
#include <linux/spinlock.h>
#include <linux/slab.h>
#include <linux/mutex.h>
#include <linux/seq_file.h>
#include <linux/anon_inodes.h>
#include <linux/kmemleak.h>
struct GEEntry {
uint64_t unique_id;
struct file *file;
int alloc_fd;
int region_num;
void *data;
int *region_sizes; /* sub data */
uint32_t **region_data; /* sub data */
struct list_head ge_entry_list;
};
#define GED_PDEBUG(fmt, ...)\
pr_debug("[GRALLOC_EXTRA,%s:%d]" fmt, __FILE__, __LINE__, ##__VA_ARGS__)
static struct kmem_cache *gPoolCache;
static LIST_HEAD(ge_entry_list_head);
static DEFINE_SPINLOCK(ge_entry_list_lock);
/* region alloc and free lock */
static DEFINE_SPINLOCK(ge_raf_lock);
static uint64_t gen_unique_id(void)
{
/* A collision may occur after 2^44 sec, aka 557,474 years,
* if we do allocate and free in 2^20 times per second.
*/
static uint64_t serial;
unsigned long flags;
uint64_t ret;
spin_lock_irqsave(&ge_raf_lock, flags);
ret = serial++;
spin_unlock_irqrestore(&ge_raf_lock, flags);
return ret;
}
static int ge_entry_release(struct inode *inode, struct file *file)
{
unsigned long flags;
int i;
struct GEEntry *entry = file->private_data;
spin_lock_irqsave(&ge_entry_list_lock, flags);
list_del(&entry->ge_entry_list);
spin_unlock_irqrestore(&ge_entry_list_lock, flags);
/* remove object */
for (i = 0; i < entry->region_num; ++i)
kfree(entry->region_data[i]);
kfree(entry->region_sizes);
kmem_cache_free(gPoolCache, entry);
return 0;
}
static const struct file_operations GEEntry_fops = {
.release = ge_entry_release,
};
GED_ERROR ged_ge_init(void)
{
int flags = 0;
GED_ERROR err = GED_OK;
gPoolCache = kmem_cache_create("gralloc_extra",
sizeof(struct GEEntry), 0, flags, NULL);
if (!gPoolCache) {
GED_PDEBUG("kmem_cache_create fail\n");
err = GED_ERROR_FAIL;
}
return err;
}
int ged_ge_exit(void)
{
/* TODO : free all memory */
kmem_cache_destroy(gPoolCache);
return 0;
}
int ged_ge_alloc(int region_num, uint32_t *region_sizes)
{
unsigned long flags;
int fd;
int i;
struct GEEntry *entry =
(struct GEEntry *)kmem_cache_zalloc(gPoolCache, GFP_KERNEL);
if (!entry) {
GED_PDEBUG("alloc entry fail, size:%zu\n",
sizeof(struct GEEntry));
goto err_entry;
}
fd = entry->alloc_fd = get_unused_fd_flags(O_CLOEXEC);
if (entry->alloc_fd < 0) {
GED_PDEBUG("get_unused_fd_flags() return %d\n",
entry->alloc_fd);
goto err_fd;
}
entry->file = anon_inode_getfile("gralloc_extra",
&GEEntry_fops, entry, 0);
if (IS_ERR(entry->file)) {
GED_PDEBUG("anon_inode_getfile() fail\n");
goto err_entry_file;
}
/* Avoid using the unpredictable value for Fuzzing */
region_num = GE_ALLOC_STRUCT_NUM;
entry->region_num = region_num;
entry->data =
kzalloc((sizeof(uint32_t) + sizeof(uint32_t *)) * region_num,
GFP_KERNEL);
if (!entry->data) {
GED_PDEBUG("alloc data fail, size:%zu\n",
sizeof(void *) * region_num);
goto err_kmalloc;
}
entry->region_sizes = (uint32_t *)entry->data;
entry->region_data = (uint32_t **)(entry->region_sizes + region_num);
for (i = 0; i < region_num; ++i) {
// check region_sizes parameter
if (region_sizes[i] <= 0 ||
region_sizes[i] > GE_MAX_REGION_SIZE) {
GED_PDEBUG("check size fail region_sizes[%d]:%u\n",
i, region_sizes[i]);
goto err_parameter;
}
entry->region_sizes[i] = region_sizes[i];
}
entry->unique_id = gen_unique_id();
spin_lock_irqsave(&ge_entry_list_lock, flags);
list_add_tail(&entry->ge_entry_list, &ge_entry_list_head);
spin_unlock_irqrestore(&ge_entry_list_lock, flags);
fd_install(entry->alloc_fd, entry->file);
return fd;
err_parameter:
kfree(entry->data);
err_kmalloc:
err_entry_file:
put_unused_fd(entry->alloc_fd);
err_fd:
kmem_cache_free(gPoolCache, entry);
err_entry:
return -1;
}
static void dump_ge_regions(struct GEEntry *entry)
{
int i;
for (i = 0; i < entry->region_num; ++i) {
GED_PDEBUG("ged_ge dump, %d/%d, s %d\n",
i, entry->region_num, entry->region_sizes[i]);
}
}
static int valid_parameters(struct GEEntry *entry, int region_id,
int u32_offset, int u32_size)
{
if (region_id < 0 || region_id >= entry->region_num ||
u32_offset < 0 ||
u32_offset >= (GE_MAX_REGION_SIZE / sizeof(uint32_t)) ||
u32_size <= 0 ||
u32_size > (GE_MAX_REGION_SIZE / sizeof(uint32_t)) ||
u32_offset * sizeof(uint32_t) >= entry->region_sizes[region_id] ||
u32_size * sizeof(uint32_t) > entry->region_sizes[region_id] ||
(u32_offset + u32_size) * sizeof(uint32_t) >
entry->region_sizes[region_id]) {
GED_PDEBUG("fail, invalid r_id %d, o %d, s %d\n",
region_id, u32_offset, u32_size);
dump_ge_regions(entry);
return -EFAULT;
}
return 0;
}
int ged_ge_get(int ge_fd, int region_id, int u32_offset,
int u32_size, uint32_t *output_data)
{
unsigned long flags;
int err = 0;
uint32_t i;
struct GEEntry *entry = NULL;
uint32_t *pregion_data = NULL;
struct file *file = fget(ge_fd);
if (file == NULL || file->f_op != &GEEntry_fops) {
GED_PDEBUG("fail, invalid ge_fd %d\n", ge_fd);
if (file)
fput(file);
return -EFAULT;
}
entry = file->private_data;
if (valid_parameters(entry, region_id, u32_offset, u32_size)) {
err = -EFAULT;
goto err_parameter;
}
spin_lock_irqsave(&ge_raf_lock, flags);
pregion_data = entry->region_data[region_id];
spin_unlock_irqrestore(&ge_raf_lock, flags);
if (pregion_data) {
for (i = 0; i < u32_size; ++i)
output_data[i] = pregion_data[u32_offset + i];
} else {
for (i = 0; i < u32_size; ++i)
output_data[i] = 0;
}
err_parameter:
fput(file);
return err;
}
int ged_ge_set(int ge_fd, int region_id, int u32_offset,
int u32_size, uint32_t *input_data)
{
unsigned long flags;
int err = 0;
uint32_t i;
struct GEEntry *entry = NULL;
uint32_t *pregion_data = NULL;
struct file *file = fget(ge_fd);
if (file == NULL || file->f_op != &GEEntry_fops) {
GED_PDEBUG("fail, invalid ge_fd %d\n", ge_fd);
if (file)
fput(file);
return -EFAULT;
}
entry = file->private_data;
if (valid_parameters(entry, region_id, u32_offset, u32_size)) {
err = -EFAULT;
goto err_parameter;
}
spin_lock_irqsave(&ge_raf_lock, flags);
while (!entry->region_data[region_id]) {
void *data = NULL;
spin_unlock_irqrestore(&ge_raf_lock, flags);
data = kzalloc(entry->region_sizes[region_id], GFP_KERNEL);
if (!data) {
GED_PDEBUG("kmalloc fail, size: %d\n",
entry->region_sizes[region_id]);
err = -EFAULT;
goto err_parameter;
}
spin_lock_irqsave(&ge_raf_lock, flags);
/* Check again.
* Assign data to entry->region_data[region_id]
* if it still is NULL.
*/
if (entry->region_data[region_id]) {
spin_unlock_irqrestore(&ge_raf_lock, flags);
kfree(data);
spin_lock_irqsave(&ge_raf_lock, flags);
break;
}
entry->region_data[region_id] = data;
/* Avoid kmemleak scan false positive */
kmemleak_ignore(data);
}
pregion_data = entry->region_data[region_id];
spin_unlock_irqrestore(&ge_raf_lock, flags);
for (i = 0; i < u32_size; ++i)
pregion_data[u32_offset + i] = input_data[i];
err_parameter:
fput(file);
return err;
}
int ged_bridge_ge_alloc(
struct GED_BRIDGE_IN_GE_ALLOC *psALLOC_IN,
struct GED_BRIDGE_OUT_GE_ALLOC *psALLOC_OUT)
{
psALLOC_OUT->ge_fd =
ged_ge_alloc(psALLOC_IN->region_num, psALLOC_IN->region_sizes);
psALLOC_OUT->eError = !!(psALLOC_OUT->ge_fd) ? GED_OK : GED_ERROR_OOM;
return 0;
}
int ged_bridge_ge_get(
struct GED_BRIDGE_IN_GE_GET *psGET_IN,
struct GED_BRIDGE_OUT_GE_GET *psGET_OUT,
int output_buffer_size)
{
/* in gpu_ext/ged/lib/ged_ge.cpp. ged_ge_get()
* iOutSize will show the header size and data size.
*/
int header_size = sizeof(struct GED_BRIDGE_OUT_GE_GET);
if (psGET_IN->uint32_offset < 0 ||
psGET_IN->uint32_offset >= (GE_MAX_REGION_SIZE / sizeof(uint32_t)) ||
psGET_IN->uint32_size <= 0 ||
psGET_IN->uint32_size > (GE_MAX_REGION_SIZE / sizeof(uint32_t))) {
GED_PDEBUG("[%s] invalid offset(%d) or size(%d)",
__func__,
psGET_IN->uint32_offset,
psGET_IN->uint32_size);
return -EFAULT;
}
// check output buffer alloc size
if (output_buffer_size !=
header_size +
(psGET_IN->uint32_size * sizeof(uint32_t))) {
GED_PDEBUG("[%s] output_buffer_size (%d byte) != header_size + u32_size (%d byte)",
__func__,
(unsigned int)output_buffer_size,
(unsigned int)(header_size +
(psGET_IN->uint32_size * sizeof(uint32_t))));
return -EFAULT;
}
psGET_OUT->eError = ged_ge_get(
psGET_IN->ge_fd,
psGET_IN->region_id,
psGET_IN->uint32_offset,
psGET_IN->uint32_size,
psGET_OUT->data);
return 0;
}
int ged_bridge_ge_set(
struct GED_BRIDGE_IN_GE_SET *psSET_IN,
struct GED_BRIDGE_OUT_GE_SET *psSET_OUT,
int input_buffer_size)
{
int header_size = sizeof(struct GED_BRIDGE_IN_GE_SET);
if (psSET_IN->uint32_offset < 0 ||
psSET_IN->uint32_offset >= (GE_MAX_REGION_SIZE / sizeof(uint32_t)) ||
psSET_IN->uint32_size <= 0 ||
psSET_IN->uint32_size > (GE_MAX_REGION_SIZE / sizeof(uint32_t))) {
GED_PDEBUG("[%s] invalid offset(%d) or size(%d)",
__func__,
psSET_IN->uint32_offset,
psSET_IN->uint32_size);
return -EFAULT;
}
// check input buffer alloc size
if (input_buffer_size !=
header_size +
(psSET_IN->uint32_size * sizeof(uint32_t))) {
GED_PDEBUG("[%s] input_buffer_size (%d byte) != header_size + u32_size (%d byte)",
__func__,
(unsigned int)input_buffer_size,
(unsigned int)(header_size +
(psSET_IN->uint32_size * sizeof(uint32_t))));
return -EFAULT;
}
psSET_OUT->eError = ged_ge_set(
psSET_IN->ge_fd,
psSET_IN->region_id,
psSET_IN->uint32_offset,
psSET_IN->uint32_size,
psSET_IN->data);
return 0;
}
int ged_bridge_ge_info(
struct GED_BRIDGE_IN_GE_INFO *psINFO_IN,
struct GED_BRIDGE_OUT_GE_INFO *psINFO_OUT)
{
struct file *file = fget(psINFO_IN->ge_fd);
if (file == NULL || file->f_op != &GEEntry_fops) {
GED_PDEBUG("ged_ge fail, invalid ge_fd %d\n", psINFO_IN->ge_fd);
if (file)
fput(file);
return -EFAULT;
}
psINFO_OUT->unique_id =
((struct GEEntry *)(file->private_data))->unique_id;
psINFO_OUT->eError = GED_OK;
fput(file);
return 0;
}
+26
View File
@@ -0,0 +1,26 @@
/* SPDX-License-Identifier: GPL-2.0 */
/*
* Copyright (c) 2019 MediaTek Inc.
*/
#ifndef __GED_GE_H__
#define __GED_GE_H__
#include <linux/types.h>
#include <ged_type.h>
/* Must be the same as item number in region_sizes[], which in
* /vendor/mediatek/proprietary/hardware/gralloc_extra/ge_misc.cpp
*/
#define GE_ALLOC_STRUCT_NUM 18
#define GE_MAX_REGION_SIZE 8192
GED_ERROR ged_ge_init(void);
int ged_ge_exit(void);
int ged_ge_alloc(int region_num, uint32_t *region_sizes);
int ged_ge_get(int ge_fd, int region_id, int u32_offset,
int u32_size, uint32_t *output_data);
int ged_ge_set(int ge_fd, int region_id, int u32_offset,
int u32_size, uint32_t *input_data);
#endif
@@ -0,0 +1,618 @@
// SPDX-License-Identifier: GPL-2.0
/*
* Copyright (c) 2019 MediaTek Inc.
*/
#include "ged_gpu_tuner.h"
#include "ged_base.h"
#include "ged_log.h"
#include "ged_sysfs.h"
#define DEBUG_ON 1
#define DEBUG_OFF 0
const char *global_packagename = "-1";
static struct mutex gsGPUTunerLock;
static struct GED_GPU_TUNER_ITEM gpu_tuner_status;
static struct GED_GPU_TUNER_HINT gpu_tuner_last_custom_hint;
static struct list_head gItemList;
static struct kobject *gpu_tuner_kobj;
static int debug = DEBUG_OFF;
#define GPU_TUNER_TAG "[GT]"
#define GPU_TUNER_DEBUG(fmt, args...) \
do { \
if (debug == DEBUG_ON) \
pr_debug(GPU_TUNER_TAG fmt, ##args); \
} while (0)
#define GPU_TUNER_INFO(fmt, args...) pr_info(GPU_TUNER_TAG fmt, ##args)
#define GPU_TUNER_ERROR(fmt, args...) pr_info(GPU_TUNER_TAG fmt, ##args)
static GED_ERROR _tolower_s(
char *p)
{
GED_ERROR ret = GED_OK;
const int tolower = 'a' - 'A';
int str_len = strlen(p);
if (*(p + str_len) != '\0')
return GED_ERROR_INVALID_PARAMS;
for ( ; *p; ++p) {
if (*p >= 'A' && *p <= 'Z')
*p += tolower;
}
return ret;
}
static GED_ERROR _translateCmdToFeature(
char *cmd,
int *feature)
{
GED_ERROR ret = GED_OK;
if (!cmd || !feature)
return GED_ERROR_INVALID_PARAMS;
if (!strncmp("anisotropic_disable", cmd, strlen(cmd)))
*feature = MTK_GPU_TUNER_ANISOTROPIC_DISABLE;
else if (!strncmp("trilinear_disable", cmd, strlen(cmd)))
*feature = MTK_GPU_TUNER_TRILINEAR_DISABLE;
else
ret = GED_ERROR_FAIL;
return ret;
}
int _tokenizer(
char *src,
int len,
int *index,
int indexNum)
{
int i, j, head;
if (!src || !index)
return GED_ERROR_INVALID_PARAMS;
i = j = 0;
head = -1;
for ( ; i < len; i++) {
if (src[i] != ' ') {
if (head == -1)
head = i;
} else {
if (head != -1) {
index[j] = head;
j++;
if (j == indexNum)
return j;
head = -1;
}
src[i] = 0;
}
}
if (head != -1) {
index[j] = head;
j++;
return j;
}
return -1;
}
static void _update_all_with_global_status(void)
{
struct list_head *listentry;
struct GED_GPU_TUNER_ITEM *item;
list_for_each(listentry, &gItemList) {
item = list_entry(listentry, struct GED_GPU_TUNER_ITEM, List);
if (item) {
mutex_lock(&gsGPUTunerLock);
item->status.feature &= gpu_tuner_status.status.feature;
mutex_unlock(&gsGPUTunerLock);
GPU_TUNER_DEBUG(
"[%s] item feature(%08x) global feature(%8x)\n",
__func__, item->status.feature,
gpu_tuner_status.status.feature);
}
}
}
static struct GED_GPU_TUNER_ITEM *_ged_gpu_tuner_find_item_by_package_name(
char *packagename)
{
struct list_head *listentry;
struct GED_GPU_TUNER_ITEM *item;
char *p = packagename;
GED_ERROR err = GED_OK;
if (!packagename)
return NULL;
err = _tolower_s(p);
if (err != GED_OK)
return NULL;
list_for_each(listentry, &gItemList) {
item = list_entry(listentry,
struct GED_GPU_TUNER_ITEM, List);
if (item &&
!(strncmp(item->status.packagename, packagename,
strlen(packagename)))) {
GPU_TUNER_DEBUG(
"[%s] package_name(%s) feature(%08x) value(%d)\n",
__func__, item->status.packagename,
item->status.feature, item->status.value);
return item;
}
}
return NULL;
}
//-----------------------------------------------------------------------------
static ssize_t dump_status_show(struct kobject *kobj,
struct kobj_attribute *attr,
char *buf)
{
struct list_head *listentry;
struct GED_GPU_TUNER_ITEM *item;
int cnt = 0;
int pos = 0;
int length;
length = scnprintf(buf + pos, PAGE_SIZE - pos,
"========================================\n"
"[Global Status]\n"
"feature(%08x)\n"
"========================================\n",
gpu_tuner_status.status.feature);
pos += length;
list_for_each(listentry, &gItemList) {
item = list_entry(listentry, struct GED_GPU_TUNER_ITEM, List);
if (item) {
length = scnprintf(buf + pos,
PAGE_SIZE - pos,
" [%d]\n"
" pkgname(%s) cmd(%s) feature(%08x)\n"
"========================================\n",
cnt++,
item->status.packagename,
(*item->status.cmd) ?
item->status.cmd : "N/A",
item->status.feature);
pos += length;
}
}
return pos;
}
static KOBJ_ATTR_RO(dump_status);
//-----------------------------------------------------------------------------
static ssize_t custom_hint_set_store(struct kobject *kobj,
struct kobj_attribute *attr,
const char *buf, size_t count)
{
#define NUM_TOKEN 3
/*
* This proc node accept only: [PACKAGE NAME][CMD][VALUE]
* for ex: "packagename anisotropic_disable 1"
*
*/
char acBuffer[GED_SYSFS_MAX_BUFF_SIZE] = {0};
int index[NUM_TOKEN] = {0};
int i;
char *packagename, *cmd, *val;
int value = 0;
int feature, len;
GED_ERROR ret;
if (!((count > 0) && (count < GED_SYSFS_MAX_BUFF_SIZE)))
return GED_ERROR_INVALID_PARAMS;
if (!scnprintf(acBuffer, GED_SYSFS_MAX_BUFF_SIZE, "%s", buf))
return GED_ERROR_FAIL;
ret = _tokenizer(acBuffer, count, index, NUM_TOKEN);
GPU_TUNER_DEBUG("retOfTokenizer(%d) acBuffer(%s)n", ret, acBuffer);
if (ret == NUM_TOKEN) {
for (i = 0; i < ret; i++) {
GPU_TUNER_DEBUG("index[%d] = %s\n",
i, acBuffer + index[i]);
}
packagename = acBuffer + index[0];
cmd = acBuffer + index[1];
val = acBuffer + index[2];
ret = _translateCmdToFeature(cmd, &feature);
if (ret != GED_OK) {
GPU_TUNER_ERROR("[%s] No recognize cmd %s\n",
__func__, cmd);
return GED_ERROR_FAIL;
}
ret = kstrtoint(val, 0, &value);
if (value != 0)
ret = ged_gpu_tuner_hint_set(packagename, feature);
else
ret = ged_gpu_tuner_hint_restore(packagename, feature);
len = strlen(packagename);
strncpy(gpu_tuner_last_custom_hint.packagename,
packagename, len);
strncpy(gpu_tuner_last_custom_hint.cmd, cmd, strlen(cmd));
gpu_tuner_last_custom_hint.feature = feature;
gpu_tuner_last_custom_hint.value = value;
GPU_TUNER_DEBUG(
"[last_hint] name(%s) cmd(%s) feature(%08x) value(%d)\n",
gpu_tuner_last_custom_hint.packagename,
gpu_tuner_last_custom_hint.cmd,
gpu_tuner_last_custom_hint.feature,
gpu_tuner_last_custom_hint.value);
} else {
GPU_TUNER_ERROR("[%s]invalid input\n", __func__);
return GED_ERROR_FAIL;
}
return count;
#undef NUM_TOKEN
}
static ssize_t custom_hint_set_show(struct kobject *kobj,
struct kobj_attribute *attr,
char *buf)
{
int pos = 0;
int length;
length = scnprintf(buf + pos, PAGE_SIZE - pos,
"support cmd list\n"
"anisotropic_disable => MTK_GPU_TUNER_ANISOTROPIC_DISABLE\n"
"trilinear_disable => MTK_GPU_TUNER_TRILINEAR_DISABLE\n"
"========================================\n");
pos += length;
if (gpu_tuner_last_custom_hint.packagename[0]) {
length = scnprintf(buf + pos, PAGE_SIZE - pos,
" name(%s) cmd(%s) feature(%d) value(%d)\n",
gpu_tuner_last_custom_hint.packagename,
gpu_tuner_last_custom_hint.cmd,
gpu_tuner_last_custom_hint.feature,
gpu_tuner_last_custom_hint.value);
pos += length;
} else {
length = scnprintf(buf + pos, PAGE_SIZE - pos,
"no custom hint is set\n");
pos += length;
}
return pos;
}
static KOBJ_ATTR_RW(custom_hint_set);
//-----------------------------------------------------------------------------
static ssize_t debug_store(struct kobject *kobj, struct kobj_attribute *attr,
const char *buf, size_t count)
{
#define NUM_TOKEN 1
char acBuffer[GED_SYSFS_MAX_BUFF_SIZE] = {0};
char *val;
int index[NUM_TOKEN] = {0};
int value = 0;
GED_ERROR ret;
if (!((count > 0) && (count < GED_SYSFS_MAX_BUFF_SIZE)))
return GED_ERROR_INVALID_PARAMS;
if (!scnprintf(acBuffer, GED_SYSFS_MAX_BUFF_SIZE, "%s", buf))
return GED_ERROR_FAIL;
ret = _tokenizer(acBuffer, count, index, NUM_TOKEN);
if (ret == NUM_TOKEN) {
val = acBuffer + index[0];
ret = kstrtoint(val, 0, &value);
GPU_TUNER_INFO("debug(%d)\n", value);
debug = value;
} else {
GPU_TUNER_ERROR("[%s] invalid input\n", __func__);
return GED_ERROR_FAIL;
}
return count;
#undef NUM_TOKEN
}
static ssize_t debug_show(struct kobject *kobj, struct kobj_attribute *attr,
char *buf)
{
return scnprintf(buf, PAGE_SIZE, "debug(%d)\n", debug);
}
static KOBJ_ATTR_RW(debug);
//-----------------------------------------------------------------------------
GED_ERROR ged_gpu_get_stauts_by_packagename(
char *packagename,
struct GED_GPU_TUNER_ITEM *status)
{
struct GED_GPU_TUNER_ITEM *item = NULL;
char *p = packagename;
GED_ERROR err = GED_OK;
if (!status || !packagename) {
GPU_TUNER_ERROR("[%s] invalid parameter\n", __func__);
return GED_ERROR_INVALID_PARAMS;
}
GPU_TUNER_DEBUG("[%s][IN] name(%s)\n", __func__, packagename);
err = _tolower_s(p);
if (err != GED_OK)
return err;
if (!strncmp(packagename, global_packagename,
strlen(global_packagename)))
status->status.feature |= gpu_tuner_status.status.feature;
else {
item = _ged_gpu_tuner_find_item_by_package_name(packagename);
if (item) {
item->status.feature |= gpu_tuner_status.status.feature;
status->status.feature = item->status.feature;
GPU_TUNER_DEBUG("[%s] in(%08x) out(%08x)\n",
__func__, item->status.feature, status->status.feature);
}
}
GPU_TUNER_DEBUG("[%s][OUT] found(%d)\n", __func__, (item) ? 1 : 0);
return (item) ? GED_OK : GED_ERROR_FAIL;
}
GED_ERROR ged_gpu_tuner_hint_set(
char *packagename,
enum GPU_TUNER_FEATURE eFeature)
{
GED_ERROR err = GED_OK;
struct GED_GPU_TUNER_ITEM *item = NULL;
int len = 0;
bool find = false;
char *p = packagename;
if (!packagename || (strlen(packagename) > (BUF_LEN - 1))) {
GPU_TUNER_ERROR("[%s] invalid parameter\n", __func__);
return GED_ERROR_INVALID_PARAMS;
}
GPU_TUNER_DEBUG("[%s][IN] name(%s) feature(%08x)\n",
__func__, packagename, eFeature);
err = _tolower_s(p);
if (err != GED_OK)
return err;
if (!strncmp(packagename, global_packagename,
strlen(global_packagename))) {
mutex_lock(&gsGPUTunerLock);
gpu_tuner_status.status.feature |= eFeature;
mutex_unlock(&gsGPUTunerLock);
_update_all_with_global_status();
} else {
item = _ged_gpu_tuner_find_item_by_package_name(packagename);
if (item) {
find = true;
GPU_TUNER_DEBUG("[%s] item(%p)\n", __func__, item);
} else {
item =
(struct GED_GPU_TUNER_ITEM *)
ged_alloc_atomic(sizeof(struct GED_GPU_TUNER_ITEM));
if (unlikely(item == NULL)) {
GPU_TUNER_ERROR("[%s] Fail to create item\n",
__func__);
err = GED_ERROR_FAIL;
goto ERROR;
}
memset(item->status.packagename, 0, BUF_LEN);
len = strlen(packagename);
strncpy(item->status.packagename, packagename, len);
item->status.feature = item->status.value = 0;
memset(item->status.cmd, 0, BUF_LEN);
}
mutex_lock(&gsGPUTunerLock);
item->status.feature |= eFeature;
item->status.feature |= gpu_tuner_status.status.feature;
item->status.value |= eFeature;
item->status.value |= gpu_tuner_status.status.feature;
if (find == false) {
INIT_LIST_HEAD(&item->List);
list_add_tail(&item->List, &gItemList);
}
mutex_unlock(&gsGPUTunerLock);
}
if (item)
GPU_TUNER_DEBUG("[%s]name(%s) feature(%d)\n",
__func__, item->status.packagename,
item->status.feature);
GPU_TUNER_DEBUG(
"[%s]input feature(%08x) global status(%08x) err(%d)\n",
__func__, eFeature, gpu_tuner_status.status.feature, err);
ERROR:
GPU_TUNER_DEBUG("[%s][OUT] err(%d)\n", __func__, err);
return err;
}
GED_ERROR ged_gpu_tuner_hint_restore(
char *packagename,
enum GPU_TUNER_FEATURE eFeature)
{
GED_ERROR err = GED_OK;
struct GED_GPU_TUNER_ITEM *item = NULL;
char *p = packagename;
if (!packagename) {
GPU_TUNER_ERROR("[%s] invalid parameter\n", __func__);
return GED_ERROR_INVALID_PARAMS;
}
GPU_TUNER_DEBUG("[%s][IN] name(%s) feature(%08x)\n",
__func__, packagename, eFeature);
err = _tolower_s(p);
if (err != GED_OK)
return err;
if (!strncmp(packagename, global_packagename,
strlen(global_packagename))) {
mutex_lock(&gsGPUTunerLock);
gpu_tuner_status.status.feature &= ~eFeature;
mutex_unlock(&gsGPUTunerLock);
_update_all_with_global_status();
} else {
item = _ged_gpu_tuner_find_item_by_package_name(packagename);
if (!item) {
GPU_TUNER_ERROR("[%s]Can't find matched item\n",
__func__);
goto ERROR;
}
mutex_lock(&gsGPUTunerLock);
item->status.feature &= ~eFeature;
item->status.feature |= gpu_tuner_status.status.feature;
mutex_unlock(&gsGPUTunerLock);
if (item)
GPU_TUNER_DEBUG("[%s]name(%s) feature(%d) item(%p)\n",
__func__, item->status.packagename,
item->status.feature, item);
GPU_TUNER_DEBUG("[%s]input feature(%08x) global status(%08x) err(%d)\n",
__func__, eFeature, gpu_tuner_status.status.feature, err);
}
ERROR:
GPU_TUNER_DEBUG("[%s][OUT] err(%d)\n", __func__, err);
return err;
}
GED_ERROR ged_gpu_tuner_init(void)
{
GED_ERROR err = GED_OK;
GPU_TUNER_DEBUG("[%s] In\n", __func__);
INIT_LIST_HEAD(&gItemList);
mutex_init(&gsGPUTunerLock);
gpu_tuner_status.status.feature = gpu_tuner_status.status.value = 0;
debug = false;
err = ged_sysfs_create_dir(NULL, "gpu_tuner", &gpu_tuner_kobj);
if (unlikely(err != GED_OK)) {
GPU_TUNER_ERROR("[%s] failed to create gpu_tuner dir!\n",
__func__);
goto ERROR;
}
err = ged_sysfs_create_file(gpu_tuner_kobj, &kobj_attr_dump_status);
if (unlikely(err != GED_OK)) {
GPU_TUNER_ERROR("[%s] failed to create dump_status entry!\n",
__func__);
goto ERROR;
}
err = ged_sysfs_create_file(gpu_tuner_kobj, &kobj_attr_custom_hint_set);
if (unlikely(err != GED_OK)) {
GPU_TUNER_ERROR(
"[%s] failed to create custom_hint_set entry!\n",
__func__);
goto ERROR;
}
err = ged_sysfs_create_file(gpu_tuner_kobj, &kobj_attr_debug);
if (unlikely(err != GED_OK)) {
GPU_TUNER_ERROR("[%s] failed to create debug entry!\n",
__func__);
goto ERROR;
}
ERROR:
GPU_TUNER_DEBUG("[%s] Out ret(%d)\n", __func__, err);
return err;
}
GED_ERROR ged_gpu_tuner_exit(void)
{
struct list_head *listentry, *listentryTmp;
struct GED_GPU_TUNER_ITEM *item;
GPU_TUNER_DEBUG("[%s] In\n", __func__);
mutex_destroy(&gsGPUTunerLock);
list_for_each_safe(listentry, listentryTmp, &gItemList) {
item = list_entry(listentry, struct GED_GPU_TUNER_ITEM, List);
if (item) {
list_del(&item->List);
ged_free(item, sizeof(struct GED_GPU_TUNER_ITEM));
}
}
ged_sysfs_remove_file(gpu_tuner_kobj, &kobj_attr_debug);
ged_sysfs_remove_file(gpu_tuner_kobj, &kobj_attr_custom_hint_set);
ged_sysfs_remove_file(gpu_tuner_kobj, &kobj_attr_dump_status);
ged_sysfs_remove_dir(&gpu_tuner_kobj);
GPU_TUNER_DEBUG("[%s] Out\n", __func__);
return GED_OK;
}
int ged_bridge_gpu_tuner_status(
struct GED_BRIDGE_IN_GPU_TUNER_STATUS *in,
struct GED_BRIDGE_OUT_GPU_TUNER_STATUS *out)
{
struct GED_GPU_TUNER_ITEM item;
GED_ERROR err = GED_ERROR_FAIL;
if (!in || !out) {
GPU_TUNER_ERROR("[%s] invalid parameter\n", __func__);
return GED_ERROR_INVALID_PARAMS;
}
in->name[GPU_TUNER_BUF_NAME_LEN - 1] = '\0';
GPU_TUNER_DEBUG("[%s][IN] name(%s)\n", __func__, in->name);
item.status.feature = 0;
in->name[sizeof(in->name)-1] = '\0';
err = ged_gpu_get_stauts_by_packagename(in->name, &item);
if (err == GED_OK) {
out->feature = item.status.feature;
GPU_TUNER_DEBUG("[%s] out_feature(%08x) in_feature(%08x)\n",
__func__, out->feature, item.status.feature);
}
out->feature |= gpu_tuner_status.status.feature;
GPU_TUNER_DEBUG("[%s][OUT] ret(%d) out(%08x)\n",
__func__, err, (out) ? out->feature : -1);
return GED_OK;
}
+915
View File
@@ -0,0 +1,915 @@
// SPDX-License-Identifier: GPL-2.0
/*
* Copyright (c) 2019 MediaTek Inc.
*/
#include <linux/version.h>
#include <linux/io.h>
#include <linux/mm.h>
#include <linux/pagemap.h>
#include <linux/genalloc.h>
#include <linux/sched.h>
#include <linux/mutex.h>
#include <linux/slab.h>
#include <linux/proc_fs.h>
#include <linux/seq_file.h>
#include <linux/fb.h>
#include <mt-plat/mtk_gpu_utility.h>
#include <mtk_gpufreq.h>
#include "ged_base.h"
#include "ged_hal.h"
#include "ged_sysfs.h"
#include "ged_dvfs.h"
#include "ged_notify_sw_vsync.h"
#include "ged_kpi.h"
#include "ged_global.h"
#ifdef GED_DEBUG_FS
#include "ged_debugFS.h"
static struct dentry *gpsHALDir;
#ifdef CONFIG_MTK_GPU_OPP_STATS_SUPPORT
static struct dentry *gpsOppCostsEntry;
#endif /* CONFIG_MTK_GPU_OPP_STATS_SUPPORT */
#endif
static struct kobject *hal_kobj;
int tokenizer(char *pcSrc, int i32len, int *pi32IndexArray, int i32NumToken)
{
int i = 0;
int j = 0;
int head = -1;
for ( ; i < i32len; i++) {
if (pcSrc[i] != ' ') {
if (head == -1)
head = i;
} else {
if (head != -1) {
pi32IndexArray[j] = head;
j++;
if (j == i32NumToken)
return j;
head = -1;
}
pcSrc[i] = 0;
}
}
if (head != -1) {
pi32IndexArray[j] = head;
j++;
return j;
}
return -1;
}
/* -------------------------------------------------------------------------- */
#ifdef CONFIG_MTK_GPU_OPP_STATS_SUPPORT
#ifdef GED_DEBUG_FS
uint64_t reset_base_us;
static ssize_t ged_dvfs_opp_cost_write_entry
(const char __user *pszBuffer, size_t uiCount, loff_t uiPosition, void *pvData)
{
#define GED_HAL_DEBUGFS_SIZE 64
char acBuffer[GED_HAL_DEBUGFS_SIZE];
int i32Value;
if ((uiCount > 0) && (uiCount < GED_HAL_DEBUGFS_SIZE)) {
if (ged_copy_from_user(acBuffer, pszBuffer, uiCount) == 0) {
acBuffer[uiCount] = '\0';
if (kstrtoint(acBuffer, 0, &i32Value) == 0) {
ged_dvfs_reset_opp_cost(i32Value);
reset_base_us = ged_get_time();
reset_base_us = reset_base_us >> 10;
}
}
}
return uiCount;
}
//-------------------------------------------------------------------
static void *ged_dvfs_opp_cost_seq_start(struct seq_file *psSeqFile,
loff_t *puiPosition)
{
if (*puiPosition == 0)
return SEQ_START_TOKEN;
return NULL;
}
//-------------------------------------------------------------------
static void ged_dvfs_opp_cost_seq_stop(struct seq_file *psSeqFile,
void *pvData)
{
}
//-------------------------------------------------------------------
static void *ged_dvfs_opp_cost_seq_next(struct seq_file *psSeqFile,
void *pvData, loff_t *puiPosition)
{
return NULL;
}
//-------------------------------------------------------------------
static int ged_dvfs_opp_cost_seq_show(struct seq_file *psSeqFile,
void *pvData)
{
char acBuffer[32];
int len;
if (pvData != NULL) {
int i, j;
int cur_idx;
unsigned int ui32FqCount = 0;
unsigned int ui32TotalTrans;
struct GED_DVFS_OPP_STAT *report;
mtk_custom_get_gpu_freq_level_count(&ui32FqCount);
report = NULL;
if (ui32FqCount) {
report =
vmalloc(sizeof(struct GED_DVFS_OPP_STAT) *
ui32FqCount);
}
if (!ged_dvfs_query_opp_cost(report, ui32FqCount, false)) {
cur_idx = mt_gpufreq_get_cur_freq_index();
ui32TotalTrans = 0;
seq_puts(psSeqFile, " From : To\n");
seq_puts(psSeqFile, " :");
for (i = 0; i < ui32FqCount; i++) {
len = scnprintf(acBuffer, 32, "%10u",
1000 * mt_gpufreq_get_freq_by_idx(i));
acBuffer[len] = 0;
seq_puts(psSeqFile, acBuffer);
}
seq_puts(psSeqFile, " time(ms)\n");
for (i = 0; i < ui32FqCount; i++) {
if (i == cur_idx)
seq_puts(psSeqFile, "*");
else
seq_puts(psSeqFile, " ");
len = scnprintf(acBuffer, 32, "%10u ",
1000 * mt_gpufreq_get_freq_by_idx(i));
acBuffer[len] = 0;
seq_puts(psSeqFile, acBuffer);
for (j = 0; j < ui32FqCount; j++) {
len = scnprintf(acBuffer, 32, "%10u",
report[i].uMem.aTrans[j]);
acBuffer[len] = 0;
seq_puts(psSeqFile, acBuffer);
ui32TotalTrans +=
report[i].uMem.aTrans[j];
}
/* truncate to ms */
len = scnprintf(acBuffer, 32, "%10u\n",
(unsigned int)(report[i].ui64Active) >> 10);
acBuffer[len] = 0;
seq_puts(psSeqFile, acBuffer);
}
len = scnprintf(acBuffer, 32,
"Total transition : %u\n", ui32TotalTrans);
acBuffer[len] = 0;
seq_puts(psSeqFile, acBuffer);
} else
seq_puts(psSeqFile, "Not Supported.\n");
vfree(report);
}
return 0;
}
/* --------------------------------------------------------------- */
const struct seq_operations gsDvfsOppCostsReadOps = {
.start = ged_dvfs_opp_cost_seq_start,
.stop = ged_dvfs_opp_cost_seq_stop,
.next = ged_dvfs_opp_cost_seq_next,
.show = ged_dvfs_opp_cost_seq_show,
};
#endif
//-----------------------------------------------------------------------------
static ssize_t opp_logs_show(struct kobject *kobj,
struct kobj_attribute *attr,
char *buf)
{
int len;
int i, j;
int cur_idx;
unsigned int ui32FqCount = 0;
struct GED_DVFS_OPP_STAT *report;
mtk_custom_get_gpu_freq_level_count(&ui32FqCount);
report = NULL;
if (ui32FqCount) {
report =
vmalloc(sizeof(struct GED_DVFS_OPP_STAT) *
ui32FqCount);
}
if (!ged_dvfs_query_opp_cost(report, ui32FqCount, false)) {
cur_idx = mt_gpufreq_get_cur_freq_index();
len = sprintf(buf, " time(ms)\n");
for (i = 0; i < ui32FqCount; i++) {
if (i == cur_idx)
len += sprintf(buf + len, "*");
else
len += sprintf(buf + len, " ");
len += sprintf(buf + len, "%10u",
1000 * mt_gpufreq_get_freq_by_idx(i));
/* truncate to ms */
len += sprintf(buf + len, "%10u\n",
(unsigned int)(report[i].ui64Active >> 10));
}
} else
len = sprintf(buf, "Not Supported.\n");
vfree(report);
return len;
}
static KOBJ_ATTR_RO(opp_logs);
#endif /* CONFIG_MTK_GPU_OPP_STATS_SUPPORT */
//-----------------------------------------------------------------------------
static ssize_t total_gpu_freq_level_count_show(struct kobject *kobj,
struct kobj_attribute *attr,
char *buf)
{
unsigned int ui32FreqLevelCount;
if (false == mtk_custom_get_gpu_freq_level_count(&ui32FreqLevelCount))
ui32FreqLevelCount = 0;
return scnprintf(buf, PAGE_SIZE, "%u\n", ui32FreqLevelCount);
}
static KOBJ_ATTR_RO(total_gpu_freq_level_count);
//-----------------------------------------------------------------------------
static ssize_t custom_boost_gpu_freq_show(struct kobject *kobj,
struct kobj_attribute *attr,
char *buf)
{
unsigned int ui32BoostGpuFreqLevel;
if (false == mtk_get_custom_boost_gpu_freq(&ui32BoostGpuFreqLevel))
ui32BoostGpuFreqLevel = 0;
return scnprintf(buf, PAGE_SIZE, "%u\n", ui32BoostGpuFreqLevel);
}
static ssize_t custom_boost_gpu_freq_store(struct kobject *kobj,
struct kobj_attribute *attr,
const char *buf, size_t count)
{
char acBuffer[GED_SYSFS_MAX_BUFF_SIZE];
int i32Value;
if ((count > 0) && (count < GED_SYSFS_MAX_BUFF_SIZE)) {
if (scnprintf(acBuffer, GED_SYSFS_MAX_BUFF_SIZE, "%s", buf)) {
if (kstrtoint(acBuffer, 0, &i32Value) == 0) {
if (i32Value < 0)
i32Value = 0;
mtk_custom_boost_gpu_freq(i32Value);
}
}
}
return count;
}
static KOBJ_ATTR_RW(custom_boost_gpu_freq);
//-----------------------------------------------------------------------------
static ssize_t custom_upbound_gpu_freq_show(struct kobject *kobj,
struct kobj_attribute *attr,
char *buf)
{
unsigned int ui32UpboundGpuFreqLevel;
int pos = 0;
int length;
if (false == mtk_get_custom_upbound_gpu_freq(
&ui32UpboundGpuFreqLevel)) {
ui32UpboundGpuFreqLevel = 0;
length = scnprintf(buf + pos, PAGE_SIZE - pos,
"call mtk_get_custom_upbound_gpu_freq false\n");
pos += length;
}
length = scnprintf(buf + pos, PAGE_SIZE - pos,
"%u\n", ui32UpboundGpuFreqLevel);
pos += length;
return pos;
}
static ssize_t custom_upbound_gpu_freq_store(struct kobject *kobj,
struct kobj_attribute *attr,
const char *buf, size_t count)
{
char acBuffer[GED_SYSFS_MAX_BUFF_SIZE];
int i32Value;
if ((count > 0) && (count < GED_SYSFS_MAX_BUFF_SIZE)) {
if (scnprintf(acBuffer, GED_SYSFS_MAX_BUFF_SIZE, "%s", buf)) {
if (kstrtoint(acBuffer, 0, &i32Value) == 0) {
if (i32Value < 0)
i32Value = 0;
mtk_custom_upbound_gpu_freq(i32Value);
}
}
}
return count;
}
static KOBJ_ATTR_RW(custom_upbound_gpu_freq);
//-----------------------------------------------------------------------------
static ssize_t current_freqency_show(struct kobject *kobj,
struct kobj_attribute *attr,
char *buf)
{
struct GED_DVFS_FREQ_DATA sFreqInfo;
ged_dvfs_get_gpu_cur_freq(&sFreqInfo);
return scnprintf(buf, PAGE_SIZE, "%u %lu\n",
sFreqInfo.ui32Idx, sFreqInfo.ulFreq);
}
static KOBJ_ATTR_RO(current_freqency);
//-----------------------------------------------------------------------------
static ssize_t previous_freqency_show(struct kobject *kobj,
struct kobj_attribute *attr,
char *buf)
{
struct GED_DVFS_FREQ_DATA sFreqInfo;
ged_dvfs_get_gpu_pre_freq(&sFreqInfo);
return scnprintf(buf, PAGE_SIZE, "%u %lu\n",
sFreqInfo.ui32Idx, sFreqInfo.ulFreq);
}
static KOBJ_ATTR_RO(previous_freqency);
//-----------------------------------------------------------------------------
static ssize_t gpu_utilization_show(struct kobject *kobj,
struct kobj_attribute *attr,
char *buf)
{
unsigned int loading = 0;
unsigned int block = 0;
unsigned int idle = 0;
mtk_get_gpu_loading(&loading);
mtk_get_gpu_block(&block);
mtk_get_gpu_idle(&idle);
return scnprintf(buf, PAGE_SIZE, "%u %u %u\n", loading, block, idle);
}
static KOBJ_ATTR_RO(gpu_utilization);
//-----------------------------------------------------------------------------
static int32_t _boost_level = -1;
#define MAX_BOOST_DIGITS 10
static ssize_t gpu_boost_level_show(struct kobject *kobj,
struct kobj_attribute *attr,
char *buf)
{
return scnprintf(buf, PAGE_SIZE, "%d\n", _boost_level);
}
static ssize_t gpu_boost_level_store(struct kobject *kobj,
struct kobj_attribute *attr,
const char *buf, size_t count)
{
char str_num[MAX_BOOST_DIGITS];
long val;
if (count > 0 && count < MAX_BOOST_DIGITS) {
if (scnprintf(str_num, MAX_BOOST_DIGITS, "%s", buf)) {
if (kstrtol(str_num, 10, &val) == 0)
_boost_level = (int32_t)val;
}
}
return count;
}
static KOBJ_ATTR_RW(gpu_boost_level);
//-----------------------------------------------------------------------------
int ged_dvfs_boost_value(void)
{
return _boost_level;
}
//-----------------------------------------------------------------------------
#ifdef MTK_GED_KPI
static ssize_t ged_kpi_show(struct kobject *kobj, struct kobj_attribute *attr,
char *buf)
{
unsigned int fps;
unsigned int cpu_time;
unsigned int gpu_time;
unsigned int response_time;
unsigned int gpu_remained_time;
unsigned int cpu_remained_time;
unsigned int gpu_freq;
fps = ged_kpi_get_cur_fps();
cpu_time = ged_kpi_get_cur_avg_cpu_time();
gpu_time = ged_kpi_get_cur_avg_gpu_time();
response_time = ged_kpi_get_cur_avg_response_time();
cpu_remained_time = ged_kpi_get_cur_avg_cpu_remained_time();
gpu_remained_time = ged_kpi_get_cur_avg_gpu_remained_time();
gpu_freq = ged_kpi_get_cur_avg_gpu_freq();
return scnprintf(buf, PAGE_SIZE, "%u,%u,%u,%u,%u,%u,%u\n",
fps, cpu_time, gpu_time, response_time,
cpu_remained_time, gpu_remained_time, gpu_freq);
}
static KOBJ_ATTR_RO(ged_kpi);
#endif /* MTK_GED_KPI */
//-----------------------------------------------------------------------------
#if (defined(GED_ENABLE_FB_DVFS) && defined(GED_ENABLE_DYNAMIC_DVFS_MARGIN))
static ssize_t dvfs_margin_value_show(struct kobject *kobj,
struct kobj_attribute *attr,
char *buf)
{
int i32DvfsMarginValue;
int pos = 0;
int length;
if (false == mtk_get_dvfs_margin_value(&i32DvfsMarginValue)) {
i32DvfsMarginValue = 0;
length = scnprintf(buf + pos, PAGE_SIZE - pos,
"call mtk_get_dvfs_margin_value false\n");
pos += length;
}
length = scnprintf(buf + pos, PAGE_SIZE - pos,
"%d\n", i32DvfsMarginValue);
pos += length;
return pos;
}
static ssize_t dvfs_margin_value_store(struct kobject *kobj,
struct kobj_attribute *attr,
const char *buf, size_t count)
{
char acBuffer[GED_SYSFS_MAX_BUFF_SIZE];
int i32Value;
if ((count > 0) && (count < GED_SYSFS_MAX_BUFF_SIZE)) {
if (scnprintf(acBuffer, GED_SYSFS_MAX_BUFF_SIZE, "%s", buf)) {
if (kstrtoint(acBuffer, 0, &i32Value) == 0)
mtk_dvfs_margin_value(i32Value);
}
}
return count;
}
static KOBJ_ATTR_RW(dvfs_margin_value);
#endif /* (defined(GED_ENABLE_FB_DVFS) && ...) */
//-----------------------------------------------------------------------------
#ifdef GED_CONFIGURE_LOADING_BASE_DVFS_STEP
static ssize_t loading_base_dvfs_step_show(struct kobject *kobj,
struct kobj_attribute *attr,
char *buf)
{
int i32StepValue;
int pos = 0;
int length;
if (false == mtk_get_loading_base_dvfs_step(&i32StepValue)) {
i32StepValue = 0;
length = scnprintf(buf + pos, PAGE_SIZE - pos,
"call mtk_get_loading_base_dvfs_step false\n");
pos += length;
}
length = scnprintf(buf + pos, PAGE_SIZE - pos,
"%x\n", i32StepValue);
pos += length;
return pos;
}
static ssize_t loading_base_dvfs_step_store(struct kobject *kobj,
struct kobj_attribute *attr,
const char *buf, size_t count)
{
char acBuffer[GED_SYSFS_MAX_BUFF_SIZE];
int i32Value;
if ((count > 0) && (count < GED_SYSFS_MAX_BUFF_SIZE)) {
if (scnprintf(acBuffer, GED_SYSFS_MAX_BUFF_SIZE, "%s", buf)) {
if (kstrtoint(acBuffer, 0, &i32Value) == 0)
mtk_loading_base_dvfs_step(i32Value);
}
}
return count;
}
static KOBJ_ATTR_RW(loading_base_dvfs_step);
#endif /* GED_CONFIGURE_LOADING_BASE_DVFS_STEP */
//-----------------------------------------------------------------------------
#ifdef GED_ENABLE_TIMER_BASED_DVFS_MARGIN
static ssize_t timer_base_dvfs_margin_show(struct kobject *kobj,
struct kobj_attribute *attr,
char *buf)
{
int i32DvfsMarginValue;
int pos = 0;
int length;
if (false == mtk_get_timer_base_dvfs_margin(&i32DvfsMarginValue)) {
i32DvfsMarginValue = 0;
length = scnprintf(buf + pos, PAGE_SIZE - pos,
"call mtk_get_timer_base_dvfs_margin false\n");
pos += length;
}
length = scnprintf(buf + pos, PAGE_SIZE - pos,
"%d\n", i32DvfsMarginValue);
pos += length;
return pos;
}
static ssize_t timer_base_dvfs_margin_store(struct kobject *kobj,
struct kobj_attribute *attr,
const char *buf, size_t count)
{
char acBuffer[GED_SYSFS_MAX_BUFF_SIZE];
int i32Value;
if ((count > 0) && (count < GED_SYSFS_MAX_BUFF_SIZE)) {
if (scnprintf(acBuffer, GED_SYSFS_MAX_BUFF_SIZE, "%s", buf)) {
if (kstrtoint(acBuffer, 0, &i32Value) == 0)
mtk_timer_base_dvfs_margin(i32Value);
}
}
return count;
}
static KOBJ_ATTR_RW(timer_base_dvfs_margin);
#endif /* GED_ENABLE_TIMER_BASED_DVFS_MARGIN */
//-----------------------------------------------------------------------------
#ifdef GED_ENABLE_DVFS_LOADING_MODE
static ssize_t dvfs_loading_mode_show(struct kobject *kobj,
struct kobj_attribute *attr,
char *buf)
{
unsigned int ui32DvfsLoadingMode;
int pos = 0;
int length;
if (false == mtk_get_dvfs_loading_mode(&ui32DvfsLoadingMode)) {
ui32DvfsLoadingMode = 0;
length = scnprintf(buf + pos, PAGE_SIZE - pos,
"call mtk_get_dvfs_loading_mode false\n");
pos += length;
}
length = scnprintf(buf + pos, PAGE_SIZE - pos,
"%d\n", ui32DvfsLoadingMode);
pos += length;
return pos;
}
static ssize_t dvfs_loading_mode_store(struct kobject *kobj,
struct kobj_attribute *attr,
const char *buf, size_t count)
{
char acBuffer[GED_SYSFS_MAX_BUFF_SIZE];
int i32Value;
if ((count > 0) && (count < GED_SYSFS_MAX_BUFF_SIZE)) {
if (scnprintf(acBuffer, GED_SYSFS_MAX_BUFF_SIZE, "%s", buf)) {
if (kstrtoint(acBuffer, 0, &i32Value) == 0)
mtk_dvfs_loading_mode(i32Value);
}
}
return count;
}
static KOBJ_ATTR_RW(dvfs_loading_mode);
#endif /* GED_ENABLE_DVFS_LOADING_MODE */
//-----------------------------------------------------------------------------
static struct notifier_block ged_fb_notifier;
static int ged_fb_notifier_callback(struct notifier_block *self,
unsigned long event, void *data)
{
struct fb_event *evdata = data;
int blank;
/* If we aren't interested in this event, skip it immediately ... */
if (event != FB_EVENT_BLANK)
return 0;
blank = *(int *)evdata->data;
switch (blank) {
case FB_BLANK_UNBLANK:
g_ui32EventStatus |= GED_EVENT_LCD;
ged_dvfs_probe_signal(GED_GAS_SIGNAL_EVENT);
break;
case FB_BLANK_POWERDOWN:
g_ui32EventStatus &= ~GED_EVENT_LCD;
ged_dvfs_probe_signal(GED_GAS_SIGNAL_EVENT);
break;
default:
break;
}
return 0;
}
struct ged_event_change_entry_t {
ged_event_change_fp callback;
void *private_data;
char name[128];
struct list_head sList;
};
static struct {
struct mutex lock;
struct list_head listen;
} g_ged_event_change = {
.lock = __MUTEX_INITIALIZER(g_ged_event_change.lock),
.listen = LIST_HEAD_INIT(g_ged_event_change.listen),
};
bool mtk_register_ged_event_change(const char *name,
ged_event_change_fp callback, void *private_data)
{
struct ged_event_change_entry_t *entry = NULL;
entry = kmalloc(sizeof(struct ged_event_change_entry_t), GFP_KERNEL);
if (entry == NULL)
return false;
entry->callback = callback;
entry->private_data = private_data;
strncpy(entry->name, name, sizeof(entry->name) - 1);
entry->name[sizeof(entry->name) - 1] = 0;
INIT_LIST_HEAD(&entry->sList);
mutex_lock(&g_ged_event_change.lock);
list_add(&entry->sList, &g_ged_event_change.listen);
mutex_unlock(&g_ged_event_change.lock);
return true;
}
bool mtk_unregister_ged_event_change(const char *name)
{
struct list_head *pos, *head;
struct ged_event_change_entry_t *entry = NULL;
mutex_lock(&g_ged_event_change.lock);
head = &g_ged_event_change.listen;
list_for_each(pos, head) {
entry = list_entry(pos, struct ged_event_change_entry_t, sList);
if (strncmp(entry->name, name, sizeof(entry->name) - 1) == 0)
break;
entry = NULL;
}
if (entry) {
list_del(&entry->sList);
kfree(entry);
}
mutex_unlock(&g_ged_event_change.lock);
return true;
}
void mtk_ged_event_notify(int events)
{
struct list_head *pos, *head;
struct ged_event_change_entry_t *entry = NULL;
mutex_lock(&g_ged_event_change.lock);
head = &g_ged_event_change.listen;
list_for_each(pos, head) {
entry = list_entry(pos, struct ged_event_change_entry_t, sList);
entry->callback(entry->private_data, events);
}
mutex_unlock(&g_ged_event_change.lock);
}
//-----------------------------------------------------------------------------
GED_ERROR ged_hal_init(void)
{
GED_ERROR err = GED_OK;
#ifdef GED_DEBUG_FS
err = ged_debugFS_create_entry_dir(
"hal",
NULL,
&gpsHALDir);
if (unlikely(err != GED_OK)) {
err = GED_ERROR_FAIL;
GED_LOGE("ged: failed to create hal dir!\n");
goto ERROR;
}
/* Report Opp Cost */
#ifdef CONFIG_MTK_GPU_OPP_STATS_SUPPORT
err = ged_debugFS_create_entry(
"opp_logs",
gpsHALDir,
&gsDvfsOppCostsReadOps,
ged_dvfs_opp_cost_write_entry,
NULL,
&gpsOppCostsEntry);
if (unlikely(err != GED_OK)) {
GED_LOGE(
"ged: failed to create opp_logs entry!\n");
goto ERROR;
}
#endif /* CONFIG_MTK_GPU_OPP_STATS_SUPPORT */
#endif
err = ged_sysfs_create_dir(NULL, "hal", &hal_kobj);
if (unlikely(err != GED_OK)) {
GED_LOGE("Failed to create hal dir!\n");
goto ERROR;
}
err = ged_sysfs_create_file(hal_kobj,
&kobj_attr_total_gpu_freq_level_count);
if (unlikely(err != GED_OK)) {
GED_LOGE(
"Failed to create total_gpu_freq_level_count entry!\n");
goto ERROR;
}
err = ged_sysfs_create_file(hal_kobj, &kobj_attr_custom_boost_gpu_freq);
if (unlikely(err != GED_OK)) {
GED_LOGE(
"Failed to create custom_boost_gpu_freq entry!\n");
goto ERROR;
}
err = ged_sysfs_create_file(hal_kobj,
&kobj_attr_custom_upbound_gpu_freq);
if (unlikely(err != GED_OK)) {
GED_LOGE(
"Failed to create custom_upbound_gpu_freq entry!\n");
goto ERROR;
}
err = ged_sysfs_create_file(hal_kobj, &kobj_attr_current_freqency);
if (unlikely(err != GED_OK)) {
GED_LOGE("Failed to create current_freqency entry!\n");
goto ERROR;
}
err = ged_sysfs_create_file(hal_kobj, &kobj_attr_previous_freqency);
if (unlikely(err != GED_OK)) {
GED_LOGE("Failed to create previous_freqency entry!\n");
goto ERROR;
}
err = ged_sysfs_create_file(hal_kobj, &kobj_attr_gpu_utilization);
if (unlikely(err != GED_OK)) {
GED_LOGE("Failed to create gpu_utilization entry!\n");
goto ERROR;
}
err = ged_sysfs_create_file(hal_kobj, &kobj_attr_gpu_boost_level);
if (unlikely(err != GED_OK)) {
GED_LOGE("Failed to create gpu_boost_level entry!\n");
goto ERROR;
}
#ifdef CONFIG_MTK_GPU_OPP_STATS_SUPPORT
err = ged_sysfs_create_file(hal_kobj, &kobj_attr_opp_logs);
if (unlikely(err != GED_OK)) {
GED_LOGE("ged: failed to create opp_logs entry!\n");
goto ERROR;
}
#endif /* CONFIG_MTK_GPU_OPP_STATS_SUPPORT */
#ifdef MTK_GED_KPI
err = ged_sysfs_create_file(hal_kobj, &kobj_attr_ged_kpi);
if (unlikely(err != GED_OK)) {
GED_LOGE("Failed to create ged_kpi entry!\n");
goto ERROR;
}
#endif
#if (defined(GED_ENABLE_FB_DVFS) && defined(GED_ENABLE_DYNAMIC_DVFS_MARGIN))
err = ged_sysfs_create_file(hal_kobj, &kobj_attr_dvfs_margin_value);
if (unlikely(err != GED_OK)) {
GED_LOGE("Failed to create dvfs_margin_value entry!\n");
goto ERROR;
}
#endif
#ifdef GED_CONFIGURE_LOADING_BASE_DVFS_STEP
err = ged_sysfs_create_file(hal_kobj,
&kobj_attr_loading_base_dvfs_step);
if (unlikely(err != GED_OK)) {
GED_LOGE(
"Failed to create loading_base_dvfs_step entry!\n");
goto ERROR;
}
#endif
#ifdef GED_ENABLE_TIMER_BASED_DVFS_MARGIN
err = ged_sysfs_create_file(hal_kobj,
&kobj_attr_timer_base_dvfs_margin);
if (unlikely(err != GED_OK)) {
GED_LOGE(
"Failed to create timer_base_dvfs_margin entry!\n");
goto ERROR;
}
#endif
#ifdef GED_ENABLE_DVFS_LOADING_MODE
err = ged_sysfs_create_file(hal_kobj, &kobj_attr_dvfs_loading_mode);
if (unlikely(err != GED_OK)) {
GED_LOGE("Failed to create dvfs_loading_mode entry!\n");
goto ERROR;
}
#endif
ged_fb_notifier.notifier_call = ged_fb_notifier_callback;
if (fb_register_client(&ged_fb_notifier))
GED_LOGE("Register fb_notifier fail!\n");
return err;
ERROR:
ged_hal_exit();
return err;
}
//-----------------------------------------------------------------------------
void ged_hal_exit(void)
{
#ifdef GED_ENABLE_DVFS_LOADING_MODE
ged_sysfs_remove_file(hal_kobj, &kobj_attr_dvfs_loading_mode);
#endif
#ifdef GED_ENABLE_TIMER_BASED_DVFS_MARGIN
ged_sysfs_remove_file(hal_kobj, &kobj_attr_timer_base_dvfs_margin);
#endif
#ifdef GED_CONFIGURE_LOADING_BASE_DVFS_STEP
ged_sysfs_remove_file(hal_kobj, &kobj_attr_loading_base_dvfs_step);
#endif
#if (defined(GED_ENABLE_FB_DVFS) && defined(GED_ENABLE_DYNAMIC_DVFS_MARGIN))
ged_sysfs_remove_file(hal_kobj, &kobj_attr_dvfs_margin_value);
#endif
#ifdef MTK_GED_KPI
ged_sysfs_remove_file(hal_kobj, &kobj_attr_ged_kpi);
#endif
#ifdef CONFIG_MTK_GPU_OPP_STATS_SUPPORT
ged_sysfs_remove_file(hal_kobj, &kobj_attr_opp_logs);
#endif /* CONFIG_MTK_GPU_OPP_STATS_SUPPORT */
ged_sysfs_remove_file(hal_kobj, &kobj_attr_gpu_boost_level);
ged_sysfs_remove_file(hal_kobj, &kobj_attr_gpu_utilization);
ged_sysfs_remove_file(hal_kobj, &kobj_attr_previous_freqency);
ged_sysfs_remove_file(hal_kobj, &kobj_attr_current_freqency);
ged_sysfs_remove_file(hal_kobj, &kobj_attr_custom_upbound_gpu_freq);
ged_sysfs_remove_file(hal_kobj, &kobj_attr_custom_boost_gpu_freq);
ged_sysfs_remove_file(hal_kobj, &kobj_attr_total_gpu_freq_level_count);
ged_sysfs_remove_dir(&hal_kobj);
}
//-----------------------------------------------------------------------------
@@ -0,0 +1,353 @@
// SPDX-License-Identifier: GPL-2.0
/*
* Copyright (c) 2019 MediaTek Inc.
*/
#include "ged_base.h"
#include "ged_hashtable.h"
#include <linux/hashtable.h>
struct GED_HASHTABLE {
unsigned int version;
unsigned int ui32Bits;
unsigned int ui32Tmp;
unsigned long ulLength;
unsigned long ulCount;
unsigned long ulCurrentID;
struct list_head *psHashTable;
};
#define HT_VERSION sizeof(struct GED_HASHTABLE)
struct GED_HASHNODE {
unsigned long ulID;
void *pvoid;
struct list_head sList;
};
#define GED_HASHTABLE_INIT_ID 1234 // 0 = invalid
static struct GED_HASHTABLE
*__ged_hashtable_verify(GED_HASHTABLE_HANDLE hHashTable)
{
struct GED_HASHTABLE *psHT = (struct GED_HASHTABLE *)hHashTable;
if (psHT && psHT->version == HT_VERSION)
return psHT;
return NULL;
}
static void *__ged_hashtable_find(struct list_head *psList, unsigned long ulID)
{
struct GED_HASHNODE *psHN;
struct list_head *psListEntry, *psListEntryTemp;
list_for_each_safe(psListEntry, psListEntryTemp, psList) {
psHN = list_entry(psListEntry, struct GED_HASHNODE, sList);
if (psHN && (psHN->ulID == ulID))
return psHN;
}
return NULL;
}
static unsigned long ged_hash(struct GED_HASHTABLE *psHT, unsigned long ulID)
{
return hash_long(ulID, psHT->ui32Bits);
}
GED_HASHTABLE_HANDLE ged_hashtable_create(unsigned int ui32Bits)
{
struct GED_HASHTABLE *psHT;
unsigned long i;
if (ui32Bits > 20) {
// 1048576 slots !?
// Need to check the necessary
return NULL;
}
psHT = (struct GED_HASHTABLE *)
ged_alloc_atomic(sizeof(struct GED_HASHTABLE));
if (psHT) {
psHT->version = HT_VERSION;
psHT->ui32Bits = ui32Bits;
psHT->ulLength = (unsigned long)(1 << ui32Bits);
psHT->ulCount = 0;
psHT->ulCurrentID = GED_HASHTABLE_INIT_ID; /* 0 = invalid */
psHT->psHashTable = (struct list_head *)
ged_alloc_atomic(psHT->ulLength * sizeof(struct list_head));
if (psHT->psHashTable) {
for (i = 0; i < psHT->ulLength; i++)
INIT_LIST_HEAD(&psHT->psHashTable[i]);
return (GED_HASHTABLE_HANDLE)psHT;
}
}
ged_hashtable_destroy(psHT);
return NULL;
}
void ged_hashtable_destroy(GED_HASHTABLE_HANDLE hHashTable)
{
struct GED_HASHTABLE *psHT = __ged_hashtable_verify(hHashTable);
if (psHT) {
unsigned long i = 0;
struct list_head *psListEntry, *psListEntryTemp;
for (; i < psHT->ulLength; i++) {
struct list_head *psList = &psHT->psHashTable[i];
list_for_each_safe(psListEntry,
psListEntryTemp, psList) {
struct GED_HASHNODE *psHN =
list_entry(psListEntry,
struct GED_HASHNODE, sList);
if (psHN) {
list_del(&psHN->sList);
ged_free(psHN,
sizeof(struct GED_HASHNODE));
}
}
}
psHT->version = 0xf2eef2ee;
/* free the hash table */
ged_free(psHT->psHashTable,
psHT->ulLength * sizeof(struct list_head));
ged_free(psHT, sizeof(struct GED_HASHTABLE));
}
}
GED_ERROR ged_hashtable_insert(GED_HASHTABLE_HANDLE hHashTable,
void *pvoid, unsigned long *pulID)
{
struct GED_HASHTABLE *psHT = __ged_hashtable_verify(hHashTable);
struct GED_HASHNODE *psHN = NULL;
unsigned long ulHash, ulID;
GED_BOOL bFindSlot = GED_FALSE;
if ((!psHT) || (!pulID))
return GED_ERROR_INVALID_PARAMS;
ulID = psHT->ulCurrentID + 1;
while (1) {
ulHash = ged_hash(psHT, ulID);
psHN = __ged_hashtable_find(&psHT->psHashTable[ulHash], ulID);
if (psHN != NULL) {
ulID++;
if (ulID == 0) /*skip the value 0 */
ulID = 1;
if (ulID == psHT->ulCurrentID) {
bFindSlot = GED_FALSE;
break;
}
} else {
bFindSlot = GED_TRUE;
break;
}
};
if (bFindSlot == GED_FALSE)
return GED_ERROR_FAIL;
psHN =
(struct GED_HASHNODE *)ged_alloc_atomic(sizeof(struct GED_HASHNODE));
if (psHN) {
psHN->pvoid = pvoid;
psHN->ulID = ulID;
*pulID = ulID;
INIT_LIST_HEAD(&psHN->sList);
psHT->ulCurrentID = ulID;
psHT->ulCount += 1;
list_add(&psHN->sList, &psHT->psHashTable[ulHash]);
return GED_OK;
}
return GED_ERROR_OOM;
}
void ged_hashtable_remove(GED_HASHTABLE_HANDLE hHashTable, unsigned long ulID)
{
struct GED_HASHTABLE *psHT = __ged_hashtable_verify(hHashTable);
if (psHT) {
unsigned long ulHash = ged_hash(psHT, ulID);
struct GED_HASHNODE *psHN =
__ged_hashtable_find(&psHT->psHashTable[ulHash], ulID);
if (psHN) {
psHN->pvoid = NULL;
list_del(&psHN->sList);
ged_free(psHN, sizeof(struct GED_HASHNODE));
psHT->ulCount -= 1;
}
}
}
void *ged_hashtable_find(GED_HASHTABLE_HANDLE hHashTable, unsigned long ulID)
{
struct GED_HASHTABLE *psHT = __ged_hashtable_verify(hHashTable);
if (psHT) {
unsigned long ulHash = ged_hash(psHT, ulID);
struct GED_HASHNODE *psHN =
__ged_hashtable_find(&psHT->psHashTable[ulHash], ulID);
if (psHN)
return psHN->pvoid;
#ifdef GED_DEBUG
if (ulID != 0)
GED_LOGD(
"@%s: ulID=%lu ulHash=%lu fail\n", __func__,
ulID, ulHash);
#endif
}
return NULL;
}
GED_ERROR ged_hashtable_set(GED_HASHTABLE_HANDLE hHashTable,
unsigned long ulID, void *pvoid)
{
struct GED_HASHTABLE *psHT = __ged_hashtable_verify(hHashTable);
if (psHT) {
unsigned long ulHash = ged_hash(psHT, ulID);
struct GED_HASHNODE *psHN =
__ged_hashtable_find(&psHT->psHashTable[ulHash], ulID);
if (psHN) {
psHN->pvoid = pvoid;
} else {
psHN =
(struct GED_HASHNODE *)
ged_alloc_atomic(sizeof(struct GED_HASHNODE));
if (psHN == NULL)
return GED_ERROR_OOM;
psHN->pvoid = pvoid;
psHN->ulID = ulID;
INIT_LIST_HEAD(&psHN->sList);
list_add(&psHN->sList, &psHT->psHashTable[ulHash]);
psHT->ulCount += 1;
}
return GED_OK;
}
return GED_ERROR_INVALID_PARAMS;
}
void ged_hashtable_iterator(GED_HASHTABLE_HANDLE hHashTable,
GED_BOOL (*iterator)(unsigned long ulID, void *pvoid, void *pvParam),
void *pvParam)
{
struct GED_HASHTABLE *psHT = __ged_hashtable_verify(hHashTable);
if (psHT) {
struct GED_HASHNODE *psHN;
unsigned long i;
struct list_head *psListEntry, *psListEntryTemp;
for (i = 0; i < psHT->ulLength; ++i) {
struct list_head *psList = &psHT->psHashTable[i];
list_for_each_safe(psListEntry,
psListEntryTemp, psList) {
psHN = list_entry(psListEntry,
struct GED_HASHNODE, sList);
if (psHN) {
if (!iterator(psHN->ulID,
psHN->pvoid, pvParam))
return;
}
}
}
}
}
void *ged_hashtable_search(GED_HASHTABLE_HANDLE hHashTable,
void* (*pFunc)(unsigned long ulID, void *pvoid, void *pvParam),
void *pvParam)
{
struct GED_HASHTABLE *psHT = __ged_hashtable_verify(hHashTable);
void *pResult = NULL;
if (psHT) {
struct GED_HASHNODE *psHN;
unsigned long i;
struct list_head *psListEntry, *psListEntryTemp;
for (i = 0; i < psHT->ulLength; ++i) {
struct list_head *psList = &psHT->psHashTable[i];
list_for_each_safe(psListEntry, psListEntryTemp,
psList) {
psHN = list_entry(psListEntry,
struct GED_HASHNODE,
sList);
if (psHN) {
pResult = pFunc(psHN->ulID,
psHN->pvoid, pvParam);
if (pResult)
return pResult;
}
}
}
}
return pResult;
}
void ged_hashtable_iterator_delete(GED_HASHTABLE_HANDLE hHashTable,
GED_BOOL (*pFunc)(unsigned long ulID, void *pvoid, void *pvParam,
GED_BOOL *pbDeleted),
void *pvParam)
{
struct GED_HASHTABLE *psHT = __ged_hashtable_verify(hHashTable);
if (psHT) {
struct GED_HASHNODE *psHN;
unsigned long i;
struct list_head *psListEntry, *psListEntryTemp;
for (i = 0; i < psHT->ulLength; ++i) {
struct list_head *psList = &psHT->psHashTable[i];
list_for_each_safe(psListEntry,
psListEntryTemp, psList) {
psHN = list_entry(psListEntry,
struct GED_HASHNODE, sList);
if (psHN) {
GED_BOOL bDeleted = GED_FALSE;
GED_BOOL bContinue = pFunc(psHN->ulID,
psHN->pvoid, pvParam, &bDeleted);
if (bDeleted) {
psHN->pvoid = NULL;
list_del(&psHN->sList);
ged_free(psHN,
sizeof(struct GED_HASHNODE));
psHT->ulCount -= 1;
}
if (!bContinue)
return;
}
}
}
}
}
unsigned long ged_hashtable_get_count(GED_HASHTABLE_HANDLE hHashTable)
{
struct GED_HASHTABLE *psHT = __ged_hashtable_verify(hHashTable);
if (psHT)
return psHT->ulCount;
return 0;
}
File diff suppressed because it is too large. Load diff
File diff suppressed because it is too large. Load diff
+720
View File
@@ -0,0 +1,720 @@
// SPDX-License-Identifier: GPL-2.0
/*
* Copyright (c) 2019 MediaTek Inc.
*/
#include <linux/cdev.h>
#include <linux/delay.h>
#include <linux/device.h>
#include <linux/fs.h>
#include <linux/hardirq.h>
#include <linux/init.h>
#include <linux/kallsyms.h>
#include <linux/miscdevice.h>
#include <linux/module.h>
#include <linux/poll.h>
#include <linux/proc_fs.h>
#include <linux/wait.h>
#include <linux/sched.h>
#include <linux/vmalloc.h>
#include <linux/slab.h>
#include <linux/spinlock.h>
#include <linux/semaphore.h>
#include <linux/workqueue.h>
#include <linux/kthread.h>
#include <linux/platform_device.h>
#include <linux/of.h>
#include <linux/of_address.h>
#include <mt-plat/aee.h>
#ifdef GED_DEBUG_FS
#include "ged_debugFS.h"
#endif
#include "ged_sysfs.h"
#include "ged_log.h"
#include "ged_hal.h"
#include "ged_bridge.h"
#include "ged_monitor_3D_fence.h"
#include "ged_notify_sw_vsync.h"
#include "ged_dvfs.h"
#include "ged_kpi.h"
#include "ged_ge.h"
#include "ged_gpu_tuner.h"
/**
* ===============================================
* SECTION : Local functions declaration
* ===============================================
*/
static int ged_open(struct inode *inode, struct file *filp);
static int ged_release(struct inode *inode, struct file *filp);
static unsigned int ged_poll(struct file *file,
struct poll_table_struct *ptable);
static ssize_t ged_read(struct file *filp,
char __user *buf, size_t count, loff_t *f_pos);
static ssize_t ged_write(struct file *filp,
const char __user *buf, size_t count, loff_t *f_pos);
static long ged_dispatch(struct file *pFile,
struct GED_BRIDGE_PACKAGE *psBridgePackageKM);
static int ged_validate_cmd(unsigned int ioctlCmd);
static int ged_validate_cmd_32(unsigned int ioctlCmd);
static long ged_ioctl(struct file *pFile,
unsigned int ioctlCmd, unsigned long arg);
#ifdef CONFIG_COMPAT
static long ged_ioctl_compat(struct file *pFile,
unsigned int ioctlCmd, unsigned long arg);
#endif
static int ged_pdrv_probe(struct platform_device *pdev);
static void ged_exit(void);
static int ged_init(void);
/**
* ===============================================
* SECTION : Local variables definition
* ===============================================
*/
#define GED_DRIVER_DEVICE_NAME "ged"
static GED_LOG_BUF_HANDLE ghLogBuf_GPU;
#ifdef GED_DEBUG
#define GED_LOG_BUF_COMMON_GLES "GLES"
static GED_LOG_BUF_HANDLE ghLogBuf_GLES;
GED_LOG_BUF_HANDLE ghLogBuf_GED;
#endif /* GED_DEBUG */
#define GED_LOG_BUF_COMMON_HWC_ERR "HWC_err"
static GED_LOG_BUF_HANDLE ghLogBuf_HWC_ERR;
#define GED_LOG_BUF_COMMON_HWC "HWC"
static GED_LOG_BUF_HANDLE ghLogBuf_HWC;
#define GED_LOG_BUF_COMMON_FENCE "FENCE"
static GED_LOG_BUF_HANDLE ghLogBuf_FENCE;
static GED_LOG_BUF_HANDLE ghLogBuf_ftrace;
#ifdef GED_DVFS_DEBUG_BUF
GED_LOG_BUF_HANDLE ghLogBuf_DVFS;
#endif /* GED_DVFS_DEBUG_BUF */
GED_LOG_BUF_HANDLE gpufreq_ged_log;
static const struct of_device_id g_ged_of_match[] = {
{ .compatible = "mediatek,ged" },
{ /* sentinel */ }
};
static struct platform_driver g_ged_pdrv = {
.probe = ged_pdrv_probe,
.remove = NULL,
.driver = {
.name = "ged",
.owner = THIS_MODULE,
.of_match_table = g_ged_of_match,
},
};
static const struct file_operations ged_fops = {
.owner = THIS_MODULE,
.open = ged_open,
.release = ged_release,
.poll = ged_poll,
.read = ged_read,
.write = ged_write,
.unlocked_ioctl = ged_ioctl,
#ifdef CONFIG_COMPAT
.compat_ioctl = ged_ioctl_compat,
#endif
};
/******************************************************************************
* GED File operations
*****************************************************************************/
static int ged_open(struct inode *inode, struct file *filp)
{
filp->private_data = NULL;
GED_LOGD("@%s: %d:%d\n", __func__, MAJOR(inode->i_rdev),
MINOR(inode->i_rdev));
return 0;
}
static int ged_release(struct inode *inode, struct file *filp)
{
if (filp->private_data) {
void (*free_func)(void *f) =
((struct GED_FILE_PRIVATE_BASE *)
filp->private_data)->free_func;
free_func(filp->private_data);
}
GED_LOGD("@%s: %d:%d\n", __func__, MAJOR(inode->i_rdev),
MINOR(inode->i_rdev));
return 0;
}
static unsigned int
ged_poll(struct file *file, struct poll_table_struct *ptable)
{
return 0;
}
static ssize_t
ged_read(struct file *filp, char __user *buf, size_t count, loff_t *f_pos)
{
return 0;
}
static ssize_t ged_write(struct file *filp,
const char __user *buf, size_t count, loff_t *f_pos)
{
return 0;
}
static long ged_dispatch(struct file *pFile,
struct GED_BRIDGE_PACKAGE *psBridgePackageKM)
{
int ret = -EFAULT;
void *pvIn = NULL, *pvOut = NULL;
/* We make sure the both size are GE 0 integer.
*/
if (psBridgePackageKM->i32InBufferSize > 0 &&
psBridgePackageKM->i32OutBufferSize > 0) {
int32_t inputBufferSize =
psBridgePackageKM->i32InBufferSize;
if (GED_BRIDGE_COMMAND_GE_ALLOC ==
GED_GET_BRIDGE_ID(
psBridgePackageKM->ui32FunctionID)) {
inputBufferSize = sizeof(int) +
sizeof(uint32_t) * GE_ALLOC_STRUCT_NUM;
// hardcode region_num = GE_ALLOC_STRUCT_NUM,
// need check input buffer size
if (psBridgePackageKM->i32InBufferSize <
inputBufferSize) {
GED_LOGE("Failed to region_num, it must be %d\n",
GE_ALLOC_STRUCT_NUM);
goto dispatch_exit;
}
}
if (inputBufferSize <= KMALLOC_MAX_SIZE)
pvIn = kmalloc(inputBufferSize, GFP_KERNEL);
if (pvIn == NULL)
goto dispatch_exit;
if (ged_copy_from_user(pvIn,
psBridgePackageKM->pvParamIn,
inputBufferSize) != 0) {
GED_LOGE("Failed to ged_copy_from_user\n");
goto dispatch_exit;
}
pvOut = kzalloc(psBridgePackageKM->i32OutBufferSize,
GFP_KERNEL);
if (pvOut == NULL)
goto dispatch_exit;
/* Make sure that the UM will never break the KM.
* Check IO size are both matched the size of IO sturct.
*/
#define VALIDATE_ARG(struct_name) do { \
if (sizeof(struct GED_BRIDGE_IN_##struct_name)\
> psBridgePackageKM->i32InBufferSize ||\
sizeof(struct GED_BRIDGE_OUT_##struct_name)\
> psBridgePackageKM->i32OutBufferSize) {\
GED_LOGE("GED_BRIDGE_COMMAND_##cmd failed io_size:",\
"%d/%d, expected: %zu/%zu",\
psBridgePackageKM->i32InBufferSize,\
psBridgePackageKM->i32OutBufferSize,\
sizeof(struct GED_BRIDGE_IN_##struct_name),\
sizeof(struct GED_BRIDGE_OUT_##struct_name));\
goto dispatch_exit;\
} } while (0)
/* we will change the below switch into
* a function pointer mapping table in the future
*/
switch (GED_GET_BRIDGE_ID(psBridgePackageKM->ui32FunctionID)) {
case GED_BRIDGE_COMMAND_LOG_BUF_GET:
VALIDATE_ARG(LOGBUFGET);
ret = ged_bridge_log_buf_get(pvIn, pvOut);
break;
case GED_BRIDGE_COMMAND_LOG_BUF_WRITE:
VALIDATE_ARG(LOGBUFWRITE);
ret = ged_bridge_log_buf_write(pvIn, pvOut);
break;
case GED_BRIDGE_COMMAND_LOG_BUF_RESET:
VALIDATE_ARG(LOGBUFRESET);
ret = ged_bridge_log_buf_reset(pvIn, pvOut);
break;
case GED_BRIDGE_COMMAND_BOOST_GPU_FREQ:
VALIDATE_ARG(BOOSTGPUFREQ);
ret = ged_bridge_boost_gpu_freq(pvIn, pvOut);
break;
case GED_BRIDGE_COMMAND_MONITOR_3D_FENCE:
VALIDATE_ARG(MONITOR3DFENCE);
ret = ged_bridge_monitor_3D_fence(pvIn, pvOut);
break;
case GED_BRIDGE_COMMAND_QUERY_INFO:
VALIDATE_ARG(QUERY_INFO);
ret = ged_bridge_query_info(pvIn, pvOut);
break;
case GED_BRIDGE_COMMAND_NOTIFY_VSYNC:
VALIDATE_ARG(NOTIFY_VSYNC);
ret = ged_bridge_notify_vsync(pvIn, pvOut);
break;
case GED_BRIDGE_COMMAND_DVFS_PROBE:
VALIDATE_ARG(DVFS_PROBE);
ret = ged_bridge_dvfs_probe(pvIn, pvOut);
break;
case GED_BRIDGE_COMMAND_DVFS_UM_RETURN:
VALIDATE_ARG(DVFS_UM_RETURN);
ret = ged_bridge_dvfs_um_retrun(pvIn, pvOut);
break;
case GED_BRIDGE_COMMAND_EVENT_NOTIFY:
VALIDATE_ARG(EVENT_NOTIFY);
ret = ged_bridge_event_notify(pvIn, pvOut);
break;
case GED_BRIDGE_COMMAND_GPU_HINT_TO_CPU:
VALIDATE_ARG(GPU_HINT_TO_CPU);
ret = ged_bridge_gpu_hint_to_cpu(pvIn, pvOut);
break;
case GED_BRIDGE_COMMAND_HINT_FORCE_MDP:
VALIDATE_ARG(HINT_FORCE_MDP);
ret = ged_bridge_hint_force_mdp(pvIn, pvOut);
break;
case GED_BRIDGE_COMMAND_QUERY_DVFS_FREQ_PRED:
VALIDATE_ARG(QUERY_DVFS_FREQ_PRED);
ret = ged_bridge_query_dvfs_freq_pred(pvIn, pvOut);
break;
case GED_BRIDGE_COMMAND_QUERY_GPU_DVFS_INFO:
VALIDATE_ARG(QUERY_GPU_DVFS_INFO);
ret = ged_bridge_query_gpu_dvfs_info(pvIn, pvOut);
break;
case GED_BRIDGE_COMMAND_GE_ALLOC:
VALIDATE_ARG(GE_ALLOC);
ret = ged_bridge_ge_alloc(pvIn, pvOut);
break;
case GED_BRIDGE_COMMAND_GE_GET:
VALIDATE_ARG(GE_GET);
ret = ged_bridge_ge_get(pvIn, pvOut, psBridgePackageKM->i32OutBufferSize);
break;
case GED_BRIDGE_COMMAND_GE_SET:
VALIDATE_ARG(GE_SET);
ret = ged_bridge_ge_set(pvIn, pvOut, psBridgePackageKM->i32InBufferSize);
break;
case GED_BRIDGE_COMMAND_GE_INFO:
VALIDATE_ARG(GE_INFO);
ret = ged_bridge_ge_info(pvIn, pvOut);
break;
case GED_BRIDGE_COMMAND_GPU_TIMESTAMP:
VALIDATE_ARG(GPU_TIMESTAMP);
ret = ged_bridge_gpu_timestamp(pvIn, pvOut);
break;
case GED_BRIDGE_COMMAND_GPU_TUNER_STATUS:
VALIDATE_ARG(GPU_TUNER_STATUS);
ret = ged_bridge_gpu_tuner_status(pvIn, pvOut);
break;
default:
GED_LOGE("Unknown Bridge ID: %u\n",
GED_GET_BRIDGE_ID(psBridgePackageKM->ui32FunctionID));
break;
}
if (ged_copy_to_user(psBridgePackageKM->pvParamOut,
pvOut, psBridgePackageKM->i32OutBufferSize) != 0) {
goto dispatch_exit;
}
}
dispatch_exit:
kfree(pvIn);
kfree(pvOut);
return ret;
}
static int ged_validate_cmd(unsigned int ioctlCmd)
{
unsigned int valid_cmd[] = {
GED_IOWR(GED_BRIDGE_COMMAND_LOG_BUF_GET),
GED_IOWR(GED_BRIDGE_COMMAND_LOG_BUF_WRITE),
GED_IOWR(GED_BRIDGE_COMMAND_LOG_BUF_RESET),
GED_IOWR(GED_BRIDGE_COMMAND_BOOST_GPU_FREQ),
GED_IOWR(GED_BRIDGE_COMMAND_MONITOR_3D_FENCE),
GED_IOWR(GED_BRIDGE_COMMAND_QUERY_INFO),
GED_IOWR(GED_BRIDGE_COMMAND_NOTIFY_VSYNC),
GED_IOWR(GED_BRIDGE_COMMAND_DVFS_PROBE),
GED_IOWR(GED_BRIDGE_COMMAND_DVFS_UM_RETURN),
GED_IOWR(GED_BRIDGE_COMMAND_EVENT_NOTIFY),
GED_IOWR(GED_BRIDGE_COMMAND_GPU_HINT_TO_CPU),
GED_IOWR(GED_BRIDGE_COMMAND_HINT_FORCE_MDP),
GED_IOWR(GED_BRIDGE_COMMAND_QUERY_DVFS_FREQ_PRED),
GED_IOWR(GED_BRIDGE_COMMAND_QUERY_GPU_DVFS_INFO),
GED_IOWR(GED_BRIDGE_COMMAND_GE_ALLOC),
GED_IOWR(GED_BRIDGE_COMMAND_GE_GET),
GED_IOWR(GED_BRIDGE_COMMAND_GE_SET),
GED_IOWR(GED_BRIDGE_COMMAND_GE_INFO),
GED_IOWR(GED_BRIDGE_COMMAND_GPU_TIMESTAMP),
GED_IOWR(GED_BRIDGE_COMMAND_GPU_TUNER_STATUS),
};
unsigned int i;
bool is_valid = false;
for (i = 0; i < ARRAY_SIZE(valid_cmd); i++) {
if (ioctlCmd == valid_cmd[i]) {
is_valid = true;
break;
}
}
return is_valid ? 0 : -EINVAL;
}
static int ged_validate_cmd_32(unsigned int ioctlCmd)
{
unsigned int valid_cmd[] = {
GED_IOWR_32(GED_BRIDGE_COMMAND_LOG_BUF_GET),
GED_IOWR_32(GED_BRIDGE_COMMAND_LOG_BUF_WRITE),
GED_IOWR_32(GED_BRIDGE_COMMAND_LOG_BUF_RESET),
GED_IOWR_32(GED_BRIDGE_COMMAND_BOOST_GPU_FREQ),
GED_IOWR_32(GED_BRIDGE_COMMAND_MONITOR_3D_FENCE),
GED_IOWR_32(GED_BRIDGE_COMMAND_QUERY_INFO),
GED_IOWR_32(GED_BRIDGE_COMMAND_NOTIFY_VSYNC),
GED_IOWR_32(GED_BRIDGE_COMMAND_DVFS_PROBE),
GED_IOWR_32(GED_BRIDGE_COMMAND_DVFS_UM_RETURN),
GED_IOWR_32(GED_BRIDGE_COMMAND_EVENT_NOTIFY),
GED_IOWR_32(GED_BRIDGE_COMMAND_GPU_HINT_TO_CPU),
GED_IOWR_32(GED_BRIDGE_COMMAND_HINT_FORCE_MDP),
GED_IOWR_32(GED_BRIDGE_COMMAND_QUERY_DVFS_FREQ_PRED),
GED_IOWR_32(GED_BRIDGE_COMMAND_QUERY_GPU_DVFS_INFO),
GED_IOWR_32(GED_BRIDGE_COMMAND_GE_ALLOC),
GED_IOWR_32(GED_BRIDGE_COMMAND_GE_GET),
GED_IOWR_32(GED_BRIDGE_COMMAND_GE_SET),
GED_IOWR_32(GED_BRIDGE_COMMAND_GE_INFO),
GED_IOWR_32(GED_BRIDGE_COMMAND_GPU_TIMESTAMP),
GED_IOWR_32(GED_BRIDGE_COMMAND_GPU_TUNER_STATUS),
};
unsigned int i;
bool is_valid = false;
for (i = 0; i < ARRAY_SIZE(valid_cmd); i++) {
if (ioctlCmd == valid_cmd[i]) {
is_valid = true;
break;
}
}
return is_valid ? 0 : -EINVAL;
}
static long
ged_ioctl(struct file *pFile, unsigned int ioctlCmd, unsigned long arg)
{
int ret = -EFAULT;
struct GED_BRIDGE_PACKAGE *psBridgePackageKM;
struct GED_BRIDGE_PACKAGE *psBridgePackageUM =
(struct GED_BRIDGE_PACKAGE *)arg;
struct GED_BRIDGE_PACKAGE sBridgePackageKM;
psBridgePackageKM = &sBridgePackageKM;
ret = ged_validate_cmd(ioctlCmd);
if (ret) {
GED_LOGE("Unknown ioctlCmd: %u", ioctlCmd);
goto unlock_and_return;
}
if (ged_copy_from_user(psBridgePackageKM, psBridgePackageUM,
sizeof(struct GED_BRIDGE_PACKAGE)) != 0) {
GED_LOGE("Failed to ged_copy_from_user\n");
ret = -EFAULT;
goto unlock_and_return;
}
if (ioctlCmd != psBridgePackageKM->ui32FunctionID) {
GED_LOGE("ioctlCmd (%u) != ui32FunctionID (%u)",
ioctlCmd, psBridgePackageKM->ui32FunctionID);
ret = -EINVAL;
goto unlock_and_return;
}
ret = ged_dispatch(pFile, psBridgePackageKM);
unlock_and_return:
return ret;
}
#ifdef CONFIG_COMPAT
static long
ged_ioctl_compat(struct file *pFile, unsigned int ioctlCmd, unsigned long arg)
{
int ret = -EFAULT;
struct GED_BRIDGE_PACKAGE sBridgePackageKM64;
struct GED_BRIDGE_PACKAGE_32 sBridgePackageKM32;
struct GED_BRIDGE_PACKAGE_32 *psBridgePackageKM32 =
&sBridgePackageKM32;
struct GED_BRIDGE_PACKAGE_32 *psBridgePackageUM32 =
(struct GED_BRIDGE_PACKAGE_32 *)arg;
ret = ged_validate_cmd_32(ioctlCmd);
if (ret) {
GED_LOGE("Unknown ioctlCmd: %u", ioctlCmd);
goto unlock_and_return;
}
if (ged_copy_from_user(psBridgePackageKM32,
psBridgePackageUM32,
sizeof(struct GED_BRIDGE_PACKAGE_32)) != 0) {
GED_LOGE("Failed to ged_copy_from_user\n");
ret = -EFAULT;
goto unlock_and_return;
}
if (ioctlCmd != psBridgePackageKM32->ui32FunctionID) {
GED_LOGE("ioctlCmd (%u) != ui32FunctionID (%u)",
ioctlCmd, psBridgePackageKM32->ui32FunctionID);
ret = -EINVAL;
goto unlock_and_return;
}
sBridgePackageKM64.ui32FunctionID = psBridgePackageKM32->ui32FunctionID;
sBridgePackageKM64.i32Size = sizeof(struct GED_BRIDGE_PACKAGE);
sBridgePackageKM64.pvParamIn =
(void *) ((size_t) psBridgePackageKM32->ui32ParamIn);
sBridgePackageKM64.pvParamOut =
(void *) ((size_t) psBridgePackageKM32->ui32ParamOut);
sBridgePackageKM64.i32InBufferSize =
psBridgePackageKM32->i32InBufferSize;
sBridgePackageKM64.i32OutBufferSize =
psBridgePackageKM32->i32OutBufferSize;
ret = ged_dispatch(pFile, &sBridgePackageKM64);
unlock_and_return:
return ret;
}
#endif
/******************************************************************************
* Module related
*****************************************************************************/
/*
* ged driver probe
*/
static int ged_pdrv_probe(struct platform_device *pdev)
{
int err;
GED_LOGI("@%s: start to probe ged driver\n", __func__);
if (proc_create(GED_DRIVER_DEVICE_NAME, 0644, NULL, &ged_fops)
== NULL) {
err = GED_ERROR_FAIL;
GED_LOGE("Failed to register ged proc entry!\n");
goto ERROR;
}
err = ged_sysfs_init();
if (unlikely(err != GED_OK)) {
GED_LOGE("Failed to init sys FS!\n");
goto ERROR;
}
#ifdef GED_DEBUG_FS
err = ged_debugFS_init();
if (unlikely(err != GED_OK)) {
GED_LOGE("Failed to init debug FS!\n");
goto ERROR;
}
#endif
err = ged_log_system_init();
if (unlikely(err != GED_OK)) {
GED_LOGE("Failed to create gedlog entry!\n");
goto ERROR;
}
err = ged_hal_init();
if (unlikely(err != GED_OK)) {
GED_LOGE("Failed to create hal entry!\n");
goto ERROR;
}
err = ged_notify_sw_vsync_system_init();
if (unlikely(err != GED_OK)) {
GED_LOGE("Failed to init notify sw vsync!\n");
goto ERROR;
}
err = ged_dvfs_system_init();
if (unlikely(err != GED_OK)) {
GED_LOGE("Failed to init common dvfs!\n");
goto ERROR;
}
err = ged_ge_init();
if (unlikely(err != GED_OK)) {
GED_LOGE("Failed to init gralloc_extra!\n");
goto ERROR;
}
err = ged_kpi_system_init();
if (unlikely(err != GED_OK)) {
GED_LOGE("Failed to init KPI!\n");
goto ERROR;
}
err = ged_gpu_tuner_init();
if (unlikely(err != GED_OK)) {
GED_LOGE("Failed to init GPU Tuner!\n");
goto ERROR;
}
#ifndef GED_BUFFER_LOG_DISABLE
ghLogBuf_GPU = ged_log_buf_alloc(512, 128 * 512,
GED_LOG_BUF_TYPE_RINGBUFFER, "GPU_FENCE", NULL);
#ifdef GED_DEBUG
ghLogBuf_GLES = ged_log_buf_alloc(160, 128 * 160,
GED_LOG_BUF_TYPE_RINGBUFFER, GED_LOG_BUF_COMMON_GLES, NULL);
ghLogBuf_GED = ged_log_buf_alloc(32, 64 * 32,
GED_LOG_BUF_TYPE_RINGBUFFER, "GED internal", NULL);
#endif
ghLogBuf_HWC_ERR = ged_log_buf_alloc(2048, 2048 * 128,
GED_LOG_BUF_TYPE_RINGBUFFER, GED_LOG_BUF_COMMON_HWC_ERR, NULL);
ghLogBuf_HWC = ged_log_buf_alloc(4096, 128 * 4096,
GED_LOG_BUF_TYPE_RINGBUFFER, GED_LOG_BUF_COMMON_HWC, NULL);
ghLogBuf_FENCE = ged_log_buf_alloc(256, 128 * 256,
GED_LOG_BUF_TYPE_RINGBUFFER, GED_LOG_BUF_COMMON_FENCE, NULL);
ghLogBuf_ftrace = ged_log_buf_alloc(1024*32, 1024*1024,
GED_LOG_BUF_TYPE_RINGBUFFER,
"fence_trace", "fence_trace");
#ifdef GED_DVFS_DEBUG_BUF
ghLogBuf_DVFS = ged_log_buf_alloc(20*60, 20*60*100
, GED_LOG_BUF_TYPE_RINGBUFFER
, "DVFS_Log", "ged_dvfs_debug");
#endif
gpufreq_ged_log = ged_log_buf_alloc(1024, 64 * 1024,
GED_LOG_BUF_TYPE_RINGBUFFER, "gfreq", "gfreq");
#else
ghLogBuf_GPU = 0;
#ifdef GED_DEBUG
ghLogBuf_GLES = 0;
ghLogBuf_GED = 0;
#endif
ghLogBuf_HWC_ERR = 0;
ghLogBuf_HWC = 0;
ghLogBuf_FENCE = 0;
ghLogBuf_ftrace = 0;
#ifdef GED_DVFS_DEBUG_BUF
ghLogBuf_DVFS = 0;
#endif
gpufreq_ged_log = 0;
#endif /* GED_BUFFER_LOG_DISABLE */
#ifdef CONFIG_MTK_GPU_OPP_STATS_SUPPORT
err = ged_dvfs_init_opp_cost();
if (err) {
GED_LOGE("@%s: failed to probe ged driver (%d)\n", __func__, err);
}
#endif /* CONFIG_MTK_GPU_OPP_STATS_SUPPORT */
GED_LOGI("@%s: ged driver probe done\n", __func__);
ERROR:
return err;
}
/*
* unregister the gpufreq driver, remove fs node
*/
static void ged_exit(void)
{
#ifndef GED_BUFFER_LOG_DISABLE
ged_log_buf_free(gpufreq_ged_log);
gpufreq_ged_log = 0;
#ifdef GED_DVFS_DEBUG_BUF
ged_log_buf_free(ghLogBuf_DVFS);
ghLogBuf_DVFS = 0;
#endif
ged_log_buf_free(ghLogBuf_ftrace);
ghLogBuf_ftrace = 0;
ged_log_buf_free(ghLogBuf_FENCE);
ghLogBuf_FENCE = 0;
ged_log_buf_free(ghLogBuf_HWC);
ghLogBuf_HWC = 0;
ged_log_buf_free(ghLogBuf_HWC_ERR);
ghLogBuf_HWC_ERR = 0;
#ifdef GED_DEBUG
ged_log_buf_free(ghLogBuf_GED);
ghLogBuf_GED = 0;
ged_log_buf_free(ghLogBuf_GLES);
ghLogBuf_GLES = 0;
#endif
ged_log_buf_free(ghLogBuf_GPU);
ghLogBuf_GPU = 0;
#endif /* GED_BUFFER_LOG_DISABLE */
ged_gpu_tuner_exit();
ged_kpi_system_exit();
ged_ge_exit();
ged_dvfs_system_exit();
ged_notify_sw_vsync_system_exit();
ged_hal_exit();
ged_log_system_exit();
#ifdef GED_DEBUG_FS
ged_debugFS_exit();
#endif
ged_sysfs_exit();
remove_proc_entry(GED_DRIVER_DEVICE_NAME, NULL);
platform_driver_unregister(&g_ged_pdrv);
}
/*
* register the ged driver, create fs node
*/
static int ged_init(void)
{
GED_ERROR err = GED_ERROR_FAIL;
GED_LOGI("@%s: start to initialize ged driver\n", __func__);
/* register platform driver */
err = platform_driver_register(&g_ged_pdrv);
if (err) {
GED_LOGE("@%s: failed to register ged driver\n", __func__);
goto ERROR;
}
GED_LOGI("@%s: ged driver init done\n", __func__);
ERROR:
return err;
}
#ifdef GED_MODULE_LATE_INIT
late_initcall(ged_init);
#else
module_init(ged_init);
#endif
module_exit(ged_exit);
MODULE_DEVICE_TABLE(of, g_ged_of_match);
MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("MediaTek GED Driver");
MODULE_AUTHOR("MediaTek Inc.");
@@ -0,0 +1,216 @@
// SPDX-License-Identifier: GPL-2.0
/*
* Copyright (c) 2019 MediaTek Inc.
*/
#include <linux/version.h>
#include <linux/workqueue.h>
#include <linux/sched.h>
#include <linux/atomic.h>
#include <linux/module.h>
#include <linux/sync_file.h>
#include <linux/dma-fence.h>
#include <mt-plat/mtk_gpu_utility.h>
/* This has been removed on kernel-4.19
* #include <trace/events/gpu.h>
*/
#ifdef MTK_GPU_DVFS
#include <mtk_gpufreq.h>
#else
#include "ged_gpufreq.h"
#endif
#include "ged_monitor_3D_fence.h"
#include "ged_log.h"
#include "ged_base.h"
#include "ged_type.h"
#include "ged_dvfs.h"
#include "ged_global.h"
#include <asm/div64.h>
static atomic_t g_i32Count = ATOMIC_INIT(0);
static unsigned int ged_monitor_3D_fence_debug;
static unsigned int ged_monitor_3D_fence_disable;
static unsigned int ged_monitor_3D_fence_switch;
static unsigned int ged_smart_boost;
static unsigned int ged_monitor_3D_fence_systrace;
static unsigned long g_ul3DFenceDoneTime;
struct GED_MONITOR_3D_FENCE {
struct dma_fence_cb sSyncWaiter;
struct work_struct sWork;
struct dma_fence *psSyncFence;
};
static void ged_sync_cb(struct dma_fence *sFence, struct dma_fence_cb *waiter)
{
struct GED_MONITOR_3D_FENCE *psMonitor;
unsigned long long t;
t = ged_get_time();
do_div(t, 1000);
ged_monitor_3D_fence_notify();
psMonitor = GED_CONTAINER_OF(waiter,
struct GED_MONITOR_3D_FENCE, sSyncWaiter);
ged_log_buf_print(ghLogBuf_DVFS,
"[-] ged_monitor_3D_fence_done (ts=%llu) %p",
t, psMonitor->psSyncFence);
schedule_work(&psMonitor->sWork);
}
static void ged_monitor_3D_fence_work_cb(struct work_struct *psWork)
{
struct GED_MONITOR_3D_FENCE *psMonitor;
#ifdef GED_DEBUG_MONITOR_3D_FENCE
ged_log_buf_print(ghLogBuf_GED, "ged_monitor_3D_fence_work_cb");
#endif
if (atomic_sub_return(1, &g_i32Count) < 1) {
unsigned int uiFreqLevelID;
if (mtk_get_bottom_gpu_freq(&uiFreqLevelID)) {
if (uiFreqLevelID > 0 && ged_monitor_3D_fence_switch) {
#ifdef GED_DEBUG_MONITOR_3D_FENCE
ged_log_buf_print(ghLogBuf_GED,
"mtk_set_bottom_gpu_freq(0)");
#endif
mtk_set_bottom_gpu_freq(0);
}
}
}
if (ged_monitor_3D_fence_debug > 0)
GED_LOGD("[-]3D fences count = %d\n", atomic_read(&g_i32Count));
psMonitor = GED_CONTAINER_OF(psWork,
struct GED_MONITOR_3D_FENCE, sWork);
dma_fence_put(psMonitor->psSyncFence);
ged_free(psMonitor, sizeof(struct GED_MONITOR_3D_FENCE));
}
unsigned long ged_monitor_3D_fence_done_time(void)
{
return g_ul3DFenceDoneTime;
}
GED_ERROR ged_monitor_3D_fence_add(int fence_fd)
{
int err;
unsigned long long t;
struct GED_MONITOR_3D_FENCE *psMonitor;
struct dma_fence *psDebugAddress;
if (ged_monitor_3D_fence_disable)
return GED_OK;
t = ged_get_time();
do_div(t, 1000);
psMonitor =
(struct GED_MONITOR_3D_FENCE *)
ged_alloc(sizeof(struct GED_MONITOR_3D_FENCE));
#ifdef GED_DEBUG_MONITOR_3D_FENCE
ged_log_buf_print(ghLogBuf_GED, "[+]$s", __func__);
#endif
if (!psMonitor)
return GED_ERROR_OOM;
psMonitor->psSyncFence = sync_file_get_fence(fence_fd);
if (psMonitor->psSyncFence == NULL) {
ged_free(psMonitor, sizeof(struct GED_MONITOR_3D_FENCE));
return GED_ERROR_INVALID_PARAMS;
}
INIT_WORK(&psMonitor->sWork, ged_monitor_3D_fence_work_cb);
psDebugAddress = psMonitor->psSyncFence;
err = dma_fence_add_callback(psMonitor->psSyncFence,
&psMonitor->sSyncWaiter, ged_sync_cb);
ged_log_buf_print(ghLogBuf_DVFS,
"[+] %s (ts=%llu) %p", __func__, t, psDebugAddress);
#ifdef GED_DEBUG_MONITOR_3D_FENCE
ged_log_buf_print(ghLogBuf_GED,
"dma_fence_add_callback, err = %d", err);
#endif
if (err < 0) {
dma_fence_put(psMonitor->psSyncFence);
ged_free(psMonitor, sizeof(struct GED_MONITOR_3D_FENCE));
} else {
int iCount = atomic_add_return(1, &g_i32Count);
if (iCount > 1) {
unsigned int uiFreqLevelID;
if (mtk_get_bottom_gpu_freq(&uiFreqLevelID)) {
if (uiFreqLevelID !=
mt_gpufreq_get_dvfs_table_num() - 1) {
if (ged_monitor_3D_fence_switch)
mtk_set_bottom_gpu_freq(
mt_gpufreq_get_dvfs_table_num()
- 1);
}
}
}
}
if (ged_monitor_3D_fence_debug > 0)
GED_LOGD("[+]3D fences count = %d\n", atomic_read(&g_i32Count));
#ifdef GED_DEBUG_MONITOR_3D_FENCE
ged_log_buf_print(ghLogBuf_GED,
"[-]%s, count = %d", __func__,
atomic_read(&g_i32Count));
#endif
return GED_OK;
}
void ged_monitor_3D_fence_set_enable(GED_BOOL bEnable)
{
if (bEnable != ged_monitor_3D_fence_switch && ged_smart_boost)
ged_monitor_3D_fence_switch = bEnable;
}
void ged_monitor_3D_fence_notify(void)
{
unsigned long long t;
t = ged_get_time();
do_div(t, 1000);
g_ul3DFenceDoneTime = (unsigned long)t;
}
int ged_monitor_3D_fence_get_count(void)
{
return atomic_read(&g_i32Count);
}
module_param(ged_smart_boost, uint, 0644);
module_param(ged_monitor_3D_fence_debug, uint, 0644);
module_param(ged_monitor_3D_fence_disable, uint, 0644);
module_param(ged_monitor_3D_fence_systrace, uint, 0644);
@@ -0,0 +1,574 @@
// SPDX-License-Identifier: GPL-2.0
/*
* Copyright (c) 2019 MediaTek Inc.
*/
#include <linux/version.h>
#include <linux/workqueue.h>
#include <linux/sched/clock.h>
#include <linux/atomic.h>
#include <linux/kernel.h>
#include <linux/hrtimer.h>
#include <linux/ktime.h>
#include <linux/mutex.h>
#include <asm/div64.h>
#include <mt-plat/mtk_gpu_utility.h>
#include "ged_notify_sw_vsync.h"
#include "ged_log.h"
#include "ged_base.h"
#include "ged_monitor_3D_fence.h"
#include "ged.h"
#undef CONFIG_MTK_QOS_V1_SUPPORT
#ifdef CONFIG_MTK_QOS_V1_SUPPORT
#include <mtk_gpu_bw.h>
#endif
#ifdef GED_ENABLE_FB_DVFS
#define GED_DVFS_FB_TIMER_TIMEOUT 100000000
#define GED_DVFS_TIMER_TIMEOUT g_fallback_time_out
#else
#define GED_DVFS_TIMER_TIMEOUT 25000000
#endif
#ifndef ENABLE_TIMER_BACKUP
#undef GED_DVFS_TIMER_TIMEOUT
#ifdef GED_ENABLE_FB_DVFS
#define GED_DVFS_FB_TIMER_TIMEOUT 100000000
#define GED_DVFS_TIMER_TIMEOUT g_fallback_time_out
#else
#define GED_DVFS_TIMER_TIMEOUT 25000000
#endif
#endif
#ifdef GED_ENABLE_FB_DVFS
static u64 g_fallback_time_out = GED_DVFS_FB_TIMER_TIMEOUT;
#endif
static struct hrtimer g_HT_hwvsync_emu;
#include "ged_dvfs.h"
#include "ged_global.h"
static struct workqueue_struct *g_psNotifyWorkQueue;
#if defined(CONFIG_MACH_MT8167) || defined(CONFIG_MACH_MT8173)\
|| defined(CONFIG_MACH_MT6739) || defined(CONFIG_MACH_MT6761)\
|| defined(CONFIG_MACH_MT6765)
static struct workqueue_struct *g_psDumpFW;
#endif
static struct mutex gsVsyncStampLock;
struct GED_NOTIFY_SW_SYNC {
struct work_struct sWork;
unsigned long t;
long phase;
unsigned long ul3DFenceDoneTime;
};
#if defined(CONFIG_MACH_MT8167) || defined(CONFIG_MACH_MT8173)\
|| defined(CONFIG_MACH_MT6739) || defined(CONFIG_MACH_MT6761)\
|| defined(CONFIG_MACH_MT6765)
struct GED_DUMP_FW {
struct work_struct sWork;
};
#endif
int (*ged_sw_vsync_event_fp)(bool bMode) = NULL;
EXPORT_SYMBOL(ged_sw_vsync_event_fp);
static struct mutex gsVsyncModeLock;
static int ged_sw_vsync_event(bool bMode)
{
static bool bCurMode;
int ret;
ret = 0;
mutex_lock(&gsVsyncModeLock);
if (bCurMode != bMode) {
bCurMode = bMode;
if (ged_sw_vsync_event_fp) {
ret = ged_sw_vsync_event_fp(bMode);
ged_log_buf_print(ghLogBuf_DVFS,
"[GED_K] ALL mode change to %d ", bCurMode);
} else
ged_log_buf_print(ghLogBuf_DVFS,
"[GED_K] LOCAL mode change to %d ", bCurMode);
if (bCurMode)
ret = 1;
}
mutex_unlock(&gsVsyncModeLock);
return ret;
}
static unsigned long long sw_vsync_ts;
static void ged_notify_sw_sync_work_handle(struct work_struct *psWork)
{
struct GED_NOTIFY_SW_SYNC *psNotify =
GED_CONTAINER_OF(psWork, struct GED_NOTIFY_SW_SYNC, sWork);
unsigned long long temp;
temp = 0;
if (psNotify) {
/* if callback is queued, send mode off to real driver */
ged_sw_vsync_event(false);
#ifdef ENABLE_TIMER_BACKUP
temp = ged_get_time();
if (temp-sw_vsync_ts > GED_DVFS_TIMER_TIMEOUT) {
do_div(temp, 1000);
psNotify->t = temp;
ged_dvfs_run(psNotify->t, psNotify->phase,
psNotify->ul3DFenceDoneTime);
ged_log_buf_print(ghLogBuf_DVFS,
"[GED_K] Timer kicked (ts=%llu) ", temp);
} else {
ged_log_buf_print(ghLogBuf_DVFS,
"[GED_K] Timer kick giveup (ts=%llu)", temp);
}
#endif
ged_free(psNotify, sizeof(struct GED_NOTIFY_SW_SYNC));
}
}
#define GED_VSYNC_MISS_QUANTUM_NS 16666666
#ifdef ENABLE_COMMON_DVFS
static unsigned long long hw_vsync_ts;
#endif
static bool g_timer_on;
static unsigned long long g_timer_on_ts;
static bool g_bGPUClock;
/*
* void timer_switch(bool bTock)
* only set the staus, not really operating on real timer
*/
void timer_switch(bool bTock)
{
mutex_lock(&gsVsyncStampLock);
g_timer_on = bTock;
if (bTock)
g_timer_on_ts = ged_get_time();
mutex_unlock(&gsVsyncStampLock);
}
void timer_switch_locked(bool bTock)
{
g_timer_on = bTock;
if (bTock)
g_timer_on_ts = ged_get_time();
}
static void ged_timer_switch_work_handle(struct work_struct *psWork)
{
struct GED_NOTIFY_SW_SYNC *psNotify =
GED_CONTAINER_OF(psWork, struct GED_NOTIFY_SW_SYNC, sWork);
if (psNotify) {
ged_sw_vsync_event(false);
timer_switch(false);
ged_free(psNotify, sizeof(struct GED_NOTIFY_SW_SYNC));
}
}
#ifdef GED_ENABLE_FB_DVFS
void ged_set_backup_timer_timeout(u64 time_out)
{
if (time_out != 0)
g_fallback_time_out = time_out;
else
g_fallback_time_out = GED_DVFS_FB_TIMER_TIMEOUT;
}
void ged_cancel_backup_timer(void)
{
unsigned long long temp;
temp = ged_get_time();
#ifdef ENABLE_TIMER_BACKUP
if (hrtimer_try_to_cancel(&g_HT_hwvsync_emu)) {
/* Timer is either queued or in cb
* cancel it to ensure it is not bother any way
*/
hrtimer_cancel(&g_HT_hwvsync_emu);
hrtimer_start(&g_HT_hwvsync_emu,
ns_to_ktime(GED_DVFS_TIMER_TIMEOUT), HRTIMER_MODE_REL);
ged_log_buf_print(ghLogBuf_DVFS,
"[GED_K] Timer Restart (ts=%llu)", temp);
} else {
/*
* Timer is not existed
*/
hrtimer_start(&g_HT_hwvsync_emu,
ns_to_ktime(GED_DVFS_TIMER_TIMEOUT), HRTIMER_MODE_REL);
ged_log_buf_print(ghLogBuf_DVFS,
"[GED_K] New Timer Start (ts=%llu)", temp);
timer_switch_locked(true);
}
#endif /* #ifdef ENABLE_TIMER_BACKUP */
}
#endif
GED_ERROR ged_notify_sw_vsync(GED_VSYNC_TYPE eType,
struct GED_DVFS_UM_QUERY_PACK *psQueryData)
{
ged_notification(GED_NOTIFICATION_TYPE_SW_VSYNC);
{
#ifdef ENABLE_COMMON_DVFS
#ifndef GED_ENABLE_FB_DVFS
long phase = 0;
unsigned long t;
bool bHWEventKick = false;
long long llDiff = 0;
#endif
unsigned long long temp;
unsigned long ul3DFenceDoneTime;
psQueryData->bFirstBorn = ged_sw_vsync_event(true);
ul3DFenceDoneTime = ged_monitor_3D_fence_done_time();
psQueryData->ul3DFenceDoneTime = ul3DFenceDoneTime;
hw_vsync_ts = temp = ged_get_time();
if (g_gpu_timer_based_emu) {
ged_log_buf_print(ghLogBuf_DVFS,
"[GED_K] Vsync ignored (ts=%llu)", temp);
#ifndef GED_ENABLE_FB_DVFS
return GED_ERROR_INTENTIONAL_BLOCK;
#endif
}
#ifdef GED_ENABLE_FB_DVFS
return GED_ERROR_INTENTIONAL_BLOCK;
#else
/*critical session begin*/
mutex_lock(&gsVsyncStampLock);
if (eType == GED_VSYNC_SW_EVENT) {
sw_vsync_ts = temp;
#ifdef ENABLE_TIMER_BACKUP
if (hrtimer_try_to_cancel(&g_HT_hwvsync_emu)) {
/* Timer is either queued or in cb
* cancel it to ensure it is not bother any way
*/
hrtimer_cancel(&g_HT_hwvsync_emu);
hrtimer_start(&g_HT_hwvsync_emu,
ns_to_ktime(GED_DVFS_TIMER_TIMEOUT), HRTIMER_MODE_REL);
ged_log_buf_print(ghLogBuf_DVFS,
"[GED_K] Timer Restart (ts=%llu)", temp);
} else {
/*
* Timer is not existed
*/
hrtimer_start(&g_HT_hwvsync_emu,
ns_to_ktime(GED_DVFS_TIMER_TIMEOUT), HRTIMER_MODE_REL);
ged_log_buf_print(ghLogBuf_DVFS,
"[GED_K] New Timer Start (ts=%llu)", temp);
timer_switch_locked(true);
}
#endif // #ifdef ENABLE_TIMER_BACKUP
} else {
hw_vsync_ts = temp;
llDiff = (long long)(hw_vsync_ts - sw_vsync_ts);
if (llDiff > GED_VSYNC_MISS_QUANTUM_NS)
bHWEventKick = true;
}
#ifdef GED_DVFS_DEBUG
if (eType == GED_VSYNC_HW_EVENT)
GED_LOGD("HW VSYNC: llDiff=",
"%lld, hw_vsync_ts=%llu, sw_vsync_ts=%llu\n", llDiff,
hw_vsync_ts, sw_vsync_ts);
else
GED_LOGD("SW VSYNC: llDiff=",
"%lld, hw_vsync_ts=%llu, sw_vsync_ts=%llu\n", llDiff,
hw_vsync_ts, sw_vsync_ts);
#endif /// #ifdef GED_DVFS_DEBUG
if (eType == GED_VSYNC_HW_EVENT)
ged_log_buf_print(ghLogBuf_DVFS,
"[GED_K] HW VSYNC (ts=%llu) ", hw_vsync_ts);
else
ged_log_buf_print(ghLogBuf_DVFS,
"[GED_K] SW VSYNC (ts=%llu) ", sw_vsync_ts);
mutex_unlock(&gsVsyncStampLock);
/*critical session end*/
if (eType == GED_VSYNC_SW_EVENT) {
do_div(temp, 1000);
t = (unsigned long)(temp);
// for some cases just align vsync to FenceDoneTime
if (ul3DFenceDoneTime > t) {
if (ul3DFenceDoneTime - t < GED_DVFS_DIFF_THRESHOLD)
t = ul3DFenceDoneTime;
}
psQueryData->usT = t;
ged_dvfs_run(t, phase, ul3DFenceDoneTime);
ged_dvfs_sw_vsync_query_data(psQueryData);
} else {
if (bHWEventKick) {
#ifdef GED_DVFS_DEBUG
GED_LOGD("HW Event: kick!\n");
#endif /// GED_DVFS_DEBUG
ged_log_buf_print(ghLogBuf_DVFS,
"[GED_K] HW VSync: mending kick!");
ged_dvfs_run(0, 0, 0);
}
}
#endif
#else
unsigned long long temp;
temp = ged_get_time();
ged_sw_vsync_event(true);
return GED_ERROR_INTENTIONAL_BLOCK;
#endif
return GED_OK;
}
}
extern unsigned int gpu_loading;
enum hrtimer_restart ged_sw_vsync_check_cb(struct hrtimer *timer)
{
unsigned long long temp;
long long llDiff;
struct GED_NOTIFY_SW_SYNC *psNotify;
temp = cpu_clock(smp_processor_id());
llDiff = (long long)(temp - sw_vsync_ts);
if (llDiff > GED_VSYNC_MISS_QUANTUM_NS) {
psNotify = (struct GED_NOTIFY_SW_SYNC *)
ged_alloc_atomic(sizeof(struct GED_NOTIFY_SW_SYNC));
#ifndef ENABLE_TIMER_BACKUP
ged_dvfs_cal_gpu_utilization(&gpu_av_loading,
&gpu_block, &gpu_idle);
gpu_loading = gpu_av_loading;
#endif
if (false == g_bGPUClock && 0 == gpu_loading
&& (temp - g_ns_gpu_on_ts > GED_DVFS_TIMER_TIMEOUT)) {
if (psNotify) {
INIT_WORK(&psNotify->sWork,
ged_timer_switch_work_handle);
queue_work(g_psNotifyWorkQueue,
&psNotify->sWork);
}
ged_log_buf_print(ghLogBuf_DVFS,
"[GED_K] Timer removed (ts=%llu) ", temp);
return HRTIMER_NORESTART;
}
if (psNotify) {
INIT_WORK(&psNotify->sWork,
ged_notify_sw_sync_work_handle);
psNotify->phase = GED_DVFS_TIMER_BACKUP;
psNotify->ul3DFenceDoneTime = 0;
queue_work(g_psNotifyWorkQueue, &psNotify->sWork);
ged_log_buf_print(ghLogBuf_DVFS,
"[GED_K] Timer queue to kick (ts=%llu)", temp);
hrtimer_start(&g_HT_hwvsync_emu,
ns_to_ktime(GED_DVFS_TIMER_TIMEOUT), HRTIMER_MODE_REL);
g_timer_on_ts = temp;
}
}
return HRTIMER_NORESTART;
}
bool ged_gpu_power_on_notified;
bool ged_gpu_power_off_notified;
void ged_dvfs_gpu_clock_switch_notify(bool bSwitch)
{
if (bSwitch) {
ged_gpu_power_on_notified = true;
#ifdef CONFIG_MTK_QOS_V1_SUPPORT
mt_gpu_bw_toggle(1);
#endif
g_ns_gpu_on_ts = ged_get_time();
g_bGPUClock = true;
if (g_timer_on) {
ged_log_buf_print(ghLogBuf_DVFS,
"[GED_K] Timer Already Start");
} else {
hrtimer_start(&g_HT_hwvsync_emu,
ns_to_ktime(GED_DVFS_TIMER_TIMEOUT), HRTIMER_MODE_REL);
ged_log_buf_print(ghLogBuf_DVFS,
"[GED_K] HW Start Timer");
timer_switch(true);
}
} else {
#ifdef CONFIG_MTK_QOS_V1_SUPPORT
mt_gpu_bw_toggle(0);
#endif
ged_gpu_power_off_notified = true;
g_bGPUClock = false;
ged_log_buf_print(ghLogBuf_DVFS, "[GED_K] Buck-off");
}
}
EXPORT_SYMBOL(ged_dvfs_gpu_clock_switch_notify);
#define GED_TIMER_BACKUP_THRESHOLD 3000
/*
* SODI implementation need to cancel timer physically.
* but timer status is logically unchanged *
*/
/*
* enter sodi state is trivial, just cancel timer
*/
void ged_sodi_start(void)
{
hrtimer_try_to_cancel(&g_HT_hwvsync_emu);
}
/*
* exit sodi state should aware sands of time is still running
*/
void ged_sodi_stop(void)
{
unsigned long long ns_cur_time;
unsigned long long ns_timer_remains;
if (g_timer_on) {
ns_cur_time = ged_get_time();
ns_timer_remains = ns_cur_time
- g_timer_on_ts - GED_DVFS_TIMER_TIMEOUT;
// slept too long, do timber-based DVFS now
if (ns_timer_remains < GED_TIMER_BACKUP_THRESHOLD) {
struct GED_NOTIFY_SW_SYNC *psNotify;
psNotify = (struct GED_NOTIFY_SW_SYNC *)
ged_alloc_atomic(
sizeof(struct GED_NOTIFY_SW_SYNC));
if (psNotify) {
INIT_WORK(&psNotify->sWork,
ged_notify_sw_sync_work_handle);
psNotify->t = ns_cur_time;
psNotify->phase = GED_DVFS_TIMER_BACKUP;
psNotify->ul3DFenceDoneTime = 0;
queue_work(g_psNotifyWorkQueue,
&psNotify->sWork);
}
hrtimer_start(&g_HT_hwvsync_emu,
ns_to_ktime(GED_DVFS_TIMER_TIMEOUT), HRTIMER_MODE_REL);
} else if (ns_timer_remains > GED_DVFS_TIMER_TIMEOUT) {
// unknown status, just start timer with default timeout
hrtimer_start(&g_HT_hwvsync_emu,
ns_to_ktime(GED_DVFS_TIMER_TIMEOUT), HRTIMER_MODE_REL);
} else {
// keep counting down the timer with real remianing time
hrtimer_start(&g_HT_hwvsync_emu,
ns_to_ktime(ns_timer_remains), HRTIMER_MODE_REL);
}
}
}
#ifdef CONFIG_MTK_GPU_SUPPORT /* Only enable when GPU isn't kerenl module */
#if defined(CONFIG_MACH_MT8167) || defined(CONFIG_MACH_MT8173)\
|| defined(CONFIG_MACH_MT6739) || defined(CONFIG_MACH_MT6761)\
|| defined(CONFIG_MACH_MT6765)
static void ged_dump_fw_handle(struct work_struct *psWork)
{
struct GED_DUMP_FW *psNotify
= GED_CONTAINER_OF(psWork, struct GED_DUMP_FW, sWork);
if (psNotify) {
MTKFWDump();
ged_free(psNotify, sizeof(struct GED_DUMP_FW));
}
}
void ged_dump_fw(void)
{
struct GED_DUMP_FW *psNotify;
psNotify = (struct GED_DUMP_FW *)
ged_alloc_atomic(sizeof(struct GED_DUMP_FW));
if (psNotify) {
INIT_WORK(&psNotify->sWork, ged_dump_fw_handle);
queue_work(g_psDumpFW, &psNotify->sWork);
}
}
EXPORT_SYMBOL(ged_dump_fw);
#endif
#endif /* CONFIG_MTK_GPU_SUPPORT */
GED_ERROR ged_notify_sw_vsync_system_init(void)
{
g_psNotifyWorkQueue = create_workqueue("ged_notify_sw_vsync");
if (g_psNotifyWorkQueue == NULL)
return GED_ERROR_OOM;
#if defined(CONFIG_MACH_MT8167) || defined(CONFIG_MACH_MT8173)\
|| defined(CONFIG_MACH_MT6739) || defined(CONFIG_MACH_MT6761)\
|| defined(CONFIG_MACH_MT6765)
g_psDumpFW = NULL;
g_psDumpFW = create_workqueue("ged_dump_fw_log");
if (g_psDumpFW == NULL)
return GED_ERROR_OOM;
#endif
mutex_init(&gsVsyncStampLock);
mutex_init(&gsVsyncModeLock);
hrtimer_init(&g_HT_hwvsync_emu, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
g_HT_hwvsync_emu.function = ged_sw_vsync_check_cb;
mtk_gpu_sodi_entry_fp = ged_sodi_start;
mtk_gpu_sodi_exit_fp = ged_sodi_stop;
return GED_OK;
}
void ged_notify_sw_vsync_system_exit(void)
{
if (g_psNotifyWorkQueue != NULL) {
flush_workqueue(g_psNotifyWorkQueue);
destroy_workqueue(g_psNotifyWorkQueue);
g_psNotifyWorkQueue = NULL;
}
#if defined(CONFIG_MACH_MT8167) || defined(CONFIG_MACH_MT8173)\
|| defined(CONFIG_MACH_MT6739) || defined(CONFIG_MACH_MT6761)\
|| defined(CONFIG_MACH_MT6765)
if (g_psDumpFW != NULL) {
flush_workqueue(g_psDumpFW);
destroy_workqueue(g_psDumpFW);
g_psDumpFW = NULL;
}
#endif
#ifdef ENABLE_COMMON_DVFS
hrtimer_cancel(&g_HT_hwvsync_emu);
#endif
mutex_destroy(&gsVsyncModeLock);
mutex_destroy(&gsVsyncStampLock);
}
+82
View File
@@ -0,0 +1,82 @@
// SPDX-License-Identifier: GPL-2.0
/*
* Copyright (C) 2020 MediaTek Inc.
*/
#include "ged_sysfs.h"
#include "ged_base.h"
#define GED_SYSFS_DIR_NAME "ged"
static struct kobject *ged_kobj;
GED_ERROR ged_sysfs_create_dir(struct kobject *parent,
const char *name, struct kobject **ppsKobj)
{
struct kobject *psKobj = NULL;
if (name == NULL || ppsKobj == NULL)
return GED_ERROR_INVALID_PARAMS;
parent = (parent != NULL) ? parent : ged_kobj;
psKobj = kobject_create_and_add(name, parent);
if (!psKobj) {
GED_LOGE("Failed to create '%s' sysfs directory\n",
name);
return GED_ERROR_FAIL;
}
*ppsKobj = psKobj;
return GED_OK;
}
// --------------------------------------------------
void ged_sysfs_remove_dir(struct kobject **ppsKobj)
{
if (ppsKobj == NULL)
return;
kobject_put(*ppsKobj);
*ppsKobj = NULL;
}
// --------------------------------------------------
GED_ERROR ged_sysfs_create_file(struct kobject *parent,
struct kobj_attribute *kobj_attr)
{
if (kobj_attr == NULL)
return GED_ERROR_INVALID_PARAMS;
parent = (parent != NULL) ? parent : ged_kobj;
if (sysfs_create_file(parent, &(kobj_attr->attr))) {
GED_LOGE("Failed to create sysfs file\n");
return GED_ERROR_FAIL;
}
return GED_OK;
}
// --------------------------------------------------
void ged_sysfs_remove_file(struct kobject *parent,
struct kobj_attribute *kobj_attr)
{
if (kobj_attr == NULL)
return;
parent = (parent != NULL) ? parent : ged_kobj;
sysfs_remove_file(parent, &(kobj_attr->attr));
}
// --------------------------------------------------
GED_ERROR ged_sysfs_init(void)
{
ged_kobj = kobject_create_and_add(GED_SYSFS_DIR_NAME,
kernel_kobj);
if (!ged_kobj) {
GED_LOGE("Failed to create '%s' sysfs root directory\n",
GED_SYSFS_DIR_NAME);
return GED_ERROR_FAIL;
}
return GED_OK;
}
// --------------------------------------------------
void ged_sysfs_exit(void)
{
kobject_put(ged_kobj);
ged_kobj = NULL;
}
+71
View File
@@ -0,0 +1,71 @@
// SPDX-License-Identifier: GPL-2.0
/*
* Copyright (c) 2019 MediaTek Inc.
*/
#include <linux/sched.h>
#include <linux/kthread.h>
#include "ged_base.h"
#include "ged_thread.h"
struct GED_THREAD_DATA {
struct task_struct *psThread;
void (*pFunc)(void *f);
void *pvData;
};
static int ged_thread_run(void *pvData)
{
struct GED_THREAD_DATA *psThreadData = (struct GED_THREAD_DATA *)pvData;
if (psThreadData == NULL)
return 0;
psThreadData->pFunc(psThreadData->pvData);
while (!kthread_should_stop())
schedule();
return 0;
}
GED_ERROR ged_thread_create(GED_THREAD_HANDLE *phThread,
const char *szThreadName, void (*pFunc)(void *), void *pvData)
{
struct GED_THREAD_DATA *psThreadData;
if (phThread == NULL)
return GED_ERROR_INVALID_PARAMS;
psThreadData =
(struct GED_THREAD_DATA *)ged_alloc(sizeof(struct GED_THREAD_DATA));
if (psThreadData == NULL)
return GED_ERROR_OOM;
psThreadData->pFunc = pFunc;
psThreadData->pvData = pvData;
psThreadData->psThread =
kthread_run(ged_thread_run, psThreadData, szThreadName);
if (IS_ERR(psThreadData->psThread)) {
ged_free(psThreadData, sizeof(struct GED_THREAD_DATA));
return GED_ERROR_OOM;
}
*phThread = (GED_THREAD_HANDLE)psThreadData;
return GED_OK;
}
GED_ERROR ged_thread_destroy(GED_THREAD_HANDLE hThread)
{
struct GED_THREAD_DATA *psThreadData =
(struct GED_THREAD_DATA *)hThread;
if (psThreadData == NULL)
return GED_ERROR_INVALID_PARAMS;
kthread_stop(psThreadData->psThread);
ged_free(psThreadData, sizeof(struct GED_THREAD_DATA));
return GED_OK;
}
+20
View File
@@ -0,0 +1,20 @@
/* SPDX-License-Identifier: GPL-2.0 */
/*
* Copyright (c) 2019 MediaTek Inc.
*/
#ifndef __GED_THREAD_H__
#define __GED_THREAD_H__
#include "ged_type.h"
#define GED_THREAD_HANDLE void*
//typedef void (*GED_THREAD_FUNC)(void *);
GED_ERROR ged_thread_create(GED_THREAD_HANDLE *phThread,
const char *szThreadName, void (*pFunc)(void *), void *pvData);
GED_ERROR ged_thread_destroy(GED_THREAD_HANDLE hThread);
#endif
+21
View File
@@ -0,0 +1,21 @@
#
# Copyright (C) 2018 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.
#
ccflags-y += -I$(srctree)/drivers/misc/mediatek/dramc/$(MTK_PLATFORM)/
ccflags-y += -I$(srctree)/drivers/misc/mediatek/sspm/
ccflags-y += -I$(srctree)/drivers/misc/mediatek/qos/
ccflags-y += -I$(srctree)/drivers/gpu/mediatek/
ccflags-$(CONFIG_MTK_QOS_FRAMEWORK) += -DMTK_QOS_FRAMEWORK
obj-y += gpu_bm.o
+149
View File
@@ -0,0 +1,149 @@
// SPDX-License-Identifier: GPL-2.0
/*
* Copyright (c) 2019 MediaTek Inc.
*/
#include <linux/slab.h>
#include <linux/fs.h>
#include <linux/debugfs.h>
#include <linux/seq_file.h>
#include <linux/proc_fs.h>
#include <linux/io.h>
#include <linux/timer.h>
#ifdef MTK_QOS_FRAMEWORK
#include <mtk_qos_ipi.h>
#endif
#include <mt-plat/mtk_gpu_utility.h>
#define MTKGPUQoS_UNREFERENCED(param) ((void)(param))
uint32_t __iomem *gpu_info_buf;
static void _mgq_proc_show_v1(struct seq_file *m)
{
seq_printf(m, "ctx: \t0x%x\n", readl(gpu_info_buf + 1));
seq_printf(m, "frame: \t0x%x\n", readl(gpu_info_buf + 2));
seq_printf(m, "job: \t0x%x\n", readl(gpu_info_buf + 3));
seq_printf(m, "freq: \t0x%x\n", readl(gpu_info_buf + 4));
seq_printf(m, "bw: \t0x%x\n", readl(gpu_info_buf + 5));
seq_printf(m, "pbw: \t0x%x\n", readl(gpu_info_buf + 6));
}
static int _mgq_proc_show(struct seq_file *m, void *v)
{
if (gpu_info_buf) {
unsigned int version = readl(gpu_info_buf + 0);
seq_printf(m, "version: 0x%x\n", version);
if (version == 1)
_mgq_proc_show_v1(m);
else
seq_printf(m, "unknown version: 0x%x\n", version);
} else {
seq_puts(m, "gpu_info_buf == null\n");
}
return 0;
}
static int _mgq_proc_open(struct inode *inode, struct file *file)
{
return single_open(file, _mgq_proc_show, NULL);
}
static const struct file_operations _mgq_proc_fops = {
.owner = THIS_MODULE,
.open = _mgq_proc_open,
.read = seq_read,
.llseek = seq_lseek,
.release = single_release,
};
static int _MTKGPUQoS_initDebugFS(void)
{
struct proc_dir_entry *dir = NULL;
dir = proc_mkdir("mgq", NULL);
if (!dir) {
pr_debug("@%s: create /proc/mgq failed\n", __func__);
return -ENOMEM;
}
if (!proc_create("job_status", 0664, dir, &_mgq_proc_fops))
pr_debug("@%s: create /proc/mgq/job_status failed\n", __func__);
return 0;
}
struct timer_list timer_setupFW;
struct setupfw_t {
phys_addr_t phyaddr;
size_t size;
};
static void _MTKGPUQoS_setupFW(phys_addr_t phyaddr, size_t size)
{
#ifdef MTK_QOS_FRAMEWORK
struct qos_ipi_data qos_d;
int ret;
qos_d.cmd = QOS_IPI_SETUP_GPU_INFO;
qos_d.u.gpu_info.addr = (unsigned int)phyaddr;
qos_d.u.gpu_info.addr_hi = (unsigned int)(phyaddr >> 32);
qos_d.u.gpu_info.size = (unsigned int)size;
ret = qos_ipi_to_sspm_command(&qos_d, 4);
pr_debug("%s: addr:0x%x, addr_hi:0x%x, ret:%d\n",
__func__,
qos_d.u.gpu_info.addr,
qos_d.u.gpu_info.addr_hi,
ret);
#endif
}
static void bw_v1_gpu_power_change_notify(int power_on)
{
static int ctx;
if (!power_on) {
ctx = readl(gpu_info_buf + 1);
writel(0, gpu_info_buf + 1); // ctx
} else {
writel(ctx, gpu_info_buf + 1);
}
}
void MTKGPUQoS_setup(uint32_t *cpuaddr, phys_addr_t phyaddr, size_t size)
{
gpu_info_buf = cpuaddr;
_MTKGPUQoS_initDebugFS();
_MTKGPUQoS_setupFW(phyaddr, size);
#if defined(CONFIG_MTK_GPU_SUPPORT)
mtk_register_gpu_power_change("qpu_qos", bw_v1_gpu_power_change_notify);
#else
MTKGPUQoS_UNREFERENCED(bw_v1_gpu_power_change_notify);
#endif
}
EXPORT_SYMBOL(MTKGPUQoS_setup);
uint32_t MTKGPUQoS_getBW(uint32_t offset)
{
unsigned int version;
if (!gpu_info_buf)
return 0;
version = readl(gpu_info_buf + 0);
if (version == 1) {
if (offset == 0)
return readl(gpu_info_buf + 5);
if (offset == 1)
return readl(gpu_info_buf + 6);
}
return 0;
}
EXPORT_SYMBOL(MTKGPUQoS_getBW);
+12
View File
@@ -0,0 +1,12 @@
/* SPDX-License-Identifier: GPL-2.0 */
/*
* Copyright (c) 2019 MediaTek Inc.
*/
#ifndef __GPU_BM_H__
#define __GPU_BM_H__
void MTKGPUQoS_setup(uint32_t *cpuaddr, phys_addr_t phyaddr, size_t size);
#endif
+39
View File
@@ -0,0 +1,39 @@
#
# Copyright (C) 2018 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.
#
ifeq ($(CONFIG_MACH_MT6781),y)
MTK_PLATFORM := mt6781
ccflags-y += -I$(srctree)/drivers/misc/mediatek/dramc/mt6785/
ccflags-y += -I$(srctree)/drivers/misc/mediatek/sspm/v2/
ccflags-y += -I$(srctree)/drivers/misc/mediatek/sspm/$(MTK_PLATFORM)/
ccflags-y += -I$(srctree)/drivers/misc/mediatek/base/power/qos/$(MTK_PLATFORM)/
ccflags-y += -I$(srctree)/drivers/misc/mediatek/base/power/include/
ccflags-y += -I$(srctree)/drivers/misc/mediatek/base/power/$(MTK_PLATFORM)/
else ifneq (, $(findstring $(CONFIG_MTK_PLATFORM), "mt6885" "mt6873" "mt6853"))
ccflags-y += -I$(srctree)/drivers/misc/mediatek/dramc/$(MTK_PLATFORM)/
ccflags-y += -I$(srctree)/drivers/misc/mediatek/sspm/v2/
ccflags-y += -I$(srctree)/drivers/misc/mediatek/sspm/$(MTK_PLATFORM)/
ccflags-y += -I$(srctree)/drivers/misc/mediatek/base/power/qos/$(MTK_PLATFORM)/
ccflags-y += -I$(srctree)/drivers/misc/mediatek/base/power/include/
ccflags-y += -I$(srctree)/drivers/misc/mediatek/base/power/$(MTK_PLATFORM)/
else
ccflags-y += -I$(srctree)/drivers/misc/mediatek/dramc/$(MTK_PLATFORM)/
ccflags-y += -I$(srctree)/drivers/misc/mediatek/sspm/
ccflags-y += -I$(srctree)/drivers/misc/mediatek/qos/
endif
ccflags-y += -I$(srctree)/drivers/gpu/mediatek/
ccflags-y += -I$(srctree)/drivers/gpu/mediatek/gpufreq/
ccflags-$(CONFIG_MTK_QOS_FRAMEWORK) += -DMTK_QOS_FRAMEWORK
obj-y += gpu_bm.o
+168
View File
@@ -0,0 +1,168 @@
// SPDX-License-Identifier: GPL-2.0
/*
* Copyright (c) 2021 MediaTek Inc.
*/
#include <linux/slab.h>
#include <linux/fs.h>
#include <linux/debugfs.h>
#include <linux/seq_file.h>
#include <linux/proc_fs.h>
#include <linux/io.h>
#include <linux/timer.h>
#ifdef MTK_QOS_FRAMEWORK
#include <mtk_qos_ipi.h>
#endif
#include <mt-plat/mtk_gpu_utility.h>
#include <mtk_gpufreq.h>
struct v1_data {
unsigned int version;
unsigned int ctx;
unsigned int frame;
unsigned int job;
unsigned int freq;
};
struct v1_data *gpu_info_buf;
static void _mgq_proc_show_v1(struct seq_file *m)
{
seq_printf(m, "ctx: \t%d\n", gpu_info_buf->ctx);
seq_printf(m, "frame: \t%d\n", gpu_info_buf->frame);
seq_printf(m, "job: \t%d\n", gpu_info_buf->job);
seq_printf(m, "freq: \t%d\n", gpu_info_buf->freq);
}
static int _mgq_proc_show(struct seq_file *m, void *v)
{
if (gpu_info_buf) {
unsigned int version = readl(gpu_info_buf + 0);
seq_printf(m, "version: %d\n", version);
if (version == 1)
_mgq_proc_show_v1(m);
else
seq_printf(m, "unknown version: 0x%x\n", version);
} else {
seq_puts(m, "gpu_info_buf == null\n");
}
return 0;
}
static int _mgq_proc_open(struct inode *inode, struct file *file)
{
return single_open(file, _mgq_proc_show, NULL);
}
static const struct file_operations _mgq_proc_fops = {
.owner = THIS_MODULE,
.open = _mgq_proc_open,
.read = seq_read,
.llseek = seq_lseek,
.release = single_release,
};
static int _MTKGPUQoS_initDebugFS(void)
{
struct proc_dir_entry *dir = NULL;
dir = proc_mkdir("mgq", NULL);
if (!dir) {
pr_debug("@%s: create /proc/mgq failed\n", __func__);
return -ENOMEM;
}
if (!proc_create("job_status", 0644, dir, &_mgq_proc_fops))
pr_debug("@%s: create /proc/mgq/job_status failed\n", __func__);
return 0;
}
struct setupfw_t {
phys_addr_t phyaddr;
size_t size;
};
static struct setupfw_t setupfw_data;
static void setupfw_work_handler(struct work_struct *work);
static DECLARE_DELAYED_WORK(g_setupfw_work, setupfw_work_handler);
static void setupfw_work_handler(struct work_struct *work)
{
struct qos_ipi_data qos_d;
int ret;
qos_d.cmd = QOS_IPI_SETUP_GPU_INFO;
qos_d.u.gpu_info.addr = (unsigned int)setupfw_data.phyaddr;
qos_d.u.gpu_info.addr_hi = (unsigned int)(setupfw_data.phyaddr >> 32);
qos_d.u.gpu_info.size = (unsigned int)setupfw_data.size;
ret = qos_ipi_to_sspm_command(&qos_d, 4);
pr_debug("%s: addr:0x%x, addr_hi:0x%x, ret:%d\n",
__func__,
qos_d.u.gpu_info.addr,
qos_d.u.gpu_info.addr_hi,
ret);
if (ret == 1) {
pr_debug("%s: sspm_ipi success! (%d)\n", __func__, ret);
} else {
pr_debug("%s: sspm_ipi fail (%d)\n", __func__, ret);
schedule_delayed_work(&g_setupfw_work, 5 * HZ);
}
}
static void _MTKGPUQoS_setupFW(phys_addr_t phyaddr, size_t size)
{
setupfw_data.phyaddr = phyaddr;
setupfw_data.size = size;
schedule_delayed_work(&g_setupfw_work, 1);
}
static void bw_v1_gpu_power_change_notify(int power_on)
{
static int ctx;
unsigned int loading, idx, min_idx;
mtk_get_gpu_loading(&loading);
idx = mt_gpufreq_get_cur_freq_index();
min_idx = mt_gpufreq_get_dvfs_table_num()-1;
if (!power_on) {
ctx = gpu_info_buf->ctx;
gpu_info_buf->ctx = 0; // ctx
} else {
gpu_info_buf->ctx = ctx;
/*
* if gpu loading < 40% and gpu freq is lowest,
* don't do GPU QoS prediction.
*/
if ((idx == min_idx) && (loading < 40))
gpu_info_buf->freq = 5566;
else
gpu_info_buf->freq = 0;
}
}
void MTKGPUQoS_setup(struct v1_data *v1, phys_addr_t phyaddr, size_t size)
{
gpu_info_buf = v1;
_MTKGPUQoS_initDebugFS();
_MTKGPUQoS_setupFW(phyaddr, size);
mtk_register_gpu_power_change("qpu_qos", bw_v1_gpu_power_change_notify);
}
EXPORT_SYMBOL(MTKGPUQoS_setup);
uint32_t MTKGPUQoS_getBW(uint32_t offset)
{
return 0;
}
EXPORT_SYMBOL(MTKGPUQoS_getBW);
+14
View File
@@ -0,0 +1,14 @@
/* SPDX-License-Identifier: GPL-2.0 */
/*
* Copyright (c) 2021 MediaTek Inc.
*/
#ifndef __GPU_BM_H__
#define __GPU_BM_H__
//void MTKGPUQoS_setup(uint32_t *cpuaddr, phys_addr_t phyaddr, size_t size);
void MTKGPUQoS_setup(struct v1_data *v1, phys_addr_t phyaddr, size_t size);
#endif
+17
View File
@@ -0,0 +1,17 @@
#
# Copyright (C) 2015 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 the
# GNU General Public License for more details.
#
MTK_GPU_VERSION = $(subst ",,$(CONFIG_MTK_GPU_VERSION))
$(info *MTK_GPU_VERSION 2 = $(word 2,$(MTK_GPU_VERSION)))
obj-y += mali_$(word 2,$(MTK_GPU_VERSION))/
+21
View File
@@ -0,0 +1,21 @@
#
# Copyright (C) 2015 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 the
# GNU General Public License for more details.
#
MTK_GPU_VERSION = $(subst ",,$(CONFIG_MTK_GPU_VERSION))
$(info *MTK_GPU_VERSION 3 = $(word 3,$(MTK_GPU_VERSION)))
ifneq ($(word 3,$(MTK_GPU_VERSION)),)
obj-y += mali-$(word 3,$(MTK_GPU_VERSION))/drivers/gpu/arm/midgard/
else
obj-y += mali-EAC/drivers/
endif
@@ -0,0 +1,24 @@
#
# (C) COPYRIGHT 2012 ARM Limited. All rights reserved.
#
# This program is free software and is provided to you under the terms of the
# GNU General Public License version 2 as published by the Free Software
# Foundation, and any use by you of this program is subject to the terms
# of such GNU licence.
#
# 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 the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, you can access it online at
# http://www.gnu.org/licenses/gpl-2.0.html.
#
# SPDX-License-Identifier: GPL-2.0
#
#
ifneq ($(CONFIG_DMA_SHARED_BUFFER),)
obj-m := dma_buf_lock.o
endif
@@ -0,0 +1,38 @@
#
# (C) COPYRIGHT 2012 ARM Limited. All rights reserved.
#
# This program is free software and is provided to you under the terms of the
# GNU General Public License version 2 as published by the Free Software
# Foundation, and any use by you of this program is subject to the terms
# of such GNU licence.
#
# 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 the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, you can access it online at
# http://www.gnu.org/licenses/gpl-2.0.html.
#
# SPDX-License-Identifier: GPL-2.0
#
#
# linux build system bootstrap for out-of-tree module
# default to building for the host
ARCH ?= $(shell uname -m)
ifeq ($(KDIR),)
$(error Must specify KDIR to point to the kernel to target))
endif
all: dma_buf_lock
dma_buf_lock:
$(MAKE) ARCH=$(ARCH) -C $(KDIR) M=$(CURDIR) EXTRA_CFLAGS="-I$(CURDIR)/../../../../include"
clean:
$(MAKE) ARCH=$(ARCH) -C $(KDIR) M=$(CURDIR) clean
@@ -0,0 +1,898 @@
/*
*
* (C) COPYRIGHT 2012-2013, 2017-2018 ARM Limited. All rights reserved.
*
* This program is free software and is provided to you under the terms of the
* GNU General Public License version 2 as published by the Free Software
* Foundation, and any use by you of this program is subject to the terms
* of such GNU licence.
*
* 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 the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, you can access it online at
* http://www.gnu.org/licenses/gpl-2.0.html.
*
* SPDX-License-Identifier: GPL-2.0
*
*/
#include <linux/version.h>
#include <linux/uaccess.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/cdev.h>
#include <linux/device.h>
#include <linux/slab.h>
#include <linux/atomic.h>
#include <linux/reservation.h>
#include <linux/dma-buf.h>
#include <linux/wait.h>
#include <linux/sched.h>
#include <linux/poll.h>
#include <linux/anon_inodes.h>
#include <linux/file.h>
#if (LINUX_VERSION_CODE < KERNEL_VERSION(4, 10, 0))
#include <linux/fence.h>
#define dma_fence_context_alloc(a) fence_context_alloc(a)
#define dma_fence_init(a, b, c, d, e) fence_init(a, b, c, d, e)
#define dma_fence_get(a) fence_get(a)
#define dma_fence_put(a) fence_put(a)
#define dma_fence_signal(a) fence_signal(a)
#define dma_fence_is_signaled(a) fence_is_signaled(a)
#define dma_fence_add_callback(a, b, c) fence_add_callback(a, b, c)
#define dma_fence_remove_callback(a, b) fence_remove_callback(a, b)
#if (KERNEL_VERSION(4, 9, 68) > LINUX_VERSION_CODE)
#define dma_fence_get_status(a) (fence_is_signaled(a) ? (a)->status ?: 1 : 0)
#else
#define dma_fence_get_status(a) (fence_is_signaled(a) ? (a)->error ?: 1 : 0)
#endif
#else
#include <linux/dma-fence.h>
#if (LINUX_VERSION_CODE < KERNEL_VERSION(4, 11, 0))
#define dma_fence_get_status(a) (dma_fence_is_signaled(a) ? \
(a)->status ?: 1 \
: 0)
#endif
#endif /* < 4.10.0 */
#include "dma_buf_lock.h"
/* Maximum number of buffers that a single handle can address */
#define DMA_BUF_LOCK_BUF_MAX 32
#define DMA_BUF_LOCK_DEBUG 1
#define DMA_BUF_LOCK_INIT_BIAS 0xFF
static dev_t dma_buf_lock_dev;
static struct cdev dma_buf_lock_cdev;
static struct class *dma_buf_lock_class;
static char dma_buf_lock_dev_name[] = "dma_buf_lock";
#ifdef HAVE_UNLOCKED_IOCTL
static long dma_buf_lock_ioctl(struct file *filp, unsigned int cmd, unsigned long arg);
#else
static int dma_buf_lock_ioctl(struct inode *inode, struct file *filp, unsigned int cmd, unsigned long arg);
#endif
static struct file_operations dma_buf_lock_fops =
{
.owner = THIS_MODULE,
#ifdef HAVE_UNLOCKED_IOCTL
.unlocked_ioctl = dma_buf_lock_ioctl,
#else
.ioctl = dma_buf_lock_ioctl,
#endif
.compat_ioctl = dma_buf_lock_ioctl,
};
typedef struct dma_buf_lock_resource
{
#if (LINUX_VERSION_CODE < KERNEL_VERSION(4, 10, 0))
struct fence fence;
#else
struct dma_fence fence;
#endif
int *list_of_dma_buf_fds; /* List of buffers copied from userspace */
atomic_t locked; /* Status of lock */
struct dma_buf **dma_bufs;
unsigned long exclusive; /* Exclusive access bitmap */
atomic_t fence_dep_count; /* Number of dma-fence dependencies */
struct list_head dma_fence_callbacks; /* list of all callbacks set up to wait on other fences */
wait_queue_head_t wait;
struct kref refcount;
struct list_head link;
struct work_struct work;
int count;
} dma_buf_lock_resource;
/**
* struct dma_buf_lock_fence_cb - Callback data struct for dma-fence
* @fence_cb: Callback function
* @fence: Pointer to the fence object on which this callback is waiting
* @res: Pointer to dma_buf_lock_resource that is waiting on this callback
* @node: List head for linking this callback to the lock resource
*/
struct dma_buf_lock_fence_cb {
#if (LINUX_VERSION_CODE < KERNEL_VERSION(4, 10, 0))
struct fence_cb fence_cb;
struct fence *fence;
#else
struct dma_fence_cb fence_cb;
struct dma_fence *fence;
#endif
struct dma_buf_lock_resource *res;
struct list_head node;
};
static LIST_HEAD(dma_buf_lock_resource_list);
static DEFINE_MUTEX(dma_buf_lock_mutex);
static inline int is_dma_buf_lock_file(struct file *);
static void dma_buf_lock_dounlock(struct kref *ref);
/*** dma_buf_lock fence part ***/
/* Spin lock protecting all Mali fences as fence->lock. */
static DEFINE_SPINLOCK(dma_buf_lock_fence_lock);
static const char *
#if (LINUX_VERSION_CODE < KERNEL_VERSION(4, 10, 0))
dma_buf_lock_fence_get_driver_name(struct fence *fence)
#else
dma_buf_lock_fence_get_driver_name(struct dma_fence *fence)
#endif
{
return "dma_buf_lock";
}
static const char *
#if (LINUX_VERSION_CODE < KERNEL_VERSION(4, 10, 0))
dma_buf_lock_fence_get_timeline_name(struct fence *fence)
#else
dma_buf_lock_fence_get_timeline_name(struct dma_fence *fence)
#endif
{
return "dma_buf_lock.timeline";
}
static bool
#if (LINUX_VERSION_CODE < KERNEL_VERSION(4, 10, 0))
dma_buf_lock_fence_enable_signaling(struct fence *fence)
#else
dma_buf_lock_fence_enable_signaling(struct dma_fence *fence)
#endif
{
return true;
}
#if (LINUX_VERSION_CODE < KERNEL_VERSION(4, 10, 0))
const struct fence_ops dma_buf_lock_fence_ops = {
.wait = fence_default_wait,
#else
const struct dma_fence_ops dma_buf_lock_fence_ops = {
.wait = dma_fence_default_wait,
#endif
.get_driver_name = dma_buf_lock_fence_get_driver_name,
.get_timeline_name = dma_buf_lock_fence_get_timeline_name,
.enable_signaling = dma_buf_lock_fence_enable_signaling,
};
static void
dma_buf_lock_fence_init(dma_buf_lock_resource *resource)
{
dma_fence_init(&resource->fence,
&dma_buf_lock_fence_ops,
&dma_buf_lock_fence_lock,
0,
0);
}
static void
dma_buf_lock_fence_free_callbacks(dma_buf_lock_resource *resource)
{
struct dma_buf_lock_fence_cb *cb, *tmp;
/* Clean up and free callbacks. */
list_for_each_entry_safe(cb, tmp, &resource->dma_fence_callbacks, node) {
/* Cancel callbacks that hasn't been called yet and release the
* reference taken in dma_buf_lock_fence_add_callback().
*/
dma_fence_remove_callback(cb->fence, &cb->fence_cb);
dma_fence_put(cb->fence);
list_del(&cb->node);
kfree(cb);
}
}
static void
dma_buf_lock_fence_work(struct work_struct *pwork)
{
dma_buf_lock_resource *resource =
container_of(pwork, dma_buf_lock_resource, work);
WARN_ON(atomic_read(&resource->fence_dep_count));
WARN_ON(!atomic_read(&resource->locked));
WARN_ON(!resource->exclusive);
mutex_lock(&dma_buf_lock_mutex);
kref_put(&resource->refcount, dma_buf_lock_dounlock);
mutex_unlock(&dma_buf_lock_mutex);
}
static void
#if (LINUX_VERSION_CODE < KERNEL_VERSION(4, 10, 0))
dma_buf_lock_fence_callback(struct fence *fence, struct fence_cb *cb)
#else
dma_buf_lock_fence_callback(struct dma_fence *fence, struct dma_fence_cb *cb)
#endif
{
struct dma_buf_lock_fence_cb *dma_buf_lock_cb = container_of(cb,
struct dma_buf_lock_fence_cb,
fence_cb);
dma_buf_lock_resource *resource = dma_buf_lock_cb->res;
#if DMA_BUF_LOCK_DEBUG
printk(KERN_DEBUG "dma_buf_lock_fence_callback\n");
#endif
/* Callback function will be invoked in atomic context. */
if (atomic_dec_and_test(&resource->fence_dep_count)) {
atomic_set(&resource->locked, 1);
wake_up(&resource->wait);
if (resource->exclusive) {
/* Warn if the work was already queued */
WARN_ON(!schedule_work(&resource->work));
}
}
}
#if (LINUX_VERSION_CODE < KERNEL_VERSION(4, 10, 0))
static int
dma_buf_lock_fence_add_callback(dma_buf_lock_resource *resource,
struct fence *fence,
fence_func_t callback)
#else
static int
dma_buf_lock_fence_add_callback(dma_buf_lock_resource *resource,
struct dma_fence *fence,
dma_fence_func_t callback)
#endif
{
int err = 0;
struct dma_buf_lock_fence_cb *fence_cb;
if (!fence)
return -EINVAL;
fence_cb = kmalloc(sizeof(*fence_cb), GFP_KERNEL);
if (!fence_cb)
return -ENOMEM;
fence_cb->fence = fence;
fence_cb->res = resource;
INIT_LIST_HEAD(&fence_cb->node);
err = dma_fence_add_callback(fence, &fence_cb->fence_cb,
callback);
if (err == -ENOENT) {
/* Fence signaled, get the completion result */
err = dma_fence_get_status(fence);
/* remap success completion to err code */
if (err == 1)
err = 0;
kfree(fence_cb);
} else if (err) {
kfree(fence_cb);
} else {
/*
* Get reference to fence that will be kept until callback gets
* cleaned up in dma_buf_lock_fence_free_callbacks().
*/
dma_fence_get(fence);
atomic_inc(&resource->fence_dep_count);
/* Add callback to resource's list of callbacks */
list_add(&fence_cb->node, &resource->dma_fence_callbacks);
}
return err;
}
static int
dma_buf_lock_add_fence_reservation_callback(dma_buf_lock_resource *resource,
struct reservation_object *resv,
bool exclusive)
{
#if (LINUX_VERSION_CODE < KERNEL_VERSION(4, 10, 0))
struct fence *excl_fence = NULL;
struct fence **shared_fences = NULL;
#else
struct dma_fence *excl_fence = NULL;
struct dma_fence **shared_fences = NULL;
#endif
unsigned int shared_count = 0;
int err, i;
err = reservation_object_get_fences_rcu(resv,
&excl_fence,
&shared_count,
&shared_fences);
if (err)
return err;
if (excl_fence) {
err = dma_buf_lock_fence_add_callback(resource,
excl_fence,
dma_buf_lock_fence_callback);
/* Release our reference, taken by reservation_object_get_fences_rcu(),
* to the fence. We have set up our callback (if that was possible),
* and it's the fence's owner is responsible for singling the fence
* before allowing it to disappear.
*/
dma_fence_put(excl_fence);
if (err)
goto out;
}
if (exclusive) {
for (i = 0; i < shared_count; i++) {
err = dma_buf_lock_fence_add_callback(resource,
shared_fences[i],
dma_buf_lock_fence_callback);
if (err)
goto out;
}
}
/* Release all our references to the shared fences, taken by
* reservation_object_get_fences_rcu(). We have set up our callback (if
* that was possible), and it's the fence's owner is responsible for
* signaling the fence before allowing it to disappear.
*/
out:
for (i = 0; i < shared_count; i++)
dma_fence_put(shared_fences[i]);
kfree(shared_fences);
return err;
}
static void
dma_buf_lock_release_fence_reservation(dma_buf_lock_resource *resource,
struct ww_acquire_ctx *ctx)
{
unsigned int r;
for (r = 0; r < resource->count; r++)
ww_mutex_unlock(&resource->dma_bufs[r]->resv->lock);
ww_acquire_fini(ctx);
}
static int
dma_buf_lock_acquire_fence_reservation(dma_buf_lock_resource *resource,
struct ww_acquire_ctx *ctx)
{
struct reservation_object *content_resv = NULL;
unsigned int content_resv_idx = 0;
unsigned int r;
int err = 0;
ww_acquire_init(ctx, &reservation_ww_class);
retry:
for (r = 0; r < resource->count; r++) {
if (resource->dma_bufs[r]->resv == content_resv) {
content_resv = NULL;
continue;
}
err = ww_mutex_lock(&resource->dma_bufs[r]->resv->lock, ctx);
if (err)
goto error;
}
ww_acquire_done(ctx);
return err;
error:
content_resv_idx = r;
/* Unlock the locked one ones */
while (r--)
ww_mutex_unlock(&resource->dma_bufs[r]->resv->lock);
if (content_resv)
ww_mutex_unlock(&content_resv->lock);
/* If we deadlock try with lock_slow and retry */
if (err == -EDEADLK) {
#if DMA_BUF_LOCK_DEBUG
printk(KERN_DEBUG "deadlock at dma_buf fd %i\n",
resource->list_of_dma_buf_fds[content_resv_idx]);
#endif
content_resv = resource->dma_bufs[content_resv_idx]->resv;
ww_mutex_lock_slow(&content_resv->lock, ctx);
goto retry;
}
/* If we are here the function failed */
ww_acquire_fini(ctx);
return err;
}
static int dma_buf_lock_handle_release(struct inode *inode, struct file *file)
{
dma_buf_lock_resource *resource;
if (!is_dma_buf_lock_file(file))
return -EINVAL;
resource = file->private_data;
#if DMA_BUF_LOCK_DEBUG
printk("dma_buf_lock_handle_release\n");
#endif
mutex_lock(&dma_buf_lock_mutex);
kref_put(&resource->refcount, dma_buf_lock_dounlock);
mutex_unlock(&dma_buf_lock_mutex);
return 0;
}
static unsigned int dma_buf_lock_handle_poll(struct file *file,
struct poll_table_struct *wait)
{
dma_buf_lock_resource *resource;
unsigned int ret = 0;
if (!is_dma_buf_lock_file(file))
return POLLERR;
resource = file->private_data;
#if DMA_BUF_LOCK_DEBUG
printk("dma_buf_lock_handle_poll\n");
#endif
if (1 == atomic_read(&resource->locked))
{
/* Resources have been locked */
ret = POLLIN | POLLRDNORM;
if (resource->exclusive)
{
ret |= POLLOUT | POLLWRNORM;
}
}
else
{
if (!poll_does_not_wait(wait))
{
poll_wait(file, &resource->wait, wait);
}
}
#if DMA_BUF_LOCK_DEBUG
printk("dma_buf_lock_handle_poll : return %i\n", ret);
#endif
return ret;
}
static const struct file_operations dma_buf_lock_handle_fops = {
.owner = THIS_MODULE,
.release = dma_buf_lock_handle_release,
.poll = dma_buf_lock_handle_poll,
};
/*
* is_dma_buf_lock_file - Check if struct file* is associated with dma_buf_lock
*/
static inline int is_dma_buf_lock_file(struct file *file)
{
return file->f_op == &dma_buf_lock_handle_fops;
}
/*
* Start requested lock.
*
* Allocates required memory, copies dma_buf_fd list from userspace,
* acquires related reservation objects, and starts the lock.
*/
static int dma_buf_lock_dolock(dma_buf_lock_k_request *request)
{
dma_buf_lock_resource *resource;
struct ww_acquire_ctx ww_ctx;
int size;
int fd;
int i;
int ret;
if (NULL == request->list_of_dma_buf_fds)
{
return -EINVAL;
}
if (request->count <= 0)
{
return -EINVAL;
}
if (request->count > DMA_BUF_LOCK_BUF_MAX)
{
return -EINVAL;
}
if (request->exclusive != DMA_BUF_LOCK_NONEXCLUSIVE &&
request->exclusive != DMA_BUF_LOCK_EXCLUSIVE)
{
return -EINVAL;
}
resource = kzalloc(sizeof(dma_buf_lock_resource), GFP_KERNEL);
if (NULL == resource)
{
return -ENOMEM;
}
atomic_set(&resource->locked, 0);
kref_init(&resource->refcount);
INIT_LIST_HEAD(&resource->link);
INIT_WORK(&resource->work, dma_buf_lock_fence_work);
resource->count = request->count;
/* Allocate space to store dma_buf_fds received from user space */
size = request->count * sizeof(int);
resource->list_of_dma_buf_fds = kmalloc(size, GFP_KERNEL);
if (NULL == resource->list_of_dma_buf_fds)
{
kfree(resource);
return -ENOMEM;
}
/* Allocate space to store dma_buf pointers associated with dma_buf_fds */
size = sizeof(struct dma_buf *) * request->count;
resource->dma_bufs = kmalloc(size, GFP_KERNEL);
if (NULL == resource->dma_bufs)
{
kfree(resource->list_of_dma_buf_fds);
kfree(resource);
return -ENOMEM;
}
/* Copy requested list of dma_buf_fds from user space */
size = request->count * sizeof(int);
if (0 != copy_from_user(resource->list_of_dma_buf_fds, (void __user *)request->list_of_dma_buf_fds, size))
{
kfree(resource->list_of_dma_buf_fds);
kfree(resource->dma_bufs);
kfree(resource);
return -ENOMEM;
}
#if DMA_BUF_LOCK_DEBUG
for (i = 0; i < request->count; i++)
{
printk("dma_buf %i = %X\n", i, resource->list_of_dma_buf_fds[i]);
}
#endif
/* Initialize the fence associated with dma_buf_lock resource */
dma_buf_lock_fence_init(resource);
INIT_LIST_HEAD(&resource->dma_fence_callbacks);
atomic_set(&resource->fence_dep_count, DMA_BUF_LOCK_INIT_BIAS);
/* Add resource to global list */
mutex_lock(&dma_buf_lock_mutex);
list_add(&resource->link, &dma_buf_lock_resource_list);
mutex_unlock(&dma_buf_lock_mutex);
for (i = 0; i < request->count; i++)
{
/* Convert fd into dma_buf structure */
resource->dma_bufs[i] = dma_buf_get(resource->list_of_dma_buf_fds[i]);
if (IS_ERR_VALUE(PTR_ERR(resource->dma_bufs[i])))
{
mutex_lock(&dma_buf_lock_mutex);
kref_put(&resource->refcount, dma_buf_lock_dounlock);
mutex_unlock(&dma_buf_lock_mutex);
return -EINVAL;
}
/*Check the reservation object associated with dma_buf */
if (NULL == resource->dma_bufs[i]->resv)
{
mutex_lock(&dma_buf_lock_mutex);
kref_put(&resource->refcount, dma_buf_lock_dounlock);
mutex_unlock(&dma_buf_lock_mutex);
return -EINVAL;
}
#if DMA_BUF_LOCK_DEBUG
printk(KERN_DEBUG "dma_buf_lock_dolock : dma_buf_fd %i dma_buf %p dma_fence reservation %p\n",
resource->list_of_dma_buf_fds[i], resource->dma_bufs[i], resource->dma_bufs[i]->resv);
#endif
}
init_waitqueue_head(&resource->wait);
kref_get(&resource->refcount);
/* Create file descriptor associated with lock request */
fd = anon_inode_getfd("dma_buf_lock", &dma_buf_lock_handle_fops,
(void *)resource, 0);
if (fd < 0)
{
mutex_lock(&dma_buf_lock_mutex);
kref_put(&resource->refcount, dma_buf_lock_dounlock);
kref_put(&resource->refcount, dma_buf_lock_dounlock);
mutex_unlock(&dma_buf_lock_mutex);
return fd;
}
resource->exclusive = request->exclusive;
/* Start locking process */
ret = dma_buf_lock_acquire_fence_reservation(resource, &ww_ctx);
if (ret) {
#if DMA_BUF_LOCK_DEBUG
printk(KERN_DEBUG "dma_buf_lock_dolock : Error %d locking reservations.\n", ret);
#endif
put_unused_fd(fd);
mutex_lock(&dma_buf_lock_mutex);
kref_put(&resource->refcount, dma_buf_lock_dounlock);
kref_put(&resource->refcount, dma_buf_lock_dounlock);
mutex_unlock(&dma_buf_lock_mutex);
return ret;
}
/* Take an extra reference for exclusive access, which will be dropped
* once the pre-existing fences attached to dma-buf resources, for which
* we have commited for exclusive access, are signaled.
* At a given time there can be only one exclusive fence attached to a
* reservation object, so the new exclusive fence replaces the original
* fence and the future sync is done against the new fence which is
* supposed to be signaled only after the original fence was signaled.
* If the new exclusive fence is signaled prematurely then the resources
* would become available for new access while they are already being
* written to by the original owner.
*/
if (resource->exclusive)
kref_get(&resource->refcount);
for (i = 0; i < request->count; i++) {
struct reservation_object *resv = resource->dma_bufs[i]->resv;
if (!test_bit(i, &resource->exclusive)) {
ret = reservation_object_reserve_shared(resv);
if (ret) {
#if DMA_BUF_LOCK_DEBUG
printk(KERN_DEBUG "dma_buf_lock_dolock : Error %d reserving space for shared fence.\n", ret);
#endif
break;
}
ret = dma_buf_lock_add_fence_reservation_callback(resource,
resv,
false);
if (ret) {
#if DMA_BUF_LOCK_DEBUG
printk(KERN_DEBUG "dma_buf_lock_dolock : Error %d adding reservation to callback.\n", ret);
#endif
break;
}
reservation_object_add_shared_fence(resv, &resource->fence);
} else {
ret = dma_buf_lock_add_fence_reservation_callback(resource,
resv,
true);
if (ret) {
#if DMA_BUF_LOCK_DEBUG
printk(KERN_DEBUG "dma_buf_lock_dolock : Error %d adding reservation to callback.\n", ret);
#endif
break;
}
reservation_object_add_excl_fence(resv, &resource->fence);
}
}
dma_buf_lock_release_fence_reservation(resource, &ww_ctx);
/* Test if the callbacks were already triggered */
if (!atomic_sub_return(DMA_BUF_LOCK_INIT_BIAS, &resource->fence_dep_count)) {
atomic_set(&resource->locked, 1);
/* Drop the extra reference taken for exclusive access */
if (resource->exclusive)
dma_buf_lock_fence_work(&resource->work);
}
if (IS_ERR_VALUE((unsigned long)ret))
{
put_unused_fd(fd);
mutex_lock(&dma_buf_lock_mutex);
kref_put(&resource->refcount, dma_buf_lock_dounlock);
kref_put(&resource->refcount, dma_buf_lock_dounlock);
mutex_unlock(&dma_buf_lock_mutex);
return ret;
}
#if DMA_BUF_LOCK_DEBUG
printk("dma_buf_lock_dolock : complete\n");
#endif
mutex_lock(&dma_buf_lock_mutex);
kref_put(&resource->refcount, dma_buf_lock_dounlock);
mutex_unlock(&dma_buf_lock_mutex);
return fd;
}
static void dma_buf_lock_dounlock(struct kref *ref)
{
int i;
dma_buf_lock_resource *resource = container_of(ref, dma_buf_lock_resource, refcount);
atomic_set(&resource->locked, 0);
/* Signal the resource's fence. */
dma_fence_signal(&resource->fence);
dma_buf_lock_fence_free_callbacks(resource);
list_del(&resource->link);
for (i = 0; i < resource->count; i++)
{
if (resource->dma_bufs[i])
dma_buf_put(resource->dma_bufs[i]);
}
kfree(resource->dma_bufs);
kfree(resource->list_of_dma_buf_fds);
dma_fence_put(&resource->fence);
}
static int __init dma_buf_lock_init(void)
{
int err;
#if DMA_BUF_LOCK_DEBUG
printk("dma_buf_lock_init\n");
#endif
err = alloc_chrdev_region(&dma_buf_lock_dev, 0, 1, dma_buf_lock_dev_name);
if (0 == err)
{
cdev_init(&dma_buf_lock_cdev, &dma_buf_lock_fops);
err = cdev_add(&dma_buf_lock_cdev, dma_buf_lock_dev, 1);
if (0 == err)
{
dma_buf_lock_class = class_create(THIS_MODULE, dma_buf_lock_dev_name);
if (IS_ERR(dma_buf_lock_class))
{
err = PTR_ERR(dma_buf_lock_class);
}
else
{
struct device *mdev;
mdev = device_create(dma_buf_lock_class, NULL, dma_buf_lock_dev, NULL, dma_buf_lock_dev_name);
if (!IS_ERR(mdev))
{
return 0;
}
err = PTR_ERR(mdev);
class_destroy(dma_buf_lock_class);
}
cdev_del(&dma_buf_lock_cdev);
}
unregister_chrdev_region(dma_buf_lock_dev, 1);
}
#if DMA_BUF_LOCK_DEBUG
printk("dma_buf_lock_init failed\n");
#endif
return err;
}
static void __exit dma_buf_lock_exit(void)
{
#if DMA_BUF_LOCK_DEBUG
printk("dma_buf_lock_exit\n");
#endif
/* Unlock all outstanding references */
while (1)
{
mutex_lock(&dma_buf_lock_mutex);
if (list_empty(&dma_buf_lock_resource_list))
{
mutex_unlock(&dma_buf_lock_mutex);
break;
}
else
{
dma_buf_lock_resource *resource = list_entry(dma_buf_lock_resource_list.next,
dma_buf_lock_resource, link);
kref_put(&resource->refcount, dma_buf_lock_dounlock);
mutex_unlock(&dma_buf_lock_mutex);
}
}
device_destroy(dma_buf_lock_class, dma_buf_lock_dev);
class_destroy(dma_buf_lock_class);
cdev_del(&dma_buf_lock_cdev);
unregister_chrdev_region(dma_buf_lock_dev, 1);
}
#ifdef HAVE_UNLOCKED_IOCTL
static long dma_buf_lock_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
#else
static int dma_buf_lock_ioctl(struct inode *inode, struct file *filp, unsigned int cmd, unsigned long arg)
#endif
{
dma_buf_lock_k_request request;
int size = _IOC_SIZE(cmd);
if (_IOC_TYPE(cmd) != DMA_BUF_LOCK_IOC_MAGIC)
{
return -ENOTTY;
}
if ((_IOC_NR(cmd) < DMA_BUF_LOCK_IOC_MINNR) || (_IOC_NR(cmd) > DMA_BUF_LOCK_IOC_MAXNR))
{
return -ENOTTY;
}
switch (cmd)
{
case DMA_BUF_LOCK_FUNC_LOCK_ASYNC:
if (size != sizeof(dma_buf_lock_k_request))
{
return -ENOTTY;
}
if (copy_from_user(&request, (void __user *)arg, size))
{
return -EFAULT;
}
#if DMA_BUF_LOCK_DEBUG
printk("DMA_BUF_LOCK_FUNC_LOCK_ASYNC - %i\n", request.count);
#endif
return dma_buf_lock_dolock(&request);
}
return -ENOTTY;
}
module_init(dma_buf_lock_init);
module_exit(dma_buf_lock_exit);
MODULE_LICENSE("GPL");
@@ -0,0 +1,47 @@
/*
*
* (C) COPYRIGHT 2012 ARM Limited. All rights reserved.
*
* This program is free software and is provided to you under the terms of the
* GNU General Public License version 2 as published by the Free Software
* Foundation, and any use by you of this program is subject to the terms
* of such GNU licence.
*
* 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 the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, you can access it online at
* http://www.gnu.org/licenses/gpl-2.0.html.
*
* SPDX-License-Identifier: GPL-2.0
*
*/
#ifndef _DMA_BUF_LOCK_H
#define _DMA_BUF_LOCK_H
typedef enum dma_buf_lock_exclusive
{
DMA_BUF_LOCK_NONEXCLUSIVE = 0,
DMA_BUF_LOCK_EXCLUSIVE = -1
} dma_buf_lock_exclusive;
typedef struct dma_buf_lock_k_request
{
int count;
int *list_of_dma_buf_fds;
int timeout;
dma_buf_lock_exclusive exclusive;
} dma_buf_lock_k_request;
#define DMA_BUF_LOCK_IOC_MAGIC '~'
#define DMA_BUF_LOCK_FUNC_LOCK_ASYNC _IOW(DMA_BUF_LOCK_IOC_MAGIC, 11, dma_buf_lock_k_request)
#define DMA_BUF_LOCK_IOC_MINNR 11
#define DMA_BUF_LOCK_IOC_MAXNR 11
#endif /* _DMA_BUF_LOCK_H */
@@ -0,0 +1,24 @@
#
# (C) COPYRIGHT 2012 ARM Limited. All rights reserved.
#
# This program is free software and is provided to you under the terms of the
# GNU General Public License version 2 as published by the Free Software
# Foundation, and any use by you of this program is subject to the terms
# of such GNU licence.
#
# 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 the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, you can access it online at
# http://www.gnu.org/licenses/gpl-2.0.html.
#
# SPDX-License-Identifier: GPL-2.0
#
#
ifneq ($(CONFIG_DMA_SHARED_BUFFER),)
obj-$(CONFIG_DMA_SHARED_BUFFER_TEST_EXPORTER) += dma-buf-test-exporter.o
endif
@@ -0,0 +1,26 @@
#
# (C) COPYRIGHT 2012 ARM Limited. All rights reserved.
#
# This program is free software and is provided to you under the terms of the
# GNU General Public License version 2 as published by the Free Software
# Foundation, and any use by you of this program is subject to the terms
# of such GNU licence.
#
# 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 the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, you can access it online at
# http://www.gnu.org/licenses/gpl-2.0.html.
#
# SPDX-License-Identifier: GPL-2.0
#
#
config DMA_SHARED_BUFFER_TEST_EXPORTER
tristate "Test exporter for the dma-buf framework"
depends on DMA_SHARED_BUFFER
help
This option enables the test exporter usable to help test importerts.
@@ -0,0 +1,36 @@
#
# (C) COPYRIGHT 2011-2013 ARM Limited. All rights reserved.
#
# This program is free software and is provided to you under the terms of the
# GNU General Public License version 2 as published by the Free Software
# Foundation, and any use by you of this program is subject to the terms
# of such GNU licence.
#
# 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 the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, you can access it online at
# http://www.gnu.org/licenses/gpl-2.0.html.
#
# SPDX-License-Identifier: GPL-2.0
#
#
# linux build system bootstrap for out-of-tree module
# default to building for the host
ARCH ?= $(shell uname -m)
ifeq ($(KDIR),)
$(error Must specify KDIR to point to the kernel to target))
endif
all:
$(MAKE) ARCH=$(ARCH) -C $(KDIR) M=$(CURDIR) EXTRA_CFLAGS="-I$(CURDIR)/../../../include" CONFIG_DMA_SHARED_BUFFER_TEST_EXPORTER=m
clean:
$(MAKE) ARCH=$(ARCH) -C $(KDIR) M=$(CURDIR) clean
@@ -0,0 +1,26 @@
/*
*
* (C) COPYRIGHT 2017, 2020 ARM Limited. All rights reserved.
*
* This program is free software and is provided to you under the terms of the
* GNU General Public License version 2 as published by the Free Software
* Foundation, and any use by you of this program is subject to the terms
* of such GNU licence.
*
* A copy of the licence is included with the program, and can also be obtained
* from Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*
*/
bob_kernel_module {
name: "dma-buf-test-exporter",
srcs: [
"Kbuild",
"dma-buf-test-exporter.c",
],
kbuild_options: [
"CONFIG_DMA_SHARED_BUFFER_TEST_EXPORTER=m",
],
defaults: ["kernel_defaults"],
}
@@ -0,0 +1,844 @@
/*
*
* (C) COPYRIGHT 2012-2020 ARM Limited. All rights reserved.
*
* This program is free software and is provided to you under the terms of the
* GNU General Public License version 2 as published by the Free Software
* Foundation, and any use by you of this program is subject to the terms
* of such GNU licence.
*
* 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 the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, you can access it online at
* http://www.gnu.org/licenses/gpl-2.0.html.
*
* SPDX-License-Identifier: GPL-2.0
*
*/
#include <linux/dma-buf-test-exporter.h>
#include <linux/dma-buf.h>
#include <linux/miscdevice.h>
#include <linux/slab.h>
#include <linux/uaccess.h>
#include <linux/version.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/atomic.h>
#include <linux/mm.h>
#include <linux/highmem.h>
#if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 5, 0))
#if (LINUX_VERSION_CODE < KERNEL_VERSION(4, 8, 0))
#include <linux/dma-attrs.h>
#endif
#include <linux/dma-mapping.h>
#endif
/* Maximum size allowed in a single DMA_BUF_TE_ALLOC call */
#define DMA_BUF_TE_ALLOC_MAX_SIZE ((8ull << 30) >> PAGE_SHIFT) /* 8 GB */
/* Since kernel version 5.0 CONFIG_ARCH_NO_SG_CHAIN replaced CONFIG_ARCH_HAS_SG_CHAIN */
#if LINUX_VERSION_CODE < KERNEL_VERSION(5, 0, 0)
#if (!defined(ARCH_HAS_SG_CHAIN) && !defined(CONFIG_ARCH_HAS_SG_CHAIN))
#define NO_SG_CHAIN
#endif
#elif defined(CONFIG_ARCH_NO_SG_CHAIN)
#define NO_SG_CHAIN
#endif
struct dma_buf_te_alloc {
/* the real alloc */
size_t nr_pages;
struct page **pages;
/* the debug usage tracking */
int nr_attached_devices;
int nr_device_mappings;
int nr_cpu_mappings;
/* failure simulation */
int fail_attach;
int fail_map;
int fail_mmap;
bool contiguous;
dma_addr_t contig_dma_addr;
void *contig_cpu_addr;
};
struct dma_buf_te_attachment {
struct sg_table *sg;
bool attachment_mapped;
};
static struct miscdevice te_device;
#if (LINUX_VERSION_CODE < KERNEL_VERSION(4, 19, 0))
static int dma_buf_te_attach(struct dma_buf *buf, struct device *dev, struct dma_buf_attachment *attachment)
#else
static int dma_buf_te_attach(struct dma_buf *buf, struct dma_buf_attachment *attachment)
#endif
{
struct dma_buf_te_alloc *alloc;
alloc = buf->priv;
if (alloc->fail_attach)
return -EFAULT;
attachment->priv = kzalloc(sizeof(struct dma_buf_te_attachment), GFP_KERNEL);
if (!attachment->priv)
return -ENOMEM;
/* dma_buf is externally locked during call */
alloc->nr_attached_devices++;
return 0;
}
static void dma_buf_te_detach(struct dma_buf *buf, struct dma_buf_attachment *attachment)
{
struct dma_buf_te_alloc *alloc = buf->priv;
struct dma_buf_te_attachment *pa = attachment->priv;
/* dma_buf is externally locked during call */
WARN(pa->attachment_mapped, "WARNING: dma-buf-test-exporter detected detach with open device mappings");
alloc->nr_attached_devices--;
kfree(pa);
}
static struct sg_table *dma_buf_te_map(struct dma_buf_attachment *attachment, enum dma_data_direction direction)
{
struct sg_table *sg;
struct scatterlist *iter;
struct dma_buf_te_alloc *alloc;
struct dma_buf_te_attachment *pa = attachment->priv;
size_t i;
int ret;
alloc = attachment->dmabuf->priv;
if (alloc->fail_map)
return ERR_PTR(-ENOMEM);
if (WARN(pa->attachment_mapped,
"WARNING: Attempted to map already mapped attachment."))
return ERR_PTR(-EBUSY);
#ifdef NO_SG_CHAIN
/* if the ARCH can't chain we can't have allocs larger than a single sg can hold */
if (alloc->nr_pages > SG_MAX_SINGLE_ALLOC)
return ERR_PTR(-EINVAL);
#endif /* NO_SG_CHAIN */
sg = kmalloc(sizeof(struct sg_table), GFP_KERNEL);
if (!sg)
return ERR_PTR(-ENOMEM);
/* from here we access the allocation object, so lock the dmabuf pointing to it */
mutex_lock(&attachment->dmabuf->lock);
if (alloc->contiguous)
ret = sg_alloc_table(sg, 1, GFP_KERNEL);
else
ret = sg_alloc_table(sg, alloc->nr_pages, GFP_KERNEL);
if (ret) {
mutex_unlock(&attachment->dmabuf->lock);
kfree(sg);
return ERR_PTR(ret);
}
if (alloc->contiguous) {
sg_dma_len(sg->sgl) = alloc->nr_pages * PAGE_SIZE;
sg_set_page(sg->sgl, pfn_to_page(PFN_DOWN(alloc->contig_dma_addr)), alloc->nr_pages * PAGE_SIZE, 0);
sg_dma_address(sg->sgl) = alloc->contig_dma_addr;
} else {
for_each_sg(sg->sgl, iter, alloc->nr_pages, i)
sg_set_page(iter, alloc->pages[i], PAGE_SIZE, 0);
}
if (!dma_map_sg(attachment->dev, sg->sgl, sg->nents, direction)) {
mutex_unlock(&attachment->dmabuf->lock);
sg_free_table(sg);
kfree(sg);
return ERR_PTR(-ENOMEM);
}
alloc->nr_device_mappings++;
pa->attachment_mapped = true;
pa->sg = sg;
mutex_unlock(&attachment->dmabuf->lock);
return sg;
}
static void dma_buf_te_unmap(struct dma_buf_attachment *attachment,
struct sg_table *sg, enum dma_data_direction direction)
{
struct dma_buf_te_alloc *alloc;
struct dma_buf_te_attachment *pa = attachment->priv;
alloc = attachment->dmabuf->priv;
mutex_lock(&attachment->dmabuf->lock);
WARN(!pa->attachment_mapped, "WARNING: Unmatched unmap of attachment.");
alloc->nr_device_mappings--;
pa->attachment_mapped = false;
pa->sg = NULL;
mutex_unlock(&attachment->dmabuf->lock);
dma_unmap_sg(attachment->dev, sg->sgl, sg->nents, direction);
sg_free_table(sg);
kfree(sg);
}
static void dma_buf_te_release(struct dma_buf *buf)
{
size_t i;
struct dma_buf_te_alloc *alloc;
alloc = buf->priv;
/* no need for locking */
if (alloc->contiguous) {
#if (LINUX_VERSION_CODE >= KERNEL_VERSION(4, 8, 0))
dma_free_attrs(te_device.this_device,
alloc->nr_pages * PAGE_SIZE,
alloc->contig_cpu_addr,
alloc->contig_dma_addr,
DMA_ATTR_WRITE_COMBINE);
#elif (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 5, 0))
DEFINE_DMA_ATTRS(attrs);
dma_set_attr(DMA_ATTR_WRITE_COMBINE, &attrs);
dma_free_attrs(te_device.this_device,
alloc->nr_pages * PAGE_SIZE,
alloc->contig_cpu_addr, alloc->contig_dma_addr, &attrs);
#else
dma_free_writecombine(te_device.this_device,
alloc->nr_pages * PAGE_SIZE,
alloc->contig_cpu_addr, alloc->contig_dma_addr);
#endif
} else {
for (i = 0; i < alloc->nr_pages; i++)
__free_page(alloc->pages[i]);
}
kfree(alloc->pages);
kfree(alloc);
}
static int dma_buf_te_sync(struct dma_buf *dmabuf,
enum dma_data_direction direction,
bool start_cpu_access)
{
struct dma_buf_attachment *attachment;
mutex_lock(&dmabuf->lock);
list_for_each_entry(attachment, &dmabuf->attachments, node) {
struct dma_buf_te_attachment *pa = attachment->priv;
struct sg_table *sg = pa->sg;
if (!sg) {
dev_dbg(te_device.this_device, "no mapping for device %s\n", dev_name(attachment->dev));
continue;
}
if (start_cpu_access) {
dev_dbg(te_device.this_device, "sync cpu with device %s\n", dev_name(attachment->dev));
dma_sync_sg_for_cpu(attachment->dev, sg->sgl, sg->nents, direction);
} else {
dev_dbg(te_device.this_device, "sync device %s with cpu\n", dev_name(attachment->dev));
dma_sync_sg_for_device(attachment->dev, sg->sgl, sg->nents, direction);
}
}
mutex_unlock(&dmabuf->lock);
return 0;
}
#if (KERNEL_VERSION(4, 6, 0) <= LINUX_VERSION_CODE)
static int dma_buf_te_begin_cpu_access(struct dma_buf *dmabuf,
enum dma_data_direction direction)
#else
static int dma_buf_te_begin_cpu_access(struct dma_buf *dmabuf, size_t start,
size_t len,
enum dma_data_direction direction)
#endif
{
return dma_buf_te_sync(dmabuf, direction, true);
}
#if (KERNEL_VERSION(4, 6, 0) <= LINUX_VERSION_CODE)
static int dma_buf_te_end_cpu_access(struct dma_buf *dmabuf,
enum dma_data_direction direction)
{
return dma_buf_te_sync(dmabuf, direction, false);
}
#else
static void dma_buf_te_end_cpu_access(struct dma_buf *dmabuf, size_t start,
size_t len,
enum dma_data_direction direction)
{
dma_buf_te_sync(dmabuf, direction, false);
}
#endif
static void dma_buf_te_mmap_open(struct vm_area_struct *vma)
{
struct dma_buf *dma_buf;
struct dma_buf_te_alloc *alloc;
dma_buf = vma->vm_private_data;
alloc = dma_buf->priv;
mutex_lock(&dma_buf->lock);
alloc->nr_cpu_mappings++;
mutex_unlock(&dma_buf->lock);
}
static void dma_buf_te_mmap_close(struct vm_area_struct *vma)
{
struct dma_buf *dma_buf;
struct dma_buf_te_alloc *alloc;
dma_buf = vma->vm_private_data;
alloc = dma_buf->priv;
BUG_ON(alloc->nr_cpu_mappings <= 0);
mutex_lock(&dma_buf->lock);
alloc->nr_cpu_mappings--;
mutex_unlock(&dma_buf->lock);
}
#if LINUX_VERSION_CODE < KERNEL_VERSION(4, 11, 0)
static int dma_buf_te_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
#elif KERNEL_VERSION(5, 1, 0) > LINUX_VERSION_CODE
static int dma_buf_te_mmap_fault(struct vm_fault *vmf)
#else
static vm_fault_t dma_buf_te_mmap_fault(struct vm_fault *vmf)
#endif
{
struct dma_buf_te_alloc *alloc;
struct dma_buf *dmabuf;
struct page *pageptr;
#if LINUX_VERSION_CODE < KERNEL_VERSION(4, 11, 0)
dmabuf = vma->vm_private_data;
#else
dmabuf = vmf->vma->vm_private_data;
#endif
alloc = dmabuf->priv;
if (vmf->pgoff > alloc->nr_pages)
return VM_FAULT_SIGBUS;
pageptr = alloc->pages[vmf->pgoff];
BUG_ON(!pageptr);
get_page(pageptr);
vmf->page = pageptr;
return 0;
}
struct vm_operations_struct dma_buf_te_vm_ops = {
.open = dma_buf_te_mmap_open,
.close = dma_buf_te_mmap_close,
.fault = dma_buf_te_mmap_fault
};
static int dma_buf_te_mmap(struct dma_buf *dmabuf, struct vm_area_struct *vma)
{
struct dma_buf_te_alloc *alloc;
alloc = dmabuf->priv;
if (alloc->fail_mmap)
return -ENOMEM;
#if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 7, 0))
vma->vm_flags |= VM_IO | VM_DONTEXPAND | VM_DONTDUMP;
#else
vma->vm_flags |= VM_RESERVED | VM_IO | VM_DONTEXPAND;
#endif
vma->vm_ops = &dma_buf_te_vm_ops;
vma->vm_private_data = dmabuf;
/* we fault in the pages on access */
/* call open to do the ref-counting */
dma_buf_te_vm_ops.open(vma);
return 0;
}
#if LINUX_VERSION_CODE < KERNEL_VERSION(4, 19, 0)
static void *dma_buf_te_kmap_atomic(struct dma_buf *buf, unsigned long page_num)
{
/* IGNORE */
return NULL;
}
#endif
static void *dma_buf_te_kmap(struct dma_buf *buf, unsigned long page_num)
{
struct dma_buf_te_alloc *alloc;
alloc = buf->priv;
if (page_num >= alloc->nr_pages)
return NULL;
return kmap(alloc->pages[page_num]);
}
static void dma_buf_te_kunmap(struct dma_buf *buf,
unsigned long page_num, void *addr)
{
struct dma_buf_te_alloc *alloc;
alloc = buf->priv;
if (page_num >= alloc->nr_pages)
return;
kunmap(alloc->pages[page_num]);
return;
}
static struct dma_buf_ops dma_buf_te_ops = {
/* real handlers */
.attach = dma_buf_te_attach,
.detach = dma_buf_te_detach,
.map_dma_buf = dma_buf_te_map,
.unmap_dma_buf = dma_buf_te_unmap,
.release = dma_buf_te_release,
.mmap = dma_buf_te_mmap,
.begin_cpu_access = dma_buf_te_begin_cpu_access,
.end_cpu_access = dma_buf_te_end_cpu_access,
#if LINUX_VERSION_CODE < KERNEL_VERSION(4, 12, 0)
.kmap = dma_buf_te_kmap,
.kunmap = dma_buf_te_kunmap,
/* nop handlers for mandatory functions we ignore */
.kmap_atomic = dma_buf_te_kmap_atomic
#else
#if LINUX_VERSION_CODE < KERNEL_VERSION(5, 6, 0)
.map = dma_buf_te_kmap,
.unmap = dma_buf_te_kunmap,
#endif
#if LINUX_VERSION_CODE < KERNEL_VERSION(4, 19, 0)
/* nop handlers for mandatory functions we ignore */
.map_atomic = dma_buf_te_kmap_atomic
#endif
#endif
};
static int do_dma_buf_te_ioctl_version(struct dma_buf_te_ioctl_version __user *buf)
{
struct dma_buf_te_ioctl_version v;
if (copy_from_user(&v, buf, sizeof(v)))
return -EFAULT;
if (v.op != DMA_BUF_TE_ENQ)
return -EFAULT;
v.op = DMA_BUF_TE_ACK;
v.major = DMA_BUF_TE_VER_MAJOR;
v.minor = DMA_BUF_TE_VER_MINOR;
if (copy_to_user(buf, &v, sizeof(v)))
return -EFAULT;
else
return 0;
}
static int do_dma_buf_te_ioctl_alloc(struct dma_buf_te_ioctl_alloc __user *buf, bool contiguous)
{
struct dma_buf_te_ioctl_alloc alloc_req;
struct dma_buf_te_alloc *alloc;
struct dma_buf *dma_buf;
size_t i = 0;
size_t max_nr_pages = DMA_BUF_TE_ALLOC_MAX_SIZE;
int fd;
if (copy_from_user(&alloc_req, buf, sizeof(alloc_req))) {
dev_err(te_device.this_device, "%s: couldn't get user data", __func__);
goto no_input;
}
if (!alloc_req.size) {
dev_err(te_device.this_device, "%s: no size specified", __func__);
goto invalid_size;
}
#ifdef NO_SG_CHAIN
/* Whilst it is possible to allocate larger buffer, we won't be able to
* map it during actual usage (mmap() still succeeds). We fail here so
* userspace code can deal with it early than having driver failure
* later on. */
if (max_nr_pages > SG_MAX_SINGLE_ALLOC)
max_nr_pages = SG_MAX_SINGLE_ALLOC;
#endif /* NO_SG_CHAIN */
if (alloc_req.size > max_nr_pages) {
dev_err(te_device.this_device, "%s: buffer size of %llu pages exceeded the mapping limit of %zu pages",
__func__, alloc_req.size, max_nr_pages);
goto invalid_size;
}
alloc = kzalloc(sizeof(struct dma_buf_te_alloc), GFP_KERNEL);
if (NULL == alloc) {
dev_err(te_device.this_device, "%s: couldn't alloc object", __func__);
goto no_alloc_object;
}
alloc->nr_pages = alloc_req.size;
alloc->contiguous = contiguous;
alloc->pages = kzalloc(sizeof(struct page *) * alloc->nr_pages, GFP_KERNEL);
if (!alloc->pages) {
dev_err(te_device.this_device,
"%s: couldn't alloc %zu page structures",
__func__, alloc->nr_pages);
goto free_alloc_object;
}
if (contiguous) {
dma_addr_t dma_aux;
#if (LINUX_VERSION_CODE >= KERNEL_VERSION(4, 8, 0))
alloc->contig_cpu_addr = dma_alloc_attrs(te_device.this_device,
alloc->nr_pages * PAGE_SIZE,
&alloc->contig_dma_addr,
GFP_KERNEL | __GFP_ZERO,
DMA_ATTR_WRITE_COMBINE);
#elif (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 5, 0))
DEFINE_DMA_ATTRS(attrs);
dma_set_attr(DMA_ATTR_WRITE_COMBINE, &attrs);
alloc->contig_cpu_addr = dma_alloc_attrs(te_device.this_device,
alloc->nr_pages * PAGE_SIZE,
&alloc->contig_dma_addr,
GFP_KERNEL | __GFP_ZERO, &attrs);
#else
alloc->contig_cpu_addr = dma_alloc_writecombine(te_device.this_device,
alloc->nr_pages * PAGE_SIZE,
&alloc->contig_dma_addr,
GFP_KERNEL | __GFP_ZERO);
#endif
if (!alloc->contig_cpu_addr) {
dev_err(te_device.this_device, "%s: couldn't alloc contiguous buffer %zu pages",
__func__, alloc->nr_pages);
goto free_page_struct;
}
dma_aux = alloc->contig_dma_addr;
for (i = 0; i < alloc->nr_pages; i++) {
alloc->pages[i] = pfn_to_page(PFN_DOWN(dma_aux));
dma_aux += PAGE_SIZE;
}
} else {
for (i = 0; i < alloc->nr_pages; i++) {
alloc->pages[i] = alloc_page(GFP_KERNEL | __GFP_ZERO);
if (NULL == alloc->pages[i]) {
dev_err(te_device.this_device, "%s: couldn't alloc page", __func__);
goto no_page;
}
}
}
/* alloc ready, let's export it */
#if (LINUX_VERSION_CODE >= KERNEL_VERSION(4, 1, 0))
{
struct dma_buf_export_info export_info = {
.exp_name = "dma_buf_te",
#if (LINUX_VERSION_CODE >= KERNEL_VERSION(4, 2, 0))
.owner = THIS_MODULE,
#endif
.ops = &dma_buf_te_ops,
.size = alloc->nr_pages << PAGE_SHIFT,
.flags = O_CLOEXEC | O_RDWR,
.priv = alloc,
};
dma_buf = dma_buf_export(&export_info);
}
#elif (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 17, 0))
dma_buf = dma_buf_export(alloc, &dma_buf_te_ops,
alloc->nr_pages << PAGE_SHIFT, O_CLOEXEC|O_RDWR, NULL);
#else
dma_buf = dma_buf_export(alloc, &dma_buf_te_ops,
alloc->nr_pages << PAGE_SHIFT, O_CLOEXEC|O_RDWR);
#endif
if (IS_ERR_OR_NULL(dma_buf)) {
dev_err(te_device.this_device, "%s: couldn't export dma_buf", __func__);
goto no_export;
}
/* get fd for buf */
fd = dma_buf_fd(dma_buf, O_CLOEXEC);
if (fd < 0) {
dev_err(te_device.this_device, "%s: couldn't get fd from dma_buf", __func__);
goto no_fd;
}
return fd;
no_fd:
dma_buf_put(dma_buf);
no_export:
/* i still valid */
no_page:
if (contiguous) {
#if (LINUX_VERSION_CODE >= KERNEL_VERSION(4, 8, 0))
dma_free_attrs(te_device.this_device,
alloc->nr_pages * PAGE_SIZE,
alloc->contig_cpu_addr,
alloc->contig_dma_addr,
DMA_ATTR_WRITE_COMBINE);
#elif (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 5, 0))
DEFINE_DMA_ATTRS(attrs);
dma_set_attr(DMA_ATTR_WRITE_COMBINE, &attrs);
dma_free_attrs(te_device.this_device,
alloc->nr_pages * PAGE_SIZE,
alloc->contig_cpu_addr, alloc->contig_dma_addr, &attrs);
#else
dma_free_writecombine(te_device.this_device,
alloc->nr_pages * PAGE_SIZE,
alloc->contig_cpu_addr, alloc->contig_dma_addr);
#endif
} else {
while (i-- > 0)
__free_page(alloc->pages[i]);
}
free_page_struct:
kfree(alloc->pages);
free_alloc_object:
kfree(alloc);
no_alloc_object:
invalid_size:
no_input:
return -EFAULT;
}
static int do_dma_buf_te_ioctl_status(struct dma_buf_te_ioctl_status __user *arg)
{
struct dma_buf_te_ioctl_status status;
struct dma_buf *dmabuf;
struct dma_buf_te_alloc *alloc;
int res = -EINVAL;
if (copy_from_user(&status, arg, sizeof(status)))
return -EFAULT;
dmabuf = dma_buf_get(status.fd);
if (IS_ERR_OR_NULL(dmabuf))
return -EINVAL;
/* verify it's one of ours */
if (dmabuf->ops != &dma_buf_te_ops)
goto err_have_dmabuf;
/* ours, get the current status */
alloc = dmabuf->priv;
/* lock while reading status to take a snapshot */
mutex_lock(&dmabuf->lock);
status.attached_devices = alloc->nr_attached_devices;
status.device_mappings = alloc->nr_device_mappings;
status.cpu_mappings = alloc->nr_cpu_mappings;
mutex_unlock(&dmabuf->lock);
if (copy_to_user(arg, &status, sizeof(status)))
goto err_have_dmabuf;
/* All OK */
res = 0;
err_have_dmabuf:
dma_buf_put(dmabuf);
return res;
}
static int do_dma_buf_te_ioctl_set_failing(struct dma_buf_te_ioctl_set_failing __user *arg)
{
struct dma_buf *dmabuf;
struct dma_buf_te_ioctl_set_failing f;
struct dma_buf_te_alloc *alloc;
int res = -EINVAL;
if (copy_from_user(&f, arg, sizeof(f)))
return -EFAULT;
dmabuf = dma_buf_get(f.fd);
if (IS_ERR_OR_NULL(dmabuf))
return -EINVAL;
/* verify it's one of ours */
if (dmabuf->ops != &dma_buf_te_ops)
goto err_have_dmabuf;
/* ours, set the fail modes */
alloc = dmabuf->priv;
/* lock to set the fail modes atomically */
mutex_lock(&dmabuf->lock);
alloc->fail_attach = f.fail_attach;
alloc->fail_map = f.fail_map;
alloc->fail_mmap = f.fail_mmap;
mutex_unlock(&dmabuf->lock);
/* success */
res = 0;
err_have_dmabuf:
dma_buf_put(dmabuf);
return res;
}
static u32 dma_te_buf_fill(struct dma_buf *dma_buf, unsigned int value)
{
struct dma_buf_attachment *attachment;
struct sg_table *sgt;
struct scatterlist *sg;
unsigned int count;
unsigned int offset = 0;
int ret = 0;
size_t i;
attachment = dma_buf_attach(dma_buf, te_device.this_device);
if (IS_ERR_OR_NULL(attachment))
return -EBUSY;
sgt = dma_buf_map_attachment(attachment, DMA_BIDIRECTIONAL);
if (IS_ERR_OR_NULL(sgt)) {
ret = PTR_ERR(sgt);
goto no_import;
}
ret = dma_buf_begin_cpu_access(dma_buf,
#if LINUX_VERSION_CODE < KERNEL_VERSION(4, 6, 0)
0, dma_buf->size,
#endif
DMA_BIDIRECTIONAL);
if (ret)
goto no_cpu_access;
for_each_sg(sgt->sgl, sg, sgt->nents, count) {
for (i = 0; i < sg_dma_len(sg); i = i + PAGE_SIZE) {
void *addr = NULL;
#if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 6, 0)
addr = dma_buf_te_kmap(dma_buf, i >> PAGE_SHIFT);
#else
addr = dma_buf_kmap(dma_buf, i >> PAGE_SHIFT);
#endif
if (!addr) {
ret = -EPERM;
goto no_kmap;
}
memset(addr, value, PAGE_SIZE);
#if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 6, 0)
dma_buf_te_kunmap(dma_buf, i >> PAGE_SHIFT, addr);
#else
dma_buf_kunmap(dma_buf, i >> PAGE_SHIFT, addr);
#endif
}
offset += sg_dma_len(sg);
}
no_kmap:
dma_buf_end_cpu_access(dma_buf,
#if LINUX_VERSION_CODE < KERNEL_VERSION(4, 6, 0)
0, dma_buf->size,
#endif
DMA_BIDIRECTIONAL);
no_cpu_access:
dma_buf_unmap_attachment(attachment, sgt, DMA_BIDIRECTIONAL);
no_import:
dma_buf_detach(dma_buf, attachment);
return ret;
}
static int do_dma_buf_te_ioctl_fill(struct dma_buf_te_ioctl_fill __user *arg)
{
struct dma_buf *dmabuf;
struct dma_buf_te_ioctl_fill f;
int ret;
if (copy_from_user(&f, arg, sizeof(f)))
return -EFAULT;
dmabuf = dma_buf_get(f.fd);
if (IS_ERR_OR_NULL(dmabuf))
return -EINVAL;
ret = dma_te_buf_fill(dmabuf, f.value);
dma_buf_put(dmabuf);
return ret;
}
static long dma_buf_te_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
{
switch (cmd) {
case DMA_BUF_TE_VERSION:
return do_dma_buf_te_ioctl_version((struct dma_buf_te_ioctl_version __user *)arg);
case DMA_BUF_TE_ALLOC:
return do_dma_buf_te_ioctl_alloc((struct dma_buf_te_ioctl_alloc __user *)arg, false);
case DMA_BUF_TE_ALLOC_CONT:
return do_dma_buf_te_ioctl_alloc((struct dma_buf_te_ioctl_alloc __user *)arg, true);
case DMA_BUF_TE_QUERY:
return do_dma_buf_te_ioctl_status((struct dma_buf_te_ioctl_status __user *)arg);
case DMA_BUF_TE_SET_FAILING:
return do_dma_buf_te_ioctl_set_failing((struct dma_buf_te_ioctl_set_failing __user *)arg);
case DMA_BUF_TE_FILL:
return do_dma_buf_te_ioctl_fill((struct dma_buf_te_ioctl_fill __user *)arg);
default:
return -ENOTTY;
}
}
static const struct file_operations dma_buf_te_fops = {
.owner = THIS_MODULE,
.unlocked_ioctl = dma_buf_te_ioctl,
.compat_ioctl = dma_buf_te_ioctl,
};
static int __init dma_buf_te_init(void)
{
int res;
te_device.minor = MISC_DYNAMIC_MINOR;
te_device.name = "dma_buf_te";
te_device.fops = &dma_buf_te_fops;
res = misc_register(&te_device);
if (res) {
printk(KERN_WARNING"Misc device registration failed of 'dma_buf_te'\n");
return res;
}
te_device.this_device->coherent_dma_mask = DMA_BIT_MASK(32);
dev_info(te_device.this_device, "dma_buf_te ready\n");
return 0;
}
static void __exit dma_buf_te_exit(void)
{
misc_deregister(&te_device);
}
module_init(dma_buf_te_init);
module_exit(dma_buf_te_exit);
MODULE_LICENSE("GPL");
@@ -0,0 +1,22 @@
#
# (C) COPYRIGHT 2019 ARM Limited. All rights reserved.
#
# This program is free software and is provided to you under the terms of the
# GNU General Public License version 2 as published by the Free Software
# Foundation, and any use by you of this program is subject to the terms
# of such GNU licence.
#
# 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 the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, you can access it online at
# http://www.gnu.org/licenses/gpl-2.0.html.
#
# SPDX-License-Identifier: GPL-2.0
#
#
obj-$(CONFIG_MALI_MEMORY_GROUP_MANAGER) := memory_group_manager.o
@@ -0,0 +1,28 @@
#
# (C) COPYRIGHT 2019 ARM Limited. All rights reserved.
#
# This program is free software and is provided to you under the terms of the
# GNU General Public License version 2 as published by the Free Software
# Foundation, and any use by you of this program is subject to the terms
# of such GNU licence.
#
# 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 the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, you can access it online at
# http://www.gnu.org/licenses/gpl-2.0.html.
#
# SPDX-License-Identifier: GPL-2.0
#
#
config MALI_MEMORY_GROUP_MANAGER
tristate "MALI_MEMORY_GROUP_MANAGER"
help
This option enables an example implementation of a memory group manager
for allocation and release of pages for memory pools managed by Mali GPU
device drivers.
@@ -0,0 +1,35 @@
#
# (C) COPYRIGHT 2019 ARM Limited. All rights reserved.
#
# This program is free software and is provided to you under the terms of the
# GNU General Public License version 2 as published by the Free Software
# Foundation, and any use by you of this program is subject to the terms
# of such GNU licence.
#
# 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 the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, you can access it online at
# http://www.gnu.org/licenses/gpl-2.0.html.
#
# SPDX-License-Identifier: GPL-2.0
#
#
# linux build system bootstrap for out-of-tree module
# default to building for the host
ARCH ?= $(shell uname -m)
ifeq ($(KDIR),)
$(error Must specify KDIR to point to the kernel to target))
endif
all:
$(MAKE) ARCH=$(ARCH) -C $(KDIR) M=$(CURDIR) EXTRA_CFLAGS="-I$(CURDIR)/../../../include" modules CONFIG_MALI_MEMORY_GROUP_MANAGER=m
clean:
$(MAKE) ARCH=$(ARCH) -C $(KDIR) M=$(CURDIR) clean
@@ -0,0 +1,22 @@
/*
* (C) COPYRIGHT 2019 ARM Limited. All rights reserved.
*
* This program is free software and is provided to you under the terms of the
* GNU General Public License version 2 as published by the Free Software
* Foundation, and any use by you of this program is subject to the terms
* of such GNU licence.
*
* A copy of the licence is included with the program, and can also be obtained
* from Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
bob_kernel_module {
name: "memory_group_manager",
srcs: [
"Kbuild",
"memory_group_manager.c",
],
kbuild_options: ["CONFIG_MALI_MEMORY_GROUP_MANAGER=m"],
defaults: ["kernel_defaults"],
}
@@ -0,0 +1,493 @@
/*
*
* (C) COPYRIGHT 2019 ARM Limited. All rights reserved.
*
* This program is free software and is provided to you under the terms of the
* GNU General Public License version 2 as published by the Free Software
* Foundation, and any use by you of this program is subject to the terms
* of such GNU licence.
*
* 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 the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, you can access it online at
* http://www.gnu.org/licenses/gpl-2.0.html.
*
* SPDX-License-Identifier: GPL-2.0
*
*/
#include <linux/fs.h>
#include <linux/of.h>
#include <linux/slab.h>
#include <linux/platform_device.h>
#include <linux/version.h>
#include <linux/module.h>
#ifdef CONFIG_DEBUG_FS
#include <linux/debugfs.h>
#endif
#include <linux/mm.h>
#include <linux/memory_group_manager.h>
#if (KERNEL_VERSION(4, 20, 0) > LINUX_VERSION_CODE)
static inline vm_fault_t vmf_insert_pfn_prot(struct vm_area_struct *vma,
unsigned long addr, unsigned long pfn, pgprot_t pgprot)
{
int err;
#if ((KERNEL_VERSION(4, 4, 147) >= LINUX_VERSION_CODE) || \
((KERNEL_VERSION(4, 6, 0) > LINUX_VERSION_CODE) && \
(KERNEL_VERSION(4, 5, 0) <= LINUX_VERSION_CODE)))
if (pgprot_val(pgprot) != pgprot_val(vma->vm_page_prot))
return VM_FAULT_SIGBUS;
err = vm_insert_pfn(vma, addr, pfn);
#else
err = vm_insert_pfn_prot(vma, addr, pfn, pgprot);
#endif
if (unlikely(err == -ENOMEM))
return VM_FAULT_OOM;
if (unlikely(err < 0 && err != -EBUSY))
return VM_FAULT_SIGBUS;
return VM_FAULT_NOPAGE;
}
#endif
#define IMPORTED_MEMORY_ID (MEMORY_GROUP_MANAGER_NR_GROUPS - 1)
/**
* struct mgm_group - Structure to keep track of the number of allocated
* pages per group
*
* @size: The number of allocated small(4KB) pages
* @lp_size: The number of allocated large(2MB) pages
* @insert_pfn: The number of calls to map pages for CPU access.
* @update_gpu_pte: The number of calls to update GPU page table entries.
*
* This structure allows page allocation information to be displayed via
* debugfs. Display is organized per group with small and large sized pages.
*/
struct mgm_group {
size_t size;
size_t lp_size;
size_t insert_pfn;
size_t update_gpu_pte;
};
/**
* struct mgm_groups - Structure for groups of memory group manager
*
* @groups: To keep track of the number of allocated pages of all groups
* @dev: device attached
* @mgm_debugfs_root: debugfs root directory of memory group manager
*
* This structure allows page allocation information to be displayed via
* debugfs. Display is organized per group with small and large sized pages.
*/
struct mgm_groups {
struct mgm_group groups[MEMORY_GROUP_MANAGER_NR_GROUPS];
struct device *dev;
#ifdef CONFIG_DEBUG_FS
struct dentry *mgm_debugfs_root;
#endif
};
#ifdef CONFIG_DEBUG_FS
static int mgm_size_get(void *data, u64 *val)
{
struct mgm_group *group = data;
*val = group->size;
return 0;
}
static int mgm_lp_size_get(void *data, u64 *val)
{
struct mgm_group *group = data;
*val = group->lp_size;
return 0;
}
static int mgm_insert_pfn_get(void *data, u64 *val)
{
struct mgm_group *group = data;
*val = group->insert_pfn;
return 0;
}
static int mgm_update_gpu_pte_get(void *data, u64 *val)
{
struct mgm_group *group = data;
*val = group->update_gpu_pte;
return 0;
}
DEFINE_SIMPLE_ATTRIBUTE(fops_mgm_size, mgm_size_get, NULL, "%llu\n");
DEFINE_SIMPLE_ATTRIBUTE(fops_mgm_lp_size, mgm_lp_size_get, NULL, "%llu\n");
DEFINE_SIMPLE_ATTRIBUTE(fops_mgm_insert_pfn, mgm_insert_pfn_get, NULL,
"%llu\n");
DEFINE_SIMPLE_ATTRIBUTE(fops_mgm_update_gpu_pte, mgm_update_gpu_pte_get, NULL,
"%llu\n");
static void mgm_term_debugfs(struct mgm_groups *data)
{
debugfs_remove_recursive(data->mgm_debugfs_root);
}
#define MGM_DEBUGFS_GROUP_NAME_MAX 10
static int mgm_initialize_debugfs(struct mgm_groups *mgm_data)
{
int i;
struct dentry *e, *g;
char debugfs_group_name[MGM_DEBUGFS_GROUP_NAME_MAX];
/*
* Create root directory of memory-group-manager
*/
mgm_data->mgm_debugfs_root =
debugfs_create_dir("physical-memory-group-manager", NULL);
if (IS_ERR(mgm_data->mgm_debugfs_root)) {
dev_err(mgm_data->dev, "fail to create debugfs root directory\n");
return -ENODEV;
}
/*
* Create debugfs files per group
*/
for (i = 0; i < MEMORY_GROUP_MANAGER_NR_GROUPS; i++) {
scnprintf(debugfs_group_name, MGM_DEBUGFS_GROUP_NAME_MAX,
"group_%d", i);
g = debugfs_create_dir(debugfs_group_name,
mgm_data->mgm_debugfs_root);
if (IS_ERR(g)) {
dev_err(mgm_data->dev, "fail to create group[%d]\n", i);
goto remove_debugfs;
}
e = debugfs_create_file("size", 0444, g, &mgm_data->groups[i],
&fops_mgm_size);
if (IS_ERR(e)) {
dev_err(mgm_data->dev, "fail to create size[%d]\n", i);
goto remove_debugfs;
}
e = debugfs_create_file("lp_size", 0444, g,
&mgm_data->groups[i], &fops_mgm_lp_size);
if (IS_ERR(e)) {
dev_err(mgm_data->dev,
"fail to create lp_size[%d]\n", i);
goto remove_debugfs;
}
e = debugfs_create_file("insert_pfn", 0444, g,
&mgm_data->groups[i], &fops_mgm_insert_pfn);
if (IS_ERR(e)) {
dev_err(mgm_data->dev,
"fail to create insert_pfn[%d]\n", i);
goto remove_debugfs;
}
e = debugfs_create_file("update_gpu_pte", 0444, g,
&mgm_data->groups[i], &fops_mgm_update_gpu_pte);
if (IS_ERR(e)) {
dev_err(mgm_data->dev,
"fail to create update_gpu_pte[%d]\n", i);
goto remove_debugfs;
}
}
return 0;
remove_debugfs:
mgm_term_debugfs(mgm_data);
return -ENODEV;
}
#else
static void mgm_term_debugfs(struct mgm_groups *data)
{
}
static int mgm_initialize_debugfs(struct mgm_groups *mgm_data)
{
return 0;
}
#endif /* CONFIG_DEBUG_FS */
#define ORDER_SMALL_PAGE 0
#define ORDER_LARGE_PAGE 9
static void update_size(struct memory_group_manager_device *mgm_dev, int
group_id, int order, bool alloc)
{
struct mgm_groups *data = mgm_dev->data;
switch (order) {
case ORDER_SMALL_PAGE:
if (alloc)
data->groups[group_id].size++;
else {
WARN_ON(data->groups[group_id].size == 0);
data->groups[group_id].size--;
}
break;
case ORDER_LARGE_PAGE:
if (alloc)
data->groups[group_id].lp_size++;
else {
WARN_ON(data->groups[group_id].lp_size == 0);
data->groups[group_id].lp_size--;
}
break;
default:
dev_err(data->dev, "Unknown order(%d)\n", order);
break;
}
}
static struct page *example_mgm_alloc_page(
struct memory_group_manager_device *mgm_dev, int group_id,
gfp_t gfp_mask, unsigned int order)
{
struct mgm_groups *const data = mgm_dev->data;
struct page *p;
dev_dbg(data->dev, "%s(mgm_dev=%p, group_id=%d gfp_mask=0x%x order=%u\n",
__func__, (void *)mgm_dev, group_id, gfp_mask, order);
if (WARN_ON(group_id < 0) ||
WARN_ON(group_id >= MEMORY_GROUP_MANAGER_NR_GROUPS))
return NULL;
p = alloc_pages(gfp_mask, order);
if (p) {
update_size(mgm_dev, group_id, order, true);
} else {
struct mgm_groups *data = mgm_dev->data;
dev_err(data->dev, "alloc_pages failed\n");
}
return p;
}
static void example_mgm_free_page(
struct memory_group_manager_device *mgm_dev, int group_id,
struct page *page, unsigned int order)
{
struct mgm_groups *const data = mgm_dev->data;
dev_dbg(data->dev, "%s(mgm_dev=%p, group_id=%d page=%p order=%u\n",
__func__, (void *)mgm_dev, group_id, (void *)page, order);
if (WARN_ON(group_id < 0) ||
WARN_ON(group_id >= MEMORY_GROUP_MANAGER_NR_GROUPS))
return;
__free_pages(page, order);
update_size(mgm_dev, group_id, order, false);
}
static int example_mgm_get_import_memory_id(
struct memory_group_manager_device *mgm_dev,
struct memory_group_manager_import_data *import_data)
{
struct mgm_groups *const data = mgm_dev->data;
dev_dbg(data->dev, "%s(mgm_dev=%p, import_data=%p (type=%d)\n",
__func__, (void *)mgm_dev, (void *)import_data,
(int)import_data->type);
if (!WARN_ON(!import_data)) {
WARN_ON(!import_data->u.dma_buf);
WARN_ON(import_data->type !=
MEMORY_GROUP_MANAGER_IMPORT_TYPE_DMA_BUF);
}
return IMPORTED_MEMORY_ID;
}
static u64 example_mgm_update_gpu_pte(
struct memory_group_manager_device *const mgm_dev, int const group_id,
int const mmu_level, u64 pte)
{
struct mgm_groups *const data = mgm_dev->data;
const u32 pbha_bit_pos = 59; /* bits 62:59 */
const u32 pbha_bit_mask = 0xf; /* 4-bit */
dev_dbg(data->dev,
"%s(mgm_dev=%p, group_id=%d, mmu_level=%d, pte=0x%llx)\n",
__func__, (void *)mgm_dev, group_id, mmu_level, pte);
if (WARN_ON(group_id < 0) ||
WARN_ON(group_id >= MEMORY_GROUP_MANAGER_NR_GROUPS))
return pte;
pte |= ((u64)group_id & pbha_bit_mask) << pbha_bit_pos;
data->groups[group_id].update_gpu_pte++;
return pte;
}
static vm_fault_t example_mgm_vmf_insert_pfn_prot(
struct memory_group_manager_device *const mgm_dev, int const group_id,
struct vm_area_struct *const vma, unsigned long const addr,
unsigned long const pfn, pgprot_t const prot)
{
struct mgm_groups *const data = mgm_dev->data;
vm_fault_t fault;
dev_dbg(data->dev,
"%s(mgm_dev=%p, group_id=%d, vma=%p, addr=0x%lx, pfn=0x%lx, prot=0x%llx)\n",
__func__, (void *)mgm_dev, group_id, (void *)vma, addr, pfn,
(unsigned long long int) pgprot_val(prot));
if (WARN_ON(group_id < 0) ||
WARN_ON(group_id >= MEMORY_GROUP_MANAGER_NR_GROUPS))
return VM_FAULT_SIGBUS;
fault = vmf_insert_pfn_prot(vma, addr, pfn, prot);
if (fault == VM_FAULT_NOPAGE)
data->groups[group_id].insert_pfn++;
else
dev_err(data->dev, "vmf_insert_pfn_prot failed\n");
return fault;
}
static int mgm_initialize_data(struct mgm_groups *mgm_data)
{
int i;
for (i = 0; i < MEMORY_GROUP_MANAGER_NR_GROUPS; i++) {
mgm_data->groups[i].size = 0;
mgm_data->groups[i].lp_size = 0;
mgm_data->groups[i].insert_pfn = 0;
mgm_data->groups[i].update_gpu_pte = 0;
}
return mgm_initialize_debugfs(mgm_data);
}
static void mgm_term_data(struct mgm_groups *data)
{
int i;
for (i = 0; i < MEMORY_GROUP_MANAGER_NR_GROUPS; i++) {
if (data->groups[i].size != 0)
dev_warn(data->dev,
"%zu 0-order pages in group(%d) leaked\n",
data->groups[i].size, i);
if (data->groups[i].lp_size != 0)
dev_warn(data->dev,
"%zu 9 order pages in group(%d) leaked\n",
data->groups[i].lp_size, i);
}
mgm_term_debugfs(data);
}
static int memory_group_manager_probe(struct platform_device *pdev)
{
struct memory_group_manager_device *mgm_dev;
struct mgm_groups *mgm_data;
mgm_dev = kzalloc(sizeof(*mgm_dev), GFP_KERNEL);
if (!mgm_dev)
return -ENOMEM;
mgm_dev->owner = THIS_MODULE;
mgm_dev->ops.mgm_alloc_page = example_mgm_alloc_page;
mgm_dev->ops.mgm_free_page = example_mgm_free_page;
mgm_dev->ops.mgm_get_import_memory_id =
example_mgm_get_import_memory_id;
mgm_dev->ops.mgm_vmf_insert_pfn_prot = example_mgm_vmf_insert_pfn_prot;
mgm_dev->ops.mgm_update_gpu_pte = example_mgm_update_gpu_pte;
mgm_data = kzalloc(sizeof(*mgm_data), GFP_KERNEL);
if (!mgm_data) {
kfree(mgm_dev);
return -ENOMEM;
}
mgm_dev->data = mgm_data;
mgm_data->dev = &pdev->dev;
if (mgm_initialize_data(mgm_data)) {
kfree(mgm_data);
kfree(mgm_dev);
return -ENOENT;
}
platform_set_drvdata(pdev, mgm_dev);
dev_info(&pdev->dev, "Memory group manager probed successfully\n");
return 0;
}
static int memory_group_manager_remove(struct platform_device *pdev)
{
struct memory_group_manager_device *mgm_dev =
platform_get_drvdata(pdev);
struct mgm_groups *mgm_data = mgm_dev->data;
mgm_term_data(mgm_data);
kfree(mgm_data);
kfree(mgm_dev);
dev_info(&pdev->dev, "Memory group manager removed successfully\n");
return 0;
}
static const struct of_device_id memory_group_manager_dt_ids[] = {
{ .compatible = "arm,physical-memory-group-manager" },
{ /* sentinel */ }
};
MODULE_DEVICE_TABLE(of, memory_group_manager_dt_ids);
static struct platform_driver memory_group_manager_driver = {
.probe = memory_group_manager_probe,
.remove = memory_group_manager_remove,
.driver = {
.name = "physical-memory-group-manager",
.owner = THIS_MODULE,
.of_match_table = of_match_ptr(memory_group_manager_dt_ids),
/*
* Prevent the mgm_dev from being unbound and freed, as other's
* may have pointers to it and would get confused, or crash, if
* it suddenly disappear.
*/
.suppress_bind_attrs = true,
}
};
module_platform_driver(memory_group_manager_driver);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("ARM Ltd.");
MODULE_VERSION("1.0");
@@ -0,0 +1,22 @@
#
# (C) COPYRIGHT 2019 ARM Limited. All rights reserved.
#
# This program is free software and is provided to you under the terms of the
# GNU General Public License version 2 as published by the Free Software
# Foundation, and any use by you of this program is subject to the terms
# of such GNU licence.
#
# 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 the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, you can access it online at
# http://www.gnu.org/licenses/gpl-2.0.html.
#
# SPDX-License-Identifier: GPL-2.0
#
#
obj-$(CONFIG_MALI_PROTECTED_MEMORY_ALLOCATOR) := protected_memory_allocator.o
@@ -0,0 +1,28 @@
#
# (C) COPYRIGHT 2019 ARM Limited. All rights reserved.
#
# This program is free software and is provided to you under the terms of the
# GNU General Public License version 2 as published by the Free Software
# Foundation, and any use by you of this program is subject to the terms
# of such GNU licence.
#
# 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 the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, you can access it online at
# http://www.gnu.org/licenses/gpl-2.0.html.
#
# SPDX-License-Identifier: GPL-2.0
#
#
config MALI_PROTECTED_MEMORY_ALLOCATOR
tristate "MALI_PROTECTED_MEMORY_ALLOCATOR"
help
This option enables an example implementation of a protected memory allocator
for allocation and release of pages of secure memory intended to be used
by the firmware of Mali GPU device drivers.
@@ -0,0 +1,35 @@
#
# (C) COPYRIGHT 2019 ARM Limited. All rights reserved.
#
# This program is free software and is provided to you under the terms of the
# GNU General Public License version 2 as published by the Free Software
# Foundation, and any use by you of this program is subject to the terms
# of such GNU licence.
#
# 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 the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, you can access it online at
# http://www.gnu.org/licenses/gpl-2.0.html.
#
# SPDX-License-Identifier: GPL-2.0
#
#
# linux build system bootstrap for out-of-tree module
# default to building for the host
ARCH ?= $(shell uname -m)
ifeq ($(KDIR),)
$(error Must specify KDIR to point to the kernel to target))
endif
all:
$(MAKE) ARCH=$(ARCH) -C $(KDIR) M=$(CURDIR) EXTRA_CFLAGS="-I$(CURDIR)/../../../include" modules CONFIG_MALI_PROTECTED_MEMORY_ALLOCATOR=m
clean:
$(MAKE) ARCH=$(ARCH) -C $(KDIR) M=$(CURDIR) clean
@@ -0,0 +1,26 @@
/*
* (C) COPYRIGHT 2019 ARM Limited. All rights reserved.
*
* This program is free software and is provided to you under the terms of the
* GNU General Public License version 2 as published by the Free Software
* Foundation, and any use by you of this program is subject to the terms
* of such GNU licence.
*
* A copy of the licence is included with the program, and can also be obtained
* from Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
bob_kernel_module {
name: "protected_memory_allocator",
srcs: [
"Kbuild",
"protected_memory_allocator.c",
],
kbuild_options: ["CONFIG_MALI_PROTECTED_MEMORY_ALLOCATOR=m"],
defaults: ["kernel_defaults"],
enabled: false,
build_csf_only_module: {
enabled: true,
},
}
@@ -0,0 +1,308 @@
/*
*
* (C) COPYRIGHT 2019-2020 ARM Limited. All rights reserved.
*
* This program is free software and is provided to you under the terms of the
* GNU General Public License version 2 as published by the Free Software
* Foundation, and any use by you of this program is subject to the terms
* of such GNU licence.
*
* 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 the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, you can access it online at
* http://www.gnu.org/licenses/gpl-2.0.html.
*
* SPDX-License-Identifier: GPL-2.0
*
*/
#include <linux/version.h>
#include <linux/of.h>
#include <linux/of_reserved_mem.h>
#include <linux/platform_device.h>
#include <linux/module.h>
#include <linux/slab.h>
#include <linux/mm.h>
#include <linux/io.h>
#include <linux/protected_memory_allocator.h>
/**
* struct simple_pma_device - Simple implementation of a protected memory
* allocator device
*
* @pma_dev: Protected memory allocator device pointer
* @dev: Device pointer
* @alloc_pages: Status of all the physical memory pages within the
* protected memory region; true for allocated pages
* @rmem_base: Base address of the reserved memory region
* @rmem_size: Size of the reserved memory region, in pages
* @free_pa_offset: Offset of the lowest physical address within the protected
* memory region that is currently associated with a free page
* @num_free_pages: Number of free pages in the memory region
*/
struct simple_pma_device {
struct protected_memory_allocator_device pma_dev;
struct device *dev;
bool *alloc_pages;
phys_addr_t rmem_base;
size_t rmem_size;
size_t free_pa_offset;
size_t num_free_pages;
};
static struct protected_memory_allocation *simple_pma_alloc_page(
struct protected_memory_allocator_device *pma_dev, unsigned int order)
{
struct simple_pma_device *const epma_dev =
container_of(pma_dev, struct simple_pma_device, pma_dev);
struct protected_memory_allocation *pma;
size_t num_pages;
size_t i;
dev_dbg(epma_dev->dev, "%s(pma_dev=%px, order=%u\n",
__func__, (void *)pma_dev, order);
/* This is an example function that follows an extremely simple logic
* and is very likely to fail to allocate memory if put under stress.
*
* The simple_pma_device maintains an array of booleans to track
* the status of every page and an offset to the free page to use
* for the next allocation. The offset starts from 0 and can only grow,
* and be reset when the end of the memory region is reached.
*
* In order to create a memory allocation, the allocator simply looks
* at the offset and verifies whether there are enough free pages
* after it to accommodate the allocation request. If successful,
* the allocator shall mark all the pages as allocated and increment
* the offset accordingly.
*
* The allocator does not look for any other free pages inside the
* memory region, even if plenty of free memory is available.
* Free memory pages are counted and the offset is ignored if the
* memory region is fully allocated.
*/
/* The only candidate for allocation is the sub-region starting
* from the free_pa_offset. Verify that enough contiguous pages
* are available and that they are all free.
*/
num_pages = (size_t)1 << order;
if (epma_dev->num_free_pages < num_pages)
dev_err(epma_dev->dev, "not enough free pages\n");
if (epma_dev->free_pa_offset + num_pages > epma_dev->rmem_size) {
dev_err(epma_dev->dev, "not enough contiguous pages\n");
return NULL;
}
for (i = 0; i < num_pages; i++)
if (epma_dev->alloc_pages[epma_dev->free_pa_offset + i])
break;
if (i < num_pages) {
dev_err(epma_dev->dev, "free pages are not contiguous\n");
return NULL;
}
/* Memory allocation is successful. Mark pages as allocated.
* Update the free_pa_offset if free pages are still available:
* increment the free_pa_offset accordingly, and then making sure
* that it points at the next free page, potentially wrapping over
* the end of the memory region.
*/
pma = devm_kzalloc(epma_dev->dev, sizeof(*pma), GFP_KERNEL);
if (!pma)
return NULL;
pma->pa = epma_dev->rmem_base + (epma_dev->free_pa_offset << PAGE_SHIFT);
pma->order = order;
for (i = 0; i < num_pages; i++)
epma_dev->alloc_pages[epma_dev->free_pa_offset + i] = true;
epma_dev->num_free_pages -= num_pages;
if (epma_dev->num_free_pages) {
epma_dev->free_pa_offset += num_pages;
i = 0;
while (epma_dev->alloc_pages[epma_dev->free_pa_offset + i]) {
epma_dev->free_pa_offset++;
if (epma_dev->free_pa_offset > epma_dev->rmem_size)
epma_dev->free_pa_offset = 0;
}
}
return pma;
}
static phys_addr_t simple_pma_get_phys_addr(
struct protected_memory_allocator_device *pma_dev,
struct protected_memory_allocation *pma)
{
struct simple_pma_device *const epma_dev =
container_of(pma_dev, struct simple_pma_device, pma_dev);
dev_dbg(epma_dev->dev, "%s(pma_dev=%px, pma=%px, pa=%llx\n",
__func__, (void *)pma_dev, (void *)pma, pma->pa);
return pma->pa;
}
static void simple_pma_free_page(
struct protected_memory_allocator_device *pma_dev,
struct protected_memory_allocation *pma)
{
struct simple_pma_device *const epma_dev =
container_of(pma_dev, struct simple_pma_device, pma_dev);
size_t num_pages;
size_t offset;
size_t i;
dev_dbg(epma_dev->dev, "%s(pma_dev=%px, pma=%px, pa=%llx\n",
__func__, (void *)pma_dev, (void *)pma, pma->pa);
/* This is an example function that follows an extremely simple logic
* and is vulnerable to abuse. For instance, double frees won't be
* detected.
*
* If memory is full, must update the free_pa_offset that is currently
* pointing at an allocated page.
*
* Increase the number of free pages and mark them as free.
*/
offset = (pma->pa - epma_dev->rmem_base) >> PAGE_SHIFT;
num_pages = (size_t)1 << pma->order;
if (epma_dev->num_free_pages == 0)
epma_dev->free_pa_offset = offset;
epma_dev->num_free_pages += num_pages;
for (i = 0; i < num_pages; i++)
epma_dev->alloc_pages[offset + i] = false;
devm_kfree(epma_dev->dev, pma);
}
static int protected_memory_allocator_probe(struct platform_device *pdev)
{
struct simple_pma_device *epma_dev;
struct device_node *np;
phys_addr_t rmem_base;
size_t rmem_size;
#if (KERNEL_VERSION(4, 15, 0) <= LINUX_VERSION_CODE)
struct reserved_mem *rmem;
#endif
np = pdev->dev.of_node;
if (!np) {
dev_err(&pdev->dev, "device node pointer not set\n");
return -ENODEV;
}
np = of_parse_phandle(np, "memory-region", 0);
if (!np) {
dev_err(&pdev->dev, "memory-region node not set\n");
return -ENODEV;
}
#if (KERNEL_VERSION(4, 15, 0) <= LINUX_VERSION_CODE)
rmem = of_reserved_mem_lookup(np);
if (rmem) {
rmem_base = rmem->base;
rmem_size = rmem->size >> PAGE_SHIFT;
} else
#endif
{
of_node_put(np);
dev_err(&pdev->dev, "could not read reserved memory-region\n");
return -ENODEV;
}
of_node_put(np);
epma_dev = devm_kzalloc(&pdev->dev, sizeof(*epma_dev), GFP_KERNEL);
if (!epma_dev)
return -ENOMEM;
epma_dev->pma_dev.ops.pma_alloc_page = simple_pma_alloc_page;
epma_dev->pma_dev.ops.pma_get_phys_addr = simple_pma_get_phys_addr;
epma_dev->pma_dev.ops.pma_free_page = simple_pma_free_page;
epma_dev->pma_dev.owner = THIS_MODULE;
epma_dev->dev = &pdev->dev;
epma_dev->rmem_base = rmem_base;
epma_dev->rmem_size = rmem_size;
epma_dev->free_pa_offset = 0;
epma_dev->num_free_pages = rmem_size;
epma_dev->alloc_pages = devm_kzalloc(&pdev->dev,
sizeof(bool) * epma_dev->rmem_size, GFP_KERNEL);
if (!epma_dev->alloc_pages) {
dev_err(&pdev->dev, "failed to allocate resources\n");
devm_kfree(&pdev->dev, epma_dev);
return -ENOMEM;
}
platform_set_drvdata(pdev, &epma_dev->pma_dev);
dev_info(&pdev->dev,
"Protected memory allocator probed successfully\n");
dev_info(&pdev->dev, "Protected memory region: base=%llx num pages=%zu\n",
rmem_base, rmem_size);
return 0;
}
static int protected_memory_allocator_remove(struct platform_device *pdev)
{
struct protected_memory_allocator_device *pma_dev =
platform_get_drvdata(pdev);
struct simple_pma_device *epma_dev;
struct device *dev;
if (!pma_dev)
return -EINVAL;
epma_dev = container_of(pma_dev, struct simple_pma_device, pma_dev);
dev = epma_dev->dev;
if (epma_dev->num_free_pages < epma_dev->rmem_size) {
dev_warn(&pdev->dev, "Leaking %zu pages of protected memory\n",
epma_dev->rmem_size - epma_dev->num_free_pages);
}
platform_set_drvdata(pdev, NULL);
devm_kfree(dev, epma_dev->alloc_pages);
devm_kfree(dev, epma_dev);
dev_info(&pdev->dev,
"Protected memory allocator removed successfully\n");
return 0;
}
static const struct of_device_id protected_memory_allocator_dt_ids[] = {
{ .compatible = "arm,protected-memory-allocator" },
{ /* sentinel */ }
};
MODULE_DEVICE_TABLE(of, protected_memory_allocator_dt_ids);
static struct platform_driver protected_memory_allocator_driver = {
.probe = protected_memory_allocator_probe,
.remove = protected_memory_allocator_remove,
.driver = {
.name = "simple_protected_memory_allocator",
.owner = THIS_MODULE,
.of_match_table = of_match_ptr(protected_memory_allocator_dt_ids),
}
};
module_platform_driver(protected_memory_allocator_driver);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("ARM Ltd.");
MODULE_VERSION("1.0");
@@ -0,0 +1,23 @@
#
# (C) COPYRIGHT 2012 ARM Limited. All rights reserved.
#
# This program is free software and is provided to you under the terms of the
# GNU General Public License version 2 as published by the Free Software
# Foundation, and any use by you of this program is subject to the terms
# of such GNU licence.
#
# 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 the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, you can access it online at
# http://www.gnu.org/licenses/gpl-2.0.html.
#
# SPDX-License-Identifier: GPL-2.0
#
#
obj-$(CONFIG_MALI_MIDGARD) += midgard/
@@ -0,0 +1,25 @@
#
# (C) COPYRIGHT 2012 ARM Limited. All rights reserved.
#
# This program is free software and is provided to you under the terms of the
# GNU General Public License version 2 as published by the Free Software
# Foundation, and any use by you of this program is subject to the terms
# of such GNU licence.
#
# 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 the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, you can access it online at
# http://www.gnu.org/licenses/gpl-2.0.html.
#
# SPDX-License-Identifier: GPL-2.0
#
#
menu "ARM GPU Configuration"
source "drivers/gpu/arm/midgard/Kconfig"
endmenu
@@ -0,0 +1,240 @@
#
# (C) COPYRIGHT 2012-2020 ARM Limited. All rights reserved.
#
# This program is free software and is provided to you under the terms of the
# GNU General Public License version 2 as published by the Free Software
# Foundation, and any use by you of this program is subject to the terms
# of such GNU licence.
#
# 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 the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, you can access it online at
# http://www.gnu.org/licenses/gpl-2.0.html.
#
# SPDX-License-Identifier: GPL-2.0
#
#
include $(src)/Kbuild-mtk-custom-env
include $(src)/platform/mtk_platform_common/Kbuild
# Driver version string which is returned to userspace via an ioctl
MALI_RELEASE_NAME ?= "r26p0-01eac0"
# Paths required for build
# make $(src) as absolute path if it isn't already, by prefixing $(srctree)
src:=$(if $(patsubst /%,,$(src)),$(srctree)/$(src),$(src))
KBASE_PATH = $(src)
KBASE_PLATFORM_PATH = $(KBASE_PATH)/platform_dummy
UMP_PATH = $(src)/../../../base
# Set up defaults if not defined by build system
MALI_CUSTOMER_RELEASE ?= 1
MALI_USE_CSF ?= 0
MALI_UNIT_TEST ?= 0
MALI_KERNEL_TEST_API ?= 0
MALI_COVERAGE ?= 0
MALI_JIT_PRESSURE_LIMIT_BASE ?= 1
#CONFIG_MALI_PLATFORM_NAME ?= "devicetree"
CONFIG_MALI_PLATFORM_NAME ?= $(CONFIG_MALI_PLATFORM_THIRDPARTY_NAME)
# Experimental features (corresponding -D definition should be appended to
# DEFINES below, e.g. for MALI_EXPERIMENTAL_FEATURE,
# -DMALI_EXPERIMENTAL_FEATURE=$(MALI_EXPERIMENTAL_FEATURE) should be appended)
#
# Experimental features must default to disabled, e.g.:
# MALI_EXPERIMENTAL_FEATURE ?= 0
MALI_INCREMENTAL_RENDERING ?= 0
# Set up our defines, which will be passed to gcc
DEFINES = \
-DMALI_CUSTOMER_RELEASE=$(MALI_CUSTOMER_RELEASE) \
-DMALI_USE_CSF=$(MALI_USE_CSF) \
-DMALI_KERNEL_TEST_API=$(MALI_KERNEL_TEST_API) \
-DMALI_UNIT_TEST=$(MALI_UNIT_TEST) \
-DMALI_COVERAGE=$(MALI_COVERAGE) \
-DMALI_RELEASE_NAME=\"$(MALI_RELEASE_NAME)\" \
-DMALI_JIT_PRESSURE_LIMIT_BASE=$(MALI_JIT_PRESSURE_LIMIT_BASE) \
-DMALI_INCREMENTAL_RENDERING=$(MALI_INCREMENTAL_RENDERING)
ifeq ($(KBUILD_EXTMOD),)
# in-tree
DEFINES +=-DMALI_KBASE_PLATFORM_PATH=../../$(src)/platform/$(CONFIG_MALI_PLATFORM_NAME)
else
# out-of-tree
DEFINES +=-DMALI_KBASE_PLATFORM_PATH=$(src)/platform/$(CONFIG_MALI_PLATFORM_NAME)
endif
DEFINES +=-DCONFIG_GPU_TRACEPOINTS=y
DEFINES += -I$(srctree)/drivers/staging/android
DEFINES += -DMALI_KBASE_BUILD
# Use our defines when compiling
/* MTK Add -I$(src)/include to include ddk linux headers: dma-buf-test-exporter.h, memory_group_manager.h, protected_mode_switcher.h */
ccflags-y += $(DEFINES) -I$(KBASE_PATH) -I$(KBASE_PLATFORM_PATH) -I$(UMP_PATH) -I$(srctree)/include/linux -I$(src)/include
ifeq ($(CONFIG_MTK_TRUSTED_MEMORY_SUBSYSTEM), y)
include $(srctree)/drivers/misc/mediatek/trusted_mem/Makefile.include
ccflags-y += -I$(srctree)/drivers/staging/android/ion
endif
subdir-ccflags-y += -I$(srctree)/drivers/misc/mediatek/trusted_mem/public
subdir-ccflags-y += -I$(srctree)/drivers/gpu/mediatek/mt-plat
ccflags-y += -I$(srctree)/drivers/staging/android/mtk_ion/
ccflags-y += -I$(srctree)/drivers/gpu/mediatek/ged/include/
subdir-ccflags-y += $(DEFINES) -I$(KBASE_PATH) -I$(KBASE_PLATFORM_PATH) -I$(UMP_PATH) -I$(srctree)/include/linux -I$(src)/include
SRC := \
context/mali_kbase_context.c \
debug/mali_kbase_debug_ktrace.c \
device/mali_kbase_device.c \
mali_kbase_cache_policy.c \
mali_kbase_ccswe.c \
mali_kbase_mem.c \
mali_kbase_mem_pool_group.c \
mali_kbase_native_mgm.c \
mali_kbase_ctx_sched.c \
mali_kbase_jm.c \
mali_kbase_gpuprops.c \
mali_kbase_pm.c \
mali_kbase_config.c \
mali_kbase_vinstr.c \
mali_kbase_hwcnt.c \
mali_kbase_hwcnt_backend_jm.c \
mali_kbase_hwcnt_gpu.c \
mali_kbase_hwcnt_legacy.c \
mali_kbase_hwcnt_types.c \
mali_kbase_hwcnt_virtualizer.c \
mali_kbase_softjobs.c \
mali_kbase_hw.c \
mali_kbase_debug.c \
mali_kbase_gpu_memory_debugfs.c \
mali_kbase_mem_linux.c \
mali_kbase_core_linux.c \
mali_kbase_mem_profile_debugfs.c \
mmu/mali_kbase_mmu.c \
mmu/mali_kbase_mmu_hw_direct.c \
mmu/mali_kbase_mmu_mode_lpae.c \
mmu/mali_kbase_mmu_mode_aarch64.c \
mali_kbase_disjoint_events.c \
mali_kbase_gator_api.c \
mali_kbase_debug_mem_view.c \
mali_kbase_smc.c \
mali_kbase_mem_pool.c \
mali_kbase_mem_pool_debugfs.c \
mali_kbase_debugfs_helper.c \
mali_kbase_strings.c \
mali_kbase_as_fault_debugfs.c \
mali_kbase_regs_history_debugfs.c \
mali_power_gpu_frequency_trace.c \
thirdparty/mali_kbase_mmap.c \
tl/mali_kbase_timeline.c \
tl/mali_kbase_timeline_io.c \
tl/mali_kbase_tlstream.c \
tl/mali_kbase_tracepoints.c \
gpu/mali_kbase_gpu.c \
mali_kbase_trace_gpu_mem.c
ifeq ($(MALI_USE_CSF),1)
SRC += \
debug/backend/mali_kbase_debug_ktrace_csf.c \
device/backend/mali_kbase_device_csf.c \
gpu/backend/mali_kbase_gpu_fault_csf.c \
tl/backend/mali_kbase_timeline_csf.c \
mmu/backend/mali_kbase_mmu_csf.c \
context/backend/mali_kbase_context_csf.c
else
SRC += \
mali_kbase_dummy_job_wa.c \
mali_kbase_debug_job_fault.c \
mali_kbase_event.c \
mali_kbase_jd.c \
mali_kbase_jd_debugfs.c \
mali_kbase_js.c \
mali_kbase_js_ctx_attr.c \
mali_kbase_kinstr_jm.c \
debug/backend/mali_kbase_debug_ktrace_jm.c \
device/backend/mali_kbase_device_jm.c \
gpu/backend/mali_kbase_gpu_fault_jm.c \
tl/backend/mali_kbase_timeline_jm.c \
mmu/backend/mali_kbase_mmu_jm.c \
context/backend/mali_kbase_context_jm.c
endif
ifeq ($(CONFIG_MALI_CINSTR_GWT),y)
SRC += mali_kbase_gwt.c
endif
ifeq ($(MALI_UNIT_TEST),1)
SRC += tl/mali_kbase_timeline_test.c
endif
ifeq ($(MALI_CUSTOMER_RELEASE),0)
SRC += mali_kbase_regs_dump_debugfs.c
endif
SRC += mali_gputrace.c
ccflags-y += -I$(KBASE_PATH) -I$(KBASE_PATH)/debug \
-I$(KBASE_PATH)/debug/backend
# Tell the Linux build system from which .o file to create the kernel module
obj-$(CONFIG_MALI_MIDGARD) += mali_kbase.o
# Tell the Linux build system to enable building of our .c files
mali_kbase-y := $(SRC:.c=.o)
# Kconfig passes in the name with quotes for in-tree builds - remove them.
platform_name := $(shell echo $(CONFIG_MALI_PLATFORM_NAME))
MALI_PLATFORM_DIR := platform/$(platform_name)
ccflags-y += -I$(src)/$(MALI_PLATFORM_DIR)
include $(src)/$(MALI_PLATFORM_DIR)/Kbuild
ifeq ($(CONFIG_MALI_DEVFREQ),y)
ifeq ($(CONFIG_DEVFREQ_THERMAL),y)
include $(src)/ipa/Kbuild
endif
endif
ifeq ($(MALI_USE_CSF),1)
include $(src)/csf/Kbuild
else
# empty
endif
ifeq ($(CONFIG_MALI_ARBITER_SUPPORT),y)
include $(src)/arbiter/Kbuild
else
# empty
endif
mali_kbase-$(CONFIG_MALI_DMA_FENCE) += \
mali_kbase_dma_fence.o \
mali_kbase_fence.o
mali_kbase-$(CONFIG_SYNC) += \
mali_kbase_sync_android.o \
mali_kbase_sync_common.o
mali_kbase-$(CONFIG_SYNC_FILE) += \
mali_kbase_sync_file.o \
mali_kbase_sync_common.o \
mali_kbase_fence.o
include $(src)/backend/gpu/Kbuild
mali_kbase-y += $(BACKEND:.c=.o)
ccflags-y += -I$(src)/backend/gpu
subdir-ccflags-y += -I$(src)/backend/gpu
# For kutf and mali_kutf_irq_latency_test
obj-$(CONFIG_MALI_KUTF) += tests/
@@ -0,0 +1,42 @@
$(info mali MTK evironment, building r19p0 DDK)
MTK_TOP = $(srctree)/drivers/misc/mediatek/
# build support for a ARM Mali Midgard GPU
CONFIG_MALI_MIDGARD := y
# use mtk platform
CONFIG_MALI_PLATFORM_THIRDPARTY := y
CONFIG_MALI_PLATFORM_THIRDPARTY_NAME := $(CONFIG_MTK_PLATFORM)
$(info mtk-Kbuild CONFIG_MALI_PLATFORM_THIRDPARTY := $(CONFIG_MALI_PLATFORM_THIRDPARTY))
$(info mtk-Kbuild CONFIG_MALI_PLATFORM_THIRDPARTY_NAME := $(CONFIG_MALI_PLATFORM_THIRDPARTY_NAME))
CONFIG_MALI_DEBUG := y
ccflags-y += -I-I$(srctree)/include
ccflags-y += -I$(srctree)/drivers/staging/android
ccflags-y += -I$(srctree)/drivers/misc/mediatek/base/power/$(CONFIG_MTK_PLATFORM)
ccflags-y += -I$(srctree)/drivers/misc/mediatek/gpu/ged/include
ccflags-y += -I$(srctree)/drivers/gpu/mediatek/gpu_bm
ccflags-y += -I$(srctree)/drivers/gpu/mediatek/gpufreq/$(CONFIG_MTK_PLATFORM)
ccflags-y += -DCONFIG_MALI_MIDGARD_DVFS
ifeq ($(CONFIG_DEBUG_FS),y)
ccflags-y += -DCONFIG_DEBUG_FS
endif
ccflags-y += -DCONFIG_PROC_FS
ccflags-y += -DENABLE_MTK_MEMINFO
ccflags-$(CONFIG_MTK_GPU_COMMON_DVFS_SUPPORT) += -DENABLE_COMMON_DVFS
#ifneq (, $(findstring $(CONFIG_MTK_PLATFORM), "mt6785"))
#ccflags-y += -DMTK_GPU_BM_2
#$(info *GPU defined GPU BM QoS 2.0)
#endif
ifneq (, $(findstring $(CONFIG_MTK_PLATFORM), "mt6771"))
ccflags-y += -DENABLE_MTK_DEBUG
$(info *GPU defined ENABLE_MTK_DEBUG)
endif
@@ -0,0 +1,309 @@
#
# (C) COPYRIGHT 2012-2020 ARM Limited. All rights reserved.
#
# This program is free software and is provided to you under the terms of the
# GNU General Public License version 2 as published by the Free Software
# Foundation, and any use by you of this program is subject to the terms
# of such GNU licence.
#
# 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 the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, you can access it online at
# http://www.gnu.org/licenses/gpl-2.0.html.
#
# SPDX-License-Identifier: GPL-2.0
#
#
menuconfig MALI_MIDGARD
tristate "Mali Midgard series support"
select GPU_TRACEPOINTS if ANDROID
select DMA_SHARED_BUFFER
default n
help
Enable this option to build support for a ARM Mali Midgard GPU.
To compile this driver as a module, choose M here:
this will generate a single module, called mali_kbase.
config MALI_GATOR_SUPPORT
bool "Enable Streamline tracing support"
depends on MALI_MIDGARD
default y
help
Enables kbase tracing used by the Arm Streamline Performance Analyzer.
The tracepoints are used to derive GPU activity charts in Streamline.
config MALI_MIDGARD_DVFS
bool "Enable legacy DVFS"
depends on MALI_MIDGARD && !MALI_DEVFREQ
default n
help
Choose this option to enable legacy DVFS in the Mali Midgard DDK.
config MALI_MIDGARD_ENABLE_TRACE
bool "Enable kbase tracing"
depends on MALI_MIDGARD
default y if MALI_DEBUG
default n
help
Enables tracing in kbase. Trace log available through
the "mali_trace" debugfs file, when the CONFIG_DEBUG_FS is enabled
config MALI_DEVFREQ
bool "devfreq support for Mali"
depends on MALI_MIDGARD && PM_DEVFREQ
default y
help
Support devfreq for Mali.
Using the devfreq framework and, by default, the simpleondemand
governor, the frequency of Mali will be dynamically selected from the
available OPPs.
config MALI_DMA_FENCE
bool "DMA_BUF fence support for Mali"
depends on MALI_MIDGARD
default n
help
Support DMA_BUF fences for Mali.
This option should only be enabled if the Linux Kernel has built in
support for DMA_BUF fences.
config MALI_PLATFORM_NAME
depends on MALI_MIDGARD
string "Platform name"
default "devicetree"
help
Enter the name of the desired platform configuration directory to
include in the build. 'platform/$(MALI_PLATFORM_NAME)/Kbuild' must
exist.
config MALI_ARBITER_SUPPORT
bool "Enable arbiter support for Mali"
depends on MALI_MIDGARD
default n
help
Enable support for the arbiter interface in the driver.
This allows an external arbiter to manage driver access
to GPU hardware in a virtualized environment
If unsure, say N.
# MALI_EXPERT configuration options
menuconfig MALI_EXPERT
depends on MALI_MIDGARD
bool "Enable Expert Settings"
default n
help
Enabling this option and modifying the default settings may produce a driver with performance or
other limitations.
config MALI_CORESTACK
bool "Support controlling power to the GPU core stack"
depends on MALI_MIDGARD && MALI_EXPERT
default n
help
Enabling this feature on supported GPUs will let the driver powering
on/off the GPU core stack independently without involving the Power
Domain Controller. This should only be enabled on platforms which
integration of the PDC to the Mali GPU is known to be problematic.
This feature is currently only supported on t-Six and t-HEx GPUs.
If unsure, say N.
config MALI_DEBUG
bool "Debug build"
depends on MALI_MIDGARD && MALI_EXPERT
default n
help
Select this option for increased checking and reporting of errors.
config MALI_FENCE_DEBUG
bool "Debug sync fence usage"
depends on MALI_MIDGARD && MALI_EXPERT && (SYNC || SYNC_FILE)
default y if MALI_DEBUG
help
Select this option to enable additional checking and reporting on the
use of sync fences in the Mali driver.
This will add a 3s timeout to all sync fence waits in the Mali
driver, so that when work for Mali has been waiting on a sync fence
for a long time a debug message will be printed, detailing what fence
is causing the block, and which dependent Mali atoms are blocked as a
result of this.
The timeout can be changed at runtime through the js_soft_timeout
device attribute, where the timeout is specified in milliseconds.
config MALI_NO_MALI
bool "No Mali"
depends on MALI_MIDGARD && MALI_EXPERT
default n
help
This can be used to test the driver in a simulated environment
whereby the hardware is not physically present. If the hardware is physically
present it will not be used. This can be used to test the majority of the
driver without needing actual hardware or for software benchmarking.
All calls to the simulated hardware will complete immediately as if the hardware
completed the task.
config MALI_REAL_HW
def_bool !MALI_NO_MALI
config MALI_ERROR_INJECT
bool "Error injection"
depends on MALI_MIDGARD && MALI_EXPERT && MALI_NO_MALI
default n
help
Enables insertion of errors to test module failure and recovery mechanisms.
config MALI_SYSTEM_TRACE
bool "Enable system event tracing support"
depends on MALI_MIDGARD && MALI_EXPERT
default y if MALI_DEBUG
default n
help
Choose this option to enable system trace events for each
kbase event. This is typically used for debugging but has
minimal overhead when not in use. Enable only if you know what
you are doing.
config MALI_2MB_ALLOC
bool "Attempt to allocate 2MB pages"
depends on MALI_MIDGARD && MALI_EXPERT
default n
help
Rather than allocating all GPU memory page-by-page, attempt to
allocate 2MB pages from the kernel. This reduces TLB pressure and
helps to prevent memory fragmentation.
If in doubt, say N
config MALI_PWRSOFT_765
bool "PWRSOFT-765 ticket"
depends on MALI_MIDGARD && MALI_EXPERT
default n
help
PWRSOFT-765 fixes devfreq cooling devices issues. The fix was merged
in kernel v4.10, however if backported into the kernel then this
option must be manually selected.
If using kernel >= v4.10 then say N, otherwise if devfreq cooling
changes have been backported say Y to avoid compilation errors.
config MALI_MEMORY_FULLY_BACKED
bool "Memory fully physically-backed"
depends on MALI_MIDGARD && MALI_EXPERT
default n
help
This option enables full physical backing of all virtual
memory allocations in the kernel. Notice that this build
option only affects allocations of grow-on-GPU-page-fault
memory.
config MALI_DMA_BUF_MAP_ON_DEMAND
bool "Map imported dma-bufs on demand"
depends on MALI_MIDGARD
default n
help
This option caused kbase to set up the GPU mapping of imported
dma-buf when needed to run atoms. This is the legacy behaviour.
This is intended for testing and the option will get removed in the
future.
config MALI_DMA_BUF_LEGACY_COMPAT
bool "Enable legacy compatibility cache flush on dma-buf map"
depends on MALI_MIDGARD && !MALI_DMA_BUF_MAP_ON_DEMAND
default n
help
This option enables compatibility with legacy dma-buf mapping
behavior, then the dma-buf is mapped on import, by adding cache
maintenance where MALI_DMA_BUF_MAP_ON_DEMAND would do the mapping,
including a cache flush.
This option might work-around issues related to missing cache
flushes in other drivers. This only has an effect for clients using
UK 11.18 or older. For later UK versions it is not possible.
config MALI_HW_ERRATA_1485982_NOT_AFFECTED
bool "Disable workaround for BASE_HW_ISSUE_GPU2017_1336"
depends on MALI_MIDGARD && MALI_EXPERT
default n
help
This option disables the default workaround for GPU2017-1336. The
workaround keeps the L2 cache powered up except for powerdown and reset.
The workaround introduces a limitation that will prevent the running of
protected mode content on fully coherent platforms, as the switch to IO
coherency mode requires the L2 to be turned off.
config MALI_HW_ERRATA_1485982_USE_CLOCK_ALTERNATIVE
bool "Use alternative workaround for BASE_HW_ISSUE_GPU2017_1336"
depends on MALI_MIDGARD && MALI_EXPERT && !MALI_HW_ERRATA_1485982_NOT_AFFECTED
default n
help
This option uses an alternative workaround for GPU2017-1336. Lowering
the GPU clock to a, platform specific, known good frequeuncy before
powering down the L2 cache. The clock can be specified in the device
tree using the property, opp-mali-errata-1485982. Otherwise the
slowest clock will be selected.
config MALI_GEM5_BUILD
bool "Enable build of Mali kernel driver for GEM5"
depends on MALI_MIDGARD
default n
help
This option is to do a Mali GEM5 build.
If unsure, say N.
# Instrumentation options.
config MALI_JOB_DUMP
bool "Enable system level support needed for job dumping"
depends on MALI_MIDGARD && MALI_EXPERT
default n
help
Choose this option to enable system level support needed for
job dumping. This is typically used for instrumentation but has
minimal overhead when not in use. Enable only if you know what
you are doing.
config MALI_PRFCNT_SET_SECONDARY
bool "Use secondary set of performance counters"
depends on MALI_MIDGARD && MALI_EXPERT
default n
help
Select this option to use secondary set of performance counters. Kernel
features that depend on an access to the primary set of counters may
become unavailable. Enabling this option will prevent power management
from working optimally and may cause instrumentation tools to return
bogus results.
If unsure, say N.
config MALI_PRFCNT_SET_SECONDARY_VIA_DEBUG_FS
bool "Use secondary set of performance counters"
depends on MALI_MIDGARD && MALI_EXPERT && !MALI_PRFCNT_SET_SECONDARY && DEBUG_FS
default n
help
Select this option to make the secondary set of performance counters
available at runtime via debugfs. Kernel features that depend on an
access to the primary set of counters may become unavailable.
This feature is unsupported and unstable, and may break at any time.
Enabling this option will prevent power management from working
optimally and may cause instrumentation tools to return bogus results.
If unsure, say N.
source "drivers/gpu/arm/midgard/platform/Kconfig"
source "drivers/gpu/arm/midgard/tests/Kconfig"
@@ -0,0 +1,39 @@
#
# (C) COPYRIGHT 2010-2019 ARM Limited. All rights reserved.
#
# This program is free software and is provided to you under the terms of the
# GNU General Public License version 2 as published by the Free Software
# Foundation, and any use by you of this program is subject to the terms
# of such GNU licence.
#
# 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 the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, you can access it online at
# http://www.gnu.org/licenses/gpl-2.0.html.
#
# SPDX-License-Identifier: GPL-2.0
#
#
KDIR ?= /lib/modules/$(shell uname -r)/build
BUSLOG_PATH_RELATIVE = $(CURDIR)/../../../..
KBASE_PATH_RELATIVE = $(CURDIR)
ifeq ($(CONFIG_MALI_BUSLOG),y)
#Add bus logger symbols
EXTRA_SYMBOLS += $(BUSLOG_PATH_RELATIVE)/drivers/base/bus_logger/Module.symvers
endif
# we get the symbols from modules using KBUILD_EXTRA_SYMBOLS to prevent warnings about unknown functions
all:
$(MAKE) -C $(KDIR) M=$(CURDIR) EXTRA_CFLAGS="-I$(CURDIR)/../../../../include -I$(CURDIR)/../../../../tests/include $(SCONS_CFLAGS)" $(SCONS_CONFIGS) KBUILD_EXTRA_SYMBOLS="$(EXTRA_SYMBOLS)" modules
clean:
$(MAKE) -C $(KDIR) M=$(CURDIR) clean
@@ -0,0 +1,23 @@
#
# (C) COPYRIGHT 2010, 2013, 2018 ARM Limited. All rights reserved.
#
# This program is free software and is provided to you under the terms of the
# GNU General Public License version 2 as published by the Free Software
# Foundation, and any use by you of this program is subject to the terms
# of such GNU licence.
#
# 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 the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, you can access it online at
# http://www.gnu.org/licenses/gpl-2.0.html.
#
# SPDX-License-Identifier: GPL-2.0
#
#
EXTRA_CFLAGS += -I$(ROOT) -I$(KBASE_PATH) -I$(KBASE_PATH)/platform_$(PLATFORM)
@@ -0,0 +1,277 @@
#
# (C) COPYRIGHT 2012-2020 ARM Limited. All rights reserved.
#
# This program is free software and is provided to you under the terms of the
# GNU General Public License version 2 as published by the Free Software
# Foundation, and any use by you of this program is subject to the terms
# of such GNU licence.
#
# A copy of the licence is included with the program, and can also be obtained
# from Free Software Foundation, Inc.
#
#
menuconfig MALI_MIDGARD
bool "Mali Midgard series support"
default y
help
Enable this option to build support for a ARM Mali Midgard GPU.
To compile this driver as a module, choose M here:
this will generate a single module, called mali_kbase.
config MALI_GATOR_SUPPORT
bool "Enable Streamline tracing support"
depends on MALI_MIDGARD && !BACKEND_USER
default y
help
Enables kbase tracing used by the Arm Streamline Performance Analyzer.
The tracepoints are used to derive GPU activity charts in Streamline.
config MALI_MIDGARD_DVFS
bool "Enable legacy DVFS"
depends on MALI_MIDGARD && !MALI_DEVFREQ
default n
help
Choose this option to enable legacy DVFS in the Mali Midgard DDK.
config MALI_MIDGARD_ENABLE_TRACE
bool "Enable kbase tracing"
depends on MALI_MIDGARD
default y if MALI_DEBUG
default n
help
Enables tracing in kbase. Trace log available through
the "mali_trace" debugfs file, when the CONFIG_DEBUG_FS is enabled
config MALI_DEVFREQ
bool "devfreq support for Mali"
depends on MALI_MIDGARD
default y if PLATFORM_JUNO
default y if PLATFORM_CUSTOM
help
Support devfreq for Mali.
Using the devfreq framework and, by default, the simpleondemand
governor, the frequency of Mali will be dynamically selected from the
available OPPs.
config MALI_DMA_FENCE
bool "DMA_BUF fence support for Mali"
depends on MALI_MIDGARD
default n
help
Support DMA_BUF fences for Mali.
This option should only be enabled if the Linux Kernel has built in
support for DMA_BUF fences.
config MALI_PLATFORM_NAME
depends on MALI_MIDGARD
string "Platform name"
default "hisilicon" if PLATFORM_HIKEY960
default "hisilicon" if PLATFORM_HIKEY970
default "devicetree"
help
Enter the name of the desired platform configuration directory to
include in the build. 'platform/$(MALI_PLATFORM_NAME)/Kbuild' must
exist.
When PLATFORM_CUSTOM is set, this needs to be set manually to
pick up the desired platform files.
config MALI_ARBITER_SUPPORT
bool "Enable arbiter support for Mali"
depends on MALI_MIDGARD
default n
help
Enable support for the arbiter interface in the driver.
This allows an external arbiter to manage driver access
to GPU hardware in a virtualized environment
If unsure, say N.
# MALI_EXPERT configuration options
menuconfig MALI_EXPERT
depends on MALI_MIDGARD
bool "Enable Expert Settings"
default y
help
Enabling this option and modifying the default settings may produce a driver with performance or
other limitations.
config MALI_CORESTACK
bool "Support controlling power to the GPU core stack"
depends on MALI_MIDGARD && MALI_EXPERT
default n
help
Enabling this feature on supported GPUs will let the driver powering
on/off the GPU core stack independently without involving the Power
Domain Controller. This should only be enabled on platforms which
integration of the PDC to the Mali GPU is known to be problematic.
This feature is currently only supported on t-Six and t-HEx GPUs.
If unsure, say N.
config MALI_DEBUG
bool "Debug build"
depends on MALI_MIDGARD && MALI_EXPERT
default y if DEBUG
default n
help
Select this option for increased checking and reporting of errors.
config MALI_FENCE_DEBUG
bool "Debug sync fence usage"
depends on MALI_MIDGARD && MALI_EXPERT
default y if MALI_DEBUG
help
Select this option to enable additional checking and reporting on the
use of sync fences in the Mali driver.
This will add a 3s timeout to all sync fence waits in the Mali
driver, so that when work for Mali has been waiting on a sync fence
for a long time a debug message will be printed, detailing what fence
is causing the block, and which dependent Mali atoms are blocked as a
result of this.
The timeout can be changed at runtime through the js_soft_timeout
device attribute, where the timeout is specified in milliseconds.
choice
prompt "Error injection level"
default MALI_ERROR_INJECT_NONE
help
Enables insertion of errors to test module failure and recovery mechanisms.
config MALI_ERROR_INJECT_NONE
bool "disabled"
help
Error injection is disabled.
config MALI_ERROR_INJECT_TRACK_LIST
bool "error track list"
depends on MALI_MIDGARD && MALI_EXPERT && NO_MALI
help
Errors to inject are pre-configured by the user.
config MALI_ERROR_INJECT_RANDOM
bool "random error injection"
depends on MALI_MIDGARD && MALI_EXPERT && NO_MALI
help
Injected errors are random, rather than user-driven.
endchoice
config MALI_ERROR_INJECT_ON
string
default "0" if MALI_ERROR_INJECT_NONE
default "1" if MALI_ERROR_INJECT_TRACK_LIST
default "2" if MALI_ERROR_INJECT_RANDOM
config MALI_ERROR_INJECT
bool
default y if !MALI_ERROR_INJECT_NONE
config MALI_SYSTEM_TRACE
bool "Enable system event tracing support"
depends on MALI_MIDGARD && MALI_EXPERT
default y if MALI_DEBUG
default n
help
Choose this option to enable system trace events for each
kbase event. This is typically used for debugging but has
minimal overhead when not in use. Enable only if you know what
you are doing.
config MALI_2MB_ALLOC
bool "Attempt to allocate 2MB pages"
depends on MALI_MIDGARD && MALI_EXPERT
default n
help
Rather than allocating all GPU memory page-by-page, attempt to
allocate 2MB pages from the kernel. This reduces TLB pressure and
helps to prevent memory fragmentation.
If in doubt, say N
config MALI_PWRSOFT_765
bool "PWRSOFT-765 ticket"
depends on MALI_MIDGARD && MALI_EXPERT
default n
help
PWRSOFT-765 fixes devfreq cooling devices issues. However, they are
not merged in mainline kernel yet. So this define helps to guard those
parts of the code.
config MALI_MEMORY_FULLY_BACKED
bool "Memory fully physically-backed"
default n
help
This option enables full backing of all virtual memory allocations
for the kernel. This only affects grow-on-GPU-page-fault memory.
config MALI_DMA_BUF_MAP_ON_DEMAND
bool "Map imported dma-bufs on demand"
depends on MALI_MIDGARD
default n
default y if !DMA_BUF_SYNC_IOCTL_SUPPORTED
help
This option caused kbase to set up the GPU mapping of imported
dma-buf when needed to run atoms. This is the legacy behaviour.
config MALI_DMA_BUF_LEGACY_COMPAT
bool "Enable legacy compatibility cache flush on dma-buf map"
depends on MALI_MIDGARD && !MALI_DMA_BUF_MAP_ON_DEMAND
default n
help
This option enables compatibility with legacy dma-buf mapping
behavior, then the dma-buf is mapped on import, by adding cache
maintenance where MALI_DMA_BUF_MAP_ON_DEMAND would do the mapping,
including a cache flush.
config MALI_REAL_HW
bool
default y
default n if NO_MALI
config MALI_HW_ERRATA_1485982_NOT_AFFECTED
bool "Disable workaround for BASE_HW_ISSUE_GPU2017_1336"
depends on MALI_MIDGARD && MALI_EXPERT
default n
default y if PLATFORM_JUNO
help
This option disables the default workaround for GPU2017-1336. The
workaround keeps the L2 cache powered up except for powerdown and reset.
The workaround introduces a limitation that will prevent the running of
protected mode content on fully coherent platforms, as the switch to IO
coherency mode requires the L2 to be turned off.
config MALI_HW_ERRATA_1485982_USE_CLOCK_ALTERNATIVE
bool "Use alternative workaround for BASE_HW_ISSUE_GPU2017_1336"
depends on MALI_MIDGARD && MALI_EXPERT && !MALI_HW_ERRATA_1485982_NOT_AFFECTED
default n
help
This option uses an alternative workaround for GPU2017-1336. Lowering
the GPU clock to a, platform specific, known good frequeuncy before
powering down the L2 cache. The clock can be specified in the device
tree using the property, opp-mali-errata-1485982. Otherwise the
slowest clock will be selected.
config MALI_GEM5_BUILD
bool "Enable build of Mali kernel driver for GEM5"
depends on MALI_MIDGARD
default n
help
This option is to do a Mali GEM5 build.
If unsure, say N.
# Instrumentation options.
# config MALI_JOB_DUMP exists in the Kernel Kconfig but is configured using CINSTR_JOB_DUMP in Mconfig.
# config MALI_PRFCNT_SET_SECONDARY exists in the Kernel Kconfig but is configured using CINSTR_SECONDARY_HWC in Mconfig.
source "kernel/drivers/gpu/arm/midgard/tests/Mconfig"
@@ -0,0 +1,24 @@
#
# (C) COPYRIGHT 2019-2020 ARM Limited. All rights reserved.
#
# This program is free software and is provided to you under the terms of the
# GNU General Public License version 2 as published by the Free Software
# Foundation, and any use by you of this program is subject to the terms
# of such GNU licence.
#
# 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 the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, you can access it online at
# http://www.gnu.org/licenses/gpl-2.0.html.
#
# SPDX-License-Identifier: GPL-2.0
#
#
mali_kbase-y += \
arbiter/mali_kbase_arbif.o \
arbiter/mali_kbase_arbiter_pm.o
@@ -0,0 +1,175 @@
// SPDX-License-Identifier: GPL-2.0
/*
*
* (C) COPYRIGHT 2019-2020 ARM Limited. All rights reserved.
*
* This program is free software and is provided to you under the terms of the
* GNU General Public License version 2 as published by the Free Software
* Foundation, and any use by you of this program is subject to the terms
* of such GNU licence.
*
* 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 the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, you can access it online at
* http://www.gnu.org/licenses/gpl-2.0.html.
*
* SPDX-License-Identifier: GPL-2.0
*
*/
/**
* @file mali_kbase_arbif.c
* Mali arbiter interface APIs to share GPU between Virtual Machines
*/
#include <mali_kbase.h>
#include "mali_kbase_arbif.h"
#include <tl/mali_kbase_tracepoints.h>
#include <linux/of.h>
#include <linux/of_platform.h>
#include "mali_kbase_arbiter_interface.h"
static void on_gpu_stop(struct device *dev)
{
struct kbase_device *kbdev = dev_get_drvdata(dev);
KBASE_TLSTREAM_TL_EVENT_ARB_STOP_REQUESTED(kbdev, kbdev);
kbase_arbiter_pm_vm_event(kbdev, KBASE_VM_GPU_STOP_EVT);
}
static void on_gpu_granted(struct device *dev)
{
struct kbase_device *kbdev = dev_get_drvdata(dev);
KBASE_TLSTREAM_TL_EVENT_ARB_GRANTED(kbdev, kbdev);
kbase_arbiter_pm_vm_event(kbdev, KBASE_VM_GPU_GRANTED_EVT);
}
static void on_gpu_lost(struct device *dev)
{
struct kbase_device *kbdev = dev_get_drvdata(dev);
kbase_arbiter_pm_vm_event(kbdev, KBASE_VM_GPU_LOST_EVT);
}
int kbase_arbif_init(struct kbase_device *kbdev)
{
#ifdef CONFIG_OF
struct arbiter_if_arb_vm_ops ops;
struct arbiter_if_dev *arb_if;
struct device_node *arbiter_if_node;
struct platform_device *pdev;
int err;
dev_dbg(kbdev->dev, "%s\n", __func__);
arbiter_if_node = of_parse_phandle(kbdev->dev->of_node,
"arbiter_if", 0);
if (!arbiter_if_node) {
dev_dbg(kbdev->dev, "No arbiter_if in Device Tree\n");
/* no arbiter interface defined in device tree */
kbdev->arb.arb_dev = NULL;
kbdev->arb.arb_if = NULL;
return 0;
}
pdev = of_find_device_by_node(arbiter_if_node);
if (!pdev) {
dev_err(kbdev->dev, "Failed to find arbiter_if device\n");
return -EPROBE_DEFER;
}
if (!pdev->dev.driver || !try_module_get(pdev->dev.driver->owner)) {
dev_err(kbdev->dev, "arbiter_if driver not available\n");
return -EPROBE_DEFER;
}
kbdev->arb.arb_dev = &pdev->dev;
arb_if = platform_get_drvdata(pdev);
if (!arb_if) {
dev_err(kbdev->dev, "arbiter_if driver not ready\n");
module_put(pdev->dev.driver->owner);
return -EPROBE_DEFER;
}
kbdev->arb.arb_if = arb_if;
ops.arb_vm_gpu_stop = on_gpu_stop;
ops.arb_vm_gpu_granted = on_gpu_granted;
ops.arb_vm_gpu_lost = on_gpu_lost;
/* register kbase arbiter_if callbacks */
if (arb_if->vm_ops.vm_arb_register_dev) {
err = arb_if->vm_ops.vm_arb_register_dev(arb_if,
kbdev->dev, &ops);
if (err) {
dev_err(kbdev->dev, "Arbiter registration failed.\n");
module_put(pdev->dev.driver->owner);
return err;
}
}
#else /* CONFIG_OF */
dev_dbg(kbdev->dev, "No arbiter without Device Tree support\n");
kbdev->arb.arb_dev = NULL;
kbdev->arb.arb_if = NULL;
#endif
return 0;
}
void kbase_arbif_destroy(struct kbase_device *kbdev)
{
struct arbiter_if_dev *arb_if = kbdev->arb.arb_if;
if (arb_if && arb_if->vm_ops.vm_arb_unregister_dev) {
dev_dbg(kbdev->dev, "%s\n", __func__);
arb_if->vm_ops.vm_arb_unregister_dev(kbdev->arb.arb_if);
}
kbdev->arb.arb_if = NULL;
if (kbdev->arb.arb_dev)
module_put(kbdev->arb.arb_dev->driver->owner);
kbdev->arb.arb_dev = NULL;
}
void kbase_arbif_gpu_request(struct kbase_device *kbdev)
{
struct arbiter_if_dev *arb_if = kbdev->arb.arb_if;
if (arb_if && arb_if->vm_ops.vm_arb_gpu_request) {
dev_dbg(kbdev->dev, "%s\n", __func__);
arb_if->vm_ops.vm_arb_gpu_request(arb_if);
}
}
void kbase_arbif_gpu_stopped(struct kbase_device *kbdev, u8 gpu_required)
{
struct arbiter_if_dev *arb_if = kbdev->arb.arb_if;
if (arb_if && arb_if->vm_ops.vm_arb_gpu_stopped) {
dev_dbg(kbdev->dev, "%s\n", __func__);
KBASE_TLSTREAM_TL_EVENT_ARB_STOPPED(kbdev, kbdev);
arb_if->vm_ops.vm_arb_gpu_stopped(arb_if, gpu_required);
}
}
void kbase_arbif_gpu_active(struct kbase_device *kbdev)
{
struct arbiter_if_dev *arb_if = kbdev->arb.arb_if;
if (arb_if && arb_if->vm_ops.vm_arb_gpu_active) {
dev_dbg(kbdev->dev, "%s\n", __func__);
arb_if->vm_ops.vm_arb_gpu_active(arb_if);
}
}
void kbase_arbif_gpu_idle(struct kbase_device *kbdev)
{
struct arbiter_if_dev *arb_if = kbdev->arb.arb_if;
if (arb_if && arb_if->vm_ops.vm_arb_gpu_idle) {
dev_dbg(kbdev->dev, "vm_arb_gpu_idle\n");
arb_if->vm_ops.vm_arb_gpu_idle(arb_if);
}
}
@@ -0,0 +1,133 @@
/*
*
* (C) COPYRIGHT ARM Limited. All rights reserved.
*
* This program is free software and is provided to you under the terms of the
* GNU General Public License version 2 as published by the Free Software
* Foundation, and any use by you of this program is subject to the terms
* of such GNU licence.
*
* 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 the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, you can access it online at
* http://www.gnu.org/licenses/gpl-2.0.html.
*
* SPDX-License-Identifier: GPL-2.0
*
*//* SPDX-License-Identifier: GPL-2.0 */
/*
*
* (C) COPYRIGHT 2019-2020 ARM Limited. All rights reserved.
*
* This program is free software and is provided to you under the terms of the
* GNU General Public License version 2 as published by the Free Software
* Foundation, and any use by you of this program is subject to the terms
* of such GNU license.
*
* 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 the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, you can access it online at
* http://www.gnu.org/licenses/gpl-2.0.html.
*
*
*/
/**
* @file
* Mali arbiter interface APIs to share GPU between Virtual Machines
*/
#ifndef _MALI_KBASE_ARBIF_H_
#define _MALI_KBASE_ARBIF_H_
/**
* enum kbase_arbif_evt - Internal Arbiter event.
*
* @KBASE_VM_GPU_INITIALIZED_EVT: KBase has finished initializing
* and can be stopped
* @KBASE_VM_GPU_STOP_EVT: Stop message received from Arbiter
* @KBASE_VM_GPU_GRANTED_EVT: Grant message received from Arbiter
* @KBASE_VM_GPU_LOST_EVT: Lost message received from Arbiter
* @KBASE_VM_GPU_IDLE_EVENT: KBase has transitioned into an inactive state.
* @KBASE_VM_REF_EVENT: KBase has transitioned into an active state.
* @KBASE_VM_OS_SUSPEND_EVENT: KBase is suspending
* @KBASE_VM_OS_RESUME_EVENT: Kbase is resuming
*/
enum kbase_arbif_evt {
KBASE_VM_GPU_INITIALIZED_EVT = 1,
KBASE_VM_GPU_STOP_EVT,
KBASE_VM_GPU_GRANTED_EVT,
KBASE_VM_GPU_LOST_EVT,
KBASE_VM_GPU_IDLE_EVENT,
KBASE_VM_REF_EVENT,
KBASE_VM_OS_SUSPEND_EVENT,
KBASE_VM_OS_RESUME_EVENT,
};
/**
* kbase_arbif_init() - Initialize the arbiter interface functionality.
* @kbdev: The kbase device structure for the device (must be a valid pointer)
*
* Initialize the arbiter interface and also determines
* if Arbiter functionality is required.
*
* Return: 0 if the Arbiter interface was successfully initialized or the
* Arbiter was not required.
*/
int kbase_arbif_init(struct kbase_device *kbdev);
/**
* kbase_arbif_destroy() - Cleanups the arbiter interface functionality.
* @kbdev: The kbase device structure for the device (must be a valid pointer)
*
* Cleans up the arbiter interface functionality and resets the reference count
* of the arbif module used
*/
void kbase_arbif_destroy(struct kbase_device *kbdev);
/**
* kbase_arbif_gpu_request() - Send GPU request message to the arbiter
* @kbdev: The kbase device structure for the device (must be a valid pointer)
*
* Sends a message to Arbiter to request GPU access.
*/
void kbase_arbif_gpu_request(struct kbase_device *kbdev);
/**
* kbase_arbif_gpu_stopped() - Send GPU stopped message to the arbiter
* @kbdev: The kbase device structure for the device (must be a valid pointer)
* @gpu_required: true if GPU access is still required
* (Arbiter will automatically send another grant message)
*
* Sends a message to Arbiter to notify that the GPU has stopped.
* @note Once this call has been made, KBase must not attempt to access the GPU
* until the #KBASE_VM_GPU_GRANTED_EVT event has been received.
*/
void kbase_arbif_gpu_stopped(struct kbase_device *kbdev, u8 gpu_required);
/**
* kbase_arbif_gpu_active() - Send a GPU active message to the arbiter
* @kbdev: The kbase device structure for the device (must be a valid pointer)
*
* Sends a message to Arbiter to report that KBase has gone active.
*/
void kbase_arbif_gpu_active(struct kbase_device *kbdev);
/**
* kbase_arbif_gpu_idle() - Send a GPU idle message to the arbiter
* @kbdev: The kbase device structure for the device (must be a valid pointer)
*
* Sends a message to Arbiter to report that KBase has gone idle.
*/
void kbase_arbif_gpu_idle(struct kbase_device *kbdev);
#endif /* _MALI_KBASE_ARBIF_H_ */
@@ -0,0 +1,95 @@
/*
*
* (C) COPYRIGHT ARM Limited. All rights reserved.
*
* This program is free software and is provided to you under the terms of the
* GNU General Public License version 2 as published by the Free Software
* Foundation, and any use by you of this program is subject to the terms
* of such GNU licence.
*
* 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 the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, you can access it online at
* http://www.gnu.org/licenses/gpl-2.0.html.
*
* SPDX-License-Identifier: GPL-2.0
*
*//* SPDX-License-Identifier: GPL-2.0 */
/*
*
* (C) COPYRIGHT 2019-2020 ARM Limited. All rights reserved.
*
* This program is free software and is provided to you under the terms of the
* GNU General Public License version 2 as published by the Free Software
* Foundation, and any use by you of this program is subject to the terms
* of such GNU license.
*
* 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 the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, you can access it online at
* http://www.gnu.org/licenses/gpl-2.0.html.
*
*
*/
/**
* @file
* Mali structures define to support arbitration feature
*/
#ifndef _MALI_KBASE_ARBITER_DEFS_H_
#define _MALI_KBASE_ARBITER_DEFS_H_
#include "mali_kbase_arbiter_pm.h"
/**
* struct kbase_arbiter_vm_state - Struct representing the state and containing the
* data of pm work
* @kbdev: Pointer to kbase device structure (must be a valid pointer)
* @vm_state_lock: The lock protecting the VM state when arbiter is used.
* This lock must also be held whenever the VM state is being
* transitioned
* @vm_state_wait: Wait queue set when GPU is granted
* @vm_state: Current state of VM
* @vm_arb_wq: Work queue for resuming or stopping work on the GPU for use
* with the Arbiter
* @vm_suspend_work: Work item for vm_arb_wq to stop current work on GPU
* @vm_resume_work: Work item for vm_arb_wq to resume current work on GPU
* @vm_arb_starting: Work queue resume in progress
* @vm_arb_stopping: Work queue suspend in progress
* @vm_arb_users_waiting: Count of users waiting for GPU
*/
struct kbase_arbiter_vm_state {
struct kbase_device *kbdev;
struct mutex vm_state_lock;
wait_queue_head_t vm_state_wait;
enum kbase_vm_state vm_state;
struct workqueue_struct *vm_arb_wq;
struct work_struct vm_suspend_work;
struct work_struct vm_resume_work;
bool vm_arb_starting;
bool vm_arb_stopping;
int vm_arb_users_waiting;
};
/**
* struct kbase_arbiter_device - Representing an instance of arbiter device,
* allocated from the probe method of Mali driver
* @arb_if: Pointer to the arbiter interface device
* @arb_dev: Pointer to the arbiter device
*/
struct kbase_arbiter_device {
struct arbiter_if_dev *arb_if;
struct device *arb_dev;
};
#endif /* _MALI_KBASE_ARBITER_DEFS_H_ */
@@ -0,0 +1,181 @@
/*
*
* (C) COPYRIGHT ARM Limited. All rights reserved.
*
* This program is free software and is provided to you under the terms of the
* GNU General Public License version 2 as published by the Free Software
* Foundation, and any use by you of this program is subject to the terms
* of such GNU licence.
*
* 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 the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, you can access it online at
* http://www.gnu.org/licenses/gpl-2.0.html.
*
* SPDX-License-Identifier: GPL-2.0
*
*//* SPDX-License-Identifier: GPL-2.0 */
/*
*
* (C) COPYRIGHT 2019-2020 ARM Limited. All rights reserved.
*
* This program is free software and is provided to you under the terms of the
* GNU General Public License version 2 as published by the Free Software
* Foundation, and any use by you of this program is subject to the terms
* of such GNU license.
*
* 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 the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, you can access it online at
* http://www.gnu.org/licenses/gpl-2.0.html.
*
*
*/
/**
* @file
* Defines the Mali arbiter interface
*/
#ifndef _MALI_KBASE_ARBITER_INTERFACE_H_
#define _MALI_KBASE_ARBITER_INTERFACE_H_
/**
* @brief Mali arbiter interface version
*
* This specifies the current version of the configuration interface. Whenever
* the arbiter interface changes, so that integration effort is required, the
* version number will be increased. Each configuration must make an effort
* to check that it implements the correct version.
*
* Version history:
* 1 - Added the Mali arbiter configuration interface.
* 2 - Strip out reference code from header
* 3 - Removed DVFS utilization interface (DVFS moved to arbiter side)
*/
#define MALI_KBASE_ARBITER_INTERFACE_VERSION 3
struct arbiter_if_dev;
/**
* struct arbiter_if_arb_vm_ops - Interface to communicate messages to VM
*
* This struct contains callbacks used to deliver messages
* from the arbiter to the corresponding VM.
*
* Note that calls into these callbacks may have synchronous calls back into
* the arbiter arbiter_if_vm_arb_ops callbacks below.
* For example vm_arb_gpu_stopped() may be called as a side effect of
* arb_vm_gpu_stop() being called here.
*/
struct arbiter_if_arb_vm_ops {
/**
* arb_vm_gpu_stop() - Ask VM to stop using GPU
* @dev: The arbif kernel module device.
*
* Informs KBase to stop using the GPU as soon as possible.
* @Note: Once the driver is no longer using the GPU, a call to
* vm_arb_gpu_stopped is expected by the arbiter.
*/
void (*arb_vm_gpu_stop)(struct device *dev);
/**
* arb_vm_gpu_granted() - GPU has been granted to VM
* @dev: The arbif kernel module device.
*
* Informs KBase that the GPU can now be used by the VM.
*/
void (*arb_vm_gpu_granted)(struct device *dev);
/**
* arb_vm_gpu_lost() - VM has lost the GPU
* @dev: The arbif kernel module device.
*
* This is called if KBase takes too long to respond to the arbiter
* stop request.
* Once this is called, KBase will assume that access to the GPU
* has been lost and will fail all running jobs and reset its
* internal state.
* If successful, will respond with a vm_arb_gpu_stopped message.
*/
void (*arb_vm_gpu_lost)(struct device *dev);
};
/**
* struct arbiter_if_vm_arb_ops - Interface to communicate messages to arbiter
*
* This struct contains callbacks used to request operations
* from the VM to the arbiter
*
* Note that we must not make any synchronous calls back in to the VM
* (via arbiter_if_arb_vm_ops above) in the context of these callbacks.
*/
struct arbiter_if_vm_arb_ops {
/**
* vm_arb_register_dev() - Register VM device driver callbacks.
* @arbif_dev: The arbiter interface we are registering device callbacks
* @dev: The device structure to supply in the callbacks.
* @ops: The callbacks that the device driver supports
* (none are optional).
*/
int (*vm_arb_register_dev)(struct arbiter_if_dev *arbif_dev,
struct device *dev, struct arbiter_if_arb_vm_ops *ops);
/**
* vm_arb_unregister_dev() - Unregister VM device driver callbacks.
* @arbif_dev: The arbiter interface we are unregistering from.
*/
void (*vm_arb_unregister_dev)(struct arbiter_if_dev *arbif_dev);
/**
* vm_arb_gpu_request() - Ask the arbiter interface for GPU access.
* @arbif_dev: The arbiter interface we want to issue the request.
*/
void (*vm_arb_gpu_request)(struct arbiter_if_dev *arbif_dev);
/**
* vm_arb_gpu_active() - Inform arbiter that the driver has gone active
* @arbif_dev: The arbiter interface device.
*/
void (*vm_arb_gpu_active)(struct arbiter_if_dev *arbif_dev);
/**
* vm_arb_gpu_idle() - Inform the arbiter that the driver has gone idle
* @arbif_dev: The arbiter interface device.
*/
void (*vm_arb_gpu_idle)(struct arbiter_if_dev *arbif_dev);
/**
* vm_arb_gpu_stopped() - Inform the arbiter that the driver has stopped
* using the GPU
* @arbif_dev: The arbiter interface device.
* @gpu_required: The GPU is still needed to do more work.
*/
void (*vm_arb_gpu_stopped)(struct arbiter_if_dev *arbif_dev,
u8 gpu_required);
};
/**
* struct arbiter_if_dev - Arbiter Interface
* @vm_ops: Callback functions for connecting KBase with
* arbiter interface device.
* @priv_data: Internal arbif data not used by KBASE.
*
* Arbiter Interface Kernel Module State used for linking KBase
* with an arbiter interface platform device
*/
struct arbiter_if_dev {
struct arbiter_if_vm_arb_ops vm_ops;
void *priv_data;
};
#endif /* _MALI_KBASE_ARBITER_INTERFACE_H_ */
@@ -0,0 +1,645 @@
// SPDX-License-Identifier: GPL-2.0
/*
*
* (C) COPYRIGHT 2019-2020 ARM Limited. All rights reserved.
*
* This program is free software and is provided to you under the terms of the
* GNU General Public License version 2 as published by the Free Software
* Foundation, and any use by you of this program is subject to the terms
* of such GNU licence.
*
* 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 the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, you can access it online at
* http://www.gnu.org/licenses/gpl-2.0.html.
*
* SPDX-License-Identifier: GPL-2.0
*
*/
/**
* @file mali_kbase_arbiter_pm.c
* Mali arbiter power manager state machine and APIs
*/
#include <mali_kbase.h>
#include <mali_kbase_pm.h>
#include <mali_kbase_hwaccess_jm.h>
#include <mali_kbase_irq_internal.h>
#include <mali_kbase_hwcnt_context.h>
#include <mali_kbase_pm_internal.h>
#include <tl/mali_kbase_tracepoints.h>
static void kbase_arbiter_pm_vm_wait_gpu_assignment(struct kbase_device *kbdev);
static inline bool kbase_arbiter_pm_vm_gpu_assigned_lockheld(
struct kbase_device *kbdev);
static inline const char *kbase_arbiter_pm_vm_state_str(
enum kbase_vm_state state)
{
switch (state) {
case KBASE_VM_STATE_INITIALIZING:
return "KBASE_VM_STATE_INITIALIZING";
case KBASE_VM_STATE_INITIALIZING_WITH_GPU:
return "KBASE_VM_STATE_INITIALIZING_WITH_GPU";
case KBASE_VM_STATE_SUSPENDED:
return "KBASE_VM_STATE_SUSPENDED";
case KBASE_VM_STATE_STOPPED:
return "KBASE_VM_STATE_STOPPED";
case KBASE_VM_STATE_STOPPED_GPU_REQUESTED:
return "KBASE_VM_STATE_STOPPED_GPU_REQUESTED";
case KBASE_VM_STATE_STARTING:
return "KBASE_VM_STATE_STARTING";
case KBASE_VM_STATE_IDLE:
return "KBASE_VM_STATE_IDLE";
case KBASE_VM_STATE_ACTIVE:
return "KBASE_VM_STATE_ACTIVE";
case KBASE_VM_STATE_STOPPING_IDLE:
return "KBASE_VM_STATE_STOPPING_IDLE";
case KBASE_VM_STATE_STOPPING_ACTIVE:
return "KBASE_VM_STATE_STOPPING_ACTIVE";
case KBASE_VM_STATE_SUSPEND_PENDING:
return "KBASE_VM_STATE_SUSPEND_PENDING";
case KBASE_VM_STATE_SUSPEND_WAIT_FOR_GRANT:
return "KBASE_VM_STATE_SUSPEND_WAIT_FOR_GRANT";
default:
KBASE_DEBUG_ASSERT(false);
return "[UnknownState]";
}
}
static inline const char *kbase_arbiter_pm_vm_event_str(
enum kbase_arbif_evt evt)
{
switch (evt) {
case KBASE_VM_GPU_INITIALIZED_EVT:
return "KBASE_VM_GPU_INITIALIZED_EVT";
case KBASE_VM_GPU_STOP_EVT:
return "KBASE_VM_GPU_STOP_EVT";
case KBASE_VM_GPU_GRANTED_EVT:
return "KBASE_VM_GPU_GRANTED_EVT";
case KBASE_VM_GPU_LOST_EVT:
return "KBASE_VM_GPU_LOST_EVT";
case KBASE_VM_OS_SUSPEND_EVENT:
return "KBASE_VM_OS_SUSPEND_EVENT";
case KBASE_VM_OS_RESUME_EVENT:
return "KBASE_VM_OS_RESUME_EVENT";
case KBASE_VM_GPU_IDLE_EVENT:
return "KBASE_VM_GPU_IDLE_EVENT";
case KBASE_VM_REF_EVENT:
return "KBASE_VM_REF_EVENT";
default:
KBASE_DEBUG_ASSERT(false);
return "[UnknownEvent]";
}
}
static void kbase_arbiter_pm_vm_set_state(struct kbase_device *kbdev,
enum kbase_vm_state new_state)
{
struct kbase_arbiter_vm_state *arb_vm_state = kbdev->pm.arb_vm_state;
dev_dbg(kbdev->dev, "VM set_state %s -> %s",
kbase_arbiter_pm_vm_state_str(arb_vm_state->vm_state),
kbase_arbiter_pm_vm_state_str(new_state));
lockdep_assert_held(&arb_vm_state->vm_state_lock);
arb_vm_state->vm_state = new_state;
wake_up(&arb_vm_state->vm_state_wait);
}
static void kbase_arbiter_pm_suspend_wq(struct work_struct *data)
{
struct kbase_arbiter_vm_state *arb_vm_state = container_of(data,
struct kbase_arbiter_vm_state,
vm_suspend_work);
struct kbase_device *kbdev = arb_vm_state->kbdev;
mutex_lock(&arb_vm_state->vm_state_lock);
dev_dbg(kbdev->dev, ">%s\n", __func__);
if (arb_vm_state->vm_state == KBASE_VM_STATE_STOPPING_IDLE ||
arb_vm_state->vm_state ==
KBASE_VM_STATE_STOPPING_ACTIVE ||
arb_vm_state->vm_state ==
KBASE_VM_STATE_SUSPEND_PENDING) {
mutex_unlock(&arb_vm_state->vm_state_lock);
dev_dbg(kbdev->dev, ">kbase_pm_driver_suspend\n");
kbase_pm_driver_suspend(kbdev);
dev_dbg(kbdev->dev, "<kbase_pm_driver_suspend\n");
mutex_lock(&arb_vm_state->vm_state_lock);
}
mutex_unlock(&arb_vm_state->vm_state_lock);
dev_dbg(kbdev->dev, "<%s\n", __func__);
}
static void kbase_arbiter_pm_resume_wq(struct work_struct *data)
{
struct kbase_arbiter_vm_state *arb_vm_state = container_of(data,
struct kbase_arbiter_vm_state,
vm_resume_work);
struct kbase_device *kbdev = arb_vm_state->kbdev;
mutex_lock(&arb_vm_state->vm_state_lock);
dev_dbg(kbdev->dev, ">%s\n", __func__);
arb_vm_state->vm_arb_starting = true;
if (arb_vm_state->vm_state == KBASE_VM_STATE_STARTING) {
mutex_unlock(&arb_vm_state->vm_state_lock);
dev_dbg(kbdev->dev, ">kbase_pm_driver_resume\n");
kbase_pm_driver_resume(kbdev, true);
dev_dbg(kbdev->dev, "<kbase_pm_driver_resume\n");
mutex_lock(&arb_vm_state->vm_state_lock);
} else if (arb_vm_state->vm_state == KBASE_VM_STATE_STOPPING_ACTIVE) {
kbase_arbiter_pm_vm_stopped(kbdev);
}
arb_vm_state->vm_arb_starting = false;
mutex_unlock(&arb_vm_state->vm_state_lock);
dev_dbg(kbdev->dev, "<%s\n", __func__);
}
int kbase_arbiter_pm_early_init(struct kbase_device *kbdev)
{
int err;
struct kbase_arbiter_vm_state *arb_vm_state = NULL;
arb_vm_state = kmalloc(sizeof(struct kbase_arbiter_vm_state),
GFP_KERNEL);
if (arb_vm_state == NULL)
return -ENOMEM;
arb_vm_state->kbdev = kbdev;
arb_vm_state->vm_state = KBASE_VM_STATE_INITIALIZING;
mutex_init(&arb_vm_state->vm_state_lock);
init_waitqueue_head(&arb_vm_state->vm_state_wait);
arb_vm_state->vm_arb_wq = alloc_ordered_workqueue("kbase_vm_arb_wq",
WQ_HIGHPRI);
if (!arb_vm_state->vm_arb_wq) {
dev_err(kbdev->dev, "Failed to allocate vm_arb workqueue\n");
return -ENOMEM;
}
INIT_WORK(&arb_vm_state->vm_suspend_work, kbase_arbiter_pm_suspend_wq);
INIT_WORK(&arb_vm_state->vm_resume_work, kbase_arbiter_pm_resume_wq);
arb_vm_state->vm_arb_starting = false;
arb_vm_state->vm_arb_users_waiting = 0;
kbdev->pm.arb_vm_state = arb_vm_state;
err = kbase_arbif_init(kbdev);
if (err) {
goto arbif_init_fail;
}
if (kbdev->arb.arb_if) {
kbase_arbif_gpu_request(kbdev);
dev_dbg(kbdev->dev, "Waiting for initial GPU assignment...\n");
wait_event(arb_vm_state->vm_state_wait,
arb_vm_state->vm_state ==
KBASE_VM_STATE_INITIALIZING_WITH_GPU);
dev_dbg(kbdev->dev,
"Waiting for initial GPU assignment - done\n");
}
return 0;
arbif_init_fail:
destroy_workqueue(arb_vm_state->vm_arb_wq);
kfree(arb_vm_state);
kbdev->pm.arb_vm_state = NULL;
return err;
}
void kbase_arbiter_pm_early_term(struct kbase_device *kbdev)
{
struct kbase_arbiter_vm_state *arb_vm_state = kbdev->pm.arb_vm_state;
mutex_lock(&arb_vm_state->vm_state_lock);
if (arb_vm_state->vm_state > KBASE_VM_STATE_STOPPED_GPU_REQUESTED)
kbase_arbif_gpu_stopped(kbdev, false);
mutex_unlock(&arb_vm_state->vm_state_lock);
kbase_arbif_destroy(kbdev);
destroy_workqueue(arb_vm_state->vm_arb_wq);
arb_vm_state->vm_arb_wq = NULL;
kfree(kbdev->pm.arb_vm_state);
kbdev->pm.arb_vm_state = NULL;
}
void kbase_arbiter_pm_release_interrupts(struct kbase_device *kbdev)
{
struct kbase_arbiter_vm_state *arb_vm_state = kbdev->pm.arb_vm_state;
mutex_lock(&arb_vm_state->vm_state_lock);
if (!kbdev->arb.arb_if ||
arb_vm_state->vm_state >
KBASE_VM_STATE_STOPPED_GPU_REQUESTED)
kbase_release_interrupts(kbdev);
mutex_unlock(&arb_vm_state->vm_state_lock);
}
void kbase_arbiter_pm_vm_stopped(struct kbase_device *kbdev)
{
bool request_gpu = false;
struct kbase_arbiter_vm_state *arb_vm_state = kbdev->pm.arb_vm_state;
lockdep_assert_held(&arb_vm_state->vm_state_lock);
if (arb_vm_state->vm_arb_users_waiting > 0 &&
arb_vm_state->vm_state == KBASE_VM_STATE_STOPPING_IDLE)
kbase_arbiter_pm_vm_set_state(kbdev,
KBASE_VM_STATE_STOPPING_ACTIVE);
dev_dbg(kbdev->dev, "%s %s\n", __func__,
kbase_arbiter_pm_vm_state_str(arb_vm_state->vm_state));
kbase_release_interrupts(kbdev);
switch (arb_vm_state->vm_state) {
case KBASE_VM_STATE_STOPPING_ACTIVE:
request_gpu = true;
kbase_arbiter_pm_vm_set_state(kbdev,
KBASE_VM_STATE_STOPPED_GPU_REQUESTED);
break;
case KBASE_VM_STATE_STOPPING_IDLE:
kbase_arbiter_pm_vm_set_state(kbdev, KBASE_VM_STATE_STOPPED);
break;
case KBASE_VM_STATE_SUSPEND_PENDING:
kbase_arbiter_pm_vm_set_state(kbdev, KBASE_VM_STATE_SUSPENDED);
break;
default:
dev_warn(kbdev->dev, "unexpected pm_stop VM state %u",
arb_vm_state->vm_state);
break;
}
kbase_arbif_gpu_stopped(kbdev, request_gpu);
}
static void kbase_arbiter_pm_vm_gpu_start(struct kbase_device *kbdev)
{
struct kbase_arbiter_vm_state *arb_vm_state = kbdev->pm.arb_vm_state;
lockdep_assert_held(&arb_vm_state->vm_state_lock);
switch (arb_vm_state->vm_state) {
case KBASE_VM_STATE_INITIALIZING:
kbase_arbiter_pm_vm_set_state(kbdev,
KBASE_VM_STATE_INITIALIZING_WITH_GPU);
break;
case KBASE_VM_STATE_STOPPED_GPU_REQUESTED:
kbase_arbiter_pm_vm_set_state(kbdev, KBASE_VM_STATE_STARTING);
kbase_install_interrupts(kbdev);
queue_work(arb_vm_state->vm_arb_wq,
&arb_vm_state->vm_resume_work);
break;
case KBASE_VM_STATE_SUSPEND_WAIT_FOR_GRANT:
kbase_arbif_gpu_stopped(kbdev, false);
kbase_arbiter_pm_vm_set_state(kbdev, KBASE_VM_STATE_SUSPENDED);
break;
default:
dev_warn(kbdev->dev,
"GPU_GRANTED when not expected - state %s\n",
kbase_arbiter_pm_vm_state_str(arb_vm_state->vm_state));
break;
}
}
static void kbase_arbiter_pm_vm_gpu_stop(struct kbase_device *kbdev)
{
struct kbase_arbiter_vm_state *arb_vm_state = kbdev->pm.arb_vm_state;
lockdep_assert_held(&arb_vm_state->vm_state_lock);
if (arb_vm_state->vm_state == KBASE_VM_STATE_INITIALIZING_WITH_GPU) {
mutex_unlock(&arb_vm_state->vm_state_lock);
kbase_arbiter_pm_vm_wait_gpu_assignment(kbdev);
mutex_lock(&arb_vm_state->vm_state_lock);
}
switch (arb_vm_state->vm_state) {
case KBASE_VM_STATE_IDLE:
kbase_arbiter_pm_vm_set_state(kbdev,
KBASE_VM_STATE_STOPPING_IDLE);
queue_work(arb_vm_state->vm_arb_wq,
&arb_vm_state->vm_suspend_work);
break;
case KBASE_VM_STATE_ACTIVE:
kbase_arbiter_pm_vm_set_state(kbdev,
KBASE_VM_STATE_STOPPING_ACTIVE);
queue_work(arb_vm_state->vm_arb_wq,
&arb_vm_state->vm_suspend_work);
break;
case KBASE_VM_STATE_STARTING:
dev_dbg(kbdev->dev, "Got GPU_STOP event while STARTING.");
kbase_arbiter_pm_vm_set_state(kbdev,
KBASE_VM_STATE_STOPPING_ACTIVE);
if (arb_vm_state->vm_arb_starting)
queue_work(arb_vm_state->vm_arb_wq,
&arb_vm_state->vm_suspend_work);
break;
case KBASE_VM_STATE_SUSPEND_PENDING:
/* Suspend finishes with a stop so nothing else to do */
break;
default:
dev_warn(kbdev->dev, "GPU_STOP when not expected - state %s\n",
kbase_arbiter_pm_vm_state_str(arb_vm_state->vm_state));
break;
}
}
static void kbase_gpu_lost(struct kbase_device *kbdev)
{
struct kbase_arbiter_vm_state *arb_vm_state = kbdev->pm.arb_vm_state;
lockdep_assert_held(&arb_vm_state->vm_state_lock);
switch (arb_vm_state->vm_state) {
case KBASE_VM_STATE_STARTING:
case KBASE_VM_STATE_ACTIVE:
case KBASE_VM_STATE_IDLE:
dev_warn(kbdev->dev, "GPU lost in state %s",
kbase_arbiter_pm_vm_state_str(arb_vm_state->vm_state));
kbase_arbiter_pm_vm_gpu_stop(kbdev);
mutex_unlock(&arb_vm_state->vm_state_lock);
kbase_pm_handle_gpu_lost(kbdev);
mutex_lock(&arb_vm_state->vm_state_lock);
break;
case KBASE_VM_STATE_STOPPING_IDLE:
case KBASE_VM_STATE_STOPPING_ACTIVE:
case KBASE_VM_STATE_SUSPEND_PENDING:
dev_info(kbdev->dev, "GPU lost while stopping");
mutex_unlock(&arb_vm_state->vm_state_lock);
kbase_pm_handle_gpu_lost(kbdev);
mutex_lock(&arb_vm_state->vm_state_lock);
break;
case KBASE_VM_STATE_SUSPENDED:
case KBASE_VM_STATE_STOPPED:
case KBASE_VM_STATE_STOPPED_GPU_REQUESTED:
dev_info(kbdev->dev, "GPU lost while already stopped");
break;
case KBASE_VM_STATE_SUSPEND_WAIT_FOR_GRANT:
dev_info(kbdev->dev, "GPU lost while waiting to suspend");
kbase_arbiter_pm_vm_set_state(kbdev, KBASE_VM_STATE_SUSPENDED);
break;
default:
break;
}
}
static inline bool kbase_arbiter_pm_vm_os_suspend_ready_state(
struct kbase_device *kbdev)
{
switch (kbdev->pm.arb_vm_state->vm_state) {
case KBASE_VM_STATE_SUSPENDED:
case KBASE_VM_STATE_STOPPED:
case KBASE_VM_STATE_IDLE:
case KBASE_VM_STATE_ACTIVE:
return true;
default:
return false;
}
}
static void kbase_arbiter_pm_vm_os_prepare_suspend(struct kbase_device *kbdev)
{
struct kbase_arbiter_vm_state *arb_vm_state = kbdev->pm.arb_vm_state;
enum kbase_vm_state prev_state;
lockdep_assert_held(&arb_vm_state->vm_state_lock);
if (kbdev->arb.arb_if) {
if (kbdev->pm.arb_vm_state->vm_state ==
KBASE_VM_STATE_SUSPENDED)
return;
}
/* Block suspend OS function until we are in a stable state
* with vm_state_lock
*/
while (!kbase_arbiter_pm_vm_os_suspend_ready_state(kbdev)) {
prev_state = arb_vm_state->vm_state;
switch (arb_vm_state->vm_state) {
case KBASE_VM_STATE_STOPPING_ACTIVE:
case KBASE_VM_STATE_STOPPING_IDLE:
kbase_arbiter_pm_vm_set_state(kbdev,
KBASE_VM_STATE_SUSPEND_PENDING);
break;
case KBASE_VM_STATE_STOPPED_GPU_REQUESTED:
kbase_arbiter_pm_vm_set_state(kbdev,
KBASE_VM_STATE_SUSPEND_WAIT_FOR_GRANT);
break;
case KBASE_VM_STATE_STARTING:
if (!arb_vm_state->vm_arb_starting) {
kbase_arbiter_pm_vm_set_state(kbdev,
KBASE_VM_STATE_SUSPEND_PENDING);
kbase_arbiter_pm_vm_stopped(kbdev);
}
break;
default:
break;
}
mutex_unlock(&arb_vm_state->vm_state_lock);
wait_event(arb_vm_state->vm_state_wait,
arb_vm_state->vm_state != prev_state);
mutex_lock(&arb_vm_state->vm_state_lock);
}
switch (arb_vm_state->vm_state) {
case KBASE_VM_STATE_STOPPED:
kbase_arbiter_pm_vm_set_state(kbdev,
KBASE_VM_STATE_SUSPENDED);
break;
case KBASE_VM_STATE_IDLE:
case KBASE_VM_STATE_ACTIVE:
kbase_arbiter_pm_vm_set_state(kbdev,
KBASE_VM_STATE_SUSPEND_PENDING);
mutex_unlock(&arb_vm_state->vm_state_lock);
/* Ensure resume has completed fully before starting suspend */
flush_work(&arb_vm_state->vm_resume_work);
kbase_pm_driver_suspend(kbdev);
mutex_lock(&arb_vm_state->vm_state_lock);
break;
case KBASE_VM_STATE_SUSPENDED:
break;
default:
KBASE_DEBUG_ASSERT_MSG(false, "Unexpected state to suspend");
break;
}
}
static void kbase_arbiter_pm_vm_os_resume(struct kbase_device *kbdev)
{
struct kbase_arbiter_vm_state *arb_vm_state = kbdev->pm.arb_vm_state;
lockdep_assert_held(&arb_vm_state->vm_state_lock);
KBASE_DEBUG_ASSERT_MSG(arb_vm_state->vm_state ==
KBASE_VM_STATE_SUSPENDED,
"Unexpected state to resume");
kbase_arbiter_pm_vm_set_state(kbdev,
KBASE_VM_STATE_STOPPED_GPU_REQUESTED);
kbase_arbif_gpu_request(kbdev);
/* Release lock and block resume OS function until we have
* asynchronously received the GRANT message from the Arbiter and
* fully resumed
*/
mutex_unlock(&arb_vm_state->vm_state_lock);
kbase_arbiter_pm_vm_wait_gpu_assignment(kbdev);
flush_work(&arb_vm_state->vm_resume_work);
mutex_lock(&arb_vm_state->vm_state_lock);
}
void kbase_arbiter_pm_vm_event(struct kbase_device *kbdev,
enum kbase_arbif_evt evt)
{
struct kbase_arbiter_vm_state *arb_vm_state = kbdev->pm.arb_vm_state;
if (!kbdev->arb.arb_if)
return;
mutex_lock(&arb_vm_state->vm_state_lock);
dev_dbg(kbdev->dev, "%s %s\n", __func__,
kbase_arbiter_pm_vm_event_str(evt));
switch (evt) {
case KBASE_VM_GPU_GRANTED_EVT:
kbase_arbiter_pm_vm_gpu_start(kbdev);
break;
case KBASE_VM_GPU_STOP_EVT:
kbase_arbiter_pm_vm_gpu_stop(kbdev);
break;
case KBASE_VM_GPU_LOST_EVT:
dev_info(kbdev->dev, "KBASE_ARBIF_GPU_LOST_EVT!");
kbase_gpu_lost(kbdev);
break;
case KBASE_VM_OS_SUSPEND_EVENT:
kbase_arbiter_pm_vm_os_prepare_suspend(kbdev);
break;
case KBASE_VM_OS_RESUME_EVENT:
kbase_arbiter_pm_vm_os_resume(kbdev);
break;
case KBASE_VM_GPU_IDLE_EVENT:
switch (arb_vm_state->vm_state) {
case KBASE_VM_STATE_ACTIVE:
kbase_arbiter_pm_vm_set_state(kbdev,
KBASE_VM_STATE_IDLE);
kbase_arbif_gpu_idle(kbdev);
break;
default:
break;
}
break;
case KBASE_VM_REF_EVENT:
switch (arb_vm_state->vm_state) {
case KBASE_VM_STATE_STARTING:
KBASE_TLSTREAM_TL_EVENT_ARB_STARTED(kbdev, kbdev);
/* FALL THROUGH */
case KBASE_VM_STATE_IDLE:
kbase_arbiter_pm_vm_set_state(kbdev,
KBASE_VM_STATE_ACTIVE);
kbase_arbif_gpu_active(kbdev);
break;
case KBASE_VM_STATE_STOPPING_IDLE:
kbase_arbiter_pm_vm_set_state(kbdev,
KBASE_VM_STATE_STOPPING_ACTIVE);
break;
default:
break;
}
break;
case KBASE_VM_GPU_INITIALIZED_EVT:
lockdep_assert_held(&kbdev->pm.lock);
if (kbdev->pm.active_count > 0) {
kbase_arbiter_pm_vm_set_state(kbdev,
KBASE_VM_STATE_ACTIVE);
kbase_arbif_gpu_active(kbdev);
} else {
kbase_arbiter_pm_vm_set_state(kbdev,
KBASE_VM_STATE_IDLE);
kbase_arbif_gpu_idle(kbdev);
}
break;
default:
dev_alert(kbdev->dev, "Got Unknown Event!");
break;
}
mutex_unlock(&arb_vm_state->vm_state_lock);
}
static void kbase_arbiter_pm_vm_wait_gpu_assignment(struct kbase_device *kbdev)
{
struct kbase_arbiter_vm_state *arb_vm_state = kbdev->pm.arb_vm_state;
dev_dbg(kbdev->dev, "Waiting for GPU assignment...\n");
wait_event(arb_vm_state->vm_state_wait,
arb_vm_state->vm_state == KBASE_VM_STATE_IDLE ||
arb_vm_state->vm_state == KBASE_VM_STATE_ACTIVE);
dev_dbg(kbdev->dev, "Waiting for GPU assignment - done\n");
}
static inline bool kbase_arbiter_pm_vm_gpu_assigned_lockheld(
struct kbase_device *kbdev)
{
struct kbase_arbiter_vm_state *arb_vm_state = kbdev->pm.arb_vm_state;
lockdep_assert_held(&arb_vm_state->vm_state_lock);
return (arb_vm_state->vm_state == KBASE_VM_STATE_IDLE ||
arb_vm_state->vm_state == KBASE_VM_STATE_ACTIVE);
}
int kbase_arbiter_pm_ctx_active_handle_suspend(struct kbase_device *kbdev,
enum kbase_pm_suspend_handler suspend_handler)
{
struct kbasep_js_device_data *js_devdata = &kbdev->js_data;
struct kbase_arbiter_vm_state *arb_vm_state = kbdev->pm.arb_vm_state;
if (kbdev->arb.arb_if) {
mutex_lock(&arb_vm_state->vm_state_lock);
while (!kbase_arbiter_pm_vm_gpu_assigned_lockheld(kbdev)) {
/* Update VM state since we have GPU work to do */
if (arb_vm_state->vm_state ==
KBASE_VM_STATE_STOPPING_IDLE)
kbase_arbiter_pm_vm_set_state(kbdev,
KBASE_VM_STATE_STOPPING_ACTIVE);
else if (arb_vm_state->vm_state ==
KBASE_VM_STATE_STOPPED) {
kbase_arbiter_pm_vm_set_state(kbdev,
KBASE_VM_STATE_STOPPED_GPU_REQUESTED);
kbase_arbif_gpu_request(kbdev);
} else if (arb_vm_state->vm_state ==
KBASE_VM_STATE_INITIALIZING_WITH_GPU)
break;
if (suspend_handler !=
KBASE_PM_SUSPEND_HANDLER_NOT_POSSIBLE) {
if (suspend_handler ==
KBASE_PM_SUSPEND_HANDLER_VM_GPU_GRANTED
||
kbdev->pm.active_count > 0)
break;
mutex_unlock(&arb_vm_state->vm_state_lock);
mutex_unlock(&kbdev->pm.lock);
mutex_unlock(&js_devdata->runpool_mutex);
return 1;
}
if (arb_vm_state->vm_state ==
KBASE_VM_STATE_INITIALIZING_WITH_GPU)
break;
/* Need to synchronously wait for GPU assignment */
arb_vm_state->vm_arb_users_waiting++;
mutex_unlock(&arb_vm_state->vm_state_lock);
mutex_unlock(&kbdev->pm.lock);
mutex_unlock(&js_devdata->runpool_mutex);
kbase_arbiter_pm_vm_wait_gpu_assignment(kbdev);
mutex_lock(&js_devdata->runpool_mutex);
mutex_lock(&kbdev->pm.lock);
mutex_lock(&arb_vm_state->vm_state_lock);
arb_vm_state->vm_arb_users_waiting--;
}
mutex_unlock(&arb_vm_state->vm_state_lock);
}
return 0;
}
@@ -0,0 +1,159 @@
/*
*
* (C) COPYRIGHT ARM Limited. All rights reserved.
*
* This program is free software and is provided to you under the terms of the
* GNU General Public License version 2 as published by the Free Software
* Foundation, and any use by you of this program is subject to the terms
* of such GNU licence.
*
* 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 the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, you can access it online at
* http://www.gnu.org/licenses/gpl-2.0.html.
*
* SPDX-License-Identifier: GPL-2.0
*
*//* SPDX-License-Identifier: GPL-2.0 */
/*
*
* (C) COPYRIGHT 2019-2020 ARM Limited. All rights reserved.
*
* This program is free software and is provided to you under the terms of the
* GNU General Public License version 2 as published by the Free Software
* Foundation, and any use by you of this program is subject to the terms
* of such GNU license.
*
* 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 the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, you can access it online at
* http://www.gnu.org/licenses/gpl-2.0.html.
*
* SPDX-License-Identifier: GPL-2.0
*
*/
/**
* @file
* Mali arbiter power manager state machine and APIs
*/
#ifndef _MALI_KBASE_ARBITER_PM_H_
#define _MALI_KBASE_ARBITER_PM_H_
#include "mali_kbase_arbif.h"
/**
* enum kbase_vm_state - Current PM Arbitration state.
*
* @KBASE_VM_STATE_INITIALIZING: Special state before arbiter is initialized.
* @KBASE_VM_STATE_INITIALIZING_WITH_GPU: Initialization after GPU
* has been granted.
* @KBASE_VM_STATE_SUSPENDED: KBase is suspended by OS and GPU is not assigned.
* @KBASE_VM_STATE_STOPPED: GPU is not assigned to KBase and is not required.
* @KBASE_VM_STATE_STOPPED_GPU_REQUESTED: GPU is not assigned to KBase
* but a request has been made.
* @KBASE_VM_STATE_STARTING: GPU is assigned and KBase is getting ready to run.
* @KBASE_VM_STATE_IDLE: GPU is assigned but KBase has no work to do
* @KBASE_VM_STATE_ACTIVE: GPU is assigned and KBase is busy using it
* @KBASE_VM_STATE_SUSPEND_PENDING: OS is going into suspend mode.
* @KBASE_VM_STATE_SUSPEND_WAIT_FOR_GRANT: OS is going into suspend mode but GPU
* has already been requested.
* In this situation we must wait for
* the Arbiter to send a GRANTED message
* and respond immediately with
* a STOPPED message before entering
* the suspend mode.
* @KBASE_VM_STATE_STOPPING_IDLE: Arbiter has sent a stopped message and there
* is currently no work to do on the GPU.
* @KBASE_VM_STATE_STOPPING_ACTIVE: Arbiter has sent a stopped message when
* KBase has work to do.
*/
enum kbase_vm_state {
KBASE_VM_STATE_INITIALIZING,
KBASE_VM_STATE_INITIALIZING_WITH_GPU,
KBASE_VM_STATE_SUSPENDED,
KBASE_VM_STATE_STOPPED,
KBASE_VM_STATE_STOPPED_GPU_REQUESTED,
KBASE_VM_STATE_STARTING,
KBASE_VM_STATE_IDLE,
KBASE_VM_STATE_ACTIVE,
KBASE_VM_STATE_SUSPEND_PENDING,
KBASE_VM_STATE_SUSPEND_WAIT_FOR_GRANT,
KBASE_VM_STATE_STOPPING_IDLE,
KBASE_VM_STATE_STOPPING_ACTIVE
};
/**
* kbase_arbiter_pm_early_init() - Initialize arbiter for VM Paravirtualized use
* @kbdev: The kbase device structure for the device (must be a valid pointer)
*
* Initialize the arbiter and other required resources during the runtime
* and request the GPU for the VM for the first time.
*
* Return: 0 if successful, otherwise a standard Linux error code
*/
int kbase_arbiter_pm_early_init(struct kbase_device *kbdev);
/**
* kbase_arbiter_pm_early_term() - Shutdown arbiter and free resources.
* @kbdev: The kbase device structure for the device (must be a valid pointer)
*
* Clean up all the resources
*/
void kbase_arbiter_pm_early_term(struct kbase_device *kbdev);
/**
* kbase_arbiter_pm_release_interrupts() - Release the GPU interrupts
* @kbdev: The kbase device structure for the device (must be a valid pointer)
*
* Releases interrupts if needed (GPU is available) otherwise does nothing
*/
void kbase_arbiter_pm_release_interrupts(struct kbase_device *kbdev);
/**
* kbase_arbiter_pm_vm_event() - Dispatch VM event to the state machine
* @kbdev: The kbase device structure for the device (must be a valid pointer)
*
* The state machine function. Receives events and transitions states
* according the event received and the current state
*/
void kbase_arbiter_pm_vm_event(struct kbase_device *kbdev,
enum kbase_arbif_evt event);
/**
* kbase_arbiter_pm_ctx_active_handle_suspend() - Handle suspend operation for
* arbitration mode
* @kbdev: The kbase device structure for the device (must be a valid pointer)
* @suspend_handler: The handler code for how to handle a suspend
* that might occur
*
* This function handles a suspend event from the driver,
* communicating with the arbiter and waiting synchronously for the GPU
* to be granted again depending on the VM state.
*
* Return: 0 if success, 1 if failure due to system suspending/suspended
*/
int kbase_arbiter_pm_ctx_active_handle_suspend(struct kbase_device *kbdev,
enum kbase_pm_suspend_handler suspend_handler);
/**
* kbase_arbiter_pm_vm_stopped() - Handle stop event for the VM
* @kbdev: The kbase device structure for the device (must be a valid pointer)
*
* This function handles a stop event for the VM.
* It will update the VM state and forward the stop event to the driver.
*/
void kbase_arbiter_pm_vm_stopped(struct kbase_device *kbdev);
#endif /*_MALI_KBASE_ARBITER_PM_H_ */
@@ -0,0 +1,18 @@
# SPDX-License-Identifier: GPL-2.0
#
# (C) COPYRIGHT 2019-2020 ARM Limited. All rights reserved.
#
# This program is free software and is provided to you under the terms of the
# GNU General Public License version 2 as published by the Free Software
# Foundation, and any use by you of this program is subject to the terms
# of such GNU licence.
#
# A copy of the licence is included with the program, and can also be obtained
# from Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
# Boston, MA 02110-1301, USA.
#
#
obj-$(CONFIG_MALI_ARBITRATION) += mali_arb_plat/ mali_arbiter/
obj-$(CONFIG_MALI_ARBITRATION) += mali_arbiter/mali_arbif_xenback/
obj-$(CONFIG_MALI_ARBITRATION) += xen/mali_arbif_xenfront/
@@ -0,0 +1,23 @@
# SPDX-License-Identifier: GPL-2.0
#
# (C) COPYRIGHT 2012-2020 ARM Limited. All rights reserved.
#
# This program is free software and is provided to you under the terms of the
# GNU General Public License version 2 as published by the Free Software
# Foundation, and any use by you of this program is subject to the terms
# of such GNU licence.
#
# A copy of the licence is included with the program, and can also be obtained
# from Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
# Boston, MA 02110-1301, USA.
#
#
config MALI_ARBITRATION
bool "Enable Virtualization reference code"
depends on MALI_MIDGARD
default n
help
Enables the build of several reference modules used in the reference
virtualization setup for Mali
If unsure, say N.
@@ -0,0 +1,41 @@
#
# (C) COPYRIGHT 2020 ARM Limited. All rights reserved.
#
# This program is free software and is provided to you under the terms of the
# GNU General Public License version 2 as published by the Free Software
# Foundation, and any use by you of this program is subject to the terms
# of such GNU licence.
#
# A copy of the licence is included with the program, and can also be obtained
# from Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
# Boston, MA 02110-1301, USA.
#
#
config BSP_HAS_HYPERVISOR
bool "Kernel has a hypervisor enabled"
depends on BUILD_KERNEL_MODULES
default n
select MALI_ARBITER_SUPPORT
config MALI_HAS_ARBITER
bool "Build a reference arbiter"
default n
default y if BSP_HAS_HYPERVISOR
help
Build a daisy chain reference arbiter useful
to manage Mali to different Virtual Machines
If unsure, say N.
config BUILD_ARBITER_TESTS
bool "Kernel has arbiter tests enabled"
depends on BUILD_KERNEL_MODULES
default n
default y if MALI_HAS_ARBITER && UNIT_TEST_KERNEL_MODULES
config MALI_ARBITER_LOST_TEST
bool "Build the arbiter to aggressively test GPU_LOST"
depends on BUILD_KERNEL_MODULES && MALI_HAS_ARBITER
default n
@@ -0,0 +1,114 @@
/* SPDX-License-Identifier: GPL-2.0 OR MIT */
/*
* (C) COPYRIGHT 2020 Arm Limited or its affiliates. All rights reserved.
*/
/**
* @file
* Part of the Mali arbiter interface related to platform operations.
*/
#ifndef _MALI_ARB_PLAT_H_
#define _MALI_ARB_PLAT_H_
#define MALI_ARB_PLAT_DT_NAME "arm,mali-arbiter-platform"
struct mali_arb_plat_dev;
/**
* struct mali_arb_plat_arbiter_cb_ops - callback functions provided by the
* Arbiter for the platform integrator
*/
struct mali_arb_plat_arbiter_cb_ops {
/**
* dvfs_arb_get_utilisation() - Request GPU utilization
* @dev: the arbiter device (passed when we registered).
*
* Called by a platform integration device to request GPU
* utilization since previous call. The Arbiter should
* return the busytime and availtime since last call
*/
void (*dvfs_arb_get_utilisation)(struct device *dev, u32 *busytime,
u32 *availtime);
};
/**
* struct mali_arb_plat_ops - callback functions provided by the
* Arbiter for the platform integration device
*/
struct mali_arb_plat_ops {
/**
* mali_arb_plat_register() - Register an arbiter device
* @plat_dev: Platform integration device obtained using device tree
* @dev: The Arbiter device
* @arbiter_cb_ops: Arbiter Callbacks operation struct
*
* This function must be called before initializing the platform device
* The platform device will then ask the arbiter for utilization info
* to make decisions about frequency changes
*
* Return: 0 is successful, or a standard Linux error code
*/
int (*mali_arb_plat_register)(struct mali_arb_plat_dev *plat_dev,
struct device *dev,
struct mali_arb_plat_arbiter_cb_ops *arbiter_cb_ops);
/**
* mali_arb_plat_unregister() - Unregister an arbiter device
* @plat_dev: Platform integration device
*
* This function must be called as last operation on the platform device
* The platform device will then be unregistered by the system and
* all its resources cleaned up
*/
void (*mali_arb_plat_unregister)(struct mali_arb_plat_dev *plat_dev);
/**
* mali_arb_start_dvfs() - Register the device with devfreq framework
* @plat_dev: Platform integration device
*
* Read device tree and register with the devfreq framework
* see @kbase_devfreq_init and @power_control_init
*/
int (*mali_arb_start_dvfs)(struct mali_arb_plat_dev *plat_dev);
/**
* mali_arb_stop_dvfs() - Unregister the device with devfreq framework
* @plat_dev: Platform integration device
*
* Unregister the device from the devfreq framework and
* release the resources
*/
void (*mali_arb_stop_dvfs)(struct mali_arb_plat_dev *plat_dev);
/**
* mali_arb_gpu_power_on() - Power on the GPU
* @plat_dev: Platform integration device
*
* Enable the GPU power to the device
*/
void (*mali_arb_gpu_power_on)(struct mali_arb_plat_dev *plat_dev);
/**
* mali_arb_gpu_power_off() - Power off the GPU
* @plat_dev: The arbiter platform device
*
* Disable the GPU power of the device
*/
void (*mali_arb_gpu_power_off)(struct mali_arb_plat_dev *plats_dev);
};
/**
* struct mali_arb_plat_dev - Platform integration device data
* (use platform_get_drvdata)
* @plat_ops: Platform integration device functions.
*
* Struct which contain the arbiter platform device and used by the arbiter
* as interface to trigger platform specific (PM related) operations
*/
struct mali_arb_plat_dev {
struct mali_arb_plat_ops plat_ops;
};
#endif /* _MALI_ARB_PLAT_H_ */
@@ -0,0 +1,113 @@
/* SPDX-License-Identifier: GPL-2.0 */
/*
*
* (C) COPYRIGHT 2019-2020 ARM Limited. All rights reserved.
*
* This program is free software and is provided to you under the terms of the
* GNU General Public License version 2 as published by the Free Software
* Foundation, and any use by you of this program is subject to the terms
* of such GNU license.
*
* 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 the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, you can access it online at
* http://www.gnu.org/licenses/gpl-2.0.html.
*
*
*/
/**
* @file
* Part of the Mali reference arbiter
*/
#include <xen/interface/io/ring.h>
/**
* GPU Back-end/Front-end Common Data Structures & Macros
*/
#ifndef _MALI_ARBIF_XEN_H_
#define _MALI_ARBIF_XEN_H_
/* Request FE-->BE I need the GPU */
#define GUEST_GPU_REQUEST (0x10)
/* Request FE-->BE GPU is now suspended */
#define GUEST_GPU_SUSPEND_COMPLETE (0x11)
/* Request FE-->BE GPU has been reset and is suspended */
#define GUEST_GPU_RESET_COMPLETE (0x12)
/* Unknown command */
#define GUEST_ERROR (0x1F)
/* Response BE-->FE You have the GPU */
#define HOST_GPU_GRANTED (0x20)
/* Response BE-->FE Your time is up, (suspend now!)*/
#define HOST_GPU_SUSPEND (0x21)
/* Response BE-->FE Reset GPU now */
#define HOST_GPU_RESET (0x22)
/* Unknown command */
#define HOST_ERROR (0x2F)
/* temporary... */
#define VM_ARB_GPU_IDLE (0x23)
#define VM_ARB_GPU_ACTIVE (0x24)
#define VM_ARB_GPU_REQUEST GUEST_GPU_REQUEST
#define VM_ARB_GPU_STOPPED GUEST_GPU_SUSPEND_COMPLETE
#define ARB_VM_GPU_STOP HOST_GPU_SUSPEND
#define ARB_VM_GPU_GRANTED HOST_GPU_GRANTED
#define ARB_VM_GPU_LOST HOST_GPU_RESET
/* Be sure to bump this number if you change this file */
#define XEN_GPU_MAGIC "43"
/**
* STATUS RETURN CODES.
*/
/* Operation failed for some unspecified reason (-EIO). */
#define GPUIF_RSP_ERROR (-1)
/* Operation completed successfully. */
#define GPUIF_RSP_OKAY (0)
/**
* struct xengpu_request - Xen GPU command format
* @cmd: The command id, for example GPU_REQUEST
* @cmd_param0: optional parameter
* @cmd_param1: optional second parameter
* @cmd_param2: optional third parameter
*
* This struct is used to define the structure of the messages exchanged
* between front-end and back-end
*/
struct xengpu_request {
uint16_t cmd;
uint64_t cmd_param0;
uint64_t cmd_param1;
uint64_t cmd_param2;
} __attribute__((__packed__));
/**
* struct xengpu_response - Xen GPU response format
* @status: The status of the operation previously requested (unused)
*
* This struct is used to define the structure of the response messages
* returned by Xen for command sent
*/
struct xengpu_response {
uint16_t status;
} __attribute__((__packed__));
/**
* creates ring structures:
* 1. shared data structure, struct xengpu_sring (holds requests/responses)
* 2. private FE & BE variables, struct xengpu_front_sring,
* struct xengpu_back_sring
*/
DEFINE_RING_TYPES(xengpu, struct xengpu_request, struct xengpu_response);
#endif /* _MALI_ARBIF_XEN_H_ */
@@ -0,0 +1,51 @@
/* SPDX-License-Identifier: GPL-2.0 */
/*
*
* (C) COPYRIGHT 2019-2020 ARM Limited. All rights reserved.
*
* This program is free software and is provided to you under the terms of the
* GNU General Public License version 2 as published by the Free Software
* Foundation, and any use by you of this program is subject to the terms
* of such GNU license.
*
* 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 the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, you can access it online at
* http://www.gnu.org/licenses/gpl-2.0.html.
*
*
*/
/**
* @file
* Part of the Mali reference arbiter
*/
#ifndef _MALI_XEN_HYP_H_
#define _MALI_XEN_HYP_H_
#include <xen/interface/xen.h>
#define XENARBGPU_INTERFACE_VERSION 0x01000001
/**
* HYPERVISOR_arb_gpu_op(const struct xen_arb_gpu_op*);
*/
struct xen_arb_gpu_op {
uint32_t cmd;
domid_t domain;
uint32_t interface_version; /* XENARBGPU_INTERFACE_VERSION */
};
typedef struct xen_arb_gpu_op xen_arb_gpu_op_t;
DEFINE_GUEST_HANDLE_STRUCT(xen_arb_gpu_op_t);
#define ARB_HYP_ASSIGN_VM_GPU 8
#define ARB_HYP_FORCE_ASSIGN_VM_GPU 9
#endif /* _MALI_XEN_HYP_H_ */
@@ -0,0 +1,198 @@
GPLV2 LICENCE AGREEMENT FOR MALI GPUS LINUX KERNEL DEVICE DRIVERS SOURCE CODE
THE USE OF THE SOFTWARE ACCOMPANYING THIS DOCUMENT IS EXPRESSLY SUBJECT TO THE TERMS OF THE GNU GENERAL PUBLIC LICENSE VERSION 2 AS PUBLISHED BY THE FREE SOFTWARE FOUNDATION AND SET OUT BELOW FOR REFERENCE (?GPL LICENCE?). ARM IS ONLY WILLING TO DISTRIBUTE THE SOFTWARE TO YOU ON CONDITION THAT YOU ACCEPT ALL OF THE TERMS IN THE GPL LICENCE PRIOR TO MODIFYING OR DISTRIBUTING THE SOFTWARE.
Further for the period of three (3) years, ARM hereby offers to make available the source code of any part of the software program that is supplied as object code or in executable form.
GPL Licence
GNU GENERAL PUBLIC LICENSE
Version 2, June 1991
Copyright (C) 1989, 1991 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. Everyone is permitted to copy and distribute verbatim copies of this license document, but changing it is not allowed.
Preamble
The licenses for most software are designed to take away your freedom to share and change it. By contrast, the GNU General Public License is intended to guarantee your freedom to share and change free software--to make sure the software is free for all its users. This General Public License applies to most of the Free Software Foundation's software and to any other program whose authors commit to using it. (Some other Free Software Foundation software is covered by the GNU Lesser General Public License instead.) You can apply it to your programs, too.
When we speak of free software, we are referring to freedom, not price. Our General Public Licenses are designed to make sure that you have the freedom to distribute copies of free software (and charge for this service if you wish), that you receive source code or can get it if you want it, that you can change the software or use pieces of it in new free programs; and that you know you can do these things.
To protect your rights, we need to make restrictions that forbid anyone to deny you these rights or to ask you to surrender the rights. These restrictions translate to certain responsibilities for you if you distribute copies of the software, or if you modify it.
For example, if you distribute copies of such a program, whether gratis or for a fee, you must give the recipients all the rights that you have. You must make sure that they, too, receive or can get the source code. And you must show them these terms so they know their rights.
We protect your rights with two steps: (1) copyright the software, and (2) offer you this license which gives you legal permission to copy, distribute and/or modify the software.
Also, for each author's protection and ours, we want to make certain that everyone understands that there is no warranty for this free software. If the software is modified by someone else and passed on, we want its recipients to know that what they have is not the original, so that any problems introduced by others will not reflect on the original authors' reputations.
Finally, any free program is threatened constantly by software patents. We wish to avoid the danger that redistributors of a free program will individually obtain patent licenses, in effect making the program proprietary. To prevent this, we have made it clear that any patent must be licensed for everyone's free use or not licensed at all.
The precise terms and conditions for copying, distribution and modification follow.
GNU GENERAL PUBLIC LICENSE
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
0. This License applies to any program or other work which contains a notice placed by the copyright holder saying it may be distributed under the terms of this General Public License. The "Program", below, refers to any such program or work, and a "work based on the Program" means either the Program or any derivative work under copyright law: that is to say, a work containing the Program or a portion of it, either verbatim or with modifications and/or translated into another language. (Hereinafter, translation is included without limitation in the term "modification".) Each licensee is addressed as "you".
Activities other than copying, distribution and modification are not covered by this License; they are outside its scope. The act of running the Program is not restricted, and the output from the Program is covered only if its contents constitute a work based on the Program (independent of having been made by running the Program).
Whether that is true depends on what the Program does.
1. You may copy and distribute verbatim copies of the Program's source code as you receive it, in any medium, provided that you conspicuously and appropriately publish on each copy an appropriate copyright notice and disclaimer of warranty; keep intact all the notices that refer to this License and to the absence of any warranty;
and give any other recipients of the Program a copy of this License along with the Program.
You may charge a fee for the physical act of transferring a copy, and you may at your option offer warranty protection in exchange for a fee.
2. You may modify your copy or copies of the Program or any portion of it, thus forming a work based on the Program, and copy and distribute such modifications or work under the terms of Section 1 above, provided that you also meet all of these conditions:
a) You must cause the modified files to carry prominent notices stating that you changed the files and the date of any change.
b) You must cause any work that you distribute or publish, that in whole or in part contains or is derived from the Program or any part thereof, to be licensed as a whole at no charge to all third parties under the terms of this License.
c) If the modified program normally reads commands interactively when run, you must cause it, when started running for such interactive use in the most ordinary way, to print or display an announcement including an appropriate copyright notice and a notice that there is no warranty (or else, saying that you provide a warranty) and that users may redistribute the program under these conditions, and telling the user how to view a copy of this License. (Exception: if the Program itself is interactive but does not normally print such an announcement, your work based on the Program is not required to print an announcement.)
These requirements apply to the modified work as a whole. If identifiable sections of that work are not derived from the Program, and can be reasonably considered independent and separate works in themselves, then this License, and its terms, do not apply to those sections when you distribute them as separate works. But when you distribute the same sections as part of a whole which is a work based on the Program, the distribution of the whole must be on the terms of this License, whose permissions for other licensees extend to the entire whole, and thus to each and every part regardless of who wrote it.
Thus, it is not the intent of this section to claim rights or contest your rights to work written entirely by you; rather, the intent is to exercise the right to control the distribution of derivative or collective works based on the Program.
In addition, mere aggregation of another work not based on the Program with the Program (or with a work based on the Program) on a volume of a storage or distribution medium does not bring the other work under the scope of this License.
3. You may copy and distribute the Program (or a work based on it, under Section 2) in object code or executable form under the terms of Sections 1 and 2 above provided that you also do one of the following:
a) Accompany it with the complete corresponding machine-readable source code, which must be distributed under the terms of Sections 1 and 2 above on a medium customarily used for software interchange; or,
b) Accompany it with a written offer, valid for at least three years, to give any third party, for a charge no more than your cost of physically performing source distribution, a complete machine-readable copy of the corresponding source code, to be distributed under the terms of Sections 1 and 2 above on a medium customarily used for software interchange; or,
c) Accompany it with the information you received as to the offer to distribute corresponding source code. (This alternative is allowed only for noncommercial distribution and only if you received the program in object code or executable form with such an offer, in accord with Subsection b above.)
The source code for a work means the preferred form of the work for making modifications to it. For an executable work, complete source code means all the source code for all modules it contains, plus any associated interface definition files, plus the scripts used to control compilation and installation of the executable. However, as a special exception, the source code distributed need not include anything that is normally distributed (in either source or binary form) with the major components (compiler, kernel, and so on) of the operating system on which the executable runs, unless that component itself accompanies the executable.
If distribution of executable or object code is made by offering access to copy from a designated place, then offering equivalent access to copy the source code from the same place counts as distribution of the source code, even though third parties are not compelled to copy the source along with the object code.
4. You may not copy, modify, sublicense, or distribute the Program except as expressly provided under this License. Any attempt otherwise to copy, modify, sublicense or distribute the Program is void, and will automatically terminate your rights under this License. However, parties who have received copies, or rights, from you under this License will not have their licenses terminated so long as such parties remain in full compliance.
5. You are not required to accept this License, since you have not signed it. However, nothing else grants you permission to modify or distribute the Program or its derivative works. These actions are prohibited by law if you do not accept this License. Therefore, by modifying or distributing the Program (or any work based on the
Program), you indicate your acceptance of this License to do so, and all its terms and conditions for copying, distributing or modifying the Program or works based on it.
6. Each time you redistribute the Program (or any work based on the Program), the recipient automatically receives a license from the original licensor to copy, distribute or modify the Program subject to these terms and conditions. You may not impose any further restrictions on the recipients' exercise of the rights granted herein.
You are not responsible for enforcing compliance by third parties to this License.
7. If, as a consequence of a court judgment or allegation of patent infringement or for any other reason (not limited to patent issues), conditions are imposed on you (whether by court order, agreement or otherwise) that contradict the conditions of this License, they do not excuse you from the conditions of this License. If you cannot distribute so as to satisfy simultaneously your obligations under this License and any other pertinent obligations, then as a consequence you may not distribute the Program at all. For example, if a patent license would not permit royalty-free redistribution of the Program by all those who receive copies directly or indirectly through you, then the only way you could satisfy both it and this License would be to refrain entirely from distribution of the Program.
If any portion of this section is held invalid or unenforceable under any particular circumstance, the balance of the section is intended to apply and the section as a whole is intended to apply in other circumstances.
It is not the purpose of this section to induce you to infringe any patents or other property right claims or to contest validity of any such claims; this section has the sole purpose of protecting the integrity of the free software distribution system, which is implemented by public license practices. Many people have made generous contributions to the wide range of software distributed through that system in reliance on consistent application of that system; it is up to the author/donor to decide if he or she is willing to distribute software through any other system and a licensee cannot impose that choice.
This section is intended to make thoroughly clear what is believed to be a consequence of the rest of this License.
8. If the distribution and/or use of the Program is restricted in certain countries either by patents or by copyrighted interfaces, the original copyright holder who places the Program under this License may add an explicit geographical distribution limitation excluding those countries, so that distribution is permitted only in or among countries not thus excluded. In such case, this License incorporates the limitation as if written in the body of this License.
9. The Free Software Foundation may publish revised and/or new versions of the General Public License from time to time. Such new versions will be similar in spirit to the present version, but may differ in detail to address new problems or concerns.
Each version is given a distinguishing version number. If the Program specifies a version number of this License which applies to it and "any later version", you have the option of following the terms and conditions either of that version or of any later version published by the Free Software Foundation. If the Program does not specify a version number of this License, you may choose any version ever published by the Free Software Foundation.
10. If you wish to incorporate parts of the Program into other free programs whose distribution conditions are different, write to the author to ask for permission. For software which is copyrighted by the Free Software Foundation, write to the Free Software Foundation; we sometimes make exceptions for this. Our decision will be guided by the two goals of preserving the free status of all derivatives of our free software and of promoting the sharing and reuse of software generally.
NO WARRANTY
11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
/end
@@ -0,0 +1,15 @@
#
# (C) COPYRIGHT 2019-2020 ARM Limited. All rights reserved.
#
# This program is free software and is provided to you under the terms of the
# GNU General Public License version 2 as published by the Free Software
# Foundation, and any use by you of this program is subject to the terms
# of such GNU licence.
#
# A copy of the licence is included with the program, and can also be obtained
# from Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
# Boston, MA 02110-1301, USA.
#
#
obj-$(CONFIG_MALI_ARBITRATION) := mali_arb_plat.o
@@ -0,0 +1,30 @@
#
# (C) COPYRIGHT 2019-2020 ARM Limited. All rights reserved.
#
# This program is free software and is provided to you under the terms of the
# GNU General Public License version 2 as published by the Free Software
# Foundation, and any use by you of this program is subject to the terms
# of such GNU licence.
#
# A copy of the licence is included with the program, and can also be obtained
# from Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
# Boston, MA 02110-1301, USA.
#
#
# linux build system bootstrap for out-of-tree module
# default to building for the host
ARCH ?= $(shell uname -m)
ifeq ($(KDIR),)
$(error Must specify KDIR to point to the kernel to target))
endif
all: mali_arb_plat
mali_arb_plat:
$(MAKE) ARCH=$(ARCH) -C $(KDIR) M=$(CURDIR) EXTRA_CFLAGS="-I$(CURDIR)/../include" modules
clean:
$(MAKE) ARCH=$(ARCH) -C $(KDIR) M=$(CURDIR) clean
@@ -0,0 +1,27 @@
/*
* Copyright:
* ----------------------------------------------------------------------------
* This confidential and proprietary software may be used only as authorized
* by a licensing agreement from ARM Limited.
* (C) COPYRIGHT 2019-2020 ARM Limited, ALL RIGHTS RESERVED
* The entire notice above must be reproduced on all authorized copies and
* copies may only be made to the extent permitted by a licensing agreement
* from ARM Limited.
* ----------------------------------------------------------------------------
*/
bob_kernel_module {
name: "mali_arb_plat",
srcs: [
"Kbuild",
"mali_arb_plat.c",
],
enabled: false,
mali_has_arbiter: {
enabled: true,
},
add_to_alias: [
"kernel",
],
defaults: ["mali_arbitration_shared_config_defaults"],
}
@@ -0,0 +1,743 @@
// SPDX-License-Identifier: GPL-2.0
/*
*
* (C) COPYRIGHT 2019-2020 ARM Limited. All rights reserved.
*
* This program is free software and is provided to you under the terms of the
* GNU General Public License version 2 as published by the Free Software
* Foundation, and any use by you of this program is subject to the terms
* of such GNU license.
*
* 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 the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, you can access it online at
* http://www.gnu.org/licenses/gpl-2.0.html.
*
*
*/
/**
* @file
* Part of the Mali arbiter interface related to platform operations.
*/
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/slab.h>
#include <linux/timer.h>
#include <linux/platform_device.h>
#include <linux/of.h>
#include <linux/devfreq.h>
#include <linux/clk.h>
#include <linux/pm_opp.h>
#include <linux/version.h>
#include <linux/regulator/consumer.h>
#include <linux/pm_runtime.h>
#include <linux/mutex.h>
#include <gpu/mali_arb_plat.h>
/**
* How long pm runtime should wait in milliseconds before suspending GPU
*/
#define AUTO_SUSPEND_DELAY (100)
/**
* struct mali_arb_plat - Internal DVFS Integrator state information
* @mutex: Protect DVFS callback and arbiter callback threads
* @arb_plat_dev: Embedded public DVFS integrator device data.
* @dev: Device for DVFS integrator
* @arbiter_dev: The arbiter device that we can query about GPU
* utilization
* @arbiter_ops: callback(s) for communicating with arbiter,
* to query utilization
* @clock: Pointer to the input clock resource
* (having an id of 0),
* referenced by the GPU device node.
* @devfreq_profile: devfreq profile for the Mali GPU device,
* passed to devfreq_add_device()
* to add devfreq feature to Mali GPU device.
* @devfreq: Pointer to devfreq structure for Mali GPU device,
* returned on the call to devfreq_add_device().
* @current_freq: The real frequency, corresponding
* to @current_nominal_freq, at which the
* Mali GPU device is currently operating, as
* retrieved from @opp_table in the target callback of
* @devfreq_profile.
* @current_nominal_freq: The nominal frequency currently used for
* the Mali GPU device as retrieved through
* devfreq_recommended_opp() using the freq value
* passed as an argument to target callback
* of @devfreq_profile
* @current_voltage: The voltage corresponding to @current_nominal_freq,
* as retrieved through dev_pm_opp_get_voltage().
* @supports_power_mgmt: Whether or not the platform supports
* power management (for example has power domains)
*
* Structure used to handle DVFS information
*/
struct mali_arb_plat {
struct mutex mutex;
struct mali_arb_plat_dev arb_plat_dev;
struct device *dev;
struct device *arbiter_dev;
struct mali_arb_plat_arbiter_cb_ops arbiter_ops;
struct devfreq *devfreq;
struct devfreq_dev_profile devfreq_profile;
struct clk *clock;
unsigned long current_freq;
unsigned long current_nominal_freq;
unsigned long current_voltage;
#ifdef CONFIG_REGULATOR
struct regulator *regulator;
#endif
bool supports_power_mgmt;
};
/**
* arb_plat_from_pdev() - Convert arb_plat_dev to arb_plat
* @arb_plat_dev: The arbiter platform device
*
* Extract a pointer to the struct mali_arb_plat from the mali_arb_plat_dev
*
* Return: arb_plat or NULL if input parameter was NULL
*/
static inline struct mali_arb_plat *arb_plat_from_pdev(
struct mali_arb_plat_dev *arb_plat_dev)
{
if (likely(arb_plat_dev))
return container_of(arb_plat_dev, struct mali_arb_plat,
arb_plat_dev);
return NULL;
}
/**
* devfreq_target() - Modify the target frequency of the device
* @dev: Device of which the frequency should be updated
* @target_freq: Minimum frequency that should be set into the device
* @flags: Additional flags
*
* This function should pick a frequency at least as high as the passed
* in *target_freq, then update *target_freq to reflect the actual
* frequency chosen
*
* Return: 0 if success or an error code
*/
static int devfreq_target(struct device *dev, unsigned long *target_freq,
u32 flags)
{
int err;
unsigned long new_freq = 0;
struct dev_pm_opp *new_opp;
unsigned long new_voltage;
struct mali_arb_plat *arb_plat =
arb_plat_from_pdev(dev_get_drvdata(dev));
#if KERNEL_VERSION(4, 11, 0) > LINUX_VERSION_CODE
rcu_read_lock();
#endif
new_opp = devfreq_recommended_opp(dev, target_freq, flags);
new_voltage = dev_pm_opp_get_voltage(new_opp);
new_freq = dev_pm_opp_get_freq(new_opp);
#if KERNEL_VERSION(4, 11, 0) > LINUX_VERSION_CODE
rcu_read_unlock();
#endif
if (IS_ERR_OR_NULL(new_opp)) {
dev_err(dev, "Failed to get opp %ld\n", PTR_ERR(new_opp));
return PTR_ERR(new_opp);
}
#if KERNEL_VERSION(4, 11, 0) <= LINUX_VERSION_CODE
dev_pm_opp_put(new_opp);
#endif
#if KERNEL_VERSION(4, 11, 0) > LINUX_VERSION_CODE
rcu_read_lock();
rcu_read_unlock();
#endif
/* Only update if there is a change of frequency */
if (arb_plat->current_nominal_freq == new_freq) {
*target_freq = new_freq;
return 0;
}
#ifdef CONFIG_REGULATOR
if (arb_plat->regulator && arb_plat->current_voltage != new_voltage
&& arb_plat->current_freq < new_freq) {
err = regulator_set_voltage(arb_plat->regulator,
new_voltage, new_voltage);
if (err) {
dev_err(dev, "Failed to increase voltage %d\n", err);
return err;
}
arb_plat->current_voltage = new_voltage;
dev_info(dev, "voltage set to %lu", new_voltage);
}
#endif
err = clk_set_rate(arb_plat->clock, new_freq);
if (err) {
dev_err(arb_plat->dev,
"arb_plat: clk_set_rate returned error %d\n", err);
return err;
}
if (arb_plat->current_nominal_freq != new_freq) {
dev_info(arb_plat->dev,
"arb_plat: clk_set_rate to %lu\n", new_freq);
arb_plat->current_nominal_freq = new_freq;
}
#ifdef CONFIG_REGULATOR
if (arb_plat->regulator && arb_plat->current_voltage != new_voltage
&& arb_plat->current_freq > new_freq) {
err = regulator_set_voltage(arb_plat->regulator, new_voltage,
new_voltage);
if (err) {
dev_err(dev, "Failed to decrease voltage %d\n", err);
return err;
}
arb_plat->current_voltage = new_voltage;
dev_info(dev, "voltage set to %lu", new_voltage);
}
#endif
return 0;
}
static int devfreq_status(struct device *dev, struct devfreq_dev_status *stat)
{
struct mali_arb_plat *arb_plat = arb_plat_from_pdev(
dev_get_drvdata(dev));
u32 busy_time, total_time;
mutex_lock(&arb_plat->mutex);
stat->current_frequency = arb_plat->current_nominal_freq;
mutex_unlock(&arb_plat->mutex);
/* report busy time since last call */
arb_plat->arbiter_ops.dvfs_arb_get_utilisation(arb_plat->arbiter_dev,
&busy_time, &total_time);
mutex_lock(&arb_plat->mutex);
stat->busy_time = busy_time;
stat->total_time = total_time;
dev_dbg(arb_plat->dev, "arb_plat: %s busy_time = %lu, total_time = %lu (~ %lu%%)\n",
__func__, stat->busy_time, stat->total_time,
(stat->busy_time*100)/stat->total_time);
stat->current_frequency = arb_plat->current_nominal_freq;
stat->private_data = NULL;
mutex_unlock(&arb_plat->mutex);
return 0;
}
static void devfreq_exit(struct device *dev)
{
struct mali_arb_plat *arb_plat = arb_plat_from_pdev(
dev_get_drvdata(dev));
struct devfreq_dev_profile *dp;
dev_info(dev, "arb_plat: %s\n", __func__);
dp = &arb_plat->devfreq_profile;
if (dp && dp->freq_table) {
devm_kfree(dev, dp->freq_table);
dp->freq_table = NULL;
}
}
static int devfreq_cur_freq(struct device *dev, unsigned long *freq)
{
struct mali_arb_plat *arb_plat = arb_plat_from_pdev(
dev_get_drvdata(dev));
dev_dbg(arb_plat->dev, "arb_plat: %s %lu\n", __func__,
arb_plat->current_nominal_freq);
mutex_lock(&arb_plat->mutex);
*freq = arb_plat->current_nominal_freq;
mutex_unlock(&arb_plat->mutex);
return 0;
}
static int register_arb_dev(struct mali_arb_plat_dev *arb_plat_dev,
struct device *dev, struct mali_arb_plat_arbiter_cb_ops *ops)
{
struct mali_arb_plat *arb_plat = arb_plat_from_pdev(arb_plat_dev);
dev_info(arb_plat->dev, "arb_plat: %s\n", __func__);
if (!arb_plat || !dev || !ops)
return -EFAULT;
mutex_lock(&arb_plat->mutex);
/* we'll pass this when we call arbiter to ask for utilization. */
arb_plat->arbiter_dev = dev;
memcpy(&arb_plat->arbiter_ops, ops, sizeof(arb_plat->arbiter_ops));
mutex_unlock(&arb_plat->mutex);
return 0;
}
static void unregister_arb_dev(struct mali_arb_plat_dev *arb_plat_dev)
{
struct mali_arb_plat *arb_plat = arb_plat_from_pdev(arb_plat_dev);
dev_info(arb_plat->dev, "arb_plat: %s\n", __func__);
mutex_lock(&arb_plat->mutex);
arb_plat->arbiter_dev = NULL;
mutex_unlock(&arb_plat->mutex);
}
static int devfreq_init_freq_table_locked(struct mali_arb_plat *arb_plat,
struct devfreq_dev_profile *dp)
{
int count;
int i = 0;
unsigned long freq;
struct dev_pm_opp *opp;
#if KERNEL_VERSION(4, 11, 0) > LINUX_VERSION_CODE
rcu_read_lock();
#endif
count = dev_pm_opp_get_opp_count(arb_plat->dev);
dev_dbg(arb_plat->dev, "opp count = %d", count);
#if KERNEL_VERSION(4, 11, 0) > LINUX_VERSION_CODE
rcu_read_unlock();
#endif
if (count < 0)
return count;
dp->freq_table = devm_kmalloc_array(arb_plat->dev, count,
sizeof(dp->freq_table[0]), GFP_KERNEL);
if (!dp->freq_table)
return -ENOMEM;
#if KERNEL_VERSION(4, 11, 0) > LINUX_VERSION_CODE
rcu_read_lock();
#endif
for (i = 0, freq = ULONG_MAX; i < count; i++, freq--) {
opp = dev_pm_opp_find_freq_floor(arb_plat->dev, &freq);
if (IS_ERR(opp))
break;
#if KERNEL_VERSION(4, 11, 0) <= LINUX_VERSION_CODE
dev_pm_opp_put(opp);
#endif
dp->freq_table[i] = freq;
}
#if KERNEL_VERSION(4, 11, 0) > LINUX_VERSION_CODE
rcu_read_unlock();
#endif
if (count != i) {
dev_warn(arb_plat->dev,
"Unable to enumerate all OPPs %d!=%d\n", count, i);
}
dp->max_state = i;
return 0;
}
/**
* start_dvfs() - Register the device with devfreq framework
* @arb_plat_dev: The arbiter platform device
*
* Read device tree and register with the devfreq framework
* see @kbase_devfreq_init and @power_control_init in the DDK
*
* Return: 0 if success or an error code
*/
static int start_dvfs(struct mali_arb_plat_dev *arb_plat_dev)
{
int err;
struct devfreq_dev_profile *dp;
struct mali_arb_plat *arb_plat = arb_plat_from_pdev(arb_plat_dev);
dev_dbg(arb_plat->dev, "%s\n", __func__);
if (!arb_plat) {
pr_alert("arb_plat_from_pdev returned NULL");
return -EFAULT;
}
mutex_lock(&arb_plat->mutex);
if (!arb_plat->clock) {
dev_err(arb_plat->dev, "Clock not available for devfreq\n");
err = 0;
goto cleanup_mutex;
}
arb_plat->current_freq = clk_get_rate(arb_plat->clock);
dev_info(arb_plat->dev, "%s - current_freq = %lu\n", __func__,
arb_plat->current_freq);
arb_plat->current_nominal_freq = arb_plat->current_freq;
dp = &arb_plat->devfreq_profile;
dp->initial_freq = arb_plat->current_nominal_freq;
dp->polling_ms = 100; /* DEFAULT_PM_DVFS_PERIOD */
dp->target = devfreq_target;
dp->get_dev_status = devfreq_status;
dp->get_cur_freq = devfreq_cur_freq;
dp->exit = devfreq_exit;
if (devfreq_init_freq_table_locked(arb_plat, dp)) {
dev_err(arb_plat->dev,
"devfreq_init_freq_table_locked failed\n");
err = -EFAULT;
goto cleanup_mutex;
}
arb_plat->devfreq = devfreq_add_device(arb_plat->dev, dp,
"simple_ondemand", NULL);
if (IS_ERR(arb_plat->devfreq)) {
err = PTR_ERR(arb_plat->devfreq);
goto cleanup_freqtable;
}
dev_dbg(arb_plat->dev, "%s - added devfreq device\n", __func__);
err = devfreq_register_opp_notifier(arb_plat->dev, arb_plat->devfreq);
if (err) {
dev_err(arb_plat->dev,
"Failed to register OPP notifier %d\n", err);
goto cleanup_device;
}
dev_dbg(arb_plat->dev,
"arb_plat: init - registered devfreq device as opp notifier\n");
mutex_unlock(&arb_plat->mutex);
return 0;
cleanup_device:
if (devfreq_remove_device(arb_plat->devfreq))
dev_warn(arb_plat->dev, "Failed to terminate devfreq %d\n",
err);
arb_plat->devfreq = NULL;
cleanup_freqtable:
dev_set_drvdata(arb_plat->dev, NULL);
if (dp->freq_table) {
devm_kfree(arb_plat->dev, dp->freq_table);
dp->freq_table = NULL;
}
cleanup_mutex:
mutex_unlock(&arb_plat->mutex);
return err;
}
/**
* stop_dvfs() - Unregister the device with devfreq framework
* @arb_plat_dev: The arbiter platform device
*
* Unregister the device from the devfreq framework and release the resources
*/
static void stop_dvfs(struct mali_arb_plat_dev *arb_plat_dev)
{
int err;
struct devfreq_dev_profile *dp;
struct mali_arb_plat *arb_plat = arb_plat_from_pdev(arb_plat_dev);
dev_dbg(arb_plat->dev, "Term Mali Arbiter devfreq\n");
if (!arb_plat->clock)
return;
mutex_lock(&arb_plat->mutex);
devfreq_unregister_opp_notifier(arb_plat->dev, arb_plat->devfreq);
err = devfreq_remove_device(arb_plat->devfreq);
if (err)
dev_err(arb_plat->dev, "Failed to terminate devfreq %d\n",
err);
arb_plat->devfreq = NULL;
dp = &arb_plat->devfreq_profile;
if (dp->freq_table) {
devm_kfree(arb_plat->dev, dp->freq_table);
dp->freq_table = NULL;
}
mutex_unlock(&arb_plat->mutex);
}
/**
* power_on() - Power on the GPU
* @arb_plat_dev: The arbiter platform device
*
* Enable the GPU power to the device
*/
static void power_on(struct mali_arb_plat_dev *arb_plat_dev)
{
int err;
struct mali_arb_plat *arb_plat = arb_plat_from_pdev(arb_plat_dev);
if (!arb_plat->supports_power_mgmt)
return;
dev_dbg(arb_plat->dev, "Enabling GPU power...\n");
mutex_lock(&arb_plat->mutex);
err = clk_enable(arb_plat->clock);
if (err < 0)
dev_err(arb_plat->dev, "clk_enable returned %d\n",
err);
else
dev_dbg(arb_plat->dev, "clk_enable returned %d\n",
err);
err = pm_runtime_get_sync(arb_plat->dev);
if (err < 0)
dev_err(arb_plat->dev, "pm_runtime_get_sync returned %d\n",
err);
else
dev_dbg(arb_plat->dev, "pm_runtime_get_sync returned %d\n",
err);
mutex_unlock(&arb_plat->mutex);
}
/**
* power_off() - Power off the GPU
* @arb_plat_dev: The arbiter platform device
*
* Disable the GPU power of the device
*/
static void power_off(struct mali_arb_plat_dev *arb_plat_dev)
{
int err;
struct mali_arb_plat *arb_plat = arb_plat_from_pdev(arb_plat_dev);
if (!arb_plat->supports_power_mgmt)
return;
dev_dbg(arb_plat->dev, "Disabling GPU power...\n");
mutex_lock(&arb_plat->mutex);
pm_runtime_mark_last_busy(arb_plat->dev);
err = pm_runtime_put_autosuspend(arb_plat->dev);
if (err < 0)
dev_err(arb_plat->dev,
"pm_runtime_put_autosuspend returned %d\n", err);
else
dev_dbg(arb_plat->dev,
"pm_runtime_put_autosuspend returned %d\n", err);
clk_disable(arb_plat->clock);
mutex_unlock(&arb_plat->mutex);
}
/**
* arb_plat_probe() - Called when device is matched in device tree
* @pdev: Platform device to probe
*
* Register the device in the system and initialize several power
* related resources
*
* Return: 0 if success or an error code
*/
static int arb_plat_probe(struct platform_device *pdev)
{
int err;
struct device *dev = &pdev->dev;
struct mali_arb_plat *arb_plat;
dev_info(&pdev->dev, "%s\n", __func__);
arb_plat = devm_kzalloc(dev, sizeof(struct mali_arb_plat), GFP_KERNEL);
if (!arb_plat)
return -ENOMEM;
arb_plat->dev = dev;
mutex_init(&arb_plat->mutex);
#if defined(CONFIG_OF) && defined(CONFIG_REGULATOR)
arb_plat->regulator = regulator_get_optional(&pdev->dev, "mali");
if (IS_ERR_OR_NULL(arb_plat->regulator)) {
err = PTR_ERR(arb_plat->regulator);
arb_plat->regulator = NULL;
if (err == -EPROBE_DEFER) {
dev_err(&pdev->dev, "Failed to get regulator\n");
goto fail;
}
dev_info(arb_plat->dev,
"Continuing without Mali regulator control\n");
}
dev_info(arb_plat->dev,
"Arbiter doing Mali regulator control\n");
#endif /* defined(CONFIG_OF) && defined(CONFIG_REGULATOR) */
#if defined(CONFIG_OF)
/* find the GPU clock via Device Tree and enable */
arb_plat->clock = of_clk_get(arb_plat->dev->of_node, 0);
if (IS_ERR_OR_NULL(arb_plat->clock)) {
err = PTR_ERR(arb_plat->clock);
arb_plat->clock = NULL;
if (err == -EPROBE_DEFER) {
dev_err(&pdev->dev, "Failed to get clock\n");
goto fail;
}
dev_info(arb_plat->dev,
"Continuing without Mali clock control\n");
} else {
err = clk_prepare(arb_plat->clock);
if (err) {
dev_err(arb_plat->dev, "Failed to prepare clock %d\n",
err);
goto fail;
}
}
/* Initialize OPP table from Device Tree */
if (dev_pm_opp_of_add_table(arb_plat->dev))
dev_warn(arb_plat->dev,
"Invalid operating-points in device tree.\n");
else
dev_info(arb_plat->dev,
"arb_plat: opp table initialised from device tree operating-points.\n");
/* Discover whether power management is supported
* by looking for power domain in Device Tree.
*/
arb_plat->supports_power_mgmt = of_find_property(arb_plat->dev->of_node,
"power-domains", NULL);
if (arb_plat->supports_power_mgmt) {
dev_info(arb_plat->dev,
"Platform supports power management.\n");
pm_runtime_set_autosuspend_delay(dev, AUTO_SUSPEND_DELAY);
pm_runtime_use_autosuspend(dev);
pm_runtime_set_active(dev);
pm_runtime_enable(dev);
if (!pm_runtime_enabled(dev))
dev_warn(dev, "pm_runtime not enabled!");
} else
dev_info(arb_plat->dev,
"Platform does not support power management.\n");
#else
arb_plat->clock = NULL;
arb_plat->supports_power_mgmt = false;
#endif
arb_plat->arb_plat_dev.plat_ops.mali_arb_plat_register =
register_arb_dev;
arb_plat->arb_plat_dev.plat_ops.mali_arb_plat_unregister =
unregister_arb_dev;
arb_plat->arb_plat_dev.plat_ops.mali_arb_start_dvfs = start_dvfs;
arb_plat->arb_plat_dev.plat_ops.mali_arb_stop_dvfs = stop_dvfs;
arb_plat->arb_plat_dev.plat_ops.mali_arb_gpu_power_on = power_on;
arb_plat->arb_plat_dev.plat_ops.mali_arb_gpu_power_off = power_off;
platform_set_drvdata(pdev, &arb_plat->arb_plat_dev);
return 0;
fail:
devm_kfree(&pdev->dev, arb_plat);
return err;
}
/**
* arb_plat_cleanup() - Free up DVFS resources
* @arb_plat: The arbiter platform device to cleanup
*
* Called to free up and cleanup all the DVFS resources
*/
static void arb_plat_cleanup(struct mali_arb_plat *arb_plat)
{
struct devfreq_dev_profile *dp;
dev_dbg(arb_plat->dev, "%s\n", __func__);
mutex_lock(&arb_plat->mutex);
#if defined(CONFIG_OF)
dev_pm_opp_of_remove_table(arb_plat->dev);
clk_unprepare(arb_plat->clock);
dp = &arb_plat->devfreq_profile;
if (dp->freq_table) {
devm_kfree(arb_plat->dev, dp->freq_table);
dp->freq_table = NULL;
}
#endif
pm_runtime_disable(arb_plat->dev);
mutex_unlock(&arb_plat->mutex);
}
/**
* arb_plat_remove() - Remove the platform device
* @pdev: Platform device
*
* Called when device is removed
*/
static int arb_plat_remove(struct platform_device *pdev)
{
struct mali_arb_plat_dev *arb_plat_dev;
arb_plat_dev = platform_get_drvdata(pdev);
platform_set_drvdata(pdev, NULL);
if (arb_plat_dev) {
struct mali_arb_plat *arb_plat =
arb_plat_from_pdev(arb_plat_dev);
arb_plat_cleanup(arb_plat);
devm_kfree(&pdev->dev, arb_plat);
}
dev_info(&pdev->dev, "dvfs_integrator_remove done\n");
return 0;
}
/**
* @arb_plat_dt_match: Match the platform device with the Device Tree.
*/
static const struct of_device_id arb_plat_dt_match[] = {
{ .compatible = MALI_ARB_PLAT_DT_NAME },
{/* sentinel */}
};
/**
* @arb_plat_driver: Platform driver data.
*/
static struct platform_driver arb_plat_driver = {
.probe = arb_plat_probe,
.remove = arb_plat_remove,
.driver = {
.name = "mali arbiter platform driver",
.of_match_table = arb_plat_dt_match,
},
};
/**
* arb_plat_init() - Register platform driver
*
* Register the platform driver during the init process
*/
static int __init arb_plat_init(void)
{
return platform_driver_register(&arb_plat_driver);
}
module_init(arb_plat_init);
/**
* arb_plat_exit() - Unregister platform driver
*
* Unregister the platform driver when no longer needed
*/
static void __exit arb_plat_exit(void)
{
platform_driver_unregister(&arb_plat_driver);
}
module_exit(arb_plat_exit);
MODULE_LICENSE("GPL");
MODULE_ALIAS("mali-arbiter-plat-integration-driver");
@@ -0,0 +1,18 @@
# SPDX-License-Identifier: GPL-2.0 OR MIT
#
# (C) COPYRIGHT 2020 Arm Limited or its affiliates. All rights reserved.
#
#
obj-$(CONFIG_MALI_ARBITRATION) := mali_arbiter.o
# Set up defaults if not defined by build system
MALI_ARBITER_TEST_API ?= 0
# Set up our defines, which will be passed to gcc
DEFINES = \
-DMALI_ARBITER_TEST_API=$(MALI_ARBITER_TEST_API)
# Use our defines when compiling
ccflags-y += $(DEFINES)
subdir-ccflags-y += $(DEFINES)
@@ -0,0 +1,13 @@
# SPDX-License-Identifier: GPL-2.0 OR MIT
#
# (C) COPYRIGHT 2020 Arm Limited or its affiliates. All rights reserved.
#
#
config MALI_ARBITRATION
tristate "Enable compilation of arbitration modules"
default n
default y if MALI_ARBITER_SUPPORT
help
This option enables the compilation of arbitration modules needed to
to create a virtualized setup.
@@ -0,0 +1,22 @@
# SPDX-License-Identifier: GPL-2.0 OR MIT
#
# (C) COPYRIGHT 2020 Arm Limited or its affiliates. All rights reserved.
#
#
# linux build system bootstrap for out-of-tree module
# default to building for the host
ARCH ?= $(shell uname -m)
ifeq ($(KDIR),)
$(error Must specify KDIR to point to the kernel to target))
endif
all: mali_arbiter
mali_arbiter:
$(MAKE) ARCH=$(ARCH) -C $(KDIR) M=$(CURDIR) EXTRA_CFLAGS="-I$(CURDIR)/../include" modules
clean:
$(MAKE) ARCH=$(ARCH) -C $(KDIR) M=$(CURDIR) clean
@@ -0,0 +1,49 @@
/* SPDX-License-Identifier: GPL-2.0 OR MIT */
/*
* (C) COPYRIGHT 2020 Arm Limited or its affiliates. All rights reserved.
*/
/* Default settings shared among Arbitration modules */
bob_defaults {
name: "mali_arbitration_shared_config_defaults",
kbuild_options: [
"CONFIG_MALI_ARBITRATION=m",
],
local_include_dirs: [
"../include",
],
defaults: [
"kernel_defaults",
"mali_kbase_shared_config_defaults",
],
}
/* Default settings shared among Arbiter modules */
bob_defaults {
name: "mali_arbiter_shared_config_defaults",
kbuild_options: [
"MALI_ARBITER_TEST_API={{.debug}}",
],
defaults: ["mali_arbitration_shared_config_defaults"],
}
bob_kernel_module {
name: "mali_arbiter",
srcs: [
"Kbuild",
"mali_arbiter.c",
"mali_arbiter.h",
],
enabled: false,
mali_has_arbiter: {
enabled: true,
},
add_to_alias: [
"kernel",
],
mali_arbiter_lost_test: {
kbuild_options: ["MALI_ARBITER_LOST_TEST=y"],
},
defaults: ["mali_arbiter_shared_config_defaults"],
}
@@ -0,0 +1,21 @@
MIT License
Copyright (c) 2020 ARM Limited.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
@@ -0,0 +1,31 @@
# SPDX-License-Identifier: GPL-2.0
#
# (C) COPYRIGHT 2019-2020 ARM Limited. All rights reserved.
#
# This program is free software and is provided to you under the terms of the
# GNU General Public License version 2 as published by the Free Software
# Foundation, and any use by you of this program is subject to the terms
# of such GNU licence.
#
# A copy of the licence is included with the program, and can also be obtained
# from Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
# Boston, MA 02110-1301, USA.
#
#
ifneq ($(CONFIG_XEN),)
obj-$(CONFIG_MALI_ARBITRATION) := mali_arbif_xenback.o
endif
mali_arbif_xenback-y := mali_arbif_xenback_main.o mali_arbif_xenback_hypercall.o
# Set up defaults if not defined by build system
MALI_ARBITER_TEST_API ?= 0
# Set up our defines, which will be passed to gcc
DEFINES = \
-DMALI_ARBITER_TEST_API=$(MALI_ARBITER_TEST_API)
# Use our defines when compiling
ccflags-y += $(DEFINES)
subdir-ccflags-y += $(DEFINES)
Loaded 100 of 80519 files, more files were not shown because too many files have changed in this diff. Show more