Skip to content

Commit f070da0

Browse files
anholtpopcornmix
authored andcommitted
drm/v3d: Clock V3D down when not in use.
My various attempts at re-enabling runtime PM have failed, so just crank the clock down when V3D is idle to reduce power consumption. Signed-off-by: Eric Anholt <eric@anholt.net>
1 parent 933e506 commit f070da0

3 files changed

Lines changed: 72 additions & 5 deletions

File tree

‎drivers/gpu/drm/v3d/v3d_drv.c‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,21 @@ static int v3d_platform_drm_probe(struct platform_device *pdev)
289289
}
290290
}
291291

292+
v3d->clk = devm_clk_get(dev, NULL);
293+
if (IS_ERR(v3d->clk)) {
294+
if (ret != -EPROBE_DEFER)
295+
dev_err(dev, "Failed to get clock\n");
296+
goto dev_free;
297+
}
298+
v3d->clk_up_rate = clk_get_rate(v3d->clk);
299+
/* For downclocking, drop it to the minimum frequency we can get from
300+
* the CPRMAN clock generator dividing off our parent. The divider is
301+
* 4 bits, but ask for just higher than that so that rounding doesn't
302+
* make cprman reject our rate.
303+
*/
304+
v3d->clk_down_rate =
305+
(clk_get_rate(clk_get_parent(v3d->clk)) / (1 << 4)) + 10000;
306+
292307
if (v3d->ver < 41) {
293308
ret = map_regs(v3d, &v3d->gca_regs, "gca");
294309
if (ret)
@@ -315,6 +330,9 @@ static int v3d_platform_drm_probe(struct platform_device *pdev)
315330
if (ret)
316331
goto irq_disable;
317332

333+
ret = clk_set_rate(v3d->clk, v3d->clk_down_rate);
334+
WARN_ON_ONCE(ret != 0);
335+
318336
return 0;
319337

320338
irq_disable:

‎drivers/gpu/drm/v3d/v3d_drv.h‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,12 @@ struct v3d_dev {
5151
void __iomem *bridge_regs;
5252
void __iomem *gca_regs;
5353
struct clk *clk;
54+
struct delayed_work clk_down_work;
55+
unsigned long clk_up_rate, clk_down_rate;
56+
struct mutex clk_lock;
57+
u32 clk_refcount;
58+
bool clk_up;
59+
5460
struct reset_control *reset;
5561

5662
/* Virtual and DMA addresses of the single shared page table. */

‎drivers/gpu/drm/v3d/v3d_gem.c‎

Lines changed: 48 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include <linux/device.h>
55
#include <linux/dma-mapping.h>
66
#include <linux/io.h>
7+
#include <linux/clk.h>
78
#include <linux/module.h>
89
#include <linux/platform_device.h>
910
#include <linux/pm_runtime.h>
@@ -18,6 +19,47 @@
1819
#include "v3d_regs.h"
1920
#include "v3d_trace.h"
2021

22+
static void
23+
v3d_clock_down_work(struct work_struct *work)
24+
{
25+
struct v3d_dev *v3d =
26+
container_of(work, struct v3d_dev, clk_down_work.work);
27+
int ret;
28+
29+
ret = clk_set_rate(v3d->clk, v3d->clk_down_rate);
30+
v3d->clk_up = false;
31+
WARN_ON_ONCE(ret != 0);
32+
}
33+
34+
static void
35+
v3d_clock_up_get(struct v3d_dev *v3d)
36+
{
37+
mutex_lock(&v3d->clk_lock);
38+
if (v3d->clk_refcount++ == 0) {
39+
cancel_delayed_work_sync(&v3d->clk_down_work);
40+
if (!v3d->clk_up) {
41+
int ret;
42+
43+
ret = clk_set_rate(v3d->clk, v3d->clk_up_rate);
44+
WARN_ON_ONCE(ret != 0);
45+
v3d->clk_up = true;
46+
}
47+
}
48+
mutex_unlock(&v3d->clk_lock);
49+
}
50+
51+
static void
52+
v3d_clock_up_put(struct v3d_dev *v3d)
53+
{
54+
mutex_lock(&v3d->clk_lock);
55+
if (--v3d->clk_refcount == 0) {
56+
schedule_delayed_work(&v3d->clk_down_work,
57+
msecs_to_jiffies(100));
58+
}
59+
mutex_unlock(&v3d->clk_lock);
60+
}
61+
62+
2163
static void
2264
v3d_init_core(struct v3d_dev *v3d, int core)
2365
{
@@ -354,6 +396,7 @@ v3d_job_free(struct kref *ref)
354396
struct v3d_job *job = container_of(ref, struct v3d_job, refcount);
355397
unsigned long index;
356398
struct dma_fence *fence;
399+
struct v3d_dev *v3d = job->v3d;
357400
int i;
358401

359402
for (i = 0; i < job->bo_count; i++) {
@@ -367,11 +410,7 @@ v3d_job_free(struct kref *ref)
367410
}
368411
xa_destroy(&job->deps);
369412

370-
dma_fence_put(job->irq_fence);
371-
dma_fence_put(job->done_fence);
372-
373-
pm_runtime_mark_last_busy(job->v3d->drm.dev);
374-
pm_runtime_put_autosuspend(job->v3d->drm.dev);
413+
v3d_clock_up_put(v3d);
375414

376415
kfree(job);
377416
}
@@ -453,6 +492,7 @@ v3d_job_init(struct v3d_dev *v3d, struct drm_file *file_priv,
453492
if (ret)
454493
goto fail;
455494

495+
v3d_clock_up_get(v3d);
456496
kref_init(&job->refcount);
457497

458498
return 0;
@@ -879,6 +919,9 @@ v3d_gem_init(struct drm_device *dev)
879919
mutex_init(&v3d->sched_lock);
880920
mutex_init(&v3d->cache_clean_lock);
881921

922+
mutex_init(&v3d->clk_lock);
923+
INIT_DELAYED_WORK(&v3d->clk_down_work, v3d_clock_down_work);
924+
882925
/* Note: We don't allocate address 0. Various bits of HW
883926
* treat 0 as special, such as the occlusion query counters
884927
* where 0 means "disabled".

0 commit comments

Comments
 (0)