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

+9
View File
@@ -0,0 +1,9 @@
config MTK_PPM
tristate "MTK Performance and Power Management"
help
This kernel config can be used to enable PPM.
Handles power/performance related requests from
kernel or user-space–Calculates appropriate CPU
core number and frequency range accordingly.
If unsure, say N.
+16
View File
@@ -0,0 +1,16 @@
#
# 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.
#
export PPM_ROOT_DIR ?= $(srctree)/drivers/misc/mediatek/ppm_v3
obj-y += src/
@@ -0,0 +1,335 @@
/* SPDX-License-Identifier: GPL-2.0 */
/*
* Copyright (c) 2019 MediaTek Inc.
*/
#ifndef __MT_PPM_INTERNAL_H__
#define __MT_PPM_INTERNAL_H__
#ifdef __cplusplus
extern "C" {
#endif
#include <linux/module.h>
#include <linux/platform_device.h>
#include <linux/cpufreq.h>
#include <linux/mutex.h>
#include <linux/types.h>
#include <linux/kernel.h>
#include <linux/proc_fs.h>
#include <linux/seq_file.h>
#include <linux/printk.h>
#include <linux/sched.h>
#include <linux/cpumask.h>
#include "mtk_ppm_api.h"
#include "mtk_ppm_platform.h"
#include "mtk_ppm_ipi.h"
#ifdef CONFIG_MTK_UNIFY_POWER
#include "mtk_common_upower.h"
#endif
/*==============================================================*/
/* Definitions */
/*==============================================================*/
/* POLICY */
/* If priority value is the same, it will decide by ppm_policy enum value */
#define PPM_POLICY_PRIO_HIGHEST (0x0)
#define PPM_POLICY_PRIO_POWER_BUDGET_BASE (0x1)
#define PPM_POLICY_PRIO_USER_SPECIFY_BASE (0x40)
#define PPM_POLICY_PRIO_PERFORMANCE_BASE (0x80)
#define PPM_POLICY_PRIO_SYSTEM_BASE (0xC0)
#define PPM_POLICY_PRIO_LOWEST (0xFF)
/* Cluster setting */
#define get_cluster_min_cpufreq_idx(id) \
(ppm_main_info.cluster_info[id].dvfs_opp_num - 1)
#define get_cluster_max_cpufreq_idx(id) (0)
#define get_cluster_min_cpu_core(id) (0)
#define get_cluster_max_cpu_core(id) \
(ppm_main_info.cluster_info[id].core_num)
#define get_cluster_max_cpufreq(id) \
((ppm_main_info.cluster_info[id].dvfs_tbl) \
? ppm_main_info.cluster_info[id].dvfs_tbl[0].frequency \
: ~0)
#define get_cluster_min_cpufreq(id) \
((ppm_main_info.cluster_info[id].dvfs_tbl) \
? ppm_main_info.cluster_info[id].dvfs_tbl[DVFS_OPP_NUM-1].frequency \
: 0)
/* loop macros */
#define for_each_ppm_clusters(i) \
for (i = 0; i < ppm_main_info.cluster_num; i++)
#define for_each_ppm_clients(i) for (i = 0; i < NR_PPM_CLIENTS; i++)
/* operation */
#ifndef MAX
#define MAX(a, b) ((a) >= (b) ? (a) : (b))
#endif
#ifndef MIN
#define MIN(a, b) ((a) >= (b) ? (b) : (a))
#endif
/* LOCK */
#define ppm_lock(lock) mutex_lock(lock)
#define ppm_unlock(lock) mutex_unlock(lock)
/* PROCFS */
#define PROC_FOPS_RW(name) \
static int ppm_ ## name ## _proc_open(struct inode *inode, struct file *file) \
{ \
return single_open(file, ppm_ ## name ## _proc_show, PDE_DATA(inode));\
} \
static const struct file_operations ppm_ ## name ## _proc_fops = { \
.owner = THIS_MODULE, \
.open = ppm_ ## name ## _proc_open, \
.read = seq_read, \
.llseek = seq_lseek, \
.release = single_release, \
.write = ppm_ ## name ## _proc_write, \
}
#define PROC_FOPS_RO(name) \
static int ppm_ ## name ## _proc_open(struct inode *inode, struct file *file) \
{ \
return single_open(file, ppm_ ## name ## _proc_show, PDE_DATA(inode));\
} \
static const struct file_operations ppm_ ## name ## _proc_fops = { \
.owner = THIS_MODULE, \
.open = ppm_ ## name ## _proc_open, \
.read = seq_read, \
.llseek = seq_lseek, \
.release = single_release, \
}
#define PROC_ENTRY(name) {__stringify(name), &ppm_ ## name ## _proc_fops}
/* LOG */
#undef TAG
#define TAG "[Power/PPM] "
#define ppm_err ppm_info
#define ppm_warn ppm_info
#define ppm_info(fmt, args...) pr_notice(TAG""fmt, ##args)
#define ppm_dbg(type, fmt, args...) \
do { \
if (ppm_debug & ALL || ppm_debug & type) \
ppm_info(fmt, ##args); \
else if (type == MAIN) \
pr_debug(TAG""fmt, ##args); \
} while (0)
#define ppm_ver(fmt, args...) \
do { \
if (ppm_debug == ALL) \
ppm_info(fmt, ##args); \
} while (0)
#define FUNC_LV_MODULE BIT(0) /* module, platform driver interface */
#define FUNC_LV_API BIT(1) /* mt_ppm driver global function */
#define FUNC_LV_MAIN BIT(2) /* mt_ppm driver main function */
#define FUNC_LV_POLICY BIT(4) /* mt_ppm driver policy function */
#define FUNC_ENTER(lv) \
do { if ((lv) & ppm_func_lv_mask) \
ppm_info(">> %s()\n", __func__); } while (0)
#define FUNC_EXIT(lv) \
do { if ((lv) & ppm_func_lv_mask) \
ppm_info("<< %s():%d\n", __func__, __LINE__); } while (0)
/* cpufreq */
static inline void mtk_cpu_update_policy(void)
{
#ifdef CONFIG_CPU_FREQ
if (strcmp(CONFIG_MTK_PLATFORM, "mt6779"))
ppm_info("trigger cpufreq_update_policy(*)\n");
cpufreq_update_policy(0); /* little core */
cpufreq_update_policy(CORE_NUM_L); /* big core */
#endif
}
/*==============================================================*/
/* Enum */
/*==============================================================*/
enum {
NO_LOG = 0,
ALL = 1 << 0,
MAIN = 1 << 1,
HICA = 1 << 2,
DLPT = 1 << 3,
USER_LIMIT = 1 << 4,
TIME_PROFILE = 1 << 5,
COBRA = 1 << 6,
SYS_BOOST = 1 << 7,
IPI = 1 << 8,
CPI = 1 << 9,
HARD_USER_LIMIT = 1 << 10,
};
enum ppm_policy {
PPM_POLICY_PTPOD = 0, /* highest priority */
PPM_POLICY_UT,
PPM_POLICY_FORCE_LIMIT,
PPM_POLICY_PWR_THRO,
PPM_POLICY_THERMAL,
PPM_POLICY_DLPT,
PPM_POLICY_HARD_USER_LIMIT,
PPM_POLICY_USER_LIMIT,
PPM_POLICY_LCM_OFF,
PPM_POLICY_SYS_BOOST,
PPM_POLICY_HICA,
NR_PPM_POLICIES,
};
/*==============================================================*/
/* Data Structures */
/*==============================================================*/
struct ppm_cluster_limit {
int min_cpufreq_idx;
int max_cpufreq_idx;
unsigned int min_cpu_core;
unsigned int max_cpu_core;
};
struct ppm_policy_req {
unsigned int cluster_num;
unsigned int power_budget;
unsigned int perf_idx;
struct ppm_cluster_limit *limit;
};
struct ppm_policy_data {
/* settings */
const char *name;
enum ppm_policy policy;
unsigned int priority; /* smaller value has higher priority */
/* status */
bool is_enabled;
bool is_activated;
bool is_limit_updated;
/* lock */
struct mutex lock;
/* list link */
struct list_head link;
/* request to PPM */
struct ppm_policy_req req;
/* callbacks */
void (*update_limit_cb)(void);
void (*status_change_cb)(bool enable);
};
struct ppm_cluster_info {
unsigned int cluster_id;
unsigned int core_num;
unsigned int cpu_id; /* cpu id of the dvfs policy owner */
unsigned int dvfs_opp_num;
unsigned int max_freq_except_userlimit;
unsigned int freq_khz;
/* unsigned int volt_uv; */
struct cpufreq_frequency_table *dvfs_tbl; /* from DVFS driver */
int doe_max;
int doe_min;
};
struct ppm_data {
bool is_enabled;
bool is_doe_enabled;
bool is_in_suspend;
unsigned int min_power_budget;
cpumask_var_t exclusive_core;
/* platform settings */
unsigned int cluster_num;
enum dvfs_table_type dvfs_tbl_type;
/* platform dev/driver */
const struct dev_pm_ops ppm_pm_ops;
struct platform_device ppm_pdev;
struct platform_driver ppm_pdrv;
/* PPM core data */
struct mutex lock;
struct ppm_cluster_info *cluster_info;
struct ppm_client_data client_info[NR_PPM_CLIENTS];
struct ppm_client_req client_req;
struct ppm_client_req last_req; /* for debugging purpose */
struct list_head policy_list;
};
struct ppm_user_limit {
int min_freq_idx;
int max_freq_idx;
int min_core_num;
int max_core_num;
};
struct ppm_userlimit_data {
bool is_freq_limited_by_user;
bool is_core_limited_by_user;
struct ppm_user_limit *limit;
};
/*==============================================================*/
/* Global variables */
/*==============================================================*/
extern struct ppm_data ppm_main_info;
extern struct proc_dir_entry *policy_dir;
extern struct proc_dir_entry *profile_dir;
extern struct proc_dir_entry *cpi_dir;
extern unsigned int ppm_func_lv_mask;
extern unsigned int ppm_debug;
/*==============================================================*/
/* APIs */
/*==============================================================*/
extern int mt_ppm_main(void);
/* procfs */
extern int ppm_procfs_init(void);
extern char *ppm_copy_from_user_for_proc(
const char __user *buffer, size_t count);
/* platform dependent APIs */
extern void ppm_update_req_by_pwr(struct ppm_policy_req *req);
extern int ppm_get_min_pwr_idx(void);
extern int ppm_get_max_pwr_idx(void);
/* main */
extern int ppm_main_freq_to_idx(unsigned int cluster_id,
unsigned int freq, unsigned int relation);
extern void ppm_clear_policy_limit(struct ppm_policy_data *policy);
extern void ppm_main_clear_client_req(struct ppm_client_req *c_req);
extern int ppm_main_register_policy(struct ppm_policy_data *policy);
extern void ppm_main_unregister_policy(struct ppm_policy_data *policy);
/* profiling */
extern int ppm_profile_init(void);
extern void ppm_profile_exit(void);
extern void ppm_profile_update_client_exec_time(
enum ppm_client client, unsigned long long time);
#ifdef PPM_SSPM_SUPPORT
extern void ppm_profile_update_ipi_exec_time(int id, unsigned long long time);
#endif
/* SRAM debugging */
#ifdef CONFIG_MTK_RAM_CONSOLE
extern void aee_rr_rec_ppm_cluster_limit(int id, u32 val);
extern void aee_rr_rec_ppm_step(u8 val);
extern void aee_rr_rec_ppm_min_pwr_bgt(u32 val);
extern void aee_rr_rec_ppm_policy_mask(u32 val);
extern void aee_rr_rec_ppm_waiting_for_pbm(u8 val);
#endif
#ifdef __cplusplus
}
#endif
#endif
@@ -0,0 +1,64 @@
/* SPDX-License-Identifier: GPL-2.0 */
/*
* Copyright (c) 2019 MediaTek Inc.
*/
#ifndef _MT_PPM_IPI_
#define _MT_PPM_IPI_
#include "mtk_ppm_api.h"
#include "mtk_ppm_platform.h"
#define PPM_D_LEN (7) /* # of cmd + arg0 + arg1 + ... */
/* IPI Msg type */
enum {
PPM_IPI_INIT,
PPM_IPI_UPDATE_LIMIT,
PPM_IPI_THERMAL_LIMIT_TEST,
PPM_IPI_PTPOD_TEST,
NR_PPM_IPI,
};
/* IPI Msg data structure */
struct ppm_ipi_data {
/* 1 int for opcode */
unsigned int cmd;
/* 6 int for ipi data */
union {
struct {
unsigned int efuse_val;
unsigned int cobra_tbl_addr;
unsigned int dvfs_tbl_type;
} init;
struct {
struct {
unsigned char min_cpufreq_idx;
unsigned char max_cpufreq_idx;
unsigned char max_cpu_core;
unsigned char advise_freq_idx;
} cluster_limit[NR_PPM_CLUSTERS];
} update_limit;
struct {
unsigned int budget;
} thermal_limit_test;
struct {
unsigned int activate;
} ptpod_test;
/* padding to 6 int */
struct {
unsigned int ppm_data[6];
};
} u;
};
#ifdef PPM_SSPM_SUPPORT
extern void ppm_ipi_init(unsigned int efuse_val, unsigned int cobra_tbl_addr);
extern void ppm_ipi_update_limit(struct ppm_client_req req);
extern void ppm_ipi_thermal_limit_test(unsigned int budget);
extern void ppm_ipi_ptpod_test(unsigned int activate);
#endif
#endif
+62
View File
@@ -0,0 +1,62 @@
#
# 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 the
# GNU General Public License for more details.
#
ccflags-y += \
-I$(PPM_ROOT_DIR)/inc \
-I$(PPM_ROOT_DIR)/src/mach/mt6779 \
-I$(srctree)/drivers/misc/mediatek/include/mt-plat/$(CONFIG_MTK_PLATFORM)/include \
-I$(srctree)/drivers/misc/mediatek/include/mt-plat \
-I$(srctree)/drivers/misc/mediatek/base/power/$(CONFIG_MTK_PLATFORM) \
-I$(srctree)/drivers/misc/mediatek/power_throttling \
ifeq ($(CONFIG_MTK_TINYSYS_SSPM_SUPPORT), y)
ccflags-y += \
-I$(srctree)/drivers/misc/mediatek/sspm/ \
-I$(srctree)/drivers/misc/mediatek/sspm/$(CONFIG_MTK_PLATFORM)
endif
ifneq ($(CONFIG_FPGA_EARLY_PORTING),y)
# main
obj-y := mtk_ppm_main.o
obj-y += mtk_ppm_interface.o
obj-y += mtk_ppm_api.o
# for profiling
obj-y += mtk_ppm_profile.o
# for IPI to SSPM
# obj-y += mtk_ppm_ipi.o
# no need for mt6779, will not port this
# for CPI
# obj-y += mtk_ppm_cpi.o
# policies
obj-y += mtk_ppm_policy_dlpt.o
obj-y += mtk_ppm_policy_sys_boost.o
# obj-y += mtk_ppm_policy_ptpod.o
obj-y += mtk_ppm_policy_pwr_thro.o
obj-y += mtk_ppm_policy_thermal.o
obj-y += mtk_ppm_policy_lcm_off.o
obj-y += mtk_ppm_policy_hard_user_limit.o
obj-y += mtk_ppm_policy_user_limit.o
obj-y += mtk_ppm_policy_force_limit.o
# for test purpose
obj-y += mtk_ppm_policy_ut.o
# platform dependent files
obj-y += mach/
else
# dummy driver to avoid build error
obj-y := mtk_ppm_dummy.o
endif
@@ -0,0 +1 @@
obj-y += mt6779/
@@ -0,0 +1,23 @@
#
# 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 the
# GNU General Public License for more details.
#
ccflags-y += \
-I$(PPM_ROOT_DIR)/inc \
-I$(srctree)/drivers/misc/mediatek/include/mt-plat
# platform dependent functions
obj-y += mtk_ppm_platform.o
# power data related
obj-y += mtk_ppm_power_data.o
# limit calculation algorithm
obj-y += mtk_ppm_cobra_algo.o
@@ -0,0 +1,826 @@
// SPDX-License-Identifier: GPL-2.0
/*
* Copyright (c) 2019 MediaTek Inc.
*/
#include <linux/slab.h>
#include <linux/string.h>
#include <linux/kernel.h>
#include <linux/types.h>
#include <linux/cpumask.h>
#include <linux/topology.h>
#include <linux/io.h>
#include "mtk_ppm_platform.h"
#include "mtk_ppm_internal.h"
struct ppm_cobra_data *cobra_tbl;
struct ppm_cobra_lookup cobra_lookup_data;
static int Core_limit[NR_PPM_CLUSTERS] = {CORE_NUM_L, CORE_NUM_B};
int cobra_init_done;
static int is_perf_fist;
#define ACT_CORE(cluster) (active_core[PPM_CLUSTER_##cluster])
#define CORE_LIMIT(cluster) (core_limit_tmp[PPM_CLUSTER_##cluster])
struct ppm_cobra_data *ppm_cobra_pass_tbl(void)
{
if (cobra_init_done)
return cobra_tbl;
return NULL;
}
int eara_is_perf_first(void)
{
return is_perf_fist;
}
void eara_pass_perf_first_hint(int enable)
{
is_perf_fist = enable;
}
static unsigned int get_idx_in_pwr_tbl(enum ppm_cluster cluster)
{
unsigned int idx = 0;
if (cluster >= NR_PPM_CLUSTERS) {
ppm_err("%s: Invalid input: cluster=%d\n", __func__, cluster);
WARN_ON(1);
}
while (cluster)
idx += get_cluster_max_cpu_core(--cluster);
return idx;
}
static short get_delta_pwr(enum ppm_cluster cluster,
unsigned int core, unsigned int opp)
{
unsigned int idx;
unsigned int cur_opp, prev_opp;
int delta_pwr;
if (core > get_cluster_max_cpu_core(cluster)
|| opp > get_cluster_min_cpufreq_idx(cluster)) {
ppm_err("%s: Invalid input: core=%d, opp=%d\n",
__func__, core, opp);
WARN_ON(1);
return 0;
}
if (core == 0)
return 0;
idx = get_idx_in_pwr_tbl(cluster);
cur_opp = opp;
prev_opp = opp + 1;
if (opp == COBRA_OPP_NUM - 1) {
delta_pwr = (core == 1)
? cobra_tbl->basic_pwr_tbl[idx+core-1][cur_opp].power_idx
: (cobra_tbl->basic_pwr_tbl[idx+core-1][cur_opp].power_idx -
cobra_tbl->basic_pwr_tbl[idx+core-2][cur_opp].power_idx);
} else {
delta_pwr =
cobra_tbl->basic_pwr_tbl[idx+core-1][cur_opp].power_idx -
cobra_tbl->basic_pwr_tbl[idx+core-1][prev_opp].power_idx;
}
return delta_pwr;
}
static short get_delta_perf(enum ppm_cluster cluster, unsigned int core,
unsigned int opp)
{
unsigned int idx, cur_opp, prev_opp;
short delta_perf;
if (core > get_cluster_max_cpu_core(cluster)
|| opp > get_cluster_min_cpufreq_idx(cluster)) {
ppm_err("%s: Invalid input: cluster=%d, core=%d, opp=%d\n",
__func__, cluster, core, opp);
WARN_ON(1);
}
if (core == 0)
return 0;
idx = get_idx_in_pwr_tbl(cluster);
cur_opp = opp;
prev_opp = opp + 1;
if (opp == COBRA_OPP_NUM - 1) {
delta_perf = (core == 1)
? cobra_tbl->basic_pwr_tbl[idx+core-1][cur_opp].perf_idx
: (cobra_tbl->basic_pwr_tbl[idx+core-1][cur_opp].perf_idx -
cobra_tbl->basic_pwr_tbl[idx+core-2][cur_opp].perf_idx);
} else {
delta_perf =
cobra_tbl->basic_pwr_tbl[idx+core-1][cur_opp].perf_idx -
cobra_tbl->basic_pwr_tbl[idx+core-1][prev_opp].perf_idx;
}
return delta_perf;
}
static int get_perf(enum ppm_cluster cluster, unsigned int core,
unsigned int opp)
{
unsigned int idx, min_idx;
int perf;
int ratio = 0;
ppm_dbg(COBRA, "%s: input: cluster=%d, core=%d, opp=%d\n",
__func__, cluster, core, opp);
if (core > get_cluster_max_cpu_core(cluster)) {
ppm_err("%s: Invalid input: cluster=%d, core=%d, opp=%d\n",
__func__, cluster, core, opp);
WARN_ON(1);
return 0;
}
if (core == 0)
return 0;
min_idx = get_cluster_min_cpufreq_idx(cluster);
if (opp >= min_idx) {
opp = min_idx;
core--;
ratio = 100;
}
if (core == 0)
core = 1;
idx = get_idx_in_pwr_tbl(cluster);
perf = cobra_tbl->basic_pwr_tbl[idx+core-1][opp].perf_idx *
cobra_tbl->basic_pwr_tbl[idx][opp].perf_idx;
if (ratio)
perf = perf * ratio;
ppm_dbg(COBRA, "%s: output: idx=%d, core=%d, opp=%d, perf=%d\n",
__func__, idx, core, opp, perf);
return perf;
}
static short get_delta_eff(enum ppm_cluster cluster, unsigned int core,
unsigned int opp)
{
short delta_pwr, delta_perf, delta_eff;
if (core > get_cluster_max_cpu_core(cluster)
|| opp > get_cluster_min_cpufreq_idx(cluster)) {
ppm_err("%s: Invalid input: core=%d, opp=%d, cluster=%d\n",
__func__, core, opp, cluster);
WARN_ON(1);
}
if (core == 0)
return 0;
delta_pwr = get_delta_pwr(cluster, core, opp);
if (delta_pwr <= 0)
return 0;
delta_perf = get_delta_perf(cluster, core, opp);
if (opp == COBRA_OPP_NUM - 1)
/* x10 to make it hard to turn off cores */
delta_eff = (delta_perf * 1000) / delta_pwr;
else
delta_eff = (delta_perf * 100) / delta_pwr;
return delta_eff;
}
void ppm_cobra_update_core_limit(unsigned int cluster, int limit)
{
if (cluster >= NR_PPM_CLUSTERS) {
ppm_err("%s: Invalid cluster id = %d\n", __func__, cluster);
WARN_ON(1);
return;
}
if (limit < 0 || limit > get_cluster_max_cpu_core(cluster)) {
ppm_err("%s: Invalid core limit for cluster%d = %d\n",
__func__, cluster, limit);
WARN_ON(1);
return;
}
Core_limit[cluster] = limit;
}
void ppm_cobra_update_freq_limit(unsigned int cluster, int limit)
{
/* NO NEED */
}
void ppm_cobra_update_limit(void *user_req)
{
struct ppm_policy_req *req;
int power_budget;
int opp[NR_PPM_CLUSTERS];
int active_core[NR_PPM_CLUSTERS];
#if PPM_COBRA_USE_CORE_LIMIT
int core_limit_tmp[NR_PPM_CLUSTERS];
#endif
int i;
struct cpumask cluster_cpu, online_cpu;
int delta_power;
/* Get power index of current OPP */
int curr_power = 0;
struct ppm_cluster_status cl_status[NR_PPM_CLUSTERS];
/* skip if COBRA is not init yet */
if (!cobra_init_done)
return;
if (!user_req)
return;
req = (struct ppm_policy_req *)user_req;
power_budget = req->power_budget;
if (power_budget >= ppm_get_max_pwr_idx())
return;
ppm_dbg(COBRA, "[PREV]Core_Limit=%d%d, policy_limit=%d%d\n",
Core_limit[PPM_CLUSTER_L],
Core_limit[PPM_CLUSTER_B],
req->limit[PPM_CLUSTER_L].max_cpu_core,
req->limit[PPM_CLUSTER_B].max_cpu_core);
for_each_ppm_clusters(i) {
arch_get_cluster_cpus(&cluster_cpu, i);
cpumask_and(&online_cpu, &cluster_cpu, cpu_online_mask);
cl_status[i].core_num = cpumask_weight(&online_cpu);
cl_status[i].volt = 0; /* don't care */
if (!cl_status[i].core_num)
cl_status[i].freq_idx = -1;
else
cl_status[i].freq_idx = ppm_main_freq_to_idx(i,
ppm_get_cur_freq(i),
CPUFREQ_RELATION_L);
ppm_ver("[%d] core = %d, freq_idx = %d\n",
i, cl_status[i].core_num, cl_status[i].freq_idx);
}
#if PPM_COBRA_USE_CORE_LIMIT
for_each_ppm_clusters(i) {
if (cl_status[i].core_num > Core_limit[i])
cl_status[i].core_num = Core_limit[i];
if (req->limit[i].max_cpu_core < Core_limit[i])
Core_limit[i] = req->limit[i].max_cpu_core;
}
#endif
if (cl_status[PPM_CLUSTER_L].core_num == 0 &&
cl_status[PPM_CLUSTER_B].core_num == 0) {
if (Core_limit[PPM_CLUSTER_L] > 0) {
cl_status[PPM_CLUSTER_L].core_num = 1;
cl_status[PPM_CLUSTER_L].freq_idx =
get_cluster_max_cpufreq_idx(PPM_CLUSTER_L);
} else {
cl_status[PPM_CLUSTER_B].core_num = 1;
cl_status[PPM_CLUSTER_B].freq_idx =
get_cluster_max_cpufreq_idx(PPM_CLUSTER_B);
}
}
curr_power = ppm_find_pwr_idx(cl_status);
if (curr_power < 0)
curr_power = mt_ppm_thermal_get_max_power();
delta_power = power_budget - curr_power;
for_each_ppm_clusters(i) {
opp[i] = cl_status[i].freq_idx;
/* Get Active Core number of each cluster */
active_core[i] = (cl_status[i].core_num >= 0) ?
cl_status[i].core_num : 0;
#if PPM_COBRA_USE_CORE_LIMIT
core_limit_tmp[i] = Core_limit[i];
req->limit[i].max_cpu_core = core_limit_tmp[i];
#endif
}
ppm_dbg(COBRA,
"[IN](bgt/delta/cur)=(%d/%d/%d),(opp/act/c_lmt)=(%d,%d/%d%d/%d%d)\n",
power_budget, delta_power, curr_power,
opp[PPM_CLUSTER_L], opp[PPM_CLUSTER_B],
ACT_CORE(L), ACT_CORE(B),
CORE_LIMIT(L), CORE_LIMIT(B));
/* increase ferquency limit */
if (delta_power >= 0) {
while (1) {
int ChoosenCl = -1, MaxEff = 0, ChoosenPwr = 0;
int target_delta_pwr, target_delta_eff;
/* give remaining power to B-cluster if L-cluster opp is 0 */
if (opp[PPM_CLUSTER_L] == 0 && ACT_CORE(B) == 0) {
target_delta_pwr =
get_delta_pwr(
PPM_CLUSTER_B,
1,
COBRA_OPP_NUM-1);
if (delta_power >= target_delta_pwr) {
ACT_CORE(B) = 1;
delta_power -= target_delta_pwr;
opp[PPM_CLUSTER_B] = COBRA_OPP_NUM - 1;
}
}
/* B-cluster */
if (ACT_CORE(B) > 0 && opp[PPM_CLUSTER_B] > 0) {
target_delta_pwr =
get_delta_pwr(
PPM_CLUSTER_B,
ACT_CORE(B),
opp[PPM_CLUSTER_B]-1);
if (delta_power >= target_delta_pwr) {
if (is_perf_fist)
MaxEff = get_perf(
PPM_CLUSTER_B,
ACT_CORE(B),
opp[PPM_CLUSTER_B]-1);
else
MaxEff = get_delta_eff(
PPM_CLUSTER_B,
ACT_CORE(B),
opp[PPM_CLUSTER_B]-1);
ChoosenCl = PPM_CLUSTER_B;
ChoosenPwr = target_delta_pwr;
}
}
/* L-cluster */
if (ACT_CORE(L) > 0 && opp[PPM_CLUSTER_L] > 0) {
target_delta_pwr = get_delta_pwr(
PPM_CLUSTER_L,
ACT_CORE(L),
opp[PPM_CLUSTER_L]-1);
if (is_perf_fist)
target_delta_eff = get_perf(
PPM_CLUSTER_L,
ACT_CORE(L),
opp[PPM_CLUSTER_L]-1);
else
target_delta_eff = get_delta_eff(
PPM_CLUSTER_L,
ACT_CORE(L),
opp[PPM_CLUSTER_L]-1);
if (delta_power >= target_delta_pwr &&
MaxEff <= target_delta_eff) {
MaxEff = target_delta_eff;
ChoosenCl = PPM_CLUSTER_L;
ChoosenPwr = target_delta_pwr;
}
}
if (ChoosenCl != -1)
goto prepare_next_round;
/* exceed power budget or all active core is highest freq. */
#if PPM_COBRA_USE_CORE_LIMIT
/* no enough budget */
if (opp[PPM_CLUSTER_L] != 0)
goto end;
if (is_perf_fist) {
/* give budget to B */
while (CORE_LIMIT(B) <
get_cluster_max_cpu_core(PPM_CLUSTER_B)) {
target_delta_pwr = get_delta_pwr(
PPM_CLUSTER_B,
CORE_LIMIT(B)+1,
COBRA_OPP_NUM-1);
if (delta_power < target_delta_pwr)
break;
delta_power -= target_delta_pwr;
req->limit[PPM_CLUSTER_B].max_cpu_core =
++CORE_LIMIT(B);
}
/* give budget to L */
while (CORE_LIMIT(L) <
get_cluster_max_cpu_core(PPM_CLUSTER_L)) {
target_delta_pwr = get_delta_pwr(
PPM_CLUSTER_L,
CORE_LIMIT(L)+1,
COBRA_OPP_NUM-1);
if (delta_power < target_delta_pwr)
break;
delta_power -= target_delta_pwr;
req->limit[PPM_CLUSTER_L].max_cpu_core =
++CORE_LIMIT(L);
}
} else {
/* give budget to L */
while (CORE_LIMIT(L) <
get_cluster_max_cpu_core(PPM_CLUSTER_L)) {
target_delta_pwr = get_delta_pwr(
PPM_CLUSTER_L,
CORE_LIMIT(L)+1,
COBRA_OPP_NUM-1);
if (delta_power < target_delta_pwr)
break;
delta_power -= target_delta_pwr;
req->limit[PPM_CLUSTER_L].max_cpu_core =
++CORE_LIMIT(L);
}
/* give budget to B */
while (CORE_LIMIT(B) <
get_cluster_max_cpu_core(PPM_CLUSTER_B)) {
target_delta_pwr = get_delta_pwr(
PPM_CLUSTER_B,
CORE_LIMIT(B)+1,
COBRA_OPP_NUM-1);
if (delta_power < target_delta_pwr)
break;
delta_power -= target_delta_pwr;
req->limit[PPM_CLUSTER_B].max_cpu_core =
++CORE_LIMIT(B);
}
}
end:
#endif
ppm_dbg(COBRA,
"[+]ChoosenCl=-1! delta=%d, (opp/c_lmt)=(%d,%d/%d%d)\n",
delta_power,
opp[PPM_CLUSTER_L],
opp[PPM_CLUSTER_B],
CORE_LIMIT(L),
CORE_LIMIT(B));
break;
prepare_next_round:
opp[ChoosenCl] -= 1;
delta_power -= ChoosenPwr;
ppm_dbg(COBRA,
"[+](delta/Cl/Pwr)=(%d,%d,%d), opp=%d,%d\n",
delta_power, ChoosenCl, ChoosenPwr,
opp[PPM_CLUSTER_L], opp[PPM_CLUSTER_B]);
}
} else {
while (delta_power < 0) {
int ChoosenCl = -1;
int MinEff = 10000;
/*
* should be bigger than max value of
* efficiency_* array
*/
int ChoosenPwr = 0;
int target_delta_eff;
/* B-cluster */
if (ACT_CORE(B) > 0
&& opp[PPM_CLUSTER_B] < PPM_COBRA_MAX_FREQ_IDX) {
if (is_perf_fist)
MinEff =
get_perf(PPM_CLUSTER_B,
ACT_CORE(B),
opp[PPM_CLUSTER_B]);
else
MinEff =
get_delta_eff(PPM_CLUSTER_B,
ACT_CORE(B),
opp[PPM_CLUSTER_B]);
ChoosenCl = PPM_CLUSTER_B;
ChoosenPwr =
get_delta_pwr(PPM_CLUSTER_B,
ACT_CORE(B),
opp[PPM_CLUSTER_B]);
}
/* L-cluster */
if (ACT_CORE(L) > 0
&& opp[PPM_CLUSTER_L] < PPM_COBRA_MAX_FREQ_IDX) {
if (is_perf_fist) {
/*keep 1L active*/
if (ACT_CORE(L) > 1
|| opp[PPM_CLUSTER_L]
!= PPM_COBRA_MAX_FREQ_IDX-1)
target_delta_eff =
get_perf(PPM_CLUSTER_L,
ACT_CORE(L),
opp[PPM_CLUSTER_L]);
else
target_delta_eff = 9999999;
} else
target_delta_eff = get_delta_eff(
PPM_CLUSTER_L,
ACT_CORE(L),
opp[PPM_CLUSTER_L]);
if (MinEff > target_delta_eff) {
MinEff = target_delta_eff;
ChoosenCl = PPM_CLUSTER_L;
ChoosenPwr =
get_delta_pwr(PPM_CLUSTER_L,
ACT_CORE(L),
opp[PPM_CLUSTER_L]);
}
}
if (ChoosenCl == -1) {
ppm_err("No lower OPP!(bgt/delta/cur)= ");
ppm_err("(%d/%d/%d),(opp/act)=(%d,%d/%d%d)\n",
power_budget, delta_power, curr_power,
opp[PPM_CLUSTER_L], opp[PPM_CLUSTER_B],
ACT_CORE(L), ACT_CORE(B));
break;
}
/* change opp of cluster */
opp[ChoosenCl] += 1;
/* Turned off core */
/* TODO: check this! */
#if PPM_COBRA_USE_CORE_LIMIT
if (opp[PPM_CLUSTER_B] == PPM_COBRA_MAX_FREQ_IDX
&& ACT_CORE(B) > 0) {
req->limit[PPM_CLUSTER_B].max_cpu_core =
--ACT_CORE(B);
opp[PPM_CLUSTER_B] = PPM_COBRA_MAX_FREQ_IDX - 1;
} else if (opp[PPM_CLUSTER_L] ==
PPM_COBRA_MAX_FREQ_IDX) {
if (ACT_CORE(L) > 1)
req->limit[PPM_CLUSTER_L].max_cpu_core =
--ACT_CORE(L);
opp[PPM_CLUSTER_L] = PPM_COBRA_MAX_FREQ_IDX - 1;
}
#endif
delta_power += ChoosenPwr;
curr_power -= ChoosenPwr;
ppm_dbg(COBRA,
"[-](delta/Cl/Pwr)=(%d,%d,%d), (opp/act)=(%d,%d/%d%d)\n",
delta_power, ChoosenCl, ChoosenPwr,
opp[PPM_CLUSTER_L], opp[PPM_CLUSTER_B],
ACT_CORE(L), ACT_CORE(B));
}
}
/* Set all frequency limit of the cluster */
/* Set OPP of Cluser n to opp[n] */
for_each_ppm_clusters(i) {
if (i == PPM_CLUSTER_B) {
if (opp[i] >= 0 && ACT_CORE(B) > 0)
req->limit[i].max_cpufreq_idx = opp[i];
else
req->limit[i].max_cpufreq_idx =
get_cluster_min_cpufreq_idx(i);
} else
req->limit[i].max_cpufreq_idx = opp[i];
}
ppm_dbg(COBRA,
"[OUT]delta=%d, (opp/act/c_lmt/f_lmt)=(%d,%d/%d%d/%d%d/%d,%d)\n",
delta_power,
opp[PPM_CLUSTER_L], opp[PPM_CLUSTER_B],
ACT_CORE(L), ACT_CORE(B),
req->limit[PPM_CLUSTER_L].max_cpu_core,
req->limit[PPM_CLUSTER_B].max_cpu_core,
req->limit[PPM_CLUSTER_L].max_cpufreq_idx,
req->limit[PPM_CLUSTER_B].max_cpufreq_idx);
/* error check */
for_each_ppm_clusters(i) {
if (req->limit[i].max_cpufreq_idx >
req->limit[i].min_cpufreq_idx)
req->limit[i].min_cpufreq_idx =
req->limit[i].max_cpufreq_idx;
if (req->limit[i].max_cpu_core <
req->limit[i].min_cpu_core)
req->limit[i].min_cpu_core =
req->limit[i].max_cpu_core;
}
}
void ppm_cobra_init(void)
{
int i, j;
cobra_tbl = kzalloc(PPM_COBRA_TBL_SRAM_SIZE, GFP_KERNEL);
if (!cobra_tbl) {
WARN_ON(1);
return;
}
#ifdef CONFIG_MTK_UNIFY_POWER
{
unsigned int core, dyn, lkg, dyn_c, lkg_c, cap;
/* generate basic power table */
ppm_ver("basic power table:\n");
for (i = 0; i < TOTAL_CORE_NUM; i++) {
for (j = 0; j < DVFS_OPP_NUM; j++) {
core = (i % CORE_NUM_L) + 1;
dyn = upower_get_power(
i/CORE_NUM_L, j, UPOWER_DYN);
lkg = upower_get_power(
i/CORE_NUM_L, j, UPOWER_LKG);
dyn_c = upower_get_power(
i/CORE_NUM_L + NR_PPM_CLUSTERS,
j, UPOWER_DYN);
lkg_c = upower_get_power(
i/CORE_NUM_L + NR_PPM_CLUSTERS,
j, UPOWER_LKG);
cap = upower_get_power(
i/CORE_NUM_L, j, UPOWER_CPU_STATES);
cobra_tbl->basic_pwr_tbl[i][j].power_idx =
((dyn + lkg) * core +
(dyn_c + lkg_c)) / 1000;
cobra_tbl->basic_pwr_tbl[i][j].perf_idx =
cap * core;
ppm_ver("[%d][%d] = (%d, %d)\n", i, j,
cobra_tbl->basic_pwr_tbl[i][j].power_idx,
cobra_tbl->basic_pwr_tbl[i][j].perf_idx);
}
}
}
#else
for (i = 0; i < TOTAL_CORE_NUM; i++) {
for (j = 0; j < DVFS_OPP_NUM; j++) {
cobra_tbl->basic_pwr_tbl[i][j].power_idx = 0;
cobra_tbl->basic_pwr_tbl[i][j].perf_idx = 0;
}
}
#endif
cobra_init_done = 1;
ppm_info("COBRA init done!\n");
}
void ppm_cobra_dump_tbl(struct seq_file *m)
{
struct ppm_cluster_status cl_status[NR_PPM_CLUSTERS];
int i, j;
int power;
seq_puts(m, "\n========================================================\n");
seq_puts(m, "(L_core, L_opp, B_core, B_opp) = power");
seq_puts(m, "\n========================================================\n");
/* only list power for all core online case */
for_each_ppm_clusters(i)
cl_status[i].core_num = get_cluster_max_cpu_core(i);
for (i = 0; i < DVFS_OPP_NUM; i++) {
for (j = 0; j < DVFS_OPP_NUM; j++) {
cl_status[PPM_CLUSTER_L].freq_idx = i;
cl_status[PPM_CLUSTER_B].freq_idx = j;
power = ppm_find_pwr_idx(cl_status);
if (power) {
seq_printf(m, "(%d, %2d, %d, %2d) = %4d\n",
cl_status[PPM_CLUSTER_L].core_num,
cl_status[PPM_CLUSTER_L].freq_idx,
cl_status[PPM_CLUSTER_B].core_num,
cl_status[PPM_CLUSTER_B].freq_idx,
power
);
}
}
}
}
static unsigned int get_limit_opp_and_budget(void)
{
unsigned int power = 0;
int i, j, k, idx;
for (i = 0; i <= get_cluster_min_cpufreq_idx(PPM_CLUSTER_L); i++) {
cobra_lookup_data.limit[PPM_CLUSTER_L].opp = i;
for (j = 0;
j <= get_cluster_min_cpufreq_idx(PPM_CLUSTER_B);
j++) {
cobra_lookup_data.limit[PPM_CLUSTER_B].opp = j;
for_each_ppm_clusters(k) {
if (!cobra_lookup_data.limit[k].core)
continue;
idx =
get_idx_in_pwr_tbl(k) +
cobra_lookup_data.limit[k].core - 1;
power += (k == PPM_CLUSTER_B)
? cobra_tbl->basic_pwr_tbl[idx][j].power_idx
: cobra_tbl->basic_pwr_tbl[idx][i].power_idx;
}
if (power <= cobra_lookup_data.budget)
return power;
power = 0;
}
}
return power;
}
static void ppm_cobra_lookup_by_budget(struct seq_file *m)
{
int i, j;
unsigned int power;
seq_puts(m, "\n========================================================\n");
seq_puts(m, "(L_core, L_opp, B_core, B_opp) = power");
seq_puts(m, "\n========================================================\n");
seq_printf(m, "Input budget = %d\n\n", cobra_lookup_data.budget);
for (i = get_cluster_max_cpu_core(PPM_CLUSTER_B); i >= 0; i--) {
for (j = get_cluster_max_cpu_core(PPM_CLUSTER_L); j >= 0; j--) {
if (!i && !j)
continue;
cobra_lookup_data.limit[PPM_CLUSTER_L].core = j;
cobra_lookup_data.limit[PPM_CLUSTER_B].core = i;
power = get_limit_opp_and_budget();
if (power) {
seq_printf(m, "(%d, %2d, %d, %2d) = %4d\n",
j,
cobra_lookup_data.limit[PPM_CLUSTER_L].opp,
i,
cobra_lookup_data.limit[PPM_CLUSTER_B].opp,
power);
}
}
}
}
static void ppm_cobra_lookup_by_limit(struct seq_file *m)
{
unsigned int budget = 0, core, opp, i;
for_each_ppm_clusters(i) {
core = (cobra_lookup_data.limit[i].core >
get_cluster_max_cpu_core(i)) ?
get_cluster_max_cpu_core(i) :
cobra_lookup_data.limit[i].core;
opp = (cobra_lookup_data.limit[i].opp >
get_cluster_min_cpufreq_idx(i)) ?
get_cluster_min_cpufreq_idx(i) :
cobra_lookup_data.limit[i].opp;
if (core)
budget += cobra_tbl->basic_pwr_tbl
[CORE_NUM_L*i+core-1][opp].power_idx;
seq_printf(m,
"Cluster %d: core = %d, opp = %d\n", i, core, opp);
}
seq_printf(m, "Budget = %d mW\n", budget);
}
extern void ppm_cobra_lookup_get_result(
struct seq_file *m, enum ppm_cobra_lookup_type type)
{
switch (type) {
case LOOKUP_BY_BUDGET:
ppm_cobra_lookup_by_budget(m);
break;
case LOOKUP_BY_LIMIT:
ppm_cobra_lookup_by_limit(m);
break;
default:
ppm_err("Invalid lookup type %d\n", type);
break;
}
}
@@ -0,0 +1,417 @@
// SPDX-License-Identifier: GPL-2.0
/*
* Copyright (c) 2019 MediaTek Inc.
*/
#include <linux/slab.h>
#include <linux/string.h>
#include <linux/kernel.h>
#include <linux/types.h>
#include <linux/cpu.h>
#include <linux/cpufreq.h>
#include <linux/cpumask.h>
#include <linux/notifier.h>
/* #include <linux/regulator/consumer.h> */
/* #include <linux/device.h> */
#include "mtk_ppm_platform.h"
#include "mtk_ppm_internal.h"
int __attribute__((weak)) mt_spower_get_leakage(
int dev, unsigned int voltage, int degree)
{
return 0;
}
int __attribute__((weak)) get_immediate_cpuL_wrap(void)
{
return 0;
}
int __attribute__((weak)) get_immediate_cpuB_wrap(void)
{
return 0;
}
struct cpufreq_frequency_table mtk_dvfs_tbl_cl0[DVFS_OPP_NUM];
struct cpufreq_frequency_table mtk_dvfs_tbl_cl1[DVFS_OPP_NUM];
/* static struct device *cpu_dev[NR_PPM_CLUSTERS]; */
/* static struct regulator *proc_reg[NR_PPM_CLUSTERS]; */
/* APIs */
void mt_ppm_set_dvfs_table(unsigned int cpu,
struct cpufreq_frequency_table *tbl,
unsigned int num, enum dvfs_table_type type)
{
#ifdef CONFIG_CPU_FREQ
struct ppm_data *p = &ppm_main_info;
int i, j;
struct cpufreq_policy *policy;
FUNC_ENTER(FUNC_LV_API);
for_each_ppm_clusters(i) {
cpu = p->cluster_info[i].cpu_id;
policy = cpufreq_cpu_get(cpu);
/* return if table is existed */
if (p->cluster_info[i].dvfs_tbl)
return;
/* ppm_lock(&(p->lock)); */
p->dvfs_tbl_type = 0;
/* p->cluster_info[i].dvfs_tbl = policy->freq_table; */
p->cluster_info[i].dvfs_tbl =
i ? mtk_dvfs_tbl_cl0 : mtk_dvfs_tbl_cl1;
p->cluster_info[i].dvfs_opp_num =
cpufreq_table_count_valid_entries(policy);
/* dump dvfs table */
ppm_info("DVFS table type = %d\n", type);
ppm_info("DVFS table of cluster %d:\n",
p->cluster_info[i].cluster_id);
num = p->cluster_info[i].dvfs_opp_num;
/* swap dvfs table for MTK reverse ordering */
for (j = 0; j < num; j++) {
/* swap */
p->cluster_info[i].dvfs_tbl[j] =
policy->freq_table[15-j];
ppm_info("%d: %d KHz\n",
j, p->cluster_info[i].dvfs_tbl[j].frequency);
}
/* ppm_unlock(&(p->lock)); */
}
FUNC_EXIT(FUNC_LV_API);
#endif
}
unsigned int ppm_get_cur_freq(enum ppm_cluster cluster)
{
if (cluster >= NR_PPM_CLUSTERS)
return 0;
return ppm_main_info.cluster_info[cluster].freq_khz;
}
static unsigned int ppm_get_cur_volt(enum ppm_cluster cluster)
{
#ifdef PPM_TODO
int volt;
if (cluster >= NR_PPM_CLUSTERS)
return 0;
if (cpu_dev[cluster] == NULL || proc_reg[cluster] == NULL)
return 0;
volt = regulator_get_voltage(proc_reg[cluster]);
if (volt < 0) {
ppm_err("%s: invalid cl %d Vproc value: %d\n",
__func__, cluster, volt);
return ppm_main_info.cluster_info[cluster].volt_uv;
}
ppm_main_info.cluster_info[cluster].volt_uv = volt;
ppm_warn("cl %d volt = %d uV\n", cluster,
ppm_main_info.cluster_info[cluster].volt_uv);
return volt;
#else
return 1000;
#endif
}
static void ppm_get_cluster_status(struct ppm_cluster_status *cl_status)
{
struct cpumask cluster_cpu, online_cpu;
int i;
for_each_ppm_clusters(i) {
arch_get_cluster_cpus(&cluster_cpu, i);
cpumask_and(&online_cpu, &cluster_cpu, cpu_online_mask);
cl_status[i].core_num = cpumask_weight(&online_cpu);
cl_status[i].volt = ppm_get_cur_volt(i) / 1000;
cl_status[i].freq_idx =
ppm_main_freq_to_idx(i,
ppm_get_cur_freq(i), CPUFREQ_RELATION_L);
}
}
#ifdef CONFIG_CPU_FREQ
static int ppm_cpu_freq_callback(struct notifier_block *nb,
unsigned long val, void *data)
{
struct ppm_cluster_status cl_status[NR_PPM_CLUSTERS];
struct cpufreq_freqs *freq = data;
int cpu = freq->cpu;
int i, is_root_cpu = 0;
if (freq->flags & CPUFREQ_CONST_LOOPS)
return NOTIFY_OK;
/* we only need to notify DLPT when root cpu's freq is changed */
for_each_ppm_clusters(i) {
if (cpu == ppm_main_info.cluster_info[i].cpu_id) {
is_root_cpu = 1;
break;
}
}
if (!is_root_cpu)
return NOTIFY_OK;
switch (val) {
case CPUFREQ_POSTCHANGE:
ppm_main_info.cluster_info[i].freq_khz = freq->new;
ppm_dbg(DLPT, "%s: POSTCHANGE!, cpu = %d, freq = %dKHz\n",
__func__, cpu,
ppm_main_info.cluster_info[i].freq_khz);
ppm_get_cluster_status(cl_status);
mt_ppm_dlpt_kick_PBM(cl_status, ppm_main_info.cluster_num);
break;
default:
break;
}
return NOTIFY_OK;
}
static struct notifier_block ppm_cpu_freq_notifier = {
.notifier_call = ppm_cpu_freq_callback,
};
#endif
#ifdef CONFIG_THERMAL
static unsigned int ppm_get_cpu_temp(enum ppm_cluster cluster)
{
unsigned int temp = 85;
switch (cluster) {
case PPM_CLUSTER_L:
temp = get_immediate_cpuL_wrap() / 1000;
break;
case PPM_CLUSTER_B:
temp = get_immediate_cpuB_wrap() / 1000;
break;
default:
ppm_err("@%s: invalid cluster id = %d\n", __func__, cluster);
break;
}
return temp;
}
#endif
static int ppm_get_spower_devid(enum ppm_cluster cluster)
{
return 0;
}
int ppm_platform_init(void)
{
#ifdef PPM_TODO
unsigned int cpu;
int i;
for_each_ppm_clusters(i) {
cpu = ppm_main_info.cluster_info[i].cpu_id;
cpu_dev[i] = get_cpu_device(cpu);
if (!cpu_dev[i]) {
ppm_err("failed to get cpu%d device\n", cpu);
return 0;
}
proc_reg[i] = regulator_get_exclusive(cpu_dev[i], "proc");
if (IS_ERR(proc_reg[i])) {
ppm_err("failed to get cpu%d proc regulator\n", cpu);
return 0;
}
}
#endif
#ifdef CONFIG_CPU_FREQ
cpufreq_register_notifier(
&ppm_cpu_freq_notifier, CPUFREQ_TRANSITION_NOTIFIER);
#endif
return 0;
}
void ppm_update_req_by_pwr(struct ppm_policy_req *req)
{
ppm_cobra_update_limit(req);
}
int ppm_find_pwr_idx(struct ppm_cluster_status *cluster_status)
{
unsigned int pwr_idx = 0;
int i;
if (!cobra_init_done) {
ppm_warn("@%s: cobra_init_done is 0!\n", __func__);
return -1; /* wait cobra init */
}
for_each_ppm_clusters(i) {
int core = cluster_status[i].core_num;
int opp = cluster_status[i].freq_idx;
#ifdef CONFIG_MTK_UNIFY_POWER
if (core > 0 && opp >= 0 && opp < DVFS_OPP_NUM) {
pwr_idx += cobra_tbl->basic_pwr_tbl
[CORE_NUM_L*i+core-1][opp].power_idx;
}
#else
pwr_idx += 100;
#endif
ppm_ver("[%d] core = %d, opp = %d\n", i, core, opp);
}
if (!pwr_idx) {
ppm_warn("@%s: pwr_idx is 0!\n", __func__);
return -1; /* not found */
}
ppm_ver("@%s: pwr_idx = %d\n", __func__, pwr_idx);
return pwr_idx;
}
int ppm_get_min_pwr_idx(void)
{
struct ppm_cluster_status status[NR_PPM_CLUSTERS];
int i;
for_each_ppm_clusters(i) {
/* use max core for ACAO */
status[i].core_num = get_cluster_max_cpu_core(i);
status[i].freq_idx = get_cluster_min_cpufreq_idx(i);
}
return ppm_find_pwr_idx(status);
}
int ppm_get_max_pwr_idx(void)
{
struct ppm_cluster_status status[NR_PPM_CLUSTERS];
int i;
for_each_ppm_clusters(i) {
status[i].core_num = get_cluster_max_cpu_core(i);
status[i].freq_idx = get_cluster_max_cpufreq_idx(i);
}
return ppm_find_pwr_idx(status);
}
unsigned int ppm_calc_total_power(struct ppm_cluster_status *cluster_status,
unsigned int cluster_num, unsigned int percentage)
{
unsigned int dynamic, lkg, total, budget = 0;
int i;
ktime_t now, delta;
for (i = 0; i < cluster_num; i++) {
int core = cluster_status[i].core_num;
int opp = cluster_status[i].freq_idx;
if (core != 0 && opp >= 0 && opp < DVFS_OPP_NUM) {
now = ktime_get();
#ifdef CONFIG_MTK_UNIFY_POWER
dynamic =
upower_get_power(i, opp, UPOWER_DYN) / 1000;
lkg =
mt_ppm_get_leakage_mw((enum ppm_cluster_lkg)i);
total =
((((dynamic * 100 + percentage - 1) /
percentage) + lkg) * core) +
((upower_get_power(
i + NR_PPM_CLUSTERS,
opp,
UPOWER_DYN) +
upower_get_power(
i + NR_PPM_CLUSTERS,
opp,
UPOWER_LKG)) / 1000);
#else
dynamic = 100;
lkg = 50;
total = dynamic + lkg;
#endif
budget += total;
delta = ktime_sub(ktime_get(), now);
ppm_dbg(DLPT,
"(%d):OPP/V/core/Lkg/total = %d/%d/%d/%d/%d(time = %lldus)\n",
i, opp, cluster_status[i].volt,
cluster_status[i].core_num,
lkg, total, ktime_to_us(delta));
}
}
if (!budget) {
ppm_warn("@%s: pwr_idx is 0!\n", __func__);
return -1; /* not found */
}
ppm_dbg(DLPT, "@%s: total budget = %d\n", __func__, budget);
return budget;
}
unsigned int mt_ppm_get_leakage_mw(enum ppm_cluster_lkg cluster)
{
int temp, dev_id, volt;
unsigned int mw = 0;
/* read total leakage */
if (cluster >= TOTAL_CLUSTER_LKG) {
struct ppm_cluster_status cl_status[NR_PPM_CLUSTERS];
int i;
ppm_get_cluster_status(cl_status);
for_each_ppm_clusters(i) {
if (!cl_status[i].core_num)
continue;
#ifdef CONFIG_THERMAL
temp = ppm_get_cpu_temp((enum ppm_cluster)i);
#else
temp = 85;
#endif
volt = ppm_get_cur_volt(i) / 1000;
dev_id = ppm_get_spower_devid((enum ppm_cluster)i);
if (dev_id < 0)
return 0;
mw += mt_spower_get_leakage(dev_id, volt, temp);
}
} else {
#ifdef CONFIG_THERMAL
temp = ppm_get_cpu_temp((enum ppm_cluster)cluster);
#else
temp = 85;
#endif
volt = ppm_get_cur_volt((enum ppm_cluster)cluster) / 1000;
dev_id = ppm_get_spower_devid((enum ppm_cluster)cluster);
if (dev_id < 0)
return 0;
mw = mt_spower_get_leakage(dev_id, volt, temp);
}
return mw;
}
@@ -0,0 +1,162 @@
/* SPDX-License-Identifier: GPL-2.0 */
/*
* Copyright (c) 2019 MediaTek Inc.
*/
#ifndef __MT_PPM_PLATFORM_H__
#define __MT_PPM_PLATFORM_H__
#ifdef __cplusplus
extern "C" {
#endif
#include "mtk_ppm_api.h"
#include <linux/cpufreq.h>
#ifdef CONFIG_MTK_TINYSYS_SSPM_SUPPORT
/* #define PPM_SSPM_SUPPORT (0) */
#endif
/*==============================================================*/
/* Macros */
/*==============================================================*/
/* TODO: remove these workaround for k49 migration */
#define NO_MTK_TRACE (1)
#define NO_SCHEDULE_API (1)
#define DYNAMIC_TABLE2REAL_PERCENTAGE (58)
/* for COBRA algo */
#define PPM_COBRA_USE_CORE_LIMIT (1)
#define PPM_COBRA_RUNTIME_CALC_DELTA (1)
#ifdef PPM_COBRA_USE_CORE_LIMIT
#define PPM_COBRA_MAX_FREQ_IDX (COBRA_OPP_NUM)
#else
#define PPM_COBRA_MAX_FREQ_IDX (COBRA_OPP_NUM-1)
#endif
#define COBRA_OPP_NUM (DVFS_OPP_NUM)
#define TOTAL_CORE_NUM (CORE_NUM_L+CORE_NUM_B)
#define CORE_NUM_L (6)
#define CORE_NUM_B (2)
#define PPM_COBRA_TBL_SRAM_ADDR (0x0011B800)
#define PPM_COBRA_TBL_SRAM_SIZE \
(sizeof(struct ppm_cobra_basic_pwr_data) \
*TOTAL_CORE_NUM*COBRA_OPP_NUM)
#ifdef PPM_SSPM_SUPPORT
/* online core to SSPM */
#define PPM_ONLINE_CORE_SRAM_ADDR \
(PPM_COBRA_TBL_SRAM_ADDR + PPM_COBRA_TBL_SRAM_SIZE)
#endif
/* other policy settings */
#define PTPOD_FREQ_IDX (7)
#define PWRTHRO_BAT_PER_MW (600)
#define PWRTHRO_BAT_OC_MW (600)
#define PWRTHRO_LOW_BAT_LV1_MW (600)
#define PWRTHRO_LOW_BAT_LV2_MW (600)
#define DVFS_OPP_NUM (16)
#define get_cluster_ptpod_fix_freq_idx(id) (mt_cpufreq_find_Vboot_idx(id))
#define get_cluster_cpu_core(id) \
(id ? CORE_NUM_B : CORE_NUM_L)
/*==============================================================*/
/* Enum */
/*==============================================================*/
enum ppm_cluster {
PPM_CLUSTER_L = 0,
PPM_CLUSTER_B,
NR_PPM_CLUSTERS,
};
enum ppm_cobra_lookup_type {
LOOKUP_BY_BUDGET,
LOOKUP_BY_LIMIT,
NR_PPM_COBRA_LOOKUP_TYPE,
};
/*==============================================================*/
/* Data Structures */
/*==============================================================*/
struct ppm_cobra_basic_pwr_data {
unsigned short perf_idx;
unsigned short power_idx;
};
struct ppm_cobra_delta_data {
unsigned int delta_pwr;
};
struct ppm_cobra_data {
struct ppm_cobra_basic_pwr_data
basic_pwr_tbl[TOTAL_CORE_NUM][DVFS_OPP_NUM];
};
struct ppm_cobra_lookup {
unsigned int budget;
struct {
unsigned int opp;
unsigned int core;
} limit[NR_PPM_CLUSTERS];
};
/*==============================================================*/
/* Global Variables */
/*==============================================================*/
extern struct ppm_cobra_data *cobra_tbl;
extern struct ppm_cobra_lookup cobra_lookup_data;
extern int cobra_init_done;
/*==============================================================*/
/* APIs */
/*==============================================================*/
extern unsigned int ppm_calc_total_power(
struct ppm_cluster_status *cluster_status,
unsigned int cluster_num,
unsigned int percentage);
extern int ppm_platform_init(void);
/* COBRA algo */
extern void ppm_cobra_update_core_limit(unsigned int cluster, int limit);
extern void ppm_cobra_update_freq_limit(unsigned int cluster, int limit);
extern void ppm_cobra_update_limit(void *user_req);
extern void ppm_cobra_init(void);
extern void ppm_cobra_dump_tbl(struct seq_file *m);
extern void ppm_cobra_lookup_get_result(
struct seq_file *m, enum ppm_cobra_lookup_type type);
extern void mt_ppm_set_dvfs_table(unsigned int cpu,
struct cpufreq_frequency_table *tbl,
unsigned int num, enum dvfs_table_type type);
extern unsigned int ppm_get_cur_freq(enum ppm_cluster cluster);
static inline void arch_get_cluster_cpus(struct cpumask *cpus, int cluster_id)
{
int cpu = 0;
cpumask_clear(cpus);
if (cluster_id == 0) {
cpu = 0;
while (cpu < CORE_NUM_L) {
cpumask_set_cpu(cpu, cpus);
cpu++;
}
} else {
cpu = CORE_NUM_L;
while (cpu < TOTAL_CORE_NUM) {
cpumask_set_cpu(cpu, cpus);
cpu++;
}
}
}
#ifdef __cplusplus
}
#endif
#endif
@@ -0,0 +1,38 @@
// SPDX-License-Identifier: GPL-2.0
/*
* Copyright (c) 2019 MediaTek Inc.
*/
#include <linux/slab.h>
#include <linux/string.h>
#include <linux/kernel.h>
#include <linux/types.h>
#include "mtk_ppm_platform.h"
#include "mtk_ppm_internal.h"
static int __init ppm_power_data_init(void)
{
ppm_lock(&ppm_main_info.lock);
mt_ppm_set_dvfs_table(0, NULL, 0, 0);
ppm_cobra_init();
ppm_platform_init();
#ifdef PPM_SSPM_SUPPORT
ppm_ipi_init(0, PPM_COBRA_TBL_SRAM_ADDR);
#endif
ppm_unlock(&ppm_main_info.lock);
ppm_info("power data init done!\n");
return 0;
}
/* should be run after cpufreq and upower init */
late_initcall(ppm_power_data_init);
@@ -0,0 +1,162 @@
/* SPDX-License-Identifier: GPL-2.0 */
/*
* Copyright (c) 2019 MediaTek Inc.
*/
#ifndef __MT_PPM_PLATFORM_H__
#define __MT_PPM_PLATFORM_H__
#ifdef __cplusplus
extern "C" {
#endif
#include "mtk_ppm_api.h"
#include <linux/cpufreq.h>
#ifdef CONFIG_MTK_TINYSYS_SSPM_SUPPORT
/* #define PPM_SSPM_SUPPORT (0) */
#endif
/*==============================================================*/
/* Macros */
/*==============================================================*/
/* TODO: remove these workaround for k49 migration */
#define NO_MTK_TRACE (1)
#define NO_SCHEDULE_API (1)
#define DYNAMIC_TABLE2REAL_PERCENTAGE (58)
/* for COBRA algo */
#define PPM_COBRA_USE_CORE_LIMIT (1)
#define PPM_COBRA_RUNTIME_CALC_DELTA (1)
#ifdef PPM_COBRA_USE_CORE_LIMIT
#define PPM_COBRA_MAX_FREQ_IDX (COBRA_OPP_NUM)
#else
#define PPM_COBRA_MAX_FREQ_IDX (COBRA_OPP_NUM-1)
#endif
#define COBRA_OPP_NUM (DVFS_OPP_NUM)
#define TOTAL_CORE_NUM (CORE_NUM_L+CORE_NUM_B)
#define CORE_NUM_L (6)
#define CORE_NUM_B (2)
#define PPM_COBRA_TBL_SRAM_ADDR (0x0011B800)
#define PPM_COBRA_TBL_SRAM_SIZE \
(sizeof(struct ppm_cobra_basic_pwr_data) \
*TOTAL_CORE_NUM*COBRA_OPP_NUM)
#ifdef PPM_SSPM_SUPPORT
/* online core to SSPM */
#define PPM_ONLINE_CORE_SRAM_ADDR \
(PPM_COBRA_TBL_SRAM_ADDR + PPM_COBRA_TBL_SRAM_SIZE)
#endif
/* other policy settings */
#define PTPOD_FREQ_IDX (7)
#define PWRTHRO_BAT_PER_MW (600)
#define PWRTHRO_BAT_OC_MW (600)
#define PWRTHRO_LOW_BAT_LV1_MW (600)
#define PWRTHRO_LOW_BAT_LV2_MW (600)
#define DVFS_OPP_NUM (16)
#define get_cluster_ptpod_fix_freq_idx(id) (mt_cpufreq_find_Vboot_idx(id))
#define get_cluster_cpu_core(id) \
(id ? CORE_NUM_B : CORE_NUM_L)
/*==============================================================*/
/* Enum */
/*==============================================================*/
enum ppm_cluster {
PPM_CLUSTER_L = 0,
PPM_CLUSTER_B,
NR_PPM_CLUSTERS,
};
enum ppm_cobra_lookup_type {
LOOKUP_BY_BUDGET,
LOOKUP_BY_LIMIT,
NR_PPM_COBRA_LOOKUP_TYPE,
};
/*==============================================================*/
/* Data Structures */
/*==============================================================*/
struct ppm_cobra_basic_pwr_data {
unsigned short perf_idx;
unsigned short power_idx;
};
struct ppm_cobra_delta_data {
unsigned int delta_pwr;
};
struct ppm_cobra_data {
struct ppm_cobra_basic_pwr_data
basic_pwr_tbl[TOTAL_CORE_NUM][DVFS_OPP_NUM];
};
struct ppm_cobra_lookup {
unsigned int budget;
struct {
unsigned int opp;
unsigned int core;
} limit[NR_PPM_CLUSTERS];
};
/*==============================================================*/
/* Global Variables */
/*==============================================================*/
extern struct ppm_cobra_data *cobra_tbl;
extern struct ppm_cobra_lookup cobra_lookup_data;
extern int cobra_init_done;
/*==============================================================*/
/* APIs */
/*==============================================================*/
extern unsigned int ppm_calc_total_power(
struct ppm_cluster_status *cluster_status,
unsigned int cluster_num,
unsigned int percentage);
extern int ppm_platform_init(void);
/* COBRA algo */
extern void ppm_cobra_update_core_limit(unsigned int cluster, int limit);
extern void ppm_cobra_update_freq_limit(unsigned int cluster, int limit);
extern void ppm_cobra_update_limit(void *user_req);
extern void ppm_cobra_init(void);
extern void ppm_cobra_dump_tbl(struct seq_file *m);
extern void ppm_cobra_lookup_get_result(
struct seq_file *m, enum ppm_cobra_lookup_type type);
extern void mt_ppm_set_dvfs_table(unsigned int cpu,
struct cpufreq_frequency_table *tbl,
unsigned int num, enum dvfs_table_type type);
extern unsigned int ppm_get_cur_freq(enum ppm_cluster cluster);
static inline void arch_get_cluster_cpus(struct cpumask *cpus, int cluster_id)
{
int cpu = 0;
cpumask_clear(cpus);
if (cluster_id == 0) {
cpu = 0;
while (cpu < CORE_NUM_L) {
cpumask_set_cpu(cpu, cpus);
cpu++;
}
} else {
cpu = CORE_NUM_L;
while (cpu < TOTAL_CORE_NUM) {
cpumask_set_cpu(cpu, cpus);
cpu++;
}
}
}
#ifdef __cplusplus
}
#endif
#endif
@@ -0,0 +1,37 @@
// SPDX-License-Identifier: GPL-2.0
/*
* Copyright (c) 2019 MediaTek Inc.
*/
#include <linux/cpufreq.h>
#include "mtk_ppm_internal.h"
/* APIs */
void mt_ppm_register_client(enum ppm_client client,
void (*limit)(struct ppm_client_req req))
{
FUNC_ENTER(FUNC_LV_API);
ppm_lock(&ppm_main_info.lock);
/* init client */
switch (client) {
case PPM_CLIENT_DVFS:
ppm_main_info.client_info[client].name = "DVFS";
break;
case PPM_CLIENT_HOTPLUG:
ppm_main_info.client_info[client].name = "HOTPLUG";
break;
default:
ppm_main_info.client_info[client].name = "UNKNOWN";
break;
}
ppm_main_info.client_info[client].client = client;
ppm_main_info.client_info[client].limit_cb = limit;
ppm_unlock(&ppm_main_info.lock);
FUNC_EXIT(FUNC_LV_API);
}
@@ -0,0 +1,119 @@
// SPDX-License-Identifier: GPL-2.0
/*
* Copyright (c) 2019 MediaTek Inc.
*/
#include <linux/cpufreq.h>
#include "mtk_ppm_api.h"
void mt_ppm_set_dvfs_table(unsigned int cpu,
struct cpufreq_frequency_table *tbl,
unsigned int num, enum dvfs_table_type type)
{
}
void mt_ppm_register_client(enum ppm_client client,
void (*limit)(struct ppm_client_req req))
{
}
/* SYS boost policy */
void mt_ppm_sysboost_core(enum ppm_sysboost_user user, unsigned int core_num)
{
}
void mt_ppm_sysboost_freq(enum ppm_sysboost_user user, unsigned int freq)
{
}
void mt_ppm_sysboost_set_core_limit(enum ppm_sysboost_user user,
unsigned int cluster, int min_core, int max_core)
{
}
void mt_ppm_sysboost_set_freq_limit(enum ppm_sysboost_user user,
unsigned int cluster, int min_freq, int max_freq)
{
}
/* DLPT policy */
void mt_ppm_dlpt_set_limit_by_pbm(unsigned int limited_power)
{
}
void mt_ppm_dlpt_kick_PBM(struct ppm_cluster_status *cluster_status,
unsigned int cluster_num)
{
}
/* Thermal policy */
void mt_ppm_cpu_thermal_protect(unsigned int limited_power)
{
}
unsigned int mt_ppm_thermal_get_min_power(void)
{
return 0;
}
unsigned int mt_ppm_thermal_get_max_power(void)
{
return 0;
}
unsigned int mt_ppm_thermal_get_cur_power(void)
{
return 0;
}
/* User limit policy */
unsigned int mt_ppm_userlimit_cpu_core(unsigned int cluster_num,
struct ppm_limit_data *data)
{
return 0;
}
unsigned int mt_ppm_userlimit_cpu_freq(unsigned int cluster_num,
struct ppm_limit_data *data)
{
return 0;
}
unsigned int mt_ppm_userlimit_freq_limit_by_others(unsigned int cluster)
{
return 0;
}
void ppm_game_mode_change_cb(int is_game_mode)
{
}
/* Force limit policy */
unsigned int mt_ppm_forcelimit_cpu_core(unsigned int cluster_num,
struct ppm_limit_data *data)
{
return 0;
}
/* PTPOD policy */
void mt_ppm_ptpod_policy_activate(void)
{
}
void mt_ppm_ptpod_policy_deactivate(void)
{
}
unsigned int mt_ppm_get_leakage_mw(enum ppm_cluster_lkg cluster)
{
return 0;
}
/* CPI */
unsigned int ppm_get_cluster_cpi(unsigned int cluster)
{
return 0;
}
@@ -0,0 +1,466 @@
// SPDX-License-Identifier: GPL-2.0
/*
* Copyright (c) 2019 MediaTek Inc.
*/
#include <linux/proc_fs.h>
#include <linux/seq_file.h>
#include <linux/uaccess.h>
#include "mtk_ppm_internal.h"
#define PPM_MODE_NAME_LEN 16
/* procfs dir for policies */
struct proc_dir_entry *policy_dir;
struct proc_dir_entry *profile_dir;
struct proc_dir_entry *cpi_dir;
unsigned int ppm_debug;
unsigned int ppm_func_lv_mask;
char *ppm_copy_from_user_for_proc(const char __user *buffer, size_t count)
{
char *buf = (char *)__get_free_page(GFP_USER);
if (!buf)
return NULL;
if (count >= PAGE_SIZE)
goto out;
if (copy_from_user(buf, buffer, count))
goto out;
buf[count] = '\0';
return buf;
out:
free_page((unsigned long)buf);
return NULL;
}
static int ppm_func_debug_proc_show(struct seq_file *m, void *v)
{
seq_printf(m, "ppm func lv debug mask = 0x%x\n", ppm_func_lv_mask);
return 0;
}
static ssize_t ppm_func_debug_proc_write(struct file *file,
const char __user *buffer, size_t count, loff_t *pos)
{
unsigned int func_dbg_lv;
char *buf = ppm_copy_from_user_for_proc(buffer, count);
if (!buf)
return -EINVAL;
if (!kstrtouint(buf, 10, &func_dbg_lv))
ppm_func_lv_mask = func_dbg_lv;
else
ppm_err("echo func_dbg_lv (dec) > /proc/ppm/func_debug\n");
free_page((unsigned long)buf);
return count;
}
static int ppm_debug_proc_show(struct seq_file *m, void *v)
{
seq_printf(m, "ppm debug (log level) = %d\n", ppm_debug);
return 0;
}
static ssize_t ppm_debug_proc_write(struct file *file,
const char __user *buffer, size_t count, loff_t *pos)
{
unsigned int dbg_lv;
char *buf = ppm_copy_from_user_for_proc(buffer, count);
if (!buf)
return -EINVAL;
if (!kstrtouint(buf, 10, &dbg_lv))
ppm_debug = dbg_lv;
else
ppm_err("echo dbg_lv (dec) > /proc/ppm/debug\n");
free_page((unsigned long)buf);
return count;
}
static int ppm_enabled_proc_show(struct seq_file *m, void *v)
{
if (ppm_main_info.is_enabled == true)
seq_puts(m, "ppm is enabled\n");
else
seq_puts(m, "ppm is disabled\n");
return 0;
}
static ssize_t ppm_enabled_proc_write(struct file *file,
const char __user *buffer, size_t count, loff_t *pos)
{
unsigned int enabled;
char *buf = ppm_copy_from_user_for_proc(buffer, count);
if (!buf)
return -EINVAL;
ppm_lock(&ppm_main_info.lock);
if (!kstrtouint(buf, 10, &enabled)) {
ppm_main_info.is_enabled = (enabled) ? true : false;
if (!ppm_main_info.is_enabled) {
int i;
struct ppm_client_req *c_req =
&(ppm_main_info.client_req);
struct ppm_client_req *last_req =
&(ppm_main_info.last_req);
/* send default limit to client */
ppm_main_clear_client_req(c_req);
#ifdef PPM_SSPM_SUPPORT
/* update limit to SSPM first */
ppm_ipi_update_limit(*c_req);
#endif
for_each_ppm_clients(i) {
if (!ppm_main_info.client_info[i].limit_cb)
continue;
ppm_main_info.client_info[i].limit_cb(*c_req);
}
memcpy(last_req->cpu_limit, c_req->cpu_limit,
ppm_main_info.cluster_num *
sizeof(*c_req->cpu_limit));
ppm_info("PPM disabled, send no limit to clinet!\n");
}
} else
ppm_err("echo [0/1] > /proc/ppm/enabled\n");
ppm_unlock(&ppm_main_info.lock);
free_page((unsigned long)buf);
return count;
}
static int ppm_exclusive_core_proc_show(struct seq_file *m, void *v)
{
seq_printf(m, "ppm exclusive core = %*pbl\n",
cpumask_pr_args(ppm_main_info.exclusive_core));
return 0;
}
static ssize_t ppm_exclusive_core_proc_write(struct file *file,
const char __user *buffer, size_t count, loff_t *pos)
{
unsigned int mask;
int cpu;
char *buf = ppm_copy_from_user_for_proc(buffer, count);
if (!buf)
return -EINVAL;
if (!kstrtouint(buf, 10, &mask)) {
ppm_lock(&ppm_main_info.lock);
cpumask_clear(ppm_main_info.exclusive_core);
for_each_present_cpu(cpu) {
if (mask & 0x1)
cpumask_set_cpu(cpu,
ppm_main_info.exclusive_core);
mask >>= 1;
}
ppm_unlock(&ppm_main_info.lock);
ppm_info("update exclusive core = %*pbl\n",
cpumask_pr_args(ppm_main_info.exclusive_core));
mt_ppm_main();
} else
ppm_err("echo <bitmask> > /proc/ppm/exclusive_core\n");
free_page((unsigned long)buf);
return count;
}
static int ppm_dump_power_table_proc_show(struct seq_file *m, void *v)
{
ppm_cobra_dump_tbl(m);
return 0;
}
static int ppm_dump_policy_list_proc_show(struct seq_file *m, void *v)
{
struct ppm_policy_data *pos;
unsigned int i = 0, j = 0;
ppm_lock(&ppm_main_info.lock);
seq_puts(m, "\nFinal limit:\n");
for_each_ppm_clusters(j) {
seq_printf(m, "cluster %d: (%d)(%d)(%d)(%d)\n", j,
ppm_main_info.last_req.cpu_limit[j].min_cpufreq_idx,
ppm_main_info.last_req.cpu_limit[j].max_cpufreq_idx,
ppm_main_info.last_req.cpu_limit[j].min_cpu_core,
ppm_main_info.last_req.cpu_limit[j].max_cpu_core);
}
seq_puts(m, "\nPolicy limit:\n");
list_for_each_entry(pos, &ppm_main_info.policy_list, link) {
ppm_lock(&pos->lock);
seq_printf(m, "[%d] %s (priority: %d)\n",
i, pos->name, pos->priority);
seq_printf(m, "is_enabled = %d, is_activated = %d\n",
pos->is_enabled, pos->is_activated);
seq_printf(m, "req_perf_idx = %d, req_power_budget = %d\n",
pos->req.perf_idx, pos->req.power_budget);
for_each_ppm_clusters(j) {
seq_printf(m, "cluster %d: (%d)(%d)(%d)(%d)\n", j,
pos->req.limit[j].min_cpufreq_idx,
pos->req.limit[j].max_cpufreq_idx,
pos->req.limit[j].min_cpu_core,
pos->req.limit[j].max_cpu_core);
}
seq_puts(m, "\n");
ppm_unlock(&pos->lock);
i++;
}
ppm_unlock(&ppm_main_info.lock);
return 0;
}
static int ppm_policy_status_proc_show(struct seq_file *m, void *v)
{
struct ppm_policy_data *pos;
list_for_each_entry_reverse(pos, &ppm_main_info.policy_list, link)
seq_printf(m, "[%d] %s: %s\n", pos->policy, pos->name,
(pos->is_enabled) ? "enabled" : "disabled");
seq_puts(m, "\nUsage: echo <idx> <1/0> > /proc/ppm/policy_status\n\n");
return 0;
}
static ssize_t ppm_policy_status_proc_write(struct file *file,
const char __user *buffer, size_t count, loff_t *pos)
{
struct ppm_policy_data *l_pos;
unsigned int policy_idx, enabled;
char *buf = ppm_copy_from_user_for_proc(buffer, count);
if (!buf)
return -EINVAL;
if (sscanf(buf, "%d %d", &policy_idx, &enabled) == 2) {
if (enabled > 1)
enabled = 1;
/* set mode status and notify policy via status change cb */
list_for_each_entry(l_pos,
&ppm_main_info.policy_list, link) {
if (l_pos->policy == policy_idx
&& l_pos->is_enabled != enabled) {
ppm_lock(&l_pos->lock);
l_pos->is_enabled =
(enabled) ? true : false;
if (!l_pos->is_enabled)
l_pos->is_activated = false;
if (l_pos->status_change_cb)
l_pos->status_change_cb(
l_pos->is_enabled);
ppm_unlock(&l_pos->lock);
mt_ppm_main();
break;
}
}
} else
ppm_err("Usage: echo <idx> <1/0> > /proc/ppm/policy_status\n");
free_page((unsigned long)buf);
return count;
}
static int ppm_dump_dvfs_table_proc_show(struct seq_file *m, void *v)
{
struct ppm_cluster_info *info = (struct ppm_cluster_info *)m->private;
unsigned int i;
if (!info->dvfs_tbl) {
ppm_err("DVFS table for cluster %d is NULL!\n",
info->cluster_id);
goto end;
}
for (i = 0; i < info->dvfs_opp_num; i++)
seq_printf(m, "%d ", info->dvfs_tbl[i].frequency);
seq_puts(m, "\n");
end:
return 0;
}
static int ppm_cobra_budget_to_limit_proc_show(struct seq_file *m, void *v)
{
ppm_cobra_lookup_get_result(m, LOOKUP_BY_BUDGET);
return 0;
}
static ssize_t ppm_cobra_budget_to_limit_proc_write(struct file *file,
const char __user *buffer, size_t count, loff_t *pos)
{
unsigned int budget;
char *buf = ppm_copy_from_user_for_proc(buffer, count);
if (!buf)
return -EINVAL;
if (!kstrtoint(buf, 10, &budget))
cobra_lookup_data.budget = budget;
else
ppm_err("echo <budget> > /proc/ppm/cobra_budget_to_limit\n");
free_page((unsigned long)buf);
return count;
}
static int ppm_cobra_limit_to_budget_proc_show(struct seq_file *m, void *v)
{
ppm_cobra_lookup_get_result(m, LOOKUP_BY_LIMIT);
return 0;
}
static ssize_t ppm_cobra_limit_to_budget_proc_write(struct file *file,
const char __user *buffer, size_t count, loff_t *pos)
{
char *tok, *tmp;
unsigned int i = 0, data;
char *buf = ppm_copy_from_user_for_proc(buffer, count);
if (!buf)
return -EINVAL;
tmp = buf;
while ((tok = strsep(&tmp, " ")) != NULL) {
if (i == NR_PPM_CLUSTERS * 2) {
ppm_err("@%s: number of arguments > %d!\n",
__func__, NR_PPM_CLUSTERS * 2);
goto out;
}
if (kstrtoint(tok, 10, &data)) {
ppm_err("@%s: Invalid input: %s\n", __func__, tok);
goto out;
} else {
if (i % 2) /* OPP */
cobra_lookup_data.limit[i/2].opp = data;
else /* core */
cobra_lookup_data.limit[i/2].core = data;
i++;
}
}
out:
free_page((unsigned long)buf);
return count;
}
PROC_FOPS_RW(func_debug);
PROC_FOPS_RW(debug);
PROC_FOPS_RW(enabled);
PROC_FOPS_RW(exclusive_core);
PROC_FOPS_RO(dump_power_table);
PROC_FOPS_RO(dump_policy_list);
PROC_FOPS_RW(policy_status);
PROC_FOPS_RO(dump_dvfs_table);
PROC_FOPS_RW(cobra_budget_to_limit);
PROC_FOPS_RW(cobra_limit_to_budget);
int ppm_procfs_init(void)
{
struct proc_dir_entry *dir = NULL;
int i;
char str[32];
struct pentry {
const char *name;
const struct file_operations *fops;
};
const struct pentry entries[] = {
PROC_ENTRY(func_debug),
PROC_ENTRY(debug),
PROC_ENTRY(enabled),
PROC_ENTRY(exclusive_core),
PROC_ENTRY(dump_power_table),
PROC_ENTRY(dump_policy_list),
PROC_ENTRY(policy_status),
PROC_ENTRY(cobra_budget_to_limit),
PROC_ENTRY(cobra_limit_to_budget),
};
dir = proc_mkdir("ppm", NULL);
if (!dir) {
ppm_err("@%s: fail to create /proc/ppm dir\n", __func__);
return -ENOMEM;
}
/* mkdir for policies */
policy_dir = proc_mkdir("policy", dir);
if (!policy_dir) {
ppm_err("fail to create /proc/ppm/policy dir\n");
return -ENOMEM;
}
profile_dir = proc_mkdir("profile", dir);
if (!profile_dir) {
ppm_err("fail to create /proc/ppm/profile dir\n");
return -ENOMEM;
}
cpi_dir = proc_mkdir("cpi", dir);
if (!cpi_dir) {
ppm_err("fail to create /proc/ppm/cpi dir\n");
return -ENOMEM;
}
for (i = 0; i < ARRAY_SIZE(entries); i++) {
if (!proc_create(entries[i].name, 0664,
dir, entries[i].fops))
ppm_err("%s(), create /proc/ppm/%s failed\n",
__func__, entries[i].name);
}
for_each_ppm_clusters(i) {
sprintf(str, "dump_cluster_%d_dvfs_table", i);
if (!proc_create_data(str, 0644,
dir, &ppm_dump_dvfs_table_proc_fops,
&ppm_main_info.cluster_info[i]))
ppm_err("%s(), create /proc/ppm/%s failed\n",
__func__, str);
}
return 0;
}
@@ -0,0 +1,163 @@
// SPDX-License-Identifier: GPL-2.0
/*
* Copyright (c) 2019 MediaTek Inc.
*/
#include "mtk_ppm_internal.h"
#ifdef PPM_SSPM_SUPPORT
#include <linux/ktime.h>
#include "sspm_ipi.h"
static int ppm_ipi_to_sspm_command(unsigned char cmd,
struct ppm_ipi_data *data)
{
int ack_data = 0, ret = 0, i, opt;
ktime_t now;
unsigned long long delta;
BUILD_BUG_ON(sizeof(struct ppm_ipi_data) != PPM_D_LEN * sizeof(int));
ppm_dbg(IPI, "@%s: cmd=0x%x\n", __func__, cmd);
now = ktime_get();
opt = IPI_OPT_POLLING;
switch (cmd) {
case PPM_IPI_INIT:
data->cmd = cmd;
ppm_dbg(IPI,
"efuse_val=%d,cobra_tbl_addr=0x%x,dvfs_tbl_type=%d\n",
data->u.init.efuse_val,
data->u.init.cobra_tbl_addr,
data->u.init.dvfs_tbl_type);
ret = sspm_ipi_send_sync(IPI_ID_PPM,
opt, data, PPM_D_LEN, &ack_data, 1);
if (ret != 0)
ppm_err("sspm_ipi_send_sync failed, ret=%d\n", ret);
else if (ack_data < 0) {
ret = ack_data;
ppm_err("cmd(0x%x) return %d\n", cmd, ret);
}
break;
case PPM_IPI_UPDATE_LIMIT:
data->cmd = cmd;
for_each_ppm_clusters(i) {
ppm_dbg(IPI, "cluster %d limit: (%d)(%d) (%d) (%d)\n",
i,
data->u.update_limit.cluster_limit[i].min_cpufreq_idx,
data->u.update_limit.cluster_limit[i].max_cpufreq_idx,
data->u.update_limit.cluster_limit[i].max_cpu_core,
data->u.update_limit.cluster_limit[i].advise_freq_idx
);
}
ret = sspm_ipi_send_sync(IPI_ID_PPM,
opt, data, PPM_D_LEN, &ack_data, 1);
if (ret != 0)
ppm_err("sspm_ipi_send_sync failed, ret=%d\n", ret);
else if (ack_data < 0) {
ret = ack_data;
ppm_err("cmd(0x%x) return %d\n", cmd, ret);
}
break;
case PPM_IPI_THERMAL_LIMIT_TEST:
data->cmd = cmd;
ppm_dbg(IPI, "thermal test budget = %d\n",
data->u.thermal_limit_test.budget);
ret = sspm_ipi_send_sync(IPI_ID_PPM,
opt, data, PPM_D_LEN, &ack_data, 1);
if (ret != 0)
ppm_err("sspm_ipi_send_sync failed, ret=%d\n", ret);
else if (ack_data < 0) {
ret = ack_data;
ppm_err("cmd(0x%x) return %d\n", cmd, ret);
}
break;
case PPM_IPI_PTPOD_TEST:
data->cmd = cmd;
ppm_dbg(IPI, "ptpod test activate=%d\n",
data->u.ptpod_test.activate);
ret = sspm_ipi_send_sync(IPI_ID_PPM,
opt, data, PPM_D_LEN, &ack_data, 1);
if (ret != 0)
ppm_err("sspm_ipi_send_sync failed, ret=%d\n", ret);
else if (ack_data < 0) {
ret = ack_data;
ppm_err("cmd(0x%x) return %d\n", cmd, ret);
}
break;
default:
ppm_err("@%s cmd(0x%x) wrong!!!\n", __func__, cmd);
return -1;
}
delta = ktime_to_us(ktime_sub(ktime_get(), now));
ppm_profile_update_ipi_exec_time(cmd, delta);
return ret;
}
void ppm_ipi_init(unsigned int efuse_val, unsigned int cobra_tbl_addr)
{
struct ppm_ipi_data data;
data.u.init.efuse_val = efuse_val;
data.u.init.cobra_tbl_addr = cobra_tbl_addr;
data.u.init.dvfs_tbl_type = (unsigned int)ppm_main_info.dvfs_tbl_type;
ppm_ipi_to_sspm_command(PPM_IPI_INIT, &data);
}
void ppm_ipi_update_limit(struct ppm_client_req req)
{
struct ppm_ipi_data data;
int i;
for (i = 0; i < req.cluster_num; i++) {
data.u.update_limit.cluster_limit[i].min_cpufreq_idx =
(char)req.cpu_limit[i].min_cpufreq_idx;
data.u.update_limit.cluster_limit[i].max_cpufreq_idx =
(char)req.cpu_limit[i].max_cpufreq_idx;
data.u.update_limit.cluster_limit[i].max_cpu_core =
(unsigned char)req.cpu_limit[i].max_cpu_core;
data.u.update_limit.cluster_limit[i].advise_freq_idx =
(req.cpu_limit[i].advise_cpufreq_idx < 0)
? 0xFF : (char)req.cpu_limit[i].advise_cpufreq_idx;
}
ppm_ipi_to_sspm_command(PPM_IPI_UPDATE_LIMIT, &data);
}
void ppm_ipi_thermal_limit_test(unsigned int budget)
{
struct ppm_ipi_data data;
data.u.thermal_limit_test.budget = budget;
ppm_ipi_to_sspm_command(PPM_IPI_THERMAL_LIMIT_TEST, &data);
}
void ppm_ipi_ptpod_test(unsigned int activate)
{
struct ppm_ipi_data data;
data.u.ptpod_test.activate = activate;
ppm_ipi_to_sspm_command(PPM_IPI_PTPOD_TEST, &data);
}
#endif
File diff suppressed because it is too large. Load diff
@@ -0,0 +1,279 @@
// SPDX-License-Identifier: GPL-2.0
/*
* Copyright (c) 2019 MediaTek Inc.
*/
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/proc_fs.h>
#include <linux/seq_file.h>
#include <linux/uaccess.h>
#include "mtk_ppm_internal.h"
#include "mtk_ppm_platform.h"
static unsigned int ppm_dlpt_pwr_budget_preprocess(
unsigned int budget);
static unsigned int ppm_dlpt_pwr_budget_postprocess(
unsigned int budget, unsigned int pwr_idx);
static void ppm_dlpt_update_limit_cb(void);
static void ppm_dlpt_status_change_cb(bool enable);
static unsigned int dlpt_percentage_to_real_power;
/* other members will init by ppm_main */
static struct ppm_policy_data dlpt_policy = {
.name = __stringify(PPM_POLICY_DLPT),
.lock = __MUTEX_INITIALIZER(dlpt_policy.lock),
.policy = PPM_POLICY_DLPT,
.priority = PPM_POLICY_PRIO_POWER_BUDGET_BASE,
.update_limit_cb = ppm_dlpt_update_limit_cb,
.status_change_cb = ppm_dlpt_status_change_cb,
};
void __attribute__((weak)) kicker_pbm_by_cpu(
unsigned int loading, int core, int voltage)
{
}
void mt_ppm_dlpt_kick_PBM(struct ppm_cluster_status *cluster_status,
unsigned int cluster_num)
{
int power_idx;
unsigned int total_core = 0;
unsigned int max_volt = 0;
unsigned int budget = 0;
int i;
FUNC_ENTER(FUNC_LV_POLICY);
/* find power bgt in table, skip this round if idx not found */
power_idx = ppm_find_pwr_idx(cluster_status);
if (power_idx < 0)
goto end;
for (i = 0; i < cluster_num; i++) {
total_core += cluster_status[i].core_num;
max_volt = MAX(max_volt, cluster_status[i].volt);
}
budget = ppm_calc_total_power(cluster_status, cluster_num,
DYNAMIC_TABLE2REAL_PERCENTAGE);
if (!budget)
goto end;
budget = ppm_dlpt_pwr_budget_postprocess(
budget, (unsigned int)power_idx);
ppm_dbg(DLPT, "budget = %d(%d), total_core = %d, max_volt = %d\n",
budget, power_idx, total_core, max_volt);
#ifndef DISABLE_PBM_FEATURE
#ifdef CONFIG_MTK_RAM_CONSOLE
aee_rr_rec_ppm_waiting_for_pbm(1);
kicker_pbm_by_cpu(budget, total_core, max_volt);
aee_rr_rec_ppm_waiting_for_pbm(0);
#else
kicker_pbm_by_cpu(budget, total_core, max_volt);
#endif
#endif
end:
FUNC_EXIT(FUNC_LV_POLICY);
}
void mt_ppm_dlpt_set_limit_by_pbm(unsigned int limited_power)
{
unsigned int budget;
FUNC_ENTER(FUNC_LV_POLICY);
budget = ppm_dlpt_pwr_budget_preprocess(limited_power);
ppm_dbg(DLPT, "Get PBM notifier => budget = %d(%d)\n",
budget, limited_power);
ppm_lock(&dlpt_policy.lock);
if (!dlpt_policy.is_enabled) {
ppm_warn("@%s: dlpt policy is not enabled!\n", __func__);
ppm_unlock(&dlpt_policy.lock);
goto end;
}
dlpt_policy.req.power_budget = budget;
dlpt_policy.is_activated = (budget) ? true : false;
ppm_unlock(&dlpt_policy.lock);
mt_ppm_main();
end:
FUNC_EXIT(FUNC_LV_POLICY);
}
static unsigned int ppm_dlpt_pwr_budget_preprocess(unsigned int budget)
{
unsigned int percentage = dlpt_percentage_to_real_power;
if (!percentage)
percentage = 100;
return (budget * percentage + (100 - 1)) / 100;
}
static unsigned int ppm_dlpt_pwr_budget_postprocess(unsigned int budget,
unsigned int pwr_idx)
{
/* just calculate new ratio */
dlpt_percentage_to_real_power =
(pwr_idx * 100 + (budget - 1)) / budget;
ppm_dbg(DLPT, "new dlpt ratio = %d (%d/%d)\n",
dlpt_percentage_to_real_power, pwr_idx, budget);
return budget;
}
static void ppm_dlpt_update_limit_cb(void)
{
FUNC_ENTER(FUNC_LV_POLICY);
ppm_clear_policy_limit(&dlpt_policy);
/* update limit according to power budget */
ppm_update_req_by_pwr(&dlpt_policy.req);
FUNC_EXIT(FUNC_LV_POLICY);
}
static void ppm_dlpt_status_change_cb(bool enable)
{
FUNC_ENTER(FUNC_LV_POLICY);
ppm_ver("@%s: dlpt policy status changed to %d\n", __func__, enable);
FUNC_EXIT(FUNC_LV_POLICY);
}
static int ppm_dlpt_limit_proc_show(struct seq_file *m, void *v)
{
seq_printf(m, "limited power = %d\n", dlpt_policy.req.power_budget);
seq_printf(m, "PPM DLPT activate = %d\n", dlpt_policy.is_activated);
return 0;
}
static ssize_t ppm_dlpt_limit_proc_write(struct file *file,
const char __user *buffer, size_t count, loff_t *pos)
{
unsigned int limited_power;
char *buf = ppm_copy_from_user_for_proc(buffer, count);
if (!buf)
return -EINVAL;
if (!kstrtouint(buf, 10, &limited_power))
mt_ppm_dlpt_set_limit_by_pbm(limited_power);
else
ppm_err("@%s: Invalid input!\n", __func__);
free_page((unsigned long)buf);
return count;
}
static int ppm_dlpt_budget_trans_percentage_proc_show(
struct seq_file *m, void *v)
{
seq_printf(m, "trans_percentage = %d\n",
dlpt_percentage_to_real_power);
seq_printf(m, "PPM DLPT activate = %d\n",
dlpt_policy.is_activated);
return 0;
}
static ssize_t ppm_dlpt_budget_trans_percentage_proc_write(
struct file *file, const char __user *buffer,
size_t count, loff_t *pos)
{
unsigned int percentage;
char *buf = ppm_copy_from_user_for_proc(buffer, count);
if (!buf)
return -EINVAL;
if (!kstrtouint(buf, 10, &percentage)) {
if (!percentage)
ppm_err("percentage should not be 0!\n");
else
dlpt_percentage_to_real_power = percentage;
} else
ppm_err("@%s: Invalid input!\n", __func__);
free_page((unsigned long)buf);
return count;
}
PROC_FOPS_RW(dlpt_limit);
PROC_FOPS_RW(dlpt_budget_trans_percentage);
static int __init ppm_dlpt_policy_init(void)
{
int i, ret = 0;
struct pentry {
const char *name;
const struct file_operations *fops;
};
const struct pentry entries[] = {
PROC_ENTRY(dlpt_limit),
PROC_ENTRY(dlpt_budget_trans_percentage),
};
FUNC_ENTER(FUNC_LV_POLICY);
#ifdef DISABLE_DLPT_FEATURE
goto out;
#endif
/* create procfs */
for (i = 0; i < ARRAY_SIZE(entries); i++) {
if (!proc_create(entries[i].name, 0644,
policy_dir, entries[i].fops)) {
ppm_err("%s(), create /proc/ppm/policy/%s failed\n",
__func__, entries[i].name);
ret = -EINVAL;
goto out;
}
}
if (ppm_main_register_policy(&dlpt_policy)) {
ppm_err("@%s: dlpt policy register failed\n", __func__);
ret = -EINVAL;
goto out;
}
ppm_info("@%s: register %s done!\n", __func__, dlpt_policy.name);
out:
FUNC_EXIT(FUNC_LV_POLICY);
return ret;
}
static void __exit ppm_dlpt_policy_exit(void)
{
FUNC_ENTER(FUNC_LV_POLICY);
ppm_main_unregister_policy(&dlpt_policy);
FUNC_EXIT(FUNC_LV_POLICY);
}
module_init(ppm_dlpt_policy_init);
module_exit(ppm_dlpt_policy_exit);
@@ -0,0 +1,278 @@
// SPDX-License-Identifier: GPL-2.0
/*
* Copyright (c) 2019 MediaTek Inc.
*/
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/slab.h>
#include "mtk_ppm_internal.h"
static void ppm_forcelimit_update_limit_cb(void);
static void ppm_forcelimit_status_change_cb(bool enable);
/* other members will init by ppm_main */
static struct ppm_policy_data forcelimit_policy = {
.name = __stringify(PPM_POLICY_FORCE_LIMIT),
.lock = __MUTEX_INITIALIZER(forcelimit_policy.lock),
.policy = PPM_POLICY_FORCE_LIMIT,
.priority = PPM_POLICY_PRIO_HIGHEST,
.update_limit_cb = ppm_forcelimit_update_limit_cb,
.status_change_cb = ppm_forcelimit_status_change_cb,
};
struct ppm_userlimit_data forcelimit_data = {
.is_freq_limited_by_user = false,
.is_core_limited_by_user = false,
};
/* MUST in lock */
static bool ppm_forcelimit_is_policy_active(void)
{
if (!forcelimit_data.is_core_limited_by_user)
return false;
else
return true;
}
static void ppm_forcelimit_update_limit_cb(void)
{
unsigned int i;
struct ppm_policy_req *req = &forcelimit_policy.req;
FUNC_ENTER(FUNC_LV_POLICY);
if (forcelimit_data.is_core_limited_by_user) {
ppm_clear_policy_limit(&forcelimit_policy);
for (i = 0; i < req->cluster_num; i++) {
req->limit[i].min_cpu_core =
(forcelimit_data.limit[i].min_core_num == -1)
? req->limit[i].min_cpu_core
: forcelimit_data.limit[i].min_core_num;
req->limit[i].max_cpu_core =
(forcelimit_data.limit[i].max_core_num == -1)
? req->limit[i].max_cpu_core
: forcelimit_data.limit[i].max_core_num;
}
}
FUNC_EXIT(FUNC_LV_POLICY);
}
static void ppm_forcelimit_status_change_cb(bool enable)
{
FUNC_ENTER(FUNC_LV_POLICY);
ppm_ver("forcelimit policy status changed to %d\n", enable);
FUNC_EXIT(FUNC_LV_POLICY);
}
unsigned int mt_ppm_forcelimit_cpu_core(unsigned int cluster_num,
struct ppm_limit_data *data)
{
int i = 0;
int min_core, max_core;
bool is_limit = false;
/* Error check */
if (cluster_num > NR_PPM_CLUSTERS) {
ppm_err("Invalid cluster num = %d\n", cluster_num);
return -1;
}
if (!data) {
ppm_err("@%s: limit data is NULL!\n", __func__);
return -1;
}
for (i = 0; i < cluster_num; i++) {
min_core = data[i].min;
max_core = data[i].max;
/* invalid input check */
if (min_core != -1
&& min_core < (int)get_cluster_min_cpu_core(i)) {
ppm_err("Invalid input! min_core for cluster%d = %d\n",
i, min_core);
return -1;
}
if (max_core != -1
&& max_core > (int)get_cluster_max_cpu_core(i)) {
ppm_err("Invalid input! max_core for cluster%d = %d\n",
i, max_core);
return -1;
}
/* check is all limit clear or not */
if (min_core != -1 || max_core != -1)
is_limit = true;
/* sync to max_core if min > max */
if (min_core != -1 && max_core != -1 && min_core > max_core)
data[i].min = data[i].max;
}
ppm_lock(&forcelimit_policy.lock);
if (!forcelimit_policy.is_enabled) {
ppm_warn("forcelimit policy is not enabled!\n");
ppm_unlock(&forcelimit_policy.lock);
return -1;
}
/* update policy data */
for (i = 0; i < cluster_num; i++) {
min_core = data[i].min;
max_core = data[i].max;
if (min_core != forcelimit_data.limit[i].min_core_num ||
max_core != forcelimit_data.limit[i].max_core_num) {
forcelimit_data.limit[i].min_core_num = min_core;
forcelimit_data.limit[i].max_core_num = max_core;
ppm_info("forcelimit min/max core for cl%d=%d/%d\n",
i, min_core, max_core);
}
}
forcelimit_data.is_core_limited_by_user = is_limit;
forcelimit_policy.is_activated = ppm_forcelimit_is_policy_active();
ppm_unlock(&forcelimit_policy.lock);
mt_ppm_main();
return 0;
}
static int ppm_forcelimit_cpu_core_proc_show(
struct seq_file *m, void *v)
{
int i;
for (i = 0; i < forcelimit_policy.req.cluster_num; i++) {
seq_printf(m, "cl%d: min_core_num = %d, max_core_num = %d\n",
i, forcelimit_data.limit[i].min_core_num,
forcelimit_data.limit[i].max_core_num);
}
return 0;
}
static ssize_t ppm_forcelimit_cpu_core_proc_write(struct file *file,
const char __user *buffer, size_t count, loff_t *pos)
{
int i = 0, data;
struct ppm_limit_data core_limit[NR_PPM_CLUSTERS];
unsigned int arg_num = NR_PPM_CLUSTERS * 2; /* for min and max */
char *tok, *tmp;
char *buf = ppm_copy_from_user_for_proc(buffer, count);
if (!buf)
return -EINVAL;
tmp = buf;
while ((tok = strsep(&tmp, " ")) != NULL) {
if (i == arg_num) {
ppm_err("number of arguments > %d!\n", arg_num);
goto out;
}
if (kstrtoint(tok, 10, &data)) {
ppm_err("@%s: Invalid input: %s\n", __func__, tok);
goto out;
} else {
if (i % 2) /* max */
core_limit[i/2].max = data;
else /* min */
core_limit[i/2].min = data;
i++;
}
}
if (i < arg_num)
ppm_err("number of arguments < %d!\n", arg_num);
else
mt_ppm_forcelimit_cpu_core(NR_PPM_CLUSTERS, core_limit);
out:
free_page((unsigned long)buf);
return count;
}
PROC_FOPS_RW(forcelimit_cpu_core);
static int __init ppm_forcelimit_policy_init(void)
{
int i, ret = 0;
struct pentry {
const char *name;
const struct file_operations *fops;
};
const struct pentry entries[] = {
PROC_ENTRY(forcelimit_cpu_core),
};
FUNC_ENTER(FUNC_LV_POLICY);
/* create procfs */
for (i = 0; i < ARRAY_SIZE(entries); i++) {
if (!proc_create(entries[i].name, 0644,
policy_dir, entries[i].fops)) {
ppm_err("%s(), create /proc/ppm/policy/%s failed\n",
__func__, entries[i].name);
ret = -EINVAL;
goto out;
}
}
forcelimit_data.limit = kcalloc(ppm_main_info.cluster_num,
sizeof(*forcelimit_data.limit), GFP_KERNEL);
if (!forcelimit_data.limit) {
ret = -ENOMEM;
goto out;
}
/* init forcelimit_data */
for_each_ppm_clusters(i) {
forcelimit_data.limit[i].min_freq_idx = -1;
forcelimit_data.limit[i].max_freq_idx = -1;
forcelimit_data.limit[i].min_core_num = -1;
forcelimit_data.limit[i].max_core_num = -1;
}
if (ppm_main_register_policy(&forcelimit_policy)) {
ppm_err("@%s: forcelimit policy register failed\n", __func__);
kfree(forcelimit_data.limit);
ret = -EINVAL;
goto out;
}
ppm_info("@%s: register %s done!\n", __func__, forcelimit_policy.name);
out:
FUNC_EXIT(FUNC_LV_POLICY);
return ret;
}
static void __exit ppm_forcelimit_policy_exit(void)
{
FUNC_ENTER(FUNC_LV_POLICY);
kfree(forcelimit_data.limit);
ppm_main_unregister_policy(&forcelimit_policy);
FUNC_EXIT(FUNC_LV_POLICY);
}
module_init(ppm_forcelimit_policy_init);
module_exit(ppm_forcelimit_policy_exit);
@@ -0,0 +1,488 @@
// SPDX-License-Identifier: GPL-2.0
/*
* Copyright (c) 2019 MediaTek Inc.
*/
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/slab.h>
#include "mtk_ppm_internal.h"
static void ppm_hard_userlimit_update_limit_cb(void);
static void ppm_hard_userlimit_status_change_cb(bool enable);
/* other members will init by ppm_main */
static struct ppm_policy_data hard_userlimit_policy = {
.name = __stringify(PPM_POLICY_HARD_USER_LIMIT),
.lock = __MUTEX_INITIALIZER
(hard_userlimit_policy.lock),
.policy = PPM_POLICY_HARD_USER_LIMIT,
.priority = PPM_POLICY_PRIO_USER_SPECIFY_BASE,
.update_limit_cb = ppm_hard_userlimit_update_limit_cb,
.status_change_cb = ppm_hard_userlimit_status_change_cb,
};
struct ppm_userlimit_data hard_userlimit_data = {
.is_freq_limited_by_user = false,
.is_core_limited_by_user = false,
};
/* MUST in lock */
static bool ppm_hard_userlimit_is_policy_active(void)
{
if (!hard_userlimit_data.is_freq_limited_by_user
&& !hard_userlimit_data.is_core_limited_by_user)
return false;
else
return true;
}
static void ppm_hard_userlimit_update_limit_cb(void)
{
unsigned int i;
struct ppm_policy_req *req = &hard_userlimit_policy.req;
FUNC_ENTER(FUNC_LV_POLICY);
if (hard_userlimit_data.is_freq_limited_by_user
|| hard_userlimit_data.is_core_limited_by_user) {
ppm_clear_policy_limit(&hard_userlimit_policy);
for (i = 0; i < req->cluster_num; i++) {
req->limit[i].min_cpufreq_idx =
(hard_userlimit_data.limit[i].min_freq_idx
== -1)
? req->limit[i].min_cpufreq_idx
: hard_userlimit_data.limit[i].min_freq_idx;
req->limit[i].max_cpufreq_idx =
(hard_userlimit_data.limit[i].max_freq_idx
== -1)
? req->limit[i].max_cpufreq_idx
: hard_userlimit_data.limit[i].max_freq_idx;
}
/* error check */
for (i = 0; i < req->cluster_num; i++) {
if (req->limit[i].max_cpu_core <
req->limit[i].min_cpu_core)
req->limit[i].min_cpu_core =
req->limit[i].max_cpu_core;
if (req->limit[i].max_cpufreq_idx >
req->limit[i].min_cpufreq_idx)
req->limit[i].min_cpufreq_idx =
req->limit[i].max_cpufreq_idx;
}
}
FUNC_EXIT(FUNC_LV_POLICY);
}
static void ppm_hard_userlimit_status_change_cb(bool enable)
{
FUNC_ENTER(FUNC_LV_POLICY);
ppm_ver("hard userlimit policy status changed to %d\n", enable);
FUNC_EXIT(FUNC_LV_POLICY);
}
unsigned int mt_ppm_hard_userlimit_cpu_core(unsigned int cluster_num,
struct ppm_limit_data *data)
{
/* phase-out */
return 0;
}
unsigned int mt_ppm_hard_userlimit_cpu_freq(
unsigned int cluster_num, struct ppm_limit_data *data)
{
int i = 0;
int min_freq, max_freq, min_freq_idx, max_freq_idx;
bool is_limit = false;
/* Error check */
if (cluster_num > NR_PPM_CLUSTERS) {
ppm_err("Invalid cluster num = %d\n", cluster_num);
return -1;
}
if (!data) {
ppm_err("limit data is NULL!\n");
return -1;
}
ppm_lock(&hard_userlimit_policy.lock);
if (!hard_userlimit_policy.is_enabled) {
ppm_warn("hard userlimit policy is not enabled!\n");
ppm_unlock(&hard_userlimit_policy.lock);
return -1;
}
/* update policy data */
for (i = 0; i < cluster_num; i++) {
min_freq = data[i].min;
max_freq = data[i].max;
/* check is all limit clear or not */
if (min_freq != -1 || max_freq != -1)
is_limit = true;
/* freq to idx */
min_freq_idx = (min_freq == -1) ? -1
: ppm_main_freq_to_idx(i, min_freq,
CPUFREQ_RELATION_L);
max_freq_idx = (max_freq == -1) ? -1
: ppm_main_freq_to_idx(i, max_freq,
CPUFREQ_RELATION_H);
/* sync to max_freq if min < max */
if (min_freq_idx != -1 && max_freq_idx != -1
&& min_freq_idx < max_freq_idx)
min_freq_idx = max_freq_idx;
/* write to policy data */
if (min_freq_idx != hard_userlimit_data.limit[i].min_freq_idx ||
max_freq_idx !=
hard_userlimit_data.limit[i].max_freq_idx) {
hard_userlimit_data.limit[i].min_freq_idx =
min_freq_idx;
hard_userlimit_data.limit[i].max_freq_idx =
max_freq_idx;
ppm_dbg(HARD_USER_LIMIT,
"%d:hard user limit min/max freq = %d(%d)/%d(%d)\n",
i, min_freq, min_freq_idx,
max_freq, max_freq_idx);
}
}
hard_userlimit_data.is_freq_limited_by_user = is_limit;
hard_userlimit_policy.is_activated =
ppm_hard_userlimit_is_policy_active();
ppm_unlock(&hard_userlimit_policy.lock);
mt_ppm_main();
return 0;
}
unsigned int mt_ppm_hard_userlimit_freq_limit_by_others(unsigned int cluster)
{
struct ppm_data *p = &ppm_main_info;
if (cluster >= NR_PPM_CLUSTERS)
return 0;
else
return p->cluster_info[cluster].max_freq_except_userlimit;
}
static int ppm_hard_userlimit_min_cpu_freq_proc_show
(struct seq_file *m, void *v)
{
int i;
for (i = 0; i < hard_userlimit_policy.req.cluster_num; i++) {
seq_printf(m, "%d: min_freq_idx = %d, max_freq_idx = %d\n",
i, hard_userlimit_data.limit[i].min_freq_idx,
hard_userlimit_data.limit[i].max_freq_idx);
}
return 0;
}
static ssize_t ppm_hard_userlimit_min_cpu_freq_proc_write(struct file *file,
const char __user *buffer, size_t count, loff_t *pos)
{
int id, min_freq, idx, i;
bool is_limit = false;
char *buf = ppm_copy_from_user_for_proc(buffer, count);
if (!buf)
return -EINVAL;
if (sscanf(buf, "%d %d", &id, &min_freq) == 2) {
if (id < 0 || id >= ppm_main_info.cluster_num) {
ppm_err("Invalid cluster id: %d\n", id);
goto out;
}
ppm_lock(&hard_userlimit_policy.lock);
if (!hard_userlimit_policy.is_enabled) {
ppm_warn("hard userlimit policy is not enabled!\n");
ppm_unlock(&hard_userlimit_policy.lock);
goto out;
}
idx = (min_freq == -1) ? -1
: ppm_main_freq_to_idx(id, min_freq,
CPUFREQ_RELATION_L);
/* error check, sync to max idx if min freq > max freq */
if (hard_userlimit_data.limit[id].max_freq_idx != -1
&& idx != -1
&& idx < hard_userlimit_data.limit[id].max_freq_idx)
idx = hard_userlimit_data.limit[id].max_freq_idx;
if (idx != hard_userlimit_data.limit[id].min_freq_idx) {
hard_userlimit_data.limit[id].min_freq_idx = idx;
ppm_dbg(HARD_USER_LIMIT,
"%d:hard userlimit min_freq= %d KHz(%d)\n",
id, min_freq, idx);
}
/* check is freq limited or not */
for (i = 0; i < hard_userlimit_policy.req.cluster_num; i++) {
if (hard_userlimit_data.limit[i].min_freq_idx != -1
|| hard_userlimit_data.limit[i].max_freq_idx != -1) {
is_limit = true;
break;
}
}
hard_userlimit_data.is_freq_limited_by_user = is_limit;
hard_userlimit_policy.is_activated =
ppm_hard_userlimit_is_policy_active();
ppm_unlock(&hard_userlimit_policy.lock);
mt_ppm_main();
} else
ppm_err("@%s: Invalid input!\n", __func__);
out:
free_page((unsigned long)buf);
return count;
}
static int ppm_hard_userlimit_max_cpu_freq_proc_show
(struct seq_file *m, void *v)
{
int i;
for (i = 0; i < hard_userlimit_policy.req.cluster_num; i++) {
seq_printf(m, "%d: min_freq_idx = %d, max_freq_idx = %d\n",
i, hard_userlimit_data.limit[i].min_freq_idx,
hard_userlimit_data.limit[i].max_freq_idx);
}
return 0;
}
static ssize_t ppm_hard_userlimit_max_cpu_freq_proc_write(
struct file *file, const char __user *buffer, size_t count, loff_t *pos)
{
int id, max_freq, idx, i;
bool is_limit = false;
char *buf = ppm_copy_from_user_for_proc(buffer, count);
if (!buf)
return -EINVAL;
if (sscanf(buf, "%d %d", &id, &max_freq) == 2) {
if (id < 0 || id >= ppm_main_info.cluster_num) {
ppm_err("Invalid cluster id: %d\n", id);
goto out;
}
ppm_lock(&hard_userlimit_policy.lock);
if (!hard_userlimit_policy.is_enabled) {
ppm_warn("hard userlimit policy is not enabled!\n");
ppm_unlock(&hard_userlimit_policy.lock);
goto out;
}
idx = (max_freq == -1) ? -1
: ppm_main_freq_to_idx(id, max_freq,
CPUFREQ_RELATION_H);
/* error check, sync to max idx if max freq < min freq */
if (hard_userlimit_data.limit[id].min_freq_idx != -1
&& idx != -1
&& idx > hard_userlimit_data.limit[id].min_freq_idx)
hard_userlimit_data.limit[id].min_freq_idx = idx;
if (idx != hard_userlimit_data.limit[id].max_freq_idx) {
hard_userlimit_data.limit[id].max_freq_idx = idx;
ppm_dbg(HARD_USER_LIMIT,
"%d:hard user limit max_freq = %d KHz(idx = %d)\n",
id, max_freq, idx);
}
/* check is freq limited or not */
for (i = 0; i < hard_userlimit_policy.req.cluster_num; i++) {
if (hard_userlimit_data.limit[i].min_freq_idx != -1
|| hard_userlimit_data.limit[i].max_freq_idx != -1) {
is_limit = true;
break;
}
}
hard_userlimit_data.is_freq_limited_by_user = is_limit;
hard_userlimit_policy.is_activated =
ppm_hard_userlimit_is_policy_active();
ppm_unlock(&hard_userlimit_policy.lock);
mt_ppm_main();
} else
ppm_err("@%s: Invalid input!\n", __func__);
out:
free_page((unsigned long)buf);
return count;
}
static int ppm_hard_userlimit_cpu_freq_proc_show(struct seq_file *m, void *v)
{
int i;
for (i = 0; i < hard_userlimit_policy.req.cluster_num; i++) {
seq_printf(m, "%d: min_freq_idx = %d, max_freq_idx = %d\n",
i, hard_userlimit_data.limit[i].min_freq_idx,
hard_userlimit_data.limit[i].max_freq_idx);
}
return 0;
}
static ssize_t ppm_hard_userlimit_cpu_freq_proc_write(struct file *file,
const char __user *buffer, size_t count, loff_t *pos)
{
int i = 0, data;
struct ppm_limit_data freq_limit[NR_PPM_CLUSTERS];
unsigned int arg_num = NR_PPM_CLUSTERS * 2; /* for min and max */
char *tok, *tmp;
char *buf = ppm_copy_from_user_for_proc(buffer, count);
if (!buf)
return -EINVAL;
tmp = buf;
while ((tok = strsep(&tmp, " ")) != NULL) {
if (i == arg_num) {
ppm_err("number of arguments > %d!\n", arg_num);
goto out;
}
if (kstrtoint(tok, 10, &data)) {
ppm_err("Invalid input: %s\n", tok);
goto out;
} else {
if (i % 2) /* max */
freq_limit[i/2].max = data;
else /* min */
freq_limit[i/2].min = data;
i++;
}
}
if (i < arg_num)
ppm_err("number of arguments < %d!\n", arg_num);
else
mt_ppm_hard_userlimit_cpu_freq(NR_PPM_CLUSTERS, freq_limit);
out:
free_page((unsigned long)buf);
return count;
}
static int ppm_hard_userlimit_freq_limit_by_others_proc_show(
struct seq_file *m, void *v)
{
int i;
for_each_ppm_clusters(i) {
seq_printf(m, "cluster %d max_freq_limit = %d\n",
i, mt_ppm_hard_userlimit_freq_limit_by_others(i));
}
return 0;
}
PROC_FOPS_RW(hard_userlimit_min_cpu_freq);
PROC_FOPS_RW(hard_userlimit_max_cpu_freq);
PROC_FOPS_RW(hard_userlimit_cpu_freq);
PROC_FOPS_RO(hard_userlimit_freq_limit_by_others);
static int __init ppm_hard_userlimit_policy_init(void)
{
int i, ret = 0;
struct pentry {
const char *name;
const struct file_operations *fops;
};
const struct pentry entries[] = {
PROC_ENTRY(hard_userlimit_min_cpu_freq),
PROC_ENTRY(hard_userlimit_max_cpu_freq),
PROC_ENTRY(hard_userlimit_cpu_freq),
PROC_ENTRY(hard_userlimit_freq_limit_by_others),
};
FUNC_ENTER(FUNC_LV_POLICY);
/* create procfs */
for (i = 0; i < ARRAY_SIZE(entries); i++) {
if (!proc_create(entries[i].name, 0644,
policy_dir, entries[i].fops)) {
ppm_err("%s(), create /proc/ppm/policy/%s failed\n",
__func__, entries[i].name);
ret = -EINVAL;
goto out;
}
}
hard_userlimit_data.limit = kcalloc(ppm_main_info.cluster_num,
sizeof(*hard_userlimit_data.limit), GFP_KERNEL);
if (!hard_userlimit_data.limit) {
ret = -ENOMEM;
goto out;
}
/* init hard_userlimit_data */
for_each_ppm_clusters(i) {
hard_userlimit_data.limit[i].min_freq_idx = -1;
hard_userlimit_data.limit[i].max_freq_idx = -1;
hard_userlimit_data.limit[i].min_core_num = -1;
hard_userlimit_data.limit[i].max_core_num = -1;
}
if (ppm_main_register_policy(&hard_userlimit_policy)) {
ppm_err("@%s:hard userlimit policy register failed\n",
__func__);
kfree(hard_userlimit_data.limit);
ret = -EINVAL;
goto out;
}
ppm_info("@%s: register %s done!\n",
__func__, hard_userlimit_policy.name);
out:
FUNC_EXIT(FUNC_LV_POLICY);
return ret;
}
static void __exit ppm_hard_userlimit_policy_exit(void)
{
FUNC_ENTER(FUNC_LV_POLICY);
kfree(hard_userlimit_data.limit);
ppm_main_unregister_policy(&hard_userlimit_policy);
FUNC_EXIT(FUNC_LV_POLICY);
}
module_init(ppm_hard_userlimit_policy_init);
module_exit(ppm_hard_userlimit_policy_exit);
@@ -0,0 +1,239 @@
// SPDX-License-Identifier: GPL-2.0
/*
* Copyright (c) 2019 MediaTek Inc.
*/
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/notifier.h>
#include <linux/fb.h>
#include "mtk_ppm_internal.h"
static void ppm_lcmoff_update_limit_cb(void);
static void ppm_lcmoff_status_change_cb(bool enable);
static int lcmoff_min_freq;
/* other members will init by ppm_main */
static struct ppm_policy_data lcmoff_policy = {
.name = __stringify(PPM_POLICY_LCM_OFF),
.lock = __MUTEX_INITIALIZER(lcmoff_policy.lock),
.policy = PPM_POLICY_LCM_OFF,
.priority = PPM_POLICY_PRIO_USER_SPECIFY_BASE,
.update_limit_cb = ppm_lcmoff_update_limit_cb,
.status_change_cb = ppm_lcmoff_status_change_cb,
};
bool ppm_lcmoff_is_policy_activated(void)
{
bool is_activate;
ppm_lock(&lcmoff_policy.lock);
is_activate = lcmoff_policy.is_activated;
ppm_unlock(&lcmoff_policy.lock);
return is_activate;
}
static void ppm_lcmoff_update_limit_cb(void)
{
unsigned int i;
FUNC_ENTER(FUNC_LV_POLICY);
ppm_clear_policy_limit(&lcmoff_policy);
/* only apply min freq for LL cluster */
for (i = 0; i < 1; i++) {
/* for (i = 0; i < lcmoff_policy.req.cluster_num; i++) { */
if (lcmoff_policy.req.limit[i].min_cpufreq_idx != -1) {
int idx = ppm_main_freq_to_idx(i,
lcmoff_min_freq,
CPUFREQ_RELATION_L);
lcmoff_policy.req.limit[i].min_cpufreq_idx =
MIN(lcmoff_policy.req.limit[i].min_cpufreq_idx,
idx);
}
}
FUNC_EXIT(FUNC_LV_POLICY);
}
static void ppm_lcmoff_status_change_cb(bool enable)
{
FUNC_ENTER(FUNC_LV_POLICY);
ppm_ver("@%s: lcmoff policy status changed to %d\n", __func__, enable);
FUNC_EXIT(FUNC_LV_POLICY);
}
static void ppm_lcmoff_switch(int onoff)
{
unsigned int i;
FUNC_ENTER(FUNC_LV_POLICY);
ppm_info("@%s: onoff = %d\n", __func__, onoff);
ppm_lock(&lcmoff_policy.lock);
/* onoff = 0: LCM OFF */
/* others: LCM ON */
if (onoff) {
/* deactivate lcmoff policy */
if (lcmoff_policy.is_activated) {
lcmoff_policy.is_activated = false;
for (i = 0; i < lcmoff_policy.req.cluster_num; i++) {
lcmoff_policy.req.limit[i].min_cpufreq_idx =
get_cluster_min_cpufreq_idx(i);
lcmoff_policy.req.limit[i].max_cpufreq_idx =
get_cluster_max_cpufreq_idx(i);
}
}
} else {
/* activate lcmoff policy */
if (lcmoff_policy.is_enabled)
lcmoff_policy.is_activated = true;
}
ppm_unlock(&lcmoff_policy.lock);
FUNC_EXIT(FUNC_LV_POLICY);
}
static int ppm_lcmoff_fb_notifier_callback(struct notifier_block *self,
unsigned long event, void *data)
{
struct fb_event *evdata = data;
int blank;
FUNC_ENTER(FUNC_LV_POLICY);
/* skip if it's not a blank event */
if (event != FB_EVENT_BLANK)
return 0;
blank = *(int *)evdata->data;
ppm_ver("@%s: blank = %d, event = %lu\n", __func__, blank, event);
switch (blank) {
/* LCM ON */
case FB_BLANK_UNBLANK:
ppm_lcmoff_switch(1);
break;
/* LCM OFF */
case FB_BLANK_POWERDOWN:
ppm_lcmoff_switch(0);
break;
default:
break;
}
FUNC_EXIT(FUNC_LV_POLICY);
return 0;
}
static struct notifier_block ppm_lcmoff_fb_notifier = {
.notifier_call = ppm_lcmoff_fb_notifier_callback,
};
static int ppm_lcmoff_min_freq_proc_show(struct seq_file *m, void *v)
{
seq_printf(m, "lcmoff_min_freq = %d KHz\n", lcmoff_min_freq);
return 0;
}
static ssize_t ppm_lcmoff_min_freq_proc_write(struct file *file,
const char __user *buffer, size_t count, loff_t *pos)
{
unsigned int freq;
char *buf = ppm_copy_from_user_for_proc(buffer, count);
if (!buf)
return -EINVAL;
if (!kstrtouint(buf, 10, &freq))
lcmoff_min_freq = freq;
else
ppm_err("@%s: Invalid input!\n", __func__);
free_page((unsigned long)buf);
return count;
}
PROC_FOPS_RW(lcmoff_min_freq);
static int __init ppm_lcmoff_policy_init(void)
{
int ret = 0, i;
struct pentry {
const char *name;
const struct file_operations *fops;
};
const struct pentry entries[] = {
PROC_ENTRY(lcmoff_min_freq),
};
FUNC_ENTER(FUNC_LV_POLICY);
/* create procfs */
for (i = 0; i < ARRAY_SIZE(entries); i++) {
if (!proc_create(entries[i].name, 0664,
policy_dir, entries[i].fops)) {
ppm_err("%s(), create /proc/ppm/policy/%s failed\n",
__func__, entries[i].name);
ret = -EINVAL;
goto out;
}
}
if (fb_register_client(&ppm_lcmoff_fb_notifier)) {
ppm_err("@%s: lcmoff policy register FB client failed!\n",
__func__);
ret = -EINVAL;
goto out;
}
if (ppm_main_register_policy(&lcmoff_policy)) {
ppm_err("@%s: lcmoff policy register failed\n", __func__);
ret = -EINVAL;
goto out;
}
#ifdef LCMOFF_MIN_FREQ
lcmoff_min_freq = LCMOFF_MIN_FREQ;
#else
lcmoff_policy.is_enabled = false;
#endif
ppm_info("@%s: register %s done!\n", __func__, lcmoff_policy.name);
out:
FUNC_EXIT(FUNC_LV_POLICY);
return ret;
}
static void __exit ppm_lcmoff_policy_exit(void)
{
FUNC_ENTER(FUNC_LV_POLICY);
fb_unregister_client(&ppm_lcmoff_fb_notifier);
ppm_main_unregister_policy(&lcmoff_policy);
FUNC_EXIT(FUNC_LV_POLICY);
}
/* Cannot init before FB driver */
late_initcall(ppm_lcmoff_policy_init);
module_exit(ppm_lcmoff_policy_exit);
@@ -0,0 +1,186 @@
// SPDX-License-Identifier: GPL-2.0
/*
* Copyright (c) 2019 MediaTek Inc.
*/
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/init.h>
#include "mtk_ppm_internal.h"
static void ppm_ptpod_update_limit_cb(void);
static void ppm_ptpod_status_change_cb(bool enable);
/* other members will init by ppm_main */
static struct ppm_policy_data ptpod_policy = {
.name = __stringify(PPM_POLICY_PTPOD),
.lock = __MUTEX_INITIALIZER(ptpod_policy.lock),
.policy = PPM_POLICY_PTPOD,
.priority = PPM_POLICY_PRIO_HIGHEST,
.update_limit_cb = ppm_ptpod_update_limit_cb,
.status_change_cb = ppm_ptpod_status_change_cb,
};
void mt_ppm_ptpod_policy_activate(void)
{
unsigned int i;
FUNC_ENTER(FUNC_LV_API);
ppm_lock(&ptpod_policy.lock);
if (!ptpod_policy.is_enabled) {
ppm_warn("@%s: ptpod policy is not enabled!\n", __func__);
ppm_unlock(&ptpod_policy.lock);
goto end;
}
ptpod_policy.is_activated = true;
for (i = 0; i < ptpod_policy.req.cluster_num; i++) {
ptpod_policy.req.limit[i].min_cpufreq_idx =
get_cluster_ptpod_fix_freq_idx(i);
ptpod_policy.req.limit[i].max_cpufreq_idx =
get_cluster_ptpod_fix_freq_idx(i);
}
ppm_unlock(&ptpod_policy.lock);
mt_ppm_main();
end:
FUNC_EXIT(FUNC_LV_API);
}
void mt_ppm_ptpod_policy_deactivate(void)
{
unsigned int i;
FUNC_ENTER(FUNC_LV_API);
ppm_lock(&ptpod_policy.lock);
/* deactivate ptpod policy */
if (ptpod_policy.is_activated) {
ptpod_policy.is_activated = false;
/* restore to default setting */
for (i = 0; i < ptpod_policy.req.cluster_num; i++) {
ptpod_policy.req.limit[i].min_cpufreq_idx =
get_cluster_min_cpufreq_idx(i);
ptpod_policy.req.limit[i].max_cpufreq_idx =
get_cluster_max_cpufreq_idx(i);
}
ppm_unlock(&ptpod_policy.lock);
mt_ppm_main();
} else
ppm_unlock(&ptpod_policy.lock);
FUNC_EXIT(FUNC_LV_API);
}
static void ppm_ptpod_update_limit_cb(void)
{
FUNC_ENTER(FUNC_LV_POLICY);
FUNC_EXIT(FUNC_LV_POLICY);
}
static void ppm_ptpod_status_change_cb(bool enable)
{
FUNC_ENTER(FUNC_LV_POLICY);
ppm_ver("@%s: ptpod policy status changed to %d\n", __func__, enable);
FUNC_EXIT(FUNC_LV_POLICY);
}
static int ppm_ptpod_test_proc_show(struct seq_file *m, void *v)
{
seq_printf(m, "PTPOD is_activate = %d\n", ptpod_policy.is_activated);
return 0;
}
static ssize_t ppm_ptpod_test_proc_write(struct file *file,
const char __user *buffer, size_t count, loff_t *pos)
{
unsigned int enabled;
char *buf = ppm_copy_from_user_for_proc(buffer, count);
if (!buf)
return -EINVAL;
if (!kstrtouint(buf, 10, &enabled)) {
#ifdef PPM_SSPM_SUPPORT
ppm_ipi_ptpod_test(enabled);
#else
if (enabled)
mt_ppm_ptpod_policy_activate();
else
mt_ppm_ptpod_policy_deactivate();
#endif
} else
ppm_err("@%s: Invalid input!\n", __func__);
free_page((unsigned long)buf);
return count;
}
PROC_FOPS_RW(ptpod_test);
static int __init ppm_ptpod_policy_init(void)
{
int i, ret = 0;
struct pentry {
const char *name;
const struct file_operations *fops;
};
const struct pentry entries[] = {
PROC_ENTRY(ptpod_test),
};
FUNC_ENTER(FUNC_LV_POLICY);
/* create procfs */
for (i = 0; i < ARRAY_SIZE(entries); i++) {
if (!proc_create(entries[i].name, 0644,
policy_dir, entries[i].fops)) {
ppm_err("%s(), create /proc/ppm/policy/%s failed\n",
__func__, entries[i].name);
ret = -EINVAL;
goto out;
}
}
if (ppm_main_register_policy(&ptpod_policy)) {
ppm_err("@%s: ptpod policy register failed\n", __func__);
ret = -EINVAL;
goto out;
}
ppm_info("@%s: register %s done!\n", __func__, ptpod_policy.name);
out:
FUNC_EXIT(FUNC_LV_POLICY);
return ret;
}
static void __exit ppm_ptpod_policy_exit(void)
{
FUNC_ENTER(FUNC_LV_POLICY);
ppm_main_unregister_policy(&ptpod_policy);
FUNC_EXIT(FUNC_LV_POLICY);
}
module_init(ppm_ptpod_policy_init);
module_exit(ppm_ptpod_policy_exit);
@@ -0,0 +1,206 @@
// SPDX-License-Identifier: GPL-2.0
/*
* Copyright (c) 2019 MediaTek Inc.
*/
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/init.h>
#include "mtk_ppm_internal.h"
#include "mtk_low_battery_throttling.h"
#include "mtk_battery_oc_throttling.h"
#include "mtk_battery_percentage_throttling.h"
static void ppm_pwrthro_update_limit_cb(void);
static void ppm_pwrthro_status_change_cb(bool enable);
/* other members will init by ppm_main */
static struct ppm_policy_data pwrthro_policy = {
.name = __stringify(PPM_POLICY_PWR_THRO),
.lock = __MUTEX_INITIALIZER(pwrthro_policy.lock),
.policy = PPM_POLICY_PWR_THRO,
.priority = PPM_POLICY_PRIO_POWER_BUDGET_BASE,
.update_limit_cb = ppm_pwrthro_update_limit_cb,
.status_change_cb = ppm_pwrthro_status_change_cb,
};
static void ppm_pwrthro_update_limit_cb(void)
{
FUNC_ENTER(FUNC_LV_POLICY);
ppm_clear_policy_limit(&pwrthro_policy);
/* update limit according to power budget */
ppm_update_req_by_pwr(&pwrthro_policy.req);
FUNC_EXIT(FUNC_LV_POLICY);
}
static void ppm_pwrthro_status_change_cb(bool enable)
{
FUNC_ENTER(FUNC_LV_POLICY);
ppm_ver("pwrthro policy status changed to %d\n", enable);
FUNC_EXIT(FUNC_LV_POLICY);
}
#ifndef DISABLE_BATTERY_PERCENT_PROTECT
static void ppm_pwrthro_bat_per_protect(BATTERY_PERCENT_LEVEL level)
{
unsigned int limited_power = ~0;
FUNC_ENTER(FUNC_LV_API);
ppm_ver("@%s: bat percent lv = %d\n", __func__, level);
ppm_lock(&pwrthro_policy.lock);
if (!pwrthro_policy.is_enabled) {
ppm_warn("@%s: pwrthro policy is not enabled!\n", __func__);
ppm_unlock(&pwrthro_policy.lock);
goto end;
}
switch (level) {
case BATTERY_PERCENT_LEVEL_1:
limited_power = PWRTHRO_BAT_PER_MW;
break;
default:
/* Unlimit */
limited_power = 0;
break;
}
pwrthro_policy.req.power_budget = limited_power;
pwrthro_policy.is_activated = (limited_power) ? true : false;
ppm_unlock(&pwrthro_policy.lock);
mt_ppm_main();
end:
FUNC_EXIT(FUNC_LV_API);
}
#endif
#ifndef DISABLE_BATTERY_OC_PROTECT
static void ppm_pwrthro_bat_oc_protect(enum BATTERY_OC_LEVEL_TAG level)
{
unsigned int limited_power = ~0;
FUNC_ENTER(FUNC_LV_API);
ppm_ver("@%s: bat OC lv = %d\n", __func__, level);
ppm_lock(&pwrthro_policy.lock);
if (!pwrthro_policy.is_enabled) {
ppm_warn("@%s: pwrthro policy is not enabled!\n", __func__);
ppm_unlock(&pwrthro_policy.lock);
goto end;
}
switch (level) {
case BATTERY_OC_LEVEL_1:
limited_power = PWRTHRO_BAT_OC_MW;
break;
default:
/* Unlimit */
limited_power = 0;
break;
}
pwrthro_policy.req.power_budget = limited_power;
pwrthro_policy.is_activated = (limited_power) ? true : false;
ppm_unlock(&pwrthro_policy.lock);
mt_ppm_main();
end:
FUNC_EXIT(FUNC_LV_API);
}
#endif
#ifndef DISABLE_LOW_BATTERY_PROTECT
void ppm_pwrthro_low_bat_protect(enum LOW_BATTERY_LEVEL_TAG level)
{
unsigned int limited_power = ~0;
FUNC_ENTER(FUNC_LV_API);
ppm_ver("@%s: low bat lv = %d\n", __func__, level);
ppm_lock(&pwrthro_policy.lock);
if (!pwrthro_policy.is_enabled) {
ppm_warn("@%s: pwrthro policy is not enabled!\n", __func__);
ppm_unlock(&pwrthro_policy.lock);
goto end;
}
switch (level) {
case LOW_BATTERY_LEVEL_1:
limited_power = PWRTHRO_LOW_BAT_LV1_MW;
break;
case LOW_BATTERY_LEVEL_2:
limited_power = PWRTHRO_LOW_BAT_LV2_MW;
break;
default:
/* Unlimit */
limited_power = 0;
break;
}
pwrthro_policy.req.power_budget = limited_power;
pwrthro_policy.is_activated = (limited_power) ? true : false;
ppm_unlock(&pwrthro_policy.lock);
mt_ppm_main();
end:
FUNC_EXIT(FUNC_LV_API);
}
#endif
static int __init ppm_pwrthro_policy_init(void)
{
int ret = 0;
FUNC_ENTER(FUNC_LV_POLICY);
if (ppm_main_register_policy(&pwrthro_policy)) {
ppm_err("@%s: pwrthro policy register failed\n", __func__);
ret = -EINVAL;
goto out;
}
register_battery_percent_notify(&ppm_pwrthro_bat_per_protect,
BATTERY_PERCENT_PRIO_CPU_L);
register_battery_oc_notify(&ppm_pwrthro_bat_oc_protect,
BATTERY_OC_PRIO_CPU_L);
register_low_battery_notify(&ppm_pwrthro_low_bat_protect,
LOW_BATTERY_PRIO_CPU_L);
ppm_info("@%s: register %s done!\n", __func__, pwrthro_policy.name);
out:
FUNC_EXIT(FUNC_LV_POLICY);
return ret;
}
static void __exit ppm_pwrthro_policy_exit(void)
{
FUNC_ENTER(FUNC_LV_POLICY);
ppm_main_unregister_policy(&pwrthro_policy);
FUNC_EXIT(FUNC_LV_POLICY);
}
module_init(ppm_pwrthro_policy_init);
module_exit(ppm_pwrthro_policy_exit);
@@ -0,0 +1,586 @@
// SPDX-License-Identifier: GPL-2.0
/*
* Copyright (c) 2019 MediaTek Inc.
*/
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/string.h>
#include <linux/slab.h>
#include "mtk_ppm_internal.h"
static void ppm_sysboost_update_limit_cb(void);
static void ppm_sysboost_status_change_cb(bool enable);
/* other members will init by ppm_main */
static struct ppm_policy_data sysboost_policy = {
.name = __stringify(PPM_POLICY_SYS_BOOST),
.lock = __MUTEX_INITIALIZER(sysboost_policy.lock),
.policy = PPM_POLICY_SYS_BOOST,
.priority = PPM_POLICY_PRIO_PERFORMANCE_BASE,
.update_limit_cb = ppm_sysboost_update_limit_cb,
.status_change_cb = ppm_sysboost_status_change_cb,
};
struct ppm_sysboost_data {
enum ppm_sysboost_user user;
char *user_name;
/* record user input for old interface */
unsigned int min_freq;
unsigned int min_core_num;
struct ppm_user_limit limit[NR_PPM_CLUSTERS];
struct list_head link;
};
static LIST_HEAD(sysboost_user_list);
/* to save each user's request */
static struct ppm_sysboost_data sysboost_data[NR_PPM_SYSBOOST_USER];
/* final limit */
static struct ppm_userlimit_data sysboost_final_limit = {
.is_freq_limited_by_user = false,
.is_core_limited_by_user = false,
};
static void ppm_sysboost_dump_final_limit(void *data)
{
int i;
if (!data) {
ppm_dbg(SYS_BOOST,
"is_core_limited = %d, is_freq_limited = %d\n",
sysboost_final_limit.is_core_limited_by_user,
sysboost_final_limit.is_freq_limited_by_user);
for_each_ppm_clusters(i) {
ppm_dbg(SYS_BOOST, "cluster %d = (%d)(%d)(%d)(%d)\n",
i,
sysboost_final_limit.limit[i].min_freq_idx,
sysboost_final_limit.limit[i].max_freq_idx,
sysboost_final_limit.limit[i].min_core_num,
sysboost_final_limit.limit[i].max_core_num);
}
} else {
struct seq_file *m = (struct seq_file *)data;
seq_printf(m, "is_core_limited = %d, is_freq_limited = %d\n",
sysboost_final_limit.is_core_limited_by_user,
sysboost_final_limit.is_freq_limited_by_user);
for_each_ppm_clusters(i) {
seq_printf(m, "cluster %d = (%d)(%d)(%d)(%d)\n", i,
sysboost_final_limit.limit[i].min_freq_idx,
sysboost_final_limit.limit[i].max_freq_idx,
sysboost_final_limit.limit[i].min_core_num,
sysboost_final_limit.limit[i].max_core_num);
}
}
}
/* must acquire sysboost_policy lock before calling this functions! */
static void ppm_sysboost_update_final_limit(void)
{
struct ppm_sysboost_data *data;
int min_core, max_core, min_freq_idx, max_freq_idx;
int i;
/* update user core setting */
for_each_ppm_clusters(i) {
min_core = max_core = min_freq_idx = max_freq_idx = -1;
list_for_each_entry_reverse(data, &sysboost_user_list, link) {
min_core = MAX(min_core, data->limit[i].min_core_num);
max_core = (data->limit[i].max_core_num == -1)
? max_core
: (max_core == -1)
? data->limit[i].max_core_num
: MIN(max_core, data->limit[i].max_core_num);
min_freq_idx = (data->limit[i].min_freq_idx == -1)
? min_freq_idx
: (min_freq_idx == -1)
? data->limit[i].min_freq_idx
: MIN(min_freq_idx,
data->limit[i].min_freq_idx);
max_freq_idx = MAX(max_freq_idx,
data->limit[i].max_freq_idx);
}
/* error check */
if (min_freq_idx != -1 && min_freq_idx < max_freq_idx)
min_freq_idx = max_freq_idx;
if (max_core != -1 && min_core > max_core)
min_core = max_core;
sysboost_final_limit.limit[i].min_freq_idx = min_freq_idx;
sysboost_final_limit.limit[i].max_freq_idx = max_freq_idx;
sysboost_final_limit.limit[i].min_core_num = min_core;
sysboost_final_limit.limit[i].max_core_num = max_core;
}
sysboost_final_limit.is_freq_limited_by_user = false;
sysboost_final_limit.is_core_limited_by_user = false;
for_each_ppm_clusters(i) {
if (!sysboost_final_limit.is_freq_limited_by_user
&& (sysboost_final_limit.limit[i].min_freq_idx != -1
|| sysboost_final_limit.limit[i].max_freq_idx != -1)) {
sysboost_final_limit.is_freq_limited_by_user = true;
}
if (!sysboost_final_limit.is_core_limited_by_user
&& (sysboost_final_limit.limit[i].min_core_num != -1
|| sysboost_final_limit.limit[i].max_core_num != -1)) {
sysboost_final_limit.is_core_limited_by_user = true;
}
if (sysboost_final_limit.is_freq_limited_by_user
&& sysboost_final_limit.is_core_limited_by_user)
break;
}
if (!sysboost_final_limit.is_freq_limited_by_user
&& !sysboost_final_limit.is_core_limited_by_user)
sysboost_policy.is_activated = false;
else
sysboost_policy.is_activated = true;
ppm_sysboost_dump_final_limit(NULL);
}
void mt_ppm_sysboost_core(enum ppm_sysboost_user user, unsigned int core_num)
{
/* phase-out */
}
EXPORT_SYMBOL(mt_ppm_sysboost_core);
void mt_ppm_sysboost_freq(enum ppm_sysboost_user user, unsigned int freq)
{
struct ppm_sysboost_data *data;
int i, freq_idx;
if (user >= NR_PPM_SYSBOOST_USER) {
ppm_err("@%s: Invalid input: user = %d, freq = %d\n",
__func__, user, freq);
return;
}
ppm_lock(&sysboost_policy.lock);
if (!sysboost_policy.is_enabled) {
ppm_err("@%s: sysboost policy is not enabled!\n", __func__);
ppm_unlock(&sysboost_policy.lock);
return;
}
ppm_info("sys boost by %s: req_freq = %d\n",
sysboost_data[user].user_name, freq);
/* update user freq setting */
list_for_each_entry_reverse(data, &sysboost_user_list, link) {
if (data->user == user) {
data->min_freq = freq;
/* clear previous setting */
if (freq == 0) {
for_each_ppm_clusters(i)
data->limit[i].min_freq_idx = -1;
break;
}
for_each_ppm_clusters(i) {
freq_idx = (freq == -1) ? -1
: ppm_main_freq_to_idx(i, freq,
CPUFREQ_RELATION_L);
/* error check */
if (data->limit[i].max_freq_idx != -1
&& freq_idx != -1
&& freq_idx <
data->limit[i].max_freq_idx)
freq_idx = data->limit[i].max_freq_idx;
data->limit[i].min_freq_idx = freq_idx;
}
}
}
ppm_sysboost_update_final_limit();
ppm_unlock(&sysboost_policy.lock);
mt_ppm_main();
}
EXPORT_SYMBOL(mt_ppm_sysboost_freq);
void mt_ppm_sysboost_set_core_limit(enum ppm_sysboost_user user,
unsigned int cluster, int min_core, int max_core)
{
/* phase-out */
}
EXPORT_SYMBOL(mt_ppm_sysboost_set_core_limit);
void mt_ppm_sysboost_set_freq_limit(enum ppm_sysboost_user user,
unsigned int cluster, int min_freq, int max_freq)
{
struct ppm_sysboost_data *data;
if (cluster >= NR_PPM_CLUSTERS) {
ppm_err("Invalid input: cluster = %d\n", cluster);
return;
}
if ((max_freq != -1 && max_freq > get_cluster_max_cpufreq(cluster))
|| (min_freq != -1
&& min_freq < get_cluster_min_cpufreq(cluster))
|| user >= NR_PPM_SYSBOOST_USER) {
ppm_err("Invalid input: user/cl=%d/%d, min/max freq=%d/%d\n",
user, cluster, min_freq, max_freq);
return;
}
ppm_info("sys boost by %s: cluster %d min/max freq = %d/%d\n",
sysboost_data[user].user_name, cluster, min_freq, max_freq);
if (min_freq > max_freq && max_freq != -1)
min_freq = max_freq;
ppm_lock(&sysboost_policy.lock);
if (!sysboost_policy.is_enabled) {
ppm_err("@%s: sysboost policy is not enabled!\n", __func__);
ppm_unlock(&sysboost_policy.lock);
return;
}
/* update user freq setting */
list_for_each_entry_reverse(data, &sysboost_user_list, link) {
if (data->user == user) {
data->limit[cluster].min_freq_idx = (min_freq == -1)
? -1
: ppm_main_freq_to_idx(cluster, min_freq,
CPUFREQ_RELATION_L);
data->limit[cluster].max_freq_idx = (max_freq == -1)
? -1
: ppm_main_freq_to_idx(cluster, max_freq,
CPUFREQ_RELATION_H);
/* error check */
if (data->limit[cluster].min_freq_idx != -1
&& data->limit[cluster].min_freq_idx
< data->limit[cluster].max_freq_idx)
data->limit[cluster].min_freq_idx =
data->limit[cluster].max_freq_idx;
}
}
ppm_sysboost_update_final_limit();
ppm_unlock(&sysboost_policy.lock);
mt_ppm_main();
}
EXPORT_SYMBOL(mt_ppm_sysboost_set_freq_limit);
static void ppm_sysboost_update_limit_cb(void)
{
unsigned int i;
struct ppm_policy_req *req = &sysboost_policy.req;
struct ppm_userlimit_data *p = &sysboost_final_limit;
FUNC_ENTER(FUNC_LV_POLICY);
if (p->is_freq_limited_by_user
|| p->is_core_limited_by_user) {
ppm_clear_policy_limit(&sysboost_policy);
for (i = 0; i < req->cluster_num; i++) {
req->limit[i].min_cpufreq_idx =
(p->limit[i].min_freq_idx == -1)
? req->limit[i].min_cpufreq_idx
: p->limit[i].min_freq_idx;
req->limit[i].max_cpufreq_idx =
(p->limit[i].max_freq_idx == -1)
? req->limit[i].max_cpufreq_idx
: p->limit[i].max_freq_idx;
}
/* error check */
for (i = 0; i < req->cluster_num; i++) {
if (req->limit[i].max_cpu_core <
req->limit[i].min_cpu_core)
req->limit[i].min_cpu_core =
req->limit[i].max_cpu_core;
if (req->limit[i].max_cpufreq_idx >
req->limit[i].min_cpufreq_idx)
req->limit[i].min_cpufreq_idx =
req->limit[i].max_cpufreq_idx;
}
}
FUNC_EXIT(FUNC_LV_POLICY);
}
static void ppm_sysboost_status_change_cb(bool enable)
{
FUNC_ENTER(FUNC_LV_POLICY);
ppm_ver("sysboost policy status changed to %d\n", enable);
FUNC_EXIT(FUNC_LV_POLICY);
}
static int ppm_sysboost_freq_proc_show(struct seq_file *m, void *v)
{
struct ppm_sysboost_data *data;
/* update user freq setting */
list_for_each_entry_reverse(data, &sysboost_user_list, link)
seq_printf(m, "[%d] %s: %d\n",
data->user, data->user_name, data->min_freq);
ppm_sysboost_dump_final_limit(m);
return 0;
}
static ssize_t ppm_sysboost_freq_proc_write(struct file *file,
const char __user *buffer, size_t count, loff_t *pos)
{
int user, freq;
char *buf = ppm_copy_from_user_for_proc(buffer, count);
if (!buf)
return -EINVAL;
if (sscanf(buf, "%d %d", &user, &freq) == 2)
mt_ppm_sysboost_freq((enum ppm_sysboost_user)user, freq);
else
ppm_err("@%s: Invalid input!\n", __func__);
free_page((unsigned long)buf);
return count;
}
static int ppm_sysboost_cluster_freq_limit_proc_show(
struct seq_file *m, void *v)
{
struct ppm_sysboost_data *data;
int i;
/* update user freq setting */
list_for_each_entry_reverse(data, &sysboost_user_list, link) {
seq_printf(m, "[%d] %s: %d\t",
data->user, data->user_name, data->min_freq);
for_each_ppm_clusters(i)
seq_printf(m, "(%d)(%d)",
data->limit[i].min_freq_idx,
data->limit[i].max_freq_idx);
seq_puts(m, "\n");
}
ppm_sysboost_dump_final_limit(m);
return 0;
}
static ssize_t ppm_sysboost_cluster_freq_limit_proc_write(struct file *file,
const char __user *buffer, size_t count, loff_t *pos)
{
int user, min_freq, max_freq, cluster;
char *buf = ppm_copy_from_user_for_proc(buffer, count);
if (!buf)
return -EINVAL;
if (sscanf(buf, "%d %d %d %d",
&user, &cluster, &min_freq, &max_freq) == 4)
mt_ppm_sysboost_set_freq_limit((enum ppm_sysboost_user)user,
cluster, min_freq, max_freq);
else
ppm_err("@%s: Invalid input!\n", __func__);
free_page((unsigned long)buf);
return count;
}
static int ppm_sysboost_cluster_freqidx_limit_proc_show(
struct seq_file *m, void *v)
{
struct ppm_sysboost_data *data;
int i;
/* update user freq setting */
list_for_each_entry_reverse(data, &sysboost_user_list, link) {
seq_printf(m, "[%d] %s: %d\t",
data->user, data->user_name, data->min_freq);
for_each_ppm_clusters(i)
seq_printf(m, "(%d)(%d)",
data->limit[i].min_freq_idx,
data->limit[i].max_freq_idx);
seq_puts(m, "\n");
}
ppm_sysboost_dump_final_limit(m);
return 0;
}
static ssize_t ppm_sysboost_cluster_freqidx_limit_proc_write(struct file *file,
const char __user *buffer, size_t count, loff_t *pos)
{
int user, min_freq_idx, max_freq_idx, cl;
struct ppm_data *p = &ppm_main_info;
char *buf = ppm_copy_from_user_for_proc(buffer, count);
if (!buf)
return -EINVAL;
if (sscanf(buf, "%d %d %d %d",
&user, &cl, &min_freq_idx, &max_freq_idx) == 4) {
int min_freq, max_freq;
if (cl < 0 || cl >= NR_PPM_CLUSTERS)
goto end;
if (!ppm_main_info.cluster_info[cl].dvfs_tbl)
goto end;
min_freq = (min_freq_idx < 0 || min_freq_idx >= DVFS_OPP_NUM)
? -1
: p->cluster_info[cl].dvfs_tbl[min_freq_idx].frequency;
max_freq = (max_freq_idx < 0 || max_freq_idx >= DVFS_OPP_NUM)
? -1
: p->cluster_info[cl].dvfs_tbl[max_freq_idx].frequency;
mt_ppm_sysboost_set_freq_limit((enum ppm_sysboost_user)user,
cl, min_freq, max_freq);
} else
ppm_err("@%s: Invalid input!\n", __func__);
end:
free_page((unsigned long)buf);
return count;
}
PROC_FOPS_RW(sysboost_freq);
PROC_FOPS_RW(sysboost_cluster_freq_limit);
PROC_FOPS_RW(sysboost_cluster_freqidx_limit);
static int __init ppm_sysboost_policy_init(void)
{
int i, j, ret = 0;
struct pentry {
const char *name;
const struct file_operations *fops;
};
const struct pentry entries[] = {
PROC_ENTRY(sysboost_freq),
PROC_ENTRY(sysboost_cluster_freq_limit),
PROC_ENTRY(sysboost_cluster_freqidx_limit),
};
FUNC_ENTER(FUNC_LV_POLICY);
/* create procfs */
for (i = 0; i < ARRAY_SIZE(entries); i++) {
if (!proc_create(entries[i].name, 0644,
policy_dir, entries[i].fops)) {
ppm_err("%s(), create /proc/ppm/policy/%s failed\n",
__func__, entries[i].name);
ret = -EINVAL;
goto out;
}
}
/* allocate mem and init policy limit_data */
sysboost_final_limit.limit =
kcalloc(ppm_main_info.cluster_num,
sizeof(*sysboost_final_limit.limit), GFP_KERNEL);
if (!sysboost_final_limit.limit) {
ret = -ENOMEM;
goto out;
}
for_each_ppm_clusters(i) {
sysboost_final_limit.limit[i].min_freq_idx = -1;
sysboost_final_limit.limit[i].max_freq_idx = -1;
sysboost_final_limit.limit[i].min_core_num = -1;
sysboost_final_limit.limit[i].max_core_num = -1;
}
/* list init */
for (i = 0; i < NR_PPM_SYSBOOST_USER; i++) {
sysboost_data[i].user = (enum ppm_sysboost_user)i;
switch (sysboost_data[i].user) {
case BOOST_BY_WIFI:
sysboost_data[i].user_name = "WIFI";
break;
case BOOST_BY_PERFSERV:
sysboost_data[i].user_name = "PERFSERV";
break;
case BOOST_BY_USB:
sysboost_data[i].user_name = "USB";
break;
case BOOST_BY_USB_PD:
sysboost_data[i].user_name = "USB_PD";
break;
case BOOST_BY_DEBUGD:
sysboost_data[i].user_name = "DEBUGD";
break;
case BOOST_BY_DEBUGD_64:
sysboost_data[i].user_name = "DEBUGD_64";
break;
case BOOST_BY_BOOT_TIME_OPT:
sysboost_data[i].user_name = "BOOT_TIME_OPT";
break;
case BOOST_BY_UT:
default:
sysboost_data[i].user_name = "UT";
break;
}
for_each_ppm_clusters(j) {
sysboost_data[i].limit[j].min_freq_idx = -1;
sysboost_data[i].limit[j].max_freq_idx = -1;
sysboost_data[i].limit[j].min_core_num = -1;
sysboost_data[i].limit[j].max_core_num = -1;
}
list_add(&sysboost_data[i].link, &sysboost_user_list);
}
if (ppm_main_register_policy(&sysboost_policy)) {
ppm_err("@%s: sysboost policy register failed\n", __func__);
ret = -EINVAL;
goto out;
}
ppm_info("@%s: register %s done!\n", __func__, sysboost_policy.name);
out:
sysboost_policy.is_enabled = false;
FUNC_EXIT(FUNC_LV_POLICY);
return ret;
}
static void __exit ppm_sysboost_policy_exit(void)
{
FUNC_ENTER(FUNC_LV_POLICY);
kfree(sysboost_final_limit.limit);
ppm_main_unregister_policy(&sysboost_policy);
FUNC_EXIT(FUNC_LV_POLICY);
}
module_init(ppm_sysboost_policy_init);
module_exit(ppm_sysboost_policy_exit);
@@ -0,0 +1,245 @@
// SPDX-License-Identifier: GPL-2.0
/*
* Copyright (c) 2019 MediaTek Inc.
*/
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/proc_fs.h>
#include <linux/seq_file.h>
#include <linux/slab.h>
#include <linux/uaccess.h>
#include <linux/topology.h>
#include "mtk_ppm_internal.h"
static void ppm_thermal_update_limit_cb(void);
static void ppm_thermal_status_change_cb(bool enable);
static struct ppm_cluster_status *cluster_status;
/* other members will init by ppm_main */
static struct ppm_policy_data thermal_policy = {
.name = __stringify(PPM_POLICY_THERMAL),
.lock = __MUTEX_INITIALIZER(thermal_policy.lock),
.policy = PPM_POLICY_THERMAL,
.priority = PPM_POLICY_PRIO_POWER_BUDGET_BASE,
.update_limit_cb = ppm_thermal_update_limit_cb,
.status_change_cb = ppm_thermal_status_change_cb,
};
void mt_ppm_cpu_thermal_protect(unsigned int limited_power)
{
FUNC_ENTER(FUNC_LV_POLICY);
ppm_ver("Get budget from thermal => limited_power = %d\n",
limited_power);
ppm_lock(&thermal_policy.lock);
if (!thermal_policy.is_enabled) {
ppm_warn("@%s: thermal policy is not enabled!\n", __func__);
ppm_unlock(&thermal_policy.lock);
goto end;
}
thermal_policy.req.power_budget = limited_power;
thermal_policy.is_activated = (limited_power) ? true : false;
ppm_unlock(&thermal_policy.lock);
#ifdef PPM_THERMAL_ENHANCEMENT
if (mutex_is_locked(&ppm_main_info.lock))
goto end;
#endif
mt_ppm_main();
end:
FUNC_EXIT(FUNC_LV_POLICY);
}
unsigned int mt_ppm_thermal_get_min_power(void)
{
return (unsigned int)ppm_get_min_pwr_idx();
}
unsigned int mt_ppm_thermal_get_max_power(void)
{
return (unsigned int)ppm_get_max_pwr_idx();
}
unsigned int mt_ppm_thermal_get_cur_power(void)
{
#ifndef NO_SCHEDULE_API
struct cpumask cluster_cpu, online_cpu;
#endif
int i;
int power;
/* skip if DVFS is not ready (we cannot get current freq...) */
if (!ppm_main_info.client_info[PPM_CLIENT_DVFS].limit_cb)
return 0;
if (!cluster_status)
return mt_ppm_thermal_get_max_power();
for_each_ppm_clusters(i) {
#ifndef NO_SCHEDULE_API
arch_get_cluster_cpus(&cluster_cpu, i);
cpumask_and(&online_cpu, &cluster_cpu, cpu_online_mask);
cluster_status[i].core_num = cpumask_weight(&online_cpu);
#else
cluster_status[i].core_num = get_cluster_max_cpu_core(i);
#endif
cluster_status[i].volt = 0; /* don't care */
if (!cluster_status[i].core_num)
cluster_status[i].freq_idx = -1;
else
cluster_status[i].freq_idx = ppm_main_freq_to_idx(
i,
ppm_get_cur_freq(i),
CPUFREQ_RELATION_L);
ppm_ver("[%d] core = %d, freq_idx = %d\n",
i, cluster_status[i].core_num,
cluster_status[i].freq_idx);
}
power = ppm_find_pwr_idx(cluster_status);
return (power == -1)
? mt_ppm_thermal_get_max_power() : (unsigned int)power;
}
static void ppm_thermal_update_limit_cb(void)
{
FUNC_ENTER(FUNC_LV_POLICY);
ppm_clear_policy_limit(&thermal_policy);
/* update limit according to power budget */
ppm_update_req_by_pwr(&thermal_policy.req);
FUNC_EXIT(FUNC_LV_POLICY);
}
static void ppm_thermal_status_change_cb(bool enable)
{
FUNC_ENTER(FUNC_LV_POLICY);
ppm_ver("thermal policy status changed to %d\n", enable);
FUNC_EXIT(FUNC_LV_POLICY);
}
static int ppm_thermal_limit_proc_show(struct seq_file *m, void *v)
{
seq_printf(m, "limited power = %d\n",
thermal_policy.req.power_budget);
seq_printf(m, "PPM thermal activate = %d\n",
thermal_policy.is_activated);
return 0;
}
static ssize_t ppm_thermal_limit_proc_write(struct file *file,
const char __user *buffer, size_t count, loff_t *pos)
{
unsigned int limited_power;
char *buf = ppm_copy_from_user_for_proc(buffer, count);
if (!buf)
return -EINVAL;
if (!kstrtouint(buf, 10, &limited_power))
#ifdef PPM_SSPM_SUPPORT
ppm_ipi_thermal_limit_test(limited_power);
#else
mt_ppm_cpu_thermal_protect(limited_power);
#endif
else
ppm_err("@%s: Invalid input!\n", __func__);
free_page((unsigned long)buf);
return count;
}
static int ppm_thermal_cur_power_proc_show(struct seq_file *m, void *v)
{
seq_printf(m, "current power = %d\n", mt_ppm_thermal_get_cur_power());
seq_printf(m, "min power = %d\n", mt_ppm_thermal_get_min_power());
seq_printf(m, "max power = %d\n", mt_ppm_thermal_get_max_power());
return 0;
}
PROC_FOPS_RW(thermal_limit);
PROC_FOPS_RO(thermal_cur_power);
static int __init ppm_thermal_policy_init(void)
{
int i, ret = 0;
struct pentry {
const char *name;
const struct file_operations *fops;
};
const struct pentry entries[] = {
PROC_ENTRY(thermal_limit),
PROC_ENTRY(thermal_cur_power),
};
FUNC_ENTER(FUNC_LV_POLICY);
/* create procfs */
for (i = 0; i < ARRAY_SIZE(entries); i++) {
if (!proc_create(entries[i].name, 0644,
policy_dir, entries[i].fops)) {
ppm_err("%s(), create /proc/ppm/policy/%s failed\n",
__func__, entries[i].name);
ret = -EINVAL;
goto out;
}
}
cluster_status = kcalloc(ppm_main_info.cluster_num,
sizeof(*cluster_status), GFP_KERNEL);
if (!cluster_status) {
ret = -ENOMEM;
goto out;
}
if (ppm_main_register_policy(&thermal_policy)) {
ppm_err("@%s: thermal policy register failed\n", __func__);
kfree(cluster_status);
ret = -EINVAL;
goto out;
}
ppm_info("@%s: register %s done!\n", __func__, thermal_policy.name);
out:
FUNC_EXIT(FUNC_LV_POLICY);
return ret;
}
static void __exit ppm_thermal_policy_exit(void)
{
FUNC_ENTER(FUNC_LV_POLICY);
kfree(cluster_status);
ppm_main_unregister_policy(&thermal_policy);
FUNC_EXIT(FUNC_LV_POLICY);
}
module_init(ppm_thermal_policy_init);
module_exit(ppm_thermal_policy_exit);
@@ -0,0 +1,478 @@
// SPDX-License-Identifier: GPL-2.0
/*
* Copyright (c) 2019 MediaTek Inc.
*/
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/slab.h>
#include "mtk_ppm_internal.h"
static void ppm_userlimit_update_limit_cb(void);
static void ppm_userlimit_status_change_cb(bool enable);
/* other members will init by ppm_main */
static struct ppm_policy_data userlimit_policy = {
.name = __stringify(PPM_POLICY_USER_LIMIT),
.lock = __MUTEX_INITIALIZER(userlimit_policy.lock),
.policy = PPM_POLICY_USER_LIMIT,
.priority = PPM_POLICY_PRIO_USER_SPECIFY_BASE,
.update_limit_cb = ppm_userlimit_update_limit_cb,
.status_change_cb = ppm_userlimit_status_change_cb,
};
struct ppm_userlimit_data userlimit_data = {
.is_freq_limited_by_user = false,
.is_core_limited_by_user = false,
};
/* MUST in lock */
static bool ppm_userlimit_is_policy_active(void)
{
if (!userlimit_data.is_freq_limited_by_user
&& !userlimit_data.is_core_limited_by_user)
return false;
else
return true;
}
static void ppm_userlimit_update_limit_cb(void)
{
unsigned int i;
struct ppm_policy_req *req = &userlimit_policy.req;
FUNC_ENTER(FUNC_LV_POLICY);
if (userlimit_data.is_freq_limited_by_user
|| userlimit_data.is_core_limited_by_user) {
ppm_clear_policy_limit(&userlimit_policy);
for (i = 0; i < req->cluster_num; i++) {
req->limit[i].min_cpufreq_idx =
(userlimit_data.limit[i].min_freq_idx == -1)
? req->limit[i].min_cpufreq_idx
: userlimit_data.limit[i].min_freq_idx;
req->limit[i].max_cpufreq_idx =
(userlimit_data.limit[i].max_freq_idx == -1)
? req->limit[i].max_cpufreq_idx
: userlimit_data.limit[i].max_freq_idx;
}
/* error check */
for (i = 0; i < req->cluster_num; i++) {
if (req->limit[i].max_cpu_core <
req->limit[i].min_cpu_core)
req->limit[i].min_cpu_core =
req->limit[i].max_cpu_core;
if (req->limit[i].max_cpufreq_idx >
req->limit[i].min_cpufreq_idx)
req->limit[i].min_cpufreq_idx =
req->limit[i].max_cpufreq_idx;
}
}
FUNC_EXIT(FUNC_LV_POLICY);
}
static void ppm_userlimit_status_change_cb(bool enable)
{
FUNC_ENTER(FUNC_LV_POLICY);
ppm_ver("userlimit policy status changed to %d\n", enable);
FUNC_EXIT(FUNC_LV_POLICY);
}
unsigned int mt_ppm_userlimit_cpu_core(unsigned int cluster_num,
struct ppm_limit_data *data)
{
/* phase-out */
return 0;
}
unsigned int mt_ppm_userlimit_cpu_freq(
unsigned int cluster_num, struct ppm_limit_data *data)
{
int i = 0;
int min_freq, max_freq, min_freq_idx, max_freq_idx;
bool is_limit = false;
/* Error check */
if (cluster_num > NR_PPM_CLUSTERS) {
ppm_err("Invalid cluster num = %d\n", cluster_num);
return -1;
}
if (!data) {
ppm_err("limit data is NULL!\n");
return -1;
}
ppm_lock(&userlimit_policy.lock);
if (!userlimit_policy.is_enabled) {
ppm_warn("userlimit policy is not enabled!\n");
ppm_unlock(&userlimit_policy.lock);
return -1;
}
/* update policy data */
for (i = 0; i < cluster_num; i++) {
min_freq = data[i].min;
max_freq = data[i].max;
/* check is all limit clear or not */
if (min_freq != -1 || max_freq != -1)
is_limit = true;
/* freq to idx */
min_freq_idx = (min_freq == -1) ? -1
: ppm_main_freq_to_idx(i, min_freq,
CPUFREQ_RELATION_L);
max_freq_idx = (max_freq == -1) ? -1
: ppm_main_freq_to_idx(i, max_freq,
CPUFREQ_RELATION_H);
/* sync to max_freq if min < max */
if (min_freq_idx != -1 && max_freq_idx != -1
&& min_freq_idx < max_freq_idx)
min_freq_idx = max_freq_idx;
/* write to policy data */
if (min_freq_idx != userlimit_data.limit[i].min_freq_idx ||
max_freq_idx != userlimit_data.limit[i].max_freq_idx) {
userlimit_data.limit[i].min_freq_idx = min_freq_idx;
userlimit_data.limit[i].max_freq_idx = max_freq_idx;
ppm_dbg(USER_LIMIT,
"%d:user limit min/max freq = %d(%d)/%d(%d)\n",
i, min_freq, min_freq_idx,
max_freq, max_freq_idx);
}
}
userlimit_data.is_freq_limited_by_user = is_limit;
userlimit_policy.is_activated = ppm_userlimit_is_policy_active();
ppm_unlock(&userlimit_policy.lock);
mt_ppm_main();
return 0;
}
unsigned int mt_ppm_userlimit_freq_limit_by_others(unsigned int cluster)
{
struct ppm_data *p = &ppm_main_info;
if (cluster >= NR_PPM_CLUSTERS)
return 0;
else
return p->cluster_info[cluster].max_freq_except_userlimit;
}
static int ppm_userlimit_min_cpu_freq_proc_show(struct seq_file *m, void *v)
{
int i;
for (i = 0; i < userlimit_policy.req.cluster_num; i++) {
seq_printf(m, "%d: min_freq_idx = %d, max_freq_idx = %d\n",
i, userlimit_data.limit[i].min_freq_idx,
userlimit_data.limit[i].max_freq_idx);
}
return 0;
}
static ssize_t ppm_userlimit_min_cpu_freq_proc_write(struct file *file,
const char __user *buffer, size_t count, loff_t *pos)
{
int id, min_freq, idx, i;
bool is_limit = false;
char *buf = ppm_copy_from_user_for_proc(buffer, count);
if (!buf)
return -EINVAL;
if (sscanf(buf, "%d %d", &id, &min_freq) == 2) {
if (id < 0 || id >= ppm_main_info.cluster_num) {
ppm_err("Invalid cluster id: %d\n", id);
goto out;
}
ppm_lock(&userlimit_policy.lock);
if (!userlimit_policy.is_enabled) {
ppm_warn("userlimit policy is not enabled!\n");
ppm_unlock(&userlimit_policy.lock);
goto out;
}
idx = (min_freq == -1) ? -1
: ppm_main_freq_to_idx(id, min_freq,
CPUFREQ_RELATION_L);
/* error check, sync to max idx if min freq > max freq */
if (userlimit_data.limit[id].max_freq_idx != -1
&& idx != -1
&& idx < userlimit_data.limit[id].max_freq_idx)
idx = userlimit_data.limit[id].max_freq_idx;
if (idx != userlimit_data.limit[id].min_freq_idx) {
userlimit_data.limit[id].min_freq_idx = idx;
ppm_dbg(USER_LIMIT,
"%d: userlimit min_freq= %d KHz(%d)\n",
id, min_freq, idx);
}
/* check is freq limited or not */
for (i = 0; i < userlimit_policy.req.cluster_num; i++) {
if (userlimit_data.limit[i].min_freq_idx != -1
|| userlimit_data.limit[i].max_freq_idx != -1) {
is_limit = true;
break;
}
}
userlimit_data.is_freq_limited_by_user = is_limit;
userlimit_policy.is_activated =
ppm_userlimit_is_policy_active();
ppm_unlock(&userlimit_policy.lock);
mt_ppm_main();
} else
ppm_err("@%s: Invalid input!\n", __func__);
out:
free_page((unsigned long)buf);
return count;
}
static int ppm_userlimit_max_cpu_freq_proc_show(struct seq_file *m, void *v)
{
int i;
for (i = 0; i < userlimit_policy.req.cluster_num; i++) {
seq_printf(m, "%d: min_freq_idx = %d, max_freq_idx = %d\n",
i, userlimit_data.limit[i].min_freq_idx,
userlimit_data.limit[i].max_freq_idx);
}
return 0;
}
static ssize_t ppm_userlimit_max_cpu_freq_proc_write(
struct file *file, const char __user *buffer, size_t count, loff_t *pos)
{
int id, max_freq, idx, i;
bool is_limit = false;
char *buf = ppm_copy_from_user_for_proc(buffer, count);
if (!buf)
return -EINVAL;
if (sscanf(buf, "%d %d", &id, &max_freq) == 2) {
if (id < 0 || id >= ppm_main_info.cluster_num) {
ppm_err("Invalid cluster id: %d\n", id);
goto out;
}
ppm_lock(&userlimit_policy.lock);
if (!userlimit_policy.is_enabled) {
ppm_warn("userlimit policy is not enabled!\n");
ppm_unlock(&userlimit_policy.lock);
goto out;
}
idx = (max_freq == -1) ? -1
: ppm_main_freq_to_idx(id, max_freq,
CPUFREQ_RELATION_H);
/* error check, sync to max idx if max freq < min freq */
if (userlimit_data.limit[id].min_freq_idx != -1
&& idx != -1
&& idx > userlimit_data.limit[id].min_freq_idx)
userlimit_data.limit[id].min_freq_idx = idx;
if (idx != userlimit_data.limit[id].max_freq_idx) {
userlimit_data.limit[id].max_freq_idx = idx;
ppm_dbg(USER_LIMIT,
"%d:user limit max_freq = %d KHz(idx = %d)\n",
id, max_freq, idx);
}
/* check is freq limited or not */
for (i = 0; i < userlimit_policy.req.cluster_num; i++) {
if (userlimit_data.limit[i].min_freq_idx != -1
|| userlimit_data.limit[i].max_freq_idx != -1) {
is_limit = true;
break;
}
}
userlimit_data.is_freq_limited_by_user = is_limit;
userlimit_policy.is_activated =
ppm_userlimit_is_policy_active();
ppm_unlock(&userlimit_policy.lock);
mt_ppm_main();
} else
ppm_err("@%s: Invalid input!\n", __func__);
out:
free_page((unsigned long)buf);
return count;
}
static int ppm_userlimit_cpu_freq_proc_show(struct seq_file *m, void *v)
{
int i;
for (i = 0; i < userlimit_policy.req.cluster_num; i++) {
seq_printf(m, "%d: min_freq_idx = %d, max_freq_idx = %d\n",
i, userlimit_data.limit[i].min_freq_idx,
userlimit_data.limit[i].max_freq_idx);
}
return 0;
}
static ssize_t ppm_userlimit_cpu_freq_proc_write(struct file *file,
const char __user *buffer, size_t count, loff_t *pos)
{
int i = 0, data;
struct ppm_limit_data freq_limit[NR_PPM_CLUSTERS];
unsigned int arg_num = NR_PPM_CLUSTERS * 2; /* for min and max */
char *tok, *tmp;
char *buf = ppm_copy_from_user_for_proc(buffer, count);
if (!buf)
return -EINVAL;
tmp = buf;
while ((tok = strsep(&tmp, " ")) != NULL) {
if (i == arg_num) {
ppm_err("number of arguments > %d!\n", arg_num);
goto out;
}
if (kstrtoint(tok, 10, &data)) {
ppm_err("Invalid input: %s\n", tok);
goto out;
} else {
if (i % 2) /* max */
freq_limit[i/2].max = data;
else /* min */
freq_limit[i/2].min = data;
i++;
}
}
if (i < arg_num)
ppm_err("number of arguments < %d!\n", arg_num);
else
mt_ppm_userlimit_cpu_freq(NR_PPM_CLUSTERS, freq_limit);
out:
free_page((unsigned long)buf);
return count;
}
static int ppm_userlimit_freq_limit_by_others_proc_show(
struct seq_file *m, void *v)
{
int i;
for_each_ppm_clusters(i) {
seq_printf(m, "cluster %d max_freq_limit = %d\n",
i, mt_ppm_userlimit_freq_limit_by_others(i));
}
return 0;
}
PROC_FOPS_RW(userlimit_min_cpu_freq);
PROC_FOPS_RW(userlimit_max_cpu_freq);
PROC_FOPS_RW(userlimit_cpu_freq);
PROC_FOPS_RO(userlimit_freq_limit_by_others);
static int __init ppm_userlimit_policy_init(void)
{
int i, ret = 0;
struct pentry {
const char *name;
const struct file_operations *fops;
};
const struct pentry entries[] = {
PROC_ENTRY(userlimit_min_cpu_freq),
PROC_ENTRY(userlimit_max_cpu_freq),
PROC_ENTRY(userlimit_cpu_freq),
PROC_ENTRY(userlimit_freq_limit_by_others),
};
FUNC_ENTER(FUNC_LV_POLICY);
/* create procfs */
for (i = 0; i < ARRAY_SIZE(entries); i++) {
if (!proc_create(entries[i].name, 0644,
policy_dir, entries[i].fops)) {
ppm_err("%s(), create /proc/ppm/policy/%s failed\n",
__func__, entries[i].name);
ret = -EINVAL;
goto out;
}
}
userlimit_data.limit = kcalloc(ppm_main_info.cluster_num,
sizeof(*userlimit_data.limit), GFP_KERNEL);
if (!userlimit_data.limit) {
ret = -ENOMEM;
goto out;
}
/* init userlimit_data */
for_each_ppm_clusters(i) {
userlimit_data.limit[i].min_freq_idx = -1;
userlimit_data.limit[i].max_freq_idx = -1;
userlimit_data.limit[i].min_core_num = -1;
userlimit_data.limit[i].max_core_num = -1;
}
if (ppm_main_register_policy(&userlimit_policy)) {
ppm_err("@%s: userlimit policy register failed\n", __func__);
kfree(userlimit_data.limit);
ret = -EINVAL;
goto out;
}
ppm_info("@%s: register %s done!\n", __func__, userlimit_policy.name);
out:
FUNC_EXIT(FUNC_LV_POLICY);
return ret;
}
static void __exit ppm_userlimit_policy_exit(void)
{
FUNC_ENTER(FUNC_LV_POLICY);
kfree(userlimit_data.limit);
ppm_main_unregister_policy(&userlimit_policy);
FUNC_EXIT(FUNC_LV_POLICY);
}
module_init(ppm_userlimit_policy_init);
module_exit(ppm_userlimit_policy_exit);
@@ -0,0 +1,332 @@
// SPDX-License-Identifier: GPL-2.0
/*
* Copyright (c) 2019 MediaTek Inc.
*/
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/slab.h>
#include "mtk_ppm_internal.h"
static void ppm_ut_update_limit_cb(void);
/* other members will init by ppm_main */
static struct ppm_policy_data ut_policy = {
.name = __stringify(PPM_POLICY_UT),
.lock = __MUTEX_INITIALIZER(ut_policy.lock),
.policy = PPM_POLICY_UT,
.priority = PPM_POLICY_PRIO_HIGHEST,
.update_limit_cb = ppm_ut_update_limit_cb,
.status_change_cb = NULL,
};
struct ppm_ut_data {
bool is_freq_idx_fixed;
bool is_core_num_fixed;
struct ppm_ut_limit {
int freq_idx;
int core_num;
} *limit;
} ut_data;
static void ppm_ut_update_limit_cb(void)
{
}
static int ppm_ut_fix_core_num_proc_show(struct seq_file *m, void *v)
{
int i;
for (i = 0; i < ut_policy.req.cluster_num; i++)
seq_printf(m, "cluster %d fix core num = %d\n",
i, ut_data.limit[i].core_num);
return 0;
}
static ssize_t ppm_ut_fix_core_num_proc_write(struct file *file,
const char __user *buffer, size_t count, loff_t *pos)
{
int i = 0;
int *fix_core;
bool activated = true;
bool is_clear = true;
unsigned int cluster_num = ut_policy.req.cluster_num;
char *buf, *tok, *tmp;
if (!ut_data.limit)
return -EINVAL;
buf = ppm_copy_from_user_for_proc(buffer, count);
if (!buf)
return -ENOMEM;
fix_core = kcalloc(cluster_num, sizeof(*fix_core), GFP_KERNEL);
if (!fix_core)
goto no_mem;
tmp = buf;
while ((tok = strsep(&tmp, " ")) != NULL) {
if (i == cluster_num) {
ppm_err("number of arguments > %d!\n", cluster_num);
goto out;
}
if (kstrtoint(tok, 10, &fix_core[i])) {
ppm_err("Invalid input: %s\n", tok);
goto out;
} else
i++;
}
if (i < cluster_num) {
ppm_err("number of arguments < %d!\n", cluster_num);
goto out;
}
for (i = 0; i < cluster_num; i++) {
if (fix_core[i] > (int)get_cluster_max_cpu_core(i)) {
ppm_err("@%s: Invalid input! fix_core[%d] = %d\n",
__func__, i, fix_core[i]);
goto out;
} else if (fix_core[i] != -1)
is_clear = false;
}
ppm_lock(&ut_policy.lock);
if (!ut_policy.is_enabled) {
ppm_warn("@%s: UT policy is not enabled!\n", __func__);
ppm_unlock(&ut_policy.lock);
goto out;
}
if (is_clear) {
ut_data.is_core_num_fixed = false;
for (i = 0; i < cluster_num; i++) {
ut_data.limit[i].core_num = -1;
ut_policy.req.limit[i].min_cpu_core =
get_cluster_min_cpu_core(i);
ut_policy.req.limit[i].max_cpu_core =
get_cluster_max_cpu_core(i);
}
} else {
ut_data.is_core_num_fixed = true;
for (i = 0; i < cluster_num; i++) {
ut_data.limit[i].core_num = fix_core[i];
ut_policy.req.limit[i].min_cpu_core =
(fix_core[i] == -1)
? get_cluster_min_cpu_core(i)
: fix_core[i];
ut_policy.req.limit[i].max_cpu_core =
(fix_core[i] == -1)
? get_cluster_max_cpu_core(i)
: fix_core[i];
}
}
/* unlimited */
if (!ut_data.is_core_num_fixed && !ut_data.is_freq_idx_fixed)
activated = false;
/* fire ppm HERE!*/
ut_policy.is_activated = activated;
ppm_unlock(&ut_policy.lock);
mt_ppm_main();
out:
kfree(fix_core);
no_mem:
free_page((unsigned long)buf);
return count;
}
static int ppm_ut_fix_freq_idx_proc_show(struct seq_file *m, void *v)
{
int i;
for (i = 0; i < ut_policy.req.cluster_num; i++)
seq_printf(m, "cluster %d fix freq idx = %d\n",
i, ut_data.limit[i].freq_idx);
return 0;
}
static ssize_t ppm_ut_fix_freq_idx_proc_write(struct file *file,
const char __user *buffer, size_t count, loff_t *pos)
{
int i = 0;
int *fix_freq;
bool activated = true;
bool is_clear = true;
unsigned int cluster_num = ut_policy.req.cluster_num;
char *buf, *tok, *tmp;
if (!ut_data.limit)
return -EINVAL;
buf = ppm_copy_from_user_for_proc(buffer, count);
if (!buf)
return -ENOMEM;
fix_freq = kcalloc(cluster_num, sizeof(*fix_freq), GFP_KERNEL);
if (!fix_freq)
goto no_mem;
tmp = buf;
while ((tok = strsep(&tmp, " ")) != NULL) {
if (i == cluster_num) {
ppm_err("number of arguments > %d!\n", cluster_num);
goto out;
}
if (kstrtoint(tok, 10, &fix_freq[i])) {
ppm_err("Invalid input: %s\n", tok);
goto out;
} else
i++;
}
if (i < cluster_num) {
ppm_err("number of arguments < %d!\n", cluster_num);
goto out;
}
for (i = 0; i < cluster_num; i++) {
if (fix_freq[i] > (int)get_cluster_min_cpufreq_idx(i)) {
ppm_err("@%s: Invalid input! fix_freq[%d] = %d\n",
__func__, i, fix_freq[i]);
goto out;
} else if (fix_freq[i] != -1)
is_clear = false;
}
ppm_lock(&ut_policy.lock);
if (!ut_policy.is_enabled) {
ppm_warn("@%s: UT policy is not enabled!\n", __func__);
ppm_unlock(&ut_policy.lock);
goto out;
}
if (is_clear) {
ut_data.is_freq_idx_fixed = false;
for (i = 0; i < cluster_num; i++) {
ut_data.limit[i].freq_idx = -1;
ut_policy.req.limit[i].min_cpufreq_idx =
get_cluster_min_cpufreq_idx(i);
ut_policy.req.limit[i].max_cpufreq_idx =
get_cluster_max_cpufreq_idx(i);
}
} else {
ut_data.is_freq_idx_fixed = true;
for (i = 0; i < cluster_num; i++) {
ut_data.limit[i].freq_idx = fix_freq[i];
ut_policy.req.limit[i].min_cpufreq_idx =
(fix_freq[i] == -1)
? get_cluster_min_cpufreq_idx(i)
: fix_freq[i];
ut_policy.req.limit[i].max_cpufreq_idx =
(fix_freq[i] == -1)
? get_cluster_max_cpufreq_idx(i)
: fix_freq[i];
}
}
/* unlimited */
if (!ut_data.is_core_num_fixed && !ut_data.is_freq_idx_fixed)
activated = false;
/* fire ppm HERE!*/
ut_policy.is_activated = activated;
ppm_unlock(&ut_policy.lock);
mt_ppm_main();
out:
kfree(fix_freq);
no_mem:
free_page((unsigned long)buf);
return count;
}
PROC_FOPS_RW(ut_fix_core_num);
PROC_FOPS_RW(ut_fix_freq_idx);
static int __init ppm_ut_policy_init(void)
{
int i, ret = 0;
struct pentry {
const char *name;
const struct file_operations *fops;
};
const struct pentry entries[] = {
PROC_ENTRY(ut_fix_core_num),
PROC_ENTRY(ut_fix_freq_idx),
};
FUNC_ENTER(FUNC_LV_POLICY);
/* create procfs */
for (i = 0; i < ARRAY_SIZE(entries); i++) {
if (!proc_create(entries[i].name, 0644,
policy_dir, entries[i].fops)) {
ppm_err("%s(), create /proc/ppm/policy/%s failed\n",
__func__, entries[i].name);
ret = -EINVAL;
goto out;
}
}
ut_data.limit = kcalloc(ppm_main_info.cluster_num,
sizeof(*ut_data.limit), GFP_KERNEL);
if (!ut_data.limit) {
ret = -ENOMEM;
goto out;
}
/* init ut_data */
ut_data.is_freq_idx_fixed = false;
ut_data.is_core_num_fixed = false;
for (i = 0; i < ppm_main_info.cluster_num; i++) {
ut_data.limit[i].freq_idx = -1;
ut_data.limit[i].core_num = -1;
}
if (ppm_main_register_policy(&ut_policy)) {
ppm_err("@%s: UT policy register failed\n", __func__);
ret = -EINVAL;
kfree(ut_data.limit);
goto out;
}
ppm_info("@%s: register %s done!\n", __func__, ut_policy.name);
out:
FUNC_EXIT(FUNC_LV_POLICY);
return ret;
}
static void __exit ppm_ut_policy_exit(void)
{
FUNC_ENTER(FUNC_LV_POLICY);
kfree(ut_data.limit);
ppm_main_unregister_policy(&ut_policy);
FUNC_EXIT(FUNC_LV_POLICY);
}
module_init(ppm_ut_policy_init);
module_exit(ppm_ut_policy_exit);
@@ -0,0 +1,252 @@
// SPDX-License-Identifier: GPL-2.0
/*
* Copyright (c) 2019 MediaTek Inc.
*/
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/cpu.h>
#include <linux/spinlock.h>
#include <linux/ktime.h>
#include <linux/string.h>
#include "mtk_ppm_internal.h"
struct ppm_profile {
bool is_profiling;
struct mutex lock;
/* client execution time profiling */
unsigned long long max_client_exec_time_us[NR_PPM_CLIENTS];
unsigned long long avg_client_exec_time_us[NR_PPM_CLIENTS];
#ifdef PPM_SSPM_SUPPORT
/* ipi execution time profiling */
unsigned long long max_ipi_exec_time_us[NR_PPM_IPI];
unsigned long long avg_ipi_exec_time_us[NR_PPM_IPI];
#endif
} ppm_profile_data;
static void ppm_profile_dump_client_exec_time(struct seq_file *m)
{
int i;
seq_puts(m, "\n==========================================\n");
seq_puts(m, "PPM Client Execution Time (us)");
seq_puts(m, "\n==========================================\n");
seq_printf(m, "%-10s%-10s%-10s\n", "Client", "Avg Time", "Max Time");
for_each_ppm_clients(i) {
seq_printf(m, "%-10s%-10lld%-10lld\n",
ppm_main_info.client_info[i].name,
ppm_profile_data.avg_client_exec_time_us[i],
ppm_profile_data.max_client_exec_time_us[i]);
}
}
#ifdef PPM_SSPM_SUPPORT
static void ppm_profile_dump_ipi_exec_time(struct seq_file *m)
{
int i;
seq_puts(m, "\n==========================================\n");
seq_puts(m, "PPM IPI Execution Time (us)");
seq_puts(m, "\n==========================================\n");
seq_printf(m, "%-10s%-10s%-10s\n", "IPI type",
"Avg Time", "Max Time");
for (i = 0; i < NR_PPM_IPI; i++) {
seq_printf(m, "%-10d%-10lld%-10lld\n", i,
ppm_profile_data.avg_ipi_exec_time_us[i],
ppm_profile_data.max_ipi_exec_time_us[i]);
}
}
#endif
void ppm_profile_update_client_exec_time(enum ppm_client client,
unsigned long long time)
{
ppm_lock(&ppm_profile_data.lock);
if (!ppm_profile_data.is_profiling)
goto end;
if (client >= NR_PPM_CLIENTS)
goto end;
if (time > ppm_profile_data.max_client_exec_time_us[client])
ppm_profile_data.max_client_exec_time_us[client] = time;
ppm_profile_data.avg_client_exec_time_us[client] =
(ppm_profile_data.avg_client_exec_time_us[client] + time) / 2;
ppm_dbg(TIME_PROFILE, "@%s: client = %s, time = %lld\n",
__func__, ppm_main_info.client_info[client].name, time);
end:
ppm_unlock(&ppm_profile_data.lock);
}
#ifdef PPM_SSPM_SUPPORT
void ppm_profile_update_ipi_exec_time(int id,
unsigned long long time)
{
ppm_lock(&ppm_profile_data.lock);
if (!ppm_profile_data.is_profiling)
goto end;
if (id >= NR_PPM_IPI)
goto end;
if (time > ppm_profile_data.max_ipi_exec_time_us[id])
ppm_profile_data.max_ipi_exec_time_us[id] = time;
ppm_profile_data.avg_ipi_exec_time_us[id] =
(ppm_profile_data.avg_ipi_exec_time_us[id] + time) / 2;
ppm_dbg(TIME_PROFILE, "@%s: IPI id = %d, time = %lld\n",
__func__, id, time);
end:
ppm_unlock(&ppm_profile_data.lock);
}
#endif
static int ppm_dump_info_proc_show(struct seq_file *m, void *v)
{
ppm_lock(&ppm_profile_data.lock);
ppm_profile_dump_client_exec_time(m);
#ifdef PPM_SSPM_SUPPORT
ppm_profile_dump_ipi_exec_time(m);
#endif
ppm_unlock(&ppm_profile_data.lock);
return 0;
}
static int ppm_client_exec_time_proc_show(struct seq_file *m, void *v)
{
ppm_lock(&ppm_profile_data.lock);
ppm_profile_dump_client_exec_time(m);
ppm_unlock(&ppm_profile_data.lock);
return 0;
}
#ifdef PPM_SSPM_SUPPORT
static int ppm_ipi_exec_time_proc_show(struct seq_file *m, void *v)
{
ppm_lock(&ppm_profile_data.lock);
ppm_profile_dump_ipi_exec_time(m);
ppm_unlock(&ppm_profile_data.lock);
return 0;
}
#endif
static int ppm_profile_on_proc_show(struct seq_file *m, void *v)
{
seq_printf(m, "%d\n", ppm_profile_data.is_profiling);
return 0;
}
static ssize_t ppm_profile_on_proc_write(struct file *file,
const char __user *buffer, size_t count, loff_t *pos)
{
int enable;
struct ppm_profile *p = &ppm_profile_data;
char *buf = ppm_copy_from_user_for_proc(buffer, count);
if (!buf)
return -EINVAL;
if (!kstrtoint(buf, 10, &enable)) {
ppm_lock(&(p->lock));
if ((p->is_profiling && enable)
|| (!p->is_profiling && !enable))
goto end;
/* on: record current time and clear all data */
/* off: update time_in_state */
if (!enable) {
ppm_info("profiling stop!\n");
p->is_profiling = false;
} else {
int i;
ppm_info("profiling start!\n");
/* clear data */
for_each_ppm_clients(i) {
p->max_client_exec_time_us[i] = 0;
p->avg_client_exec_time_us[i] = 0;
#ifdef PPM_SSPM_SUPPORT
p->max_ipi_exec_time_us[i] = 0;
p->avg_ipi_exec_time_us[i] = 0;
#endif
}
/* start profiling */
p->is_profiling = true;
}
end:
ppm_unlock(&(p->lock));
} else
ppm_err("echo [0/1] > /proc/ppm/profile/profile_on\n");
free_page((unsigned long)buf);
return count;
}
PROC_FOPS_RO(dump_info);
PROC_FOPS_RO(client_exec_time);
#ifdef PPM_SSPM_SUPPORT
PROC_FOPS_RO(ipi_exec_time);
#endif
PROC_FOPS_RW(profile_on);
int ppm_profile_init(void)
{
int i, ret = 0;
struct pentry {
const char *name;
const struct file_operations *fops;
};
const struct pentry entries[] = {
PROC_ENTRY(dump_info),
PROC_ENTRY(client_exec_time),
#ifdef PPM_SSPM_SUPPORT
PROC_ENTRY(ipi_exec_time),
#endif
PROC_ENTRY(profile_on),
};
/* create procfs */
for (i = 0; i < ARRAY_SIZE(entries); i++) {
if (!proc_create(entries[i].name, 0644,
profile_dir, entries[i].fops)) {
ppm_err("%s(), create /proc/ppm/profile/%s failed\n",
__func__, entries[i].name);
ret = -EINVAL;
goto out;
}
}
mutex_init(&ppm_profile_data.lock);
ppm_info("%s done\n", __func__);
out:
return ret;
}
void ppm_profile_exit(void)
{
}