summaryrefslogtreecommitdiffstats
path: root/core/rootfs/rootfs-stage31/data/inc/drm.functions
blob: bbc7b7ad71b876573db8ca2565793a3c39bc0852 (plain) (blame)
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

grepmod () {
	local line
	line=$( grep -h -i -m1 "^$1\\s" /drm.cfg.d/* )
	[ -z "$line" ] && return
	echo ${line:${#1}}
}

# pass module name(s) relative path in /lib/modules with .ko extension, or special like @nvidia or @amd
load_gfx () {
	local MOD FILES RETVAL
	RETVAL=1 # default: failure
	while [ $# -gt 0 ]; do
		MOD=$(echo $1) # trim :)
		shift
		[ -z "$MOD" ] && continue
		if [ "x${MOD}" != "x${MOD#@}" ]; then
			# starts with '@' - special
			FILES=$( grepmod "$MOD" )
			[ -z "$FILES" ] && drop_shell "Could not find entry for special $MOD"
			if load_gfx $FILES; then
				RETVAL=0
			else
				# loading special case failed, try fallback if found
				MOD="${MOD}_fallback"
				FILES=$( grepmod "$MOD" )
				[ -n "$FILES" ] && load_gfx $FILES && RETVAL=0
			fi
		else # regular module name or filename
			if [ "x${MOD%.ko}" == "x${MOD}" ]; then
				# regular module name
				modprobe "$MOD" && RETVAL=0
			else
				# a .ko file
				insmod "/lib/modules/$MOD" && RETVAL=0
			fi
		fi
	done
	return $RETVAL
}

setup_gfx () {
	local KERN RETVAL CARD CARDS SUCCESS FILES DRM
	# check if forced by KCL
	if [ "x$GFX" == "xnvidia" ]; then
		load_gfx @nvidia
		return
	fi
	if [ "x$GFX" == "xamdgpu" ]; then
		load_gfx @amdgpu
		return
	fi
	if [ "x$GFX" == "xradeon" ]; then
		load_gfx @radeon
		return
	fi
	# not forced - check which driver to load
	CARDS=$(lspci | grep 'Class 0300' | awk '{print $4}')
	if [ -d "/drm.cfg.d" ] && [ -n "$CARDS" ]; then
		SUCCESS="yes"
		for CARD in $CARDS; do
			# look up exact pci id of this card
			[ $DEBUG -ge 1 ] && echo "Trying exact matching for drm drivers for $CARD"
			FILES=$( grepmod "$CARD" )
			load_gfx $FILES && continue
			# failed... try vendor id only
			CARD=${CARD:0:4}
			[ $DEBUG -ge 1 ] && echo "Trying vendor matching for drm drivers for $CARD"
			FILES=$( grepmod "$CARD" )
			load_gfx $FILES && continue
			# everything failed for this card
			echo "Unknown PCI vendor id: $CARD"
			SUCCESS="no"
		done
		[ "x$SUCCESS" == "xyes" ] && return 0
	fi
	# braindead fallback
	echo "At least one gfx card has no known drm drivers.... will load them all :/"
	KERN=$(uname -r)
	RETVAL=1
	[ -z "$KERN" ] && KERN=$(ls '/lib/modules' | grep '\.' | tail -n 1)
	for DRM in $(find "/lib/modules/$KERN/kernel/drivers/gpu/drm" -name "*.ko"); do
		DRM="$(basename "$DRM")"
		DRM="${DRM%.ko}"
		modprobe "$DRM" && RETVAL=0
	done
	return $RETVAL
}