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> |
| 16 | #include <linux/notifier.h> |
| 17 | #include <linux/cpu.h> |
| 18 | #include <linux/cpumask.h> |
| 19 | #include <linux/cpufreq.h> |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 20 | #include <linux/kthread.h> |
| 21 | #include <linux/sched.h> |
| 22 | #include <linux/sched/rt.h> |
| 23 | |
Olav Haugan | 09bcc68 | 2016-09-02 17:12:20 -0700 | [diff] [blame] | 24 | #include <trace/events/sched.h> |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 25 | |
Olav Haugan | e3a3db9 | 2016-06-27 11:35:43 -0700 | [diff] [blame] | 26 | #define MAX_CPUS_PER_CLUSTER 4 |
| 27 | #define MAX_CLUSTERS 2 |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 28 | |
Olav Haugan | e3a3db9 | 2016-06-27 11:35:43 -0700 | [diff] [blame] | 29 | struct cluster_data { |
| 30 | bool inited; |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 31 | unsigned int min_cpus; |
| 32 | unsigned int max_cpus; |
| 33 | unsigned int offline_delay_ms; |
Olav Haugan | e3a3db9 | 2016-06-27 11:35:43 -0700 | [diff] [blame] | 34 | unsigned int busy_up_thres[MAX_CPUS_PER_CLUSTER]; |
| 35 | unsigned int busy_down_thres[MAX_CPUS_PER_CLUSTER]; |
Olav Haugan | 833926cb | 2016-06-27 11:38:06 -0700 | [diff] [blame] | 36 | unsigned int active_cpus; |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 37 | unsigned int num_cpus; |
Pavankumar Kondeti | 32b2dd0 | 2017-04-13 16:58:57 +0530 | [diff] [blame^] | 38 | unsigned int nr_isolated_cpus; |
Olav Haugan | 833926cb | 2016-06-27 11:38:06 -0700 | [diff] [blame] | 39 | cpumask_t cpu_mask; |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 40 | unsigned int need_cpus; |
| 41 | unsigned int task_thres; |
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; |
| 47 | int nrrun; |
| 48 | bool nrrun_changed; |
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 | |
| 170 | ret = sscanf(buf, "%u %u %u %u\n", &val[0], &val[1], &val[2], &val[3]); |
| 171 | if (ret != 1 && ret != state->num_cpus) |
| 172 | return -EINVAL; |
| 173 | |
| 174 | if (ret == 1) { |
| 175 | for (i = 0; i < state->num_cpus; i++) |
| 176 | state->busy_up_thres[i] = val[0]; |
| 177 | } else { |
| 178 | for (i = 0; i < state->num_cpus; i++) |
| 179 | state->busy_up_thres[i] = val[i]; |
| 180 | } |
| 181 | apply_need(state); |
| 182 | return count; |
| 183 | } |
| 184 | |
Olav Haugan | e3a3db9 | 2016-06-27 11:35:43 -0700 | [diff] [blame] | 185 | 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] | 186 | { |
| 187 | int i, count = 0; |
| 188 | |
| 189 | for (i = 0; i < state->num_cpus; i++) |
| 190 | count += snprintf(buf + count, PAGE_SIZE - count, "%u ", |
| 191 | state->busy_up_thres[i]); |
Olav Haugan | e3a3db9 | 2016-06-27 11:35:43 -0700 | [diff] [blame] | 192 | |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 193 | count += snprintf(buf + count, PAGE_SIZE - count, "\n"); |
| 194 | return count; |
| 195 | } |
| 196 | |
Olav Haugan | e3a3db9 | 2016-06-27 11:35:43 -0700 | [diff] [blame] | 197 | static ssize_t store_busy_down_thres(struct cluster_data *state, |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 198 | const char *buf, size_t count) |
| 199 | { |
Olav Haugan | e3a3db9 | 2016-06-27 11:35:43 -0700 | [diff] [blame] | 200 | unsigned int val[MAX_CPUS_PER_CLUSTER]; |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 201 | int ret, i; |
| 202 | |
| 203 | ret = sscanf(buf, "%u %u %u %u\n", &val[0], &val[1], &val[2], &val[3]); |
| 204 | if (ret != 1 && ret != state->num_cpus) |
| 205 | return -EINVAL; |
| 206 | |
| 207 | if (ret == 1) { |
| 208 | for (i = 0; i < state->num_cpus; i++) |
| 209 | state->busy_down_thres[i] = val[0]; |
| 210 | } else { |
| 211 | for (i = 0; i < state->num_cpus; i++) |
| 212 | state->busy_down_thres[i] = val[i]; |
| 213 | } |
| 214 | apply_need(state); |
| 215 | return count; |
| 216 | } |
| 217 | |
Olav Haugan | e3a3db9 | 2016-06-27 11:35:43 -0700 | [diff] [blame] | 218 | 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] | 219 | { |
| 220 | int i, count = 0; |
| 221 | |
| 222 | for (i = 0; i < state->num_cpus; i++) |
| 223 | count += snprintf(buf + count, PAGE_SIZE - count, "%u ", |
| 224 | state->busy_down_thres[i]); |
Olav Haugan | e3a3db9 | 2016-06-27 11:35:43 -0700 | [diff] [blame] | 225 | |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 226 | count += snprintf(buf + count, PAGE_SIZE - count, "\n"); |
| 227 | return count; |
| 228 | } |
| 229 | |
Olav Haugan | e3a3db9 | 2016-06-27 11:35:43 -0700 | [diff] [blame] | 230 | static ssize_t store_is_big_cluster(struct cluster_data *state, |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 231 | const char *buf, size_t count) |
| 232 | { |
| 233 | unsigned int val; |
| 234 | |
| 235 | if (sscanf(buf, "%u\n", &val) != 1) |
| 236 | return -EINVAL; |
| 237 | |
| 238 | state->is_big_cluster = val ? 1 : 0; |
| 239 | return count; |
| 240 | } |
| 241 | |
Olav Haugan | e3a3db9 | 2016-06-27 11:35:43 -0700 | [diff] [blame] | 242 | 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] | 243 | { |
| 244 | return snprintf(buf, PAGE_SIZE, "%u\n", state->is_big_cluster); |
| 245 | } |
| 246 | |
Olav Haugan | e3a3db9 | 2016-06-27 11:35:43 -0700 | [diff] [blame] | 247 | 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] | 248 | { |
| 249 | return snprintf(buf, PAGE_SIZE, "%u\n", state->need_cpus); |
| 250 | } |
| 251 | |
Olav Haugan | 833926cb | 2016-06-27 11:38:06 -0700 | [diff] [blame] | 252 | 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] | 253 | { |
Olav Haugan | 833926cb | 2016-06-27 11:38:06 -0700 | [diff] [blame] | 254 | return snprintf(buf, PAGE_SIZE, "%u\n", state->active_cpus); |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 255 | } |
| 256 | |
Olav Haugan | e3a3db9 | 2016-06-27 11:35:43 -0700 | [diff] [blame] | 257 | 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] | 258 | { |
| 259 | struct cpu_data *c; |
Olav Haugan | e3a3db9 | 2016-06-27 11:35:43 -0700 | [diff] [blame] | 260 | struct cluster_data *cluster; |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 261 | ssize_t count = 0; |
| 262 | unsigned int cpu; |
| 263 | |
Pavankumar Kondeti | a207d5c | 2017-04-13 18:44:24 +0530 | [diff] [blame] | 264 | spin_lock_irq(&state_lock); |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 265 | for_each_possible_cpu(cpu) { |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 266 | c = &per_cpu(cpu_state, cpu); |
Olav Haugan | e3a3db9 | 2016-06-27 11:35:43 -0700 | [diff] [blame] | 267 | cluster = c->cluster; |
| 268 | if (!cluster || !cluster->inited) |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 269 | continue; |
Olav Haugan | e3a3db9 | 2016-06-27 11:35:43 -0700 | [diff] [blame] | 270 | |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 271 | count += snprintf(buf + count, PAGE_SIZE - count, |
Olav Haugan | 833926cb | 2016-06-27 11:38:06 -0700 | [diff] [blame] | 272 | "CPU%u\n", cpu); |
| 273 | count += snprintf(buf + count, PAGE_SIZE - count, |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 274 | "\tCPU: %u\n", c->cpu); |
| 275 | count += snprintf(buf + count, PAGE_SIZE - count, |
Pavankumar Kondeti | 85dfbfa | 2017-03-17 15:29:13 +0530 | [diff] [blame] | 276 | "\tOnline: %u\n", |
| 277 | cpu_online(c->cpu)); |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 278 | count += snprintf(buf + count, PAGE_SIZE - count, |
Pavankumar Kondeti | fbfa7b3 | 2017-03-17 15:52:30 +0530 | [diff] [blame] | 279 | "\tIsolated: %u\n", |
| 280 | cpu_isolated(c->cpu)); |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 281 | count += snprintf(buf + count, PAGE_SIZE - count, |
Olav Haugan | e3a3db9 | 2016-06-27 11:35:43 -0700 | [diff] [blame] | 282 | "\tFirst CPU: %u\n", |
| 283 | cluster->first_cpu); |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 284 | count += snprintf(buf + count, PAGE_SIZE - count, |
| 285 | "\tBusy%%: %u\n", c->busy); |
| 286 | count += snprintf(buf + count, PAGE_SIZE - count, |
| 287 | "\tIs busy: %u\n", c->is_busy); |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 288 | count += snprintf(buf + count, PAGE_SIZE - count, |
Pavankumar Kondeti | 63877bc | 2017-02-16 08:25:21 +0530 | [diff] [blame] | 289 | "\tNot preferred: %u\n", |
| 290 | c->not_preferred); |
| 291 | count += snprintf(buf + count, PAGE_SIZE - count, |
Olav Haugan | e3a3db9 | 2016-06-27 11:35:43 -0700 | [diff] [blame] | 292 | "\tNr running: %u\n", cluster->nrrun); |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 293 | count += snprintf(buf + count, PAGE_SIZE - count, |
Olav Haugan | 833926cb | 2016-06-27 11:38:06 -0700 | [diff] [blame] | 294 | "\tActive CPUs: %u\n", get_active_cpu_count(cluster)); |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 295 | count += snprintf(buf + count, PAGE_SIZE - count, |
Olav Haugan | e3a3db9 | 2016-06-27 11:35:43 -0700 | [diff] [blame] | 296 | "\tNeed CPUs: %u\n", cluster->need_cpus); |
Olav Haugan | 833926cb | 2016-06-27 11:38:06 -0700 | [diff] [blame] | 297 | count += snprintf(buf + count, PAGE_SIZE - count, |
Pavankumar Kondeti | 32b2dd0 | 2017-04-13 16:58:57 +0530 | [diff] [blame^] | 298 | "\tNr isolated CPUs: %u\n", |
| 299 | cluster->nr_isolated_cpus); |
| 300 | count += snprintf(buf + count, PAGE_SIZE - count, |
Olav Haugan | 833926cb | 2016-06-27 11:38:06 -0700 | [diff] [blame] | 301 | "\tBoost: %u\n", (unsigned int) cluster->boost); |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 302 | } |
Pavankumar Kondeti | a207d5c | 2017-04-13 18:44:24 +0530 | [diff] [blame] | 303 | spin_unlock_irq(&state_lock); |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 304 | |
| 305 | return count; |
| 306 | } |
| 307 | |
Olav Haugan | e3a3db9 | 2016-06-27 11:35:43 -0700 | [diff] [blame] | 308 | static ssize_t store_not_preferred(struct cluster_data *state, |
| 309 | const char *buf, size_t count) |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 310 | { |
| 311 | struct cpu_data *c; |
Olav Haugan | e3a3db9 | 2016-06-27 11:35:43 -0700 | [diff] [blame] | 312 | unsigned int i; |
| 313 | unsigned int val[MAX_CPUS_PER_CLUSTER]; |
| 314 | unsigned long flags; |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 315 | int ret; |
| 316 | |
| 317 | ret = sscanf(buf, "%u %u %u %u\n", &val[0], &val[1], &val[2], &val[3]); |
Pavankumar Kondeti | 63877bc | 2017-02-16 08:25:21 +0530 | [diff] [blame] | 318 | if (ret != state->num_cpus) |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 319 | return -EINVAL; |
| 320 | |
Olav Haugan | e3a3db9 | 2016-06-27 11:35:43 -0700 | [diff] [blame] | 321 | spin_lock_irqsave(&state_lock, flags); |
Pavankumar Kondeti | 63877bc | 2017-02-16 08:25:21 +0530 | [diff] [blame] | 322 | for (i = 0; i < state->num_cpus; i++) { |
| 323 | c = &per_cpu(cpu_state, i + state->first_cpu); |
| 324 | c->not_preferred = val[i]; |
| 325 | } |
Olav Haugan | e3a3db9 | 2016-06-27 11:35:43 -0700 | [diff] [blame] | 326 | spin_unlock_irqrestore(&state_lock, flags); |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 327 | |
| 328 | return count; |
| 329 | } |
| 330 | |
Olav Haugan | e3a3db9 | 2016-06-27 11:35:43 -0700 | [diff] [blame] | 331 | 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] | 332 | { |
| 333 | struct cpu_data *c; |
| 334 | ssize_t count = 0; |
Olav Haugan | e3a3db9 | 2016-06-27 11:35:43 -0700 | [diff] [blame] | 335 | unsigned long flags; |
Pavankumar Kondeti | 63877bc | 2017-02-16 08:25:21 +0530 | [diff] [blame] | 336 | int i; |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 337 | |
Olav Haugan | e3a3db9 | 2016-06-27 11:35:43 -0700 | [diff] [blame] | 338 | spin_lock_irqsave(&state_lock, flags); |
Pavankumar Kondeti | 63877bc | 2017-02-16 08:25:21 +0530 | [diff] [blame] | 339 | for (i = 0; i < state->num_cpus; i++) { |
| 340 | c = &per_cpu(cpu_state, i + state->first_cpu); |
| 341 | count += scnprintf(buf + count, PAGE_SIZE - count, |
| 342 | "CPU#%d: %u\n", c->cpu, c->not_preferred); |
| 343 | } |
Olav Haugan | e3a3db9 | 2016-06-27 11:35:43 -0700 | [diff] [blame] | 344 | spin_unlock_irqrestore(&state_lock, flags); |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 345 | |
| 346 | return count; |
| 347 | } |
| 348 | |
Olav Haugan | e3a3db9 | 2016-06-27 11:35:43 -0700 | [diff] [blame] | 349 | |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 350 | struct core_ctl_attr { |
| 351 | struct attribute attr; |
Olav Haugan | e3a3db9 | 2016-06-27 11:35:43 -0700 | [diff] [blame] | 352 | ssize_t (*show)(const struct cluster_data *, char *); |
| 353 | ssize_t (*store)(struct cluster_data *, const char *, size_t count); |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 354 | }; |
| 355 | |
| 356 | #define core_ctl_attr_ro(_name) \ |
| 357 | static struct core_ctl_attr _name = \ |
| 358 | __ATTR(_name, 0444, show_##_name, NULL) |
| 359 | |
| 360 | #define core_ctl_attr_rw(_name) \ |
| 361 | static struct core_ctl_attr _name = \ |
| 362 | __ATTR(_name, 0644, show_##_name, store_##_name) |
| 363 | |
| 364 | core_ctl_attr_rw(min_cpus); |
| 365 | core_ctl_attr_rw(max_cpus); |
| 366 | core_ctl_attr_rw(offline_delay_ms); |
| 367 | core_ctl_attr_rw(busy_up_thres); |
| 368 | core_ctl_attr_rw(busy_down_thres); |
| 369 | core_ctl_attr_rw(task_thres); |
| 370 | core_ctl_attr_rw(is_big_cluster); |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 371 | core_ctl_attr_ro(need_cpus); |
Olav Haugan | 833926cb | 2016-06-27 11:38:06 -0700 | [diff] [blame] | 372 | core_ctl_attr_ro(active_cpus); |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 373 | core_ctl_attr_ro(global_state); |
| 374 | core_ctl_attr_rw(not_preferred); |
| 375 | |
| 376 | static struct attribute *default_attrs[] = { |
| 377 | &min_cpus.attr, |
| 378 | &max_cpus.attr, |
| 379 | &offline_delay_ms.attr, |
| 380 | &busy_up_thres.attr, |
| 381 | &busy_down_thres.attr, |
| 382 | &task_thres.attr, |
| 383 | &is_big_cluster.attr, |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 384 | &need_cpus.attr, |
Olav Haugan | 833926cb | 2016-06-27 11:38:06 -0700 | [diff] [blame] | 385 | &active_cpus.attr, |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 386 | &global_state.attr, |
| 387 | ¬_preferred.attr, |
| 388 | NULL |
| 389 | }; |
| 390 | |
Olav Haugan | e3a3db9 | 2016-06-27 11:35:43 -0700 | [diff] [blame] | 391 | #define to_cluster_data(k) container_of(k, struct cluster_data, kobj) |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 392 | #define to_attr(a) container_of(a, struct core_ctl_attr, attr) |
| 393 | static ssize_t show(struct kobject *kobj, struct attribute *attr, char *buf) |
| 394 | { |
Olav Haugan | e3a3db9 | 2016-06-27 11:35:43 -0700 | [diff] [blame] | 395 | struct cluster_data *data = to_cluster_data(kobj); |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 396 | struct core_ctl_attr *cattr = to_attr(attr); |
| 397 | ssize_t ret = -EIO; |
| 398 | |
| 399 | if (cattr->show) |
| 400 | ret = cattr->show(data, buf); |
| 401 | |
| 402 | return ret; |
| 403 | } |
| 404 | |
| 405 | static ssize_t store(struct kobject *kobj, struct attribute *attr, |
| 406 | const char *buf, size_t count) |
| 407 | { |
Olav Haugan | e3a3db9 | 2016-06-27 11:35:43 -0700 | [diff] [blame] | 408 | struct cluster_data *data = to_cluster_data(kobj); |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 409 | struct core_ctl_attr *cattr = to_attr(attr); |
| 410 | ssize_t ret = -EIO; |
| 411 | |
| 412 | if (cattr->store) |
| 413 | ret = cattr->store(data, buf, count); |
| 414 | |
| 415 | return ret; |
| 416 | } |
| 417 | |
| 418 | static const struct sysfs_ops sysfs_ops = { |
| 419 | .show = show, |
| 420 | .store = store, |
| 421 | }; |
| 422 | |
| 423 | static struct kobj_type ktype_core_ctl = { |
| 424 | .sysfs_ops = &sysfs_ops, |
| 425 | .default_attrs = default_attrs, |
| 426 | }; |
| 427 | |
| 428 | /* ==================== runqueue based core count =================== */ |
| 429 | |
| 430 | #define RQ_AVG_TOLERANCE 2 |
| 431 | #define RQ_AVG_DEFAULT_MS 20 |
| 432 | #define NR_RUNNING_TOLERANCE 5 |
| 433 | static unsigned int rq_avg_period_ms = RQ_AVG_DEFAULT_MS; |
| 434 | |
| 435 | static s64 rq_avg_timestamp_ms; |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 436 | |
| 437 | static void update_running_avg(bool trigger_update) |
| 438 | { |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 439 | int avg, iowait_avg, big_avg, old_nrrun; |
| 440 | s64 now; |
| 441 | unsigned long flags; |
Olav Haugan | e3a3db9 | 2016-06-27 11:35:43 -0700 | [diff] [blame] | 442 | struct cluster_data *cluster; |
| 443 | unsigned int index = 0; |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 444 | |
| 445 | spin_lock_irqsave(&state_lock, flags); |
| 446 | |
| 447 | now = ktime_to_ms(ktime_get()); |
| 448 | if (now - rq_avg_timestamp_ms < rq_avg_period_ms - RQ_AVG_TOLERANCE) { |
| 449 | spin_unlock_irqrestore(&state_lock, flags); |
| 450 | return; |
| 451 | } |
| 452 | rq_avg_timestamp_ms = now; |
| 453 | sched_get_nr_running_avg(&avg, &iowait_avg, &big_avg); |
| 454 | |
| 455 | spin_unlock_irqrestore(&state_lock, flags); |
| 456 | |
| 457 | /* |
| 458 | * Round up to the next integer if the average nr running tasks |
| 459 | * is within NR_RUNNING_TOLERANCE/100 of the next integer. |
| 460 | * If normal rounding up is used, it will allow a transient task |
| 461 | * to trigger online event. By the time core is onlined, the task |
| 462 | * has finished. |
| 463 | * Rounding to closest suffers same problem because scheduler |
| 464 | * might only provide running stats per jiffy, and a transient |
| 465 | * task could skew the number for one jiffy. If core control |
| 466 | * samples every 2 jiffies, it will observe 0.5 additional running |
| 467 | * average which rounds up to 1 task. |
| 468 | */ |
| 469 | avg = (avg + NR_RUNNING_TOLERANCE) / 100; |
| 470 | big_avg = (big_avg + NR_RUNNING_TOLERANCE) / 100; |
| 471 | |
Olav Haugan | e3a3db9 | 2016-06-27 11:35:43 -0700 | [diff] [blame] | 472 | for_each_cluster(cluster, index) { |
| 473 | if (!cluster->inited) |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 474 | continue; |
Olav Haugan | e3a3db9 | 2016-06-27 11:35:43 -0700 | [diff] [blame] | 475 | old_nrrun = cluster->nrrun; |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 476 | /* |
| 477 | * Big cluster only need to take care of big tasks, but if |
| 478 | * there are not enough big cores, big tasks need to be run |
| 479 | * on little as well. Thus for little's runqueue stat, it |
| 480 | * has to use overall runqueue average, or derive what big |
| 481 | * tasks would have to be run on little. The latter approach |
| 482 | * is not easy to get given core control reacts much slower |
| 483 | * than scheduler, and can't predict scheduler's behavior. |
| 484 | */ |
Olav Haugan | e3a3db9 | 2016-06-27 11:35:43 -0700 | [diff] [blame] | 485 | cluster->nrrun = cluster->is_big_cluster ? big_avg : avg; |
| 486 | if (cluster->nrrun != old_nrrun) { |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 487 | if (trigger_update) |
Olav Haugan | e3a3db9 | 2016-06-27 11:35:43 -0700 | [diff] [blame] | 488 | apply_need(cluster); |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 489 | else |
Olav Haugan | e3a3db9 | 2016-06-27 11:35:43 -0700 | [diff] [blame] | 490 | cluster->nrrun_changed = true; |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 491 | } |
| 492 | } |
Olav Haugan | e3a3db9 | 2016-06-27 11:35:43 -0700 | [diff] [blame] | 493 | return; |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 494 | } |
| 495 | |
| 496 | /* adjust needed CPUs based on current runqueue information */ |
Olav Haugan | e3a3db9 | 2016-06-27 11:35:43 -0700 | [diff] [blame] | 497 | static unsigned int apply_task_need(const struct cluster_data *cluster, |
| 498 | unsigned int new_need) |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 499 | { |
Olav Haugan | 833926cb | 2016-06-27 11:38:06 -0700 | [diff] [blame] | 500 | /* unisolate all cores if there are enough tasks */ |
Olav Haugan | e3a3db9 | 2016-06-27 11:35:43 -0700 | [diff] [blame] | 501 | if (cluster->nrrun >= cluster->task_thres) |
| 502 | return cluster->num_cpus; |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 503 | |
Olav Haugan | 833926cb | 2016-06-27 11:38:06 -0700 | [diff] [blame] | 504 | /* only unisolate more cores if there are tasks to run */ |
Olav Haugan | e3a3db9 | 2016-06-27 11:35:43 -0700 | [diff] [blame] | 505 | if (cluster->nrrun > new_need) |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 506 | return new_need + 1; |
| 507 | |
| 508 | return new_need; |
| 509 | } |
| 510 | |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 511 | /* ======================= load based core count ====================== */ |
| 512 | |
Olav Haugan | e3a3db9 | 2016-06-27 11:35:43 -0700 | [diff] [blame] | 513 | static unsigned int apply_limits(const struct cluster_data *cluster, |
| 514 | unsigned int need_cpus) |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 515 | { |
Olav Haugan | e3a3db9 | 2016-06-27 11:35:43 -0700 | [diff] [blame] | 516 | return min(max(cluster->min_cpus, need_cpus), cluster->max_cpus); |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 517 | } |
| 518 | |
Olav Haugan | 833926cb | 2016-06-27 11:38:06 -0700 | [diff] [blame] | 519 | static unsigned int get_active_cpu_count(const struct cluster_data *cluster) |
| 520 | { |
| 521 | return cluster->num_cpus - |
| 522 | sched_isolate_count(&cluster->cpu_mask, true); |
| 523 | } |
| 524 | |
| 525 | static bool is_active(const struct cpu_data *state) |
| 526 | { |
Pavankumar Kondeti | 85dfbfa | 2017-03-17 15:29:13 +0530 | [diff] [blame] | 527 | return cpu_online(state->cpu) && !cpu_isolated(state->cpu); |
Olav Haugan | 833926cb | 2016-06-27 11:38:06 -0700 | [diff] [blame] | 528 | } |
| 529 | |
| 530 | static bool adjustment_possible(const struct cluster_data *cluster, |
| 531 | unsigned int need) |
| 532 | { |
| 533 | return (need < cluster->active_cpus || (need > cluster->active_cpus && |
Pavankumar Kondeti | 32b2dd0 | 2017-04-13 16:58:57 +0530 | [diff] [blame^] | 534 | cluster->nr_isolated_cpus)); |
Olav Haugan | 833926cb | 2016-06-27 11:38:06 -0700 | [diff] [blame] | 535 | } |
| 536 | |
Olav Haugan | e3a3db9 | 2016-06-27 11:35:43 -0700 | [diff] [blame] | 537 | static bool eval_need(struct cluster_data *cluster) |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 538 | { |
| 539 | unsigned long flags; |
| 540 | struct cpu_data *c; |
| 541 | unsigned int need_cpus = 0, last_need, thres_idx; |
| 542 | int ret = 0; |
| 543 | bool need_flag = false; |
Olav Haugan | 833926cb | 2016-06-27 11:38:06 -0700 | [diff] [blame] | 544 | unsigned int new_need; |
Pavankumar Kondeti | 0824857 | 2017-04-13 15:45:11 +0530 | [diff] [blame] | 545 | s64 now, elapsed; |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 546 | |
Olav Haugan | e3a3db9 | 2016-06-27 11:35:43 -0700 | [diff] [blame] | 547 | if (unlikely(!cluster->inited)) |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 548 | return 0; |
| 549 | |
| 550 | spin_lock_irqsave(&state_lock, flags); |
Olav Haugan | 833926cb | 2016-06-27 11:38:06 -0700 | [diff] [blame] | 551 | |
| 552 | if (cluster->boost) { |
| 553 | need_cpus = cluster->max_cpus; |
| 554 | } else { |
Pavankumar Kondeti | 0824857 | 2017-04-13 15:45:11 +0530 | [diff] [blame] | 555 | cluster->active_cpus = get_active_cpu_count(cluster); |
| 556 | thres_idx = cluster->active_cpus ? cluster->active_cpus - 1 : 0; |
Olav Haugan | 833926cb | 2016-06-27 11:38:06 -0700 | [diff] [blame] | 557 | list_for_each_entry(c, &cluster->lru, sib) { |
| 558 | if (c->busy >= cluster->busy_up_thres[thres_idx]) |
| 559 | c->is_busy = true; |
| 560 | else if (c->busy < cluster->busy_down_thres[thres_idx]) |
| 561 | c->is_busy = false; |
| 562 | need_cpus += c->is_busy; |
| 563 | } |
| 564 | need_cpus = apply_task_need(cluster, need_cpus); |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 565 | } |
Olav Haugan | 833926cb | 2016-06-27 11:38:06 -0700 | [diff] [blame] | 566 | new_need = apply_limits(cluster, need_cpus); |
| 567 | need_flag = adjustment_possible(cluster, new_need); |
| 568 | |
Olav Haugan | e3a3db9 | 2016-06-27 11:35:43 -0700 | [diff] [blame] | 569 | last_need = cluster->need_cpus; |
Pavankumar Kondeti | 83236e3 | 2017-02-10 09:58:58 +0530 | [diff] [blame] | 570 | now = ktime_to_ms(ktime_get()); |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 571 | |
Pavankumar Kondeti | 0824857 | 2017-04-13 15:45:11 +0530 | [diff] [blame] | 572 | if (new_need > cluster->active_cpus) { |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 573 | ret = 1; |
Pavankumar Kondeti | 0824857 | 2017-04-13 15:45:11 +0530 | [diff] [blame] | 574 | } else { |
| 575 | if (new_need == last_need) { |
| 576 | cluster->need_ts = now; |
| 577 | spin_unlock_irqrestore(&state_lock, flags); |
| 578 | return 0; |
| 579 | } |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 580 | |
Pavankumar Kondeti | 0824857 | 2017-04-13 15:45:11 +0530 | [diff] [blame] | 581 | elapsed = now - cluster->need_ts; |
Olav Haugan | 833926cb | 2016-06-27 11:38:06 -0700 | [diff] [blame] | 582 | ret = elapsed >= cluster->offline_delay_ms; |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 583 | } |
| 584 | |
Pavankumar Kondeti | 83236e3 | 2017-02-10 09:58:58 +0530 | [diff] [blame] | 585 | if (ret) { |
| 586 | cluster->need_ts = now; |
| 587 | cluster->need_cpus = new_need; |
| 588 | } |
Pavankumar Kondeti | 0824857 | 2017-04-13 15:45:11 +0530 | [diff] [blame] | 589 | trace_core_ctl_eval_need(cluster->first_cpu, last_need, new_need, |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 590 | ret && need_flag); |
| 591 | spin_unlock_irqrestore(&state_lock, flags); |
| 592 | |
| 593 | return ret && need_flag; |
| 594 | } |
| 595 | |
Olav Haugan | e3a3db9 | 2016-06-27 11:35:43 -0700 | [diff] [blame] | 596 | static void apply_need(struct cluster_data *cluster) |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 597 | { |
Olav Haugan | e3a3db9 | 2016-06-27 11:35:43 -0700 | [diff] [blame] | 598 | if (eval_need(cluster)) |
Olav Haugan | 833926cb | 2016-06-27 11:38:06 -0700 | [diff] [blame] | 599 | wake_up_core_ctl_thread(cluster); |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 600 | } |
| 601 | |
| 602 | static int core_ctl_set_busy(unsigned int cpu, unsigned int busy) |
| 603 | { |
| 604 | struct cpu_data *c = &per_cpu(cpu_state, cpu); |
Olav Haugan | e3a3db9 | 2016-06-27 11:35:43 -0700 | [diff] [blame] | 605 | struct cluster_data *cluster = c->cluster; |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 606 | unsigned int old_is_busy = c->is_busy; |
| 607 | |
Olav Haugan | e3a3db9 | 2016-06-27 11:35:43 -0700 | [diff] [blame] | 608 | if (!cluster || !cluster->inited) |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 609 | return 0; |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 610 | |
| 611 | update_running_avg(false); |
Olav Haugan | e3a3db9 | 2016-06-27 11:35:43 -0700 | [diff] [blame] | 612 | if (c->busy == busy && !cluster->nrrun_changed) |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 613 | return 0; |
| 614 | c->busy = busy; |
Olav Haugan | e3a3db9 | 2016-06-27 11:35:43 -0700 | [diff] [blame] | 615 | cluster->nrrun_changed = false; |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 616 | |
Olav Haugan | e3a3db9 | 2016-06-27 11:35:43 -0700 | [diff] [blame] | 617 | apply_need(cluster); |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 618 | trace_core_ctl_set_busy(cpu, busy, old_is_busy, c->is_busy); |
| 619 | return 0; |
| 620 | } |
| 621 | |
| 622 | /* ========================= core count enforcement ==================== */ |
| 623 | |
Olav Haugan | 833926cb | 2016-06-27 11:38:06 -0700 | [diff] [blame] | 624 | static void wake_up_core_ctl_thread(struct cluster_data *cluster) |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 625 | { |
| 626 | unsigned long flags; |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 627 | |
Olav Haugan | e3a3db9 | 2016-06-27 11:35:43 -0700 | [diff] [blame] | 628 | spin_lock_irqsave(&cluster->pending_lock, flags); |
| 629 | cluster->pending = true; |
| 630 | spin_unlock_irqrestore(&cluster->pending_lock, flags); |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 631 | |
Olav Haugan | 833926cb | 2016-06-27 11:38:06 -0700 | [diff] [blame] | 632 | wake_up_process_no_notif(cluster->core_ctl_thread); |
| 633 | } |
| 634 | |
| 635 | static u64 core_ctl_check_timestamp; |
| 636 | static u64 core_ctl_check_interval; |
| 637 | |
| 638 | static bool do_check(u64 wallclock) |
| 639 | { |
| 640 | bool do_check = false; |
| 641 | unsigned long flags; |
| 642 | |
| 643 | spin_lock_irqsave(&state_lock, flags); |
| 644 | if ((wallclock - core_ctl_check_timestamp) >= core_ctl_check_interval) { |
| 645 | core_ctl_check_timestamp = wallclock; |
| 646 | do_check = true; |
| 647 | } |
| 648 | spin_unlock_irqrestore(&state_lock, flags); |
| 649 | return do_check; |
| 650 | } |
| 651 | |
Olav Haugan | a024f47 | 2016-10-13 10:34:11 -0700 | [diff] [blame] | 652 | int core_ctl_set_boost(bool boost) |
Olav Haugan | 833926cb | 2016-06-27 11:38:06 -0700 | [diff] [blame] | 653 | { |
| 654 | unsigned int index = 0; |
| 655 | struct cluster_data *cluster; |
Olav Haugan | a024f47 | 2016-10-13 10:34:11 -0700 | [diff] [blame] | 656 | unsigned long flags; |
| 657 | int ret = 0; |
| 658 | bool boost_state_changed = false; |
Olav Haugan | 833926cb | 2016-06-27 11:38:06 -0700 | [diff] [blame] | 659 | |
Pavankumar Kondeti | d198dde | 2017-03-21 14:21:21 +0530 | [diff] [blame] | 660 | if (unlikely(!initialized)) |
| 661 | return 0; |
| 662 | |
Olav Haugan | a024f47 | 2016-10-13 10:34:11 -0700 | [diff] [blame] | 663 | spin_lock_irqsave(&state_lock, flags); |
Olav Haugan | 833926cb | 2016-06-27 11:38:06 -0700 | [diff] [blame] | 664 | for_each_cluster(cluster, index) { |
Olav Haugan | a024f47 | 2016-10-13 10:34:11 -0700 | [diff] [blame] | 665 | if (cluster->is_big_cluster) { |
| 666 | if (boost) { |
| 667 | boost_state_changed = !cluster->boost; |
| 668 | ++cluster->boost; |
| 669 | } else { |
| 670 | if (!cluster->boost) { |
| 671 | pr_err("Error turning off boost. Boost already turned off\n"); |
| 672 | ret = -EINVAL; |
| 673 | } else { |
| 674 | --cluster->boost; |
| 675 | boost_state_changed = !cluster->boost; |
| 676 | } |
| 677 | } |
| 678 | break; |
Olav Haugan | 833926cb | 2016-06-27 11:38:06 -0700 | [diff] [blame] | 679 | } |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 680 | } |
Olav Haugan | a024f47 | 2016-10-13 10:34:11 -0700 | [diff] [blame] | 681 | spin_unlock_irqrestore(&state_lock, flags); |
| 682 | |
| 683 | if (boost_state_changed) |
| 684 | apply_need(cluster); |
| 685 | |
| 686 | trace_core_ctl_set_boost(cluster->boost, ret); |
| 687 | |
| 688 | return ret; |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 689 | } |
Olav Haugan | 4d77e57 | 2016-11-14 16:14:23 -0800 | [diff] [blame] | 690 | EXPORT_SYMBOL(core_ctl_set_boost); |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 691 | |
Olav Haugan | 833926cb | 2016-06-27 11:38:06 -0700 | [diff] [blame] | 692 | void core_ctl_check(u64 wallclock) |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 693 | { |
Olav Haugan | 833926cb | 2016-06-27 11:38:06 -0700 | [diff] [blame] | 694 | if (unlikely(!initialized)) |
| 695 | return; |
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 | if (do_check(wallclock)) { |
| 698 | unsigned int index = 0; |
| 699 | struct cluster_data *cluster; |
Olav Haugan | e3a3db9 | 2016-06-27 11:35:43 -0700 | [diff] [blame] | 700 | |
Olav Haugan | 833926cb | 2016-06-27 11:38:06 -0700 | [diff] [blame] | 701 | update_running_avg(true); |
| 702 | |
| 703 | for_each_cluster(cluster, index) { |
| 704 | if (eval_need(cluster)) |
| 705 | wake_up_core_ctl_thread(cluster); |
| 706 | } |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 707 | } |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 708 | } |
| 709 | |
Olav Haugan | 833926cb | 2016-06-27 11:38:06 -0700 | [diff] [blame] | 710 | static void move_cpu_lru(struct cpu_data *cpu_data) |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 711 | { |
Olav Haugan | 833926cb | 2016-06-27 11:38:06 -0700 | [diff] [blame] | 712 | unsigned long flags; |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 713 | |
Olav Haugan | 833926cb | 2016-06-27 11:38:06 -0700 | [diff] [blame] | 714 | spin_lock_irqsave(&state_lock, flags); |
| 715 | list_del(&cpu_data->sib); |
| 716 | list_add_tail(&cpu_data->sib, &cpu_data->cluster->lru); |
| 717 | spin_unlock_irqrestore(&state_lock, flags); |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 718 | } |
| 719 | |
Olav Haugan | 833926cb | 2016-06-27 11:38:06 -0700 | [diff] [blame] | 720 | static void try_to_isolate(struct cluster_data *cluster, unsigned int need) |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 721 | { |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 722 | struct cpu_data *c, *tmp; |
Olav Haugan | 8597073 | 2016-11-08 13:45:01 -0800 | [diff] [blame] | 723 | unsigned long flags; |
| 724 | unsigned int num_cpus = cluster->num_cpus; |
Pavankumar Kondeti | 32b2dd0 | 2017-04-13 16:58:57 +0530 | [diff] [blame^] | 725 | unsigned int nr_isolated = 0; |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 726 | |
Olav Haugan | 8597073 | 2016-11-08 13:45:01 -0800 | [diff] [blame] | 727 | /* |
| 728 | * Protect against entry being removed (and added at tail) by other |
| 729 | * thread (hotplug). |
| 730 | */ |
| 731 | spin_lock_irqsave(&state_lock, flags); |
Olav Haugan | 833926cb | 2016-06-27 11:38:06 -0700 | [diff] [blame] | 732 | list_for_each_entry_safe(c, tmp, &cluster->lru, sib) { |
Olav Haugan | 8597073 | 2016-11-08 13:45:01 -0800 | [diff] [blame] | 733 | if (!num_cpus--) |
| 734 | break; |
| 735 | |
Olav Haugan | 833926cb | 2016-06-27 11:38:06 -0700 | [diff] [blame] | 736 | if (!is_active(c)) |
| 737 | continue; |
| 738 | if (cluster->active_cpus == need) |
| 739 | break; |
| 740 | /* Don't offline busy CPUs. */ |
| 741 | if (c->is_busy) |
| 742 | continue; |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 743 | |
Olav Haugan | 8597073 | 2016-11-08 13:45:01 -0800 | [diff] [blame] | 744 | spin_unlock_irqrestore(&state_lock, flags); |
| 745 | |
Olav Haugan | 833926cb | 2016-06-27 11:38:06 -0700 | [diff] [blame] | 746 | pr_debug("Trying to isolate CPU%u\n", c->cpu); |
| 747 | if (!sched_isolate_cpu(c->cpu)) { |
| 748 | c->isolated_by_us = true; |
| 749 | move_cpu_lru(c); |
Pavankumar Kondeti | 32b2dd0 | 2017-04-13 16:58:57 +0530 | [diff] [blame^] | 750 | nr_isolated++; |
Olav Haugan | 833926cb | 2016-06-27 11:38:06 -0700 | [diff] [blame] | 751 | } else { |
| 752 | pr_debug("Unable to isolate CPU%u\n", c->cpu); |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 753 | } |
Olav Haugan | 833926cb | 2016-06-27 11:38:06 -0700 | [diff] [blame] | 754 | cluster->active_cpus = get_active_cpu_count(cluster); |
Olav Haugan | 8597073 | 2016-11-08 13:45:01 -0800 | [diff] [blame] | 755 | spin_lock_irqsave(&state_lock, flags); |
Olav Haugan | 833926cb | 2016-06-27 11:38:06 -0700 | [diff] [blame] | 756 | } |
Pavankumar Kondeti | 32b2dd0 | 2017-04-13 16:58:57 +0530 | [diff] [blame^] | 757 | cluster->nr_isolated_cpus += nr_isolated; |
Olav Haugan | 8597073 | 2016-11-08 13:45:01 -0800 | [diff] [blame] | 758 | spin_unlock_irqrestore(&state_lock, flags); |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 759 | |
Olav Haugan | 833926cb | 2016-06-27 11:38:06 -0700 | [diff] [blame] | 760 | /* |
| 761 | * If the number of active CPUs is within the limits, then |
| 762 | * don't force isolation of any busy CPUs. |
| 763 | */ |
| 764 | if (cluster->active_cpus <= cluster->max_cpus) |
| 765 | return; |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 766 | |
Pavankumar Kondeti | 32b2dd0 | 2017-04-13 16:58:57 +0530 | [diff] [blame^] | 767 | nr_isolated = 0; |
Olav Haugan | 8597073 | 2016-11-08 13:45:01 -0800 | [diff] [blame] | 768 | num_cpus = cluster->num_cpus; |
| 769 | spin_lock_irqsave(&state_lock, flags); |
Olav Haugan | 833926cb | 2016-06-27 11:38:06 -0700 | [diff] [blame] | 770 | list_for_each_entry_safe(c, tmp, &cluster->lru, sib) { |
Olav Haugan | 8597073 | 2016-11-08 13:45:01 -0800 | [diff] [blame] | 771 | if (!num_cpus--) |
| 772 | break; |
| 773 | |
Olav Haugan | 833926cb | 2016-06-27 11:38:06 -0700 | [diff] [blame] | 774 | if (!is_active(c)) |
| 775 | continue; |
| 776 | if (cluster->active_cpus <= cluster->max_cpus) |
| 777 | break; |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 778 | |
Olav Haugan | 8597073 | 2016-11-08 13:45:01 -0800 | [diff] [blame] | 779 | spin_unlock_irqrestore(&state_lock, flags); |
| 780 | |
Olav Haugan | 833926cb | 2016-06-27 11:38:06 -0700 | [diff] [blame] | 781 | pr_debug("Trying to isolate CPU%u\n", c->cpu); |
| 782 | if (!sched_isolate_cpu(c->cpu)) { |
| 783 | c->isolated_by_us = true; |
| 784 | move_cpu_lru(c); |
Pavankumar Kondeti | 32b2dd0 | 2017-04-13 16:58:57 +0530 | [diff] [blame^] | 785 | nr_isolated++; |
Olav Haugan | 833926cb | 2016-06-27 11:38:06 -0700 | [diff] [blame] | 786 | } else { |
| 787 | pr_debug("Unable to isolate CPU%u\n", c->cpu); |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 788 | } |
Olav Haugan | 833926cb | 2016-06-27 11:38:06 -0700 | [diff] [blame] | 789 | cluster->active_cpus = get_active_cpu_count(cluster); |
Olav Haugan | 8597073 | 2016-11-08 13:45:01 -0800 | [diff] [blame] | 790 | spin_lock_irqsave(&state_lock, flags); |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 791 | } |
Pavankumar Kondeti | 32b2dd0 | 2017-04-13 16:58:57 +0530 | [diff] [blame^] | 792 | cluster->nr_isolated_cpus += nr_isolated; |
Olav Haugan | 8597073 | 2016-11-08 13:45:01 -0800 | [diff] [blame] | 793 | spin_unlock_irqrestore(&state_lock, flags); |
| 794 | |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 795 | } |
| 796 | |
Olav Haugan | 833926cb | 2016-06-27 11:38:06 -0700 | [diff] [blame] | 797 | static void __try_to_unisolate(struct cluster_data *cluster, |
| 798 | unsigned int need, bool force) |
| 799 | { |
| 800 | struct cpu_data *c, *tmp; |
Olav Haugan | 8597073 | 2016-11-08 13:45:01 -0800 | [diff] [blame] | 801 | unsigned long flags; |
| 802 | unsigned int num_cpus = cluster->num_cpus; |
Pavankumar Kondeti | 32b2dd0 | 2017-04-13 16:58:57 +0530 | [diff] [blame^] | 803 | unsigned int nr_unisolated = 0; |
Olav Haugan | 833926cb | 2016-06-27 11:38:06 -0700 | [diff] [blame] | 804 | |
Olav Haugan | 8597073 | 2016-11-08 13:45:01 -0800 | [diff] [blame] | 805 | /* |
| 806 | * Protect against entry being removed (and added at tail) by other |
| 807 | * thread (hotplug). |
| 808 | */ |
| 809 | spin_lock_irqsave(&state_lock, flags); |
Olav Haugan | 833926cb | 2016-06-27 11:38:06 -0700 | [diff] [blame] | 810 | list_for_each_entry_safe(c, tmp, &cluster->lru, sib) { |
Olav Haugan | 8597073 | 2016-11-08 13:45:01 -0800 | [diff] [blame] | 811 | if (!num_cpus--) |
| 812 | break; |
| 813 | |
Olav Haugan | 833926cb | 2016-06-27 11:38:06 -0700 | [diff] [blame] | 814 | if (!c->isolated_by_us) |
| 815 | continue; |
Pavankumar Kondeti | 85dfbfa | 2017-03-17 15:29:13 +0530 | [diff] [blame] | 816 | if ((cpu_online(c->cpu) && !cpu_isolated(c->cpu)) || |
Olav Haugan | 833926cb | 2016-06-27 11:38:06 -0700 | [diff] [blame] | 817 | (!force && c->not_preferred)) |
| 818 | continue; |
| 819 | if (cluster->active_cpus == need) |
| 820 | break; |
| 821 | |
Olav Haugan | 8597073 | 2016-11-08 13:45:01 -0800 | [diff] [blame] | 822 | spin_unlock_irqrestore(&state_lock, flags); |
| 823 | |
Olav Haugan | 833926cb | 2016-06-27 11:38:06 -0700 | [diff] [blame] | 824 | pr_debug("Trying to unisolate CPU%u\n", c->cpu); |
| 825 | if (!sched_unisolate_cpu(c->cpu)) { |
| 826 | c->isolated_by_us = false; |
| 827 | move_cpu_lru(c); |
Pavankumar Kondeti | 32b2dd0 | 2017-04-13 16:58:57 +0530 | [diff] [blame^] | 828 | nr_unisolated++; |
Olav Haugan | 833926cb | 2016-06-27 11:38:06 -0700 | [diff] [blame] | 829 | } else { |
| 830 | pr_debug("Unable to unisolate CPU%u\n", c->cpu); |
| 831 | } |
| 832 | cluster->active_cpus = get_active_cpu_count(cluster); |
Olav Haugan | 8597073 | 2016-11-08 13:45:01 -0800 | [diff] [blame] | 833 | spin_lock_irqsave(&state_lock, flags); |
Olav Haugan | 833926cb | 2016-06-27 11:38:06 -0700 | [diff] [blame] | 834 | } |
Pavankumar Kondeti | 32b2dd0 | 2017-04-13 16:58:57 +0530 | [diff] [blame^] | 835 | cluster->nr_isolated_cpus -= nr_unisolated; |
Olav Haugan | 8597073 | 2016-11-08 13:45:01 -0800 | [diff] [blame] | 836 | spin_unlock_irqrestore(&state_lock, flags); |
Olav Haugan | 833926cb | 2016-06-27 11:38:06 -0700 | [diff] [blame] | 837 | } |
| 838 | |
| 839 | static void try_to_unisolate(struct cluster_data *cluster, unsigned int need) |
| 840 | { |
| 841 | bool force_use_non_preferred = false; |
| 842 | |
| 843 | __try_to_unisolate(cluster, need, force_use_non_preferred); |
| 844 | |
| 845 | if (cluster->active_cpus == need) |
| 846 | return; |
| 847 | |
| 848 | force_use_non_preferred = true; |
| 849 | __try_to_unisolate(cluster, need, force_use_non_preferred); |
| 850 | } |
| 851 | |
| 852 | static void __ref do_core_ctl(struct cluster_data *cluster) |
| 853 | { |
| 854 | unsigned int need; |
| 855 | |
| 856 | need = apply_limits(cluster, cluster->need_cpus); |
| 857 | |
| 858 | if (adjustment_possible(cluster, need)) { |
| 859 | pr_debug("Trying to adjust group %u from %u to %u\n", |
| 860 | cluster->first_cpu, cluster->active_cpus, need); |
| 861 | |
| 862 | if (cluster->active_cpus > need) |
| 863 | try_to_isolate(cluster, need); |
| 864 | else if (cluster->active_cpus < need) |
| 865 | try_to_unisolate(cluster, need); |
| 866 | } |
| 867 | } |
| 868 | |
| 869 | static int __ref try_core_ctl(void *data) |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 870 | { |
Olav Haugan | e3a3db9 | 2016-06-27 11:35:43 -0700 | [diff] [blame] | 871 | struct cluster_data *cluster = data; |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 872 | unsigned long flags; |
| 873 | |
| 874 | while (1) { |
| 875 | set_current_state(TASK_INTERRUPTIBLE); |
Olav Haugan | e3a3db9 | 2016-06-27 11:35:43 -0700 | [diff] [blame] | 876 | spin_lock_irqsave(&cluster->pending_lock, flags); |
| 877 | if (!cluster->pending) { |
| 878 | spin_unlock_irqrestore(&cluster->pending_lock, flags); |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 879 | schedule(); |
| 880 | if (kthread_should_stop()) |
| 881 | break; |
Olav Haugan | e3a3db9 | 2016-06-27 11:35:43 -0700 | [diff] [blame] | 882 | spin_lock_irqsave(&cluster->pending_lock, flags); |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 883 | } |
| 884 | set_current_state(TASK_RUNNING); |
Olav Haugan | e3a3db9 | 2016-06-27 11:35:43 -0700 | [diff] [blame] | 885 | cluster->pending = false; |
| 886 | spin_unlock_irqrestore(&cluster->pending_lock, flags); |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 887 | |
Olav Haugan | 833926cb | 2016-06-27 11:38:06 -0700 | [diff] [blame] | 888 | do_core_ctl(cluster); |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 889 | } |
| 890 | |
| 891 | return 0; |
| 892 | } |
| 893 | |
| 894 | static int __ref cpu_callback(struct notifier_block *nfb, |
| 895 | unsigned long action, void *hcpu) |
| 896 | { |
| 897 | uint32_t cpu = (uintptr_t)hcpu; |
| 898 | struct cpu_data *state = &per_cpu(cpu_state, cpu); |
Olav Haugan | e3a3db9 | 2016-06-27 11:35:43 -0700 | [diff] [blame] | 899 | struct cluster_data *cluster = state->cluster; |
Olav Haugan | 833926cb | 2016-06-27 11:38:06 -0700 | [diff] [blame] | 900 | unsigned int need; |
Pavankumar Kondeti | 32b2dd0 | 2017-04-13 16:58:57 +0530 | [diff] [blame^] | 901 | bool do_wakeup, unisolated = false; |
| 902 | unsigned long flags; |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 903 | |
Olav Haugan | e3a3db9 | 2016-06-27 11:35:43 -0700 | [diff] [blame] | 904 | if (unlikely(!cluster || !cluster->inited)) |
Pavankumar Kondeti | 046f223 | 2017-04-13 18:58:56 +0530 | [diff] [blame] | 905 | return NOTIFY_DONE; |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 906 | |
Olav Haugan | 1e8a44c | 2016-11-17 18:31:33 -0800 | [diff] [blame] | 907 | switch (action & ~CPU_TASKS_FROZEN) { |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 908 | case CPU_ONLINE: |
Olav Haugan | 833926cb | 2016-06-27 11:38:06 -0700 | [diff] [blame] | 909 | cluster->active_cpus = get_active_cpu_count(cluster); |
| 910 | |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 911 | /* |
| 912 | * Moving to the end of the list should only happen in |
| 913 | * CPU_ONLINE and not on CPU_UP_PREPARE to prevent an |
| 914 | * infinite list traversal when thermal (or other entities) |
| 915 | * reject trying to online CPUs. |
| 916 | */ |
Olav Haugan | 833926cb | 2016-06-27 11:38:06 -0700 | [diff] [blame] | 917 | move_cpu_lru(state); |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 918 | break; |
| 919 | |
| 920 | case CPU_DEAD: |
Olav Haugan | 833926cb | 2016-06-27 11:38:06 -0700 | [diff] [blame] | 921 | /* |
| 922 | * We don't want to have a CPU both offline and isolated. |
| 923 | * So unisolate a CPU that went down if it was isolated by us. |
| 924 | */ |
| 925 | if (state->isolated_by_us) { |
| 926 | sched_unisolate_cpu_unlocked(cpu); |
| 927 | state->isolated_by_us = false; |
Pavankumar Kondeti | 32b2dd0 | 2017-04-13 16:58:57 +0530 | [diff] [blame^] | 928 | unisolated = true; |
Olav Haugan | 833926cb | 2016-06-27 11:38:06 -0700 | [diff] [blame] | 929 | } |
| 930 | |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 931 | /* 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] | 932 | move_cpu_lru(state); |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 933 | |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 934 | state->busy = 0; |
Olav Haugan | 833926cb | 2016-06-27 11:38:06 -0700 | [diff] [blame] | 935 | cluster->active_cpus = get_active_cpu_count(cluster); |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 936 | break; |
Pavankumar Kondeti | 046f223 | 2017-04-13 18:58:56 +0530 | [diff] [blame] | 937 | default: |
| 938 | return NOTIFY_DONE; |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 939 | } |
| 940 | |
Olav Haugan | 833926cb | 2016-06-27 11:38:06 -0700 | [diff] [blame] | 941 | need = apply_limits(cluster, cluster->need_cpus); |
Pavankumar Kondeti | 32b2dd0 | 2017-04-13 16:58:57 +0530 | [diff] [blame^] | 942 | spin_lock_irqsave(&state_lock, flags); |
| 943 | if (unisolated) |
| 944 | cluster->nr_isolated_cpus--; |
| 945 | do_wakeup = adjustment_possible(cluster, need); |
| 946 | spin_unlock_irqrestore(&state_lock, flags); |
| 947 | if (do_wakeup) |
Olav Haugan | 833926cb | 2016-06-27 11:38:06 -0700 | [diff] [blame] | 948 | wake_up_core_ctl_thread(cluster); |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 949 | |
Pavankumar Kondeti | 046f223 | 2017-04-13 18:58:56 +0530 | [diff] [blame] | 950 | return NOTIFY_OK; |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 951 | } |
| 952 | |
| 953 | static struct notifier_block __refdata cpu_notifier = { |
| 954 | .notifier_call = cpu_callback, |
| 955 | }; |
| 956 | |
| 957 | /* ============================ init code ============================== */ |
| 958 | |
Pavankumar Kondeti | 1e951a7 | 2017-03-21 14:00:09 +0530 | [diff] [blame] | 959 | static cpumask_var_t core_ctl_disable_cpumask; |
| 960 | static bool core_ctl_disable_cpumask_present; |
| 961 | |
| 962 | static int __init core_ctl_disable_setup(char *str) |
| 963 | { |
| 964 | if (!*str) |
| 965 | return -EINVAL; |
| 966 | |
| 967 | alloc_bootmem_cpumask_var(&core_ctl_disable_cpumask); |
| 968 | |
| 969 | if (cpulist_parse(str, core_ctl_disable_cpumask) < 0) { |
| 970 | free_bootmem_cpumask_var(core_ctl_disable_cpumask); |
| 971 | return -EINVAL; |
| 972 | } |
| 973 | |
| 974 | core_ctl_disable_cpumask_present = true; |
| 975 | pr_info("disable_cpumask=%*pbl\n", |
| 976 | cpumask_pr_args(core_ctl_disable_cpumask)); |
| 977 | |
| 978 | return 0; |
| 979 | } |
| 980 | early_param("core_ctl_disable_cpumask", core_ctl_disable_setup); |
| 981 | |
| 982 | static bool should_skip(const struct cpumask *mask) |
| 983 | { |
| 984 | if (!core_ctl_disable_cpumask_present) |
| 985 | return false; |
| 986 | |
| 987 | /* |
| 988 | * We operate on a cluster basis. Disable the core_ctl for |
| 989 | * a cluster, if all of it's cpus are specified in |
| 990 | * core_ctl_disable_cpumask |
| 991 | */ |
| 992 | return cpumask_subset(mask, core_ctl_disable_cpumask); |
| 993 | } |
| 994 | |
Olav Haugan | e3a3db9 | 2016-06-27 11:35:43 -0700 | [diff] [blame] | 995 | static struct cluster_data *find_cluster_by_first_cpu(unsigned int first_cpu) |
| 996 | { |
| 997 | unsigned int i; |
| 998 | |
| 999 | for (i = 0; i < num_clusters; ++i) { |
| 1000 | if (cluster_state[i].first_cpu == first_cpu) |
| 1001 | return &cluster_state[i]; |
| 1002 | } |
| 1003 | |
| 1004 | return NULL; |
| 1005 | } |
| 1006 | |
| 1007 | static int cluster_init(const struct cpumask *mask) |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 1008 | { |
| 1009 | struct device *dev; |
| 1010 | unsigned int first_cpu = cpumask_first(mask); |
Olav Haugan | e3a3db9 | 2016-06-27 11:35:43 -0700 | [diff] [blame] | 1011 | struct cluster_data *cluster; |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 1012 | struct cpu_data *state; |
| 1013 | unsigned int cpu; |
| 1014 | struct sched_param param = { .sched_priority = MAX_RT_PRIO-1 }; |
| 1015 | |
Pavankumar Kondeti | 1e951a7 | 2017-03-21 14:00:09 +0530 | [diff] [blame] | 1016 | if (should_skip(mask)) |
| 1017 | return 0; |
| 1018 | |
Olav Haugan | e3a3db9 | 2016-06-27 11:35:43 -0700 | [diff] [blame] | 1019 | if (find_cluster_by_first_cpu(first_cpu)) |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 1020 | return 0; |
| 1021 | |
| 1022 | dev = get_cpu_device(first_cpu); |
| 1023 | if (!dev) |
| 1024 | return -ENODEV; |
| 1025 | |
| 1026 | pr_info("Creating CPU group %d\n", first_cpu); |
| 1027 | |
Olav Haugan | e3a3db9 | 2016-06-27 11:35:43 -0700 | [diff] [blame] | 1028 | if (num_clusters == MAX_CLUSTERS) { |
| 1029 | pr_err("Unsupported number of clusters. Only %u supported\n", |
| 1030 | MAX_CLUSTERS); |
| 1031 | return -EINVAL; |
| 1032 | } |
| 1033 | cluster = &cluster_state[num_clusters]; |
| 1034 | ++num_clusters; |
| 1035 | |
Olav Haugan | 833926cb | 2016-06-27 11:38:06 -0700 | [diff] [blame] | 1036 | cpumask_copy(&cluster->cpu_mask, mask); |
Olav Haugan | e3a3db9 | 2016-06-27 11:35:43 -0700 | [diff] [blame] | 1037 | cluster->num_cpus = cpumask_weight(mask); |
| 1038 | if (cluster->num_cpus > MAX_CPUS_PER_CLUSTER) { |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 1039 | pr_err("HW configuration not supported\n"); |
| 1040 | return -EINVAL; |
| 1041 | } |
Olav Haugan | e3a3db9 | 2016-06-27 11:35:43 -0700 | [diff] [blame] | 1042 | cluster->first_cpu = first_cpu; |
| 1043 | cluster->min_cpus = 1; |
| 1044 | cluster->max_cpus = cluster->num_cpus; |
| 1045 | cluster->need_cpus = cluster->num_cpus; |
Olav Haugan | e3a3db9 | 2016-06-27 11:35:43 -0700 | [diff] [blame] | 1046 | cluster->offline_delay_ms = 100; |
| 1047 | cluster->task_thres = UINT_MAX; |
| 1048 | cluster->nrrun = cluster->num_cpus; |
| 1049 | INIT_LIST_HEAD(&cluster->lru); |
Olav Haugan | e3a3db9 | 2016-06-27 11:35:43 -0700 | [diff] [blame] | 1050 | spin_lock_init(&cluster->pending_lock); |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 1051 | |
| 1052 | for_each_cpu(cpu, mask) { |
| 1053 | pr_info("Init CPU%u state\n", cpu); |
| 1054 | |
| 1055 | state = &per_cpu(cpu_state, cpu); |
Olav Haugan | e3a3db9 | 2016-06-27 11:35:43 -0700 | [diff] [blame] | 1056 | state->cluster = cluster; |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 1057 | state->cpu = cpu; |
Olav Haugan | e3a3db9 | 2016-06-27 11:35:43 -0700 | [diff] [blame] | 1058 | list_add_tail(&state->sib, &cluster->lru); |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 1059 | } |
Olav Haugan | 833926cb | 2016-06-27 11:38:06 -0700 | [diff] [blame] | 1060 | cluster->active_cpus = get_active_cpu_count(cluster); |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 1061 | |
Olav Haugan | 833926cb | 2016-06-27 11:38:06 -0700 | [diff] [blame] | 1062 | cluster->core_ctl_thread = kthread_run(try_core_ctl, (void *) cluster, |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 1063 | "core_ctl/%d", first_cpu); |
Olav Haugan | 833926cb | 2016-06-27 11:38:06 -0700 | [diff] [blame] | 1064 | if (IS_ERR(cluster->core_ctl_thread)) |
| 1065 | return PTR_ERR(cluster->core_ctl_thread); |
| 1066 | |
| 1067 | sched_setscheduler_nocheck(cluster->core_ctl_thread, SCHED_FIFO, |
Olav Haugan | e3a3db9 | 2016-06-27 11:35:43 -0700 | [diff] [blame] | 1068 | ¶m); |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 1069 | |
Olav Haugan | e3a3db9 | 2016-06-27 11:35:43 -0700 | [diff] [blame] | 1070 | cluster->inited = true; |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 1071 | |
Olav Haugan | e3a3db9 | 2016-06-27 11:35:43 -0700 | [diff] [blame] | 1072 | kobject_init(&cluster->kobj, &ktype_core_ctl); |
| 1073 | return kobject_add(&cluster->kobj, &dev->kobj, "core_ctl"); |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 1074 | } |
| 1075 | |
| 1076 | static int cpufreq_policy_cb(struct notifier_block *nb, unsigned long val, |
| 1077 | void *data) |
| 1078 | { |
| 1079 | struct cpufreq_policy *policy = data; |
Olav Haugan | 833926cb | 2016-06-27 11:38:06 -0700 | [diff] [blame] | 1080 | int ret; |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 1081 | |
| 1082 | switch (val) { |
| 1083 | case CPUFREQ_CREATE_POLICY: |
Olav Haugan | 833926cb | 2016-06-27 11:38:06 -0700 | [diff] [blame] | 1084 | ret = cluster_init(policy->related_cpus); |
| 1085 | if (ret) |
| 1086 | pr_warn("unable to create core ctl group: %d\n", ret); |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 1087 | break; |
| 1088 | } |
| 1089 | |
| 1090 | return NOTIFY_OK; |
| 1091 | } |
| 1092 | |
| 1093 | static struct notifier_block cpufreq_pol_nb = { |
| 1094 | .notifier_call = cpufreq_policy_cb, |
| 1095 | }; |
| 1096 | |
| 1097 | static int cpufreq_gov_cb(struct notifier_block *nb, unsigned long val, |
| 1098 | void *data) |
| 1099 | { |
| 1100 | struct cpufreq_govinfo *info = data; |
| 1101 | |
| 1102 | switch (val) { |
| 1103 | case CPUFREQ_LOAD_CHANGE: |
| 1104 | core_ctl_set_busy(info->cpu, info->load); |
| 1105 | break; |
| 1106 | } |
| 1107 | |
| 1108 | return NOTIFY_OK; |
| 1109 | } |
| 1110 | |
| 1111 | static struct notifier_block cpufreq_gov_nb = { |
| 1112 | .notifier_call = cpufreq_gov_cb, |
| 1113 | }; |
| 1114 | |
| 1115 | static int __init core_ctl_init(void) |
| 1116 | { |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 1117 | unsigned int cpu; |
| 1118 | |
Pavankumar Kondeti | 1e951a7 | 2017-03-21 14:00:09 +0530 | [diff] [blame] | 1119 | if (should_skip(cpu_possible_mask)) |
| 1120 | return 0; |
| 1121 | |
Olav Haugan | 833926cb | 2016-06-27 11:38:06 -0700 | [diff] [blame] | 1122 | core_ctl_check_interval = (rq_avg_period_ms - RQ_AVG_TOLERANCE) |
| 1123 | * NSEC_PER_MSEC; |
| 1124 | |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 1125 | register_cpu_notifier(&cpu_notifier); |
| 1126 | cpufreq_register_notifier(&cpufreq_pol_nb, CPUFREQ_POLICY_NOTIFIER); |
| 1127 | cpufreq_register_notifier(&cpufreq_gov_nb, CPUFREQ_GOVINFO_NOTIFIER); |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 1128 | |
Olav Haugan | 1e8a44c | 2016-11-17 18:31:33 -0800 | [diff] [blame] | 1129 | cpu_maps_update_begin(); |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 1130 | for_each_online_cpu(cpu) { |
Olav Haugan | 833926cb | 2016-06-27 11:38:06 -0700 | [diff] [blame] | 1131 | struct cpufreq_policy *policy; |
| 1132 | int ret; |
| 1133 | |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 1134 | policy = cpufreq_cpu_get(cpu); |
| 1135 | if (policy) { |
Olav Haugan | 833926cb | 2016-06-27 11:38:06 -0700 | [diff] [blame] | 1136 | ret = cluster_init(policy->related_cpus); |
| 1137 | if (ret) |
| 1138 | pr_warn("unable to create core ctl group: %d\n" |
| 1139 | , ret); |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 1140 | cpufreq_cpu_put(policy); |
| 1141 | } |
| 1142 | } |
Olav Haugan | 1e8a44c | 2016-11-17 18:31:33 -0800 | [diff] [blame] | 1143 | cpu_maps_update_done(); |
Olav Haugan | 833926cb | 2016-06-27 11:38:06 -0700 | [diff] [blame] | 1144 | initialized = true; |
Olav Haugan | 9306c80 | 2016-08-18 17:22:44 -0700 | [diff] [blame] | 1145 | return 0; |
| 1146 | } |
| 1147 | |
| 1148 | late_initcall(core_ctl_init); |