brintos

brintos / linux-shallow public Read only

0
0
Text · 7.1 KiB · 6b6f51b Raw
318 lines · c
1// SPDX-License-Identifier: GPL-2.0-only2/*3 * Copyright 2020 Linaro Limited4 *5 * Author: Daniel Lezcano <daniel.lezcano@linaro.org>6 *7 * The DTPM CPU is based on the energy model. It hooks the CPU in the8 * DTPM tree which in turns update the power number by propagating the9 * power number from the CPU energy model information to the parents.10 *11 * The association between the power and the performance state, allows12 * to set the power of the CPU at the OPP granularity.13 *14 * The CPU hotplug is supported and the power numbers will be updated15 * if a CPU is hot plugged / unplugged.16 */17#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt18 19#include <linux/cpumask.h>20#include <linux/cpufreq.h>21#include <linux/cpuhotplug.h>22#include <linux/dtpm.h>23#include <linux/energy_model.h>24#include <linux/of.h>25#include <linux/pm_qos.h>26#include <linux/slab.h>27 28struct dtpm_cpu {29	struct dtpm dtpm;30	struct freq_qos_request qos_req;31	int cpu;32};33 34static DEFINE_PER_CPU(struct dtpm_cpu *, dtpm_per_cpu);35 36static struct dtpm_cpu *to_dtpm_cpu(struct dtpm *dtpm)37{38	return container_of(dtpm, struct dtpm_cpu, dtpm);39}40 41static u64 set_pd_power_limit(struct dtpm *dtpm, u64 power_limit)42{43	struct dtpm_cpu *dtpm_cpu = to_dtpm_cpu(dtpm);44	struct em_perf_domain *pd = em_cpu_get(dtpm_cpu->cpu);45	struct em_perf_state *table;46	unsigned long freq;47	u64 power;48	int i, nr_cpus;49 50	nr_cpus = cpumask_weight_and(cpu_online_mask, to_cpumask(pd->cpus));51 52	rcu_read_lock();53	table = em_perf_state_from_pd(pd);54	for (i = 0; i < pd->nr_perf_states; i++) {55 56		power = table[i].power * nr_cpus;57 58		if (power > power_limit)59			break;60	}61 62	freq = table[i - 1].frequency;63	power_limit = table[i - 1].power * nr_cpus;64	rcu_read_unlock();65 66	freq_qos_update_request(&dtpm_cpu->qos_req, freq);67 68	return power_limit;69}70 71static u64 scale_pd_power_uw(struct cpumask *pd_mask, u64 power)72{73	unsigned long max, sum_util = 0;74	int cpu;75 76	/*77	 * The capacity is the same for all CPUs belonging to78	 * the same perf domain.79	 */80	max = arch_scale_cpu_capacity(cpumask_first(pd_mask));81 82	for_each_cpu_and(cpu, pd_mask, cpu_online_mask)83		sum_util += sched_cpu_util(cpu);84 85	return (power * ((sum_util << 10) / max)) >> 10;86}87 88static u64 get_pd_power_uw(struct dtpm *dtpm)89{90	struct dtpm_cpu *dtpm_cpu = to_dtpm_cpu(dtpm);91	struct em_perf_state *table;92	struct em_perf_domain *pd;93	struct cpumask *pd_mask;94	unsigned long freq;95	u64 power = 0;96	int i;97 98	pd = em_cpu_get(dtpm_cpu->cpu);99 100	pd_mask = em_span_cpus(pd);101 102	freq = cpufreq_quick_get(dtpm_cpu->cpu);103 104	rcu_read_lock();105	table = em_perf_state_from_pd(pd);106	for (i = 0; i < pd->nr_perf_states; i++) {107 108		if (table[i].frequency < freq)109			continue;110 111		power = scale_pd_power_uw(pd_mask, table[i].power);112		break;113	}114	rcu_read_unlock();115 116	return power;117}118 119static int update_pd_power_uw(struct dtpm *dtpm)120{121	struct dtpm_cpu *dtpm_cpu = to_dtpm_cpu(dtpm);122	struct em_perf_domain *em = em_cpu_get(dtpm_cpu->cpu);123	struct em_perf_state *table;124	int nr_cpus;125 126	nr_cpus = cpumask_weight_and(cpu_online_mask, to_cpumask(em->cpus));127 128	rcu_read_lock();129	table = em_perf_state_from_pd(em);130 131	dtpm->power_min = table[0].power;132	dtpm->power_min *= nr_cpus;133 134	dtpm->power_max = table[em->nr_perf_states - 1].power;135	dtpm->power_max *= nr_cpus;136 137	rcu_read_unlock();138 139	return 0;140}141 142static void pd_release(struct dtpm *dtpm)143{144	struct dtpm_cpu *dtpm_cpu = to_dtpm_cpu(dtpm);145	struct cpufreq_policy *policy;146 147	if (freq_qos_request_active(&dtpm_cpu->qos_req))148		freq_qos_remove_request(&dtpm_cpu->qos_req);149 150	policy = cpufreq_cpu_get(dtpm_cpu->cpu);151	if (policy) {152		for_each_cpu(dtpm_cpu->cpu, policy->related_cpus)153			per_cpu(dtpm_per_cpu, dtpm_cpu->cpu) = NULL;154 155		cpufreq_cpu_put(policy);156	}157 158	kfree(dtpm_cpu);159}160 161static struct dtpm_ops dtpm_ops = {162	.set_power_uw	 = set_pd_power_limit,163	.get_power_uw	 = get_pd_power_uw,164	.update_power_uw = update_pd_power_uw,165	.release	 = pd_release,166};167 168static int cpuhp_dtpm_cpu_offline(unsigned int cpu)169{170	struct dtpm_cpu *dtpm_cpu;171 172	dtpm_cpu = per_cpu(dtpm_per_cpu, cpu);173	if (dtpm_cpu)174		dtpm_update_power(&dtpm_cpu->dtpm);175 176	return 0;177}178 179static int cpuhp_dtpm_cpu_online(unsigned int cpu)180{181	struct dtpm_cpu *dtpm_cpu;182 183	dtpm_cpu = per_cpu(dtpm_per_cpu, cpu);184	if (dtpm_cpu)185		return dtpm_update_power(&dtpm_cpu->dtpm);186 187	return 0;188}189 190static int __dtpm_cpu_setup(int cpu, struct dtpm *parent)191{192	struct dtpm_cpu *dtpm_cpu;193	struct cpufreq_policy *policy;194	struct em_perf_state *table;195	struct em_perf_domain *pd;196	char name[CPUFREQ_NAME_LEN];197	int ret = -ENOMEM;198 199	dtpm_cpu = per_cpu(dtpm_per_cpu, cpu);200	if (dtpm_cpu)201		return 0;202 203	policy = cpufreq_cpu_get(cpu);204	if (!policy)205		return 0;206 207	pd = em_cpu_get(cpu);208	if (!pd || em_is_artificial(pd)) {209		ret = -EINVAL;210		goto release_policy;211	}212 213	dtpm_cpu = kzalloc(sizeof(*dtpm_cpu), GFP_KERNEL);214	if (!dtpm_cpu) {215		ret = -ENOMEM;216		goto release_policy;217	}218 219	dtpm_init(&dtpm_cpu->dtpm, &dtpm_ops);220	dtpm_cpu->cpu = cpu;221 222	for_each_cpu(cpu, policy->related_cpus)223		per_cpu(dtpm_per_cpu, cpu) = dtpm_cpu;224 225	snprintf(name, sizeof(name), "cpu%d-cpufreq", dtpm_cpu->cpu);226 227	ret = dtpm_register(name, &dtpm_cpu->dtpm, parent);228	if (ret)229		goto out_kfree_dtpm_cpu;230 231	rcu_read_lock();232	table = em_perf_state_from_pd(pd);233	ret = freq_qos_add_request(&policy->constraints,234				   &dtpm_cpu->qos_req, FREQ_QOS_MAX,235				   table[pd->nr_perf_states - 1].frequency);236	rcu_read_unlock();237	if (ret < 0)238		goto out_dtpm_unregister;239 240	cpufreq_cpu_put(policy);241	return 0;242 243out_dtpm_unregister:244	dtpm_unregister(&dtpm_cpu->dtpm);245	dtpm_cpu = NULL;246 247out_kfree_dtpm_cpu:248	for_each_cpu(cpu, policy->related_cpus)249		per_cpu(dtpm_per_cpu, cpu) = NULL;250	kfree(dtpm_cpu);251 252release_policy:253	cpufreq_cpu_put(policy);254	return ret;255}256 257static int dtpm_cpu_setup(struct dtpm *dtpm, struct device_node *np)258{259	int cpu;260 261	cpu = of_cpu_node_to_id(np);262	if (cpu < 0)263		return 0;264 265	return __dtpm_cpu_setup(cpu, dtpm);266}267 268static int dtpm_cpu_init(void)269{270	int ret;271 272	/*273	 * The callbacks at CPU hotplug time are calling274	 * dtpm_update_power() which in turns calls update_pd_power().275	 *276	 * The function update_pd_power() uses the online mask to277	 * figure out the power consumption limits.278	 *279	 * At CPUHP_AP_ONLINE_DYN, the CPU is present in the CPU280	 * online mask when the cpuhp_dtpm_cpu_online function is281	 * called, but the CPU is still in the online mask for the282	 * tear down callback. So the power can not be updated when283	 * the CPU is unplugged.284	 *285	 * At CPUHP_AP_DTPM_CPU_DEAD, the situation is the opposite as286	 * above. The CPU online mask is not up to date when the CPU287	 * is plugged in.288	 *289	 * For this reason, we need to call the online and offline290	 * callbacks at different moments when the CPU online mask is291	 * consistent with the power numbers we want to update.292	 */293	ret = cpuhp_setup_state(CPUHP_AP_DTPM_CPU_DEAD, "dtpm_cpu:offline",294				NULL, cpuhp_dtpm_cpu_offline);295	if (ret < 0)296		return ret;297 298	ret = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "dtpm_cpu:online",299				cpuhp_dtpm_cpu_online, NULL);300	if (ret < 0)301		return ret;302 303	return 0;304}305 306static void dtpm_cpu_exit(void)307{308	cpuhp_remove_state_nocalls(CPUHP_AP_ONLINE_DYN);309	cpuhp_remove_state_nocalls(CPUHP_AP_DTPM_CPU_DEAD);310}311 312struct dtpm_subsys_ops dtpm_cpu_ops = {313	.name = KBUILD_MODNAME,314	.init = dtpm_cpu_init,315	.exit = dtpm_cpu_exit,316	.setup = dtpm_cpu_setup,317};318