summaryrefslogtreecommitdiffstats
path: root/meta-xilinx-demos/recipes-examples/hellopm/files/hellopm.sh
blob: 8b4dd75e301a4f58020f7b259ce4141f0da5143f (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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
#!/bin/sh

#*******************************************************************************
#
# Copyright (C) 2019 Xilinx, Inc.  All rights reserved.
# Copyright (c) 2022-2023 Advanced Micro Devices, Inc. All Rights Reserved.
# 
# Permission is hereby granted, free of charge, to any person obtaining a copy of
# this software and associated documentation files (the "Software"), to deal in
# the Software without restriction, including without limitation the rights to
# use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
# of the Software, and to permit persons to whom the Software is furnished to do
# so, subject to the following conditions:
# 
# The above copyright notice and this permission notice shall be included in all 
# copies or substantial portions of the Software.
# 
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 
# AUTHORS OR COPYRIGHT HOLDERS  BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 
# SOFTWARE.
#
# Author:     Will Wong <willw@xilinx.com>
#
# ******************************************************************************

# Xilinx Power Management Demo

# Stop on errors
set -e

suspend_menu () {
	while true; do
		cat <<-EOF

		++++++++++++++++++
		+ Suspend/Resume +
		++++++++++++++++++

		EOF

		cat <<-EOF

		This operation suspends the kernel and powers off the processor.  All
		peripherals not used as a result of this operation will be powered
		down or put into retention mode.  (Note that peripherals that are still
		being used by the RPU, such as memory, will not be powered down.)

		If no peripherals on the FPD are being used as a result of this operation,
		the FPD will be powered off.

		While the processor is suspended, all the data and context information is
		stored in the DRAM.  If the FPD is powered off, the DRAM will be put into
		self-refresh mode.

		Options:
		1. Suspend now, wake-up on Real-time Clock (RTC)
		0. Go Back

		EOF
		echo -n "Input: "
		read choice
		echo ""


		case $choice in
		0)
			break
			;;
		1)
			cat <<-EOF

			Suspending now, wake-up on Real-time Clock (RTC)

			Enable RTC as the wake-up source.
			Command: echo +10 > /sys/class/rtc/rtc0/wakealarm

			EOF

			echo +10 > /sys/class/rtc/rtc0/wakealarm

			cat <<-EOF

			Invoke suspend sequence
			Command: echo mem > /sys/power/state
			Waking up in about 10 seconds ...

			EOF
			sleep 1
			echo mem > /sys/power/state

			echo "Press RETURN to continue ..."
			read dummy
			;;
		esac
	done
	echo ""
}


cpu_hotplug_menu () {
	while true; do
		cat <<-EOF
		+++++++++++++++
		+ CPU Hotplug +
		+++++++++++++++

		EOF

		cat <<-EOF
		This operation brings individual CPU cores online and offline.  Processes
		running on a CPU core will be moved over to another core before the core
		is taken offline."

		CPU Hotlplug is different from, but can be operating along with, CPU Idle.
		The latter is having the kernel power off CPU cores when they are idling.

		Options:
		1. Check CPU3 Status
		2. Take CPU3 Offline
		3. Bring CPU3 Online
		0. Go Back

		EOF
		echo -n "Input: "
		read choice
		echo ""


		case $choice in
		0)
			break
			;;
		1)
			cat <<-EOF
			Check CPU3 Status

			Command: cat /sys/devices/system/cpu/cpu3/online

			EOF

			echo `cat /sys/devices/system/cpu/cpu3/online`
			echo ""
			echo "Press RETURN to continue ..."
			read dummy
			echo ""
			;;
		2)
			cat <<-EOF
			Take CPU3 Offline

			Command: echo 0 > /sys/devices/system/cpu/cpu3/online
			EOF
			echo `echo 0 > /sys/devices/system/cpu/cpu3/online`
			echo ""
			echo "Press RETURN to continue ..."
			read dummy
			echo ""
			;;
		3)
			cat <<-EOF
			Bring CPU3 Online

			Command: echo 1 > /sys/devices/system/cpu/cpu3/online
			EOF
			echo `echo 1 > /sys/devices/system/cpu/cpu3/online`
			echo ""
			echo "Press RETURN to continue ..."
			read dummy
			echo ""
			;;
		esac
	echo ""
	done
}

cpu_freq_menu () {
	while true; do
		cat <<-EOF
		++++++++++++
		+ CPU Freq +
		++++++++++++

		EOF

		cat <<-EOF
		This feature adjusts the CPU frequency on-the-fly.  All CPU cores are running
		at the same frequency.  A lower CPU frequency consumes less power, at the
		expense of performance.

		The CPU frequency can be adjusted automatically by a governor, which controls
		the CPU frequency based on its policy.  A special 'userspace' governor does
		not actually adjust the CPU frequency.  Instead, it allows the users to
		adjust the CPU frequency by themselves."

		Options:
		1. Show current CPU frequency
		2. List supported CPU frequencies
		3. Change CPU frequency
		0. Go Back

		EOF
		echo -n "Input: "
		read choice
		echo ""

		case $choice in
		0)
			break
			;;
		1)
			cat <<-EOF
			Show current CPU frequency

			Command: cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq
			EOF

			echo `cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq`
			echo ""
			echo "Press RETURN to continue ..."
			read dummy
			echo ""
			;;
		2)
			cat <<-EOF
			List supported CPU frequencies

			Command: cat /sys/devices/system/cpu/cpu1/cpufreq/scaling_available_frequencies
			EOF

			echo `cat /sys/devices/system/cpu/cpu1/cpufreq/scaling_available_frequencies`
			echo ""
			echo "Press RETURN to continue ..."
			read dummy
			echo ""
			;;
		3)
			echo "Change CPU frequency"
			echo ""
			echo -n "Enter new frequency: "
			read freq
			echo "Command: echo $freq > /sys/devices/system/cpu/cpu0/cpufreq/scaling_setspeed"
			echo `echo $freq > /sys/devices/system/cpu/cpu0/cpufreq/scaling_setspeed`
			echo ""
			echo "Press RETURN to continue ..."
			read dummy
			echo ""
			;;
		esac
	done
	echo ""
}

