summaryrefslogtreecommitdiff
path: root/drivers/accel/amdxdna/amdxdna_pm.c
blob: fa38e65d617c4bc81d155cf54a0f460e5493ab25 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
// SPDX-License-Identifier: GPL-2.0
/*
 * Copyright (C) 2025, Advanced Micro Devices, Inc.
 */

#include <drm/amdxdna_accel.h>
#include <drm/drm_drv.h>
#include <linux/pm_runtime.h>

#include "amdxdna_pm.h"

#define AMDXDNA_AUTOSUSPEND_DELAY	5000 /* milliseconds */

int amdxdna_pm_suspend(struct device *dev)
{
	struct amdxdna_dev *xdna = to_xdna_dev(dev_get_drvdata(dev));
	int ret = -EOPNOTSUPP;
	bool rpm;

	if (xdna->dev_info->ops->suspend) {
		rpm = xdna->rpm_on;
		xdna->rpm_on = false;
		ret = xdna->dev_info->ops->suspend(xdna);
		xdna->rpm_on = rpm;
	}

	XDNA_DBG(xdna, "Suspend done ret %d", ret);
	return ret;
}

int amdxdna_pm_resume(struct device *dev)
{
	struct amdxdna_dev *xdna = to_xdna_dev(dev_get_drvdata(dev));
	int ret = -EOPNOTSUPP;
	bool rpm;

	if (xdna->dev_info->ops->resume) {
		rpm = xdna->rpm_on;
		xdna->rpm_on = false;
		ret = xdna->dev_info->ops->resume(xdna);
		xdna->rpm_on = rpm;
	}

	XDNA_DBG(xdna, "Resume done ret %d", ret);
	return ret;
}

int amdxdna_pm_resume_get(struct amdxdna_dev *xdna)
{
	struct device *dev = xdna->ddev.dev;
	int ret;

	if (!xdna->rpm_on)
		return 0;

	ret = pm_runtime_resume_and_get(dev);
	if (ret) {
		XDNA_ERR(xdna, "Resume failed: %d", ret);
		pm_runtime_set_suspended(dev);
	}

	return ret;
}

void amdxdna_pm_suspend_put(struct amdxdna_dev *xdna)
{
	struct device *dev = xdna->ddev.dev;

	if (!xdna->rpm_on)
		return;

	pm_runtime_put_autosuspend(dev);
}

void amdxdna_pm_init(struct amdxdna_dev *xdna)
{
	struct device *dev = xdna->ddev.dev;

	pm_runtime_set_active(dev);
	pm_runtime_set_autosuspend_delay(dev, AMDXDNA_AUTOSUSPEND_DELAY);
	pm_runtime_use_autosuspend(dev);
	pm_runtime_allow(dev);
	pm_runtime_put_autosuspend(dev);
	xdna->rpm_on = true;
}

void amdxdna_pm_fini(struct amdxdna_dev *xdna)
{
	struct device *dev = xdna->ddev.dev;

	xdna->rpm_on = false;
	pm_runtime_get_noresume(dev);
	pm_runtime_forbid(dev);
}