blob: 5c7d9cc1d8f53574df3dbafd0f9b70aa2c15bfe1 [file] [log] [blame]
/*
* Copyright (c) 2018 Samsung Electronics Co., Ltd.
*
* CPU Part
*
* CPU Hotplug driver for Exynos
*
* 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.
*/
#include <linux/cpu.h>
#include <linux/fb.h>
#include <linux/kthread.h>
#include <linux/pm_qos.h>
#include <linux/suspend.h>
#include <linux/debug-snapshot.h>
#include <soc/samsung/exynos-cpuhp.h>
#define CPUHP_USER_NAME_LEN 16
struct cpuhp_user {
struct list_head list;
char name[CPUHP_USER_NAME_LEN];
struct cpumask online_cpus;
int type;
};
static struct {
/* Control cpu hotplug operation */
bool enabled;
/* flag for suspend */
bool suspended;
/* flag for debug print */
bool debug;
/* list head for requester */
struct list_head users;
/* user for system */
struct cpuhp_user system_user;
/* user for sysfs */
struct cpuhp_user sysfs_user;
/* Synchronizes accesses to refcount and cpumask */
struct mutex lock;
/* fast hotplug cpus */
struct cpumask fast_hp_cpus;
/* user request mask */
struct cpumask online_cpus;
/* cpuhp kobject */
struct kobject *kobj;
} cpuhp = {
.lock = __MUTEX_INITIALIZER(cpuhp.lock),
};
/**********************************************************************************/
/* Helper */
/**********************************************************************************/
static int cpuhp_do(int fast_hp);
/*
* Update pm_suspend status.
* During suspend-resume, cpuhp driver is stop
*/
static inline void cpuhp_suspend(bool enable)
{
/* This lock guarantees completion of cpuhp_do() */
mutex_lock(&cpuhp.lock);
cpuhp.suspended = enable;
mutex_unlock(&cpuhp.lock);
}
/*
* Update cpuhp enablestatus.
* cpuhp driver is working when enabled big is TRUE
*/
static inline void cpuhp_enable(bool enable)
{
mutex_lock(&cpuhp.lock);
cpuhp.enabled = enable;
mutex_unlock(&cpuhp.lock);
}
/* find user matched name. if return NULL, there is no user matched name */
static struct cpuhp_user* cpuhp_find_user(char *name)
{
struct cpuhp_user *user;
list_for_each_entry(user, &cpuhp.users, list)
if (!strcmp(user->name, name))
return user;
return NULL;
}
/* update user's requesting cpu mask */
static int cpuhp_update_user(char *name, struct cpumask mask, int type)
{
struct cpuhp_user *user = cpuhp_find_user(name);
if (!user)
return -EINVAL;
cpumask_copy(&user->online_cpus, &mask);
user->type = type;
return 0;
}
/* remove user from hotplug requesting user list */
int exynos_cpuhp_unregister(char *name, struct cpumask mask, int type)
{
return 0;
}
/*
* Register cpu-hp user
* Users and IPs that want to use cpu-hp should register through this function.
* name: Must have a unique value, and panic will occur if you use an already
* registered name.
* mask: The cpu mask that user wants to ONLINE and cpu OFF bits has more HIGH
* priority than ONLINE bit. This mask is default cpu mask at registration
* and it is reflected immediately after registration.
* type: cpu-hp type (0-> legacy cpu hp, 0xFA57-> fast cpu hp)
*/
int exynos_cpuhp_register(char *name, struct cpumask mask, int type)
{
struct cpuhp_user *user;
char buf[10];
mutex_lock(&cpuhp.lock);
/* check wether name is already register or not */
if (cpuhp_find_user(name))
panic("CPUHP: Failed to register cpuhp! this name already existed\n");
/* allocate memory for new user */
user = kzalloc(sizeof(struct cpuhp_user), GFP_KERNEL);
if (!user) {
mutex_unlock(&cpuhp.lock);
return -ENOMEM;
}
/* init new user's information */
cpumask_copy(&user->online_cpus, &mask);
strcpy(user->name, name);
user->type = type;
/* register user list */
list_add(&user->list, &cpuhp.users);
scnprintf(buf, sizeof(buf), "%*pbl", cpumask_pr_args(&user->online_cpus));
pr_info("CPUHP: reigstered new user(name:%s, mask:%s)\n", user->name, buf);;
mutex_unlock(&cpuhp.lock);
/* applying new user's request */
return cpuhp_do(true);
}
/*
* User requests cpu-hp.
* The mask contains the requested cpu mask, and the type is hp operaton type.
* The INTERSECTIONS of other user's request masks is determined by the final cpu-mask.
*/
int exynos_cpuhp_request(char *name, struct cpumask mask, int type)
{
if (cpuhp_update_user(name, mask, type))
return 0;
/* use fast cpu hotplug sequence */
if (type == FAST_HP)
return cpuhp_do(true);
return cpuhp_do(true);
}
/**********************************************************************************/
/* cpu hp operater */
/**********************************************************************************/
/* legacy hotplug in */
static int cpuhp_in(const struct cpumask *mask)
{
int cpu, ret = 0;
for_each_cpu(cpu, mask) {
ret = cpu_up(cpu);
if (ret) {
/*
* If it fails to enable cpu,
* it cancels cpu hotplug request and retries later.
*/
pr_err("%s: Failed to hotplug in CPU%d with error %d\n",
__func__, cpu, ret);
break;
}
}
return ret;
}
/* legacy hotplug out */
static int cpuhp_out(const struct cpumask *mask)
{
int cpu, ret = 0;
/*
* Reverse order of cpu,
* explore cpu7, cpu6, cpu5, ... cpu1
*/
for (cpu = nr_cpu_ids - 1; cpu > 0; cpu--) {
if (!cpumask_test_cpu(cpu, mask))
continue;
ret = cpu_down(cpu);
if (ret) {
pr_err("%s: Failed to hotplug out CPU%d with error %d\n",
__func__, cpu, ret);
break;
}
}
return ret;
}
/*
* Return last target online cpu mask
* Returns the cpu_mask INTERSECTIONS of all users in the user list.
*/
static struct cpumask cpuhp_get_online_cpus(void)
{
struct cpumask mask;
struct cpuhp_user *user;
char buf[10];
cpumask_setall(&mask);
list_for_each_entry(user, &cpuhp.users, list)
cpumask_and(&mask, &mask, &user->online_cpus);
if (cpumask_empty(&mask) || !cpumask_test_cpu(0, &mask)) {
scnprintf(buf, sizeof(buf), "%*pbl", cpumask_pr_args(&mask));
panic("CPUHP: Online mask(%s) is wrong \n", buf);
}
return mask;
}
/*
* Executes cpu_up
* Run cpu_up according to the cpu control operation type.
*/
static int cpuhp_cpu_up(struct cpumask enable_cpus, int fast_hp)
{
struct cpumask fast_cpus;
int ret = 0;
cpumask_clear(&fast_cpus);
if (fast_hp)
cpumask_copy(&fast_cpus, &cpuhp.fast_hp_cpus);
cpumask_and(&fast_cpus, &enable_cpus, &fast_cpus);
cpumask_andnot(&enable_cpus, &enable_cpus, &fast_cpus);
if (!cpumask_empty(&enable_cpus))
ret = cpuhp_in(&enable_cpus);
if (ret)
goto exit;
if (fast_hp && !cpumask_empty(&fast_cpus))
ret = cpus_up(fast_cpus);
return ret;
exit:
pr_info("failed to cpuhp_cpu_up(%d)\n", ret);
return ret;
}
/*
* Executes cpu_down
* Run cpu_up according to the cpu control operation type.
*/
static int cpuhp_cpu_down(struct cpumask disable_cpus, int fast_hp)
{
struct cpumask fast_cpus;
int ret = 0;
cpumask_clear(&fast_cpus);
if (fast_hp)
cpumask_copy(&fast_cpus, &cpuhp.fast_hp_cpus);
cpumask_and(&fast_cpus, &disable_cpus, &fast_cpus);
cpumask_andnot(&disable_cpus, &disable_cpus, &fast_cpus);
if (fast_hp && !cpumask_empty(&fast_cpus))
ret = cpus_down(fast_cpus);
if (ret)
goto exit;
if (!cpumask_empty(&disable_cpus))
ret = cpuhp_out(&disable_cpus);
return ret;
exit:
pr_info("failed to cpuhp_cpu_down(%d)\n", ret);
return ret;
}
/* print cpu control informatoin for deubgging */
static void cpuhp_print_debug_info(struct cpumask online_cpus, int fast_hp)
{
char new_buf[10], pre_buf[10];
scnprintf(pre_buf, sizeof(pre_buf), "%*pbl", cpumask_pr_args(&cpuhp.online_cpus));
scnprintf(new_buf, sizeof(new_buf), "%*pbl", cpumask_pr_args(&online_cpus));
dbg_snapshot_printk("%s: %s -> %s fast_hp=%d\n", __func__, pre_buf, new_buf, fast_hp);
/* print cpu control information */
if (cpuhp.debug)
pr_info("%s: %s -> %s fast_hp=%d\n", __func__, pre_buf, new_buf, fast_hp);
}
/*
* cpuhp_do() is the main function for cpu hotplug. Only this function
* enables or disables cpus, so all APIs in this driver call cpuhp_do()
* eventually.
*/
static int cpuhp_do(int fast_hp)
{
int ret = 0;
struct cpumask online_cpus, enable_cpus, disable_cpus;
mutex_lock(&cpuhp.lock);
/*
* If cpu hotplug is disabled or suspended,
* cpuhp_do() do nothing.
*/
if (!cpuhp.enabled || cpuhp.suspended) {
mutex_unlock(&cpuhp.lock);
return 0;
}
online_cpus = cpuhp_get_online_cpus();
cpuhp_print_debug_info(online_cpus, fast_hp);
/* if there is no mask change, skip */
if (cpumask_equal(&cpuhp.online_cpus, &online_cpus))
goto out;
/* get the enable cpu mask for new online cpu */
cpumask_andnot(&enable_cpus, &online_cpus, &cpuhp.online_cpus);
/* get the disable cpu mask for new offline cpu */
cpumask_andnot(&disable_cpus, &cpuhp.online_cpus, &online_cpus);
if (!cpumask_empty(&enable_cpus))
ret = cpuhp_cpu_up(enable_cpus, fast_hp);
if (ret)
goto out;
if (!cpumask_empty(&disable_cpus))
ret = cpuhp_cpu_down(disable_cpus, fast_hp);
cpumask_copy(&cpuhp.online_cpus, &online_cpus);
out:
mutex_unlock(&cpuhp.lock);
return ret;
}
static int cpuhp_control(bool enable)
{
struct cpumask mask;
int ret = 0;
if (enable) {
cpuhp_enable(true);
cpuhp_do(true);
} else {
mutex_lock(&cpuhp.lock);
cpumask_setall(&mask);
cpumask_andnot(&mask, &mask, cpu_online_mask);
/*
* If it success to enable all CPUs, clear cpuhp.enabled flag.
* Since then all hotplug requests are ignored.
*/
ret = cpuhp_in(&mask);
if (!ret) {
/*
* In this position, can't use cpuhp_enable()
* because already taken cpuhp.lock
*/
cpuhp.enabled = false;
} else {
pr_err("Fail to disable cpu hotplug, please try again\n");
}
mutex_unlock(&cpuhp.lock);
}
return ret;
}
/**********************************************************************************/
/* SYSFS */
/**********************************************************************************/
/*
* User can change the number of online cpu by using min_online_cpu and
* max_online_cpu sysfs node. User input minimum and maxinum online cpu
* to this node as below:
*
* #echo mask > /sys/power/cpuhp/set_online_cpu
*/
#define STR_LEN 6
static inline char toupper(char ch)
{
if ('a' <= ch && ch <= 'z')
ch += 'A' - 'a';
return ch;
}
static ssize_t set_online_cpu_show(struct device *dev,
struct device_attribute *attr, char *buf)
{
unsigned int online_cpus;
online_cpus = *(unsigned int *)cpumask_bits(&cpuhp.sysfs_user.online_cpus);
return snprintf(buf, 30, "set online cpu : 0x%x\n", online_cpus);
}
static ssize_t set_online_cpu_store(struct device *dev,
struct device_attribute *attr, const char *buf, size_t count)
{
struct cpumask online_cpus;
char str[STR_LEN], re_str[STR_LEN];
unsigned int cpumask_value;
if (strlen(buf) >= STR_LEN)
return -EINVAL;
if (!sscanf(buf, "%5s", str))
return -EINVAL;
if (str[0] == '0' && toupper(str[1]) == 'X')
/* Move str pointer to remove "0x" */
cpumask_parse(str + 2, &online_cpus);
else {
if (!sscanf(str, "%d", &cpumask_value))
return -EINVAL;
snprintf(re_str, STR_LEN - 1, "%x", cpumask_value);
cpumask_parse(re_str, &online_cpus);
}
if (!cpumask_test_cpu(0, &online_cpus)) {
pr_warn("wrong format\n");
return -EINVAL;
}
cpumask_copy(&cpuhp.sysfs_user.online_cpus, &online_cpus);
cpuhp_do(false);
return count;
}
DEVICE_ATTR_RW(set_online_cpu);
/*
* It shows cpuhp driver requested online_cpu
*
* #cat /sys/power/cpuhp/online_cpu
*/
static ssize_t show_online_cpu(struct kobject *kobj,
struct kobj_attribute *attr, char *buf)
{
unsigned int online_cpus;
online_cpus = *(unsigned int *)cpumask_bits(&cpuhp.online_cpus);
return snprintf(buf, 30, "online cpu: 0x%x\n", online_cpus);
}
/*
* It shows users information(name, requesting cpu_mask, type)
* registered in cpu-hp user_list
*
* #cat /sys/power/cpuhp/users
*/
static ssize_t show_users(struct kobject *kobj,
struct kobj_attribute *attr, char *buf)
{
unsigned int online_cpus; \
struct cpuhp_user *user;
ssize_t ret = 0;
list_for_each_entry(user, &cpuhp.users, list) {
online_cpus = *(unsigned int *)cpumask_bits(&user->online_cpus);
ret += scnprintf(&buf[ret], 30, "%s: (0x%x)\n", user->name, online_cpus);
}
return ret;
}
/*
* User can control the cpu hotplug operation as below:
*
* #echo 1 > /sys/power/cpuhp/enabled => enable
* #echo 0 > /sys/power/cpuhp/enabled => disable
*
* If enabled become 0, hotplug driver enable the all cpus and no hotplug
* operation happen from hotplug driver.
*/
static ssize_t show_enable(struct kobject *kobj,
struct kobj_attribute *attr, char *buf)
{
return snprintf(buf, 10, "%d\n", cpuhp.enabled);
}
static ssize_t store_enable(struct kobject *kobj,
struct kobj_attribute *attr, const char *buf,
size_t count)
{
int input;
if (!sscanf(buf, "%d", &input))
return -EINVAL;
cpuhp_control(!!input);
return count;
}
/*
* User can control en/disable debug mode
*
* #echo 1 > /sys/power/cpuhp/debug => enable
* #echo 0 > /sys/power/cpuhp/debug => disable
*
* When it is enabled, information is printed every time there is a cpu control
*/
static ssize_t show_debug(struct kobject *kobj,
struct kobj_attribute *attr, char *buf)
{
return snprintf(buf, 10, "%d\n", cpuhp.debug);
}
static ssize_t store_debug(struct kobject *kobj,
struct kobj_attribute *attr, const char *buf,
size_t count)
{
int input;
if (!sscanf(buf, "%d", &input))
return -EINVAL;
cpuhp.debug = !!input;
return count;
}
static struct kobj_attribute cpuhp_enabled =
__ATTR(enabled, 0644, show_enable, store_enable);
static struct kobj_attribute cpuhp_debug =
__ATTR(debug, 0644, show_debug, store_debug);
static struct kobj_attribute cpuhp_online_cpu =
__ATTR(online_cpu, 0444, show_online_cpu, NULL);
static struct kobj_attribute cpuhp_users =
__ATTR(users, 0444, show_users, NULL);
static struct attribute *cpuhp_attrs[] = {
&cpuhp_online_cpu.attr,
&dev_attr_set_online_cpu.attr,
&cpuhp_enabled.attr,
&cpuhp_debug.attr,
&cpuhp_users.attr,
NULL,
};
static const struct attribute_group cpuhp_group = {
.attrs = cpuhp_attrs,
};
/**********************************************************************************/
/* PM_NOTI */
/**********************************************************************************/
static int exynos_cpuhp_pm_notifier(struct notifier_block *notifier,
unsigned long pm_event, void *v)
{
switch (pm_event) {
case PM_SUSPEND_PREPARE:
cpuhp_suspend(true);
break;
case PM_POST_SUSPEND:
cpuhp_suspend(false);
cpuhp_do(true);
break;
}
return NOTIFY_OK;
}
static struct notifier_block exynos_cpuhp_nb = {
.notifier_call = exynos_cpuhp_pm_notifier,
};
static void __init cpuhp_dt_init(void)
{
struct device_node *np = of_find_node_by_name(NULL, "cpuhp");
const char *buf;
if (of_property_read_string(np, "fast_hp_cpus", &buf)) {
pr_info("fast_hp_cpus property is omitted!\n");
return;
}
cpulist_parse(buf, &cpuhp.fast_hp_cpus);
return;
}
/**********************************************************************************/
/* INIT */
/**********************************************************************************/
extern struct cpumask early_cpu_mask;
static void __init cpuhp_user_init(void)
{
struct cpumask mask;
/* init user list */
INIT_LIST_HEAD(&cpuhp.users);
cpumask_copy(&mask, cpu_possible_mask);
cpumask_and(&mask, &mask, &early_cpu_mask);
/* register user for SYSFS */
cpumask_copy(&cpuhp.system_user.online_cpus, &mask);
strcpy(cpuhp.system_user.name, "SYSTEM");
cpuhp.system_user.type = 0;
list_add(&cpuhp.system_user.list, &cpuhp.users);
/* register user for SYSTEM */
cpumask_copy(&cpuhp.sysfs_user.online_cpus, &mask);
strcpy(cpuhp.sysfs_user.name, "SYSFS");
cpuhp.sysfs_user.type = 0;
list_add(&cpuhp.sysfs_user.list, &cpuhp.users);
cpumask_copy(&cpuhp.online_cpus, cpu_online_mask);
}
static void __init cpuhp_sysfs_init(void)
{
cpuhp.kobj = kobject_create_and_add("cpuhp", power_kobj);
if (!cpuhp.kobj) {
pr_err("Fail to create cpuhp kboject\n");
return;
}
/* Create /sys/power/cpuhotplug */
if (sysfs_create_group(cpuhp.kobj, &cpuhp_group)) {
pr_err("Fail to create cpuhp group\n");
return;
}
/* link cpuhotplug directory to /sys/devices/system/cpu/cpuhp */
if (sysfs_create_link(&cpu_subsys.dev_root->kobj, cpuhp.kobj, "cpuhp"))
pr_err("Fail to link cpuctrl directory");
}
static int __init cpuhp_init(void)
{
/* Parse data from device tree */
cpuhp_dt_init();
/* Initialize pm_qos request and handler */
cpuhp_user_init();
/* Create sysfs */
cpuhp_sysfs_init();
/* register pm notifier */
register_pm_notifier(&exynos_cpuhp_nb);
/* Enable cpuhp */
cpuhp_enable(true);
return 0;
}
arch_initcall(cpuhp_init);