Skip to content

Commit bcdfb5e

Browse files
Russell Kinglynxeye-dev
authored andcommitted
drm/etnaviv: add etnaviv cooling device
Each Vivante GPU contains a clock divider which can divide the GPU clock by 2^n, which can lower the power dissipation from the GPU. It has been suggested that the GC600 on Dove is responsible for 20-30% of the power dissipation from the SoC, so lowering the GPU clock rate provides a way to throttle the power dissiptation, and reduce the temperature when the SoC gets hot. This patch hooks the Etnaviv driver into the kernel's thermal management to allow the GPUs to be throttled when necessary, allowing a reduction in GPU clock rate from /1 to /64 in power of 2 steps. Signed-off-by: Russell King <rmk+kernel@armlinux.org.uk> Reviewed-by: Lucas Stach <l.stach@pengutronix.de> Signed-off-by: Lucas Stach <l.stach@pengutronix.de>
1 parent fda8fa5 commit bcdfb5e

2 files changed

Lines changed: 71 additions & 15 deletions

File tree

‎drivers/gpu/drm/etnaviv/etnaviv_gpu.c‎

Lines changed: 69 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include <linux/dma-fence.h>
1919
#include <linux/moduleparam.h>
2020
#include <linux/of_device.h>
21+
#include <linux/thermal.h>
2122

2223
#include "etnaviv_cmdbuf.h"
2324
#include "etnaviv_dump.h"
@@ -409,6 +410,17 @@ static void etnaviv_gpu_load_clock(struct etnaviv_gpu *gpu, u32 clock)
409410
gpu_write(gpu, VIVS_HI_CLOCK_CONTROL, clock);
410411
}
411412

413+
static void etnaviv_gpu_update_clock(struct etnaviv_gpu *gpu)
414+
{
415+
unsigned int fscale = 1 << (6 - gpu->freq_scale);
416+
u32 clock;
417+
418+
clock = VIVS_HI_CLOCK_CONTROL_DISABLE_DEBUG_REGISTERS |
419+
VIVS_HI_CLOCK_CONTROL_FSCALE_VAL(fscale);
420+
421+
etnaviv_gpu_load_clock(gpu, clock);
422+
}
423+
412424
static int etnaviv_hw_reset(struct etnaviv_gpu *gpu)
413425
{
414426
u32 control, idle;
@@ -426,11 +438,10 @@ static int etnaviv_hw_reset(struct etnaviv_gpu *gpu)
426438
timeout = jiffies + msecs_to_jiffies(1000);
427439

428440
while (time_is_after_jiffies(timeout)) {
429-
control = VIVS_HI_CLOCK_CONTROL_DISABLE_DEBUG_REGISTERS |
430-
VIVS_HI_CLOCK_CONTROL_FSCALE_VAL(0x40);
431-
432441
/* enable clock */
433-
etnaviv_gpu_load_clock(gpu, control);
442+
etnaviv_gpu_update_clock(gpu);
443+
444+
control = gpu_read(gpu, VIVS_HI_CLOCK_CONTROL);
434445

435446
/* Wait for stable clock. Vivante's code waited for 1ms */
436447
usleep_range(1000, 10000);
@@ -490,11 +501,7 @@ static int etnaviv_hw_reset(struct etnaviv_gpu *gpu)
490501
}
491502

492503
/* We rely on the GPU running, so program the clock */
493-
control = VIVS_HI_CLOCK_CONTROL_DISABLE_DEBUG_REGISTERS |
494-
VIVS_HI_CLOCK_CONTROL_FSCALE_VAL(0x40);
495-
496-
/* enable clock */
497-
etnaviv_gpu_load_clock(gpu, control);
504+
etnaviv_gpu_update_clock(gpu);
498505

499506
return 0;
500507
}
@@ -1532,17 +1539,13 @@ static int etnaviv_gpu_hw_suspend(struct etnaviv_gpu *gpu)
15321539
#ifdef CONFIG_PM
15331540
static int etnaviv_gpu_hw_resume(struct etnaviv_gpu *gpu)
15341541
{
1535-
u32 clock;
15361542
int ret;
15371543

15381544
ret = mutex_lock_killable(&gpu->lock);
15391545
if (ret)
15401546
return ret;
15411547

1542-
clock = VIVS_HI_CLOCK_CONTROL_DISABLE_DEBUG_REGISTERS |
1543-
VIVS_HI_CLOCK_CONTROL_FSCALE_VAL(0x40);
1544-
1545-
etnaviv_gpu_load_clock(gpu, clock);
1548+
etnaviv_gpu_update_clock(gpu);
15461549
etnaviv_gpu_hw_init(gpu);
15471550

15481551
gpu->switch_context = true;
@@ -1554,6 +1557,47 @@ static int etnaviv_gpu_hw_resume(struct etnaviv_gpu *gpu)
15541557
}
15551558
#endif
15561559

