Rohit Gupta | 5e4358c | 2014-07-18 16:16:02 -0700 | [diff] [blame] | 1 | /* |
Saravana Kannan | 6f67be8 | 2017-09-07 22:22:49 -0700 | [diff] [blame] | 2 | * Copyright (c) 2015-2017, The Linux Foundation. All rights reserved. |
Rohit Gupta | 5e4358c | 2014-07-18 16:16:02 -0700 | [diff] [blame] | 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License version 2 and |
| 6 | * only version 2 as published by the Free Software Foundation. |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, |
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | * GNU General Public License for more details. |
| 12 | */ |
| 13 | |
| 14 | #define pr_fmt(fmt) "mem_lat: " fmt |
| 15 | |
| 16 | #include <linux/kernel.h> |
| 17 | #include <linux/sizes.h> |
| 18 | #include <linux/module.h> |
| 19 | #include <linux/init.h> |
| 20 | #include <linux/io.h> |
| 21 | #include <linux/delay.h> |
| 22 | #include <linux/ktime.h> |
| 23 | #include <linux/time.h> |
| 24 | #include <linux/err.h> |
| 25 | #include <linux/errno.h> |
| 26 | #include <linux/mutex.h> |
| 27 | #include <linux/interrupt.h> |
| 28 | #include <linux/platform_device.h> |
| 29 | #include <linux/of.h> |
| 30 | #include <linux/devfreq.h> |
| 31 | #include "governor.h" |
| 32 | #include "governor_memlat.h" |
| 33 | |
| 34 | #include <trace/events/power.h> |
| 35 | |
| 36 | struct memlat_node { |
| 37 | unsigned int ratio_ceil; |
Saravana Kannan | 83f2846 | 2017-09-26 19:45:15 -0700 | [diff] [blame^] | 38 | unsigned int stall_floor; |
Rohit Gupta | 5e4358c | 2014-07-18 16:16:02 -0700 | [diff] [blame] | 39 | bool mon_started; |
Saravana Kannan | a0ecf00 | 2017-09-07 16:11:16 -0700 | [diff] [blame] | 40 | bool already_zero; |
Rohit Gupta | 5e4358c | 2014-07-18 16:16:02 -0700 | [diff] [blame] | 41 | struct list_head list; |
| 42 | void *orig_data; |
| 43 | struct memlat_hwmon *hw; |
| 44 | struct devfreq_governor *gov; |
| 45 | struct attribute_group *attr_grp; |
| 46 | }; |
| 47 | |
| 48 | static LIST_HEAD(memlat_list); |
| 49 | static DEFINE_MUTEX(list_lock); |
| 50 | |
| 51 | static int use_cnt; |
| 52 | static DEFINE_MUTEX(state_lock); |
| 53 | |
| 54 | #define show_attr(name) \ |
| 55 | static ssize_t show_##name(struct device *dev, \ |
| 56 | struct device_attribute *attr, char *buf) \ |
| 57 | { \ |
| 58 | struct devfreq *df = to_devfreq(dev); \ |
| 59 | struct memlat_node *hw = df->data; \ |
| 60 | return snprintf(buf, PAGE_SIZE, "%u\n", hw->name); \ |
| 61 | } |
| 62 | |
| 63 | #define store_attr(name, _min, _max) \ |
| 64 | static ssize_t store_##name(struct device *dev, \ |
| 65 | struct device_attribute *attr, const char *buf, \ |
| 66 | size_t count) \ |
| 67 | { \ |
| 68 | struct devfreq *df = to_devfreq(dev); \ |
| 69 | struct memlat_node *hw = df->data; \ |
| 70 | int ret; \ |
| 71 | unsigned int val; \ |
| 72 | ret = kstrtouint(buf, 10, &val); \ |
| 73 | if (ret) \ |
| 74 | return ret; \ |
| 75 | val = max(val, _min); \ |
| 76 | val = min(val, _max); \ |
| 77 | hw->name = val; \ |
| 78 | return count; \ |
| 79 | } |
| 80 | |
| 81 | #define gov_attr(__attr, min, max) \ |
| 82 | show_attr(__attr) \ |
| 83 | store_attr(__attr, min, max) \ |
| 84 | static DEVICE_ATTR(__attr, 0644, show_##__attr, store_##__attr) |
| 85 | |
David Keitel | 724116e | 2016-09-13 15:41:52 -0700 | [diff] [blame] | 86 | static ssize_t show_map(struct device *dev, struct device_attribute *attr, |
| 87 | char *buf) |
| 88 | { |
| 89 | struct devfreq *df = to_devfreq(dev); |
| 90 | struct memlat_node *n = df->data; |
| 91 | struct core_dev_map *map = n->hw->freq_map; |
| 92 | unsigned int cnt = 0; |
| 93 | |
| 94 | cnt += snprintf(buf, PAGE_SIZE, "Core freq (MHz)\tDevice BW\n"); |
| 95 | |
| 96 | while (map->core_mhz && cnt < PAGE_SIZE) { |
| 97 | cnt += snprintf(buf + cnt, PAGE_SIZE - cnt, "%15u\t%9u\n", |
| 98 | map->core_mhz, map->target_freq); |
| 99 | map++; |
| 100 | } |
| 101 | if (cnt < PAGE_SIZE) |
| 102 | cnt += snprintf(buf + cnt, PAGE_SIZE - cnt, "\n"); |
| 103 | |
| 104 | return cnt; |
| 105 | } |
| 106 | |
| 107 | static DEVICE_ATTR(freq_map, 0444, show_map, NULL); |
| 108 | |
Rohit Gupta | 870b180 | 2016-04-13 16:55:04 -0700 | [diff] [blame] | 109 | static unsigned long core_to_dev_freq(struct memlat_node *node, |
| 110 | unsigned long coref) |
Rohit Gupta | 5e4358c | 2014-07-18 16:16:02 -0700 | [diff] [blame] | 111 | { |
Rohit Gupta | 5e4358c | 2014-07-18 16:16:02 -0700 | [diff] [blame] | 112 | struct memlat_hwmon *hw = node->hw; |
Rohit Gupta | 870b180 | 2016-04-13 16:55:04 -0700 | [diff] [blame] | 113 | struct core_dev_map *map = hw->freq_map; |
| 114 | unsigned long freq = 0; |
Rohit Gupta | 5e4358c | 2014-07-18 16:16:02 -0700 | [diff] [blame] | 115 | |
Rohit Gupta | 870b180 | 2016-04-13 16:55:04 -0700 | [diff] [blame] | 116 | if (!map) |
| 117 | goto out; |
Rohit Gupta | 5e4358c | 2014-07-18 16:16:02 -0700 | [diff] [blame] | 118 | |
Rohit Gupta | 870b180 | 2016-04-13 16:55:04 -0700 | [diff] [blame] | 119 | while (map->core_mhz && map->core_mhz < coref) |
| 120 | map++; |
| 121 | if (!map->core_mhz) |
| 122 | map--; |
| 123 | freq = map->target_freq; |
Rohit Gupta | 5e4358c | 2014-07-18 16:16:02 -0700 | [diff] [blame] | 124 | |
Rohit Gupta | 870b180 | 2016-04-13 16:55:04 -0700 | [diff] [blame] | 125 | out: |
| 126 | pr_debug("freq: %lu -> dev: %lu\n", coref, freq); |
| 127 | return freq; |
Rohit Gupta | 5e4358c | 2014-07-18 16:16:02 -0700 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | static struct memlat_node *find_memlat_node(struct devfreq *df) |
| 131 | { |
| 132 | struct memlat_node *node, *found = NULL; |
| 133 | |
| 134 | mutex_lock(&list_lock); |
| 135 | list_for_each_entry(node, &memlat_list, list) |
| 136 | if (node->hw->dev == df->dev.parent || |
| 137 | node->hw->of_node == df->dev.parent->of_node) { |
| 138 | found = node; |
| 139 | break; |
| 140 | } |
| 141 | mutex_unlock(&list_lock); |
| 142 | |
| 143 | return found; |
| 144 | } |
| 145 | |
| 146 | static int start_monitor(struct devfreq *df) |
| 147 | { |
| 148 | struct memlat_node *node = df->data; |
| 149 | struct memlat_hwmon *hw = node->hw; |
| 150 | struct device *dev = df->dev.parent; |
| 151 | int ret; |
| 152 | |
| 153 | ret = hw->start_hwmon(hw); |
| 154 | |
| 155 | if (ret) { |
| 156 | dev_err(dev, "Unable to start HW monitor! (%d)\n", ret); |
| 157 | return ret; |
| 158 | } |
| 159 | |
| 160 | devfreq_monitor_start(df); |
| 161 | |
| 162 | node->mon_started = true; |
| 163 | |
| 164 | return 0; |
| 165 | } |
| 166 | |
| 167 | static void stop_monitor(struct devfreq *df) |
| 168 | { |
| 169 | struct memlat_node *node = df->data; |
| 170 | struct memlat_hwmon *hw = node->hw; |
| 171 | |
| 172 | node->mon_started = false; |
| 173 | |
| 174 | devfreq_monitor_stop(df); |
| 175 | hw->stop_hwmon(hw); |
| 176 | } |
| 177 | |
| 178 | static int gov_start(struct devfreq *df) |
| 179 | { |
| 180 | int ret = 0; |
| 181 | struct device *dev = df->dev.parent; |
| 182 | struct memlat_node *node; |
| 183 | struct memlat_hwmon *hw; |
| 184 | |
| 185 | node = find_memlat_node(df); |
| 186 | if (!node) { |
| 187 | dev_err(dev, "Unable to find HW monitor!\n"); |
| 188 | return -ENODEV; |
| 189 | } |
| 190 | hw = node->hw; |
| 191 | |
| 192 | hw->df = df; |
| 193 | node->orig_data = df->data; |
| 194 | df->data = node; |
| 195 | |
| 196 | if (start_monitor(df)) |
| 197 | goto err_start; |
| 198 | |
| 199 | ret = sysfs_create_group(&df->dev.kobj, node->attr_grp); |
| 200 | if (ret) |
| 201 | goto err_sysfs; |
| 202 | |
| 203 | return 0; |
| 204 | |
| 205 | err_sysfs: |
| 206 | stop_monitor(df); |
| 207 | err_start: |
| 208 | df->data = node->orig_data; |
| 209 | node->orig_data = NULL; |
| 210 | hw->df = NULL; |
| 211 | return ret; |
| 212 | } |
| 213 | |
| 214 | static void gov_stop(struct devfreq *df) |
| 215 | { |
| 216 | struct memlat_node *node = df->data; |
| 217 | struct memlat_hwmon *hw = node->hw; |
| 218 | |
| 219 | sysfs_remove_group(&df->dev.kobj, node->attr_grp); |
| 220 | stop_monitor(df); |
| 221 | df->data = node->orig_data; |
| 222 | node->orig_data = NULL; |
| 223 | hw->df = NULL; |
| 224 | } |
| 225 | |
| 226 | static int devfreq_memlat_get_freq(struct devfreq *df, |
| 227 | unsigned long *freq) |
| 228 | { |
Saravana Kannan | a0ecf00 | 2017-09-07 16:11:16 -0700 | [diff] [blame] | 229 | int i, lat_dev = 0; |
Rohit Gupta | 5e4358c | 2014-07-18 16:16:02 -0700 | [diff] [blame] | 230 | struct memlat_node *node = df->data; |
Rohit Gupta | 870b180 | 2016-04-13 16:55:04 -0700 | [diff] [blame] | 231 | struct memlat_hwmon *hw = node->hw; |
| 232 | unsigned long max_freq = 0; |
| 233 | unsigned int ratio; |
Rohit Gupta | 5e4358c | 2014-07-18 16:16:02 -0700 | [diff] [blame] | 234 | |
Rohit Gupta | 870b180 | 2016-04-13 16:55:04 -0700 | [diff] [blame] | 235 | hw->get_cnt(hw); |
Rohit Gupta | 5e4358c | 2014-07-18 16:16:02 -0700 | [diff] [blame] | 236 | |
Rohit Gupta | 870b180 | 2016-04-13 16:55:04 -0700 | [diff] [blame] | 237 | for (i = 0; i < hw->num_cores; i++) { |
| 238 | ratio = hw->core_stats[i].inst_count; |
| 239 | |
| 240 | if (hw->core_stats[i].mem_count) |
| 241 | ratio /= hw->core_stats[i].mem_count; |
| 242 | |
Saravana Kannan | a0ecf00 | 2017-09-07 16:11:16 -0700 | [diff] [blame] | 243 | if (!hw->core_stats[i].inst_count |
| 244 | || !hw->core_stats[i].freq) |
| 245 | continue; |
| 246 | |
Rohit Gupta | 870b180 | 2016-04-13 16:55:04 -0700 | [diff] [blame] | 247 | trace_memlat_dev_meas(dev_name(df->dev.parent), |
| 248 | hw->core_stats[i].id, |
| 249 | hw->core_stats[i].inst_count, |
| 250 | hw->core_stats[i].mem_count, |
Saravana Kannan | 83f2846 | 2017-09-26 19:45:15 -0700 | [diff] [blame^] | 251 | hw->core_stats[i].freq, |
| 252 | hw->core_stats[i].stall_pct, ratio); |
Rohit Gupta | 870b180 | 2016-04-13 16:55:04 -0700 | [diff] [blame] | 253 | |
Saravana Kannan | 6f67be8 | 2017-09-07 22:22:49 -0700 | [diff] [blame] | 254 | if (ratio <= node->ratio_ceil |
Saravana Kannan | 83f2846 | 2017-09-26 19:45:15 -0700 | [diff] [blame^] | 255 | && hw->core_stats[i].stall_pct >= node->stall_floor |
Rohit Gupta | 870b180 | 2016-04-13 16:55:04 -0700 | [diff] [blame] | 256 | && hw->core_stats[i].freq > max_freq) { |
| 257 | lat_dev = i; |
| 258 | max_freq = hw->core_stats[i].freq; |
| 259 | } |
| 260 | } |
| 261 | |
Saravana Kannan | a0ecf00 | 2017-09-07 16:11:16 -0700 | [diff] [blame] | 262 | if (max_freq) |
Rohit Gupta | 870b180 | 2016-04-13 16:55:04 -0700 | [diff] [blame] | 263 | max_freq = core_to_dev_freq(node, max_freq); |
Saravana Kannan | a0ecf00 | 2017-09-07 16:11:16 -0700 | [diff] [blame] | 264 | |
| 265 | if (max_freq || !node->already_zero) { |
Rohit Gupta | 870b180 | 2016-04-13 16:55:04 -0700 | [diff] [blame] | 266 | trace_memlat_dev_update(dev_name(df->dev.parent), |
| 267 | hw->core_stats[lat_dev].id, |
| 268 | hw->core_stats[lat_dev].inst_count, |
| 269 | hw->core_stats[lat_dev].mem_count, |
| 270 | hw->core_stats[lat_dev].freq, |
| 271 | max_freq); |
| 272 | } |
| 273 | |
Saravana Kannan | a0ecf00 | 2017-09-07 16:11:16 -0700 | [diff] [blame] | 274 | node->already_zero = !max_freq; |
| 275 | |
Rohit Gupta | 870b180 | 2016-04-13 16:55:04 -0700 | [diff] [blame] | 276 | *freq = max_freq; |
Rohit Gupta | 5e4358c | 2014-07-18 16:16:02 -0700 | [diff] [blame] | 277 | return 0; |
| 278 | } |
| 279 | |
Rohit Gupta | 870b180 | 2016-04-13 16:55:04 -0700 | [diff] [blame] | 280 | gov_attr(ratio_ceil, 1U, 10000U); |
Saravana Kannan | 83f2846 | 2017-09-26 19:45:15 -0700 | [diff] [blame^] | 281 | gov_attr(stall_floor, 0U, 100U); |
Rohit Gupta | 5e4358c | 2014-07-18 16:16:02 -0700 | [diff] [blame] | 282 | |
| 283 | static struct attribute *dev_attr[] = { |
| 284 | &dev_attr_ratio_ceil.attr, |
Saravana Kannan | 83f2846 | 2017-09-26 19:45:15 -0700 | [diff] [blame^] | 285 | &dev_attr_stall_floor.attr, |
David Keitel | 724116e | 2016-09-13 15:41:52 -0700 | [diff] [blame] | 286 | &dev_attr_freq_map.attr, |
Rohit Gupta | 5e4358c | 2014-07-18 16:16:02 -0700 | [diff] [blame] | 287 | NULL, |
| 288 | }; |
| 289 | |
| 290 | static struct attribute_group dev_attr_group = { |
| 291 | .name = "mem_latency", |
| 292 | .attrs = dev_attr, |
| 293 | }; |
| 294 | |
| 295 | #define MIN_MS 10U |
| 296 | #define MAX_MS 500U |
| 297 | static int devfreq_memlat_ev_handler(struct devfreq *df, |
| 298 | unsigned int event, void *data) |
| 299 | { |
| 300 | int ret; |
| 301 | unsigned int sample_ms; |
| 302 | |
| 303 | switch (event) { |
| 304 | case DEVFREQ_GOV_START: |
| 305 | sample_ms = df->profile->polling_ms; |
| 306 | sample_ms = max(MIN_MS, sample_ms); |
| 307 | sample_ms = min(MAX_MS, sample_ms); |
| 308 | df->profile->polling_ms = sample_ms; |
| 309 | |
| 310 | ret = gov_start(df); |
| 311 | if (ret) |
| 312 | return ret; |
| 313 | |
| 314 | dev_dbg(df->dev.parent, |
| 315 | "Enabled Memory Latency governor\n"); |
| 316 | break; |
| 317 | |
| 318 | case DEVFREQ_GOV_STOP: |
| 319 | gov_stop(df); |
| 320 | dev_dbg(df->dev.parent, |
| 321 | "Disabled Memory Latency governor\n"); |
| 322 | break; |
| 323 | |
| 324 | case DEVFREQ_GOV_INTERVAL: |
| 325 | sample_ms = *(unsigned int *)data; |
| 326 | sample_ms = max(MIN_MS, sample_ms); |
| 327 | sample_ms = min(MAX_MS, sample_ms); |
| 328 | devfreq_interval_update(df, &sample_ms); |
| 329 | break; |
| 330 | } |
| 331 | |
| 332 | return 0; |
| 333 | } |
| 334 | |
| 335 | static struct devfreq_governor devfreq_gov_memlat = { |
| 336 | .name = "mem_latency", |
| 337 | .get_target_freq = devfreq_memlat_get_freq, |
| 338 | .event_handler = devfreq_memlat_ev_handler, |
| 339 | }; |
| 340 | |
Rohit Gupta | 870b180 | 2016-04-13 16:55:04 -0700 | [diff] [blame] | 341 | #define NUM_COLS 2 |
| 342 | static struct core_dev_map *init_core_dev_map(struct device *dev, |
| 343 | char *prop_name) |
| 344 | { |
| 345 | int len, nf, i, j; |
| 346 | u32 data; |
| 347 | struct core_dev_map *tbl; |
| 348 | int ret; |
| 349 | |
| 350 | if (!of_find_property(dev->of_node, prop_name, &len)) |
| 351 | return NULL; |
| 352 | len /= sizeof(data); |
| 353 | |
| 354 | if (len % NUM_COLS || len == 0) |
| 355 | return NULL; |
| 356 | nf = len / NUM_COLS; |
| 357 | |
| 358 | tbl = devm_kzalloc(dev, (nf + 1) * sizeof(struct core_dev_map), |
| 359 | GFP_KERNEL); |
| 360 | if (!tbl) |
| 361 | return NULL; |
| 362 | |
| 363 | for (i = 0, j = 0; i < nf; i++, j += 2) { |
| 364 | ret = of_property_read_u32_index(dev->of_node, prop_name, j, |
| 365 | &data); |
| 366 | if (ret) |
| 367 | return NULL; |
| 368 | tbl[i].core_mhz = data / 1000; |
| 369 | |
| 370 | ret = of_property_read_u32_index(dev->of_node, prop_name, j + 1, |
| 371 | &data); |
| 372 | if (ret) |
| 373 | return NULL; |
| 374 | tbl[i].target_freq = data; |
| 375 | pr_debug("Entry%d CPU:%u, Dev:%u\n", i, tbl[i].core_mhz, |
| 376 | tbl[i].target_freq); |
| 377 | } |
| 378 | tbl[i].core_mhz = 0; |
| 379 | |
| 380 | return tbl; |
| 381 | } |
| 382 | |
Rohit Gupta | 5e4358c | 2014-07-18 16:16:02 -0700 | [diff] [blame] | 383 | int register_memlat(struct device *dev, struct memlat_hwmon *hw) |
| 384 | { |
| 385 | int ret = 0; |
| 386 | struct memlat_node *node; |
| 387 | |
| 388 | if (!hw->dev && !hw->of_node) |
| 389 | return -EINVAL; |
| 390 | |
| 391 | node = devm_kzalloc(dev, sizeof(*node), GFP_KERNEL); |
| 392 | if (!node) |
| 393 | return -ENOMEM; |
| 394 | |
| 395 | node->gov = &devfreq_gov_memlat; |
| 396 | node->attr_grp = &dev_attr_group; |
| 397 | |
| 398 | node->ratio_ceil = 10; |
Rohit Gupta | 5e4358c | 2014-07-18 16:16:02 -0700 | [diff] [blame] | 399 | node->hw = hw; |
| 400 | |
Rohit Gupta | 870b180 | 2016-04-13 16:55:04 -0700 | [diff] [blame] | 401 | hw->freq_map = init_core_dev_map(dev, "qcom,core-dev-table"); |
| 402 | if (!hw->freq_map) { |
| 403 | dev_err(dev, "Couldn't find the core-dev freq table!\n"); |
| 404 | return -EINVAL; |
| 405 | } |
| 406 | |
Rohit Gupta | 5e4358c | 2014-07-18 16:16:02 -0700 | [diff] [blame] | 407 | mutex_lock(&list_lock); |
| 408 | list_add_tail(&node->list, &memlat_list); |
| 409 | mutex_unlock(&list_lock); |
| 410 | |
| 411 | mutex_lock(&state_lock); |
| 412 | if (!use_cnt) |
| 413 | ret = devfreq_add_governor(&devfreq_gov_memlat); |
| 414 | if (!ret) |
| 415 | use_cnt++; |
| 416 | mutex_unlock(&state_lock); |
| 417 | |
| 418 | if (!ret) |
| 419 | dev_info(dev, "Memory Latency governor registered.\n"); |
| 420 | else |
| 421 | dev_err(dev, "Memory Latency governor registration failed!\n"); |
| 422 | |
| 423 | return ret; |
| 424 | } |
| 425 | |
| 426 | MODULE_DESCRIPTION("HW monitor based dev DDR bandwidth voting driver"); |
| 427 | MODULE_LICENSE("GPL v2"); |