Revert "ARM: tegra: tegratab: dummy change"
[linux-2.6.git] / drivers / edp / tegra_core.c
1 /*
2  * Copyright (c) 2013, NVIDIA CORPORATION.  All rights reserved.
3  *
4  * This program is free software; you can redistribute it and/or modify it
5  * under the terms and conditions of the GNU General Public License,
6  * version 2, as published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope it will be useful, but WITHOUT
9  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
11  * more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
15  */
16
17 #include <linux/clk.h>
18 #include <linux/cpu.h>
19 #include <linux/debugfs.h>
20 #include <linux/edp.h>
21 #include <linux/kernel.h>
22 #include <linux/module.h>
23 #include <linux/platform_device.h>
24 #include <linux/pm_qos.h>
25 #include <linux/slab.h>
26 #include <linux/workqueue.h>
27 #include <linux/platform_data/tegra_edp.h>
28
29 struct freqcap {
30         unsigned int cpu;
31         unsigned int gpu;
32         unsigned int emc;
33 };
34
35 static unsigned int gpu_high_threshold = 700;
36 static unsigned int gpu_high_threshold_highcore = 500;
37 static unsigned int gpu_window = 80;
38 static unsigned int gain_factor = 130;
39 static unsigned int gain_factor_highcore = 130;
40 static unsigned int core_profile = TEGRA_SYSEDP_PROFILE_NORMAL;
41 static unsigned int online_cpu_count;
42 static bool gpu_busy;
43 static unsigned int core_state;
44 static unsigned int core_loan;
45 static struct tegra_sysedp_corecap *cur_corecap;
46 static struct clk *emc_cap_clk;
47 static struct clk *gpu_cap_clk;
48 static struct pm_qos_request cpufreq_qos;
49 static unsigned int cpu_power_offset;
50 static unsigned int force_gpu_pri;
51 static struct delayed_work core_work;
52 static unsigned int *core_edp_states;
53 static struct tegra_sysedp_platform_data *core_platdata;
54 static struct freqcap core_policy;
55 static struct freqcap forced_caps;
56 static struct freqcap cur_caps;
57 static DEFINE_MUTEX(core_lock);
58
59 static const char *profile_names[TEGRA_SYSEDP_PROFILE_NUM] = {
60         [TEGRA_SYSEDP_PROFILE_NORMAL]   = "profile_normal",
61         [TEGRA_SYSEDP_PROFILE_HIGHCORE] = "profile_highcore"
62 };
63
64 /* To save some cycles from a linear search */
65 static unsigned int cpu_lut_match(unsigned int power,
66                 struct tegra_system_edp_entry *lut, unsigned int lutlen)
67 {
68         unsigned int fv;
69         unsigned int lv;
70         unsigned int step;
71         unsigned int i;
72
73         if (lutlen == 1)
74                 return 0;
75
76         fv = lut[0].power_limit_100mW * 100;
77         lv = lut[lutlen - 1].power_limit_100mW * 100;
78         step = (lv - fv) / (lutlen - 1);
79
80         i = (power - fv + step - 1) / step;
81         i = min_t(unsigned int, i, lutlen - 1);
82         if (lut[i].power_limit_100mW * 100 >= power)
83                 return i;
84
85         /* Didn't work, search back from the end */
86         return lutlen - 1;
87 }
88
89 static unsigned int get_cpufreq_lim(unsigned int power)
90 {
91         struct tegra_system_edp_entry *p;
92         int i;
93
94         i = cpu_lut_match(power, core_platdata->cpufreq_lim,
95                         core_platdata->cpufreq_lim_size);
96         p = core_platdata->cpufreq_lim + i;
97
98         for (; i > 0; i--, p--) {
99                 if (p->power_limit_100mW * 100 <= power)
100                         break;
101         }
102
103         WARN_ON(p->power_limit_100mW > power);
104         return p->freq_limits[online_cpu_count - 1];
105 }
106
107 static void pr_caps(struct freqcap *old, struct freqcap *new,
108                 unsigned int cpu_power)
109 {
110         if (!IS_ENABLED(CONFIG_DEBUG_KERNEL))
111                 return;
112
113         if (new->cpu == old->cpu &&
114                         new->gpu == old->gpu &&
115                         new->emc == old->emc)
116                 return;
117
118         pr_debug("sysedp: ncpus %u, gpupri %d, core %5u mW, "
119                         "cpu %5u mW %u kHz, gpu %u kHz, emc %u kHz\n",
120                         online_cpu_count, gpu_busy, cur_corecap->power,
121                         cpu_power, new->cpu, new->gpu, new->emc);
122 }
123
124 static void apply_caps(struct tegra_sysedp_devcap *devcap)
125 {
126         struct freqcap new;
127         int r;
128
129         core_policy.cpu = get_cpufreq_lim(devcap->cpu_power +
130                         cpu_power_offset);
131         core_policy.gpu = devcap->gpufreq * 1000;
132         core_policy.emc = devcap->emcfreq * 1000;
133
134         new.cpu = forced_caps.cpu ?: core_policy.cpu;
135         new.gpu = forced_caps.gpu ?: core_policy.gpu;
136         new.emc = forced_caps.emc ?: core_policy.emc;
137
138         if (new.cpu != cur_caps.cpu)
139                 pm_qos_update_request(&cpufreq_qos, new.cpu);
140
141         if (new.emc != cur_caps.emc) {
142                 r = clk_set_rate(emc_cap_clk, new.emc * 1000);
143                 WARN_ON(r);
144         }
145
146         if (new.gpu != cur_caps.gpu) {
147                 r = clk_set_rate(gpu_cap_clk, new.gpu * 1000);
148                 WARN_ON(r);
149         }
150
151         pr_caps(&cur_caps, &new, devcap->cpu_power);
152         cur_caps = new;
153 }
154
155 static inline bool gpu_priority(void)
156 {
157         return gpu_busy || force_gpu_pri;
158 }
159
160 static void __do_cap_control(void)
161 {
162         struct tegra_sysedp_devcap *cap;
163
164         if (!cur_corecap)
165                 return;
166
167         cap = gpu_priority() ? &cur_corecap->gpupri : &cur_corecap->cpupri;
168         apply_caps(cap);
169 }
170
171 static void do_cap_control(void)
172 {
173         mutex_lock(&core_lock);
174         __do_cap_control();
175         mutex_unlock(&core_lock);
176 }
177
178 static void update_cur_corecap(void)
179 {
180         struct tegra_sysedp_corecap *cap;
181         unsigned int power;
182         int i;
183
184         if (!core_platdata)
185                 return;
186
187         power = core_edp_states[core_state] *
188                         (core_profile == TEGRA_SYSEDP_PROFILE_HIGHCORE ?
189                          gain_factor_highcore : gain_factor) / 100;
190         power += core_loan;
191         i = core_platdata->corecap_size - 1;
192
193         cap = core_profile == TEGRA_SYSEDP_PROFILE_HIGHCORE ?
194                         core_platdata->high_corecap : core_platdata->corecap;
195         cap += i;
196
197         for (; i >= 0; i--, cap--) {
198                 if (cap->power <= power) {
199                         cur_corecap = cap;
200                         return;
201                 }
202         }
203
204         WARN_ON(1);
205         cur_corecap = core_platdata->corecap;
206 }
207
208 static void state_change_cb(unsigned int new_state, void *priv_data)
209 {
210         mutex_lock(&core_lock);
211         core_state = new_state;
212         update_cur_corecap();
213         __do_cap_control();
214         mutex_unlock(&core_lock);
215 }
216
217 static unsigned int loan_update_cb(unsigned int new_size,
218                 struct edp_client *lender, void *priv_data)
219 {
220         mutex_lock(&core_lock);
221         core_loan = new_size;
222         update_cur_corecap();
223         __do_cap_control();
224         mutex_unlock(&core_lock);
225         return new_size;
226 }
227
228 static void loan_close_cb(struct edp_client *lender, void *priv_data)
229 {
230         loan_update_cb(0, lender, priv_data);
231 }
232
233 static void core_worker(struct work_struct *work)
234 {
235         if (!gpu_busy)
236                 do_cap_control();
237 }
238
239 void tegra_edp_notify_gpu_load(unsigned int load)
240 {
241         bool old;
242
243         old = gpu_busy;
244         gpu_busy = load >=
245                 (core_profile == TEGRA_SYSEDP_PROFILE_HIGHCORE ?
246                  gpu_high_threshold_highcore : gpu_high_threshold);
247
248         if (gpu_busy == old || force_gpu_pri || !core_platdata)
249                 return;
250
251         cancel_delayed_work(&core_work);
252
253         if (gpu_busy)
254                 do_cap_control();
255         else
256                 schedule_delayed_work(&core_work,
257                                 msecs_to_jiffies(gpu_window));
258 }
259
260 static int tegra_edp_cpu_notify(struct notifier_block *nb,
261                 unsigned long action, void *data)
262 {
263         switch (action) {
264         case CPU_UP_PREPARE:
265                 online_cpu_count = num_online_cpus() + 1;
266                 break;
267         case CPU_DEAD:
268                 online_cpu_count = num_online_cpus();
269                 break;
270         default:
271                 return NOTIFY_OK;
272         }
273
274         do_cap_control();
275         return NOTIFY_OK;
276 }
277
278 static struct notifier_block tegra_edp_cpu_nb = {
279         .notifier_call = tegra_edp_cpu_notify
280 };
281
282 static ssize_t core_request_store(struct edp_client *c,
283                 struct edp_client_attribute *attr, const char *s, size_t count)
284 {
285         unsigned int id;
286         unsigned int approved;
287         int r;
288
289         if (sscanf(s, "%u", &id) != 1)
290                 return -EINVAL;
291
292         mutex_lock(&core_lock);
293
294         r = edp_update_client_request(c, id, &approved);
295         if (r)
296                 goto out;
297
298         core_state = approved;
299         update_cur_corecap();
300         __do_cap_control();
301
302 out:
303         mutex_unlock(&core_lock);
304         return r ?: count;
305 }
306
307 static ssize_t core_profile_show(struct edp_client *c,
308                 struct edp_client_attribute *attr, char *buf)
309 {
310         return sprintf(buf, "%s\n", profile_names[core_profile]);
311 }
312
313 static ssize_t core_profile_store(struct edp_client *c,
314                 struct edp_client_attribute *attr, const char *buf,
315                 size_t count)
316 {
317         int i;
318         size_t l;
319         const char *name;
320
321         for (i = 0; i < ARRAY_SIZE(profile_names); i++) {
322                 name = profile_names[i];
323                 l = strlen(name);
324                 if ((l <= count) && (strncmp(buf, name, l) == 0))
325                         break;
326         }
327
328         if (i == ARRAY_SIZE(profile_names))
329                 return -ENOENT;
330
331         if (i == TEGRA_SYSEDP_PROFILE_HIGHCORE && !core_platdata->high_corecap)
332                 return -ENODEV;
333
334         mutex_lock(&core_lock);
335
336         core_profile = i;
337         update_cur_corecap();
338         __do_cap_control();
339
340         mutex_unlock(&core_lock);
341
342         return count;
343 }
344
345 struct edp_client_attribute core_attrs[] = {
346         __ATTR(set_request, 0200, NULL, core_request_store),
347         __ATTR(profile, 0644, core_profile_show, core_profile_store),
348         __ATTR_NULL
349 };
350
351 static struct edp_client core_client = {
352         .name = "core",
353         .priority = EDP_MIN_PRIO,
354         .throttle = state_change_cb,
355         .attrs = core_attrs,
356         .notify_promotion = state_change_cb,
357         .notify_loan_update = loan_update_cb,
358         .notify_loan_close = loan_close_cb
359 };
360
361 #ifdef CONFIG_DEBUG_FS
362 static int core_set(void *data, u64 val)
363 {
364         unsigned int *pdata = data;
365         unsigned int old;
366
367         old = *pdata;
368         *pdata = val;
369
370         if (old != *pdata) {
371                 if (pdata == &gain_factor)
372                         update_cur_corecap();
373                 do_cap_control();
374         }
375
376         return 0;
377 }
378
379 static int core_get(void *data, u64 *val)
380 {
381         unsigned int *pdata = data;
382         *val = *pdata;
383         return 0;
384 }
385
386 DEFINE_SIMPLE_ATTRIBUTE(core_fops, core_get, core_set, "%lld\n");
387
388 static void create_attr(const char *name, struct dentry *parent,
389                 unsigned int *data)
390 {
391         struct dentry *d;
392
393         d = debugfs_create_file(name, S_IRUGO | S_IWUSR, parent, data,
394                         &core_fops);
395         WARN_ON(IS_ERR_OR_NULL(d));
396 }
397
398 static __devinit void init_debug(void)
399 {
400         if (!core_client.dentry) {
401                 WARN_ON(1);
402                 return;
403         }
404
405         create_attr("cpu_offset", core_client.dentry, &cpu_power_offset);
406         create_attr("favor_gpu", core_client.dentry, &force_gpu_pri);
407         create_attr("gpu_threshold", core_client.dentry, &gpu_high_threshold);
408         create_attr("force_cpu", core_client.dentry, &forced_caps.cpu);
409         create_attr("force_gpu", core_client.dentry, &forced_caps.gpu);
410         create_attr("force_emc", core_client.dentry, &forced_caps.emc);
411         create_attr("gpu_window", core_client.dentry, &gpu_window);
412         create_attr("gain", core_client.dentry, &gain_factor);
413 }
414 #else
415 static inline void init_debug(void) {}
416 #endif
417
418 /* Ignore missing modem */
419 static __devinit void register_loan(void)
420 {
421         struct edp_client *c;
422         int r;
423
424         c = edp_get_client("modem");
425         if (!c) {
426                 pr_info("Could not access modem EDP client\n");
427                 return;
428         }
429
430         r = edp_register_loan(c, &core_client);
431         WARN_ON(r);
432 }
433
434 static __devinit unsigned int get_num_states(
435                 struct tegra_sysedp_platform_data *pdata)
436 {
437         unsigned int power = 0;
438         unsigned int num = 0;
439         unsigned int i;
440
441         for (i = 0; i < pdata->corecap_size; i++) {
442                 if (pdata->corecap[i].power != power) {
443                         power = pdata->corecap[i].power;
444                         num++;
445                 }
446         }
447
448         return num;
449 }
450
451 static __devinit void get_states(struct tegra_sysedp_platform_data *pdata,
452                 unsigned int num, unsigned int *states)
453 {
454         unsigned int power = 0;
455         unsigned int e0i = 0;
456         unsigned int i;
457
458         for (i = 0; i < pdata->corecap_size; i++) {
459                 if (pdata->corecap[i].power == power)
460                         continue;
461
462                 power = pdata->corecap[i].power;
463                 states[num - e0i - 1] = power;
464                 e0i++;
465         }
466 }
467
468 static __devinit unsigned int initial_req(struct edp_client *client,
469                 unsigned int watts)
470 {
471         int i;
472
473         for (i = 0; i < client->num_states; i++) {
474                 if (client->states[i] == watts)
475                         return i;
476         }
477
478         WARN_ON(1);
479         return 0;
480 }
481
482 static __devinit int init_client(struct tegra_sysedp_platform_data *pdata)
483 {
484         struct edp_manager *m;
485         unsigned int cnt;
486         unsigned int ei;
487         int r;
488
489         m = edp_get_manager("battery");
490         if (!m)
491                 return -ENODEV;
492
493         cnt = get_num_states(pdata);
494         if (!cnt)
495                 return -EINVAL;
496
497         core_edp_states = kzalloc(sizeof(*core_edp_states) * cnt, GFP_KERNEL);
498         if (!core_edp_states)
499                 return -ENOMEM;
500
501         get_states(pdata, cnt, core_edp_states);
502
503         core_client.states = core_edp_states;
504         core_client.num_states = cnt;
505         core_client.e0_index = cnt - 1;
506         core_client.private_data = &core_client;
507
508         r = edp_register_client(m, &core_client);
509         if (r)
510                 goto fail;
511
512         ei = initial_req(&core_client, pdata->init_req_watts);
513         r = edp_update_client_request(&core_client, ei, &core_state);
514         if (r)
515                 return r;
516
517         register_loan();
518         return 0;
519
520 fail:
521         kfree(core_edp_states);
522         core_edp_states = NULL;
523         return r;
524 }
525
526 static __devinit int init_clks(void)
527 {
528         emc_cap_clk = clk_get_sys("battery_edp", "emc");
529         if (IS_ERR(emc_cap_clk))
530                 return -ENODEV;
531
532         gpu_cap_clk = clk_get_sys("battery_edp", "gpu");
533         if (IS_ERR(gpu_cap_clk)) {
534                 clk_put(emc_cap_clk);
535                 return -ENODEV;
536         }
537
538         return 0;
539 }
540
541 static __devinit int tegra_sysedp_probe(struct platform_device *pdev)
542 {
543         int r;
544
545         if (!pdev->dev.platform_data)
546                 return -EINVAL;
547
548         online_cpu_count = num_online_cpus();
549         INIT_DELAYED_WORK(&core_work, core_worker);
550         pm_qos_add_request(&cpufreq_qos, PM_QOS_CPU_FREQ_MAX,
551                         PM_QOS_CPU_FREQ_MAX_DEFAULT_VALUE);
552
553         r = register_cpu_notifier(&tegra_edp_cpu_nb);
554         if (r)
555                 return r;
556
557         r = init_clks();
558         if (r)
559                 return r;
560
561         r = init_client(pdev->dev.platform_data);
562         if (r)
563                 return r;
564
565         mutex_lock(&core_lock);
566         core_platdata = pdev->dev.platform_data;
567         update_cur_corecap();
568         __do_cap_control();
569         mutex_unlock(&core_lock);
570
571         init_debug();
572
573         return 0;
574 }
575
576 static struct platform_driver tegra_sysedp_driver = {
577         .probe = tegra_sysedp_probe,
578         .driver = {
579                 .owner = THIS_MODULE,
580                 .name = "tegra_sysedp"
581         }
582 };
583
584 static __init int tegra_sysedp_init(void)
585 {
586         return platform_driver_register(&tegra_sysedp_driver);
587 }
588 late_initcall(tegra_sysedp_init);