1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright 2019 NXP
4  */
5 #include <kunit/test.h>
6 #include <linux/pm_qos.h>
7 
8 /* Basic test for aggregating two "min" requests */
freq_qos_test_min(struct kunit * test)9 static void freq_qos_test_min(struct kunit *test)
10 {
11 	struct freq_constraints	qos;
12 	struct freq_qos_request	req1, req2;
13 	int ret;
14 
15 	freq_constraints_init(&qos);
16 	memset(&req1, 0, sizeof(req1));
17 	memset(&req2, 0, sizeof(req2));
18 
19 	ret = freq_qos_add_request(&qos, &req1, FREQ_QOS_MIN, 1000);
20 	KUNIT_EXPECT_EQ(test, ret, 1);
21 	ret = freq_qos_add_request(&qos, &req2, FREQ_QOS_MIN, 2000);
22 	KUNIT_EXPECT_EQ(test, ret, 1);
23 
24 	KUNIT_EXPECT_EQ(test, freq_qos_read_value(&qos, FREQ_QOS_MIN), 2000);
25 
26 	ret = freq_qos_remove_request(&req2);
27 	KUNIT_EXPECT_EQ(test, ret, 1);
28 	KUNIT_EXPECT_EQ(test, freq_qos_read_value(&qos, FREQ_QOS_MIN), 1000);
29 
30 	ret = freq_qos_remove_request(&req1);
31 	KUNIT_EXPECT_EQ(test, ret, 1);
32 	KUNIT_EXPECT_EQ(test, freq_qos_read_value(&qos, FREQ_QOS_MIN),
33 			FREQ_QOS_MIN_DEFAULT_VALUE);
34 }
35 
36 /* Test that requests for MAX_DEFAULT_VALUE have no effect */
freq_qos_test_maxdef(struct kunit * test)37 static void freq_qos_test_maxdef(struct kunit *test)
38 {
39 	struct freq_constraints	qos;
40 	struct freq_qos_request	req1, req2;
41 	int ret;
42 
43 	freq_constraints_init(&qos);
44 	memset(&req1, 0, sizeof(req1));
45 	memset(&req2, 0, sizeof(req2));
46 	KUNIT_EXPECT_EQ(test, freq_qos_read_value(&qos, FREQ_QOS_MAX),
47 			FREQ_QOS_MAX_DEFAULT_VALUE);
48 
49 	ret = freq_qos_add_request(&qos, &req1, FREQ_QOS_MAX,
50 			FREQ_QOS_MAX_DEFAULT_VALUE);
51 	KUNIT_EXPECT_EQ(test, ret, 0);
52 	ret = freq_qos_add_request(&qos, &req2, FREQ_QOS_MAX,
53 			FREQ_QOS_MAX_DEFAULT_VALUE);
54 	KUNIT_EXPECT_EQ(test, ret, 0);
55 
56 	/* Add max 1000 */
57 	ret = freq_qos_update_request(&req1, 1000);
58 	KUNIT_EXPECT_EQ(test, ret, 1);
59 	KUNIT_EXPECT_EQ(test, freq_qos_read_value(&qos, FREQ_QOS_MAX), 1000);
60 
61 	/* Add max 2000, no impact */
62 	ret = freq_qos_update_request(&req2, 2000);
63 	KUNIT_EXPECT_EQ(test, ret, 0);
64 	KUNIT_EXPECT_EQ(test, freq_qos_read_value(&qos, FREQ_QOS_MAX), 1000);
65 
66 	/* Remove max 1000, new max 2000 */
67 	ret = freq_qos_remove_request(&req1);
68 	KUNIT_EXPECT_EQ(test, ret, 1);
69 	KUNIT_EXPECT_EQ(test, freq_qos_read_value(&qos, FREQ_QOS_MAX), 2000);
70 }
71 
72 /*
73  * Test that a freq_qos_request can be added again after removal
74  *
75  * This issue was solved by commit 05ff1ba412fd ("PM: QoS: Invalidate frequency
76  * QoS requests after removal")
77  */
freq_qos_test_readd(struct kunit * test)78 static void freq_qos_test_readd(struct kunit *test)
79 {
80 	struct freq_constraints	qos;
81 	struct freq_qos_request	req;
82 	int ret;
83 
84 	freq_constraints_init(&qos);
85 	memset(&req, 0, sizeof(req));
86 	KUNIT_EXPECT_EQ(test, freq_qos_read_value(&qos, FREQ_QOS_MIN),
87 			FREQ_QOS_MIN_DEFAULT_VALUE);
88 
89 	/* Add */
90 	ret = freq_qos_add_request(&qos, &req, FREQ_QOS_MIN, 1000);
91 	KUNIT_EXPECT_EQ(test, ret, 1);
92 	KUNIT_EXPECT_EQ(test, freq_qos_read_value(&qos, FREQ_QOS_MIN), 1000);
93 
94 	/* Remove */
95 	ret = freq_qos_remove_request(&req);
96 	KUNIT_EXPECT_EQ(test, ret, 1);
97 	KUNIT_EXPECT_EQ(test, freq_qos_read_value(&qos, FREQ_QOS_MIN),
98 			FREQ_QOS_MIN_DEFAULT_VALUE);
99 
100 	/* Add again */
101 	ret = freq_qos_add_request(&qos, &req, FREQ_QOS_MIN, 2000);
102 	KUNIT_EXPECT_EQ(test, ret, 1);
103 	KUNIT_EXPECT_EQ(test, freq_qos_read_value(&qos, FREQ_QOS_MIN), 2000);
104 }
105 
106 static struct kunit_case pm_qos_test_cases[] = {
107 	KUNIT_CASE(freq_qos_test_min),
108 	KUNIT_CASE(freq_qos_test_maxdef),
109 	KUNIT_CASE(freq_qos_test_readd),
110 	{},
111 };
112 
113 static struct kunit_suite pm_qos_test_module = {
114 	.name = "qos-kunit-test",
115 	.test_cases = pm_qos_test_cases,
116 };
117 kunit_test_suites(&pm_qos_test_module);
118