reboot_menu () {
	while true; do
		cat <<-EOF
		++++++++++
		+ Reboot +
		++++++++++

		EOF

		cat <<-EOF
		There are 3 types of reboot: APU-only, PS-only and System reboot. APU-only
		reboot will only reboot the APU.  The RPU, PMU and PL will not be affected.

		PS-only reboot will reboot the PS, including the APU, RPU and PMU.
		The PL will not be affected by a PS-only reboot, provided that the PL
		is completely isolated from the PS.

		System reboot will reboot the entire system, including the PS and the PL.

		Options:
		1. Reboot APU
		2. Reboot PS
		3. Reboot System
		0. Go Back

		EOF
		echo -n "Input: "
		read choice
		echo ""

		case $choice in
		0)
			break
			;;
		1)
			echo "Reboot APU"
			echo ""
			echo -n "This will end the demo.  Continue (Y/n)? "

			read continue

			if [ "$continue" != "n" ] && [ "$continue" != "N" ]; then 
				cat <<-EOF
				Set reboot scope to APU
				Command: echo "subsystem" > /sys/devices/platform/firmware\:zynqmp-firmware/shutdown_scope
				EOF
				echo `echo "subsystem" > /sys/devices/platform/firmware\:zynqmp-firmware/shutdown_scope`
				echo ""
				echo "Command: reboot"
				echo ""
				reboot
				while true; do
					read dummy
				done
			fi
			;;
		2)
			echo "Reboot PS"
			echo ""
			echo -n "This will end the demo.  Continue (Y/n)? "

			read continue

			if [ "$continue" != "n" ] && [ "$continue" != "N" ]; then
			  cat <<-EOF
				Set reboot scope to PS
				Command: echo "ps_only" > /sys/devices/platform/firmware\:zynqmp-firmware/shutdown_scope
				EOF
				echo `echo "ps_only" > /sys/devices/platform/firmware\:zynqmp-firmware/shutdown_scope`
				echo ""

				echo "Command: reboot"
				echo ""
				reboot
				while true; do
					read dummy
				done
			fi
			;;
		3)
			echo "Reboot System"
			echo ""
			echo -n "This will end the demo.  Continue (Y/n)? "

			read continue
			echo ""
			if [ "$continue" != "n" ] && [ "$continue" != "N" ]; then
				cat <<-EOF
				Set reboot scope to System
				Command: echo "system" > /sys/devices/platform/firmware\:zynqmp-firmware/shutdown_scope
				EOF

				echo `echo "system" > /sys/devices/platform/firmware\:zynqmp-firmware/shutdown_scope`
				echo ""
				echo "Command: reboot"
				echo ""
				reboot
				while true; do
					read dummy
				done
			fi
			;;
		esac
	done
	echo ""
}

shutdown_menu () {
	while true; do
		cat <<-EOF
		++++++++++++
		+ Shutdown +
		++++++++++++

		EOF

		cat <<-EOF
		The command will shut down the APU. The RPU and PL will not be affected.

		Options:
		1. Shutdown APU
		0. Go Back

		EOF

		echo -n "Input: "
		read choice
		echo ""

		case $choice in
		0)
			break
			;;
		1)
			echo "Shutdown APU"
			echo ""
			echo -n "This will end the demo.  Continue (Y/n)? "
			read continue
			echo ""
			if [ "$continue" != "n" ] && [ "$continue" != "N" ]; then
				echo "Command: shutdown -h now"
				echo ""
				shutdown -h now
				while true; do
					read dummy
				done
			fi
			;;
		esac
	done
	echo ""
}


main_menu () {
	while true; do

		cat <<-EOF

		+++++++++++++
		+ Main Menu +
		+++++++++++++

		EOF

                cat <<-EOF

		These are Linux-based power management features available for the
		Zynq (c) UltraScale+ MPSoC.  These examples assume you are using
		the ZCU102 board, although most of them are independent of the
		board type.

		Options:
		1. Suspend/Resume
		2. CPU Hotplug
		3. CPU Freq
		4. Reboot
		5. Shutdown
		0. Quit

		EOF

		echo -n "Input: "
		read choice
		echo ""

		case $choice in
		0)
			break
			;;
		1)
			suspend_menu
			;;
		2)
			cpu_hotplug_menu
			;;
		3)
			cpu_freq_menu
			;;
		4)
			reboot_menu
			;;
		5)
			shutdown_menu

		esac
	done
	echo ""
}

echo ""
cat <<-EOF
============================
      Hello PM World
Xilinx Power Management Demo
============================
EOF
echo ""

main_menu
echo ""