Saravana Kannan | cedb32e | 2014-01-22 00:15:33 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2014, The Linux Foundation. All rights reserved. |
| 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) "cache-hwmon: " 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_cache_hwmon.h" |
| 33 | |
Junjie Wu | 211bbef | 2014-10-17 13:13:52 -0700 | [diff] [blame^] | 34 | struct cache_hwmon_node { |
| 35 | unsigned int cycles_per_low_req; |
| 36 | unsigned int cycles_per_med_req; |
| 37 | unsigned int cycles_per_high_req; |
| 38 | unsigned int min_busy; |
| 39 | unsigned int max_busy; |
| 40 | unsigned int tolerance_mrps; |
| 41 | unsigned int guard_band_mhz; |
| 42 | unsigned int decay_rate; |
| 43 | unsigned long prev_mhz; |
| 44 | ktime_t prev_ts; |
| 45 | struct list_head list; |
| 46 | void *orig_data; |
| 47 | struct cache_hwmon *hw; |
| 48 | struct attribute_group *attr_grp; |
| 49 | }; |
| 50 | |
| 51 | static LIST_HEAD(cache_hwmon_list); |
| 52 | static DEFINE_MUTEX(list_lock); |
| 53 | |
| 54 | static int use_cnt; |
| 55 | static DEFINE_MUTEX(state_lock); |
| 56 | |
Saravana Kannan | cedb32e | 2014-01-22 00:15:33 -0800 | [diff] [blame] | 57 | #define show_attr(name) \ |
| 58 | static ssize_t show_##name(struct device *dev, \ |
| 59 | struct device_attribute *attr, char *buf) \ |
| 60 | { \ |
Junjie Wu | 211bbef | 2014-10-17 13:13:52 -0700 | [diff] [blame^] | 61 | struct devfreq *df = to_devfreq(dev); \ |
| 62 | struct cache_hwmon_node *hw = df->data; \ |
| 63 | return snprintf(buf, PAGE_SIZE, "%u\n", hw->name); \ |
Saravana Kannan | cedb32e | 2014-01-22 00:15:33 -0800 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | #define store_attr(name, _min, _max) \ |
| 67 | static ssize_t store_##name(struct device *dev, \ |
| 68 | struct device_attribute *attr, const char *buf, \ |
| 69 | size_t count) \ |
| 70 | { \ |
| 71 | int ret; \ |
| 72 | unsigned int val; \ |
Junjie Wu | 211bbef | 2014-10-17 13:13:52 -0700 | [diff] [blame^] | 73 | struct devfreq *df = to_devfreq(dev); \ |
| 74 | struct cache_hwmon_node *hw = df->data; \ |
Saravana Kannan | cedb32e | 2014-01-22 00:15:33 -0800 | [diff] [blame] | 75 | ret = kstrtoint(buf, 10, &val); \ |
| 76 | if (ret) \ |
| 77 | return ret; \ |
| 78 | val = max(val, _min); \ |
| 79 | val = min(val, _max); \ |
Junjie Wu | 211bbef | 2014-10-17 13:13:52 -0700 | [diff] [blame^] | 80 | hw->name = val; \ |
Saravana Kannan | cedb32e | 2014-01-22 00:15:33 -0800 | [diff] [blame] | 81 | return count; \ |
| 82 | } |
| 83 | |
| 84 | #define gov_attr(__attr, min, max) \ |
| 85 | show_attr(__attr) \ |
| 86 | store_attr(__attr, (min), (max)) \ |
| 87 | static DEVICE_ATTR(__attr, 0644, show_##__attr, store_##__attr) |
| 88 | |
Saravana Kannan | cedb32e | 2014-01-22 00:15:33 -0800 | [diff] [blame] | 89 | #define MIN_MS 10U |
| 90 | #define MAX_MS 500U |
Saravana Kannan | cedb32e | 2014-01-22 00:15:33 -0800 | [diff] [blame] | 91 | |
Junjie Wu | 211bbef | 2014-10-17 13:13:52 -0700 | [diff] [blame^] | 92 | static struct cache_hwmon_node *find_hwmon_node(struct devfreq *df) |
| 93 | { |
| 94 | struct cache_hwmon_node *node, *found = NULL; |
| 95 | |
| 96 | mutex_lock(&list_lock); |
| 97 | list_for_each_entry(node, &cache_hwmon_list, list) |
| 98 | if (node->hw->dev == df->dev.parent || |
| 99 | node->hw->of_node == df->dev.parent->of_node) { |
| 100 | found = node; |
| 101 | break; |
| 102 | } |
| 103 | mutex_unlock(&list_lock); |
| 104 | |
| 105 | return found; |
| 106 | } |
| 107 | |
| 108 | static unsigned long measure_mrps_and_set_irq(struct cache_hwmon_node *node, |
Saravana Kannan | cedb32e | 2014-01-22 00:15:33 -0800 | [diff] [blame] | 109 | struct mrps_stats *stat) |
| 110 | { |
| 111 | ktime_t ts; |
| 112 | unsigned int us; |
Junjie Wu | 211bbef | 2014-10-17 13:13:52 -0700 | [diff] [blame^] | 113 | struct cache_hwmon *hw = node->hw; |
Saravana Kannan | cedb32e | 2014-01-22 00:15:33 -0800 | [diff] [blame] | 114 | |
| 115 | /* |
| 116 | * Since we are stopping the counters, we don't want this short work |
| 117 | * to be interrupted by other tasks and cause the measurements to be |
| 118 | * wrong. Not blocking interrupts to avoid affecting interrupt |
| 119 | * latency and since they should be short anyway because they run in |
| 120 | * atomic context. |
| 121 | */ |
| 122 | preempt_disable(); |
| 123 | |
| 124 | ts = ktime_get(); |
Junjie Wu | 211bbef | 2014-10-17 13:13:52 -0700 | [diff] [blame^] | 125 | us = ktime_to_us(ktime_sub(ts, node->prev_ts)); |
Saravana Kannan | cedb32e | 2014-01-22 00:15:33 -0800 | [diff] [blame] | 126 | if (!us) |
| 127 | us = 1; |
| 128 | |
Junjie Wu | 211bbef | 2014-10-17 13:13:52 -0700 | [diff] [blame^] | 129 | hw->meas_mrps_and_set_irq(hw, node->tolerance_mrps, us, stat); |
| 130 | node->prev_ts = ts; |
Saravana Kannan | cedb32e | 2014-01-22 00:15:33 -0800 | [diff] [blame] | 131 | |
| 132 | preempt_enable(); |
| 133 | |
Junjie Wu | 211bbef | 2014-10-17 13:13:52 -0700 | [diff] [blame^] | 134 | dev_dbg(hw->df->dev.parent, |
| 135 | "stat H=%3lu, M=%3lu, T=%3lu, b=%3u, f=%4lu, us=%d\n", |
Saravana Kannan | cedb32e | 2014-01-22 00:15:33 -0800 | [diff] [blame] | 136 | stat->high, stat->med, stat->high + stat->med, |
Junjie Wu | 211bbef | 2014-10-17 13:13:52 -0700 | [diff] [blame^] | 137 | stat->busy_percent, hw->df->previous_freq / 1000, us); |
Saravana Kannan | cedb32e | 2014-01-22 00:15:33 -0800 | [diff] [blame] | 138 | |
| 139 | return 0; |
| 140 | } |
| 141 | |
Junjie Wu | 211bbef | 2014-10-17 13:13:52 -0700 | [diff] [blame^] | 142 | static void compute_cache_freq(struct cache_hwmon_node *node, |
| 143 | struct mrps_stats *mrps, unsigned long *freq) |
Saravana Kannan | cedb32e | 2014-01-22 00:15:33 -0800 | [diff] [blame] | 144 | { |
| 145 | unsigned long new_mhz; |
| 146 | unsigned int busy; |
| 147 | |
Junjie Wu | 211bbef | 2014-10-17 13:13:52 -0700 | [diff] [blame^] | 148 | new_mhz = mrps->high * node->cycles_per_high_req |
| 149 | + mrps->med * node->cycles_per_med_req |
| 150 | + mrps->low * node->cycles_per_low_req; |
Saravana Kannan | cedb32e | 2014-01-22 00:15:33 -0800 | [diff] [blame] | 151 | |
Junjie Wu | 211bbef | 2014-10-17 13:13:52 -0700 | [diff] [blame^] | 152 | busy = max(node->min_busy, mrps->busy_percent); |
| 153 | busy = min(node->max_busy, busy); |
Saravana Kannan | cedb32e | 2014-01-22 00:15:33 -0800 | [diff] [blame] | 154 | |
| 155 | new_mhz *= 100; |
| 156 | new_mhz /= busy; |
| 157 | |
Junjie Wu | 211bbef | 2014-10-17 13:13:52 -0700 | [diff] [blame^] | 158 | if (new_mhz < node->prev_mhz) { |
| 159 | new_mhz = new_mhz * node->decay_rate + node->prev_mhz |
| 160 | * (100 - node->decay_rate); |
Saravana Kannan | cedb32e | 2014-01-22 00:15:33 -0800 | [diff] [blame] | 161 | new_mhz /= 100; |
| 162 | } |
Junjie Wu | 211bbef | 2014-10-17 13:13:52 -0700 | [diff] [blame^] | 163 | node->prev_mhz = new_mhz; |
Saravana Kannan | cedb32e | 2014-01-22 00:15:33 -0800 | [diff] [blame] | 164 | |
Junjie Wu | 211bbef | 2014-10-17 13:13:52 -0700 | [diff] [blame^] | 165 | new_mhz += node->guard_band_mhz; |
Saravana Kannan | cedb32e | 2014-01-22 00:15:33 -0800 | [diff] [blame] | 166 | *freq = new_mhz * 1000; |
| 167 | } |
| 168 | |
| 169 | #define TOO_SOON_US (1 * USEC_PER_MSEC) |
| 170 | static irqreturn_t mon_intr_handler(int irq, void *dev) |
| 171 | { |
Junjie Wu | 211bbef | 2014-10-17 13:13:52 -0700 | [diff] [blame^] | 172 | struct cache_hwmon_node *node = dev; |
| 173 | struct devfreq *df = node->hw->df; |
Saravana Kannan | cedb32e | 2014-01-22 00:15:33 -0800 | [diff] [blame] | 174 | ktime_t ts; |
| 175 | unsigned int us; |
| 176 | int ret; |
| 177 | |
Junjie Wu | 211bbef | 2014-10-17 13:13:52 -0700 | [diff] [blame^] | 178 | if (!node->hw->is_valid_irq(node->hw)) |
Saravana Kannan | cedb32e | 2014-01-22 00:15:33 -0800 | [diff] [blame] | 179 | return IRQ_NONE; |
| 180 | |
Junjie Wu | 211bbef | 2014-10-17 13:13:52 -0700 | [diff] [blame^] | 181 | dev_dbg(df->dev.parent, "Got interrupt\n"); |
Saravana Kannan | cedb32e | 2014-01-22 00:15:33 -0800 | [diff] [blame] | 182 | devfreq_monitor_stop(df); |
| 183 | |
| 184 | /* |
| 185 | * Don't recalc cache freq if the interrupt comes right after a |
| 186 | * previous cache freq calculation. This is done for two reasons: |
| 187 | * |
| 188 | * 1. Sampling the cache request during a very short duration can |
| 189 | * result in a very inaccurate measurement due to very short |
| 190 | * bursts. |
| 191 | * 2. This can only happen if the limit was hit very close to the end |
| 192 | * of the previous sample period. Which means the current cache |
| 193 | * request estimate is not very off and doesn't need to be |
| 194 | * readjusted. |
| 195 | */ |
| 196 | ts = ktime_get(); |
Junjie Wu | 211bbef | 2014-10-17 13:13:52 -0700 | [diff] [blame^] | 197 | us = ktime_to_us(ktime_sub(ts, node->prev_ts)); |
Saravana Kannan | cedb32e | 2014-01-22 00:15:33 -0800 | [diff] [blame] | 198 | if (us > TOO_SOON_US) { |
| 199 | mutex_lock(&df->lock); |
| 200 | ret = update_devfreq(df); |
| 201 | if (ret) |
Junjie Wu | 211bbef | 2014-10-17 13:13:52 -0700 | [diff] [blame^] | 202 | dev_err(df->dev.parent, |
| 203 | "Unable to update freq on IRQ!\n"); |
Saravana Kannan | cedb32e | 2014-01-22 00:15:33 -0800 | [diff] [blame] | 204 | mutex_unlock(&df->lock); |
| 205 | } |
| 206 | |
| 207 | devfreq_monitor_start(df); |
| 208 | |
| 209 | return IRQ_HANDLED; |
| 210 | } |
| 211 | |
| 212 | static int devfreq_cache_hwmon_get_freq(struct devfreq *df, |
| 213 | unsigned long *freq) |
| 214 | { |
| 215 | struct mrps_stats stat; |
Junjie Wu | 211bbef | 2014-10-17 13:13:52 -0700 | [diff] [blame^] | 216 | struct cache_hwmon_node *node = df->data; |
Saravana Kannan | cedb32e | 2014-01-22 00:15:33 -0800 | [diff] [blame] | 217 | |
Junjie Wu | 211bbef | 2014-10-17 13:13:52 -0700 | [diff] [blame^] | 218 | memset(&stat, 0, sizeof(stat)); |
| 219 | measure_mrps_and_set_irq(node, &stat); |
| 220 | compute_cache_freq(node, &stat, freq); |
Saravana Kannan | cedb32e | 2014-01-22 00:15:33 -0800 | [diff] [blame] | 221 | |
| 222 | return 0; |
| 223 | } |
| 224 | |
| 225 | gov_attr(cycles_per_low_req, 1U, 100U); |
| 226 | gov_attr(cycles_per_med_req, 1U, 100U); |
| 227 | gov_attr(cycles_per_high_req, 1U, 100U); |
| 228 | gov_attr(min_busy, 1U, 100U); |
| 229 | gov_attr(max_busy, 1U, 100U); |
| 230 | gov_attr(tolerance_mrps, 0U, 100U); |
| 231 | gov_attr(guard_band_mhz, 0U, 500U); |
| 232 | gov_attr(decay_rate, 0U, 100U); |
| 233 | |
| 234 | static struct attribute *dev_attr[] = { |
| 235 | &dev_attr_cycles_per_low_req.attr, |
| 236 | &dev_attr_cycles_per_med_req.attr, |
| 237 | &dev_attr_cycles_per_high_req.attr, |
| 238 | &dev_attr_min_busy.attr, |
| 239 | &dev_attr_max_busy.attr, |
| 240 | &dev_attr_tolerance_mrps.attr, |
| 241 | &dev_attr_guard_band_mhz.attr, |
| 242 | &dev_attr_decay_rate.attr, |
| 243 | NULL, |
| 244 | }; |
| 245 | |
| 246 | static struct attribute_group dev_attr_group = { |
| 247 | .name = "cache_hwmon", |
| 248 | .attrs = dev_attr, |
| 249 | }; |
| 250 | |
| 251 | static int start_monitoring(struct devfreq *df) |
| 252 | { |
| 253 | int ret; |
| 254 | struct mrps_stats mrps; |
Junjie Wu | 211bbef | 2014-10-17 13:13:52 -0700 | [diff] [blame^] | 255 | struct device *dev = df->dev.parent; |
| 256 | struct cache_hwmon_node *node; |
| 257 | struct cache_hwmon *hw; |
Saravana Kannan | cedb32e | 2014-01-22 00:15:33 -0800 | [diff] [blame] | 258 | |
Junjie Wu | 211bbef | 2014-10-17 13:13:52 -0700 | [diff] [blame^] | 259 | node = find_hwmon_node(df); |
| 260 | if (!node) { |
| 261 | dev_err(dev, "Unable to find HW monitor!\n"); |
| 262 | return -ENODEV; |
| 263 | } |
| 264 | hw = node->hw; |
| 265 | hw->df = df; |
| 266 | node->orig_data = df->data; |
| 267 | df->data = node; |
Saravana Kannan | cedb32e | 2014-01-22 00:15:33 -0800 | [diff] [blame] | 268 | |
Junjie Wu | 211bbef | 2014-10-17 13:13:52 -0700 | [diff] [blame^] | 269 | node->prev_ts = ktime_get(); |
| 270 | node->prev_mhz = 0; |
| 271 | mrps.high = (df->previous_freq / 1000) - node->guard_band_mhz; |
| 272 | mrps.high /= node->cycles_per_high_req; |
| 273 | mrps.med = mrps.low = 0; |
| 274 | |
| 275 | ret = hw->start_hwmon(hw, &mrps); |
Saravana Kannan | cedb32e | 2014-01-22 00:15:33 -0800 | [diff] [blame] | 276 | if (ret) { |
Junjie Wu | 211bbef | 2014-10-17 13:13:52 -0700 | [diff] [blame^] | 277 | dev_err(dev, "Unable to start HW monitor!\n"); |
| 278 | goto err_start; |
Saravana Kannan | cedb32e | 2014-01-22 00:15:33 -0800 | [diff] [blame] | 279 | } |
| 280 | |
| 281 | devfreq_monitor_start(df); |
| 282 | |
Junjie Wu | 211bbef | 2014-10-17 13:13:52 -0700 | [diff] [blame^] | 283 | if (hw->irq) |
| 284 | ret = request_threaded_irq(hw->irq, NULL, mon_intr_handler, |
Saravana Kannan | cedb32e | 2014-01-22 00:15:33 -0800 | [diff] [blame] | 285 | IRQF_ONESHOT | IRQF_SHARED, |
Junjie Wu | 211bbef | 2014-10-17 13:13:52 -0700 | [diff] [blame^] | 286 | "cache_hwmon", node); |
Saravana Kannan | cedb32e | 2014-01-22 00:15:33 -0800 | [diff] [blame] | 287 | if (ret) { |
Junjie Wu | 211bbef | 2014-10-17 13:13:52 -0700 | [diff] [blame^] | 288 | dev_err(dev, "Unable to register interrupt handler!\n"); |
Saravana Kannan | cedb32e | 2014-01-22 00:15:33 -0800 | [diff] [blame] | 289 | goto req_irq_fail; |
| 290 | } |
| 291 | |
| 292 | ret = sysfs_create_group(&df->dev.kobj, &dev_attr_group); |
| 293 | if (ret) { |
Junjie Wu | 211bbef | 2014-10-17 13:13:52 -0700 | [diff] [blame^] | 294 | dev_err(dev, "Error creating sys entries!\n"); |
Saravana Kannan | cedb32e | 2014-01-22 00:15:33 -0800 | [diff] [blame] | 295 | goto sysfs_fail; |
| 296 | } |
| 297 | |
| 298 | return 0; |
| 299 | |
| 300 | sysfs_fail: |
Junjie Wu | 211bbef | 2014-10-17 13:13:52 -0700 | [diff] [blame^] | 301 | if (hw->irq) { |
| 302 | disable_irq(hw->irq); |
| 303 | free_irq(hw->irq, node); |
| 304 | } |
Saravana Kannan | cedb32e | 2014-01-22 00:15:33 -0800 | [diff] [blame] | 305 | req_irq_fail: |
| 306 | devfreq_monitor_stop(df); |
Junjie Wu | 211bbef | 2014-10-17 13:13:52 -0700 | [diff] [blame^] | 307 | hw->stop_hwmon(hw); |
| 308 | err_start: |
| 309 | df->data = node->orig_data; |
| 310 | node->orig_data = NULL; |
| 311 | hw->df = NULL; |
Saravana Kannan | cedb32e | 2014-01-22 00:15:33 -0800 | [diff] [blame] | 312 | return ret; |
| 313 | } |
| 314 | |
| 315 | static void stop_monitoring(struct devfreq *df) |
| 316 | { |
Junjie Wu | 211bbef | 2014-10-17 13:13:52 -0700 | [diff] [blame^] | 317 | struct cache_hwmon_node *node = df->data; |
| 318 | struct cache_hwmon *hw = node->hw; |
| 319 | |
Saravana Kannan | cedb32e | 2014-01-22 00:15:33 -0800 | [diff] [blame] | 320 | sysfs_remove_group(&df->dev.kobj, &dev_attr_group); |
Junjie Wu | 211bbef | 2014-10-17 13:13:52 -0700 | [diff] [blame^] | 321 | if (hw->irq) { |
| 322 | disable_irq(hw->irq); |
| 323 | free_irq(hw->irq, node); |
| 324 | } |
Saravana Kannan | cedb32e | 2014-01-22 00:15:33 -0800 | [diff] [blame] | 325 | devfreq_monitor_stop(df); |
Junjie Wu | 211bbef | 2014-10-17 13:13:52 -0700 | [diff] [blame^] | 326 | hw->stop_hwmon(hw); |
| 327 | df->data = node->orig_data; |
| 328 | node->orig_data = NULL; |
| 329 | hw->df = NULL; |
Saravana Kannan | cedb32e | 2014-01-22 00:15:33 -0800 | [diff] [blame] | 330 | } |
| 331 | |
| 332 | static int devfreq_cache_hwmon_ev_handler(struct devfreq *df, |
| 333 | unsigned int event, void *data) |
| 334 | { |
| 335 | int ret; |
Junjie Wu | 211bbef | 2014-10-17 13:13:52 -0700 | [diff] [blame^] | 336 | unsigned int sample_ms; |
Saravana Kannan | cedb32e | 2014-01-22 00:15:33 -0800 | [diff] [blame] | 337 | |
| 338 | switch (event) { |
| 339 | case DEVFREQ_GOV_START: |
| 340 | sample_ms = df->profile->polling_ms; |
| 341 | sample_ms = max(MIN_MS, sample_ms); |
| 342 | sample_ms = min(MAX_MS, sample_ms); |
| 343 | df->profile->polling_ms = sample_ms; |
| 344 | |
| 345 | ret = start_monitoring(df); |
| 346 | if (ret) |
| 347 | return ret; |
| 348 | |
Junjie Wu | 211bbef | 2014-10-17 13:13:52 -0700 | [diff] [blame^] | 349 | dev_dbg(df->dev.parent, "Enabled Cache HW monitor governor\n"); |
Saravana Kannan | cedb32e | 2014-01-22 00:15:33 -0800 | [diff] [blame] | 350 | break; |
| 351 | |
| 352 | case DEVFREQ_GOV_STOP: |
| 353 | stop_monitoring(df); |
Junjie Wu | 211bbef | 2014-10-17 13:13:52 -0700 | [diff] [blame^] | 354 | dev_dbg(df->dev.parent, "Disabled Cache HW monitor governor\n"); |
Saravana Kannan | cedb32e | 2014-01-22 00:15:33 -0800 | [diff] [blame] | 355 | break; |
| 356 | |
| 357 | case DEVFREQ_GOV_INTERVAL: |
| 358 | sample_ms = *(unsigned int *)data; |
| 359 | sample_ms = max(MIN_MS, sample_ms); |
| 360 | sample_ms = min(MAX_MS, sample_ms); |
| 361 | devfreq_interval_update(df, &sample_ms); |
| 362 | break; |
| 363 | } |
| 364 | |
| 365 | return 0; |
| 366 | } |
| 367 | |
| 368 | static struct devfreq_governor devfreq_cache_hwmon = { |
| 369 | .name = "cache_hwmon", |
| 370 | .get_target_freq = devfreq_cache_hwmon_get_freq, |
| 371 | .event_handler = devfreq_cache_hwmon_ev_handler, |
| 372 | }; |
| 373 | |
Junjie Wu | 211bbef | 2014-10-17 13:13:52 -0700 | [diff] [blame^] | 374 | int register_cache_hwmon(struct device *dev, struct cache_hwmon *hwmon) |
Saravana Kannan | cedb32e | 2014-01-22 00:15:33 -0800 | [diff] [blame] | 375 | { |
Junjie Wu | 211bbef | 2014-10-17 13:13:52 -0700 | [diff] [blame^] | 376 | int ret = 0; |
| 377 | struct cache_hwmon_node *node; |
Saravana Kannan | cedb32e | 2014-01-22 00:15:33 -0800 | [diff] [blame] | 378 | |
Junjie Wu | 211bbef | 2014-10-17 13:13:52 -0700 | [diff] [blame^] | 379 | if (!hwmon->dev && !hwmon->of_node) |
| 380 | return -EINVAL; |
| 381 | |
| 382 | node = devm_kzalloc(dev, sizeof(*node), GFP_KERNEL); |
| 383 | if (!node) |
| 384 | return -ENOMEM; |
| 385 | |
| 386 | node->cycles_per_med_req = 20; |
| 387 | node->cycles_per_high_req = 35; |
| 388 | node->min_busy = 100; |
| 389 | node->max_busy = 100; |
| 390 | node->tolerance_mrps = 5; |
| 391 | node->guard_band_mhz = 100; |
| 392 | node->decay_rate = 90; |
| 393 | node->hw = hwmon; |
| 394 | node->attr_grp = &dev_attr_group; |
| 395 | |
| 396 | mutex_lock(&state_lock); |
| 397 | if (!use_cnt) { |
| 398 | ret = devfreq_add_governor(&devfreq_cache_hwmon); |
| 399 | if (!ret) |
| 400 | use_cnt++; |
| 401 | } |
| 402 | mutex_unlock(&state_lock); |
| 403 | |
| 404 | if (!ret) { |
| 405 | dev_info(dev, "Cache HWmon governor registered.\n"); |
| 406 | } else { |
| 407 | dev_err(dev, "Failed to add Cache HWmon governor\n"); |
Saravana Kannan | cedb32e | 2014-01-22 00:15:33 -0800 | [diff] [blame] | 408 | return ret; |
| 409 | } |
| 410 | |
Junjie Wu | 211bbef | 2014-10-17 13:13:52 -0700 | [diff] [blame^] | 411 | mutex_lock(&list_lock); |
| 412 | list_add_tail(&node->list, &cache_hwmon_list); |
| 413 | mutex_unlock(&list_lock); |
| 414 | |
| 415 | return ret; |
Saravana Kannan | cedb32e | 2014-01-22 00:15:33 -0800 | [diff] [blame] | 416 | } |
| 417 | |
| 418 | MODULE_DESCRIPTION("HW monitor based cache freq driver"); |
| 419 | MODULE_LICENSE("GPL v2"); |