17
17
#include <linux/platform_device.h>
18
18
#include <linux/pm_opp.h>
19
19
#include <linux/reset.h>
20
+ #include <linux/workqueue.h>
20
21
21
22
#include "governor.h"
22
23
34
35
#define ACTMON_DEV_CTRL_CONSECUTIVE_ABOVE_WMARK_EN BIT(30)
35
36
#define ACTMON_DEV_CTRL_ENB BIT(31)
36
37
38
+ #define ACTMON_DEV_CTRL_STOP 0x00000000
39
+
37
40
#define ACTMON_DEV_UPPER_WMARK 0x4
38
41
#define ACTMON_DEV_LOWER_WMARK 0x8
39
42
#define ACTMON_DEV_INIT_AVG 0xc
@@ -159,7 +162,10 @@ struct tegra_devfreq {
159
162
struct clk * emc_clock ;
160
163
unsigned long max_freq ;
161
164
unsigned long cur_freq ;
162
- struct notifier_block rate_change_nb ;
165
+ struct notifier_block clk_rate_change_nb ;
166
+
167
+ struct delayed_work cpufreq_update_work ;
168
+ struct notifier_block cpu_rate_change_nb ;
163
169
164
170
struct tegra_devfreq_device devices [ARRAY_SIZE (actmon_device_configs )];
165
171
@@ -303,22 +309,32 @@ static unsigned long actmon_cpu_to_emc_rate(struct tegra_devfreq *tegra,
303
309
return 0 ;
304
310
}
305
311
312
+ static unsigned long actmon_device_target_freq (struct tegra_devfreq * tegra ,
313
+ struct tegra_devfreq_device * dev )
314
+ {
315
+ unsigned int avg_sustain_coef ;
316
+ unsigned long target_freq ;
317
+
318
+ target_freq = dev -> avg_count / ACTMON_SAMPLING_PERIOD ;
319
+ avg_sustain_coef = 100 * 100 / dev -> config -> boost_up_threshold ;
320
+ target_freq = do_percent (target_freq , avg_sustain_coef );
321
+ target_freq += dev -> boost_freq ;
322
+
323
+ return target_freq ;
324
+ }
325
+
306
326
static void actmon_update_target (struct tegra_devfreq * tegra ,
307
327
struct tegra_devfreq_device * dev )
308
328
{
309
329
unsigned long cpu_freq = 0 ;
310
330
unsigned long static_cpu_emc_freq = 0 ;
311
- unsigned int avg_sustain_coef ;
312
331
313
332
if (dev -> config -> avg_dependency_threshold ) {
314
- cpu_freq = cpufreq_get (0 );
333
+ cpu_freq = cpufreq_quick_get (0 );
315
334
static_cpu_emc_freq = actmon_cpu_to_emc_rate (tegra , cpu_freq );
316
335
}
317
336
318
- dev -> target_freq = dev -> avg_count / ACTMON_SAMPLING_PERIOD ;
319
- avg_sustain_coef = 100 * 100 / dev -> config -> boost_up_threshold ;
320
- dev -> target_freq = do_percent (dev -> target_freq , avg_sustain_coef );
321
- dev -> target_freq += dev -> boost_freq ;
337
+ dev -> target_freq = actmon_device_target_freq (tegra , dev );
322
338
323
339
if (dev -> avg_count >= dev -> config -> avg_dependency_threshold )
324
340
dev -> target_freq = max (dev -> target_freq , static_cpu_emc_freq );
@@ -349,8 +365,8 @@ static irqreturn_t actmon_thread_isr(int irq, void *data)
349
365
return handled ? IRQ_HANDLED : IRQ_NONE ;
350
366
}
351
367
352
- static int tegra_actmon_rate_notify_cb (struct notifier_block * nb ,
353
- unsigned long action , void * ptr )
368
+ static int tegra_actmon_clk_notify_cb (struct notifier_block * nb ,
369
+ unsigned long action , void * ptr )
354
370
{
355
371
struct clk_notifier_data * data = ptr ;
356
372
struct tegra_devfreq * tegra ;
@@ -360,7 +376,7 @@ static int tegra_actmon_rate_notify_cb(struct notifier_block *nb,
360
376
if (action != POST_RATE_CHANGE )
361
377
return NOTIFY_OK ;
362
378
363
- tegra = container_of (nb , struct tegra_devfreq , rate_change_nb );
379
+ tegra = container_of (nb , struct tegra_devfreq , clk_rate_change_nb );
364
380
365
381
tegra -> cur_freq = data -> new_rate / KHZ ;
366
382
@@ -373,6 +389,79 @@ static int tegra_actmon_rate_notify_cb(struct notifier_block *nb,
373
389
return NOTIFY_OK ;
374
390
}
375
391
392
+ static void tegra_actmon_delayed_update (struct work_struct * work )
393
+ {
394
+ struct tegra_devfreq * tegra = container_of (work , struct tegra_devfreq ,
395
+ cpufreq_update_work .work );
396
+
397
+ mutex_lock (& tegra -> devfreq -> lock );
398
+ update_devfreq (tegra -> devfreq );
399
+ mutex_unlock (& tegra -> devfreq -> lock );
400
+ }
401
+
402
+ static unsigned long
403
+ tegra_actmon_cpufreq_contribution (struct tegra_devfreq * tegra ,
404
+ unsigned int cpu_freq )
405
+ {
406
+ unsigned long static_cpu_emc_freq , dev_freq ;
407
+
408
+ /* check whether CPU's freq is taken into account at all */
409
+ if (tegra -> devices [MCCPU ].avg_count <
410
+ tegra -> devices [MCCPU ].config -> avg_dependency_threshold )
411
+ return 0 ;
412
+
413
+ static_cpu_emc_freq = actmon_cpu_to_emc_rate (tegra , cpu_freq );
414
+ dev_freq = actmon_device_target_freq (tegra , & tegra -> devices [MCCPU ]);
415
+
416
+ if (dev_freq >= static_cpu_emc_freq )
417
+ return 0 ;
418
+
419
+ return static_cpu_emc_freq ;
420
+ }
421
+
422
+ static int tegra_actmon_cpu_notify_cb (struct notifier_block * nb ,
423
+ unsigned long action , void * ptr )
424
+ {
425
+ struct cpufreq_freqs * freqs = ptr ;
426
+ struct tegra_devfreq * tegra ;
427
+ unsigned long old , new , delay ;
428
+
429
+ if (action != CPUFREQ_POSTCHANGE )
430
+ return NOTIFY_OK ;
431
+
432
+ tegra = container_of (nb , struct tegra_devfreq , cpu_rate_change_nb );
433
+
434
+ /*
435
+ * Quickly check whether CPU frequency should be taken into account
436
+ * at all, without blocking CPUFreq's core.
437
+ */
438
+ if (mutex_trylock (& tegra -> devfreq -> lock )) {
439
+ old = tegra_actmon_cpufreq_contribution (tegra , freqs -> old );
440
+ new = tegra_actmon_cpufreq_contribution (tegra , freqs -> new );
441
+ mutex_unlock (& tegra -> devfreq -> lock );
442
+
443
+ /*
444
+ * If CPU's frequency shouldn't be taken into account at
445
+ * the moment, then there is no need to update the devfreq's
446
+ * state because ISR will re-check CPU's frequency on the
447
+ * next interrupt.
448
+ */
449
+ if (old == new )
450
+ return NOTIFY_OK ;
451
+ }
452
+
453
+ /*
454
+ * CPUFreq driver should support CPUFREQ_ASYNC_NOTIFICATION in order
455
+ * to allow asynchronous notifications. This means we can't block
456
+ * here for too long, otherwise CPUFreq's core will complain with a
457
+ * warning splat.
458
+ */
459
+ delay = msecs_to_jiffies (ACTMON_SAMPLING_PERIOD );
460
+ schedule_delayed_work (& tegra -> cpufreq_update_work , delay );
461
+
462
+ return NOTIFY_OK ;
463
+ }
464
+
376
465
static void tegra_actmon_configure_device (struct tegra_devfreq * tegra ,
377
466
struct tegra_devfreq_device * dev )
378
467
{
@@ -405,30 +494,64 @@ static void tegra_actmon_configure_device(struct tegra_devfreq *tegra,
405
494
device_writel (dev , val , ACTMON_DEV_CTRL );
406
495
}
407
496
408
- static void tegra_actmon_start (struct tegra_devfreq * tegra )
497
+ static void tegra_actmon_stop_devices (struct tegra_devfreq * tegra )
498
+ {
499
+ struct tegra_devfreq_device * dev = tegra -> devices ;
500
+ unsigned int i ;
501
+
502
+ for (i = 0 ; i < ARRAY_SIZE (tegra -> devices ); i ++ , dev ++ ) {
503
+ device_writel (dev , ACTMON_DEV_CTRL_STOP , ACTMON_DEV_CTRL );
504
+ device_writel (dev , ACTMON_INTR_STATUS_CLEAR ,
505
+ ACTMON_DEV_INTR_STATUS );
506
+ }
507
+ }
508
+
509
+ static int tegra_actmon_start (struct tegra_devfreq * tegra )
409
510
{
410
511
unsigned int i ;
512
+ int err ;
411
513
412
514
actmon_writel (tegra , ACTMON_SAMPLING_PERIOD - 1 ,
413
515
ACTMON_GLB_PERIOD_CTRL );
414
516
415
517
for (i = 0 ; i < ARRAY_SIZE (tegra -> devices ); i ++ )
416
518
tegra_actmon_configure_device (tegra , & tegra -> devices [i ]);
417
519
520
+ /*
521
+ * We are estimating CPU's memory bandwidth requirement based on
522
+ * amount of memory accesses and system's load, judging by CPU's
523
+ * frequency. We also don't want to receive events about CPU's
524
+ * frequency transaction when governor is stopped, hence notifier
525
+ * is registered dynamically.
526
+ */
527
+ err = cpufreq_register_notifier (& tegra -> cpu_rate_change_nb ,
528
+ CPUFREQ_TRANSITION_NOTIFIER );
529
+ if (err ) {
530
+ dev_err (tegra -> devfreq -> dev .parent ,
531
+ "Failed to register rate change notifier: %d\n" , err );
532
+ goto err_stop ;
533
+ }
534
+
418
535
enable_irq (tegra -> irq );
536
+
537
+ return 0 ;
538
+
539
+ err_stop :
540
+ tegra_actmon_stop_devices (tegra );
541
+
542
+ return err ;
419
543
}
420
544
421
545
static void tegra_actmon_stop (struct tegra_devfreq * tegra )
422
546
{
423
- unsigned int i ;
424
-
425
547
disable_irq (tegra -> irq );
426
548
427
- for (i = 0 ; i < ARRAY_SIZE (tegra -> devices ); i ++ ) {
428
- device_writel (& tegra -> devices [i ], 0x00000000 , ACTMON_DEV_CTRL );
429
- device_writel (& tegra -> devices [i ], ACTMON_INTR_STATUS_CLEAR ,
430
- ACTMON_DEV_INTR_STATUS );
431
- }
549
+ cpufreq_unregister_notifier (& tegra -> cpu_rate_change_nb ,
550
+ CPUFREQ_TRANSITION_NOTIFIER );
551
+
552
+ cancel_delayed_work_sync (& tegra -> cpufreq_update_work );
553
+
554
+ tegra_actmon_stop_devices (tegra );
432
555
}
433
556
434
557
static int tegra_devfreq_target (struct device * dev , unsigned long * freq ,
@@ -536,6 +659,7 @@ static int tegra_governor_event_handler(struct devfreq *devfreq,
536
659
unsigned int event , void * data )
537
660
{
538
661
struct tegra_devfreq * tegra = dev_get_drvdata (devfreq -> dev .parent );
662
+ int ret = 0 ;
539
663
540
664
/*
541
665
* Couple devfreq-device with the governor early because it is
@@ -546,7 +670,7 @@ static int tegra_governor_event_handler(struct devfreq *devfreq,
546
670
switch (event ) {
547
671
case DEVFREQ_GOV_START :
548
672
devfreq_monitor_start (devfreq );
549
- tegra_actmon_start (tegra );
673
+ ret = tegra_actmon_start (tegra );
550
674
break ;
551
675
552
676
case DEVFREQ_GOV_STOP :
@@ -561,11 +685,11 @@ static int tegra_governor_event_handler(struct devfreq *devfreq,
561
685
562
686
case DEVFREQ_GOV_RESUME :
563
687
devfreq_monitor_resume (devfreq );
564
- tegra_actmon_start (tegra );
688
+ ret = tegra_actmon_start (tegra );
565
689
break ;
566
690
}
567
691
568
- return 0 ;
692
+ return ret ;
569
693
}
570
694
571
695
static struct devfreq_governor tegra_devfreq_governor = {
@@ -672,8 +796,14 @@ static int tegra_devfreq_probe(struct platform_device *pdev)
672
796
673
797
platform_set_drvdata (pdev , tegra );
674
798
675
- tegra -> rate_change_nb .notifier_call = tegra_actmon_rate_notify_cb ;
676
- err = clk_notifier_register (tegra -> emc_clock , & tegra -> rate_change_nb );
799
+ tegra -> cpu_rate_change_nb .notifier_call = tegra_actmon_cpu_notify_cb ;
800
+
801
+ INIT_DELAYED_WORK (& tegra -> cpufreq_update_work ,
802
+ tegra_actmon_delayed_update );
803
+
804
+ tegra -> clk_rate_change_nb .notifier_call = tegra_actmon_clk_notify_cb ;
805
+ err = clk_notifier_register (tegra -> emc_clock ,
806
+ & tegra -> clk_rate_change_nb );
677
807
if (err ) {
678
808
dev_err (& pdev -> dev ,
679
809
"Failed to register rate change notifier\n" );
@@ -701,7 +831,7 @@ static int tegra_devfreq_probe(struct platform_device *pdev)
701
831
devfreq_remove_governor (& tegra_devfreq_governor );
702
832
703
833
unreg_notifier :
704
- clk_notifier_unregister (tegra -> emc_clock , & tegra -> rate_change_nb );
834
+ clk_notifier_unregister (tegra -> emc_clock , & tegra -> clk_rate_change_nb );
705
835
706
836
remove_opps :
707
837
dev_pm_opp_remove_all_dynamic (& pdev -> dev );
@@ -719,7 +849,7 @@ static int tegra_devfreq_remove(struct platform_device *pdev)
719
849
devfreq_remove_device (tegra -> devfreq );
720
850
devfreq_remove_governor (& tegra_devfreq_governor );
721
851
722
- clk_notifier_unregister (tegra -> emc_clock , & tegra -> rate_change_nb );
852
+ clk_notifier_unregister (tegra -> emc_clock , & tegra -> clk_rate_change_nb );
723
853
dev_pm_opp_remove_all_dynamic (& pdev -> dev );
724
854
725
855
reset_control_reset (tegra -> reset );
0 commit comments