]> asedeno.scripts.mit.edu Git - linux.git/blob - drivers/gpu/drm/panfrost/panfrost_devfreq.c
drm/panfrost: Use generic code for devfreq
[linux.git] / drivers / gpu / drm / panfrost / panfrost_devfreq.c
1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright 2019 Collabora ltd. */
3 #include <linux/devfreq.h>
4 #include <linux/platform_device.h>
5 #include <linux/pm_opp.h>
6 #include <linux/clk.h>
7 #include <linux/regulator/consumer.h>
8
9 #include "panfrost_device.h"
10 #include "panfrost_devfreq.h"
11 #include "panfrost_features.h"
12 #include "panfrost_issues.h"
13 #include "panfrost_gpu.h"
14 #include "panfrost_regs.h"
15
16 static void panfrost_devfreq_update_utilization(struct panfrost_device *pfdev, int slot);
17
18 static int panfrost_devfreq_target(struct device *dev, unsigned long *freq,
19                                    u32 flags)
20 {
21         struct panfrost_device *pfdev = dev_get_drvdata(dev);
22         int err;
23
24         err = dev_pm_opp_set_rate(dev, *freq);
25         if (err)
26                 return err;
27
28         *freq = clk_get_rate(pfdev->clock);
29
30         return 0;
31 }
32
33 static void panfrost_devfreq_reset(struct panfrost_device *pfdev)
34 {
35         ktime_t now = ktime_get();
36         int i;
37
38         for (i = 0; i < NUM_JOB_SLOTS; i++) {
39                 pfdev->devfreq.slot[i].busy_time = 0;
40                 pfdev->devfreq.slot[i].idle_time = 0;
41                 pfdev->devfreq.slot[i].time_last_update = now;
42         }
43 }
44
45 static int panfrost_devfreq_get_dev_status(struct device *dev,
46                                            struct devfreq_dev_status *status)
47 {
48         struct panfrost_device *pfdev = dev_get_drvdata(dev);
49         int i;
50
51         for (i = 0; i < NUM_JOB_SLOTS; i++) {
52                 panfrost_devfreq_update_utilization(pfdev, i);
53         }
54
55         status->current_frequency = clk_get_rate(pfdev->clock);
56         status->total_time = ktime_to_ns(ktime_add(pfdev->devfreq.slot[0].busy_time,
57                                                    pfdev->devfreq.slot[0].idle_time));
58
59         status->busy_time = 0;
60         for (i = 0; i < NUM_JOB_SLOTS; i++) {
61                 status->busy_time += ktime_to_ns(pfdev->devfreq.slot[i].busy_time);
62         }
63
64         /* We're scheduling only to one core atm, so don't divide for now */
65         /* status->busy_time /= NUM_JOB_SLOTS; */
66
67         panfrost_devfreq_reset(pfdev);
68
69         dev_dbg(pfdev->dev, "busy %lu total %lu %lu %% freq %lu MHz\n", status->busy_time,
70                 status->total_time,
71                 status->busy_time / (status->total_time / 100),
72                 status->current_frequency / 1000 / 1000);
73
74         return 0;
75 }
76
77 static int panfrost_devfreq_get_cur_freq(struct device *dev, unsigned long *freq)
78 {
79         struct panfrost_device *pfdev = platform_get_drvdata(to_platform_device(dev));
80
81         *freq = clk_get_rate(pfdev->clock);
82
83         return 0;
84 }
85
86 static struct devfreq_dev_profile panfrost_devfreq_profile = {
87         .polling_ms = 50, /* ~3 frames */
88         .target = panfrost_devfreq_target,
89         .get_dev_status = panfrost_devfreq_get_dev_status,
90         .get_cur_freq = panfrost_devfreq_get_cur_freq,
91 };
92
93 int panfrost_devfreq_init(struct panfrost_device *pfdev)
94 {
95         int ret;
96         struct dev_pm_opp *opp;
97         unsigned long cur_freq;
98
99         ret = dev_pm_opp_of_add_table(&pfdev->pdev->dev);
100         if (ret == -ENODEV) /* Optional, continue without devfreq */
101                 return 0;
102         else if (ret)
103                 return ret;
104
105         panfrost_devfreq_reset(pfdev);
106
107         cur_freq = clk_get_rate(pfdev->clock);
108
109         opp = devfreq_recommended_opp(&pfdev->pdev->dev, &cur_freq, 0);
110         if (IS_ERR(opp))
111                 return PTR_ERR(opp);
112
113         panfrost_devfreq_profile.initial_freq = cur_freq;
114         dev_pm_opp_put(opp);
115
116         pfdev->devfreq.devfreq = devm_devfreq_add_device(&pfdev->pdev->dev,
117                         &panfrost_devfreq_profile, DEVFREQ_GOV_SIMPLE_ONDEMAND,
118                         NULL);
119         if (IS_ERR(pfdev->devfreq.devfreq)) {
120                 DRM_DEV_ERROR(&pfdev->pdev->dev, "Couldn't initialize GPU devfreq\n");
121                 ret = PTR_ERR(pfdev->devfreq.devfreq);
122                 pfdev->devfreq.devfreq = NULL;
123                 dev_pm_opp_of_remove_table(&pfdev->pdev->dev);
124                 return ret;
125         }
126
127         return 0;
128 }
129
130 void panfrost_devfreq_fini(struct panfrost_device *pfdev)
131 {
132         dev_pm_opp_of_remove_table(&pfdev->pdev->dev);
133 }
134
135 void panfrost_devfreq_resume(struct panfrost_device *pfdev)
136 {
137         int i;
138
139         if (!pfdev->devfreq.devfreq)
140                 return;
141
142         panfrost_devfreq_reset(pfdev);
143         for (i = 0; i < NUM_JOB_SLOTS; i++)
144                 pfdev->devfreq.slot[i].busy = false;
145
146         devfreq_resume_device(pfdev->devfreq.devfreq);
147 }
148
149 void panfrost_devfreq_suspend(struct panfrost_device *pfdev)
150 {
151         if (!pfdev->devfreq.devfreq)
152                 return;
153
154         devfreq_suspend_device(pfdev->devfreq.devfreq);
155 }
156
157 static void panfrost_devfreq_update_utilization(struct panfrost_device *pfdev, int slot)
158 {
159         struct panfrost_devfreq_slot *devfreq_slot = &pfdev->devfreq.slot[slot];
160         ktime_t now;
161         ktime_t last;
162
163         if (!pfdev->devfreq.devfreq)
164                 return;
165
166         now = ktime_get();
167         last = pfdev->devfreq.slot[slot].time_last_update;
168
169         /* If we last recorded a transition to busy, we have been idle since */
170         if (devfreq_slot->busy)
171                 pfdev->devfreq.slot[slot].busy_time += ktime_sub(now, last);
172         else
173                 pfdev->devfreq.slot[slot].idle_time += ktime_sub(now, last);
174
175         pfdev->devfreq.slot[slot].time_last_update = now;
176 }
177
178 /* The job scheduler is expected to call this at every transition busy <-> idle */
179 void panfrost_devfreq_record_transition(struct panfrost_device *pfdev, int slot)
180 {
181         struct panfrost_devfreq_slot *devfreq_slot = &pfdev->devfreq.slot[slot];
182
183         panfrost_devfreq_update_utilization(pfdev, slot);
184         devfreq_slot->busy = !devfreq_slot->busy;
185 }