Pavankumar Kondeti | 83236e3 | 2017-02-10 09:58:58 +0530 | [diff] [blame] | 1 | /* Copyright (c) 2014-2017, The Linux Foundation. All rights reserved. |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 2 | * |
| 3 | * This program is free software; you can redistribute it and/or modify |
| 4 | * it under the terms of the GNU General Public License version 2 and |
| 5 | * only version 2 as published by the Free Software Foundation. |
| 6 | * |
| 7 | * This program is distributed in the hope that it will be useful, |
| 8 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 9 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 10 | * GNU General Public License for more details. |
| 11 | */ |
| 12 | |
Pavankumar Kondeti | 088f334 | 2017-03-17 15:06:47 +0530 | [diff] [blame] | 13 | #define pr_fmt(fmt) "core_ctl: " fmt |
| 14 | |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 15 | #include <linux/init.h> |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 16 | #include <linux/cpu.h> |
| 17 | #include <linux/cpumask.h> |
| 18 | #include <linux/cpufreq.h> |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 19 | #include <linux/kthread.h> |
| 20 | #include <linux/sched.h> |
| 21 | #include <linux/sched/rt.h> |
| 22 | |
Olav Haugan | 09bcc68 | 2016-09-02 17:12:20 -0700 | [diff] [blame] | 23 | #include <trace/events/sched.h> |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 24 | |
Pavankumar Kondeti | 4b62196 | 2017-04-10 14:08:31 +0530 | [diff] [blame] | 25 | #define MAX_CPUS_PER_CLUSTER 6 |
Olav Haugan | e3a3db9 | 2016-06-27 11:35:43 -0700 | [diff] [blame] | 26 | #define MAX_CLUSTERS 2 |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 27 | |
Olav Haugan | e3a3db9 | 2016-06-27 11:35:43 -0700 | [diff] [blame] | 28 | struct cluster_data { |
| 29 | bool inited; |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 30 | unsigned int min_cpus; |
| 31 | unsigned int max_cpus; |
| 32 | unsigned int offline_delay_ms; |
Olav Haugan | e3a3db9 | 2016-06-27 11:35:43 -0700 | [diff] [blame] | 33 | unsigned int busy_up_thres[MAX_CPUS_PER_CLUSTER]; |
| 34 | unsigned int busy_down_thres[MAX_CPUS_PER_CLUSTER]; |
Olav Haugan | 833926cb | 2016-06-27 11:38:06 -0700 | [diff] [blame] | 35 | unsigned int active_cpus; |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 36 | unsigned int num_cpus; |
Pavankumar Kondeti | 32b2dd0 | 2017-04-13 16:58:57 +0530 | [diff] [blame] | 37 | unsigned int nr_isolated_cpus; |
Olav Haugan | 833926cb | 2016-06-27 11:38:06 -0700 | [diff] [blame] | 38 | cpumask_t cpu_mask; |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 39 | unsigned int need_cpus; |
| 40 | unsigned int task_thres; |
Pavankumar Kondeti | 00530928 | 2017-05-10 15:43:29 +0530 | [diff] [blame] | 41 | unsigned int max_nr; |
Pavankumar Kondeti | 83236e3 | 2017-02-10 09:58:58 +0530 | [diff] [blame] | 42 | s64 need_ts; |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 43 | struct list_head lru; |
| 44 | bool pending; |
| 45 | spinlock_t pending_lock; |
| 46 | bool is_big_cluster; |
Sabyasachi Singh | 9453cb2 | 2017-05-09 18:50:19 -0700 | [diff] [blame] | 47 | bool enable; |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 48 | int nrrun; |
Olav Haugan | 833926cb | 2016-06-27 11:38:06 -0700 | [diff] [blame] | 49 | struct task_struct *core_ctl_thread; |
Olav Haugan | e3a3db9 | 2016-06-27 11:35:43 -0700 | [diff] [blame] | 50 | unsigned int first_cpu; |
Olav Haugan | a024f47 | 2016-10-13 10:34:11 -0700 | [diff] [blame] | 51 | unsigned int boost; |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 52 | struct kobject kobj; |
| 53 | }; |
| 54 | |
Olav Haugan | e3a3db9 | 2016-06-27 11:35:43 -0700 | [diff] [blame] | 55 | struct cpu_data { |
Olav Haugan | e3a3db9 | 2016-06-27 11:35:43 -0700 | [diff] [blame] | 56 | bool is_busy; |
| 57 | unsigned int busy; |
| 58 | unsigned int cpu; |
| 59 | bool not_preferred; |
| 60 | struct cluster_data *cluster; |
| 61 | struct list_head sib; |
Olav Haugan | 833926cb | 2016-06-27 11:38:06 -0700 | [diff] [blame] | 62 | bool isolated_by_us; |
Olav Haugan | e3a3db9 | 2016-06-27 11:35:43 -0700 | [diff] [blame] | 63 | }; |
| 64 | |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 65 | static DEFINE_PER_CPU(struct cpu_data, cpu_state); |
Olav Haugan | e3a3db9 | 2016-06-27 11:35:43 -0700 | [diff] [blame] | 66 | static struct cluster_data cluster_state[MAX_CLUSTERS]; |
| 67 | static unsigned int num_clusters; |
| 68 | |
| 69 | #define for_each_cluster(cluster, idx) \ |
| 70 | for ((cluster) = &cluster_state[idx]; (idx) < num_clusters;\ |
| 71 | (idx)++, (cluster) = &cluster_state[idx]) |
| 72 | |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 73 | static DEFINE_SPINLOCK(state_lock); |
Olav Haugan | e3a3db9 | 2016-06-27 11:35:43 -0700 | [diff] [blame] | 74 | static void apply_need(struct cluster_data *state); |
Olav Haugan | 833926cb | 2016-06-27 11:38:06 -0700 | [diff] [blame] | 75 | static void wake_up_core_ctl_thread(struct cluster_data *state); |
| 76 | static bool initialized; |
| 77 | |
| 78 | static unsigned int get_active_cpu_count(const struct cluster_data *cluster); |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 79 | |
| 80 | /* ========================= sysfs interface =========================== */ |
| 81 | |
Olav Haugan | e3a3db9 | 2016-06-27 11:35:43 -0700 | [diff] [blame] | 82 | static ssize_t store_min_cpus(struct cluster_data *state, |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 83 | const char *buf, size_t count) |
| 84 | { |
| 85 | unsigned int val; |
| 86 | |
| 87 | if (sscanf(buf, "%u\n", &val) != 1) |
| 88 | return -EINVAL; |
| 89 | |
| 90 | state->min_cpus = min(val, state->max_cpus); |
Olav Haugan | 833926cb | 2016-06-27 11:38:06 -0700 | [diff] [blame] | 91 | wake_up_core_ctl_thread(state); |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 92 | |
| 93 | return count; |
| 94 | } |
| 95 | |
Olav Haugan | e3a3db9 | 2016-06-27 11:35:43 -0700 | [diff] [blame] | 96 | static ssize_t show_min_cpus(const struct cluster_data *state, char *buf) |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 97 | { |
| 98 | return snprintf(buf, PAGE_SIZE, "%u\n", state->min_cpus); |
| 99 | } |
| 100 | |
Olav Haugan | e3a3db9 | 2016-06-27 11:35:43 -0700 | [diff] [blame] | 101 | static ssize_t store_max_cpus(struct cluster_data *state, |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 102 | const char *buf, size_t count) |
| 103 | { |
| 104 | unsigned int val; |
| 105 | |
| 106 | if (sscanf(buf, "%u\n", &val) != 1) |
| 107 | return -EINVAL; |
| 108 | |
| 109 | val = min(val, state->num_cpus); |
| 110 | state->max_cpus = val; |
| 111 | state->min_cpus = min(state->min_cpus, state->max_cpus); |
Olav Haugan | 833926cb | 2016-06-27 11:38:06 -0700 | [diff] [blame] | 112 | wake_up_core_ctl_thread(state); |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 113 | |
| 114 | return count; |
| 115 | } |
| 116 | |
Olav Haugan | e3a3db9 | 2016-06-27 11:35:43 -0700 | [diff] [blame] | 117 | static ssize_t show_max_cpus(const struct cluster_data *state, char *buf) |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 118 | { |
| 119 | return snprintf(buf, PAGE_SIZE, "%u\n", state->max_cpus); |
| 120 | } |
| 121 | |
Olav Haugan | e3a3db9 | 2016-06-27 11:35:43 -0700 | [diff] [blame] | 122 | static ssize_t store_offline_delay_ms(struct cluster_data *state, |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 123 | const char *buf, size_t count) |
| 124 | { |
| 125 | unsigned int val; |
| 126 | |
| 127 | if (sscanf(buf, "%u\n", &val) != 1) |
| 128 | return -EINVAL; |
| 129 | |
| 130 | state->offline_delay_ms = val; |
| 131 | apply_need(state); |
| 132 | |
| 133 | return count; |
| 134 | } |
| 135 | |
Olav Haugan | e3a3db9 | 2016-06-27 11:35:43 -0700 | [diff] [blame] | 136 | static ssize_t show_task_thres(const struct cluster_data *state, char *buf) |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 137 | { |
| 138 | return snprintf(buf, PAGE_SIZE, "%u\n", state->task_thres); |
| 139 | } |
| 140 | |
Olav Haugan | e3a3db9 | 2016-06-27 11:35:43 -0700 | [diff] [blame] | 141 | static ssize_t store_task_thres(struct cluster_data *state, |
| 142 | const char *buf, size_t count) |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 143 | { |
| 144 | unsigned int val; |
| 145 | |
| 146 | if (sscanf(buf, "%u\n", &val) != 1) |
| 147 | return -EINVAL; |
| 148 | |
| 149 | if (val < state->num_cpus) |
| 150 | return -EINVAL; |
| 151 | |
| 152 | state->task_thres = val; |
| 153 | apply_need(state); |
| 154 | |
| 155 | return count; |
| 156 | } |
| 157 | |
Olav Haugan | e3a3db9 | 2016-06-27 11:35:43 -0700 | [diff] [blame] | 158 | static ssize_t show_offline_delay_ms(const struct cluster_data *state, |
| 159 | char *buf) |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 160 | { |
| 161 | return snprintf(buf, PAGE_SIZE, "%u\n", state->offline_delay_ms); |
| 162 | } |
| 163 | |
Olav Haugan | e3a3db9 | 2016-06-27 11:35:43 -0700 | [diff] [blame] | 164 | static ssize_t store_busy_up_thres(struct cluster_data *state, |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 165 | const char *buf, size_t count) |
| 166 | { |
Olav Haugan | e3a3db9 | 2016-06-27 11:35:43 -0700 | [diff] [blame] | 167 | unsigned int val[MAX_CPUS_PER_CLUSTER]; |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 168 | int ret, i; |
| 169 | |
Pavankumar Kondeti | 4b62196 | 2017-04-10 14:08:31 +0530 | [diff] [blame] | 170 | ret = sscanf(buf, "%u %u %u %u %u %u\n", |
| 171 | &val[0], &val[1], &val[2], &val[3], |
| 172 | &val[4], &val[5]); |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 173 | if (ret != 1 && ret != state->num_cpus) |
| 174 | return -EINVAL; |
| 175 | |
| 176 | if (ret == 1) { |
| 177 | for (i = 0; i < state->num_cpus; i++) |
| 178 | state->busy_up_thres[i] = val[0]; |
| 179 | } else { |
| 180 | for (i = 0; i < state->num_cpus; i++) |
| 181 | state->busy_up_thres[i] = val[i]; |
| 182 | } |
| 183 | apply_need(state); |
| 184 | return count; |
| 185 | } |
| 186 | |
Olav Haugan | e3a3db9 | 2016-06-27 11:35:43 -0700 | [diff] [blame] | 187 | static ssize_t show_busy_up_thres(const struct cluster_data *state, char *buf) |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 188 | { |
| 189 | int i, count = 0; |
| 190 | |
| 191 | for (i = 0; i < state->num_cpus; i++) |
| 192 | count += snprintf(buf + count, PAGE_SIZE - count, "%u ", |
| 193 | state->busy_up_thres[i]); |
Olav Haugan | e3a3db9 | 2016-06-27 11:35:43 -0700 | [diff] [blame] | 194 | |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 195 | count += snprintf(buf + count, PAGE_SIZE - count, "\n"); |
| 196 | return count; |
| 197 | } |
| 198 | |
Olav Haugan | e3a3db9 | 2016-06-27 11:35:43 -0700 | [diff] [blame] | 199 | static ssize_t store_busy_down_thres(struct cluster_data *state, |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 200 | const char *buf, size_t count) |
| 201 | { |
Olav Haugan | e3a3db9 | 2016-06-27 11:35:43 -0700 | [diff] [blame] | 202 | unsigned int val[MAX_CPUS_PER_CLUSTER]; |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 203 | int ret, i; |
| 204 | |
Pavankumar Kondeti | 4b62196 | 2017-04-10 14:08:31 +0530 | [diff] [blame] | 205 | ret = sscanf(buf, "%u %u %u %u %u %u\n", |
| 206 | &val[0], &val[1], &val[2], &val[3], |
| 207 | &val[4], &val[5]); |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 208 | if (ret != 1 && ret != state->num_cpus) |
| 209 | return -EINVAL; |
| 210 | |
| 211 | if (ret == 1) { |
| 212 | for (i = 0; i < state->num_cpus; i++) |
| 213 | state->busy_down_thres[i] = val[0]; |
| 214 | } else { |
| 215 | for (i = 0; i < state->num_cpus; i++) |
| 216 | state->busy_down_thres[i] = val[i]; |
| 217 | } |
| 218 | apply_need(state); |
| 219 | return count; |
| 220 | } |
| 221 | |
Olav Haugan | e3a3db9 | 2016-06-27 11:35:43 -0700 | [diff] [blame] | 222 | static ssize_t show_busy_down_thres(const struct cluster_data *state, char *buf) |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 223 | { |
| 224 | int i, count = 0; |
| 225 | |
| 226 | for (i = 0; i < state->num_cpus; i++) |
| 227 | count += snprintf(buf + count, PAGE_SIZE - count, "%u ", |
| 228 | state->busy_down_thres[i]); |
Olav Haugan | e3a3db9 | 2016-06-27 11:35:43 -0700 | [diff] [blame] | 229 | |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 230 | count += snprintf(buf + count, PAGE_SIZE - count, "\n"); |
| 231 | return count; |
| 232 | } |
| 233 | |
Olav Haugan | e3a3db9 | 2016-06-27 11:35:43 -0700 | [diff] [blame] | 234 | static ssize_t store_is_big_cluster(struct cluster_data *state, |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 235 | const char *buf, size_t count) |
| 236 | { |
| 237 | unsigned int val; |
| 238 | |
| 239 | if (sscanf(buf, "%u\n", &val) != 1) |
| 240 | return -EINVAL; |
| 241 | |
| 242 | state->is_big_cluster = val ? 1 : 0; |
| 243 | return count; |
| 244 | } |
| 245 | |
Olav Haugan | e3a3db9 | 2016-06-27 11:35:43 -0700 | [diff] [blame] | 246 | static ssize_t show_is_big_cluster(const struct cluster_data *state, char *buf) |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 247 | { |
| 248 | return snprintf(buf, PAGE_SIZE, "%u\n", state->is_big_cluster); |
| 249 | } |
| 250 | |
Sabyasachi Singh | 9453cb2 | 2017-05-09 18:50:19 -0700 | [diff] [blame] | 251 | static ssize_t store_enable(struct cluster_data *state, |
| 252 | const char *buf, size_t count) |
| 253 | { |
| 254 | unsigned int val; |
| 255 | bool bval; |
| 256 | |
| 257 | if (sscanf(buf, "%u\n", &val) != 1) |
| 258 | return -EINVAL; |
| 259 | |
| 260 | bval = !!val; |
| 261 | if (bval != state->enable) { |
| 262 | state->enable = bval; |
| 263 | apply_need(state); |
| 264 | } |
| 265 | |
| 266 | return count; |
| 267 | } |
| 268 | |
| 269 | static ssize_t show_enable(const struct cluster_data *state, char *buf) |
| 270 | { |
| 271 | return scnprintf(buf, PAGE_SIZE, "%u\n", state->enable); |
| 272 | } |
| 273 | |
Olav Haugan | e3a3db9 | 2016-06-27 11:35:43 -0700 | [diff] [blame] | 274 | static ssize_t show_need_cpus(const struct cluster_data *state, char *buf) |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 275 | { |
| 276 | return snprintf(buf, PAGE_SIZE, "%u\n", state->need_cpus); |
| 277 | } |
| 278 | |
Olav Haugan | 833926cb | 2016-06-27 11:38:06 -0700 | [diff] [blame] | 279 | static ssize_t show_active_cpus(const struct cluster_data *state, char *buf) |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 280 | { |
Olav Haugan | 833926cb | 2016-06-27 11:38:06 -0700 | [diff] [blame] | 281 | return snprintf(buf, PAGE_SIZE, "%u\n", state->active_cpus); |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 282 | } |
| 283 | |
Olav Haugan | e3a3db9 | 2016-06-27 11:35:43 -0700 | [diff] [blame] | 284 | static ssize_t show_global_state(const struct cluster_data *state, char *buf) |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 285 | { |
| 286 | struct cpu_data *c; |
Olav Haugan | e3a3db9 | 2016-06-27 11:35:43 -0700 | [diff] [blame] | 287 | struct cluster_data *cluster; |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 288 | ssize_t count = 0; |
| 289 | unsigned int cpu; |
| 290 | |
Pavankumar Kondeti | a207d5c | 2017-04-13 18:44:24 +0530 | [diff] [blame] | 291 | spin_lock_irq(&state_lock); |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 292 | for_each_possible_cpu(cpu) { |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 293 | c = &per_cpu(cpu_state, cpu); |
Olav Haugan | e3a3db9 | 2016-06-27 11:35:43 -0700 | [diff] [blame] | 294 | cluster = c->cluster; |
| 295 | if (!cluster || !cluster->inited) |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 296 | continue; |
Olav Haugan | e3a3db9 | 2016-06-27 11:35:43 -0700 | [diff] [blame] | 297 | |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 298 | count += snprintf(buf + count, PAGE_SIZE - count, |
Olav Haugan | 833926cb | 2016-06-27 11:38:06 -0700 | [diff] [blame] | 299 | "CPU%u\n", cpu); |
| 300 | count += snprintf(buf + count, PAGE_SIZE - count, |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 301 | "\tCPU: %u\n", c->cpu); |
| 302 | count += snprintf(buf + count, PAGE_SIZE - count, |
Pavankumar Kondeti | 85dfbfa | 2017-03-17 15:29:13 +0530 | [diff] [blame] | 303 | "\tOnline: %u\n", |
| 304 | cpu_online(c->cpu)); |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 305 | count += snprintf(buf + count, PAGE_SIZE - count, |
Pavankumar Kondeti | fbfa7b3 | 2017-03-17 15:52:30 +0530 | [diff] [blame] | 306 | "\tIsolated: %u\n", |
| 307 | cpu_isolated(c->cpu)); |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 308 | count += snprintf(buf + count, PAGE_SIZE - count, |
Olav Haugan | e3a3db9 | 2016-06-27 11:35:43 -0700 | [diff] [blame] | 309 | "\tFirst CPU: %u\n", |
| 310 | cluster->first_cpu); |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 311 | count += snprintf(buf + count, PAGE_SIZE - count, |
| 312 | "\tBusy%%: %u\n", c->busy); |
| 313 | count += snprintf(buf + count, PAGE_SIZE - count, |
| 314 | "\tIs busy: %u\n", c->is_busy); |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 315 | count += snprintf(buf + count, PAGE_SIZE - count, |
Pavankumar Kondeti | 63877bc | 2017-02-16 08:25:21 +0530 | [diff] [blame] | 316 | "\tNot preferred: %u\n", |
| 317 | c->not_preferred); |
| 318 | count += snprintf(buf + count, PAGE_SIZE - count, |
Olav Haugan | e3a3db9 | 2016-06-27 11:35:43 -0700 | [diff] [blame] | 319 | "\tNr running: %u\n", cluster->nrrun); |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 320 | count += snprintf(buf + count, PAGE_SIZE - count, |
Olav Haugan | 833926cb | 2016-06-27 11:38:06 -0700 | [diff] [blame] | 321 | "\tActive CPUs: %u\n", get_active_cpu_count(cluster)); |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 322 | count += snprintf(buf + count, PAGE_SIZE - count, |
Olav Haugan | e3a3db9 | 2016-06-27 11:35:43 -0700 | [diff] [blame] | 323 | "\tNeed CPUs: %u\n", cluster->need_cpus); |
Olav Haugan | 833926cb | 2016-06-27 11:38:06 -0700 | [diff] [blame] | 324 | count += snprintf(buf + count, PAGE_SIZE - count, |
Pavankumar Kondeti | 32b2dd0 | 2017-04-13 16:58:57 +0530 | [diff] [blame] | 325 | "\tNr isolated CPUs: %u\n", |
| 326 | cluster->nr_isolated_cpus); |
| 327 | count += snprintf(buf + count, PAGE_SIZE - count, |
Olav Haugan | 833926cb | 2016-06-27 11:38:06 -0700 | [diff] [blame] | 328 | "\tBoost: %u\n", (unsigned int) cluster->boost); |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 329 | } |
Pavankumar Kondeti | a207d5c | 2017-04-13 18:44:24 +0530 | [diff] [blame] | 330 | spin_unlock_irq(&state_lock); |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 331 | |
| 332 | return count; |
| 333 | } |
| 334 | |
Olav Haugan | e3a3db9 | 2016-06-27 11:35:43 -0700 | [diff] [blame] | 335 | static ssize_t store_not_preferred(struct cluster_data *state, |
| 336 | const char *buf, size_t count) |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 337 | { |
| 338 | struct cpu_data *c; |
Olav Haugan | e3a3db9 | 2016-06-27 11:35:43 -0700 | [diff] [blame] | 339 | unsigned int i; |
| 340 | unsigned int val[MAX_CPUS_PER_CLUSTER]; |
| 341 | unsigned long flags; |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 342 | int ret; |
| 343 | |
Pavankumar Kondeti | 4b62196 | 2017-04-10 14:08:31 +0530 | [diff] [blame] | 344 | ret = sscanf(buf, "%u %u %u %u %u %u\n", |
| 345 | &val[0], &val[1], &val[2], &val[3], |
| 346 | &val[4], &val[5]); |
Pavankumar Kondeti | 63877bc | 2017-02-16 08:25:21 +0530 | [diff] [blame] | 347 | if (ret != state->num_cpus) |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 348 | return -EINVAL; |
| 349 | |
Olav Haugan | e3a3db9 | 2016-06-27 11:35:43 -0700 | [diff] [blame] | 350 | spin_lock_irqsave(&state_lock, flags); |
Pavankumar Kondeti | 63877bc | 2017-02-16 08:25:21 +0530 | [diff] [blame] | 351 | for (i = 0; i < state->num_cpus; i++) { |
| 352 | c = &per_cpu(cpu_state, i + state->first_cpu); |
| 353 | c->not_preferred = val[i]; |
| 354 | } |
Olav Haugan | e3a3db9 | 2016-06-27 11:35:43 -0700 | [diff] [blame] | 355 | spin_unlock_irqrestore(&state_lock, flags); |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 356 | |
| 357 | return count; |
| 358 | } |
| 359 | |
Olav Haugan | e3a3db9 | 2016-06-27 11:35:43 -0700 | [diff] [blame] | 360 | static ssize_t show_not_preferred(const struct cluster_data *state, char *buf) |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 361 | { |
| 362 | struct cpu_data *c; |
| 363 | ssize_t count = 0; |
Olav Haugan | e3a3db9 | 2016-06-27 11:35:43 -0700 | [diff] [blame] | 364 | unsigned long flags; |
Pavankumar Kondeti | 63877bc | 2017-02-16 08:25:21 +0530 | [diff] [blame] | 365 | int i; |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 366 | |
Olav Haugan | e3a3db9 | 2016-06-27 11:35:43 -0700 | [diff] [blame] | 367 | spin_lock_irqsave(&state_lock, flags); |
Pavankumar Kondeti | 63877bc | 2017-02-16 08:25:21 +0530 | [diff] [blame] | 368 | for (i = 0; i < state->num_cpus; i++) { |
| 369 | c = &per_cpu(cpu_state, i + state->first_cpu); |
| 370 | count += scnprintf(buf + count, PAGE_SIZE - count, |
| 371 | "CPU#%d: %u\n", c->cpu, c->not_preferred); |
| 372 | } |
Olav Haugan | e3a3db9 | 2016-06-27 11:35:43 -0700 | [diff] [blame] | 373 | spin_unlock_irqrestore(&state_lock, flags); |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 374 | |
| 375 | return count; |
| 376 | } |
| 377 | |
Olav Haugan | e3a3db9 | 2016-06-27 11:35:43 -0700 | [diff] [blame] | 378 | |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 379 | struct core_ctl_attr { |
| 380 | struct attribute attr; |
Olav Haugan | e3a3db9 | 2016-06-27 11:35:43 -0700 | [diff] [blame] | 381 | ssize_t (*show)(const struct cluster_data *, char *); |
| 382 | ssize_t (*store)(struct cluster_data *, const char *, size_t count); |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 383 | }; |
| 384 | |
| 385 | #define core_ctl_attr_ro(_name) \ |
| 386 | static struct core_ctl_attr _name = \ |
| 387 | __ATTR(_name, 0444, show_##_name, NULL) |
| 388 | |
| 389 | #define core_ctl_attr_rw(_name) \ |
| 390 | static struct core_ctl_attr _name = \ |
| 391 | __ATTR(_name, 0644, show_##_name, store_##_name) |
| 392 | |
| 393 | core_ctl_attr_rw(min_cpus); |
| 394 | core_ctl_attr_rw(max_cpus); |
| 395 | core_ctl_attr_rw(offline_delay_ms); |
| 396 | core_ctl_attr_rw(busy_up_thres); |
| 397 | core_ctl_attr_rw(busy_down_thres); |
| 398 | core_ctl_attr_rw(task_thres); |
| 399 | core_ctl_attr_rw(is_big_cluster); |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 400 | core_ctl_attr_ro(need_cpus); |
Olav Haugan | 833926cb | 2016-06-27 11:38:06 -0700 | [diff] [blame] | 401 | core_ctl_attr_ro(active_cpus); |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 402 | core_ctl_attr_ro(global_state); |
| 403 | core_ctl_attr_rw(not_preferred); |
Sabyasachi Singh | 9453cb2 | 2017-05-09 18:50:19 -0700 | [diff] [blame] | 404 | core_ctl_attr_rw(enable); |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 405 | |
| 406 | static struct attribute *default_attrs[] = { |
| 407 | &min_cpus.attr, |
| 408 | &max_cpus.attr, |
| 409 | &offline_delay_ms.attr, |
| 410 | &busy_up_thres.attr, |
| 411 | &busy_down_thres.attr, |
| 412 | &task_thres.attr, |
| 413 | &is_big_cluster.attr, |
Sabyasachi Singh | 9453cb2 | 2017-05-09 18:50:19 -0700 | [diff] [blame] | 414 | &enable.attr, |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 415 | &need_cpus.attr, |
Olav Haugan | 833926cb | 2016-06-27 11:38:06 -0700 | [diff] [blame] | 416 | &active_cpus.attr, |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 417 | &global_state.attr, |
| 418 | ¬_preferred.attr, |
| 419 | NULL |
| 420 | }; |
| 421 | |
Olav Haugan | e3a3db9 | 2016-06-27 11:35:43 -0700 | [diff] [blame] | 422 | #define to_cluster_data(k) container_of(k, struct cluster_data, kobj) |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 423 | #define to_attr(a) container_of(a, struct core_ctl_attr, attr) |
| 424 | static ssize_t show(struct kobject *kobj, struct attribute *attr, char *buf) |
| 425 | { |
Olav Haugan | e3a3db9 | 2016-06-27 11:35:43 -0700 | [diff] [blame] | 426 | struct cluster_data *data = to_cluster_data(kobj); |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 427 | struct core_ctl_attr *cattr = to_attr(attr); |
| 428 | ssize_t ret = -EIO; |
| 429 | |
| 430 | if (cattr->show) |
| 431 | ret = cattr->show(data, buf); |
| 432 | |
| 433 | return ret; |
| 434 | } |
| 435 | |
| 436 | static ssize_t store(struct kobject *kobj, struct attribute *attr, |
| 437 | const char *buf, size_t count) |
| 438 | { |
Olav Haugan | e3a3db9 | 2016-06-27 11:35:43 -0700 | [diff] [blame] | 439 | struct cluster_data *data = to_cluster_data(kobj); |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 440 | struct core_ctl_attr *cattr = to_attr(attr); |
| 441 | ssize_t ret = -EIO; |
| 442 | |
| 443 | if (cattr->store) |
| 444 | ret = cattr->store(data, buf, count); |
| 445 | |
| 446 | return ret; |
| 447 | } |
| 448 | |
| 449 | static const struct sysfs_ops sysfs_ops = { |
| 450 | .show = show, |
| 451 | .store = store, |
| 452 | }; |
| 453 | |
| 454 | static struct kobj_type ktype_core_ctl = { |
| 455 | .sysfs_ops = &sysfs_ops, |
| 456 | .default_attrs = default_attrs, |
| 457 | }; |
| 458 | |
| 459 | /* ==================== runqueue based core count =================== */ |
| 460 | |
Syed Rameez Mustafa | 20acfe7 | 2017-01-30 09:35:46 +0530 | [diff] [blame] | 461 | static void update_running_avg(void) |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 462 | { |
Syed Rameez Mustafa | 20acfe7 | 2017-01-30 09:35:46 +0530 | [diff] [blame] | 463 | int avg, iowait_avg, big_avg; |
Pavankumar Kondeti | 00530928 | 2017-05-10 15:43:29 +0530 | [diff] [blame] | 464 | int max_nr, big_max_nr; |
Olav Haugan | e3a3db9 | 2016-06-27 11:35:43 -0700 | [diff] [blame] | 465 | struct cluster_data *cluster; |
| 466 | unsigned int index = 0; |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 467 | |
Pavankumar Kondeti | 00530928 | 2017-05-10 15:43:29 +0530 | [diff] [blame] | 468 | sched_get_nr_running_avg(&avg, &iowait_avg, &big_avg, |
| 469 | &max_nr, &big_max_nr); |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 470 | |
Olav Haugan | e3a3db9 | 2016-06-27 11:35:43 -0700 | [diff] [blame] | 471 | for_each_cluster(cluster, index) { |
| 472 | if (!cluster->inited) |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 473 | continue; |
Olav Haugan | e3a3db9 | 2016-06-27 11:35:43 -0700 | [diff] [blame] | 474 | cluster->nrrun = cluster->is_big_cluster ? big_avg : avg; |
Pavankumar Kondeti | 00530928 | 2017-05-10 15:43:29 +0530 | [diff] [blame] | 475 | cluster->max_nr = cluster->is_big_cluster ? big_max_nr : max_nr; |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 476 | } |
| 477 | } |
| 478 | |
Pavankumar Kondeti | 00530928 | 2017-05-10 15:43:29 +0530 | [diff] [blame] | 479 | #define MAX_NR_THRESHOLD 4 |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 480 | /* adjust needed CPUs based on current runqueue information */ |
Olav Haugan | e3a3db9 | 2016-06-27 11:35:43 -0700 | [diff] [blame] | 481 | static unsigned int apply_task_need(const struct cluster_data *cluster, |
| 482 | unsigned int new_need) |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 483 | { |
Olav Haugan | 833926cb | 2016-06-27 11:38:06 -0700 | [diff] [blame] | 484 | /* unisolate all cores if there are enough tasks */ |
Olav Haugan | e3a3db9 | 2016-06-27 11:35:43 -0700 | [diff] [blame] | 485 | if (cluster->nrrun >= cluster->task_thres) |
| 486 | return cluster->num_cpus; |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 487 | |
Olav Haugan | 833926cb | 2016-06-27 11:38:06 -0700 | [diff] [blame] | 488 | /* only unisolate more cores if there are tasks to run */ |
Olav Haugan | e3a3db9 | 2016-06-27 11:35:43 -0700 | [diff] [blame] | 489 | if (cluster->nrrun > new_need) |
Pavankumar Kondeti | 00530928 | 2017-05-10 15:43:29 +0530 | [diff] [blame] | 490 | new_need = new_need + 1; |
| 491 | |
| 492 | /* |
| 493 | * We don't want tasks to be overcrowded in a cluster. |
| 494 | * If any CPU has more than MAX_NR_THRESHOLD in the last |
| 495 | * window, bring another CPU to help out. |
| 496 | */ |
| 497 | if (cluster->max_nr > MAX_NR_THRESHOLD) |
| 498 | new_need = new_need + 1; |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 499 | |
| 500 | return new_need; |
| 501 | } |
| 502 | |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 503 | /* ======================= load based core count ====================== */ |
| 504 | |
Olav Haugan | e3a3db9 | 2016-06-27 11:35:43 -0700 | [diff] [blame] | 505 | static unsigned int apply_limits(const struct cluster_data *cluster, |
| 506 | unsigned int need_cpus) |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 507 | { |
Olav Haugan | e3a3db9 | 2016-06-27 11:35:43 -0700 | [diff] [blame] | 508 | return min(max(cluster->min_cpus, need_cpus), cluster->max_cpus); |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 509 | } |
| 510 | |
Olav Haugan | 833926cb | 2016-06-27 11:38:06 -0700 | [diff] [blame] | 511 | static unsigned int get_active_cpu_count(const struct cluster_data *cluster) |
| 512 | { |
| 513 | return cluster->num_cpus - |
| 514 | sched_isolate_count(&cluster->cpu_mask, true); |
| 515 | } |
| 516 | |
| 517 | static bool is_active(const struct cpu_data *state) |
| 518 | { |
Pavankumar Kondeti | 85dfbfa | 2017-03-17 15:29:13 +0530 | [diff] [blame] | 519 | return cpu_online(state->cpu) && !cpu_isolated(state->cpu); |
Olav Haugan | 833926cb | 2016-06-27 11:38:06 -0700 | [diff] [blame] | 520 | } |
| 521 | |
| 522 | static bool adjustment_possible(const struct cluster_data *cluster, |
| 523 | unsigned int need) |
| 524 | { |
| 525 | return (need < cluster->active_cpus || (need > cluster->active_cpus && |
Pavankumar Kondeti | 32b2dd0 | 2017-04-13 16:58:57 +0530 | [diff] [blame] | 526 | cluster->nr_isolated_cpus)); |
Olav Haugan | 833926cb | 2016-06-27 11:38:06 -0700 | [diff] [blame] | 527 | } |
| 528 | |
Olav Haugan | e3a3db9 | 2016-06-27 11:35:43 -0700 | [diff] [blame] | 529 | static bool eval_need(struct cluster_data *cluster) |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 530 | { |
| 531 | unsigned long flags; |
| 532 | struct cpu_data *c; |
| 533 | unsigned int need_cpus = 0, last_need, thres_idx; |
| 534 | int ret = 0; |
| 535 | bool need_flag = false; |
Olav Haugan | 833926cb | 2016-06-27 11:38:06 -0700 | [diff] [blame] | 536 | unsigned int new_need; |
Pavankumar Kondeti | 0824857 | 2017-04-13 15:45:11 +0530 | [diff] [blame] | 537 | s64 now, elapsed; |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 538 | |
Olav Haugan | e3a3db9 | 2016-06-27 11:35:43 -0700 | [diff] [blame] | 539 | if (unlikely(!cluster->inited)) |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 540 | return 0; |
| 541 | |
| 542 | spin_lock_irqsave(&state_lock, flags); |
Olav Haugan | 833926cb | 2016-06-27 11:38:06 -0700 | [diff] [blame] | 543 | |
Sabyasachi Singh | 9453cb2 | 2017-05-09 18:50:19 -0700 | [diff] [blame] | 544 | if (cluster->boost || !cluster->enable) { |
Olav Haugan | 833926cb | 2016-06-27 11:38:06 -0700 | [diff] [blame] | 545 | need_cpus = cluster->max_cpus; |
| 546 | } else { |
Pavankumar Kondeti | 0824857 | 2017-04-13 15:45:11 +0530 | [diff] [blame] | 547 | cluster->active_cpus = get_active_cpu_count(cluster); |
| 548 | thres_idx = cluster->active_cpus ? cluster->active_cpus - 1 : 0; |
Olav Haugan | 833926cb | 2016-06-27 11:38:06 -0700 | [diff] [blame] | 549 | list_for_each_entry(c, &cluster->lru, sib) { |
| 550 | if (c->busy >= cluster->busy_up_thres[thres_idx]) |
| 551 | c->is_busy = true; |
| 552 | else if (c->busy < cluster->busy_down_thres[thres_idx]) |
| 553 | c->is_busy = false; |
| 554 | need_cpus += c->is_busy; |
| 555 | } |
| 556 | need_cpus = apply_task_need(cluster, need_cpus); |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 557 | } |
Olav Haugan | 833926cb | 2016-06-27 11:38:06 -0700 | [diff] [blame] | 558 | new_need = apply_limits(cluster, need_cpus); |
| 559 | need_flag = adjustment_possible(cluster, new_need); |
| 560 | |
Olav Haugan | e3a3db9 | 2016-06-27 11:35:43 -0700 | [diff] [blame] | 561 | last_need = cluster->need_cpus; |
Pavankumar Kondeti | 83236e3 | 2017-02-10 09:58:58 +0530 | [diff] [blame] | 562 | now = ktime_to_ms(ktime_get()); |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 563 | |
Pavankumar Kondeti | 0824857 | 2017-04-13 15:45:11 +0530 | [diff] [blame] | 564 | if (new_need > cluster->active_cpus) { |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 565 | ret = 1; |
Pavankumar Kondeti | 0824857 | 2017-04-13 15:45:11 +0530 | [diff] [blame] | 566 | } else { |
| 567 | if (new_need == last_need) { |
| 568 | cluster->need_ts = now; |
| 569 | spin_unlock_irqrestore(&state_lock, flags); |
| 570 | return 0; |
| 571 | } |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 572 | |
Pavankumar Kondeti | 0824857 | 2017-04-13 15:45:11 +0530 | [diff] [blame] | 573 | elapsed = now - cluster->need_ts; |
Olav Haugan | 833926cb | 2016-06-27 11:38:06 -0700 | [diff] [blame] | 574 | ret = elapsed >= cluster->offline_delay_ms; |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 575 | } |
| 576 | |
Pavankumar Kondeti | 83236e3 | 2017-02-10 09:58:58 +0530 | [diff] [blame] | 577 | if (ret) { |
| 578 | cluster->need_ts = now; |
| 579 | cluster->need_cpus = new_need; |
| 580 | } |
Pavankumar Kondeti | 0824857 | 2017-04-13 15:45:11 +0530 | [diff] [blame] | 581 | trace_core_ctl_eval_need(cluster->first_cpu, last_need, new_need, |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 582 | ret && need_flag); |
| 583 | spin_unlock_irqrestore(&state_lock, flags); |
| 584 | |
| 585 | return ret && need_flag; |
| 586 | } |
| 587 | |
Olav Haugan | e3a3db9 | 2016-06-27 11:35:43 -0700 | [diff] [blame] | 588 | static void apply_need(struct cluster_data *cluster) |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 589 | { |
Olav Haugan | e3a3db9 | 2016-06-27 11:35:43 -0700 | [diff] [blame] | 590 | if (eval_need(cluster)) |
Olav Haugan | 833926cb | 2016-06-27 11:38:06 -0700 | [diff] [blame] | 591 | wake_up_core_ctl_thread(cluster); |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 592 | } |
| 593 | |
Syed Rameez Mustafa | 20acfe7 | 2017-01-30 09:35:46 +0530 | [diff] [blame] | 594 | static void core_ctl_set_busy(struct cpu_data *c, unsigned int busy) |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 595 | { |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 596 | unsigned int old_is_busy = c->is_busy; |
| 597 | |
Syed Rameez Mustafa | 20acfe7 | 2017-01-30 09:35:46 +0530 | [diff] [blame] | 598 | if (c->busy == busy) |
| 599 | return; |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 600 | |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 601 | c->busy = busy; |
Syed Rameez Mustafa | 20acfe7 | 2017-01-30 09:35:46 +0530 | [diff] [blame] | 602 | trace_core_ctl_set_busy(c->cpu, busy, old_is_busy, c->is_busy); |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 603 | } |
| 604 | |
| 605 | /* ========================= core count enforcement ==================== */ |
| 606 | |
Olav Haugan | 833926cb | 2016-06-27 11:38:06 -0700 | [diff] [blame] | 607 | static void wake_up_core_ctl_thread(struct cluster_data *cluster) |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 608 | { |
| 609 | unsigned long flags; |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 610 | |
Olav Haugan | e3a3db9 | 2016-06-27 11:35:43 -0700 | [diff] [blame] | 611 | spin_lock_irqsave(&cluster->pending_lock, flags); |
| 612 | cluster->pending = true; |
| 613 | spin_unlock_irqrestore(&cluster->pending_lock, flags); |
Puja Gupta | 487dec6 | 2017-06-27 10:13:50 -0700 | [diff] [blame] | 614 | wake_up_process(cluster->core_ctl_thread); |
Olav Haugan | 833926cb | 2016-06-27 11:38:06 -0700 | [diff] [blame] | 615 | } |
| 616 | |
| 617 | static u64 core_ctl_check_timestamp; |
Olav Haugan | 833926cb | 2016-06-27 11:38:06 -0700 | [diff] [blame] | 618 | |
Olav Haugan | a024f47 | 2016-10-13 10:34:11 -0700 | [diff] [blame] | 619 | int core_ctl_set_boost(bool boost) |
Olav Haugan | 833926cb | 2016-06-27 11:38:06 -0700 | [diff] [blame] | 620 | { |
| 621 | unsigned int index = 0; |
| 622 | struct cluster_data *cluster; |
Olav Haugan | a024f47 | 2016-10-13 10:34:11 -0700 | [diff] [blame] | 623 | unsigned long flags; |
| 624 | int ret = 0; |
| 625 | bool boost_state_changed = false; |
Olav Haugan | 833926cb | 2016-06-27 11:38:06 -0700 | [diff] [blame] | 626 | |
Pavankumar Kondeti | d198dde | 2017-03-21 14:21:21 +0530 | [diff] [blame] | 627 | if (unlikely(!initialized)) |
| 628 | return 0; |
| 629 | |
Olav Haugan | a024f47 | 2016-10-13 10:34:11 -0700 | [diff] [blame] | 630 | spin_lock_irqsave(&state_lock, flags); |
Olav Haugan | 833926cb | 2016-06-27 11:38:06 -0700 | [diff] [blame] | 631 | for_each_cluster(cluster, index) { |
Olav Haugan | a024f47 | 2016-10-13 10:34:11 -0700 | [diff] [blame] | 632 | if (cluster->is_big_cluster) { |
| 633 | if (boost) { |
| 634 | boost_state_changed = !cluster->boost; |
| 635 | ++cluster->boost; |
| 636 | } else { |
| 637 | if (!cluster->boost) { |
| 638 | pr_err("Error turning off boost. Boost already turned off\n"); |
| 639 | ret = -EINVAL; |
| 640 | } else { |
| 641 | --cluster->boost; |
| 642 | boost_state_changed = !cluster->boost; |
| 643 | } |
| 644 | } |
| 645 | break; |
Olav Haugan | 833926cb | 2016-06-27 11:38:06 -0700 | [diff] [blame] | 646 | } |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 647 | } |
Olav Haugan | a024f47 | 2016-10-13 10:34:11 -0700 | [diff] [blame] | 648 | spin_unlock_irqrestore(&state_lock, flags); |
| 649 | |
| 650 | if (boost_state_changed) |
| 651 | apply_need(cluster); |
| 652 | |
| 653 | trace_core_ctl_set_boost(cluster->boost, ret); |
| 654 | |
| 655 | return ret; |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 656 | } |
Olav Haugan | 4d77e57 | 2016-11-14 16:14:23 -0800 | [diff] [blame] | 657 | EXPORT_SYMBOL(core_ctl_set_boost); |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 658 | |
Syed Rameez Mustafa | 20acfe7 | 2017-01-30 09:35:46 +0530 | [diff] [blame] | 659 | void core_ctl_check(u64 window_start) |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 660 | { |
Syed Rameez Mustafa | 20acfe7 | 2017-01-30 09:35:46 +0530 | [diff] [blame] | 661 | int cpu; |
| 662 | unsigned int busy; |
| 663 | struct cpu_data *c; |
| 664 | struct cluster_data *cluster; |
| 665 | unsigned int index = 0; |
| 666 | |
Olav Haugan | 833926cb | 2016-06-27 11:38:06 -0700 | [diff] [blame] | 667 | if (unlikely(!initialized)) |
| 668 | return; |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 669 | |
Syed Rameez Mustafa | 20acfe7 | 2017-01-30 09:35:46 +0530 | [diff] [blame] | 670 | if (window_start == core_ctl_check_timestamp) |
| 671 | return; |
Olav Haugan | e3a3db9 | 2016-06-27 11:35:43 -0700 | [diff] [blame] | 672 | |
Syed Rameez Mustafa | 20acfe7 | 2017-01-30 09:35:46 +0530 | [diff] [blame] | 673 | core_ctl_check_timestamp = window_start; |
Olav Haugan | 833926cb | 2016-06-27 11:38:06 -0700 | [diff] [blame] | 674 | |
Syed Rameez Mustafa | 20acfe7 | 2017-01-30 09:35:46 +0530 | [diff] [blame] | 675 | for_each_possible_cpu(cpu) { |
| 676 | |
| 677 | c = &per_cpu(cpu_state, cpu); |
| 678 | cluster = c->cluster; |
| 679 | |
| 680 | if (!cluster || !cluster->inited) |
| 681 | continue; |
| 682 | |
| 683 | busy = sched_get_cpu_util(cpu); |
| 684 | core_ctl_set_busy(c, busy); |
| 685 | } |
| 686 | |
| 687 | update_running_avg(); |
| 688 | |
| 689 | for_each_cluster(cluster, index) { |
| 690 | if (eval_need(cluster)) |
| 691 | wake_up_core_ctl_thread(cluster); |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 692 | } |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 693 | } |
| 694 | |
Olav Haugan | 833926cb | 2016-06-27 11:38:06 -0700 | [diff] [blame] | 695 | static void move_cpu_lru(struct cpu_data *cpu_data) |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 696 | { |
Olav Haugan | 833926cb | 2016-06-27 11:38:06 -0700 | [diff] [blame] | 697 | unsigned long flags; |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 698 | |
Olav Haugan | 833926cb | 2016-06-27 11:38:06 -0700 | [diff] [blame] | 699 | spin_lock_irqsave(&state_lock, flags); |
| 700 | list_del(&cpu_data->sib); |
| 701 | list_add_tail(&cpu_data->sib, &cpu_data->cluster->lru); |
| 702 | spin_unlock_irqrestore(&state_lock, flags); |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 703 | } |
| 704 | |
Olav Haugan | 833926cb | 2016-06-27 11:38:06 -0700 | [diff] [blame] | 705 | static void try_to_isolate(struct cluster_data *cluster, unsigned int need) |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 706 | { |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 707 | struct cpu_data *c, *tmp; |
Olav Haugan | 8597073 | 2016-11-08 13:45:01 -0800 | [diff] [blame] | 708 | unsigned long flags; |
| 709 | unsigned int num_cpus = cluster->num_cpus; |
Pavankumar Kondeti | 32b2dd0 | 2017-04-13 16:58:57 +0530 | [diff] [blame] | 710 | unsigned int nr_isolated = 0; |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 711 | |
Olav Haugan | 8597073 | 2016-11-08 13:45:01 -0800 | [diff] [blame] | 712 | /* |
| 713 | * Protect against entry being removed (and added at tail) by other |
| 714 | * thread (hotplug). |
| 715 | */ |
| 716 | spin_lock_irqsave(&state_lock, flags); |
Olav Haugan | 833926cb | 2016-06-27 11:38:06 -0700 | [diff] [blame] | 717 | list_for_each_entry_safe(c, tmp, &cluster->lru, sib) { |
Olav Haugan | 8597073 | 2016-11-08 13:45:01 -0800 | [diff] [blame] | 718 | if (!num_cpus--) |
| 719 | break; |
| 720 | |
Olav Haugan | 833926cb | 2016-06-27 11:38:06 -0700 | [diff] [blame] | 721 | if (!is_active(c)) |
| 722 | continue; |
| 723 | if (cluster->active_cpus == need) |
| 724 | break; |
| 725 | /* Don't offline busy CPUs. */ |
| 726 | if (c->is_busy) |
| 727 | continue; |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 728 | |
Olav Haugan | 8597073 | 2016-11-08 13:45:01 -0800 | [diff] [blame] | 729 | spin_unlock_irqrestore(&state_lock, flags); |
| 730 | |
Olav Haugan | 833926cb | 2016-06-27 11:38:06 -0700 | [diff] [blame] | 731 | pr_debug("Trying to isolate CPU%u\n", c->cpu); |
| 732 | if (!sched_isolate_cpu(c->cpu)) { |
| 733 | c->isolated_by_us = true; |
| 734 | move_cpu_lru(c); |
Pavankumar Kondeti | 32b2dd0 | 2017-04-13 16:58:57 +0530 | [diff] [blame] | 735 | nr_isolated++; |
Olav Haugan | 833926cb | 2016-06-27 11:38:06 -0700 | [diff] [blame] | 736 | } else { |
| 737 | pr_debug("Unable to isolate CPU%u\n", c->cpu); |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 738 | } |
Olav Haugan | 833926cb | 2016-06-27 11:38:06 -0700 | [diff] [blame] | 739 | cluster->active_cpus = get_active_cpu_count(cluster); |
Olav Haugan | 8597073 | 2016-11-08 13:45:01 -0800 | [diff] [blame] | 740 | spin_lock_irqsave(&state_lock, flags); |
Olav Haugan | 833926cb | 2016-06-27 11:38:06 -0700 | [diff] [blame] | 741 | } |
Pavankumar Kondeti | 32b2dd0 | 2017-04-13 16:58:57 +0530 | [diff] [blame] | 742 | cluster->nr_isolated_cpus += nr_isolated; |
Olav Haugan | 8597073 | 2016-11-08 13:45:01 -0800 | [diff] [blame] | 743 | spin_unlock_irqrestore(&state_lock, flags); |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 744 | |
Olav Haugan | 833926cb | 2016-06-27 11:38:06 -0700 | [diff] [blame] | 745 | /* |
| 746 | * If the number of active CPUs is within the limits, then |
| 747 | * don't force isolation of any busy CPUs. |
| 748 | */ |
| 749 | if (cluster->active_cpus <= cluster->max_cpus) |
| 750 | return; |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 751 | |
Pavankumar Kondeti | 32b2dd0 | 2017-04-13 16:58:57 +0530 | [diff] [blame] | 752 | nr_isolated = 0; |
Olav Haugan | 8597073 | 2016-11-08 13:45:01 -0800 | [diff] [blame] | 753 | num_cpus = cluster->num_cpus; |
| 754 | spin_lock_irqsave(&state_lock, flags); |
Olav Haugan | 833926cb | 2016-06-27 11:38:06 -0700 | [diff] [blame] | 755 | list_for_each_entry_safe(c, tmp, &cluster->lru, sib) { |
Olav Haugan | 8597073 | 2016-11-08 13:45:01 -0800 | [diff] [blame] | 756 | if (!num_cpus--) |
| 757 | break; |
| 758 | |
Olav Haugan | 833926cb | 2016-06-27 11:38:06 -0700 | [diff] [blame] | 759 | if (!is_active(c)) |
| 760 | continue; |
| 761 | if (cluster->active_cpus <= cluster->max_cpus) |
| 762 | break; |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 763 | |
Olav Haugan | 8597073 | 2016-11-08 13:45:01 -0800 | [diff] [blame] | 764 | spin_unlock_irqrestore(&state_lock, flags); |
| 765 | |
Olav Haugan | 833926cb | 2016-06-27 11:38:06 -0700 | [diff] [blame] | 766 | pr_debug("Trying to isolate CPU%u\n", c->cpu); |
| 767 | if (!sched_isolate_cpu(c->cpu)) { |
| 768 | c->isolated_by_us = true; |
| 769 | move_cpu_lru(c); |
Pavankumar Kondeti | 32b2dd0 | 2017-04-13 16:58:57 +0530 | [diff] [blame] | 770 | nr_isolated++; |
Olav Haugan | 833926cb | 2016-06-27 11:38:06 -0700 | [diff] [blame] | 771 | } else { |
| 772 | pr_debug("Unable to isolate CPU%u\n", c->cpu); |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 773 | } |
Olav Haugan | 833926cb | 2016-06-27 11:38:06 -0700 | [diff] [blame] | 774 | cluster->active_cpus = get_active_cpu_count(cluster); |
Olav Haugan | 8597073 | 2016-11-08 13:45:01 -0800 | [diff] [blame] | 775 | spin_lock_irqsave(&state_lock, flags); |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 776 | } |
Pavankumar Kondeti | 32b2dd0 | 2017-04-13 16:58:57 +0530 | [diff] [blame] | 777 | cluster->nr_isolated_cpus += nr_isolated; |
Olav Haugan | 8597073 | 2016-11-08 13:45:01 -0800 | [diff] [blame] | 778 | spin_unlock_irqrestore(&state_lock, flags); |
| 779 | |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 780 | } |
| 781 | |
Olav Haugan | 833926cb | 2016-06-27 11:38:06 -0700 | [diff] [blame] | 782 | static void __try_to_unisolate(struct cluster_data *cluster, |
| 783 | unsigned int need, bool force) |
| 784 | { |
| 785 | struct cpu_data *c, *tmp; |
Olav Haugan | 8597073 | 2016-11-08 13:45:01 -0800 | [diff] [blame] | 786 | unsigned long flags; |
| 787 | unsigned int num_cpus = cluster->num_cpus; |
Pavankumar Kondeti | 32b2dd0 | 2017-04-13 16:58:57 +0530 | [diff] [blame] | 788 | unsigned int nr_unisolated = 0; |
Olav Haugan | 833926cb | 2016-06-27 11:38:06 -0700 | [diff] [blame] | 789 | |
Olav Haugan | 8597073 | 2016-11-08 13:45:01 -0800 | [diff] [blame] | 790 | /* |
| 791 | * Protect against entry being removed (and added at tail) by other |
| 792 | * thread (hotplug). |
| 793 | */ |
| 794 | spin_lock_irqsave(&state_lock, flags); |
Olav Haugan | 833926cb | 2016-06-27 11:38:06 -0700 | [diff] [blame] | 795 | list_for_each_entry_safe(c, tmp, &cluster->lru, sib) { |
Olav Haugan | 8597073 | 2016-11-08 13:45:01 -0800 | [diff] [blame] | 796 | if (!num_cpus--) |
| 797 | break; |
| 798 | |
Olav Haugan | 833926cb | 2016-06-27 11:38:06 -0700 | [diff] [blame] | 799 | if (!c->isolated_by_us) |
| 800 | continue; |
Pavankumar Kondeti | 85dfbfa | 2017-03-17 15:29:13 +0530 | [diff] [blame] | 801 | if ((cpu_online(c->cpu) && !cpu_isolated(c->cpu)) || |
Olav Haugan | 833926cb | 2016-06-27 11:38:06 -0700 | [diff] [blame] | 802 | (!force && c->not_preferred)) |
| 803 | continue; |
| 804 | if (cluster->active_cpus == need) |
| 805 | break; |
| 806 | |
Olav Haugan | 8597073 | 2016-11-08 13:45:01 -0800 | [diff] [blame] | 807 | spin_unlock_irqrestore(&state_lock, flags); |
| 808 | |
Olav Haugan | 833926cb | 2016-06-27 11:38:06 -0700 | [diff] [blame] | 809 | pr_debug("Trying to unisolate CPU%u\n", c->cpu); |
| 810 | if (!sched_unisolate_cpu(c->cpu)) { |
| 811 | c->isolated_by_us = false; |
| 812 | move_cpu_lru(c); |
Pavankumar Kondeti | 32b2dd0 | 2017-04-13 16:58:57 +0530 | [diff] [blame] | 813 | nr_unisolated++; |
Olav Haugan | 833926cb | 2016-06-27 11:38:06 -0700 | [diff] [blame] | 814 | } else { |
| 815 | pr_debug("Unable to unisolate CPU%u\n", c->cpu); |
| 816 | } |
| 817 | cluster->active_cpus = get_active_cpu_count(cluster); |
Olav Haugan | 8597073 | 2016-11-08 13:45:01 -0800 | [diff] [blame] | 818 | spin_lock_irqsave(&state_lock, flags); |
Olav Haugan | 833926cb | 2016-06-27 11:38:06 -0700 | [diff] [blame] | 819 | } |
Pavankumar Kondeti | 32b2dd0 | 2017-04-13 16:58:57 +0530 | [diff] [blame] | 820 | cluster->nr_isolated_cpus -= nr_unisolated; |
Olav Haugan | 8597073 | 2016-11-08 13:45:01 -0800 | [diff] [blame] | 821 | spin_unlock_irqrestore(&state_lock, flags); |
Olav Haugan | 833926cb | 2016-06-27 11:38:06 -0700 | [diff] [blame] | 822 | } |
| 823 | |
| 824 | static void try_to_unisolate(struct cluster_data *cluster, unsigned int need) |
| 825 | { |
| 826 | bool force_use_non_preferred = false; |
| 827 | |
| 828 | __try_to_unisolate(cluster, need, force_use_non_preferred); |
| 829 | |
| 830 | if (cluster->active_cpus == need) |
| 831 | return; |
| 832 | |
| 833 | force_use_non_preferred = true; |
| 834 | __try_to_unisolate(cluster, need, force_use_non_preferred); |
| 835 | } |
| 836 | |
| 837 | static void __ref do_core_ctl(struct cluster_data *cluster) |
| 838 | { |
| 839 | unsigned int need; |
| 840 | |
| 841 | need = apply_limits(cluster, cluster->need_cpus); |
| 842 | |
| 843 | if (adjustment_possible(cluster, need)) { |
| 844 | pr_debug("Trying to adjust group %u from %u to %u\n", |
| 845 | cluster->first_cpu, cluster->active_cpus, need); |
| 846 | |
| 847 | if (cluster->active_cpus > need) |
| 848 | try_to_isolate(cluster, need); |
| 849 | else if (cluster->active_cpus < need) |
| 850 | try_to_unisolate(cluster, need); |
| 851 | } |
| 852 | } |
| 853 | |
| 854 | static int __ref try_core_ctl(void *data) |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 855 | { |
Olav Haugan | e3a3db9 | 2016-06-27 11:35:43 -0700 | [diff] [blame] | 856 | struct cluster_data *cluster = data; |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 857 | unsigned long flags; |
| 858 | |
| 859 | while (1) { |
| 860 | set_current_state(TASK_INTERRUPTIBLE); |
Olav Haugan | e3a3db9 | 2016-06-27 11:35:43 -0700 | [diff] [blame] | 861 | spin_lock_irqsave(&cluster->pending_lock, flags); |
| 862 | if (!cluster->pending) { |
| 863 | spin_unlock_irqrestore(&cluster->pending_lock, flags); |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 864 | schedule(); |
| 865 | if (kthread_should_stop()) |
| 866 | break; |
Olav Haugan | e3a3db9 | 2016-06-27 11:35:43 -0700 | [diff] [blame] | 867 | spin_lock_irqsave(&cluster->pending_lock, flags); |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 868 | } |
| 869 | set_current_state(TASK_RUNNING); |
Olav Haugan | e3a3db9 | 2016-06-27 11:35:43 -0700 | [diff] [blame] | 870 | cluster->pending = false; |
| 871 | spin_unlock_irqrestore(&cluster->pending_lock, flags); |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 872 | |
Olav Haugan | 833926cb | 2016-06-27 11:38:06 -0700 | [diff] [blame] | 873 | do_core_ctl(cluster); |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 874 | } |
| 875 | |
| 876 | return 0; |
| 877 | } |
| 878 | |
Sabyasachi Singh | 518d4abc | 2017-06-26 13:56:47 -0700 | [diff] [blame^] | 879 | static int isolation_cpuhp_state(unsigned int cpu, bool online) |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 880 | { |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 881 | struct cpu_data *state = &per_cpu(cpu_state, cpu); |
Olav Haugan | e3a3db9 | 2016-06-27 11:35:43 -0700 | [diff] [blame] | 882 | struct cluster_data *cluster = state->cluster; |
Olav Haugan | 833926cb | 2016-06-27 11:38:06 -0700 | [diff] [blame] | 883 | unsigned int need; |
Sabyasachi Singh | 518d4abc | 2017-06-26 13:56:47 -0700 | [diff] [blame^] | 884 | bool do_wakeup = false, unisolated = false; |
Pavankumar Kondeti | 32b2dd0 | 2017-04-13 16:58:57 +0530 | [diff] [blame] | 885 | unsigned long flags; |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 886 | |
Olav Haugan | e3a3db9 | 2016-06-27 11:35:43 -0700 | [diff] [blame] | 887 | if (unlikely(!cluster || !cluster->inited)) |
Sabyasachi Singh | 518d4abc | 2017-06-26 13:56:47 -0700 | [diff] [blame^] | 888 | return 0; |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 889 | |
Sabyasachi Singh | 518d4abc | 2017-06-26 13:56:47 -0700 | [diff] [blame^] | 890 | if (online) { |
Olav Haugan | 833926cb | 2016-06-27 11:38:06 -0700 | [diff] [blame] | 891 | cluster->active_cpus = get_active_cpu_count(cluster); |
| 892 | |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 893 | /* |
| 894 | * Moving to the end of the list should only happen in |
| 895 | * CPU_ONLINE and not on CPU_UP_PREPARE to prevent an |
| 896 | * infinite list traversal when thermal (or other entities) |
| 897 | * reject trying to online CPUs. |
| 898 | */ |
Olav Haugan | 833926cb | 2016-06-27 11:38:06 -0700 | [diff] [blame] | 899 | move_cpu_lru(state); |
Sabyasachi Singh | 518d4abc | 2017-06-26 13:56:47 -0700 | [diff] [blame^] | 900 | } else { |
Olav Haugan | 833926cb | 2016-06-27 11:38:06 -0700 | [diff] [blame] | 901 | /* |
| 902 | * We don't want to have a CPU both offline and isolated. |
| 903 | * So unisolate a CPU that went down if it was isolated by us. |
| 904 | */ |
| 905 | if (state->isolated_by_us) { |
| 906 | sched_unisolate_cpu_unlocked(cpu); |
| 907 | state->isolated_by_us = false; |
Pavankumar Kondeti | 32b2dd0 | 2017-04-13 16:58:57 +0530 | [diff] [blame] | 908 | unisolated = true; |
Olav Haugan | 833926cb | 2016-06-27 11:38:06 -0700 | [diff] [blame] | 909 | } |
| 910 | |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 911 | /* Move a CPU to the end of the LRU when it goes offline. */ |
Olav Haugan | 833926cb | 2016-06-27 11:38:06 -0700 | [diff] [blame] | 912 | move_cpu_lru(state); |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 913 | |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 914 | state->busy = 0; |
Olav Haugan | 833926cb | 2016-06-27 11:38:06 -0700 | [diff] [blame] | 915 | cluster->active_cpus = get_active_cpu_count(cluster); |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 916 | } |
| 917 | |
Olav Haugan | 833926cb | 2016-06-27 11:38:06 -0700 | [diff] [blame] | 918 | need = apply_limits(cluster, cluster->need_cpus); |
Pavankumar Kondeti | 32b2dd0 | 2017-04-13 16:58:57 +0530 | [diff] [blame] | 919 | spin_lock_irqsave(&state_lock, flags); |
| 920 | if (unisolated) |
| 921 | cluster->nr_isolated_cpus--; |
| 922 | do_wakeup = adjustment_possible(cluster, need); |
| 923 | spin_unlock_irqrestore(&state_lock, flags); |
| 924 | if (do_wakeup) |
Olav Haugan | 833926cb | 2016-06-27 11:38:06 -0700 | [diff] [blame] | 925 | wake_up_core_ctl_thread(cluster); |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 926 | |
Sabyasachi Singh | 518d4abc | 2017-06-26 13:56:47 -0700 | [diff] [blame^] | 927 | return 0; |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 928 | } |
| 929 | |
Sabyasachi Singh | 518d4abc | 2017-06-26 13:56:47 -0700 | [diff] [blame^] | 930 | static int core_ctl_isolation_online_cpu(unsigned int cpu) |
| 931 | { |
| 932 | return isolation_cpuhp_state(cpu, true); |
| 933 | } |
| 934 | |
| 935 | static int core_ctl_isolation_dead_cpu(unsigned int cpu) |
| 936 | { |
| 937 | return isolation_cpuhp_state(cpu, false); |
| 938 | } |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 939 | |
| 940 | /* ============================ init code ============================== */ |
| 941 | |
Pavankumar Kondeti | 1e951a7 | 2017-03-21 14:00:09 +0530 | [diff] [blame] | 942 | static cpumask_var_t core_ctl_disable_cpumask; |
| 943 | static bool core_ctl_disable_cpumask_present; |
| 944 | |
| 945 | static int __init core_ctl_disable_setup(char *str) |
| 946 | { |
| 947 | if (!*str) |
| 948 | return -EINVAL; |
| 949 | |
| 950 | alloc_bootmem_cpumask_var(&core_ctl_disable_cpumask); |
| 951 | |
| 952 | if (cpulist_parse(str, core_ctl_disable_cpumask) < 0) { |
| 953 | free_bootmem_cpumask_var(core_ctl_disable_cpumask); |
| 954 | return -EINVAL; |
| 955 | } |
| 956 | |
| 957 | core_ctl_disable_cpumask_present = true; |
| 958 | pr_info("disable_cpumask=%*pbl\n", |
| 959 | cpumask_pr_args(core_ctl_disable_cpumask)); |
| 960 | |
| 961 | return 0; |
| 962 | } |
| 963 | early_param("core_ctl_disable_cpumask", core_ctl_disable_setup); |
| 964 | |
| 965 | static bool should_skip(const struct cpumask *mask) |
| 966 | { |
| 967 | if (!core_ctl_disable_cpumask_present) |
| 968 | return false; |
| 969 | |
| 970 | /* |
| 971 | * We operate on a cluster basis. Disable the core_ctl for |
| 972 | * a cluster, if all of it's cpus are specified in |
| 973 | * core_ctl_disable_cpumask |
| 974 | */ |
| 975 | return cpumask_subset(mask, core_ctl_disable_cpumask); |
| 976 | } |
| 977 | |
Olav Haugan | e3a3db9 | 2016-06-27 11:35:43 -0700 | [diff] [blame] | 978 | static struct cluster_data *find_cluster_by_first_cpu(unsigned int first_cpu) |
| 979 | { |
| 980 | unsigned int i; |
| 981 | |
| 982 | for (i = 0; i < num_clusters; ++i) { |
| 983 | if (cluster_state[i].first_cpu == first_cpu) |
| 984 | return &cluster_state[i]; |
| 985 | } |
| 986 | |
| 987 | return NULL; |
| 988 | } |
| 989 | |
| 990 | static int cluster_init(const struct cpumask *mask) |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 991 | { |
| 992 | struct device *dev; |
| 993 | unsigned int first_cpu = cpumask_first(mask); |
Olav Haugan | e3a3db9 | 2016-06-27 11:35:43 -0700 | [diff] [blame] | 994 | struct cluster_data *cluster; |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 995 | struct cpu_data *state; |
| 996 | unsigned int cpu; |
| 997 | struct sched_param param = { .sched_priority = MAX_RT_PRIO-1 }; |
| 998 | |
Pavankumar Kondeti | 1e951a7 | 2017-03-21 14:00:09 +0530 | [diff] [blame] | 999 | if (should_skip(mask)) |
| 1000 | return 0; |
| 1001 | |
Olav Haugan | e3a3db9 | 2016-06-27 11:35:43 -0700 | [diff] [blame] | 1002 | if (find_cluster_by_first_cpu(first_cpu)) |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 1003 | return 0; |
| 1004 | |
| 1005 | dev = get_cpu_device(first_cpu); |
| 1006 | if (!dev) |
| 1007 | return -ENODEV; |
| 1008 | |
| 1009 | pr_info("Creating CPU group %d\n", first_cpu); |
| 1010 | |
Olav Haugan | e3a3db9 | 2016-06-27 11:35:43 -0700 | [diff] [blame] | 1011 | if (num_clusters == MAX_CLUSTERS) { |
| 1012 | pr_err("Unsupported number of clusters. Only %u supported\n", |
| 1013 | MAX_CLUSTERS); |
| 1014 | return -EINVAL; |
| 1015 | } |
| 1016 | cluster = &cluster_state[num_clusters]; |
| 1017 | ++num_clusters; |
| 1018 | |
Olav Haugan | 833926cb | 2016-06-27 11:38:06 -0700 | [diff] [blame] | 1019 | cpumask_copy(&cluster->cpu_mask, mask); |
Olav Haugan | e3a3db9 | 2016-06-27 11:35:43 -0700 | [diff] [blame] | 1020 | cluster->num_cpus = cpumask_weight(mask); |
| 1021 | if (cluster->num_cpus > MAX_CPUS_PER_CLUSTER) { |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 1022 | pr_err("HW configuration not supported\n"); |
| 1023 | return -EINVAL; |
| 1024 | } |
Olav Haugan | e3a3db9 | 2016-06-27 11:35:43 -0700 | [diff] [blame] | 1025 | cluster->first_cpu = first_cpu; |
| 1026 | cluster->min_cpus = 1; |
| 1027 | cluster->max_cpus = cluster->num_cpus; |
| 1028 | cluster->need_cpus = cluster->num_cpus; |
Olav Haugan | e3a3db9 | 2016-06-27 11:35:43 -0700 | [diff] [blame] | 1029 | cluster->offline_delay_ms = 100; |
| 1030 | cluster->task_thres = UINT_MAX; |
| 1031 | cluster->nrrun = cluster->num_cpus; |
Sabyasachi Singh | 9453cb2 | 2017-05-09 18:50:19 -0700 | [diff] [blame] | 1032 | cluster->enable = true; |
Olav Haugan | e3a3db9 | 2016-06-27 11:35:43 -0700 | [diff] [blame] | 1033 | INIT_LIST_HEAD(&cluster->lru); |
Olav Haugan | e3a3db9 | 2016-06-27 11:35:43 -0700 | [diff] [blame] | 1034 | spin_lock_init(&cluster->pending_lock); |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 1035 | |
| 1036 | for_each_cpu(cpu, mask) { |
| 1037 | pr_info("Init CPU%u state\n", cpu); |
| 1038 | |
| 1039 | state = &per_cpu(cpu_state, cpu); |
Olav Haugan | e3a3db9 | 2016-06-27 11:35:43 -0700 | [diff] [blame] | 1040 | state->cluster = cluster; |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 1041 | state->cpu = cpu; |
Olav Haugan | e3a3db9 | 2016-06-27 11:35:43 -0700 | [diff] [blame] | 1042 | list_add_tail(&state->sib, &cluster->lru); |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 1043 | } |
Olav Haugan | 833926cb | 2016-06-27 11:38:06 -0700 | [diff] [blame] | 1044 | cluster->active_cpus = get_active_cpu_count(cluster); |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 1045 | |
Olav Haugan | 833926cb | 2016-06-27 11:38:06 -0700 | [diff] [blame] | 1046 | cluster->core_ctl_thread = kthread_run(try_core_ctl, (void *) cluster, |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 1047 | "core_ctl/%d", first_cpu); |
Olav Haugan | 833926cb | 2016-06-27 11:38:06 -0700 | [diff] [blame] | 1048 | if (IS_ERR(cluster->core_ctl_thread)) |
| 1049 | return PTR_ERR(cluster->core_ctl_thread); |
| 1050 | |
| 1051 | sched_setscheduler_nocheck(cluster->core_ctl_thread, SCHED_FIFO, |
Olav Haugan | e3a3db9 | 2016-06-27 11:35:43 -0700 | [diff] [blame] | 1052 | ¶m); |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 1053 | |
Olav Haugan | e3a3db9 | 2016-06-27 11:35:43 -0700 | [diff] [blame] | 1054 | cluster->inited = true; |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 1055 | |
Olav Haugan | e3a3db9 | 2016-06-27 11:35:43 -0700 | [diff] [blame] | 1056 | kobject_init(&cluster->kobj, &ktype_core_ctl); |
| 1057 | return kobject_add(&cluster->kobj, &dev->kobj, "core_ctl"); |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 1058 | } |
| 1059 | |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 1060 | static int __init core_ctl_init(void) |
| 1061 | { |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 1062 | unsigned int cpu; |
Syed Rameez Mustafa | 20acfe7 | 2017-01-30 09:35:46 +0530 | [diff] [blame] | 1063 | struct cpumask cpus = *cpu_possible_mask; |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 1064 | |
Pavankumar Kondeti | 1e951a7 | 2017-03-21 14:00:09 +0530 | [diff] [blame] | 1065 | if (should_skip(cpu_possible_mask)) |
| 1066 | return 0; |
| 1067 | |
Sabyasachi Singh | 518d4abc | 2017-06-26 13:56:47 -0700 | [diff] [blame^] | 1068 | cpuhp_setup_state_nocalls(CPUHP_AP_ONLINE_DYN, |
| 1069 | "core_ctl/isolation:online", |
| 1070 | core_ctl_isolation_online_cpu, NULL); |
| 1071 | |
| 1072 | cpuhp_setup_state_nocalls(CPUHP_CORE_CTL_ISOLATION_DEAD, |
| 1073 | "core_ctl/isolation:dead", |
| 1074 | NULL, core_ctl_isolation_dead_cpu); |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 1075 | |
Syed Rameez Mustafa | 20acfe7 | 2017-01-30 09:35:46 +0530 | [diff] [blame] | 1076 | for_each_cpu(cpu, &cpus) { |
Olav Haugan | 833926cb | 2016-06-27 11:38:06 -0700 | [diff] [blame] | 1077 | int ret; |
Syed Rameez Mustafa | 20acfe7 | 2017-01-30 09:35:46 +0530 | [diff] [blame] | 1078 | const struct cpumask *cluster_cpus = cpu_coregroup_mask(cpu); |
Olav Haugan | 833926cb | 2016-06-27 11:38:06 -0700 | [diff] [blame] | 1079 | |
Syed Rameez Mustafa | 20acfe7 | 2017-01-30 09:35:46 +0530 | [diff] [blame] | 1080 | ret = cluster_init(cluster_cpus); |
| 1081 | if (ret) |
| 1082 | pr_warn("unable to create core ctl group: %d\n", ret); |
| 1083 | cpumask_andnot(&cpus, &cpus, cluster_cpus); |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 1084 | } |
Olav Haugan | 833926cb | 2016-06-27 11:38:06 -0700 | [diff] [blame] | 1085 | initialized = true; |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 1086 | return 0; |
| 1087 | } |
| 1088 | |
| 1089 | late_initcall(core_ctl_init); |