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