# Framework Laptop 13 AMD Ryzen 7040 系列筆電連接外接螢幕時會觸發白色畫面閃爍問題排查
紀錄問題的相關細節與(可能的話)臨時排解或解決方案
<https://hackmd.io/@brlin/fw13-amd-external-display-bug-troubleshooting>
## 內容大綱<br>Table of contents
[TOC]
## 智慧財產授權條款<br>Intellectual property licensing
除另外標註之內容外本作品的內容以[《Creative Commons 姓名標示-相同方式分享》授權條款第 4.0 國際版](https://creativecommons.org/licenses/by-sa/4.0/deed.zh_TW)或其任意更近期版本釋出供大眾*於授權範圍內*自由使用
請使用 `林博仁(Buo-ren, Lin) <https://brlin.me>` 作為滿足「姓名標示」授權要求的表彰內容
如有任何問題或其他要求請聯繫 <buo.ren.lin+legal@gmail.com>
## 症狀<br>Symptoms
以下說明本問題的相關症狀細節:
* 筆電內建螢幕與外部顯示器畫面出現大範圍純白塊狀閃爍
* 作業系統核心日誌出現大量下列警告與錯誤紀錄:
```dmesg
amd_iommu_report_page_fault: 43347 callbacks suppressed
amdgpu 0000:c1:00.0: AMD-Vi: Event logged [IO_PAGE_FAULT domain=0x0005 address=0xfffa3c00000 flags=0x0000]
amdgpu 0000:c1:00.0: AMD-Vi: Event logged [IO_PAGE_FAULT domain=0x0005 address=0xfffa3c01000 flags=0x0000]
amdgpu 0000:c1:00.0: AMD-Vi: Event logged [IO_PAGE_FAULT domain=0x0005 address=0xfffa3c02000 flags=0x0000]
```
```dmesg
WaylandEventThr[121196]: segfault at 20 ip 00007fb0b46dfd25 sp 00007fb0acffead0 error 4 likely on CPU 12 (core 6, socket 0)
Code: 08 42 8b 4c 0e fc 42 89 4c 08 fc eb b4 0f 1f 84 00 00 00 00 00 41 56 49 89 d0 41 55 41 54 49 89 cc 55 48 89 fd 53 48 83 ec 10 <48> 8b 5f 08 64 48 8b 04 25 28 00 00 00 48 89 44 24 08 31 c0 48 89
MediaPD~oder #2[124682]: segfault at 7fa34f49e340 ip 00007fa34f49e340 sp 00007fa35d6ba478 error 15 likely on CPU 2 (core 1, socket 0)
Code: 00 00 00 2c ba 64 a3 7f 00 00 00 fe 34 52 a3 7f 00 00 20 00 00 00 e5 e5 e5 e5 00 52 c8 58 a3 7f 00 00 d0 79 7e 50 a3 7f 00 00 <c0> 7f a5 4e a3 7f 00 00 20 25 0d 54 a3 7f 00 00 88 44 ef 4a a3 7f
traps: MediaPD~oder #2[124928] general protection fault ip:7fc47a83d504 sp:7fc47b62b5f0 error:0 in radeonsi_drv_video.so[7fc47a553000+86f000]
MediaPD~oder #1[125027]: segfault at 7f1c1af84300 ip 00007f1c1af84300 sp 00007f1c255df478 error 15 likely on CPU 12 (core 6, socket 0)
Code: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 <70> 77 e1 2b 1c 7f 00 00 00 00 00 00 00 00 00 00 09 1e 65 2c 1c 7f
traps: MediaPD~oder #1[125202] general protection fault ip:7f5268047257 sp:7f52691043a0 error:0
traps: MediaPD~oder #3[125220] trap stack segment ip:7f52497347ec sp:7f525fea7300 error:0
in radeonsi_drv_video.so[7f526802c000+86f000]
in radeonsi_dri.so[7f5249714000+1196000]
```
## 重現環境<br>Reproducing environment
以下說明重現本問題的環境細節:
### 中央處理器<br>CPU
AMD Ryzen 7 7840U
### 圖形處理器<br>GPU
AMD Radeon 780M Graphics
### 作業系統<br>Operating System
Ubuntu 23.10 AMD64
### 作業系統核心<br>Operating system kernel
6.5.0-1015-oem(Canonical OEM-D kernel)
### 桌面環境<br>Desktop environment
KDE Plasma(Wayland 模式)
### 外部顯示器<br>External display
DELL P2314Ht(透過 HDMI 轉 DVI-D 埠轉接器連接 Framework Laptop 的 HDMI 擴充卡)
## 問題臨時排解方案<br>Workarounds
以下說明規避本問題的臨時排解方案:
### 使用 `iommu` Linux 作業系統核心參數變更 IOMMU 硬體的使用行為
根據 [Graphics Problems? — x86 IOMMU Support — The Linux Kernel documentation](https://www.kernel.org/doc/html/next/x86/iommu.html#id1) 文章的說明,如果系統出現顯示相關的問題可以嘗試將 `iommu` Linux 作業系統核心命令列參數的值設定為 `pt` (即直通(passthrough)模式)使系統僅在硬體輔助虛擬化情境中使用 IOMMU 硬體
使用此設定**將會降低系統安全性**,因為非硬體輔助虛擬化之情境將不會使用 IOMMU 限制主記憶體的存取,有可能造成敏感資訊洩漏或是讓攻擊者能夠提昇權限
設定流程:
1. *以 root 身份*確保 `/etc/default/grub.d` GRUB {置入式|drop-in}配置目錄存在
1. *以 root 身份*創建 `/etc/default/grub.d/fw13-amd7040-workaround-fix-white-flickering-when-connected-to-an-external-display.cfg` 置入式配置檔,加入下列設定:
```bash
# shellcheck shell=sh
# Workaround white flickering issue when the Framework Laptop 13 AMD7040
# series is connected to an external display
if ! system_manufacturer="$(dmidecode -s system-manufacturer)"; then
printf 'Error: Unable to query the system manufacturer name.\n' 1>&2
return 2
fi
if ! system_product_name="$(dmidecode -s system-product-name)"; then
printf 'Error: Unable to query the system product name.\n' 1>&2
return 2
fi
if test "${system_manufacturer}" = Framework \
&& test "${system_product_name}" = "Laptop 13 (AMD Ryzen 7040Series)"; then
printf 'Info: Framework Laptop 13 (AMD Ryzen 7040 series) detected, applying external display white flickering issue workaround...\n'
GRUB_CMDLINE_LINUX_DEFAULT="${GRUB_CMDLINE_LINUX_DEFAULT} iommu=pt"
fi
```
1. *以 root 身份*於文字界面終端機應用軟體執行下列命令以重新產生 GRUB 開機載入程式配置檔:
```bash
update-grub
```
1. 重新啟動電腦以套用設定變更
1. 於文字界面終端機應用軟體執行下列命令檢查配置是否生效:
```bash
grep_opts=(
# 只印出吻合比對式樣的部份內容
--only-matching
)
grep "${grep_opts[@]}" iommu=pt /proc/cmdline
```
命令應輸出下列內容:
```output
iommu=pt
```
## 參考資料<br>References
本作品於撰寫期間參考了下列素材的內容:
* [[SOLVED] - AMD-Vi IO_PAGE_FAULT (iommu issue) | Proxmox Support Forum](https://forum.proxmox.com/threads/amd-vi-io_page_fault-iommu-issue.108273/)
說明 IOMMU group 成員的查詢命令
* [x86 IOMMU Support — The Linux Kernel documentation](https://www.kernel.org/doc/html/next/x86/iommu.html)
說明 IOMMU 的基本概念與顯示硬體相關的問題排解
* [[@ dri2_init_screen] - Firefox 123.0.1 Crash Report - Report ID: 8af46185-fa36-47dc-ac9a-65ed70240317](https://crash-stats.mozilla.org/report/index/8af46185-fa36-47dc-ac9a-65ed70240317#tab-details)
可能有關的 Mozilla Firefox 程式崩潰報告
* [The kernel’s command-line parameters — The Linux Kernel documentation](https://www.kernel.org/doc/html/v4.14/admin-guide/kernel-parameters.html)
說明 `iommu` Linux 作業系統核心命令列參數的用途與可用值
* [What does the `iommu=pt` Linux kernel command-line parameter and value pair do? | Google Gemini](https://gemini.google.com/share/39811851b4dc)
說明將 `iommu` Linux 作業系統核心命令列參數值設定為 `pt` 的效果以及其目的
* [GNU GRUB Manual: Simple configuration](https://www.gnu.org/software/grub/manual/grub/html_node/Simple-configuration.html)
說明 `GRUB_CMDLINE_LINUX` 與 `GRUB_CMDLINE_LINUX_DEFAULT` `grub-mkconfig` 程式配置選項的差異
* [Bug #901600 “Allow /etc/default/grub overriding via /etc/default...” : Bugs : grub2 package : Ubuntu](https://bugs.launchpad.net/ubuntu/+source/grub2/+bug/901600)
[default-grub-d.patch | Package: grub2 | Debian Sources](https://sources.debian.org/patches/grub2/2.06-3~deb11u6/default-grub-d.patch/)
[Changes/EtcDefaultGrubD - Fedora Project Wiki](https://fedoraproject.org/wiki/Changes/EtcDefaultGrubD)
說明 /etc/default/grub.d GRUB {置入式|drop-in}配置目錄的使用方式
<style>
/* 調大旁註文字的字元大小 */
rt{
font-size: 10pt;
}
</style>