summaryrefslogtreecommitdiff
path: root/drivers/rmnet/shs/rmnet_shs_freq.c
blob: c6123c682fb530d3b9f5df62bf081afaeacdb8bc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/* Copyright (c) 2019 The Linux Foundation. All rights reserved.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 and
 * only 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.
 *
 * RMNET Data Smart Hash stamping solution
 *
 */
#include <linux/module.h>
#include "rmnet_shs.h"
#include "rmnet_shs_freq.h"

#include <linux/cpufreq.h>
#include <linux/cpu.h>

#define MAX_FREQ INT_MAX
#define MIN_FREQ 0
#define BOOST_FREQ MAX_FREQ

struct cpu_freq {
	unsigned int freq_floor;
	unsigned int freq_ceil;

};

unsigned int rmnet_shs_freq_enable __read_mostly = 1;
module_param(rmnet_shs_freq_enable, uint, 0644);
MODULE_PARM_DESC(rmnet_shs_freq_enable, "Enable/disable freq boost feature");

struct workqueue_struct *shs_boost_wq;
static DEFINE_PER_CPU(struct cpu_freq, cpu_boosts);
static struct work_struct boost_cpu;

static int rmnet_shs_freq_notify(struct notifier_block *nb,
				 unsigned long val,
				 void *data)
{
	struct cpufreq_policy *policy = data;
	unsigned int cpu = policy->cpu;
	struct cpu_freq *boost = &per_cpu(cpu_boosts, cpu);

	switch (val) {
	case CPUFREQ_ADJUST:
		if (rmnet_shs_freq_enable) {
			cpufreq_verify_within_limits(policy,
						     boost->freq_floor,
						     MAX_FREQ);
			trace_rmnet_freq_update(cpu, policy->min,
						policy->max);
		}
		break;
	}

	return NOTIFY_OK;
}

static struct notifier_block freq_boost_nb = {
	.notifier_call = rmnet_shs_freq_notify,
};

static void update_cpu_policy(struct work_struct *work)
{
	unsigned int i;

	get_online_cpus();
	for_each_online_cpu(i) {
		cpufreq_update_policy(i);
	}

	put_online_cpus();
}

void rmnet_shs_reset_freq(void)
{
	struct cpu_freq *boost;
	int i;

	for_each_possible_cpu(i) {
		boost = &per_cpu(cpu_boosts, i);
		boost->freq_floor = MIN_FREQ;
		boost->freq_ceil = MAX_FREQ;
	}
}

void rmnet_shs_boost_cpus(void)
{
	struct cpu_freq *boost;
	int i;

	for_each_possible_cpu(i) {

		if ((1 << i) & PERF_MASK)
			continue;
		boost = &per_cpu(cpu_boosts, i);
		boost->freq_floor = BOOST_FREQ;
		boost->freq_ceil = MAX_FREQ;
		trace_rmnet_freq_boost(i, boost->freq_floor);
	}

	if (work_pending(&boost_cpu))
		return;

	if (shs_boost_wq)
		queue_work(shs_boost_wq, &boost_cpu);
}

void rmnet_shs_reset_cpus(void)
{
	struct cpu_freq *boost;
	int i;

	for_each_possible_cpu(i) {

		if ((1 << i) & PERF_MASK)
			continue;
		boost = &per_cpu(cpu_boosts, i);
		boost->freq_floor = MIN_FREQ;
		boost->freq_ceil = MAX_FREQ;
		trace_rmnet_freq_reset(i, boost->freq_floor);
	}
	if (work_pending(&boost_cpu))
		return;

	if (shs_boost_wq)
		queue_work(shs_boost_wq, &boost_cpu);
}

int rmnet_shs_freq_init(void)
{

	if (!shs_boost_wq)
		shs_boost_wq = alloc_workqueue("shs_boost_wq", WQ_HIGHPRI, 0);

	if (!shs_boost_wq)
		return -EFAULT;
	INIT_WORK(&boost_cpu, update_cpu_policy);

	if (rmnet_shs_freq_enable)
		cpufreq_register_notifier(&freq_boost_nb,
					  CPUFREQ_POLICY_NOTIFIER);
	rmnet_shs_reset_freq();
	return 0;
}

int rmnet_shs_freq_exit(void)
{
	rmnet_shs_reset_freq();
	cancel_work_sync(&boost_cpu);

	if (shs_boost_wq) {
		destroy_workqueue(shs_boost_wq);
		shs_boost_wq = NULL;
	}

	if (rmnet_shs_freq_enable)
		cpufreq_unregister_notifier(&freq_boost_nb,
					    CPUFREQ_POLICY_NOTIFIER);
	return 0;
}