summaryrefslogtreecommitdiff
path: root/mali_kbase/csf/mali_kbase_csf_timeout.c
blob: ea6c11624157305b994c58838f9c1b0f1ae1a57f (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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
// SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note
/*
 *
 * (C) COPYRIGHT 2019-2021 ARM Limited. All rights reserved.
 *
 * This program is free software and is provided to you under the terms of the
 * GNU General Public License version 2 as published by the Free Software
 * Foundation, and any use by you of this program is subject to the terms
 * of such GNU license.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, you can access it online at
 * http://www.gnu.org/licenses/gpl-2.0.html.
 *
 */

#include <linux/kernel.h>
#include <linux/device.h>
#include <linux/mutex.h>
#include <linux/sysfs.h>
#include <linux/of.h>

#include "mali_kbase.h"
#include "mali_kbase_config_defaults.h"
#include "mali_kbase_csf_firmware.h"
#include "mali_kbase_csf_timeout.h"
#include "mali_kbase_reset_gpu.h"
#include "backend/gpu/mali_kbase_pm_internal.h"

/**
 * set_timeout - set a new global progress timeout.
 *
 * @kbdev:   Instance of a GPU platform device that implements a CSF interface.
 * @timeout: the maximum number of GPU cycles without forward progress to allow
 *           to elapse before terminating a GPU command queue group.
 *
 * Return:   0 on success, or negative on failure
 *           (e.g. -ERANGE if the requested timeout is too large).
 */
static int set_timeout(struct kbase_device *const kbdev, u64 const timeout)
{
	if (timeout > GLB_PROGRESS_TIMER_TIMEOUT_MAX) {
		dev_err(kbdev->dev, "Timeout %llu is too large.\n", timeout);
		return -ERANGE;
	}

	dev_dbg(kbdev->dev, "New progress timeout: %llu cycles\n", timeout);

	atomic64_set(&kbdev->csf.progress_timeout, timeout);

	return 0;
}

/**
 * progress_timeout_store - Store the progress_timeout device attribute.
 * @dev:   The device that has the attribute.
 * @attr:  The attributes of the sysfs file.
 * @buf:   The value written to the sysfs file.
 * @count: The number of bytes written to the sysfs file.
 *
 * This function is called when the progress_timeout sysfs file is written to.
 * It checks the data written, and if valid updates the progress timeout value.
 * The function also checks gpu reset status, if the gpu is in reset process,
 * the function will return an error code (-EBUSY), and no change for timeout
 * value.
 *
 * Return: @count if the function succeeded. An error code on failure.
 */
static ssize_t progress_timeout_store(struct device * const dev,
		struct device_attribute * const attr, const char * const buf,
		size_t const count)
{
	struct kbase_device *const kbdev = dev_get_drvdata(dev);
	int err;
	u64 timeout;

	if (!kbdev)
		return -ENODEV;

	err = kbase_reset_gpu_try_prevent(kbdev);
	if (err) {
		dev_warn(kbdev->dev,
			 "Couldn't process progress_timeout write operation for GPU reset.\n");
		return -EBUSY;
	}

	err = kstrtou64(buf, 0, &timeout);
	if (err)
		dev_err(kbdev->dev,
			"Couldn't process progress_timeout write operation.\n"
			"Use format <progress_timeout>\n");
	else
		err = set_timeout(kbdev, timeout);

	if (!err) {
		kbase_csf_scheduler_pm_active(kbdev);

		err = kbase_csf_scheduler_wait_mcu_active(kbdev);
		if (!err)
			err = kbase_csf_firmware_set_timeout(kbdev, timeout);

		kbase_csf_scheduler_pm_idle(kbdev);
	}

	kbase_reset_gpu_allow(kbdev);
	if (err)
		return err;

	return count;
}

/**
 * progress_timeout_show - Show the progress_timeout device attribute.
 * @dev: The device that has the attribute.
 * @attr: The attributes of the sysfs file.
 * @buf:  The output buffer to receive the global timeout.
 *
 * This function is called to get the progress timeout value.
 *
 * Return: The number of bytes output to @buf.
 */
static ssize_t progress_timeout_show(struct device * const dev,
		struct device_attribute * const attr, char * const buf)
{
	struct kbase_device *const kbdev = dev_get_drvdata(dev);
	int err;

	if (!kbdev)
		return -ENODEV;

	err = scnprintf(buf, PAGE_SIZE, "%llu\n", kbase_csf_timeout_get(kbdev));

	return err;

}

static DEVICE_ATTR_RW(progress_timeout);

int kbase_csf_timeout_init(struct kbase_device *const kbdev)
{
	u64 timeout = DEFAULT_PROGRESS_TIMEOUT;
	int err;

#if IS_ENABLED(CONFIG_OF)
	err = of_property_read_u64(kbdev->dev->of_node,
		"progress_timeout", &timeout);
	if (!err)
		dev_info(kbdev->dev, "Found progress_timeout = %llu in Devicetree\n",
			timeout);
#endif

	err = set_timeout(kbdev, timeout);
	if (err)
		return err;

	err = sysfs_create_file(&kbdev->dev->kobj,
		&dev_attr_progress_timeout.attr);
	if (err)
		dev_err(kbdev->dev, "SysFS file creation failed\n");

	return err;
}

void kbase_csf_timeout_term(struct kbase_device * const kbdev)
{
	sysfs_remove_file(&kbdev->dev->kobj, &dev_attr_progress_timeout.attr);
}

u64 kbase_csf_timeout_get(struct kbase_device *const kbdev)
{
	return atomic64_read(&kbdev->csf.progress_timeout);
}