This repository has been archived on 2026-08-19. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
2026-08-20 00:45:43 +02:00

130 lines
4.7 KiB
Python

#!/usr/bin/env python3
"""Patch 3: amdgpu_gmc.c - KIQ bypass + Dead-GPU detection (2 sub-patches)"""
import sys
AMDGPU = "/home/fabian/kernel-build/linux-6.19.6/drivers/gpu/drm/amd/amdgpu"
filepath = f"{AMDGPU}/amdgpu_gmc.c"
with open(filepath, 'r') as f:
content = f.read()
if 'BC-250' in content:
print("Already patched, skipping.")
sys.exit(0)
# ========================================
# Patch 3a: KIQ bypass in amdgpu_gmc_flush_gpu_tlb_pasid
# Insert AFTER down_read_trylock block, BEFORE KIQ ring code
# ========================================
func_start = content.find('int amdgpu_gmc_flush_gpu_tlb_pasid')
if func_start == -1:
print("ERROR: amdgpu_gmc_flush_gpu_tlb_pasid not found")
sys.exit(1)
# Find the trylock check
trylock = content.find('down_read_trylock', func_start)
if trylock == -1:
print("ERROR: down_read_trylock not found")
sys.exit(1)
# Find "return 0;" after trylock
return_0 = content.find('return 0;', trylock)
end_line = content.find('\n', return_0) + 1
bypass = (
'\n'
'\t/* ===== BC-250 v2 PATCH: KIQ bypass for PASID flush ===== */\n'
'\t{\n'
'\t\tuint32_t gc_ver = amdgpu_ip_version(adev, GC_HWIP, 0);\n'
'\t\tpr_warn_once("amdgpu: flush_gpu_tlb_pasid called, GC_HWIP=0x%08x "\n'
'\t\t\t "(10.1.3=0x%08x) kiq_flag=%d\\n",\n'
'\t\t\t gc_ver, IP_VERSION(10, 1, 3),\n'
'\t\t\t adev->gmc.flush_pasid_uses_kiq);\n'
'\n'
'\t\tif ((gc_ver >= IP_VERSION(10, 1, 0)) && (gc_ver < IP_VERSION(10, 2, 0))) {\n'
'\t\t\tpr_warn_once("amdgpu: BC-250 KIQ bypass active "\n'
'\t\t\t\t "(gc_ver=0x%08x)\\n", gc_ver);\n'
'\t\t\tadev->gmc.gmc_funcs->flush_gpu_tlb_pasid(adev, pasid,\n'
'\t\t\t\t\t\t\t\t flush_type, all_hub,\n'
'\t\t\t\t\t\t\t\t inst);\n'
'\t\t\tr = 0;\n'
'\t\t\tgoto error_unlock_reset;\n'
'\t\t}\n'
'\t}\n'
'\t/* ===== BC-250 v2 PATCH END ===== */\n'
)
content = content[:end_line] + bypass + content[end_line:]
# ========================================
# Patch 3b: KIQ bypass + dead-GPU in amdgpu_gmc_fw_reg_write_reg_wait
# Insert BEFORE the KIQ ring submission code
# ========================================
func2_start = content.find('void amdgpu_gmc_fw_reg_write_reg_wait')
if func2_start == -1:
print("ERROR: amdgpu_gmc_fw_reg_write_reg_wait not found")
sys.exit(1)
# Find the first operational code after variable declarations
# Look for spin_lock_irqsave or ring->sched.ready
spinlock = content.find('spin_lock_irqsave', func2_start)
if spinlock == -1:
# Try ring->sched.ready
spinlock = content.find('ring->sched.ready', func2_start)
if spinlock == -1:
# Try any substantive code line
spinlock = content.find('if (', func2_start + 200)
if spinlock == -1:
print("ERROR: Could not find KIQ code in fw_reg_write_reg_wait")
sys.exit(1)
spinlock_line_start = content.rfind('\n', 0, spinlock) + 1
bypass2 = (
'\t/* ===== BC-250 v2+v3 PATCH: KIQ bypass + dead-GPU detection ===== */\n'
'\t{\n'
'\t\tuint32_t gc_ver = amdgpu_ip_version(adev, GC_HWIP, 0);\n'
'\t\tif ((gc_ver >= IP_VERSION(10, 1, 0)) && (gc_ver < IP_VERSION(10, 2, 0))) {\n'
'\t\t\tuint32_t tmp;\n'
'\n'
'\t\t\tpr_warn_once("amdgpu: BC-250 KIQ bypass active in "\n'
'\t\t\t\t "fw_reg_write_reg_wait (gc=0x%08x)\\n", gc_ver);\n'
'\n'
'\t\t\t/* v3: Health-check read before writing */\n'
'\t\t\ttmp = RREG32_NO_KIQ(reg1);\n'
'\t\t\tif (tmp == 0xFFFFFFFF) {\n'
'\t\t\t\tdev_err_ratelimited(adev->dev,\n'
'\t\t\t\t\t"BC-250: GPU unreachable in fw_reg_write_reg_wait "\n'
'\t\t\t\t\t"(reg1=0x%x returned 0xFFFFFFFF), skipping\\n", reg1);\n'
'\t\t\t\treturn;\n'
'\t\t\t}\n'
'\n'
'\t\t\tWREG32_NO_KIQ(reg0, ref);\n'
'\t\t\tfor (cnt = 0; cnt < adev->usec_timeout; cnt++) {\n'
'\t\t\t\ttmp = RREG32_NO_KIQ(reg1);\n'
'\t\t\t\t/* v3: Dead-GPU detection in polling loop */\n'
'\t\t\t\tif (tmp == 0xFFFFFFFF) {\n'
'\t\t\t\t\tdev_err_ratelimited(adev->dev,\n'
'\t\t\t\t\t\t"BC-250: GPU died during reg_write_reg_wait "\n'
'\t\t\t\t\t\t"(0xFFFFFFFF at reg1=0x%x)\\n", reg1);\n'
'\t\t\t\t\treturn;\n'
'\t\t\t\t}\n'
'\t\t\t\tif ((tmp & mask) == (ref & mask))\n'
'\t\t\t\t\treturn;\n'
'\t\t\t\tudelay(1);\n'
'\t\t\t}\n'
'\t\t\tdev_warn(adev->dev, "BC-250: MMIO reg write/wait timeout "\n'
'\t\t\t\t "reg0=0x%x reg1=0x%x\\n", reg0, reg1);\n'
'\t\t\treturn;\n'
'\t\t}\n'
'\t}\n'
'\t/* ===== BC-250 v2+v3 PATCH END ===== */\n'
)
content = content[:spinlock_line_start] + bypass2 + content[spinlock_line_start:]
with open(filepath, 'w') as f:
f.write(content)
print("Patch 3 applied: amdgpu_gmc.c - 2 sub-patches (KIQ bypass + dead-GPU)")