Duy Truong | e833aca | 2013-02-12 13:35:08 -0800 | [diff] [blame] | 1 | /* Copyright (c) 2010-2012, The Linux Foundation. All rights reserved. |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -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 | |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 14 | #include <linux/export.h> |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 15 | #include <linux/kernel.h> |
| 16 | |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 17 | #include <asm/page.h> |
| 18 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 19 | #include "kgsl.h" |
| 20 | #include "kgsl_pwrscale.h" |
| 21 | #include "kgsl_device.h" |
| 22 | |
| 23 | struct kgsl_pwrscale_attribute { |
| 24 | struct attribute attr; |
| 25 | ssize_t (*show)(struct kgsl_device *device, char *buf); |
| 26 | ssize_t (*store)(struct kgsl_device *device, const char *buf, |
| 27 | size_t count); |
| 28 | }; |
| 29 | |
| 30 | #define to_pwrscale(k) container_of(k, struct kgsl_pwrscale, kobj) |
| 31 | #define pwrscale_to_device(p) container_of(p, struct kgsl_device, pwrscale) |
| 32 | #define to_device(k) container_of(k, struct kgsl_device, pwrscale_kobj) |
| 33 | #define to_pwrscale_attr(a) \ |
| 34 | container_of(a, struct kgsl_pwrscale_attribute, attr) |
| 35 | #define to_policy_attr(a) \ |
| 36 | container_of(a, struct kgsl_pwrscale_policy_attribute, attr) |
| 37 | |
| 38 | #define PWRSCALE_ATTR(_name, _mode, _show, _store) \ |
| 39 | struct kgsl_pwrscale_attribute pwrscale_attr_##_name = \ |
| 40 | __ATTR(_name, _mode, _show, _store) |
| 41 | |
| 42 | /* Master list of available policies */ |
| 43 | |
| 44 | static struct kgsl_pwrscale_policy *kgsl_pwrscale_policies[] = { |
| 45 | #ifdef CONFIG_MSM_SCM |
| 46 | &kgsl_pwrscale_policy_tz, |
| 47 | #endif |
Lynus Vaz | dde09ee | 2012-01-05 13:28:22 +0530 | [diff] [blame] | 48 | #ifdef CONFIG_MSM_SLEEP_STATS_DEVICE |
Lucille Sylvester | 591ea03 | 2011-07-21 16:08:37 -0600 | [diff] [blame] | 49 | &kgsl_pwrscale_policy_idlestats, |
| 50 | #endif |
Lucille Sylvester | 6e36241 | 2011-12-09 16:21:42 -0700 | [diff] [blame] | 51 | #ifdef CONFIG_MSM_DCVS |
| 52 | &kgsl_pwrscale_policy_msm, |
| 53 | #endif |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 54 | NULL |
| 55 | }; |
| 56 | |
| 57 | static ssize_t pwrscale_policy_store(struct kgsl_device *device, |
| 58 | const char *buf, size_t count) |
| 59 | { |
| 60 | int i; |
| 61 | struct kgsl_pwrscale_policy *policy = NULL; |
| 62 | |
| 63 | /* The special keyword none allows the user to detach all |
| 64 | policies */ |
| 65 | if (!strncmp("none", buf, 4)) { |
| 66 | kgsl_pwrscale_detach_policy(device); |
| 67 | return count; |
| 68 | } |
| 69 | |
| 70 | for (i = 0; kgsl_pwrscale_policies[i]; i++) { |
| 71 | if (!strncmp(kgsl_pwrscale_policies[i]->name, buf, |
| 72 | strnlen(kgsl_pwrscale_policies[i]->name, |
| 73 | PAGE_SIZE))) { |
| 74 | policy = kgsl_pwrscale_policies[i]; |
| 75 | break; |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | if (policy) |
| 80 | if (kgsl_pwrscale_attach_policy(device, policy)) |
| 81 | return -EIO; |
| 82 | |
| 83 | return count; |
| 84 | } |
| 85 | |
| 86 | static ssize_t pwrscale_policy_show(struct kgsl_device *device, char *buf) |
| 87 | { |
| 88 | int ret; |
| 89 | |
Jordan Crouse | db81758 | 2012-04-11 12:59:05 -0600 | [diff] [blame] | 90 | if (device->pwrscale.policy) { |
| 91 | ret = snprintf(buf, PAGE_SIZE, "%s", |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 92 | device->pwrscale.policy->name); |
Jordan Crouse | db81758 | 2012-04-11 12:59:05 -0600 | [diff] [blame] | 93 | if (device->pwrscale.enabled == 0) |
| 94 | ret += snprintf(buf + ret, PAGE_SIZE - ret, |
| 95 | " (disabled)"); |
| 96 | ret += snprintf(buf + ret, PAGE_SIZE - ret, "\n"); |
| 97 | } else |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 98 | ret = snprintf(buf, PAGE_SIZE, "none\n"); |
| 99 | |
| 100 | return ret; |
| 101 | } |
| 102 | |
Praveena Pachipulusu | 263467d | 2011-12-22 18:07:16 +0530 | [diff] [blame] | 103 | PWRSCALE_ATTR(policy, 0664, pwrscale_policy_show, pwrscale_policy_store); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 104 | |
| 105 | static ssize_t pwrscale_avail_policies_show(struct kgsl_device *device, |
| 106 | char *buf) |
| 107 | { |
| 108 | int i, ret = 0; |
| 109 | |
| 110 | for (i = 0; kgsl_pwrscale_policies[i]; i++) { |
| 111 | ret += snprintf(buf + ret, PAGE_SIZE - ret, "%s ", |
| 112 | kgsl_pwrscale_policies[i]->name); |
| 113 | } |
| 114 | |
| 115 | ret += snprintf(buf + ret, PAGE_SIZE - ret, "none\n"); |
| 116 | return ret; |
| 117 | } |
| 118 | PWRSCALE_ATTR(avail_policies, 0444, pwrscale_avail_policies_show, NULL); |
| 119 | |
| 120 | static struct attribute *pwrscale_attrs[] = { |
| 121 | &pwrscale_attr_policy.attr, |
| 122 | &pwrscale_attr_avail_policies.attr, |
| 123 | NULL |
| 124 | }; |
| 125 | |
| 126 | static ssize_t policy_sysfs_show(struct kobject *kobj, |
| 127 | struct attribute *attr, char *buf) |
| 128 | { |
| 129 | struct kgsl_pwrscale *pwrscale = to_pwrscale(kobj); |
| 130 | struct kgsl_device *device = pwrscale_to_device(pwrscale); |
| 131 | struct kgsl_pwrscale_policy_attribute *pattr = to_policy_attr(attr); |
| 132 | ssize_t ret; |
| 133 | |
| 134 | if (pattr->show) |
| 135 | ret = pattr->show(device, pwrscale, buf); |
| 136 | else |
| 137 | ret = -EIO; |
| 138 | |
| 139 | return ret; |
| 140 | } |
| 141 | |
| 142 | static ssize_t policy_sysfs_store(struct kobject *kobj, |
| 143 | struct attribute *attr, |
| 144 | const char *buf, size_t count) |
| 145 | { |
| 146 | struct kgsl_pwrscale *pwrscale = to_pwrscale(kobj); |
| 147 | struct kgsl_device *device = pwrscale_to_device(pwrscale); |
| 148 | struct kgsl_pwrscale_policy_attribute *pattr = to_policy_attr(attr); |
| 149 | ssize_t ret; |
| 150 | |
| 151 | if (pattr->store) |
| 152 | ret = pattr->store(device, pwrscale, buf, count); |
| 153 | else |
| 154 | ret = -EIO; |
| 155 | |
| 156 | return ret; |
| 157 | } |
| 158 | |
| 159 | static void policy_sysfs_release(struct kobject *kobj) |
| 160 | { |
| 161 | } |
| 162 | |
| 163 | static ssize_t pwrscale_sysfs_show(struct kobject *kobj, |
| 164 | struct attribute *attr, char *buf) |
| 165 | { |
| 166 | struct kgsl_device *device = to_device(kobj); |
| 167 | struct kgsl_pwrscale_attribute *pattr = to_pwrscale_attr(attr); |
| 168 | ssize_t ret; |
| 169 | |
| 170 | if (pattr->show) |
| 171 | ret = pattr->show(device, buf); |
| 172 | else |
| 173 | ret = -EIO; |
| 174 | |
| 175 | return ret; |
| 176 | } |
| 177 | |
| 178 | static ssize_t pwrscale_sysfs_store(struct kobject *kobj, |
| 179 | struct attribute *attr, |
| 180 | const char *buf, size_t count) |
| 181 | { |
| 182 | struct kgsl_device *device = to_device(kobj); |
| 183 | struct kgsl_pwrscale_attribute *pattr = to_pwrscale_attr(attr); |
| 184 | ssize_t ret; |
| 185 | |
| 186 | if (pattr->store) |
| 187 | ret = pattr->store(device, buf, count); |
| 188 | else |
| 189 | ret = -EIO; |
| 190 | |
| 191 | return ret; |
| 192 | } |
| 193 | |
| 194 | static void pwrscale_sysfs_release(struct kobject *kobj) |
| 195 | { |
| 196 | } |
| 197 | |
| 198 | static const struct sysfs_ops policy_sysfs_ops = { |
| 199 | .show = policy_sysfs_show, |
| 200 | .store = policy_sysfs_store |
| 201 | }; |
| 202 | |
| 203 | static const struct sysfs_ops pwrscale_sysfs_ops = { |
| 204 | .show = pwrscale_sysfs_show, |
| 205 | .store = pwrscale_sysfs_store |
| 206 | }; |
| 207 | |
| 208 | static struct kobj_type ktype_pwrscale_policy = { |
| 209 | .sysfs_ops = &policy_sysfs_ops, |
| 210 | .default_attrs = NULL, |
| 211 | .release = policy_sysfs_release |
| 212 | }; |
| 213 | |
| 214 | static struct kobj_type ktype_pwrscale = { |
| 215 | .sysfs_ops = &pwrscale_sysfs_ops, |
| 216 | .default_attrs = pwrscale_attrs, |
| 217 | .release = pwrscale_sysfs_release |
| 218 | }; |
| 219 | |
Jordan Crouse | db81758 | 2012-04-11 12:59:05 -0600 | [diff] [blame] | 220 | #define PWRSCALE_ACTIVE(_d) \ |
| 221 | ((_d)->pwrscale.policy && (_d)->pwrscale.enabled) |
| 222 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 223 | void kgsl_pwrscale_sleep(struct kgsl_device *device) |
| 224 | { |
Jordan Crouse | db81758 | 2012-04-11 12:59:05 -0600 | [diff] [blame] | 225 | if (PWRSCALE_ACTIVE(device) && device->pwrscale.policy->sleep) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 226 | device->pwrscale.policy->sleep(device, &device->pwrscale); |
| 227 | } |
| 228 | EXPORT_SYMBOL(kgsl_pwrscale_sleep); |
| 229 | |
| 230 | void kgsl_pwrscale_wake(struct kgsl_device *device) |
| 231 | { |
Jordan Crouse | db81758 | 2012-04-11 12:59:05 -0600 | [diff] [blame] | 232 | if (PWRSCALE_ACTIVE(device) && device->pwrscale.policy->wake) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 233 | device->pwrscale.policy->wake(device, &device->pwrscale); |
| 234 | } |
| 235 | EXPORT_SYMBOL(kgsl_pwrscale_wake); |
| 236 | |
| 237 | void kgsl_pwrscale_busy(struct kgsl_device *device) |
| 238 | { |
Jordan Crouse | db81758 | 2012-04-11 12:59:05 -0600 | [diff] [blame] | 239 | if (PWRSCALE_ACTIVE(device) && device->pwrscale.policy->busy) |
Lucille Sylvester | e5144ec | 2013-03-11 15:46:58 -0600 | [diff] [blame^] | 240 | device->pwrscale.policy->busy(device, |
| 241 | &device->pwrscale); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 242 | } |
| 243 | |
Lucille Sylvester | 721f7e7 | 2012-08-21 16:31:26 -0600 | [diff] [blame] | 244 | void kgsl_pwrscale_idle(struct kgsl_device *device) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 245 | { |
Jordan Crouse | db81758 | 2012-04-11 12:59:05 -0600 | [diff] [blame] | 246 | if (PWRSCALE_ACTIVE(device) && device->pwrscale.policy->idle) |
Lucille Sylvester | e5144ec | 2013-03-11 15:46:58 -0600 | [diff] [blame^] | 247 | if (device->state == KGSL_STATE_ACTIVE) |
Lucille Sylvester | b2679c0 | 2012-03-13 17:09:27 -0600 | [diff] [blame] | 248 | device->pwrscale.policy->idle(device, |
Lucille Sylvester | 721f7e7 | 2012-08-21 16:31:26 -0600 | [diff] [blame] | 249 | &device->pwrscale); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 250 | } |
| 251 | EXPORT_SYMBOL(kgsl_pwrscale_idle); |
| 252 | |
Jordan Crouse | db81758 | 2012-04-11 12:59:05 -0600 | [diff] [blame] | 253 | void kgsl_pwrscale_disable(struct kgsl_device *device) |
| 254 | { |
| 255 | device->pwrscale.enabled = 0; |
| 256 | } |
| 257 | EXPORT_SYMBOL(kgsl_pwrscale_disable); |
| 258 | |
| 259 | void kgsl_pwrscale_enable(struct kgsl_device *device) |
| 260 | { |
| 261 | device->pwrscale.enabled = 1; |
| 262 | } |
| 263 | EXPORT_SYMBOL(kgsl_pwrscale_enable); |
| 264 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 265 | int kgsl_pwrscale_policy_add_files(struct kgsl_device *device, |
| 266 | struct kgsl_pwrscale *pwrscale, |
| 267 | struct attribute_group *attr_group) |
| 268 | { |
| 269 | int ret; |
| 270 | |
| 271 | ret = kobject_add(&pwrscale->kobj, &device->pwrscale_kobj, |
| 272 | "%s", pwrscale->policy->name); |
| 273 | |
| 274 | if (ret) |
| 275 | return ret; |
| 276 | |
| 277 | ret = sysfs_create_group(&pwrscale->kobj, attr_group); |
| 278 | |
| 279 | if (ret) { |
| 280 | kobject_del(&pwrscale->kobj); |
| 281 | kobject_put(&pwrscale->kobj); |
| 282 | } |
| 283 | |
| 284 | return ret; |
| 285 | } |
| 286 | |
| 287 | void kgsl_pwrscale_policy_remove_files(struct kgsl_device *device, |
| 288 | struct kgsl_pwrscale *pwrscale, |
| 289 | struct attribute_group *attr_group) |
| 290 | { |
| 291 | sysfs_remove_group(&pwrscale->kobj, attr_group); |
| 292 | kobject_del(&pwrscale->kobj); |
| 293 | kobject_put(&pwrscale->kobj); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 294 | } |
| 295 | |
| 296 | static void _kgsl_pwrscale_detach_policy(struct kgsl_device *device) |
| 297 | { |
Lucille Sylvester | b0568f4 | 2011-10-19 13:40:54 -0600 | [diff] [blame] | 298 | if (device->pwrscale.policy != NULL) { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 299 | device->pwrscale.policy->close(device, &device->pwrscale); |
Jordan Crouse | 2ddfc8a | 2012-11-27 11:33:06 -0700 | [diff] [blame] | 300 | |
| 301 | /* |
| 302 | * Try to set max pwrlevel which will be limited to thermal by |
| 303 | * kgsl_pwrctrl_pwrlevel_change if thermal is indeed lower |
| 304 | */ |
| 305 | |
Lucille Sylvester | b0568f4 | 2011-10-19 13:40:54 -0600 | [diff] [blame] | 306 | kgsl_pwrctrl_pwrlevel_change(device, |
Jordan Crouse | 2ddfc8a | 2012-11-27 11:33:06 -0700 | [diff] [blame] | 307 | device->pwrctrl.max_pwrlevel); |
Lucille Sylvester | b0568f4 | 2011-10-19 13:40:54 -0600 | [diff] [blame] | 308 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 309 | device->pwrscale.policy = NULL; |
| 310 | } |
| 311 | |
| 312 | void kgsl_pwrscale_detach_policy(struct kgsl_device *device) |
| 313 | { |
| 314 | mutex_lock(&device->mutex); |
| 315 | _kgsl_pwrscale_detach_policy(device); |
| 316 | mutex_unlock(&device->mutex); |
| 317 | } |
| 318 | EXPORT_SYMBOL(kgsl_pwrscale_detach_policy); |
| 319 | |
| 320 | int kgsl_pwrscale_attach_policy(struct kgsl_device *device, |
| 321 | struct kgsl_pwrscale_policy *policy) |
| 322 | { |
| 323 | int ret = 0; |
| 324 | |
| 325 | mutex_lock(&device->mutex); |
| 326 | |
| 327 | if (device->pwrscale.policy == policy) |
| 328 | goto done; |
| 329 | |
Lynus Vaz | 3f05022 | 2012-01-10 11:49:31 +0530 | [diff] [blame] | 330 | if (device->pwrctrl.num_pwrlevels < 3) { |
| 331 | ret = -EINVAL; |
| 332 | goto done; |
| 333 | } |
| 334 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 335 | if (device->pwrscale.policy != NULL) |
| 336 | _kgsl_pwrscale_detach_policy(device); |
| 337 | |
| 338 | device->pwrscale.policy = policy; |
| 339 | |
Jordan Crouse | db81758 | 2012-04-11 12:59:05 -0600 | [diff] [blame] | 340 | /* Pwrscale is enabled by default at attach time */ |
| 341 | kgsl_pwrscale_enable(device); |
| 342 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 343 | if (policy) { |
| 344 | ret = device->pwrscale.policy->init(device, &device->pwrscale); |
| 345 | if (ret) |
| 346 | device->pwrscale.policy = NULL; |
| 347 | } |
| 348 | |
| 349 | done: |
| 350 | mutex_unlock(&device->mutex); |
| 351 | |
| 352 | return ret; |
| 353 | } |
| 354 | EXPORT_SYMBOL(kgsl_pwrscale_attach_policy); |
| 355 | |
| 356 | int kgsl_pwrscale_init(struct kgsl_device *device) |
| 357 | { |
| 358 | int ret; |
| 359 | |
| 360 | ret = kobject_init_and_add(&device->pwrscale_kobj, &ktype_pwrscale, |
| 361 | &device->dev->kobj, "pwrscale"); |
| 362 | |
| 363 | if (ret) |
| 364 | return ret; |
| 365 | |
| 366 | kobject_init(&device->pwrscale.kobj, &ktype_pwrscale_policy); |
| 367 | return ret; |
| 368 | } |
| 369 | EXPORT_SYMBOL(kgsl_pwrscale_init); |
| 370 | |
| 371 | void kgsl_pwrscale_close(struct kgsl_device *device) |
| 372 | { |
| 373 | kobject_put(&device->pwrscale_kobj); |
| 374 | } |
| 375 | EXPORT_SYMBOL(kgsl_pwrscale_close); |