1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright 2019 Collabora ltd. */
3 #include <linux/devfreq.h>
4 #include <linux/devfreq_cooling.h>
5 #include <linux/platform_device.h>
6 #include <linux/pm_opp.h>
8 #include <linux/regulator/consumer.h>
10 #include "panfrost_device.h"
11 #include "panfrost_devfreq.h"
12 #include "panfrost_features.h"
13 #include "panfrost_issues.h"
14 #include "panfrost_gpu.h"
15 #include "panfrost_regs.h"
17 static void panfrost_devfreq_update_utilization(struct panfrost_device
*pfdev
);
19 static int panfrost_devfreq_target(struct device
*dev
, unsigned long *freq
,
22 struct dev_pm_opp
*opp
;
25 opp
= devfreq_recommended_opp(dev
, freq
, flags
);
30 err
= dev_pm_opp_set_rate(dev
, *freq
);
37 static void panfrost_devfreq_reset(struct panfrost_device
*pfdev
)
39 pfdev
->devfreq
.busy_time
= 0;
40 pfdev
->devfreq
.idle_time
= 0;
41 pfdev
->devfreq
.time_last_update
= ktime_get();
44 static int panfrost_devfreq_get_dev_status(struct device
*dev
,
45 struct devfreq_dev_status
*status
)
47 struct panfrost_device
*pfdev
= dev_get_drvdata(dev
);
49 panfrost_devfreq_update_utilization(pfdev
);
51 status
->current_frequency
= clk_get_rate(pfdev
->clock
);
52 status
->total_time
= ktime_to_ns(ktime_add(pfdev
->devfreq
.busy_time
,
53 pfdev
->devfreq
.idle_time
));
55 status
->busy_time
= ktime_to_ns(pfdev
->devfreq
.busy_time
);
57 panfrost_devfreq_reset(pfdev
);
59 dev_dbg(pfdev
->dev
, "busy %lu total %lu %lu %% freq %lu MHz\n", status
->busy_time
,
61 status
->busy_time
/ (status
->total_time
/ 100),
62 status
->current_frequency
/ 1000 / 1000);
67 static struct devfreq_dev_profile panfrost_devfreq_profile
= {
68 .polling_ms
= 50, /* ~3 frames */
69 .target
= panfrost_devfreq_target
,
70 .get_dev_status
= panfrost_devfreq_get_dev_status
,
73 int panfrost_devfreq_init(struct panfrost_device
*pfdev
)
76 struct dev_pm_opp
*opp
;
77 unsigned long cur_freq
;
78 struct device
*dev
= &pfdev
->pdev
->dev
;
79 struct devfreq
*devfreq
;
80 struct thermal_cooling_device
*cooling
;
82 ret
= dev_pm_opp_of_add_table(dev
);
83 if (ret
== -ENODEV
) /* Optional, continue without devfreq */
88 panfrost_devfreq_reset(pfdev
);
90 cur_freq
= clk_get_rate(pfdev
->clock
);
92 opp
= devfreq_recommended_opp(dev
, &cur_freq
, 0);
96 panfrost_devfreq_profile
.initial_freq
= cur_freq
;
99 devfreq
= devm_devfreq_add_device(dev
, &panfrost_devfreq_profile
,
100 DEVFREQ_GOV_SIMPLE_ONDEMAND
, NULL
);
101 if (IS_ERR(devfreq
)) {
102 DRM_DEV_ERROR(dev
, "Couldn't initialize GPU devfreq\n");
103 dev_pm_opp_of_remove_table(dev
);
104 return PTR_ERR(devfreq
);
106 pfdev
->devfreq
.devfreq
= devfreq
;
108 cooling
= of_devfreq_cooling_register(dev
->of_node
, devfreq
);
110 DRM_DEV_INFO(dev
, "Failed to register cooling device\n");
112 pfdev
->devfreq
.cooling
= cooling
;
117 void panfrost_devfreq_fini(struct panfrost_device
*pfdev
)
119 if (pfdev
->devfreq
.cooling
)
120 devfreq_cooling_unregister(pfdev
->devfreq
.cooling
);
121 dev_pm_opp_of_remove_table(&pfdev
->pdev
->dev
);
124 void panfrost_devfreq_resume(struct panfrost_device
*pfdev
)
126 if (!pfdev
->devfreq
.devfreq
)
129 panfrost_devfreq_reset(pfdev
);
131 devfreq_resume_device(pfdev
->devfreq
.devfreq
);
134 void panfrost_devfreq_suspend(struct panfrost_device
*pfdev
)
136 if (!pfdev
->devfreq
.devfreq
)
139 devfreq_suspend_device(pfdev
->devfreq
.devfreq
);
142 static void panfrost_devfreq_update_utilization(struct panfrost_device
*pfdev
)
147 if (!pfdev
->devfreq
.devfreq
)
151 last
= pfdev
->devfreq
.time_last_update
;
153 if (atomic_read(&pfdev
->devfreq
.busy_count
) > 0)
154 pfdev
->devfreq
.busy_time
+= ktime_sub(now
, last
);
156 pfdev
->devfreq
.idle_time
+= ktime_sub(now
, last
);
158 pfdev
->devfreq
.time_last_update
= now
;
161 void panfrost_devfreq_record_busy(struct panfrost_device
*pfdev
)
163 panfrost_devfreq_update_utilization(pfdev
);
164 atomic_inc(&pfdev
->devfreq
.busy_count
);
167 void panfrost_devfreq_record_idle(struct panfrost_device
*pfdev
)
171 panfrost_devfreq_update_utilization(pfdev
);
172 count
= atomic_dec_if_positive(&pfdev
->devfreq
.busy_count
);