1#!/bin/bash
2# SPDX-License-Identifier: GPL-2.0
3#
4# Modules specific tests cases
5
6# protect against multiple inclusion
7if [ $FILE_MODULE ]; then
8	return 0
9else
10	FILE_MODULE=DONE
11fi
12
13source cpu.sh
14source cpufreq.sh
15source governor.sh
16
17# Check basic insmod/rmmod
18# $1: module
19test_basic_insmod_rmmod()
20{
21	printf "** Test: Running ${FUNCNAME[0]} **\n\n"
22
23	printf "Inserting $1 module\n"
24	# insert module
25	insmod $1
26	if [ $? != 0 ]; then
27		printf "Insmod $1 failed\n"
28		exit;
29	fi
30
31	printf "Removing $1 module\n"
32	# remove module
33	rmmod $1
34	if [ $? != 0 ]; then
35		printf "rmmod $1 failed\n"
36		exit;
37	fi
38
39	printf "\n"
40}
41
42# Insert cpufreq driver module and perform basic tests
43# $1: cpufreq-driver module to insert
44# $2: If we want to play with CPUs (1) or not (0)
45module_driver_test_single()
46{
47	printf "** Test: Running ${FUNCNAME[0]} for driver $1 and cpus_hotplug=$2 **\n\n"
48
49	if [ $2 -eq 1 ]; then
50		# offline all non-boot CPUs
51		for_each_non_boot_cpu offline_cpu
52		printf "\n"
53	fi
54
55	# insert module
56	printf "Inserting $1 module\n\n"
57	insmod $1
58	if [ $? != 0 ]; then
59		printf "Insmod $1 failed\n"
60		return;
61	fi
62
63	if [ $2 -eq 1 ]; then
64		# online all non-boot CPUs
65		for_each_non_boot_cpu online_cpu
66		printf "\n"
67	fi
68
69	# run basic tests
70	cpufreq_basic_tests
71
72	# remove module
73	printf "Removing $1 module\n\n"
74	rmmod $1
75	if [ $? != 0 ]; then
76		printf "rmmod $1 failed\n"
77		return;
78	fi
79
80	# There shouldn't be any cpufreq directories now.
81	for_each_cpu cpu_should_not_have_cpufreq_directory
82	printf "\n"
83}
84
85# $1: cpufreq-driver module to insert
86module_driver_test()
87{
88	printf "** Test: Running ${FUNCNAME[0]} **\n\n"
89
90	# check if module is present or not
91	ls $1 > /dev/null
92	if [ $? != 0 ]; then
93		printf "$1: not present in `pwd` folder\n"
94		return;
95	fi
96
97	# test basic module tests
98	test_basic_insmod_rmmod $1
99
100	# Do simple module test
101	module_driver_test_single $1 0
102
103	# Remove CPUs before inserting module and then bring them back
104	module_driver_test_single $1 1
105	printf "\n"
106}
107
108# find governor name based on governor module name
109# $1: governor module name
110find_gov_name()
111{
112	if [ $1 = "cpufreq_ondemand.ko" ]; then
113		printf "ondemand"
114	elif [ $1 = "cpufreq_conservative.ko" ]; then
115		printf "conservative"
116	elif [ $1 = "cpufreq_userspace.ko" ]; then
117		printf "userspace"
118	elif [ $1 = "cpufreq_performance.ko" ]; then
119		printf "performance"
120	elif [ $1 = "cpufreq_powersave.ko" ]; then
121		printf "powersave"
122	elif [ $1 = "cpufreq_schedutil.ko" ]; then
123		printf "schedutil"
124	fi
125}
126
127# $1: governor string, $2: governor module, $3: policy
128# example: module_governor_test_single "ondemand" "cpufreq_ondemand.ko" 2
129module_governor_test_single()
130{
131	printf "** Test: Running ${FUNCNAME[0]} for $3 **\n\n"
132
133	backup_governor $3
134
135	# switch to new governor
136	printf "Switch from $CUR_GOV to $1\n"
137	switch_show_governor $3 $1
138
139	# try removing module, it should fail as governor is used
140	printf "Removing $2 module\n\n"
141	rmmod $2
142	if [ $? = 0 ]; then
143		printf "WARN: rmmod $2 succeeded even if governor is used\n"
144		insmod $2
145	else
146		printf "Pass: unable to remove $2 while it is being used\n\n"
147	fi
148
149	# switch back to old governor
150	printf "Switchback to $CUR_GOV from $1\n"
151	restore_governor $3
152	printf "\n"
153}
154
155# Insert cpufreq governor module and perform basic tests
156# $1: cpufreq-governor module to insert
157module_governor_test()
158{
159	printf "** Test: Running ${FUNCNAME[0]} **\n\n"
160
161	# check if module is present or not
162	ls $1 > /dev/null
163	if [ $? != 0 ]; then
164		printf "$1: not present in `pwd` folder\n"
165		return;
166	fi
167
168	# test basic module tests
169	test_basic_insmod_rmmod $1
170
171	# insert module
172	printf "Inserting $1 module\n\n"
173	insmod $1
174	if [ $? != 0 ]; then
175		printf "Insmod $1 failed\n"
176		return;
177	fi
178
179	# switch to new governor for each cpu
180	for_each_policy module_governor_test_single $(find_gov_name $1) $1
181
182	# remove module
183	printf "Removing $1 module\n\n"
184	rmmod $1
185	if [ $? != 0 ]; then
186		printf "rmmod $1 failed\n"
187		return;
188	fi
189	printf "\n"
190}
191
192# test modules: driver and governor
193# $1: driver module, $2: governor module
194module_test()
195{
196	printf "** Test: Running ${FUNCNAME[0]} **\n\n"
197
198	# check if modules are present or not
199	ls $1 $2 > /dev/null
200	if [ $? != 0 ]; then
201		printf "$1 or $2: is not present in `pwd` folder\n"
202		return;
203	fi
204
205	# TEST1: Insert gov after driver
206	# insert driver module
207	printf "Inserting $1 module\n\n"
208	insmod $1
209	if [ $? != 0 ]; then
210		printf "Insmod $1 failed\n"
211		return;
212	fi
213
214	# run governor tests
215	module_governor_test $2
216
217	# remove driver module
218	printf "Removing $1 module\n\n"
219	rmmod $1
220	if [ $? != 0 ]; then
221		printf "rmmod $1 failed\n"
222		return;
223	fi
224
225	# TEST2: Insert driver after governor
226	# insert governor module
227	printf "Inserting $2 module\n\n"
228	insmod $2
229	if [ $? != 0 ]; then
230		printf "Insmod $2 failed\n"
231		return;
232	fi
233
234	# run governor tests
235	module_driver_test $1
236
237	# remove driver module
238	printf "Removing $2 module\n\n"
239	rmmod $2
240	if [ $? != 0 ]; then
241		printf "rmmod $2 failed\n"
242		return;
243	fi
244}
245