Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * Energy Model of devices
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  * Copyright (c) 2018-2020, Arm ltd.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  * Written by: Quentin Perret, Arm ltd.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  * Improvements provided by: Lukasz Luba, Arm ltd.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #define pr_fmt(fmt) "energy_model: " fmt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <linux/cpu.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <linux/cpumask.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <linux/debugfs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include <linux/energy_model.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include <linux/sched/topology.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20)  * Mutex serializing the registrations of performance domains and letting
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21)  * callbacks defined by drivers sleep.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) static DEFINE_MUTEX(em_pd_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) static bool _is_cpu_device(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) 	return (dev->bus == &cpu_subsys);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) #ifdef CONFIG_DEBUG_FS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) static struct dentry *rootdir;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) static void em_debug_create_ps(struct em_perf_state *ps, struct dentry *pd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 	struct dentry *d;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 	char name[24];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 	snprintf(name, sizeof(name), "ps:%lu", ps->frequency);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 	/* Create per-ps directory */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 	d = debugfs_create_dir(name, pd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 	debugfs_create_ulong("frequency", 0444, d, &ps->frequency);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 	debugfs_create_ulong("power", 0444, d, &ps->power);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 	debugfs_create_ulong("cost", 0444, d, &ps->cost);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) static int em_debug_cpus_show(struct seq_file *s, void *unused)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 	seq_printf(s, "%*pbl\n", cpumask_pr_args(to_cpumask(s->private)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) DEFINE_SHOW_ATTRIBUTE(em_debug_cpus);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) static int em_debug_units_show(struct seq_file *s, void *unused)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 	struct em_perf_domain *pd = s->private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 	char *units = pd->milliwatts ? "milliWatts" : "bogoWatts";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 	seq_printf(s, "%s\n", units);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) DEFINE_SHOW_ATTRIBUTE(em_debug_units);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) static void em_debug_create_pd(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 	struct dentry *d;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 	/* Create the directory of the performance domain */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 	d = debugfs_create_dir(dev_name(dev), rootdir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 	if (_is_cpu_device(dev))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 		debugfs_create_file("cpus", 0444, d, dev->em_pd->cpus,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 				    &em_debug_cpus_fops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 	debugfs_create_file("units", 0444, d, dev->em_pd, &em_debug_units_fops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 	/* Create a sub-directory for each performance state */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 	for (i = 0; i < dev->em_pd->nr_perf_states; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 		em_debug_create_ps(&dev->em_pd->table[i], d);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) static void em_debug_remove_pd(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 	struct dentry *debug_dir;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 	debug_dir = debugfs_lookup(dev_name(dev), rootdir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 	debugfs_remove_recursive(debug_dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) static int __init em_debug_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 	/* Create /sys/kernel/debug/energy_model directory */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 	rootdir = debugfs_create_dir("energy_model", NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) fs_initcall(em_debug_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) #else /* CONFIG_DEBUG_FS */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) static void em_debug_create_pd(struct device *dev) {}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) static void em_debug_remove_pd(struct device *dev) {}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) static int em_create_perf_table(struct device *dev, struct em_perf_domain *pd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 				int nr_states, struct em_data_callback *cb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 	unsigned long power, freq, prev_freq = 0, prev_cost = ULONG_MAX;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 	struct em_perf_state *table;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 	int i, ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 	u64 fmax;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 	table = kcalloc(nr_states, sizeof(*table), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 	if (!table)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 	/* Build the list of performance states for this performance domain */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 	for (i = 0, freq = 0; i < nr_states; i++, freq++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 		 * active_power() is a driver callback which ceils 'freq' to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 		 * lowest performance state of 'dev' above 'freq' and updates
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 		 * 'power' and 'freq' accordingly.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 		ret = cb->active_power(&power, &freq, dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 		if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 			dev_err(dev, "EM: invalid perf. state: %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 				ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 			goto free_ps_table;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 		 * We expect the driver callback to increase the frequency for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 		 * higher performance states.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 		if (freq <= prev_freq) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 			dev_err(dev, "EM: non-increasing freq: %lu\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 				freq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 			goto free_ps_table;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 		 * The power returned by active_state() is expected to be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 		 * positive, in milli-watts and to fit into 16 bits.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 		if (!power || power > EM_MAX_POWER) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 			dev_err(dev, "EM: invalid power: %lu\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 				power);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 			goto free_ps_table;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 		table[i].power = power;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 		table[i].frequency = prev_freq = freq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 	/* Compute the cost of each performance state. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 	fmax = (u64) table[nr_states - 1].frequency;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 	for (i = nr_states - 1; i >= 0; i--) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 		unsigned long power_res = em_scale_power(table[i].power);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 		table[i].cost = div64_u64(fmax * power_res,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 					  table[i].frequency);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 		if (table[i].cost >= prev_cost) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 			dev_dbg(dev, "EM: OPP:%lu is inefficient\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 				table[i].frequency);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 			prev_cost = table[i].cost;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 	pd->table = table;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 	pd->nr_perf_states = nr_states;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) free_ps_table:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 	kfree(table);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 	return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) static int em_create_pd(struct device *dev, int nr_states,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 			struct em_data_callback *cb, cpumask_t *cpus)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 	struct em_perf_domain *pd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 	struct device *cpu_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 	int cpu, ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 	if (_is_cpu_device(dev)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 		pd = kzalloc(sizeof(*pd) + cpumask_size(), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 		if (!pd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 			return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 		cpumask_copy(em_span_cpus(pd), cpus);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 		pd = kzalloc(sizeof(*pd), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 		if (!pd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 			return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 	ret = em_create_perf_table(dev, pd, nr_states, cb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 		kfree(pd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 	if (_is_cpu_device(dev))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 		for_each_cpu(cpu, cpus) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 			cpu_dev = get_cpu_device(cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 			cpu_dev->em_pd = pd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 	dev->em_pd = pd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219)  * em_pd_get() - Return the performance domain for a device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220)  * @dev : Device to find the performance domain for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222)  * Returns the performance domain to which @dev belongs, or NULL if it doesn't
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223)  * exist.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) struct em_perf_domain *em_pd_get(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 	if (IS_ERR_OR_NULL(dev))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 	return dev->em_pd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) EXPORT_SYMBOL_GPL(em_pd_get);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235)  * em_cpu_get() - Return the performance domain for a CPU
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236)  * @cpu : CPU to find the performance domain for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238)  * Returns the performance domain to which @cpu belongs, or NULL if it doesn't
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239)  * exist.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) struct em_perf_domain *em_cpu_get(int cpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 	struct device *cpu_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 	cpu_dev = get_cpu_device(cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 	if (!cpu_dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 	return em_pd_get(cpu_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) EXPORT_SYMBOL_GPL(em_cpu_get);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254)  * em_dev_register_perf_domain() - Register the Energy Model (EM) for a device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255)  * @dev		: Device for which the EM is to register
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256)  * @nr_states	: Number of performance states to register
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257)  * @cb		: Callback functions providing the data of the Energy Model
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258)  * @cpus	: Pointer to cpumask_t, which in case of a CPU device is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259)  *		obligatory. It can be taken from i.e. 'policy->cpus'. For other
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260)  *		type of devices this should be set to NULL.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261)  * @milliwatts	: Flag indicating that the power values are in milliWatts or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262)  *		in some other scale. It must be set properly.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264)  * Create Energy Model tables for a performance domain using the callbacks
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265)  * defined in cb.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267)  * The @milliwatts is important to set with correct value. Some kernel
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268)  * sub-systems might rely on this flag and check if all devices in the EM are
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269)  * using the same scale.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271)  * If multiple clients register the same performance domain, all but the first
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272)  * registration will be ignored.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274)  * Return 0 on success
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) int em_dev_register_perf_domain(struct device *dev, unsigned int nr_states,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 				struct em_data_callback *cb, cpumask_t *cpus,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 				bool milliwatts)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 	unsigned long cap, prev_cap = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 	int cpu, ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 	if (!dev || !nr_states || !cb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 	 * Use a mutex to serialize the registration of performance domains and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 	 * let the driver-defined callback functions sleep.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 	mutex_lock(&em_pd_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 	if (dev->em_pd) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 		ret = -EEXIST;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 		goto unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 	if (_is_cpu_device(dev)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 		if (!cpus) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 			dev_err(dev, "EM: invalid CPU mask\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 			ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 			goto unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 		for_each_cpu(cpu, cpus) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 			if (em_cpu_get(cpu)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 				dev_err(dev, "EM: exists for CPU%d\n", cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 				ret = -EEXIST;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 				goto unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 			 * All CPUs of a domain must have the same
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 			 * micro-architecture since they all share the same
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 			 * table.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 			cap = arch_scale_cpu_capacity(cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 			if (prev_cap && prev_cap != cap) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 				dev_err(dev, "EM: CPUs of %*pbl must have the same capacity\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 					cpumask_pr_args(cpus));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 				ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 				goto unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 			prev_cap = cap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 	ret = em_create_pd(dev, nr_states, cb, cpus);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 		goto unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 	dev->em_pd->milliwatts = milliwatts;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 	em_debug_create_pd(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) 	dev_info(dev, "EM: created perf domain\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) unlock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) 	mutex_unlock(&em_pd_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) EXPORT_SYMBOL_GPL(em_dev_register_perf_domain);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343)  * em_dev_unregister_perf_domain() - Unregister Energy Model (EM) for a device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344)  * @dev		: Device for which the EM is registered
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346)  * Unregister the EM for the specified @dev (but not a CPU device).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) void em_dev_unregister_perf_domain(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) 	if (IS_ERR_OR_NULL(dev) || !dev->em_pd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) 	if (_is_cpu_device(dev))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) 	 * The mutex separates all register/unregister requests and protects
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) 	 * from potential clean-up/setup issues in the debugfs directories.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) 	 * The debugfs directory name is the same as device's name.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) 	mutex_lock(&em_pd_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) 	em_debug_remove_pd(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) 	kfree(dev->em_pd->table);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) 	kfree(dev->em_pd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) 	dev->em_pd = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) 	mutex_unlock(&em_pd_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) EXPORT_SYMBOL_GPL(em_dev_unregister_perf_domain);