-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtestgpu.sh
More file actions
367 lines (301 loc) · 9.98 KB
/
testgpu.sh
File metadata and controls
367 lines (301 loc) · 9.98 KB
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
#!/bin/bash
# GPU Permission and Groups Fix Script for RK3588
# Comprehensive fix for GPU access permissions and user groups
set -e
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
CYAN='\033[0;36m'
NC='\033[0m'
print_header() {
echo -e "${CYAN}================================${NC}"
echo -e "${CYAN}$1${NC}"
echo -e "${CYAN}================================${NC}"
}
print_success() {
echo -e "${GREEN}✓ $1${NC}"
}
print_warning() {
echo -e "${YELLOW}⚠ $1${NC}"
}
print_error() {
echo -e "${RED}✗ $1${NC}"
}
print_info() {
echo -e "${BLUE}ℹ $1${NC}"
}
# Check if running as root
if [[ $EUID -eq 0 ]]; then
print_error "This script should not be run as root"
print_info "Run as regular user - script will use sudo when needed"
exit 1
fi
print_header "GPU Permission and Groups Fix Script"
echo "System: $(uname -a)"
echo "Current user: $(whoami)"
echo "Date: $(date)"
# All GPU-related groups to add user to
GPU_GROUPS=(
"video" # Primary GPU access group
"render" # DRM render nodes access
"audio" # Sometimes needed for multimedia acceleration
"input" # Input devices (for some GPU applications)
"dialout" # Serial devices (sometimes used by GPU tools)
"plugdev" # Pluggable devices
"users" # General users group
)
# Check current user groups
print_header "Current User Groups Analysis"
echo "Current groups for user $(whoami):"
current_groups=$(groups)
echo "$current_groups"
# Check which groups are missing
missing_groups=()
for group in "${GPU_GROUPS[@]}"; do
if echo "$current_groups" | grep -q "\b$group\b"; then
print_success "User is in group: $group"
else
print_warning "User is NOT in group: $group"
missing_groups+=("$group")
fi
done
# Check GPU device permissions
print_header "GPU Device Permissions Analysis"
echo "GPU device files:"
if ls /dev/mali* >/dev/null 2>&1; then
ls -la /dev/mali*
for device in /dev/mali*; do
if [ -r "$device" ] && [ -w "$device" ]; then
print_success "Can read/write: $device"
else
print_warning "Cannot access: $device"
fi
done
else
print_error "No Mali devices found"
fi
echo -e "\nDRM device files:"
if ls /dev/dri/* >/dev/null 2>&1; then
ls -la /dev/dri/*
for device in /dev/dri/renderD*; do
if [ -r "$device" ] && [ -w "$device" ]; then
print_success "Can read/write: $device"
else
print_warning "Cannot access: $device"
fi
done
else
print_error "No DRM devices found"
fi
# Check if groups exist on system
print_header "System Groups Verification"
for group in "${GPU_GROUPS[@]}"; do
if getent group "$group" >/dev/null 2>&1; then
print_success "Group exists: $group"
else
print_warning "Group does not exist: $group"
fi
done
# Install required packages
print_header "Installing Required Packages"
print_info "Updating package list..."
sudo apt update
packages_to_install=(
"mesa-utils"
"mesa-utils-extra"
"libegl1-mesa-dev"
"libgles2-mesa-dev"
"libdrm-dev"
"libgbm-dev"
"libwayland-egl1-mesa"
"clinfo"
"vulkan-tools"
"libvulkan1"
)
print_info "Installing graphics packages..."
for package in "${packages_to_install[@]}"; do
if dpkg -l | grep -q "^ii $package "; then
print_success "Already installed: $package"
else
print_info "Installing: $package"
sudo apt install -y "$package" || print_warning "Failed to install: $package"
fi
done
# Add user to groups
print_header "Adding User to Required Groups"
if [ ${#missing_groups[@]} -gt 0 ]; then
print_info "Adding user $(whoami) to missing groups..."
# Build usermod command
groups_string=$(IFS=,; echo "${missing_groups[*]}")
print_info "Adding to groups: $groups_string"
sudo usermod -a -G "$groups_string" "$(whoami)"
print_success "User added to groups: $groups_string"
else
print_success "User is already in all required groups"
fi
# Fix device permissions
print_header "Fixing Device Permissions"
# Fix Mali devices
if ls /dev/mali* >/dev/null 2>&1; then
for device in /dev/mali*; do
print_info "Setting permissions for: $device"
sudo chmod 666 "$device"
sudo chown root:video "$device"
print_success "Fixed permissions: $device"
done
else
print_warning "No Mali devices to fix"
fi
# Fix DRM devices
if ls /dev/dri/* >/dev/null 2>&1; then
for device in /dev/dri/renderD*; do
print_info "Setting permissions for: $device"
sudo chmod 666 "$device"
sudo chown root:render "$device"
print_success "Fixed permissions: $device"
done
for device in /dev/dri/card*; do
print_info "Setting permissions for: $device"
sudo chmod 666 "$device"
sudo chown root:video "$device"
print_success "Fixed permissions: $device"
done
else
print_warning "No DRM devices to fix"
fi
# Create udev rules for persistent permissions
print_header "Creating Persistent udev Rules"
print_info "Creating udev rules for GPU devices..."
sudo tee /etc/udev/rules.d/99-gpu-permissions.rules > /dev/null << 'EOF'
# GPU device permissions for RK3588 Mali-G610
# Mali GPU devices
KERNEL=="mali[0-9]*", GROUP="video", MODE="0666"
# DRM devices
KERNEL=="card[0-9]*", GROUP="video", MODE="0666"
KERNEL=="renderD[0-9]*", GROUP="render", MODE="0666"
KERNEL=="controlD[0-9]*", GROUP="video", MODE="0666"
# NPU devices (if present)
KERNEL=="rknpu[0-9]*", GROUP="video", MODE="0666"
# General GPU-related devices
SUBSYSTEM=="drm", GROUP="video", MODE="0666"
SUBSYSTEM=="gpu", GROUP="video", MODE="0666"
EOF
print_success "Created udev rules: /etc/udev/rules.d/99-gpu-permissions.rules"
# Reload udev rules
print_info "Reloading udev rules..."
sudo udevadm control --reload-rules
sudo udevadm trigger
print_success "udev rules reloaded"
# Set environment variables
print_header "Setting Environment Variables"
# Create GPU environment file
env_file="$HOME/.gpu_environment"
cat > "$env_file" << 'EOF'
# GPU Environment Variables for RK3588 Mali-G610
export MESA_LOADER_DRIVER_OVERRIDE=panfrost
export MESA_GL_VERSION_OVERRIDE=3.3
export MESA_GLSL_VERSION_OVERRIDE=330
export EGL_PLATFORM=drm
export GBM_BACKEND=panfrost
export LIBGL_DRIVERS_PATH=/usr/lib/aarch64-linux-gnu/dri
export LIBGL_ALWAYS_SOFTWARE=0
export GALLIUM_DRIVER=panfrost
EOF
print_success "Created GPU environment file: $env_file"
# Add to shell profiles
for profile in ~/.bashrc ~/.profile ~/.zshrc; do
if [ -f "$profile" ]; then
if ! grep -q "gpu_environment" "$profile"; then
echo "" >> "$profile"
echo "# GPU Environment Variables" >> "$profile"
echo "if [ -f ~/.gpu_environment ]; then" >> "$profile"
echo " source ~/.gpu_environment" >> "$profile"
echo "fi" >> "$profile"
print_success "Added GPU environment to: $profile"
else
print_info "GPU environment already in: $profile"
fi
fi
done
# Create GPU optimization script
print_header "Creating GPU Optimization Script"
cat > "$HOME/gpu_optimize.sh" << 'EOF'
#!/bin/bash
# GPU Optimization Script for RK3588 Mali-G610
echo "=== GPU Optimization ==="
# Set GPU governor to performance
if [ -w "/sys/class/devfreq/fb000000.gpu/governor" ]; then
echo performance | sudo tee /sys/class/devfreq/fb000000.gpu/governor
echo "✓ Set GPU governor to performance"
else
echo "⚠ Cannot access GPU governor"
fi
# Set GPU frequencies
if [ -w "/sys/class/devfreq/fb000000.gpu/min_freq" ]; then
echo 400000000 | sudo tee /sys/class/devfreq/fb000000.gpu/min_freq
echo 1000000000 | sudo tee /sys/class/devfreq/fb000000.gpu/max_freq
echo "✓ Set GPU frequency range: 400-1000 MHz"
else
echo "⚠ Cannot access GPU frequency controls"
fi
# Show current GPU status
echo -e "\nCurrent GPU Status:"
if [ -r "/sys/class/devfreq/fb000000.gpu/cur_freq" ]; then
echo "Frequency: $(($(cat /sys/class/devfreq/fb000000.gpu/cur_freq) / 1000000)) MHz"
echo "Governor: $(cat /sys/class/devfreq/fb000000.gpu/governor)"
fi
echo "✓ GPU optimization completed"
EOF
chmod +x "$HOME/gpu_optimize.sh"
print_success "Created GPU optimization script: $HOME/gpu_optimize.sh"
# Test GPU access
print_header "Testing GPU Access"
# Source the new environment
source "$env_file"
# Test basic GPU commands
print_info "Testing basic GPU functionality..."
if command -v glxinfo >/dev/null 2>&1; then
if glxinfo >/dev/null 2>&1; then
print_success "glxinfo works"
echo "OpenGL Renderer: $(glxinfo | grep "OpenGL renderer" | cut -d: -f2 | xargs)"
else
print_warning "glxinfo failed"
fi
else
print_warning "glxinfo not available"
fi
if command -v clinfo >/dev/null 2>&1; then
if clinfo >/dev/null 2>&1; then
print_success "clinfo works"
echo "OpenCL Platforms: $(clinfo | grep -c "Platform #" || echo "0")"
else
print_warning "clinfo failed"
fi
else
print_warning "clinfo not available"
fi
# Summary
print_header "Summary and Next Steps"
print_success "GPU permission and groups fix completed!"
echo -e "\nChanges made:"
echo "• Added user to groups: ${missing_groups[*]:-none needed}"
echo "• Fixed device permissions for Mali and DRM devices"
echo "• Created persistent udev rules"
echo "• Set GPU environment variables"
echo "• Created GPU optimization script"
print_warning "IMPORTANT: To apply group changes, you must:"
echo "1. Logout and login again, OR"
echo "2. Restart your system, OR"
echo "3. Run: newgrp video && newgrp render"
print_info "After relogin/restart:"
echo "1. Source GPU environment: source ~/.gpu_environment"
echo "2. Optimize GPU: ./gpu_optimize.sh"
echo "3. Test GPU: ./testgpu_fixed.sh"
print_info "Useful commands:"
echo "• Check GPU status: cat /sys/class/devfreq/fb000000.gpu/cur_freq"
echo "• Test OpenGL: glxinfo | grep OpenGL"
echo "• Test OpenCL: clinfo"
echo "• Verify groups: groups"
echo -e "\nGPU setup completed at $(date)"
print_success "Ready to test GPU functionality!"