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