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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
|
/*
* Copyright (C) 2015 Samsung Electronics.
*
* This software is licensed under the terms of the GNU General Public
* License version 2, as published by the Free Software Foundation, and
* may be copied, distributed, and modified under those terms.
*
* 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.
*
*/
#include <linux/module.h>
#include <linux/notifier.h>
#include "modem_prj.h"
#define MIF_MAX_RPS_STR 8
#define MIF_MAX_NDEV_NUM 8
#define MIF_ARGOS_IPC_LABEL "IPC"
#define MIF_ARGOS_CLAT_LABEL "CLAT"
//#define MIF_ARGOS_DEBUG
const char *ndev_prefix[2] = {"rmnet", "v4-rmnet"};
enum ndev_prefix_idx {
IPC = 0,
CLAT
};
struct argos_notifier {
struct notifier_block ipc_nb;
struct notifier_block clat_nb;
unsigned int prev_ipc_rps;
unsigned int prev_clat_rps;
};
unsigned int lit_rmnet_rps = 0x06;
module_param(lit_rmnet_rps, uint, S_IRUGO | S_IWUSR | S_IWGRP);
MODULE_PARM_DESC(lit_rmnet_rps, "rps_cpus for rmnetx: LITTLE(only rmnetx up)");
unsigned int lit_rmnet_clat_rps = 0x04;
module_param(lit_rmnet_clat_rps, uint, S_IRUGO | S_IWUSR | S_IWGRP);
MODULE_PARM_DESC(lit_rmnet_clat_rps, "rps_cpus for rmnetx: LITTLE(both up)");
unsigned int lit_clat_rps = 0x02;
module_param(lit_clat_rps, uint, S_IRUGO | S_IWUSR | S_IWGRP);
MODULE_PARM_DESC(lit_clat_rps, "rps_cpus for v4-rmnetx: LITTLE(both up)");
unsigned int big_rmnet_rps = 0x60;
module_param(big_rmnet_rps, uint, S_IRUGO | S_IWUSR | S_IWGRP);
MODULE_PARM_DESC(big_rmnet_rps, "rps_cpus for rmnetx: BIG(only rmnetx up)");
unsigned int big_rmnet_clat_rps = 0x40;
module_param(big_rmnet_clat_rps, uint, S_IRUGO | S_IWUSR | S_IWGRP);
MODULE_PARM_DESC(big_rmnet_clat_rps, "rps_cpus for rmnetx: BIG(both up)");
unsigned int big_clat_rps = 0x20;
module_param(big_clat_rps, uint, S_IRUGO | S_IWUSR | S_IWGRP);
MODULE_PARM_DESC(big_clat_rps, "rps_cpus for v4-rmnetx: BIG(both up)");
unsigned int mif_rps_thresh = 200;
module_param(mif_rps_thresh, uint, S_IRUGO | S_IWUSR | S_IWGRP);
MODULE_PARM_DESC(mif_rps_thresh, "threshold speed");
static int mif_store_rps_map(struct netdev_rx_queue *queue, char *buf, size_t len)
{
struct rps_map *old_map, *map;
cpumask_var_t mask;
int err, cpu, i;
static DEFINE_SPINLOCK(rps_map_lock);
if (!alloc_cpumask_var(&mask, GFP_KERNEL)) {
mif_err("failed to alloc_cpumask\n");
return -ENOMEM;
}
err = bitmap_parse(buf, len, cpumask_bits(mask), nr_cpumask_bits);
if (err) {
free_cpumask_var(mask);
mif_err("failed to parse bitmap\n");
return err;
}
map = kzalloc(max_t(unsigned int,
RPS_MAP_SIZE(cpumask_weight(mask)), L1_CACHE_BYTES),
GFP_KERNEL);
if (!map) {
free_cpumask_var(mask);
mif_err("failed to alloc kmem\n");
return -ENOMEM;
}
i = 0;
for_each_cpu_and(cpu, mask, cpu_online_mask) {
map->cpus[i++] = cpu;
}
if (i) {
map->len = i;
} else {
kfree(map);
map = NULL;
free_cpumask_var(mask);
mif_err("failed to map rps_cpu\n");
return -EINVAL;
}
spin_lock(&rps_map_lock);
old_map = rcu_dereference_protected(queue->rps_map,
lockdep_is_held(&rps_map_lock));
rcu_assign_pointer(queue->rps_map, map);
if (map)
static_key_slow_inc(&rps_needed);
if (old_map)
static_key_slow_dec(&rps_needed);
spin_unlock(&rps_map_lock);
if (old_map)
kfree_rcu(old_map, rcu);
free_cpumask_var(mask);
#ifdef MIF_ARGOS_DEBUG
mif_info("map:%d\n", map->len);
#endif
return map->len;
}
static void mif_update_ndevs_rps(const char *ndev_prefix, unsigned int rps_vaule)
{
struct net_device *ndev;
char ndev_name[IFNAMSIZ];
char value[MIF_MAX_RPS_STR];
int ret;
int i;
if (!rps_vaule)
return;
snprintf(value, MIF_MAX_RPS_STR, "%x", rps_vaule);
for (i = 0; i < MIF_MAX_NDEV_NUM; i++) {
memset(ndev_name, 0, IFNAMSIZ);
snprintf(ndev_name, IFNAMSIZ, "%s%d", ndev_prefix, i);
ndev = dev_get_by_name(&init_net, ndev_name);
if (!ndev) {
#ifdef MIF_ARGOS_DEBUG
mif_info("Cannot find %s from init_net\n", ndev_name);
#endif
continue;
}
ret = mif_store_rps_map(ndev->_rx, value, strlen(value));
dev_put(ndev);
if (ret < 0) {
mif_err("failed to update ndevs_rps (%s)\n", ndev_name);
continue;
}
#ifdef MIF_ARGOS_DEBUG
mif_info("%s's rps: 0x%s\n", ndev_name, value);
#endif
}
}
static int mif_argos_notifier_ipc(struct notifier_block *nb, unsigned long speed, void *data)
{
struct argos_notifier *nf = container_of(nb, struct argos_notifier, ipc_nb);
unsigned int prev_ipc_rps = nf->prev_ipc_rps, new_ipc_rps = 0;
#ifdef MIF_ARGOS_DEBUG
mif_info("Speed: %luMbps, IPC|CLAT: 0x%02x|0x%02x\n",
speed, prev_ipc_rps, nf->prev_clat_rps);
#endif
if (speed >= mif_rps_thresh)
new_ipc_rps = nf->prev_clat_rps ? big_rmnet_clat_rps : big_rmnet_rps;
else
new_ipc_rps = nf->prev_clat_rps ? lit_rmnet_clat_rps : lit_rmnet_rps;
if (prev_ipc_rps != new_ipc_rps) {
mif_update_ndevs_rps(ndev_prefix[IPC], new_ipc_rps);
nf->prev_ipc_rps = new_ipc_rps;
mif_info("Speed: %luMbps, IPC|CLAT: 0x%02x|0x%02x -> 0x%02x|0x%02x\n",
speed, prev_ipc_rps, nf->prev_clat_rps, new_ipc_rps, nf->prev_clat_rps);
}
return NOTIFY_OK;
}
static int mif_argos_notifier_clat(struct notifier_block *nb, unsigned long speed, void *data)
{
struct argos_notifier *nf = container_of(nb, struct argos_notifier, clat_nb);
unsigned int prev_clat_rps = nf->prev_clat_rps, new_clat_rps = 0;
unsigned int prev_ipc_rps = nf->prev_ipc_rps, new_ipc_rps = 0;
bool changed = false;
#ifdef MIF_ARGOS_DEBUG
mif_info("Speed: %luMbps, IPC|CLAT: 0x%02x|0x%02x\n",
speed, prev_ipc_rps, nf->prev_clat_rps);
#endif
/* If already set to Big core */
if (nf->prev_ipc_rps > 0xF) {
new_clat_rps = (speed > 0) ? big_clat_rps : 0;
new_ipc_rps = (new_clat_rps > 0) ? big_rmnet_clat_rps : big_rmnet_rps;
} else {
new_clat_rps = (speed > 0) ? lit_clat_rps : 0;
new_ipc_rps = (new_clat_rps > 0) ? lit_rmnet_clat_rps : lit_rmnet_rps;
}
if (prev_clat_rps != new_clat_rps) {
mif_update_ndevs_rps(ndev_prefix[CLAT], new_clat_rps);
nf->prev_clat_rps = new_clat_rps;
changed = true;
}
if (prev_ipc_rps != new_ipc_rps) {
mif_update_ndevs_rps(ndev_prefix[IPC], new_ipc_rps);
nf->prev_ipc_rps = new_ipc_rps;
changed = true;
}
if (changed) {
mif_info("Speed: %luMbps, IPC|CLAT: 0x%02x|0x%02x -> 0x%02x|0x%02x\n",
speed, prev_ipc_rps, prev_clat_rps, new_ipc_rps, new_clat_rps);
}
return NOTIFY_OK;
}
int mif_init_argos_notifier(void)
{
struct argos_notifier *argos_nf;
int ret;
mif_info("++\n");
argos_nf = kzalloc(sizeof(struct argos_notifier), GFP_ATOMIC);
if (!argos_nf) {
mif_err("failed to allocate argos_nf\n");
return -ENOMEM;
}
argos_nf->ipc_nb.notifier_call = mif_argos_notifier_ipc;
ret = sec_argos_register_notifier(&argos_nf->ipc_nb, MIF_ARGOS_IPC_LABEL);
if (ret < 0) {
mif_err("failed to register ipc_nb(%d)\n", ret);
goto exit;
}
argos_nf->clat_nb.notifier_call = mif_argos_notifier_clat;
ret = sec_argos_register_notifier(&argos_nf->clat_nb, MIF_ARGOS_CLAT_LABEL);
if (ret < 0) {
mif_err("failed to register clat_nb(%d)\n", ret);
sec_argos_unregister_notifier(&argos_nf->ipc_nb, MIF_ARGOS_IPC_LABEL);
goto exit;
}
/* default rmnetx rps: 0x06 */
mif_update_ndevs_rps(ndev_prefix[IPC], lit_rmnet_rps);
mif_info("--\n");
return 0;
exit:
kfree(argos_nf);
return ret;
}
|