1560+
static int
1561+
etnaviv_gpu_cooling_get_max_state(struct thermal_cooling_device *cdev,
1562+
unsigned long *state)
1563+
{
1564+
*state = 6;
1565+
1566+
return 0;
1567+
}
1568+
1569+
static int
1570+
etnaviv_gpu_cooling_get_cur_state(struct thermal_cooling_device *cdev,
1571+
unsigned long *state)
1572+
{
1573+
struct etnaviv_gpu *gpu = cdev->devdata;
1574+
1575+
*state = gpu->freq_scale;
1576+
1577+
return 0;
1578+
}
1579+
1580+
static int
1581+
etnaviv_gpu_cooling_set_cur_state(struct thermal_cooling_device *cdev,
1582+
unsigned long state)
1583+
{
1584+
struct etnaviv_gpu *gpu = cdev->devdata;
1585+
1586+
mutex_lock(&gpu->lock);
1587+
gpu->freq_scale = state;
1588+
if (!pm_runtime_suspended(gpu->dev))
1589+
etnaviv_gpu_update_clock(gpu);
1590+
mutex_unlock(&gpu->lock);
1591+
1592+
return 0;
1593+
}
1594+
1595+
static struct thermal_cooling_device_ops cooling_ops = {
1596+
.get_max_state = etnaviv_gpu_cooling_get_max_state,
1597+
.get_cur_state = etnaviv_gpu_cooling_get_cur_state,
1598+
.set_cur_state = etnaviv_gpu_cooling_set_cur_state,
1599+
};
1600+
15571601
static int etnaviv_gpu_bind(struct device *dev, struct device *master,
15581602
void *data)
15591603
{
@@ -1562,13 +1606,20 @@ static int etnaviv_gpu_bind(struct device *dev, struct device *master,
15621606
struct etnaviv_gpu *gpu = dev_get_drvdata(dev);
15631607
int ret;
15641608

1609+
gpu->cooling = thermal_of_cooling_device_register(dev->of_node,
1610+
(char *)dev_name(dev), gpu, &cooling_ops);
1611+
if (IS_ERR(gpu->cooling))
1612+
return PTR_ERR(gpu->cooling);
1613+
15651614
#ifdef CONFIG_PM
15661615
ret = pm_runtime_get_sync(gpu->dev);
15671616
#else
15681617
ret = etnaviv_gpu_clk_enable(gpu);
15691618
#endif
1570-
if (ret < 0)
1619+
if (ret < 0) {
1620+
thermal_cooling_device_unregister(gpu->cooling);
15711621
return ret;
1622+
}
15721623

15731624
gpu->drm = drm;
15741625
gpu->fence_context = dma_fence_context_alloc(1);
@@ -1622,6 +1673,9 @@ static void etnaviv_gpu_unbind(struct device *dev, struct device *master,
16221673
}
16231674

16241675
gpu->drm = NULL;
1676+
1677+
thermal_cooling_device_unregister(gpu->cooling);
1678+
gpu->cooling = NULL;
16251679
}
16261680

16271681
static const struct component_ops gpu_ops = {

‎drivers/gpu/drm/etnaviv/etnaviv_gpu.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ struct etnaviv_cmdbuf;
9797

9898
struct etnaviv_gpu {
9999
struct drm_device *drm;
100+
struct thermal_cooling_device *cooling;
100101
struct device *dev;
101102
struct mutex lock;
102103
struct etnaviv_chip_identity identity;
@@ -150,6 +151,7 @@ struct etnaviv_gpu {
150151
u32 hangcheck_fence;
151152
u32 hangcheck_dma_addr;
152153
struct work_struct recover_work;
154+
unsigned int freq_scale;
153155
};
154156

155157
static inline void gpu_write(struct etnaviv_gpu *gpu, u32 reg, u32 data)

0 commit comments

Comments
 (0)