driver_load_check.sh 3.9 KB
Newer Older
liumg's avatar
liumg committed
1
2
3
#!/bin/bash
#huangjun@hygon.cn
#v0.2
liumg's avatar
liumg committed
4
# 设备ID正则表达式 - 支持DCU设备
liumg's avatar
liumg committed
5
DEVICE_ID="1d94:(5|6)[0-9a-z]{3,3}"
liumg's avatar
liumg committed
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28

# 设备名称映射 - 根据设备系列号匹配
declare -A pn
pn["55b7"]="zifang"    # 子房
pn["54b7"]="zifang"	   # 子房
pn["62b7"]="kongming"  # 孔明 
pn["6210"]="zhongda"   # 仲达
pn["6211"]="zhongda"   # 仲达
pn["6320"]="bowen"     # 伯温
pn["6430"]="bowen"     # 伯温

# 提取完整设备ID 
dev_id=$(lspci -nn | grep -oE "1d94:(5|6)[0-9a-z]{3,3}" | head -n 1)
# 提取设备系列号部分 (如 55b7)
dev_series=${dev_id#*:}

# 根据系列号确定设备名称
devname=${pn[$dev_series]}
if [ -z "$devname" ]; then
    echo "未识别的设备型号: $dev_id"
    echo "支持的设备型号: 55b7(zifang), 62b7(kongming), 6210(zhongda), 6320(bowen), 6430(bowen)"
    exit 1
fi
liumg's avatar
liumg committed
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
echo "===THIS SCRIPT JUST FOR 5.16.21  5.2 V1.10 and later==="

function ko_is_loaded()
{
	local st=$(lsmod | grep "\<$1\>")
	local ret=yes
	if [ "$st" = "" ];then
		ret="no"
	fi
	echo $ret
}

function have_mod()
{
	echo "$(modinfo $1)"
}

function check_iommu()
{
	if [ "$(ko_is_loaded iommu_v2)" = "yes" ];then
		echo "use iommu_v2, ready"
		return 0
	fi

	if [ "$(ko_is_loaded amd_iommu_v2)" = "yes" ];then
		echo "use amd_iommu_v2, ready"
		return 0
	fi

	if [ "$(have_mod iommu_v2)" != "" ];then
		echo "have iommu_v2 in disk, but not loaded"
		return -1
	fi

	if [ "$(have_mod amd_iommu_v2)" != "" ];then
		echo "have amd_iommu_v2 in disk, but not loaded"
		return -2
	fi

	echo "no iommu driver on this system"
	return -3
}

function check_vfio_pci()
{
	if [ "$(ko_is_loaded vfio-pci)" = "yes" ];then
		echo "Some device have attach to VM"
		echo "pls check it"
	fi
}

function _have_read_perm()
{
	[[ -r $1 ]] && echo "yes"
}

function _find_ucode_in_path()
{
	local u=$2
	local p=$1
	local au=""
	local tc=0
	local cnt=0

liumg's avatar
liumg committed
93
94
	au="$(find $p -name "${devname}_${u}.bin" 2> /dev/null)"
	tc=$(find $p -name "${devname}_${u}.bin" 2> /dev/null | wc -l)
liumg's avatar
liumg committed
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
	cnt=$(($cnt + $tc))
	echo $au
	return $cnt
}

function check_ucode()
{
	local ucodes="sdma sdma1 mec mec2 rlc smu"
	local v=$(uname -r)
	local paths="/lib/firmware/updates/$v /lib/firmware/updates/ /lib/firmware/$v"
	local cnt=0
	local au=""
	local e=
	local u
	local p
	local rp

	for u in $ucodes;do
		for p in $paths;do
			au="$au $(_find_ucode_in_path $p $u)"
			cnt=$(($cnt + $?))
		done
		if [[ $cnt -gt 1 ]];then
			echo "our firmware is local:[/lib/firmware/$v]"
			echo "pls rmove the other firmware."
			echo "all:[$au]"
			e="yes"
		fi
		if [ "$cnt" = "0" ];then
			echo "no ${devname}_$u.bin found! pls reinstall driver."
			e="yes"
		fi
		for p in $au;do 
			local r=$(_have_read_perm $p)
			if [ "$r" != "yes" ];then
				echo "no read perm on firmware: $p"
				e="yes"
			fi
		done
		au=""
		cnt=0
	done
	if [ "$e" = "yes" ];then
		exit -1
	fi
	echo "firmware, ready"
}

function check_ko()
{
liumg's avatar
liumg committed
145
	local kos="hycu.ko  hycu-sched.ko  hydrm_ttm_helper.ko  hy-extra.ko  hykcl.ko  hyttm.ko"
liumg's avatar
liumg committed
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
	local dir="/opt/hyhal/dkms/"
	local ret=

	for k in $kos;do
		local r=$(_have_read_perm $dir/$k)
		if [ "$r" != "yes" ];then
			ret="$ret $k"
		fi
	done
	if [ "$ret" != "" ];then
		echo "no driver installed or loss read perm"
		echo "pls check[$kos] in $dir"
		exit -1
	fi
	echo "dcu ko, ready"
}

function check_cuser_if_video()
{
	local r=$(cat /etc/group | grep video | grep $USER)
	if [ "$r" = "" ];then
		echo "you should add user:$USER to video group. sudo usermod -aG video $USER"
		exit -1
	fi
	echo "user group, ready"
}

function check_system_cap()
{
	if [ -r /sys/fs/selinux/enforce ] && [ "$(semodule -l | grep hydcu)" = "" ];then
		echo "system service no cap to load module"
		echo "pls install driver again"
		exit -1
	fi
	echo "system service policy, ready"
}

#0
if [ "$(ko_is_loaded hydcu)" = "yes" ];then
	echo "driver loaded"
	exit 0
fi
#1
check_iommu
check_vfio_pci
#2
check_ko
#3
liumg's avatar
liumg committed
194
# check_ucode
liumg's avatar
liumg committed
195
196
197
#4
check_system_cap
#5
liumg's avatar
liumg committed
198
# check_cuser_if_video
liumg's avatar
liumg committed
199
200

echo "驱动检查结束,没有发现明显问题"