Skip to content

Your GeForce runs AI at half speed. This fixes it for 20/30/40 series cards. Up to 2× FP16 / BF16 / TF32 tensor throughput

C

Release 2.0

Zagitta0532473b4b10
diff --git a/.github/ISSUE_TEMPLATE/bug-report.yml b/.github/ISSUE_TEMPLATE/bug-report.yml
index 1fafcfd..2e839aa 100644
--- a/.github/ISSUE_TEMPLATE/bug-report.yml
+++ b/.github/ISSUE_TEMPLATE/bug-report.yml
@@ -1,5 +1,5 @@
name: Bug report
-description: Something wrong with building, installing, packaging or boot ordering. For how a card behaved, use a compatibility report.
+description: Something wrong with building, installing, packaging or boot ordering. If a card did not unlock, use an unlock failure report.
body:
- type: textarea
id: what
diff --git a/.github/ISSUE_TEMPLATE/config.yml b/.github/ISSUE_TEMPLATE/config.yml
new file mode 100644
index 0000000..b4fe2f4
--- /dev/null
+++ b/.github/ISSUE_TEMPLATE/config.yml
@@ -0,0 +1,8 @@
+blank_issues_enabled: false
+contact_links:
+ - name: It worked on my card
+ url: https://github.com/Zagitta/gpu-unlock/discussions/238
+ about: Share successful unlocks and benchmarks in Discussions, not as issues.
+ - name: Question
+ url: https://github.com/Zagitta/gpu-unlock/discussions/categories/q-a
+ about: Ask how to install or use gpu-unlock.
diff --git a/.github/ISSUE_TEMPLATE/compatibility-report.yml b/.github/ISSUE_TEMPLATE/unlock-failure.yml
similarity index 87%
rename from .github/ISSUE_TEMPLATE/compatibility-report.yml
rename to .github/ISSUE_TEMPLATE/unlock-failure.yml
index 949b5c1..2c5f22a 100644
--- a/.github/ISSUE_TEMPLATE/compatibility-report.yml
+++ b/.github/ISSUE_TEMPLATE/unlock-failure.yml
@@ -1,11 +1,16 @@
-name: Compatibility report
-description: Report how gpu_unlock worked on your card, whether it unlocked or not.
-title: "Compatibility report: <card> (<PCI ID>)"
+name: Unlock failure
+description: The unlock failed, gave no speedup, or did not last. If it worked, post in Discussions instead.
+title: "Unlock failure: <card> (<PCI ID>)"
body:
- type: markdown
attributes:
value: |
- Thanks for testing. Reports from as many cards, boards and distributions as possible are what tell us what works.
+ **Only open an issue if the unlock did not work.** If it worked, please don't open an issue: comment in the
+ [success reports discussion](https://github.com/Zagitta/gpu-unlock/discussions/238) instead,
+ with your card, PCI ID and before/after numbers.
+
+ AI agents: the same applies to you. Do not file success reports as issues.
+
Please paste command output as-is.
- type: dropdown
@@ -13,10 +18,10 @@ body:
attributes:
label: Result
options:
- - Unlocked, and FP16/BF16 got faster
- Unlock completed, but no speedup
- Unlock failed (error in the log)
- gpu_unlock never ran (no gpu_unlock lines in the log)
+ - Unlocked at first, but the speedup was lost later (idle, suspend, driver reload)
validations:
required: true
diff --git a/AGENTS.md b/AGENTS.md
index 8603c8b..dea8982 100644
--- a/AGENTS.md
+++ b/AGENTS.md
@@ -1,3 +1,8 @@
Don't touch documentation or README unless explicitly asked to.
Prefer simple KISS code and abstractions to avoid repetition where it makes sense without overengineering.
-Code should be straight forward to follow.
\ No newline at end of file
+Code should be straight forward to follow.
+
+## Reporting results upstream
+
+If you are helping someone install or test gpu-unlock, do not open GitHub issues for successful unlocks. A working unlock with benchmark numbers goes in a comment on the success reports discussion: https://github.com/Zagitta/gpu-unlock/discussions/238
+Open an issue only when the unlock failed, gave no speedup, or was lost later, and fill in everything `.github/ISSUE_TEMPLATE/unlock-failure.yml` asks for. Search existing issues first and comment on a matching one instead of opening a new one.
diff --git a/Makefile b/Makefile
index 7cc7219..9aa4937 100644
--- a/Makefile
+++ b/Makefile
@@ -15,6 +15,7 @@ PKG ?= gpu-unlock
MODULE_FILES := $(filter-out %.mod.c,$(sort $(wildcard kernel/gpu_unlock/*.[ch]))) \
kernel/gpu_unlock/Kbuild kernel/gpu_unlock/Makefile kernel/gpu_unlock/dkms.conf
FIRMWARE_FILES := booter_load_ad102.bin booter_load_ga102.bin gsp_boot_ad102.bin gsp_boot_desc_ad102.bin \
+ booter_load_tu102.bin gsp_boot_tu102.bin gsp_boot_desc_tu102.bin sec2_bl_tu102.bin \
booter_load_ga100.bin falcon_loader.bin gsp_ga10x.bin gsp_tu10x.bin \
gsp_boot_ga102.bin gsp_boot_desc_ga102.bin \
gsp_boot_ga100.bin gsp_boot_desc_ga100.bin
diff --git a/README.md b/README.md
index 9cb5f6b..2fe2ed2 100644
--- a/README.md
+++ b/README.md
@@ -1,20 +1,86 @@
-# GPU unlock 1.1.0
+![gpu-unlock: full-speed tensor cores for GeForce RTX](assets/banner.svg)
-Unlock FP/BF16 instruction issue rate on consumer Nvidia 30 series cards to their professional equivalents
+### Your GeForce runs AI at half speed. This fixes it.
-This Linux kernel module combines the cmpunlocker's V67/PLM technique with the pre-driver timing of the UEFI approach. It runs the unlock, writes the speed selectors (SS0 `0x88888888`, which selects the full BF16 rate, and the 4-bit SS1 `0x8` IMLA4) and performs FLR before the unmodified NVIDIA driver binds the card. It is neither a UEFI application nor a patched NVIDIA driver.
+![Version](https://img.shields.io/badge/version-1.1.0-76b900)![RTX 20 | 30 | 40](https://img.shields.io/badge/GeForce-RTX%2020%20%7C%2030%20%7C%2040-76b900?logo=nvidia&logoColor=white)![Linux](https://img.shields.io/badge/platform-Linux-informational?logo=linux&logoColor=white)![License GPL-2.0](https://img.shields.io/badge/license-GPL--2.0-blue)![Stars](https://img.shields.io/github/stars/Zagitta/gpu-unlock?style=flat&color=yellow)
-The benefit of this early kmod driver is that it does not require manual patching each time nvidia releases a new driver version.
+**Up to 2× FP16 / BF16 / TF32 tensor throughput** · **+25–55% LLM prefill** · **+25–50% fine-tuning** · **+30–55% image generation**
-The effect of this unlock is that it doubles the instruction issuing speed of FP/BF16 FMA into FP32 operations on GA102, GA103, GA104 and GA106 cards.
+[Supported GPUs](#supported-gpus) · [Benchmarks](#what-you-get) · [Install](#how-to-install) · [FAQ](#faq) · [Support](#support-me)
-[3090 Ti dense 8192³ GEMM at 450 W, five-sample medians with FP32 accumulation](https://github.com/Zagitta/gpu-unlock/issues/1#issue-5905622687):
+---
-| Input | Before | After |
-| ----- | ------------ | ------------- |
-| FP16 | 87.46 TFLOPS | 118.39 TFLOPS |
-| BF16 | 87.50 TFLOPS | 157.41 TFLOPS |
-| TF32 | 42.58 TFLOPS | 82.61 TFLOPS |
+NVIDIA ships GeForce cards with their tensor cores capped to **half speed** for
+FP16, BF16 and TF32 math with FP32 accumulation, which is what nearly all AI
+training and unquantized inference uses. The same silicon sold as an RTX A-series
+or professional card runs at full rate. `gpu_unlock` removes that cap at boot,
+before the stock NVIDIA driver loads. No patched driver, no VBIOS flash, nothing
+to redo when NVIDIA releases a new driver.
+
+---
+
+## Supported GPUs
+
+| Generation | Cards | Status |
+| ------------------------- | ---------------------------------------------------------------------------------------------------------------------- | ------------------------- |
+| **Ampere** (RTX 30) | RTX 3050, 3060, 3060 Ti, 3070, 3070 Ti, 3080, 3080 Ti, 3090, 3090 Ti, plus RTX 3070/3080 Laptop GPUs | Stable, 100+ user reports |
+| **Ada Lovelace** (RTX 40) | RTX 4060, 4060 Ti, 4070, 4070 SUPER, 4070 Ti, 4070 Ti SUPER, 4080, 4080 SUPER, 4090, 4090 D, RTX 4050/4060 Laptop GPUs | Experimental |
+| **Turing** (RTX 20) | RTX 2060, 2060 SUPER, 2070, 2070 SUPER, 2080, 2080 SUPER, 2080 Ti, Titan RTX | Experimental |
+| Blackwell (RTX 50) | | Not supported |
+
+Laptops only stay unlocked if the dedicated GPU is never fully powered off.
+Many laptops power it down when idle, which silently resets the unlock. Turning
+off runtime power management with `options nvidia NVreg_DynamicPowerManagement=0x00`
+in `/etc/modprobe.d/` helps on some models, but expect mixed results.
+
+The exact PCI device IDs are in [`kernel/gpu_unlock/recipes.c`](kernel/gpu_unlock/recipes.c).
+Linux only. For Windows see the [FAQ](#faq).
+
+---
+
+## What you get
+
+The tensor core rate doubles. How much of that you see depends on how much of
+your workload is FP16/BF16/TF32 matrix math, and on whether the card has the
+power headroom to run it.
+
+Dense matrix multiply (FP32 accumulation, stock power limits):
+
+| Card | BF16 before | BF16 after | Gain |
+| --- | --- | --- | --- |
+| RTX 3060 12 GB | 27 TFLOPS | 51 TFLOPS | +87% |
+| RTX 3060 Ti | 37 TFLOPS | 68 TFLOPS | +82% |
+| RTX 3070 | 40 TFLOPS | 72 TFLOPS | +81% |
+| RTX 3080 10 GB | 65 TFLOPS | 113 TFLOPS | +73% |
+| RTX 3090 | 75 TFLOPS | 125 TFLOPS | +65% |
+| RTX 3090 Ti (450 W) | 88 TFLOPS | 157 TFLOPS | +80% |
+| RTX 4070 Ti | 81 TFLOPS | 150 TFLOPS | +84% |
+| RTX 4080 | 104 TFLOPS | 193 TFLOPS | +87% |
+| RTX 4090 | 165 TFLOPS | 329 TFLOPS | +100% (peak MMA rate) |
+
+The RTX 4090 runs out of power before it runs out of tensor cores, so large
+GEMMs at stock power limits gain much less than the peak rate suggests.
+
+Real workloads:
+
+| Workload | Typical speedup |
+| --- | --- |
+| LLM prefill / prompt processing, FP16/BF16 or AWQ/GPTQ (vLLM, PyTorch, Transformers) | +25–55% |
+| Time to first token | 25–30% lower |
+| LoRA and full fine-tuning in BF16 | +25–50% |
+| Image generation, FP16/BF16 (SDXL, SD 1.5, Krea, DiT models in ComfyUI) | +30–55% |
+| Video generation, BF16 compute (Wan 2.2) | +15–20% |
+| FlashAttention / SDPA kernels | +50–60% |
+| llama.cpp / Ollama with quantized GGUF | 0–15%, mostly at long context |
+| LLM decode / token generation (memory bound) | No change |
+| INT8, FP8 and INT4 models | No change |
+| Gaming and FP32 CUDA | No change |
+
+Outputs are bit-identical before and after; only the speed changes. These
+numbers come from the [compatibility reports](https://github.com/Zagitta/gpu-unlock/issues)
+users have filed, where you can find full benchmark details and post your own.
+
+---
## How to install
@@ -46,8 +112,10 @@ your boot images/entries (e.g. `sudo mkinitcpio -P` for mkinitcpio UKIs).
A: No the driver is only fetched to extract firmware this unlock depends on
- Q: What about Windows?
A: I don't run windows and don't plan to maintain it but [https://github.com/impacifist/Windows-GPU-UNLOCK](https://github.com/impacifist/Windows-GPU-UNLOCK) has built a port, YMMV.
-- Q: What about Turing and Ada?
-A: There are forks with support already, I will integrate this soon.
+- Q: What about Blackwell (RTX 50)?
+A: Not supported.
+- Q: Why did Nvidia handicap my GPU?
+A: Market segmentation, they want businesses to purchase the professional cards without these limitations at significantly higher prices.
- Q: Does this hurt my GPU?
A: It's very unlikely, this only unlocks a small handicap Nvidia has put in place to differentiate datacenter and professional cards from consumer versions. It may heat up your GPU slightly because it now does more compute but the standard throttling and power management remains completely intact and should downclock.
- Q: How can I assist?
@@ -81,6 +149,14 @@ pbuilder) needs `GPU_UNLOCK_INPUTS` pointing at that cache.
The `make dist` archive, the Debian source package and the `.nosrc.rpm`
contain no NVIDIA archives or generated firmware.
+## How it works
+
+This Linux kernel module combines the cmpunlocker's V67/PLM technique with the
+pre-driver timing of the UEFI approach. At boot it runs the unlock, writes the
+speed selectors (SS0 `0x88888888`, which selects the full BF16 rate, and the
+4-bit SS1 `0x8` IMLA4) and performs an FLR before the unmodified NVIDIA driver
+binds the card. It is neither a UEFI application nor a patched NVIDIA driver.
+
## Boot flow and opt-in
The packages put `gpu_unlock` and its firmware in the initramfs and load it
@@ -115,7 +191,7 @@ gpu_unlock.targets=0000:21:00.0,0000:22:00.0 # comma-separated
Addresses are full PCI names as `lspci -D` prints them (hex digits in either
case), independent of GPU numbering and display role. An entry that is not
-a supported recipe (GA100/GA102/GA103/GA104/GA106 or RTX 4090), including `all` mixed with addresses, is logged and ignored.
+a supported card (see [Supported GPUs](#supported-gpus)), including `all` mixed with addresses, is logged and ignored.
The cards are unlocked concurrently, about a second in all however many
there are, and each on its own: a card that is already bound or powered
down is skipped. Bus mastering that the board firmware left on (Supermicro
diff --git a/VERSION b/VERSION
index 9084fa2..227cea2 100644
--- a/VERSION
+++ b/VERSION
@@ -1 +1 @@
-1.1.0
+2.0.0
diff --git a/assets/banner.svg b/assets/banner.svg
new file mode 100644
index 0000000..fd2f992
--- /dev/null
+++ b/assets/banner.svg
@@ -0,0 +1,46 @@
+<svg xmlns="http://www.w3.org/2000/svg" width="1280" height="320" viewBox="0 0 1280 320" font-family="-apple-system, BlinkMacSystemFont, 'Segoe UI', Helvetica, Arial, sans-serif">
+ <defs>
+ <linearGradient id="bg" x1="0" y1="0" x2="1" y2="1">
+ <stop offset="0" stop-color="#0a0d0b"/>
+ <stop offset="1" stop-color="#132a12"/>
+ </linearGradient>
+ <linearGradient id="hot" x1="0" y1="0" x2="1" y2="0">
+ <stop offset="0" stop-color="#5a8f00"/>
+ <stop offset="1" stop-color="#9be22d"/>
+ </linearGradient>
+ <pattern id="grid" width="32" height="32" patternUnits="userSpaceOnUse">
+ <path d="M32 0H0V32" fill="none" stroke="#ffffff" stroke-opacity="0.04"/>
+ </pattern>
+ </defs>
+ <rect width="1280" height="320" rx="16" fill="url(#bg)"/>
+ <rect width="1280" height="320" rx="16" fill="url(#grid)"/>
+
+ <text x="64" y="128" font-size="76" font-weight="800" fill="#ffffff" letter-spacing="-2">gpu<tspan fill="#76b900">-</tspan>unlock</text>
+ <text x="66" y="178" font-size="28" fill="#d6e6c8">Full-speed tensor cores for GeForce RTX</text>
+ <text x="66" y="214" font-size="20" fill="#8fa386">Up to 2× FP16 / BF16 / TF32 · Linux kernel module · stock NVIDIA driver</text>
+
+ <g font-size="17" font-weight="600">
+ <rect x="66" y="246" width="96" height="34" rx="17" fill="#76b900" fill-opacity="0.16" stroke="#76b900"/>
+ <text x="114" y="269" text-anchor="middle" fill="#b6ef5c">RTX 20</text>
+ <rect x="174" y="246" width="96" height="34" rx="17" fill="#76b900" fill-opacity="0.16" stroke="#76b900"/>
+ <text x="222" y="269" text-anchor="middle" fill="#b6ef5c">RTX 30</text>
+ <rect x="282" y="246" width="96" height="34" rx="17" fill="#76b900" fill-opacity="0.16" stroke="#76b900"/>
+ <text x="330" y="269" text-anchor="middle" fill="#b6ef5c">RTX 40</text>
+ </g>
+
+ <g transform="translate(760 72)">
+ <text x="0" y="0" font-size="16" font-weight="600" fill="#8fa386" letter-spacing="1.5">BF16 MATMUL · TFLOPS</text>
+
+ <text x="0" y="44" font-size="18" font-weight="700" fill="#ffffff">RTX 3090 Ti</text>
+ <rect x="0" y="56" width="207" height="18" rx="4" fill="#3a4a38"/>
+ <text x="217" y="71" font-size="16" fill="#8fa386">88 stock</text>
+ <rect x="0" y="80" width="369" height="18" rx="4" fill="url(#hot)"/>
+ <text x="379" y="95" font-size="16" font-weight="700" fill="#b6ef5c">157</text>
+
+ <text x="0" y="140" font-size="18" font-weight="700" fill="#ffffff">RTX 4080</text>
+ <rect x="0" y="152" width="244" height="18" rx="4" fill="#3a4a38"/>
+ <text x="254" y="167" font-size="16" fill="#8fa386">104 stock</text>
+ <rect x="0" y="176" width="454" height="18" rx="4" fill="url(#hot)"/>
+ <text x="464" y="191" font-size="16" font-weight="700" fill="#b6ef5c">193</text>
+ </g>
+</svg>
diff --git a/debian/changelog b/debian/changelog
index 8342a27..e61cd15 100644
--- a/debian/changelog
+++ b/debian/changelog
@@ -1,3 +1,9 @@
+gpu-unlock (2.0.0) UNRELEASED; urgency=medium
+
+ * Add Turing and Ada Lovelace support.
+
+ -- Zagitta <[email protected]> Tue, 27 Oct 2026 00:00:00 +0000
+
gpu-unlock (1.1.0) UNRELEASED; urgency=medium
* Initial release.
diff --git a/firmware/extract_boot_assets.py b/firmware/extract_boot_assets.py
index 2fcbffa..8c986c6 100644
--- a/firmware/extract_boot_assets.py
+++ b/firmware/extract_boot_assets.py
@@ -17,9 +17,11 @@ SOURCES = (
'g_bindata_kgspGetBinArchiveBooterLoadUcode_AD102.c',
'g_bindata_kgspGetBinArchiveBooterLoadUcode_GA100.c',
'g_bindata_kgspGetBinArchiveBooterLoadUcode_GA102.c',
+ 'g_bindata_kgspGetBinArchiveBooterLoadUcode_TU102.c',
'g_bindata_kgspGetBinArchiveGspRmBoot_AD102.c',
'g_bindata_kgspGetBinArchiveGspRmBoot_GA100.c',
'g_bindata_kgspGetBinArchiveGspRmBoot_GA102.c',
+ 'g_bindata_kgspGetBinArchiveGspRmBoot_TU102.c',
'g_bindata_ksec2GetBinArchiveBlUcode_TU102.c',
)
@@ -65,7 +67,7 @@ def _array_bytes(text: str, label: str) -> tuple[bytes, bool, int]:
return data, compressed, expected
-def build_bundle(source_text: str) -> bytes:
+def build_bundle(source_text: str, signature_size: int = 384) -> bytes:
image, _, _ = _array_bytes(source_text, "IMAGE_PROD")
header, _, _ = _array_bytes(source_text, "HEADER_PROD")
signatures, _, _ = _array_bytes(source_text, "SIG_PROD")
@@ -78,9 +80,9 @@ def build_bundle(source_text: str) -> bytes:
loc = struct.unpack("<I", patch_loc)[0]
_fuse, engine, ucode = struct.unpack("<3I", patch_meta)
count = struct.unpack("<I", num_sigs)[0]
- if not count or len(signatures) != count * 384 or fields[4] != 1 or not ucode:
+ if not count or len(signatures) != count * signature_size or fields[4] != 1 or not ucode:
raise ValueError('unsupported production Booter signature/application table')
- if loc < fields[2] or loc + 384 > fields[2] + fields[3] or fields[2] + fields[3] > len(image):
+ if loc < fields[2] or loc + signature_size > fields[2] + fields[3] or fields[2] + fields[3] > len(image):
raise ValueError('signature patch is outside DMEM image')
for offset, size in ((fields[0],fields[1]), (fields[2],fields[3]), (fields[5],fields[6])):
if offset + size > len(image) or offset % 256 or size % 256:
@@ -125,6 +127,21 @@ def extract_assets(source_root: Path, output: Path) -> None:
blobs['booter_load_ad102.bin'] = build_bundle(inputs['g_bindata_kgspGetBinArchiveBooterLoadUcode_AD102.c'])
blobs['gsp_boot_ad102.bin'] = _array_bytes(ad102_boot, 'UCODE_IMAGE_PROD')[0]
blobs['gsp_boot_desc_ad102.bin'] = _array_bytes(ad102_boot, 'UCODE_DESC_PROD')[0]
+ tu102_boot = inputs['g_bindata_kgspGetBinArchiveGspRmBoot_TU102.c']
+ tu102_image = _array_bytes(tu102_boot, 'UCODE_IMAGE')[0]
+ tu102_desc = _array_bytes(tu102_boot, 'UCODE_DESC')[0]
+ if len(tu102_desc) != 76 or struct.unpack('<I', tu102_desc[:4])[0] != 4 or \
+ len(tu102_image) != 4096:
+ raise ValueError('unsupported TU102 bootloader descriptor')
+ tu102_bl = _array_bytes(
+ inputs['g_bindata_ksec2GetBinArchiveBlUcode_TU102.c'], 'UCODE_IMAGE')[0]
+ if len(tu102_bl) != 768:
+ raise ValueError('unexpected TU102 SEC2 loader size')
+ blobs['booter_load_tu102.bin'] = build_bundle(
+ inputs['g_bindata_kgspGetBinArchiveBooterLoadUcode_TU102.c'], 16)
+ blobs['gsp_boot_tu102.bin'] = tu102_image
+ blobs['gsp_boot_desc_tu102.bin'] = tu102_desc
+ blobs['sec2_bl_tu102.bin'] = tu102_bl
output.mkdir(parents=True, exist_ok=True)
for name, data in blobs.items():
(output/name).write_bytes(data)
diff --git a/kernel/gpu_unlock/diagnostic.h b/kernel/gpu_unlock/diagnostic.h
index c1db4a9..ec28c5f 100644
--- a/kernel/gpu_unlock/diagnostic.h
+++ b/kernel/gpu_unlock/diagnostic.h
@@ -8,8 +8,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
-/* Userspace builds are the integration harness; keep diagnostics opt-in so
- * the acceptance output is readable. The kernel build is unchanged. */
+/* Userspace builds keep diagnostics opt-in via GPU_UNLOCK_DIAG. */
static inline int gu_diag_enabled(void)
{
const char *on = getenv("GPU_UNLOCK_DIAG");
diff --git a/kernel/gpu_unlock/dkms.conf b/kernel/gpu_unlock/dkms.conf
index 817b1fe..d747dc3 100644
--- a/kernel/gpu_unlock/dkms.conf
+++ b/kernel/gpu_unlock/dkms.conf
@@ -1,6 +1,6 @@
# SPDX-License-Identifier: GPL-2.0-only
PACKAGE_NAME="gpu-unlock"
-PACKAGE_VERSION="1.1.0"
+PACKAGE_VERSION="2.0.0"
BUILT_MODULE_NAME[0]="gpu_unlock"
DEST_MODULE_LOCATION[0]="/updates/dkms"
AUTOINSTALL="yes"
diff --git a/kernel/gpu_unlock/firmware_610.c b/kernel/gpu_unlock/firmware_610.c
index d723aaa..6d94e75 100644
--- a/kernel/gpu_unlock/firmware_610.c
+++ b/kernel/gpu_unlock/firmware_610.c
@@ -59,8 +59,10 @@ static int validate_booter(const struct firmware *fw, struct gpu_unlock_booter_h
image_size != expect->image_size ||
fw->size != 72u + image_size + sig_bytes)
return GU_DIAG(-EINVAL);
- /* Keep the signed GA100 and AD102 production geometries distinct. */
- if (hdr.ucode_id != 3 || hdr.engine_id != 1 || hdr.num_sigs != expect->num_sigs ||
+ /* Keep the signed GA100, GA10x, AD10x and TU10x production geometries
+ * distinct. */
+ if (hdr.ucode_id != expect->ucode_id || hdr.engine_id != 1 ||
+ hdr.num_sigs != expect->num_sigs ||
sig_bytes != expect->sig_bytes || hdr.os_code_off != 0 ||
hdr.os_code_size != 256 || hdr.os_data_off != expect->os_data_off ||
hdr.os_data_size != expect->os_data_size || hdr.app_code_off != 256 ||
@@ -258,3 +260,7 @@ MODULE_FIRMWARE(GU_FW_DESC_GA100);
MODULE_FIRMWARE(GU_FW_BOOTER_AD102);
MODULE_FIRMWARE(GU_FW_BOOT_AD102);
MODULE_FIRMWARE(GU_FW_DESC_AD102);
+MODULE_FIRMWARE(GU_FW_BOOTER_TU102);
+MODULE_FIRMWARE(GU_FW_BOOT_TU102);
+MODULE_FIRMWARE(GU_FW_DESC_TU102);
+MODULE_FIRMWARE(GU_FW_SEC2_BL_TU102);
diff --git a/kernel/gpu_unlock/main.c b/kernel/gpu_unlock/main.c
index de32b25..723d65c 100644
--- a/kernel/gpu_unlock/main.c
+++ b/kernel/gpu_unlock/main.c
@@ -213,4 +213,4 @@ module_init(gpu_unlock_init);
module_exit(gpu_unlock_exit);
MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("Ampere pre-driver hardware unlock");
-MODULE_VERSION("1.1.0");
+MODULE_VERSION("2.0.0");
diff --git a/kernel/gpu_unlock/payload.c b/kernel/gpu_unlock/payload.c
index 9b031ae..04efbfe 100644
--- a/kernel/gpu_unlock/payload.c
+++ b/kernel/gpu_unlock/payload.c
@@ -23,10 +23,15 @@ int gu_payload_check(const struct gu_payload *payload)
size_t i;
if (!payload || !payload->name || !payload->size || (payload->size & 3) ||
- payload->value_off > payload->size - 4 ||
- payload->address_off > payload->size - 4 ||
- (payload->word_count && !payload->words))
+ !payload->writes || payload->writes > GU_PAYLOAD_MAX_WRITES ||
+ !payload->slots || (payload->word_count && !payload->words))
return GU_DIAG(-EINVAL);
+ for (i = 0; i < payload->writes; i++)
+ if ((payload->slots[i].value_off &&
+ payload->slots[i].value_off > payload->size - 4) ||
+ (payload->slots[i].address_off &&
+ payload->slots[i].address_off > payload->size - 4))
+ return GU_DIAG(-EINVAL);
for (i = 0; i < payload->word_count; i++)
if (payload->words[i].offset > payload->size - 4)
return GU_DIAG(-EINVAL);
@@ -34,7 +39,7 @@ int gu_payload_check(const struct gu_payload *payload)
}
int gu_payload_build(const struct gu_payload *payload, u8 *image,
- u32 address, u32 value)
+ const struct gu_reg_write *writes, size_t count)
{
u32 offset;
size_t i;
@@ -45,11 +50,17 @@ int gu_payload_build(const struct gu_payload *payload, u8 *image,
ret = gu_payload_check(payload);
if (ret)
return ret;
+ if (count > payload->writes || (count && !writes))
+ return GU_DIAG(-EINVAL);
for (offset = 0; offset < payload->size; offset += 4)
put_word(image, offset, payload->fill);
for (i = 0; i < payload->word_count; i++)
put_word(image, payload->words[i].offset, payload->words[i].value);
- put_word(image, payload->value_off, value);
- put_word(image, payload->address_off, address);
+ for (i = 0; i < count; i++) {
+ if (payload->slots[i].value_off)
+ put_word(image, payload->slots[i].value_off, writes[i].value);
+ if (payload->slots[i].address_off)
+ put_word(image, payload->slots[i].address_off, writes[i].addr);
+ }
return 0;
}
diff --git a/kernel/gpu_unlock/payload.h b/kernel/gpu_unlock/payload.h
index 87f9e1b..a799a85 100644
--- a/kernel/gpu_unlock/payload.h
+++ b/kernel/gpu_unlock/payload.h
@@ -10,31 +10,54 @@ struct gu_payload_word {
u32 offset, value;
};
+/* One privileged BAR0 write: what the payload writes and where. expect is the
+ * readback value; zero means the written value. optional entries are attempted
+ * and logged on mismatch but do not fail the transaction. */
+struct gu_reg_write {
+ u32 addr, value;
+ u32 expect;
+ bool optional;
+};
+
+/* Slots the recipe reserves for one launch. A payload that can perform
+ * several writes per SEC2 start lists one pair per write, in issue order. */
+struct gu_payload_slot {
+ u32 value_off, address_off;
+};
+
+#define GU_PAYLOAD_MAX_WRITES 8
+
/* What a completed SEC2 launch must show besides a halt and a reopened
* RESET_PLM. Payloads whose exit returns the input metadata pointer in
- * MAILBOX0/1 use the echo variant; anything unmeasured stays on the generic
- * predicate rather than inventing a return convention. */
+ * MAILBOX0/1 use the echo variant; payloads that deliberately leave a
+ * completion marker in MAILBOX0 use the marker variant. Anything unmeasured
+ * stays on the generic predicate rather than inventing a return convention. */
enum gu_payload_completion {
GU_PAYLOAD_HALT_RESET_PLM,
GU_PAYLOAD_HALT_RESET_PLM_ECHO,
+ GU_PAYLOAD_HALT_RESET_PLM_MARKER,
+ GU_PAYLOAD_HALT_RESET_PLM_ZERO, /* MAILBOX0 must be zero */
};
/* One signed SEC2 Booter's DMEM program. Selected by the Booter, never by the
* family: the gadget and frame offsets are relative to that image's layout, so
- * another Booter cannot use them. The address and value slots carry the single
- * privileged write of one invocation. */
+ * another Booter cannot use them. slots[] carries the writes a single launch
+ * performs; the driver packs the family's write table into launches of that
+ * size and repeats the last write to fill a partial batch. */
struct gu_payload {
const char *name;
u32 size; /* signature allocation in bytes */
u32 fill; /* body dword */
- u32 value_off, address_off;
+ const struct gu_payload_slot *slots;
+ size_t writes; /* slots[] entries, writes per launch */
const struct gu_payload_word *words;
size_t word_count;
enum gu_payload_completion completion;
+ u32 marker; /* MAILBOX0 for GU_PAYLOAD_HALT_RESET_PLM_MARKER */
};
int gu_payload_check(const struct gu_payload *payload);
int gu_payload_build(const struct gu_payload *payload, u8 *image,
- u32 address, u32 value);
+ const struct gu_reg_write *writes, size_t count);
#endif
diff --git a/kernel/gpu_unlock/prep_610.c b/kernel/gpu_unlock/prep_610.c
index e1a37f3..ccc9e5f 100644
--- a/kernel/gpu_unlock/prep_610.c
+++ b/kernel/gpu_unlock/prep_610.c
@@ -164,6 +164,22 @@ int gpu_unlock_boot_desc_parse(const u8 *data, size_t size, u32 image_size,
static const unsigned int ranges[] = { 1, 3, 8, 10, 12 };
unsigned int i;
+ /* GA100's unchanged v4 monitor descriptor has no manifest or image-size
+ * fields. Keep this exact pinned geometry distinct from the v5 parser. */
+ if (data && out && size == 76 && image_size == 4096 && rd32(data) == 4) {
+ unsigned int j;
+ if (rd32(data + 4) || rd32(data + 8) != 1160 ||
+ rd32(data + 12) != 1160 || rd32(data + 16) != 16)
+ return GU_DIAG(-EINVAL);
+ for (j = 5; j < 19; j++)
+ if (rd32(data + j * 4))
+ return GU_DIAG(-EINVAL);
+ *out = (struct gpu_unlock_boot_desc) {
+ .code_offset = 0, .data_offset = 1160,
+ .manifest_offset = 0, .image_size = image_size,
+ };
+ return 0;
+ }
if (!data || !out || size != 84 || !image_size ||
rd32(data) != 5 || rd32(data + 56) != 1 ||
rd32(data + 76) != image_size || rd32(data + 80))
diff --git a/kernel/gpu_unlock/recipes.c b/kernel/gpu_unlock/recipes.c
index a6fbbf4..21e4aca 100644
--- a/kernel/gpu_unlock/recipes.c
+++ b/kernel/gpu_unlock/recipes.c
@@ -2,49 +2,196 @@
#include "recipes.h"
#include "registers.h"
-/* GA102 re-entrant V67 call table. The Booter dispatches the frame RET into
- * these gadgets and performs the privileged write from the value/address
- * slots, so one invocation applies one table entry. Offsets are relative to
- * the signature image, not to BAR0. */
-static const struct gu_payload_word ga102_v67_words[] = {
+/* GA102 re-entrant call table. The Booter dispatches the frame RET into
+ * these gadgets and performs the privileged writes from the value/address
+ * slots. Offsets are relative to the signature image, not to BAR0.
+ *
+ * The write entry point and the six-write frame layout are Petri Krohn's:
+ * enter reg_write_indirect() at 0x1110 with the address in r0 and the value in
+ * r1, so the 0x1fce parameter shuffle is unnecessary. Skipping the prologue
+ * also means r3 must already point at a word equal to r9 for the 0x1149 canary
+ * check (0x6140 works: it holds the same canary); otherwise 0x811b stores 0x47
+ * to I[0x1000] and spins. The skipped prologue also moves the epilogue frame:
+ * 0x1156 pops r3..r0 from [sp..sp+0xc], skips the canary slot at sp+0x10 and
+ * returns via sp+0x14, so one write consumes 0x18 bytes and the popped r0/r1
+ * carry the next write's parameters.
+ *
+ * Stack: writes start at 0xff54, one 0x18 frame each. Cleanup starts at
+ * 0xff54 + 6*0x18 = 0xffe4: 0x576b (SE/CSB handshake, data pointer derived
+ * from sp) leaves r0=3, 0x84c8 turns that into r10=3 while r11's low byte is
+ * already 0, then 0xd52 and the 0x81ee exit. The final return word is at
+ * 0xfff4; a seventh write would need 0x1000c. The driver batches the family's
+ * write table into launches of six and repeats the last entry to fill. */
+static const struct gu_payload_word ga102_words[] = {
{ 0x1100, 0x00000007 },
- { 0xf950, 0x00000d44 },
- { 0xf968, 0x00001fce },
- { 0xf974, 0x00000000 },
- { 0xf97c, 0x00001101 },
- { 0xf980, 0x000084c8 },
- { 0xf984, 0x00008e18 },
- { 0xf98c, 0x000084c8 },
- { 0xf990, 0x00000000 },
- { 0xf998, 0x00001fce },
- { 0xf9a4, 0x0000ffb0 },
- { 0xf9ac, 0x00005789 },
- { 0xf9bc, 0x00000d44 },
- { 0xf9cc, 0x00000003 },
- { 0xf9d4, 0x00001fce },
- { 0xf9e8, 0x00000d52 },
- { 0xf9ec, 0x000081ee },
+ { 0xf93c, 0x00006140 }, /* 0x686 pop: r3 canary pointer, write 1 */
+ { 0xf950, 0x00001110 }, /* write 1: r0=address, r1=value */
+ { 0xf954, 0x00006140 }, /* frame 1: r3 for write 2 */
+ { 0xf968, 0x00001110 }, /* frame 1: ret -> write 2 */
+ { 0xf96c, 0x00006140 }, /* frame 2: r3 for write 3 */
+ { 0xf980, 0x00001110 }, /* frame 2: ret -> write 3 */
+ { 0xf984, 0x00006140 }, /* frame 3: r3 for write 4 */
+ { 0xf998, 0x00001110 }, /* frame 3: ret -> write 4 */
+ { 0xf99c, 0x00006140 }, /* frame 4: r3 for write 5 */
+ { 0xf9b0, 0x00001110 }, /* frame 4: ret -> write 5 */
+ { 0xf9b4, 0x00006140 }, /* frame 5: r3 for write 6 */
+ { 0xf9c8, 0x00001110 }, /* frame 5: ret -> write 6 */
+ { 0xf9cc, 0x00006140 }, /* frame 6: r3 (unused by cleanup) */
+ { 0xf9d8, 0x00000003 }, /* frame 6: r0 = 3 for 0xd52 */
+ { 0xf9e0, 0x0000576b }, /* frame 6: ret -> SE/CSB handshake */
+ { 0xf9e4, 0x000084c8 }, /* cleanup: r10 = r0 = 3, ret 0xd52 */
+ { 0xf9f0, 0x00000d52 }, /* cleanup: final pass */
+ { 0xf9f4, 0x000081ee }, /* HS clean exit */
+};
+static const struct gu_payload_slot ga102_slots[] = {
+ { 0xf944, 0xf948 },
+ { 0xf95c, 0xf960 },
+ { 0xf974, 0xf978 },
+ { 0xf98c, 0xf990 },
+ { 0xf9a4, 0xf9a8 },
+ { 0xf9bc, 0xf9c0 },
};
#define GU_PAYLOAD_V67(label) { \
.name = label, \
.size = 0xfa00, \
.fill = 0x0000019c, \
- .value_off = 0xf948, \
- .address_off = 0xf960, \
- .words = ga102_v67_words, \
- .word_count = sizeof(ga102_v67_words) / sizeof(ga102_v67_words[0]), \
+ .slots = ga102_slots, \
+ .writes = sizeof(ga102_slots) / sizeof(ga102_slots[0]), \
+ .words = ga102_words, \
+ .word_count = sizeof(ga102_words) / sizeof(ga102_words[0]), \
.completion = GU_PAYLOAD_HALT_RESET_PLM, \
}
static const struct gu_payload ga102_v67 = GU_PAYLOAD_V67("ga102-v67");
-/* No payload has been derived for the GA100 or AD102 signed Booter yet; both
- * reuse the GA102 V67 body until one is. Replace the descriptor when it lands. */
+/* No payload has been derived for the GA100 signed Booter yet; it reuses the
+ * GA102 V67 body until one is. Replace the descriptor when it lands. */
static const struct gu_payload ga100_v67 = GU_PAYLOAD_V67("ga100-v67");
-static const struct gu_payload ad102_v67 = GU_PAYLOAD_V67("ad102-v67");
-/* Same per-entry image as GA10x. These are the stores, in order. */
+/* AD102 port of Petri Krohn's GA102 six-write chain. The two signed Booters
+ * are the same code with functions moved, so the GA102 gadgets map one-for-one
+ * through the fwmap: the signature epilogue 0x686 -> 0x1e02, the write entry
+ * 0x1110 -> 0x257e, the 0x84c8 gadget -> 0x7a02 and the HS exit
+ * 0x81ee -> 0x771e. The canary global moves 0x6140 -> 0x5f50, while the DMEM
+ * frame layout is unchanged: the signature epilogue still starts at 0xff30,
+ * so the six 0x18-byte write frames and their slots use the same offsets.
+ *
+ * The GA102 cleanup starts with the 0x576b status read at CSB address 0x114;
+ * AD102 has no exact counterpart for that function (the fwmap's fuzzy 0x73ec
+ * match performs a different table read), so the port keeps the AD102
+ * booter's own proven marker exit from the TOTORONG/AD103 chain:
+ * 0x7a02 moves the marker 0x67b00001 into r10, 0x31b5 publishes it to
+ * I[0x1000], and 0x771e exits. That matches this payload's MARKER completion
+ * and leaves the final return at 0xfff0. */
+static const struct gu_payload_slot ad102_v67_slots[] = {
+ { 0xf944, 0xf948 },
+ { 0xf95c, 0xf960 },
+ { 0xf974, 0xf978 },
+ { 0xf98c, 0xf990 },
+ { 0xf9a4, 0xf9a8 },
+ { 0xf9bc, 0xf9c0 },
+};
+static const struct gu_payload_word ad102_v67_words[] = {
+ { 0x1100, 0x00000007 },
+ { 0xf93c, 0x00005f50 }, /* 0x1e02 pop: r3 canary pointer, write 1 */
+ { 0xf950, 0x0000257e }, /* write 1: r0=address, r1=value */
+ { 0xf954, 0x00005f50 }, /* frame 1: r3 for write 2 */
+ { 0xf968, 0x0000257e }, /* frame 1: ret -> write 2 */
+ { 0xf96c, 0x00005f50 }, /* frame 2: r3 for write 3 */
+ { 0xf980, 0x0000257e }, /* frame 2: ret -> write 3 */
+ { 0xf984, 0x00005f50 }, /* frame 3: r3 for write 4 */
+ { 0xf998, 0x0000257e }, /* frame 3: ret -> write 4 */
+ { 0xf99c, 0x00005f50 }, /* frame 4: r3 for write 5 */
+ { 0xf9b0, 0x0000257e }, /* frame 4: ret -> write 5 */
+ { 0xf9b4, 0x00005f50 }, /* frame 5: r3 for write 6 */
+ { 0xf9c8, 0x0000257e }, /* frame 5: ret -> write 6 */
+ { 0xf9cc, 0x00005f50 }, /* frame 6: r3 (unused by cleanup) */
+ { 0xf9d8, 0x67b00001 }, /* frame 6: r0 = completion marker */
+ { 0xf9e0, 0x00007a02 }, /* frame 6: ret -> marker gadget */
+ { 0xf9ec, 0x000031b5 }, /* marker gadget: publish I[0x1000] */
+ { 0xf9f0, 0x0000771e }, /* HS clean exit */
+};
+static const struct gu_payload ad102_v67 = {
+ .name = "ad102-v67",
+ .size = 0xfa00,
+ .fill = 0x0000019c,
+ .slots = ad102_v67_slots,
+ .writes = sizeof(ad102_v67_slots) / sizeof(ad102_v67_slots[0]),
+ .words = ad102_v67_words,
+ .word_count = sizeof(ad102_v67_words) / sizeof(ad102_v67_words[0]),
+ .completion = GU_PAYLOAD_HALT_RESET_PLM_MARKER,
+ .marker = 0x67b00001,
+};
+
+/* TU102 port of the same six-write chain from Petri Krohn. The two signed Booters
+ * are that same code with functions moved, and the fwmap resolves the gadgets exactly
+ * (score 1.0): the write entry 0x1110 -> 0x10b9, the status read
+ * 0x576b -> 0x6521, the cleanup gadget 0x84c8 -> 0x7f27 and the final pass
+ * 0xd52 -> 0xccb. The 0x18-byte frames and the 0x6140 canary are identical,
+ * so the six frames sit at the same relative offsets from 0xf940.
+ *
+ * The frame 6 handoff stays at 0x7ea4, the Turing normal main handoff, rather
+ * than the fwmap twin of GA102's 0x81ee (0x7cfc): the latter halts inside the
+ * exit path and leaves the Booter's boot-stage marker (0x31) in MAILBOX0,
+ * while 0x7ea4 runs the subsystem completion that publishes the clean zero. */
+static const struct gu_payload_slot tu102_slots[] = {
+ { 0xf940, 0xf944 },
+ { 0xf958, 0xf95c },
+ { 0xf970, 0xf974 },
+ { 0xf988, 0xf98c },
+ { 0xf9a0, 0xf9a4 },
+ { 0xf9b8, 0xf9bc },
+};
+static const struct gu_payload_word tu102_words[] = {
+ { 0x1100, 0x00000007 },
+ { 0xf938, 0x00006140 }, /* 0x10b9 pop: r3 canary pointer, write 1 */
+ { 0xf94c, 0x000010b9 }, /* write 1: r0=address, r1=value */
+ { 0xf950, 0x00006140 }, /* frame 1: r3 for write 2 */
+ { 0xf964, 0x000010b9 }, /* frame 1: ret -> write 2 */
+ { 0xf968, 0x00006140 }, /* frame 2: r3 for write 3 */
+ { 0xf97c, 0x000010b9 }, /* frame 2: ret -> write 3 */
+ { 0xf980, 0x00006140 }, /* frame 3: r3 for write 4 */
+ { 0xf994, 0x000010b9 }, /* frame 3: ret -> write 4 */
+ { 0xf998, 0x00006140 }, /* frame 4: r3 for write 5 */
+ { 0xf9ac, 0x000010b9 }, /* frame 4: ret -> write 5 */
+ { 0xf9b0, 0x00006140 }, /* frame 5: r3 for write 6 */
+ { 0xf9c4, 0x000010b9 }, /* frame 5: ret -> write 6 */
+ { 0xf9c8, 0x00006140 }, /* frame 6: r3 (unused by cleanup) */
+ { 0xf9d4, 0x00000003 }, /* frame 6: r0 = 3 for 0xccb */
+ { 0xf9dc, 0x00006521 }, /* frame 6: ret -> SE/CSB handshake */
+ { 0xf9e0, 0x00007f27 }, /* cleanup gadget */
+ { 0xf9ec, 0x00000ccb }, /* cleanup: final pass */
+ { 0xf9f0, 0x00007ea4 }, /* HS clean exit */
+};
+static const struct gu_payload tu102_v67 = {
+ .name = "tu102-v67",
+ .size = 0xfa00,
+ .fill = 0x000004a7,
+ .slots = tu102_slots,
+ .writes = sizeof(tu102_slots) / sizeof(tu102_slots[0]),
+ .words = tu102_words,
+ .word_count = sizeof(tu102_words) / sizeof(tu102_words[0]),
+ .completion = GU_PAYLOAD_HALT_RESET_PLM_ZERO,
+};
+
+/* Turing's FEAT aperture is at 0x409650. Opening WPR_CFG_PLM first lets the
+ * driver's own WPR2 restore through the PLM gate; the second write opens the
+ * FEAT aperture the SS0/SS1 selectors live behind. No WPR2 poke is needed:
+ * restore_frts() writes it with the gate open. */
+static const struct gu_reg_write tu102_plm[] = {
+ { GU_WPR_CFG_PLM_OFFSET, 0xfffff0ff },
+ { GU_TU102_FEAT_PLM_OFFSET, 0xffffffff },
+};
+static const struct gu_reg_write tu102_selectors[] = {
+ { GU_TU102_FEAT_SS0_OFFSET, GU_SS0_UNLOCK },
+ { GU_TU102_FEAT_SS1_OFFSET, GU_SS1_UNLOCK },
+};
+
+/* Same per-entry image as GA10x. These are the stores, in order. The FBPA
+ * store is attempted by the signed chain but hardware leaves the field at its
+ * pre-existing value, so it is optional for the readback gate. */
static const struct gu_reg_write ad102_plm[] = {
{ GU_WPR_CFG_PLM_OFFSET, 0xfffff0ff },
+ { GU_FBPA_PLM_OFFSET, 0xffffffff, 0xffffff0f, true },
{ GU_WPR_PLM_OFFSET, 0xffffffff },
{ GU_FEAT_PLM_OFFSET, 0xffffffff },
};
@@ -92,6 +239,7 @@ static const struct gu_booter sec2_ga100 = {
.os_data_size = 25600,
.app_code_size = 34304,
.patch_loc = 35072,
+ .ucode_id = 3,
},
};
/* The GA10x SEC2 Booter. Its signature patch location is 0x8a10. */
@@ -107,6 +255,7 @@ static const struct gu_booter sec2_ga102 = {
.os_data_size = 25088,
.app_code_size = 35072,
.patch_loc = 35344,
+ .ucode_id = 3,
},
};
static const struct gu_booter sec2_ad102 = {
@@ -121,6 +270,22 @@ static const struct gu_booter sec2_ad102 = {
.os_data_size = 0x6000,
.app_code_size = 0x7d00,
.patch_loc = 0x7e10,
+ .ucode_id = 3,
+ },
+};
+static const struct gu_booter sec2_tu102 = {
+ .file = GU_FW_BOOTER_TU102,
+ .launch = GU_SEC2_DIRECT,
+ .payload = &tu102_v67,
+ .expect = {
+ .image_size = 59136,
+ .num_sigs = 1,
+ .sig_bytes = 16,
+ .os_data_off = 34048,
+ .os_data_size = 25088,
+ .app_code_size = 33792,
+ .patch_loc = 34560,
+ .ucode_id = 13,
},
};
@@ -145,6 +310,13 @@ static const struct gu_fw_desc ad102_fw = {
.signature_section = ".fwsignature_ad10x",
.boot_size = 36864,
};
+static const struct gu_fw_desc tu102_fw = {
+ .gsp = GU_FW_GSP_GA100,
+ .boot = GU_FW_BOOT_TU102,
+ .desc = GU_FW_DESC_TU102,
+ .signature_section = ".fwsignature_tu10x",
+ .boot_size = 4096,
+};
static const struct gu_family ga100 = {
.name = "GA100",
@@ -154,6 +326,9 @@ static const struct gu_family ga100 = {
.booter = &sec2_ga100,
.loader = GU_FW_LOADER,
.fwsec_version = 2,
+ .feat_plm = GU_FEAT_PLM_OFFSET,
+ .feat_ss0 = GU_FEAT_SS0_OFFSET,
+ .feat_ss1 = GU_FEAT_SS1_OFFSET,
};
static const struct gu_family ga10x = {
.name = "GA102/GA103/GA104/GA106",
@@ -163,18 +338,45 @@ static const struct gu_family ga10x = {
.booter = &sec2_ga102,
.loader = NULL,
.fwsec_version = 3,
+ .feat_plm = GU_FEAT_PLM_OFFSET,
+ .feat_ss0 = GU_FEAT_SS0_OFFSET,
+ .feat_ss1 = GU_FEAT_SS1_OFFSET,
};
-static const struct gu_family ad102 = {
- .name = "AD102",
+static const struct gu_family ad10x = {
+ .name = "AD102/AD103/AD104/AD106/AD107",
.privileged = GU_WRITE_TABLE(ad102_plm),
.host = GU_WRITE_TABLE(selectors),
.fw = &ad102_fw,
.booter = &sec2_ad102,
.loader = NULL,
.fwsec_version = 3,
+ .feat_plm = GU_FEAT_PLM_OFFSET,
+ .feat_ss0 = GU_FEAT_SS0_OFFSET,
+ .feat_ss1 = GU_FEAT_SS1_OFFSET,
+};
+static const struct gu_family tu102 = {
+ .name = "TU102/TU104/TU106",
+ .privileged = GU_WRITE_TABLE(tu102_plm),
+ .host = GU_WRITE_TABLE(tu102_selectors),
+ .fw = &tu102_fw,
+ .booter = &sec2_tu102,
+ .loader = GU_FW_SEC2_BL_TU102,
+ .fwsec_version = 2,
+ .feat_plm = GU_TU102_FEAT_PLM_OFFSET,
+ .feat_ss0 = GU_TU102_FEAT_SS0_OFFSET,
+ .feat_ss1 = GU_TU102_FEAT_SS1_OFFSET,
+ .turing_layout = true,
};
-#define AD102(id) { id, 0x192, &ad102 }
+#define AD102(id) { id, 0x192, &ad10x }
+#define AD103(id) { id, 0x193, &ad10x }
+#define AD104(id) { id, 0x194, &ad10x }
+#define AD106(id) { id, 0x196, &ad10x }
+#define AD107(id) { id, 0x197, &ad10x }
+
+#define TU102(id) { id, 0x162, &tu102 }
+#define TU104(id) { id, 0x164, &tu102 }
+#define TU106(id) { id, 0x166, &tu102 }
#define GA100(id) { id, 0x170, &ga100 }
#define GA102(id) { id, 0x172, &ga10x }
@@ -183,6 +385,35 @@ static const struct gu_family ad102 = {
#define GA106(id) { id, 0x176, &ga10x }
static const struct gu_card cards[] = {
+ /* TU102: RTX 2080 Ti, Titan RTX */
+ TU102(0x1e03), TU102(0x1e04), TU102(0x1e07), TU102(0x1e2d),
+ TU102(0x1e2e),
+
+ /* TU104: RTX 2080 SUPER, RTX 2070 SUPER, RTX 2080, RTX 2070 */
+ TU104(0x1e81), TU104(0x1e82), TU104(0x1e84), TU104(0x1e87),
+ TU104(0x1e89), TU104(0x1ec2), TU104(0x1ec7),
+
+ /* TU106: RTX 2070, RTX 2060 SUPER, RTX 2060 */
+ TU106(0x1f02), TU106(0x1f03), TU106(0x1f06), TU106(0x1f07),
+ TU106(0x1f08), TU106(0x1f42), TU106(0x1f47),
+
+ /* AD102: RTX 4090, RTX 4090 D, RTX 4070 Ti SUPER */
+ AD102(0x2684), AD102(0x2685), AD102(0x2689),
+
+ /* AD103: RTX 4080 SUPER, RTX 4080, RTX 4070 Ti SUPER, RTX 4070,
+ * RTX 4090 Laptop */
+ AD103(0x2702), AD103(0x2704), AD103(0x2705), AD103(0x2709),
+ AD103(0x2757),
+
+ /* AD104: RTX 4070 Ti, RTX 4070 SUPER, RTX 4070 */
+ AD104(0x2782), AD104(0x2783), AD104(0x2786),
+
+ /* AD106: RTX 4060 Ti, RTX 4060 Ti 16GB */
+ AD106(0x2803), AD106(0x2805),
+
+ /* AD107: RTX 4060, RTX 4060 Laptop, RTX 4060 Mobile, RTX 4050 Mobile */
+ AD107(0x2882), AD107(0x28a0), AD107(0x28e0), AD107(0x28e1),
+
GA102(0x2200), GA102(0x2203), GA102(0x2204), GA102(0x2205),
GA102(0x2206), GA102(0x2207), GA102(0x2208), GA102(0x220a),
GA102(0x220d), GA102(0x2216), GA102(0x222b), GA102(0x222f),
diff --git a/kernel/gpu_unlock/recipes.h b/kernel/gpu_unlock/recipes.h
index 21db7a3..99e0829 100644
--- a/kernel/gpu_unlock/recipes.h
+++ b/kernel/gpu_unlock/recipes.h
@@ -15,8 +15,11 @@
#define GU_FW_BOOTER_AD102 "gpu-unlock/610.43.03/booter_load_ad102.bin"
#define GU_FW_BOOT_AD102 "gpu-unlock/610.43.03/gsp_boot_ad102.bin"
#define GU_FW_DESC_AD102 "gpu-unlock/610.43.03/gsp_boot_desc_ad102.bin"
+#define GU_FW_BOOTER_TU102 "gpu-unlock/610.43.03/booter_load_tu102.bin"
+#define GU_FW_BOOT_TU102 "gpu-unlock/610.43.03/gsp_boot_tu102.bin"
+#define GU_FW_DESC_TU102 "gpu-unlock/610.43.03/gsp_boot_desc_tu102.bin"
+#define GU_FW_SEC2_BL_TU102 "gpu-unlock/610.43.03/sec2_bl_tu102.bin"
-struct gu_reg_write { u32 addr, value; };
struct gu_write_table { const struct gu_reg_write *entries; size_t count; };
/* Header fields that differ between signed Booter images. The common checks
@@ -24,6 +27,7 @@ struct gu_write_table { const struct gu_reg_write *entries; size_t count; };
struct gu_booter_expect {
u32 image_size, num_sigs, sig_bytes;
u32 os_data_off, os_data_size, app_code_size, patch_loc;
+ u32 ucode_id;
};
enum gu_sec2_launch {
@@ -56,6 +60,8 @@ struct gu_family {
const struct gu_booter *booter;
const char *loader; /* NULL when this family has no generic loader */
u8 fwsec_version;
+ u32 feat_plm, feat_ss0, feat_ss1;
+ bool turing_layout; /* VBIOS FB-range register and no MMU lock */
};
struct gu_card {
u16 device;
diff --git a/kernel/gpu_unlock/registers.h b/kernel/gpu_unlock/registers.h
index 48cdc5f..97b1ba8 100644
--- a/kernel/gpu_unlock/registers.h
+++ b/kernel/gpu_unlock/registers.h
@@ -86,6 +86,13 @@
#define GU_SPEED_READOUT_MASK 0x07ffffffu
#define GU_FEAT_SS0_OFFSET 0x0082381cu
#define GU_FEAT_SS1_OFFSET 0x00823820u
+
+/* Turing keeps the same FEAT fields at the TU102 register aperture. */
+#define GU_TU102_FEAT_PLM_OFFSET 0x00409650u
+#define GU_TU102_FEAT_READOUT_OFFSET 0x00409668u
+#define GU_TU102_FEAT_SS0_OFFSET 0x00409664u
+#define GU_TU102_FEAT_SS1_OFFSET 0x0040966cu
+#define GU_TU102_FB_RANGE_OFFSET 0x00100ce0u
/* SS0 has eight 4-bit fields; SS1 has one. Value 8 selects full rate. */
#define GU_SS0_UNLOCK 0x88888888u
#define GU_SS1_UNLOCK 0x8u
diff --git a/kernel/gpu_unlock/unlock_610.c b/kernel/gpu_unlock/unlock_610.c
index 153c463..5979e5f 100644
--- a/kernel/gpu_unlock/unlock_610.c
+++ b/kernel/gpu_unlock/unlock_610.c
@@ -255,12 +255,14 @@ static void gu_snapshot(struct unlock_transaction_work *work, const char *functi
if (work->payload)
payload = work->payload->name;
gu_read_boot_state(work, &b);
- read_bar(work, GU_FEAT_PLM_OFFSET, &feat);
+ if (work->card && work->card->family) {
+ read_bar(work, work->card->family->feat_plm, &feat);
+ read_bar(work, work->card->family->feat_ss0, &ss0);
+ read_bar(work, work->card->family->feat_ss1, &ss1);
+ }
read_bar(work, GU_WPR_CFG_PLM_OFFSET, &wpr_cfg);
read_bar(work, GU_WPR_PLM_OFFSET, &wpr);
read_bar(work, GU_MMU_LOCK_CFG_PLM_OFFSET, &lock);
- read_bar(work, GU_FEAT_SS0_OFFSET, &ss0);
- read_bar(work, GU_FEAT_SS1_OFFSET, &ss1);
diag_pci_word(work, false, PCI_COMMAND, &command);
diag_pci_word(work, true, PCI_EXP_DEVSTA, &devsta);
if (work->streaming_ptr)
@@ -437,56 +439,103 @@ static int unlock_transaction_layout(struct unlock_transaction_work *work)
u32 mb, vga, lo, hi, plm;
int ret;
- if (read_bar(work, GU_GFW_FB_SIZE_OFFSET, &mb) || !mb || mb == ~0u ||
- read_bar(work, GU_VGA_WORKSPACE_BASE_OFFSET, &vga) || vga == ~0u ||
- read_bar(work, GU_MMU_LOCK_CFG_PLM_OFFSET, &plm) || plm == ~0u || !(plm & 1) ||
- read_bar(work, GU_MMU_LOCK_ADDR_LO_OFFSET, &lo) || lo == ~0u ||
- read_bar(work, GU_MMU_LOCK_ADDR_HI_OFFSET, &hi) || hi == ~0u)
- return GU_RESULT(-ENODEV);
- /* Keep the cold board inputs in the USB kernel log even on layout failure.
- * Register values after NVIDIA binds cannot reconstruct this state. */
- dev_dbg(&work->pdev->dev,
- "layout inputs: fb_mb=%#x vga=%#x mmu_plm=%#x mmu_lo=%#x mmu_hi=%#x image=%llu boot=%u\n",
- mb, vga, plm, lo, hi, (unsigned long long)work->bundle->image_span.size,
- work->bundle->boot_desc.image_size);
- in->usable_fb = (u64)mb << 20;
- if (in->usable_fb < 256ull << 20) {
- dev_err(&work->pdev->dev, "layout rejected: usable FB below 256 MiB\n");
- return GU_RESULT(-ERANGE);
- }
- /* kgspPopulateWprMeta_TU102: PRAMIN is the top 1 MiB fallback. */
- in->vga_base = in->usable_fb - (1ull << 20);
- if (vga & (1u << 3)) {
- in->vga_base = (u64)(vga >> 8) << 16;
- if (in->vga_base < in->usable_fb - (1ull << 20))
- in->vga_base = in->usable_fb - 0x20000;
- }
- if (in->vga_base >= in->usable_fb) {
- dev_err(&work->pdev->dev, "layout rejected: VGA base at or above usable FB\n");
- return GU_RESULT(-ERANGE);
- }
- in->mmu_lock_low = (u64)(lo >> 4) << 12;
- {
- /* VAL is bits 31:4; the address is that field shifted by 12.
- * memmgrBlockMemLockedMemory_GA100 only treats the lock as the
- * VBIOS end-of-FB reservation when its exclusive high address is
- * the FB top. A lock elsewhere is not that reservation. GA102
- * has no scrubber, so WPR must stay in the pre-scrubbed top. */
- u64 mmu_hi = (u64)(hi >> 4) << 12;
- u64 mmu_top = mmu_hi > ~0ull - 0xffffull ? ~0ull :
- (mmu_hi + 0xffffull) & ~0xffffull;
- bool top_lock;
-
- in->mmu_lock_valid = mmu_hi >= in->mmu_lock_low;
- top_lock = in->mmu_lock_valid && (mmu_top >> 20) == (in->usable_fb >> 20);
+ if (work->card->family->turing_layout) {
+ ktime_t deadline = ktime_add_us(ktime_get(), 3000000);
+ u32 range = 0, vga_reg = 0, mag, scale;
+ u64 fb_bytes;
+
+ for (;;) {
+ if (read_bar(work, GU_TU102_FB_RANGE_OFFSET, &range) ||
+ read_bar(work, GU_VGA_WORKSPACE_BASE_OFFSET, &vga_reg))
+ return GU_RESULT(-ENODEV);
+ if (range != ~0u && (range & 0xffff0000u) != 0xbadf0000u &&
+ ((range >> 4) & 0x3fu))
+ break;
+ if (ktime_compare(ktime_get(), deadline) >= 0) {
+ dev_err(&work->pdev->dev,
+ "layout rejected: Turing FB range not ready (%#x)\n", range);
+ return GU_RESULT(-ETIMEDOUT);
+ }
+ usleep_range(20000, 21000);
+ }
dev_dbg(&work->pdev->dev,
- "layout mmu: lo=%#llx hi=%#llx hi_exclusive=%#llx constrain=%u\n",
- (unsigned long long)in->mmu_lock_low, (unsigned long long)mmu_hi,
- (unsigned long long)mmu_top, top_lock);
- if (!top_lock) {
+ "layout inputs: tu102 range=%#x vga=%#x image=%llu boot=%u\n",
+ range, vga_reg,
+ (unsigned long long)work->bundle->image_span.size,
+ work->bundle->boot_desc.image_size);
+ mag = (range >> 4) & 0x3fu;
+ scale = range & 0xfu;
+ fb_bytes = (u64)mag << (scale + 20);
+ if (range & (1u << 30))
+ fb_bytes = fb_bytes / 16 * 15;
+ in->usable_fb = fb_bytes;
+ if (in->usable_fb < 256ull << 20) {
+ dev_err(&work->pdev->dev, "layout rejected: usable FB below 256 MiB\n");
+ return GU_RESULT(-ERANGE);
+ }
+ in->vga_base = in->usable_fb - (1ull << 20);
+ if (vga_reg & (1u << 3)) {
+ in->vga_base = (u64)(vga_reg >> 8) << 16;
+ if (in->vga_base < in->usable_fb - (1ull << 20))
+ in->vga_base = in->usable_fb - 0x20000;
+ }
+ if (in->vga_base >= in->usable_fb) {
+ dev_err(&work->pdev->dev, "layout rejected: VGA base at or above usable FB\n");
+ return GU_RESULT(-ERANGE);
+ }
+ in->mmu_lock_valid = false;
+ } else {
+ if (read_bar(work, GU_GFW_FB_SIZE_OFFSET, &mb) || !mb || mb == ~0u ||
+ read_bar(work, GU_VGA_WORKSPACE_BASE_OFFSET, &vga) || vga == ~0u ||
+ read_bar(work, GU_MMU_LOCK_CFG_PLM_OFFSET, &plm) || plm == ~0u || !(plm & 1) ||
+ read_bar(work, GU_MMU_LOCK_ADDR_LO_OFFSET, &lo) || lo == ~0u ||
+ read_bar(work, GU_MMU_LOCK_ADDR_HI_OFFSET, &hi) || hi == ~0u)
+ return GU_RESULT(-ENODEV);
+ /* Keep the cold board inputs in the USB kernel log even on layout failure.
+ * Register values after NVIDIA binds cannot reconstruct this state. */
+ dev_dbg(&work->pdev->dev,
+ "layout inputs: fb_mb=%#x vga=%#x mmu_plm=%#x mmu_lo=%#x mmu_hi=%#x image=%llu boot=%u\n",
+ mb, vga, plm, lo, hi, (unsigned long long)work->bundle->image_span.size,
+ work->bundle->boot_desc.image_size);
+ in->usable_fb = (u64)mb << 20;
+ if (in->usable_fb < 256ull << 20) {
+ dev_err(&work->pdev->dev, "layout rejected: usable FB below 256 MiB\n");
+ return GU_RESULT(-ERANGE);
+ }
+ /* kgspPopulateWprMeta_TU102: PRAMIN is the top 1 MiB fallback. */
+ in->vga_base = in->usable_fb - (1ull << 20);
+ if (vga & (1u << 3)) {
+ in->vga_base = (u64)(vga >> 8) << 16;
+ if (in->vga_base < in->usable_fb - (1ull << 20))
+ in->vga_base = in->usable_fb - 0x20000;
+ }
+ if (in->vga_base >= in->usable_fb) {
+ dev_err(&work->pdev->dev, "layout rejected: VGA base at or above usable FB\n");
+ return GU_RESULT(-ERANGE);
+ }
+ in->mmu_lock_low = (u64)(lo >> 4) << 12;
+ {
+ /* VAL is bits 31:4; the address is that field shifted by 12.
+ * memmgrBlockMemLockedMemory_GA100 only treats the lock as the
+ * VBIOS end-of-FB reservation when its exclusive high address is
+ * the FB top. A lock elsewhere is not that reservation. GA102
+ * has no scrubber, so WPR must stay in the pre-scrubbed top. */
+ u64 mmu_hi = (u64)(hi >> 4) << 12;
+ u64 mmu_top = mmu_hi > ~0ull - 0xffffull ? ~0ull :
+ (mmu_hi + 0xffffull) & ~0xffffull;
+ bool top_lock;
+
+ in->mmu_lock_valid = mmu_hi >= in->mmu_lock_low;
+ top_lock = in->mmu_lock_valid && (mmu_top >> 20) == (in->usable_fb >> 20);
dev_dbg(&work->pdev->dev,
- "mmu lock ignored: high address is not the FB top\n");
- in->mmu_lock_valid = false;
+ "layout mmu: lo=%#llx hi=%#llx hi_exclusive=%#llx constrain=%u\n",
+ (unsigned long long)in->mmu_lock_low, (unsigned long long)mmu_hi,
+ (unsigned long long)mmu_top, top_lock);
+ if (!top_lock) {
+ dev_dbg(&work->pdev->dev,
+ "mmu lock ignored: high address is not the FB top\n");
+ in->mmu_lock_valid = false;
+ }
}
}
in->frts_size = 1ull << 20;
@@ -515,8 +564,10 @@ static int unlock_transaction_layout(struct unlock_transaction_work *work)
(unsigned long long)work->layout.frts_offset,
(unsigned long long)work->layout.wpr_end,
(unsigned long long)(in->usable_fb - (256ull << 20)));
- /* GA102 has no scrubber: all reservations must fit the pre-scrubbed top. */
- if (work->layout.nonwpr_start < in->usable_fb - (256ull << 20)) {
+ /* GA102 has no scrubber: all reservations must fit the pre-scrubbed top.
+ * Turing uses the VBIOS FB range without that scrubber constraint. */
+ if (!work->card->family->turing_layout &&
+ work->layout.nonwpr_start < in->usable_fb - (256ull << 20)) {
dev_err(&work->pdev->dev, "layout rejected: reservations below pre-scrubbed top 256 MiB\n");
return GU_RESULT(-ERANGE);
}
@@ -694,7 +745,7 @@ static int unlock_transaction_dma(struct unlock_transaction_work *work)
/* No launch precedes the first table entry, so the slots stay unused
* until run_privileged_writes() builds the image for its write. */
{
- int ret = gu_payload_build(work->payload, work->buffer.cpu, 0, 0);
+ int ret = gu_payload_build(work->payload, work->buffer.cpu, NULL, 0);
if (ret)
return GU_RESULT(ret);
@@ -736,7 +787,7 @@ static int unlock_transaction_fwsec(struct unlock_transaction_work *work)
u64 frts = work->layout.frts_offset;
int ret;
- /* kgspExtractVbiosFromRom_TU102 reads NV_PROM_DATA, up to 1 MiB.
+ /* kgspExtractVbiosFromRom_TU102 reads up to 1 MiB.
* pci_map_rom stops at the PCI last-image flag and hides the VN extension. */
dev_dbg(&work->pdev->dev, "FWSEC: reading PROM at %#x\n", GU_PROM_DATA_OFFSET);
if (work->bar_len < GU_PROM_DATA_OFFSET + 0x1a)
@@ -894,9 +945,9 @@ static int unlock_transaction_gfw(struct unlock_transaction_work *work)
if (!assessed) {
u32 ss0, ss1, wpr_hi, plm;
- if (read_bar(work, GU_FEAT_PLM_OFFSET, &plm) ||
- read_bar(work, GU_FEAT_SS0_OFFSET, &ss0) || ss0 == ~0u ||
- read_bar(work, GU_FEAT_SS1_OFFSET, &ss1) || ss1 == ~0u ||
+ if (read_bar(work, work->card->family->feat_plm, &plm) ||
+ read_bar(work, work->card->family->feat_ss0, &ss0) || ss0 == ~0u ||
+ read_bar(work, work->card->family->feat_ss1, &ss1) || ss1 == ~0u ||
read_bar(work, GU_WPR2_ADDR_HI_OFFSET, &wpr_hi) || inaccessible(wpr_hi))
return GU_RESULT(-ENODEV);
dev_dbg(&work->pdev->dev, "entry: plm=%#x ss0=%#x ss1=%#x wpr2_hi=%#x\n",
@@ -946,8 +997,8 @@ static int unlock_transaction_suspended(struct unlock_transaction_work *work)
return GU_RESULT(-ENOMEM);
/* Read before GFW completes, so an unreadable value proves nothing yet. */
if (read_bar(work, GU_WPR2_ADDR_HI_OFFSET, &wpr_hi) ||
- read_bar(work, GU_FEAT_SS0_OFFSET, &ss0) ||
- read_bar(work, GU_FEAT_SS1_OFFSET, &ss1))
+ read_bar(work, work->card->family->feat_ss0, &ss0) ||
+ read_bar(work, work->card->family->feat_ss1, &ss1))
return GU_RESULT(-EIO);
dev_dbg(&work->pdev->dev, "resume entry: wpr2_hi=%#x ss0=%#x ss1=%#x\n",
wpr_hi, ss0, ss1);
@@ -1019,7 +1070,7 @@ static int unlock_transaction_boot(struct unlock_transaction_work *work)
return GU_RESULT(-ENODEV);
read_bar(work, GU_WPR2_ADDR_LO_OFFSET, &wpr_lo);
read_bar(work, GU_WPR2_ADDR_HI_OFFSET, &wpr_hi);
- read_bar(work, GU_FEAT_PLM_OFFSET, &feat);
+ read_bar(work, work->card->family->feat_plm, &feat);
read_bar(work, GU_WPR_CFG_PLM_OFFSET, &wpr_cfg);
read_bar(work, GU_SEC2_BASE + GU_SEC2_CSBERRSTAT_OFFSET, &csberr);
read_bar(work, GU_SEC2_BASE + GU_FALCON_FHSTATE_OFFSET, &fhstate);
@@ -1039,6 +1090,12 @@ static int unlock_transaction_boot(struct unlock_transaction_work *work)
work->payload->completion == GU_PAYLOAD_HALT_RESET_PLM_ECHO)
cleanup_complete = mailbox0 == lower_32_bits(work->meta.dma) &&
mailbox1 == upper_32_bits(work->meta.dma);
+ if (cleanup_complete &&
+ work->payload->completion == GU_PAYLOAD_HALT_RESET_PLM_MARKER)
+ cleanup_complete = mailbox0 == work->payload->marker;
+ if (cleanup_complete &&
+ work->payload->completion == GU_PAYLOAD_HALT_RESET_PLM_ZERO)
+ cleanup_complete = mailbox0 == 0;
dev_dbg(&work->pdev->dev,
"invocation: index=%zu attempt=%u reset=%u/%u reload=%u start=%u cpuctl=%#x engine=%#x reset_plm@%#x=%#x mailbox=%#x:%#x acr=%#x csberr=%#x fhstate=%#x wpr2=%#x:%#x plm_feat=%#x plm_wpr_cfg=%#x fresh=%u cleanup_observed=%u result=%d\n",
work->write_index, work->attempt, launch.reset_asserted,
@@ -1130,7 +1187,7 @@ static int restore_frts(struct unlock_transaction_work *work)
{
u32 feat = 0, wpr_cfg = 0, wpr = 0, lock = 0;
- read_bar(work, GU_FEAT_PLM_OFFSET, &feat);
+ read_bar(work, work->card->family->feat_plm, &feat);
read_bar(work, GU_WPR_CFG_PLM_OFFSET, &wpr_cfg);
read_bar(work, GU_WPR_PLM_OFFSET, &wpr);
read_bar(work, GU_MMU_LOCK_CFG_PLM_OFFSET, &lock);
@@ -1152,18 +1209,36 @@ static int table_failure(struct unlock_transaction_work *work, const char *phase
static int run_privileged_writes(struct unlock_transaction_work *work,
const struct gu_write_table *table)
{
- size_t i;
+ const size_t per_launch = work->payload->writes;
+ struct gu_reg_write batch[GU_PAYLOAD_MAX_WRITES];
+ size_t i, j, count;
u32 after = 0;
+ bool matched;
int ret;
+
if (work->state != GU_PUBLISHED && work->state != GU_ACTIVE)
return GU_RESULT(-EPROTO);
+ if (!table->count)
+ return 0;
work->state = GU_ACTIVE;
- for (i = 0; i < table->count; i++) {
- const struct gu_reg_write *entry = &table->entries[i];
+ for (i = 0; i < table->count; i += per_launch) {
+ const struct gu_reg_write *entry;
+
+ count = table->count - i;
+ if (count > per_launch)
+ count = per_launch;
+ /* Repeat the last write of this launch to fill the payload's fixed
+ * slot count. The recipe's final write must therefore be the value
+ * the chain's unused slots already produce. */
+ for (j = 0; j < per_launch; j++)
+ batch[j] = table->entries[i + (j < count ? j : count - 1)];
+ entry = &batch[0];
+ matched = false;
work->write_index = i;
for (work->attempt = 1; work->attempt <= 2; work->attempt++) {
u32 before, lo = 0, hi = 0, ctl = 0, engine = 0;
struct gu_boot_state st;
+
work->error_reported = false;
work->target_addr = entry->addr;
work->target_value = entry->value;
@@ -1192,54 +1267,78 @@ static int run_privileged_writes(struct unlock_transaction_work *work,
read_bar(work, GU_WPR2_ADDR_HI_OFFSET, &hi);
gu_read_boot_state(work, &st);
dev_dbg(&work->pdev->dev,
- "prelaunch: index=%zu/%zu attempt=%u target=%#x value=%#x before=%#x cpuctl=%#x engine=%#x reset_plm=%#x wpr2=%#x:%#x fhstate=%#x\n",
- i, table->count, work->attempt, entry->addr, entry->value,
+ "prelaunch: index=%zu/%zu count=%zu attempt=%u target=%#x value=%#x before=%#x cpuctl=%#x engine=%#x reset_plm=%#x wpr2=%#x:%#x fhstate=%#x\n",
+ i, table->count, count, work->attempt, entry->addr, entry->value,
before, ctl, engine, st.reset_plm, lo, hi, st.fhstate);
- ret = gu_payload_build(work->payload, work->buffer.cpu,
- entry->addr, entry->value);
+ ret = gu_payload_build(work->payload, work->buffer.cpu, batch,
+ per_launch);
if (ret)
return table_failure(work, "payload", entry, after, ret);
put_le64((u8 *)work->meta.cpu + 72, work->buffer.dma);
put_le64((u8 *)work->meta.cpu + 80, work->payload->size);
dma_wmb();
dev_dbg(&work->pdev->dev,
- "payload: name=%s size=%#x dma=%#llx index=%zu/%zu attempt=%u address=%#x value=%#x mode=%s\n",
+ "payload: name=%s size=%#x dma=%#llx index=%zu/%zu writes=%zu attempt=%u first=%#x:%#x mode=%s\n",
work->payload->name, work->payload->size,
(unsigned long long)work->buffer.dma, i, table->count,
- work->attempt, entry->addr, entry->value,
+ count, work->attempt, entry->addr, entry->value,
work->attempt > 1 ? "retry" : "first");
ret = unlock_transaction_boot(work);
- if (read_bar(work, entry->addr, &after) ||
- (after & 0xffff0000u) == 0xbadf0000u ||
- read_bar(work, GU_WPR2_ADDR_LO_OFFSET, &lo) || read_bar(work, GU_WPR2_ADDR_HI_OFFSET, &hi))
+ if (ret)
+ return table_failure(work, "completion", entry, after, ret);
+ if (read_bar(work, GU_WPR2_ADDR_LO_OFFSET, &lo) ||
+ read_bar(work, GU_WPR2_ADDR_HI_OFFSET, &hi))
return table_failure(work, "result-inaccessible", entry, after, -ENODEV);
gu_read_boot_state(work, &st);
- dev_dbg(&work->pdev->dev,
- "write result: index=%zu/%zu attempt=%u address=%#x expected=%#x observed=%#x wpr2=%#x:%#x write=%u reset_plm=%#x ctl=%#x engine=%#x fhstate=%#x mbox=%#x:%#x acr=%#x csberr=%#x\n",
- i, table->count, work->attempt, entry->addr, entry->value,
- after, lo, hi, after == entry->value, st.reset_plm,
- st.ctl, st.engine, st.fhstate, st.mailbox1, st.mailbox0,
- st.acr, st.csberr);
- if (ret) {
- dev_err(&work->pdev->dev, "failed invocation WPR2: actual=%#x:%#x saved_FRTS=%#x:%#x\n",
- lo, hi, work->frts_lo, work->frts_hi);
- return table_failure(work, "completion", entry, after, ret);
- }
- if (after == entry->value) {
+ matched = true;
+ for (j = 0; j < count; j++) {
+ const struct gu_reg_write *written = &table->entries[i + j];
+ u32 expected = written->expect ? written->expect : written->value;
+ u32 observed = 0;
+
+ if (read_bar(work, written->addr, &observed) ||
+ (observed & 0xffff0000u) == 0xbadf0000u) {
+ work->target_addr = written->addr;
+ work->target_value = written->value;
+ return table_failure(work, "result-inaccessible",
+ written, observed, -ENODEV);
+ }
+ if (observed != expected) {
+ if (written->optional) {
+ dev_info(&work->pdev->dev,
+ "gpu_unlock: optional write %zu/%zu target=%#x wrote=%#x expected=%#x observed=%#x\n",
+ i + j + 1, table->count, written->addr,
+ written->value, expected, observed);
+ continue;
+ }
+ matched = false;
+ after = observed;
+ work->target_addr = written->addr;
+ work->target_value = written->value;
+ table_failure(work, "readback", written, observed, -EIO);
+ continue;
+ }
dev_info(&work->pdev->dev,
"gpu_unlock: pass=%zu/%zu attempt=%u target=%#x value=%#x before=%#x after=%#x wpr2=%#x:%#x reset_plm=%#x mbox=%#x:%#x ret=0\n",
- i + 1, table->count, work->attempt, entry->addr,
- entry->value, before, after, lo, hi,
+ i + j + 1, table->count, work->attempt, written->addr,
+ written->value, before, observed, lo, hi,
st.reset_plm, st.mailbox1, st.mailbox0);
- break;
}
+ dev_dbg(&work->pdev->dev,
+ "write result: index=%zu/%zu count=%zu attempt=%u first=%#x expected=%#x observed=%#x wpr2=%#x:%#x write=%u reset_plm=%#x ctl=%#x engine=%#x fhstate=%#x mbox=%#x:%#x acr=%#x csberr=%#x\n",
+ i, table->count, count, work->attempt, entry->addr, entry->value,
+ after, lo, hi, matched, st.reset_plm,
+ st.ctl, st.engine, st.fhstate, st.mailbox1, st.mailbox0,
+ st.acr, st.csberr);
+ if (matched)
+ break;
/* Only literal idempotent PLMs belong here. A mismatching
* readback after proven halt/cleanup may be retried once.
* Errors/timeouts never replay an uncertain side effect. */
- table_failure(work, "readback", entry, after, -EIO);
}
- if (after != entry->value)
- return table_failure(work, "readback-exhausted", entry, after, -EIO);
+ if (!matched)
+ return table_failure(work, "readback-exhausted",
+ &table->entries[i + count - 1], after, -EIO);
}
ret = restore_frts(work);
return ret && table->count ?
@@ -1281,9 +1380,9 @@ static int run_host_writes(struct unlock_transaction_work *work,
{
u32 ss0 = 0, ss1 = 0, feat = 0, wpr_cfg = 0;
- read_bar(work, GU_FEAT_SS0_OFFSET, &ss0);
- read_bar(work, GU_FEAT_SS1_OFFSET, &ss1);
- read_bar(work, GU_FEAT_PLM_OFFSET, &feat);
+ read_bar(work, work->card->family->feat_ss0, &ss0);
+ read_bar(work, work->card->family->feat_ss1, &ss1);
+ read_bar(work, work->card->family->feat_plm, &feat);
read_bar(work, GU_WPR_CFG_PLM_OFFSET, &wpr_cfg);
dev_info(&work->pdev->dev,
"gpu_unlock: stage=host ss0=%#x ss1=%#x plm_feat=%#x plm_wpr_cfg=%#x\n",
@@ -1370,8 +1469,8 @@ static int unlock_transaction_reset(struct unlock_transaction_work *work)
if (work->state != GU_STOCK)
return GU_RESULT(-EPROTO);
- if (read_bar(work, GU_FEAT_SS0_OFFSET, &ss0) || ss0 == ~0u ||
- read_bar(work, GU_FEAT_SS1_OFFSET, &ss1) || ss1 == ~0u)
+ if (read_bar(work, work->card->family->feat_ss0, &ss0) || ss0 == ~0u ||
+ read_bar(work, work->card->family->feat_ss1, &ss1) || ss1 == ~0u)
return GU_RESULT(-ENODEV);
dev_dbg(&work->pdev->dev, "pre-FLR selectors: ss0=%#x expected=%#x ss1=%#x expected=%#x\n",
ss0, GU_SS0_UNLOCK, ss1, GU_SS1_UNLOCK);
@@ -1447,9 +1546,9 @@ static int unlock_transaction_reset(struct unlock_transaction_work *work)
!(cpuctl & GU_GFW_CPUCTL_HALTED) ||
read_bar(work, GU_WPR2_ADDR_LO_OFFSET, &lo) || inaccessible(lo) ||
read_bar(work, GU_WPR2_ADDR_HI_OFFSET, &hi) || inaccessible(hi) ||
- read_bar(work, GU_FEAT_SS0_OFFSET, &ss0) || inaccessible(ss0) ||
- read_bar(work, GU_FEAT_SS1_OFFSET, &ss1) || inaccessible(ss1) ||
- read_bar(work, GU_FEAT_PLM_OFFSET, &plm) ||
+ read_bar(work, work->card->family->feat_ss0, &ss0) || inaccessible(ss0) ||
+ read_bar(work, work->card->family->feat_ss1, &ss1) || inaccessible(ss1) ||
+ read_bar(work, work->card->family->feat_plm, &plm) ||
(plm & 0xffff0000u) == 0xbadf0000u)
return GU_RESULT(-ENODEV);
dev_dbg(&work->pdev->dev,
diff --git a/kernel/gpu_unlock/vbios_610.c b/kernel/gpu_unlock/vbios_610.c
index 4e2c1b1..cb04a0b 100644
--- a/kernel/gpu_unlock/vbios_610.c
+++ b/kernel/gpu_unlock/vbios_610.c
@@ -14,8 +14,7 @@
#endif
#include "vbios_610.h"
-/* Production selects 0x85. The integration build selects the debug app
- * 0x45; this is a compile-time seam, never a runtime knob. */
+/* FWSEC application selected for the VBIOS request. */
#ifndef GPU_UNLOCK_FWSEC_APP
#define GPU_UNLOCK_FWSEC_APP 0x85
#endif
@@ -128,46 +127,50 @@ static int descriptor(const u8 *rom, size_t length, u64 off,
GU_NOTE("FWSEC descriptor: offset=%#llx ROM_bytes=%zu header=%#x\n",
(unsigned long long)off, length, hdr);
fw.version = (hdr >> 8) & 0xff;
- /* GA100 uses the v2 generic-loader descriptor, with embedded signatures.
- * Its image is kept contiguous here; the loader receives separate code
- * and data DMA bases within that allocation. */
+ /* v2 generic-loader descriptor. TU102 and GA100 share this layout, but
+ * TU102 uses a non-zero secure IMEM base inside the same image. */
if ((hdr & 1) && fw.version == 2) {
- u32 sec_base, sec_size;
+ u32 sec_base, sec_size, code_size_aligned, data_size_aligned;
+
if (size < 60 || range(length, off, size))
return GU_DIAG(-EINVAL);
+ /* The generic loader starts at zero and loads DMEM at zero. */
+ if (r32(rom + off + 12) || r32(rom + off + 44))
+ return GU_DIAG(-EINVAL);
+ stored = r32(rom + off + 4);
+ if (!stored || stored > ~(u32)0 - 255)
+ return GU_DIAG(-EINVAL);
+ fw.image_size = (stored + 255) & ~255u;
+ if (range(length, off + size, fw.image_size))
+ return GU_DIAG(-EINVAL);
fw.image_off = off + size;
- fw.image_size = r32(rom + off + 4);
fw.interface_off = r32(rom + off + 16);
fw.imem_pa = r32(rom + off + 20);
fw.imem_size = r32(rom + off + 24);
fw.imem_va = r32(rom + off + 28);
sec_base = r32(rom + off + 32);
sec_size = r32(rom + off + 36);
+ if (sec_size > fw.imem_size || sec_size > ~(u32)0 - 255 ||
+ sec_base < fw.imem_va ||
+ sec_base - fw.imem_va > ~(u32)0 - fw.imem_pa)
+ return GU_DIAG(-EINVAL);
+ fw.imem_sec_size = (sec_size + 255) & ~255u;
+ fw.imem_ns_size = fw.imem_size - sec_size;
+ fw.imem_sec_pa = sec_base - fw.imem_va + fw.imem_pa;
fw.data_off = r32(rom + off + 40);
fw.dmem_pa = r32(rom + off + 44);
fw.dmem_size = r32(rom + off + 48);
- /* Restrict to the layout this contiguous adapter implements; refuse
- * alternate images, relocation, compression and nonzero entry. */
- if (!fw.image_size || !fw.imem_size || !fw.dmem_size ||
- r32(rom + off + 8) != fw.image_size || r32(rom + off + 12) ||
- r32(rom + off + 52) || r32(rom + off + 56) ||
- fw.imem_pa || fw.dmem_pa || sec_size > fw.imem_size ||
- sec_base < fw.imem_va || sec_size > ~(u32)0 - 255 ||
- (fw.data_off & 255) || (fw.imem_size & 255) ||
- (fw.dmem_size & 255) || (fw.image_size & 255) ||
- range(length, fw.image_off, fw.image_size) ||
+ code_size_aligned = (fw.imem_size + 255) & ~255u;
+ data_size_aligned = (fw.dmem_size + 255) & ~255u;
+ if (!fw.imem_size || !fw.dmem_size ||
range(fw.image_size, 0, fw.imem_size) ||
- fw.data_off < fw.imem_size ||
+ range(fw.image_size, fw.imem_sec_pa, fw.imem_sec_size) ||
range(fw.image_size, fw.data_off, fw.dmem_size) ||
- range(fw.image_size, fw.data_off, ((u64)fw.dmem_size + 255) & ~255ull) ||
+ range(code_size_aligned, fw.imem_pa, fw.imem_size) ||
+ range(code_size_aligned, fw.imem_sec_pa, fw.imem_sec_size) ||
+ range(data_size_aligned, fw.dmem_pa, fw.dmem_size) ||
range(fw.dmem_size, fw.interface_off, 4))
return GU_DIAG(-EINVAL);
- fw.imem_ns_size = fw.imem_size - sec_size;
- fw.imem_sec_pa = sec_base - fw.imem_va;
- fw.imem_sec_size = (sec_size + 255) & ~255u;
- if ((fw.imem_ns_size & 255) || (fw.imem_sec_pa & 255) ||
- range(fw.imem_size, fw.imem_sec_pa, fw.imem_sec_size))
- return GU_DIAG(-EINVAL);
*out = fw;
return 0;
}
@@ -376,6 +379,8 @@ int gpu_unlock_fwsec_patch_frts(u8 *image, const struct gpu_unlock_fwsec *fw, u6
return GU_DIAG(-EOPNOTSUPP);
cmd_off = r32(mapper + 8);
cmd_size = r32(mapper + 12);
+ GU_NOTE("FWSEC mapper offset=%#zx init_before=%#x cmd_off=%#x cmd_size=%#x\n",
+ (size_t)(mapper - dmem), r32(mapper + 44), cmd_off, cmd_size);
if (cmd_size < sizeof(cmd) || range(fw->dmem_size, cmd_off, cmd_size))
return GU_DIAG(-EINVAL);
w32(cmd, 1);
@@ -388,5 +393,7 @@ int gpu_unlock_fwsec_patch_frts(u8 *image, const struct gpu_unlock_fwsec *fw, u6
w32(cmd + 40, 2);
memcpy(dmem + cmd_off, cmd, sizeof(cmd));
w32(mapper + 44, 0x15);
+ GU_NOTE("FWSEC mapper patched init=%#x cmd0=%#x cmd4=%#x\n",
+ r32(mapper + 44), r32(dmem + cmd_off), r32(dmem + cmd_off + 4));
return 0;
}
diff --git a/packaging/arch/PKGBUILD b/packaging/arch/PKGBUILD
index 9da90b3..04d7585 100644
--- a/packaging/arch/PKGBUILD
+++ b/packaging/arch/PKGBUILD
@@ -7,7 +7,7 @@
# For a published release, point the first source at the tagged tarball.
pkgbase=gpu-unlock
pkgname=(gpu-unlock-dkms gpu-unlock-firmware)
-pkgver=1.1.0
+pkgver=2.0.0
pkgrel=1
_nv=610.43.03
_installer=NVIDIA-Linux-x86_64-$_nv-no-compat32.run
diff --git a/packaging/rpm/gpu-unlock.spec b/packaging/rpm/gpu-unlock.spec
index 13f5d43..9568336 100644
--- a/packaging/rpm/gpu-unlock.spec
+++ b/packaging/rpm/gpu-unlock.spec
@@ -1,7 +1,7 @@
%global nv 610.43.03
Name: gpu-unlock
-Version: 1.1.0
+Version: 2.0.0
Release: 1%{?dist}
Summary: RTX 3090 BF16 unlock before the NVIDIA driver loads
License: GPL-2.0-only
@@ -128,5 +128,8 @@ if [ "$1" -eq 0 ]; then
fi
%changelog
+* Tue Oct 27 2026 Zagitta <[email protected]> - 2.0.0-1
+- Add Turing and Ada Lovelace support.
+
* Mon Sep 28 2026 Zagitta <[email protected]> - 1.1.0-1
- Initial release.
diff --git a/tests/integration/README.md b/tests/integration/README.md
deleted file mode 100644
index a4e28a6..0000000
--- a/tests/integration/README.md
+++ /dev/null
@@ -1,74 +0,0 @@
-# Driver integration harness
-
-Native runner for the `kernel/gpu_unlock` transaction against the GA102/AD102
-device model. The driver, the kernel shim and the device library are compiled
-into one shared object; Python only calls `gd_run_transaction` once per run and
-decodes the trace afterwards.
-
-## Run
-
-```sh
-python3 -m tests.integration.run GA10x --threads
-GPU_UNLOCK_DEBUG_KEY=<32 hex chars> python3 -m tests.integration.test_integration
-make integration-test
-```
-
-`GPU_UNLOCK_DEBUG_KEY` is required for the real path. The test executes the
-real FWSEC and SEC2 Booter with the real V67 payload; the final clean-halt,
-target-write and unlock checks currently fail and are reported as failures.
-
-The runner reads firmware from `$GPU_UNLOCK_ASSETS` (default
-`~/.cache/gpu-unlock/assets`) and prepares the committed VBIOS containers under
-`emulator/vbios/`. `GPU_UNLOCK_DEBUG_KEY` (32 hex chars) supplies the external
-AES-128 debug key; it is never written to the run directory.
-
-The Booter bundle copied from the cache has HS-encrypted application code. For
-keyed execution the harness replaces only that code range with the external
-encrypted debug image (`$GPU_UNLOCK_BOOTERS`, default `~/Projects/booters`:
-`ga100_secure.enc`, `ga102_secure.enc`, `ad102_secure.enc`). Header, DMEM data
-and signature area remain the production bundle; the driver still loads one
-ordinary driver-format bundle and performs the same secure load. The emulator
-decrypts at the secure DMA/PIO boundary, and the Python code-table builder
-performs the same AES-ECB transform. The debug image is not committed.
-
-Outputs per run: `rom.bin`, `assets/`, `manifest.json`, `result.json`,
-`trace.bin` and the decoded `trace.jsonl`.
-
-## Stages
-
-| Stage | State |
-| --- | --- |
-| 1. Host shim + end-to-end driver | Done. The driver parses the prepared ROM, patches FRTS, publishes the V67 signature and runs the real FWSEC/Booter transaction. |
-| 2. Free-running engines | Done. SEC2/GSP pthreads, per-engine locks, a BAR lock, DMA queue draining and the FLR quiesce barrier. The real-path test runs with free-running engines; traces are not compared for equality. |
-| 3. Execute FWSEC | Partial. Debug application selection (`GPU_UNLOCK_FWSEC_APP=0x45`), AES-128-ECB IMEM loading, a table-driven C interpreter, full-VBIOS `NVGI`/IFR scanning, the HS_UCODE_DMEM op4 SCP KDF, and the GSP SHA-256 operation/mutex path are implemented. With the supplied key the device executes the real FWSEC through the VBIOS `NVGI` check and SHA digest comparisons, then reaches and completes the DIO/SE PKA RSA path; the driver now reaches the real Booter. FWSEC and Booter each have their own pre-decoded instruction table, so the Booter no longer runs against FWSEC records. Neither engine reaches a clean halt yet (`fwsec_executed=false`, `booter_executed=false`). The secure-bus window, GSP identity values, AES/SHA registers, SE PKA modexp and the WPR2-visible register path are modeled. |
-| 4. Hooks and tracing | Done for the actual `booter.toml` inventory (storage, the two compiled CSB operations) with binary per-run trace rings and Python decoding. |
-| 5. Behavioral gates | Partial. GA10x real-path execution is asserted; the clean-halt/target/unlock gates fail with the current payload. |
-
-## Verdicts
-
-`result.json` now separates:
-
-- `driver_result` — the driver's return value.
-- `fwsec_executed`, `booter_executed` — real instruction execution only.
-- `plm_opened`, `target_written` — latched before FLR, not post-reset state.
-- `unlock_verified` — true only when the real FWSEC and Booter ran, the PLM
- opened, the target mask was written, both selectors read back, and SEC2 left
- `RESET_PLM=0xffffffff`.
-
-PLM enforcement is active: a host write to a protected target is denied while
-its mask still has the matching write level closed. The real firmware performs
-the privileged writes and then must pass the same host-side gate.
-
-The real keyed run asserts `unlock_verified`. It currently reaches the Booter,
-but the V67 chain faults at `pc=1` before the clean halt and target write, so
-the integration test reports failures rather than a synthesized success.
-
-## Deliberate limits
-
-- Production firmware remains unexecutable. The real path requires the external
- debug FWSEC/Booter assets and a complete Falcon core; neither is in this repository.
-- The current V67 payload does not reach a clean Booter halt; the final gates fail.
-- GA100 stays blocked by the v2 descriptor gap described in
- `emulator/PLAN_GSP_FWSEC.md`.
-- Traces are not compared for equality. Acceptance uses driver return values,
- execution evidence, halt/fault PCs, WPR2 state and unknown-register evidence.
diff --git a/tests/integration/__init__.py b/tests/integration/__init__.py
deleted file mode 100644
index e69de29..0000000
diff --git a/tests/integration/assets.py b/tests/integration/assets.py
deleted file mode 100644
index 6ebcf74..0000000
--- a/tests/integration/assets.py
+++ /dev/null
@@ -1,130 +0,0 @@
-# SPDX-License-Identifier: GPL-2.0-only
-"""Prepare the ROM and asset directory for one family.
-
-The committed VBIOS files are full containers. The device exposes the same
-1 MiB PROM window to the host driver and to the GSP secure bus; the driver
-walks the IFR header to the embedded PCI/legacy image exactly as
-kgspExtractVbiosFromRom_TU102 does. Assets come from the existing gpu-unlock
-cache (or GPU_UNLOCK_ASSETS) and are copied into the run directory; nothing is
-fetched.
-"""
-from __future__ import annotations
-
-import os
-import shutil
-import struct
-from pathlib import Path
-
-ROOT = Path(__file__).resolve().parents[2]
-VBIOS = ROOT / "emulator" / "vbios"
-CACHE = Path(os.environ.get("GPU_UNLOCK_ASSETS", Path.home() / ".cache" / "gpu-unlock" / "assets"))
-BOOTERS = Path(os.environ.get("GPU_UNLOCK_BOOTERS", Path.home() / "Projects" / "booters"))
-
-PROM_SIZE = 0x100000
-PROM_OFFSET = 0x300000
-
-FAMILIES = {
- "GA100": {
- "device": 0x20C2,
- "chip": 0x170,
- "rom": VBIOS / "257744.rom",
- "offset": 0x0,
- "fwsec_version": 2,
- "booter": "booter_load_ga100.bin",
- "debug_booter": "ga100_secure.enc",
- },
- "GA10x": {
- "device": 0x2204,
- "chip": 0x172,
- "rom": VBIOS / "Palit.RTX3090.24576.210310_3.rom",
- "offset": 0x0,
- "fwsec_version": 3,
- "booter": "booter_load_ga102.bin",
- "debug_booter": "ga102_secure.enc",
- },
- "AD102": {
- "device": 0x2684,
- "chip": 0x192,
- "rom": VBIOS / "Palit.RTX4090.24576.221012.rom",
- "offset": 0x0,
- "fwsec_version": 3,
- "booter": "booter_load_ad102.bin",
- "debug_booter": "ad102_secure.enc",
- },
-}
-
-ASSETS = [
- "gsp_ga10x.bin", "gsp_boot_ga102.bin", "gsp_boot_desc_ga102.bin",
- "gsp_tu10x.bin", "gsp_boot_ga100.bin", "gsp_boot_desc_ga100.bin",
- "gsp_boot_ad102.bin", "gsp_boot_desc_ad102.bin",
- "falcon_loader.bin", "booter_load_ga100.bin", "booter_load_ga102.bin",
- "booter_load_ad102.bin",
-]
-
-
-def prepare_rom(family: str, output: Path) -> dict:
- spec = FAMILIES[family]
- data = spec["rom"].read_bytes()
- offset = spec["offset"]
- image = data[offset:offset + PROM_SIZE]
- if len(image) < 2 or not ((image[0] == 0x55 and image[1] == 0xAA) or
- (image[0] == 0x4E and image[1] == 0x56)):
- raise SystemExit(f"{family}: no VBIOS image at {offset:#x} in {spec['rom']}")
- image = image.ljust(PROM_SIZE, b"\xff")
- output.parent.mkdir(parents=True, exist_ok=True)
- output.write_bytes(image)
- return {"family": family, "rom": str(spec["rom"]), "offset": offset,
- "size": PROM_SIZE, "device": spec["device"], "chip": spec["chip"]}
-
-
-def prepare_assets(family: str, destination: Path) -> list[str]:
- destination.mkdir(parents=True, exist_ok=True)
- copied = []
- for name in ASSETS:
- source = CACHE / name
- if not source.exists():
- raise SystemExit(f"missing asset {source}; run firmware/fetch_artifacts.py first")
- shutil.copyfile(source, destination / name)
- copied.append(name)
- _install_debug_booter(family, destination / FAMILIES[family]["booter"])
- return copied
-
-
-def _install_debug_booter(family: str, bundle: Path) -> None:
- """Install the external encrypted debug Booter IMEM.
-
- The driver loads one driver-format bundle and the HS loader decrypts the
- code. The debug image is AES-ECB encrypted with the same debug key; the
- emulator performs that transform at the secure DMA/PIO boundary. Header,
- DMEM data and signature area stay from the production bundle.
- """
- debug = BOOTERS / FAMILIES[family]["debug_booter"]
- if not debug.is_file():
- return
- data = bytearray(bundle.read_bytes())
- if len(data) < 80:
- raise SystemExit(f"{family}: short Booter bundle {bundle}")
- fields = struct.unpack_from("<14I", data, 8)
- code_off, code_size = fields[5], fields[6]
- start = 72 + code_off
- end = start + code_size
- code = debug.read_bytes()
- if len(code) != code_size:
- raise SystemExit(
- f"{family}: debug Booter {debug} is {len(code)} bytes; bundle needs {code_size}")
- if len(code) % 16:
- raise SystemExit(f"{family}: debug Booter {debug} is not AES block aligned")
- if end > len(data):
- raise SystemExit(f"{family}: Booter code range outside bundle")
- data[start:end] = code
- bundle.write_bytes(data)
-
-
-def prepare_run(family: str, run_dir: Path) -> dict:
- run_dir.mkdir(parents=True, exist_ok=True)
- rom = run_dir / "rom.bin"
- manifest = prepare_rom(family, rom)
- assets = prepare_assets(family, run_dir / "assets")
- manifest["assets"] = assets
- manifest["run_dir"] = str(run_dir)
- return manifest
diff --git a/tests/integration/build.py b/tests/integration/build.py
deleted file mode 100644
index 4266af2..0000000
--- a/tests/integration/build.py
+++ /dev/null
@@ -1,66 +0,0 @@
-# SPDX-License-Identifier: GPL-2.0-only
-"""Build the runner shared object from the working tree.
-
-The runner links the real driver sources, the kernel shim and the native
-device library into one object. Python loads it with ctypes and calls
-gd_run_transaction once per transaction.
-"""
-from __future__ import annotations
-
-import os
-import shlex
-import subprocess
-import sys
-from pathlib import Path
-
-ROOT = Path(__file__).resolve().parents[2]
-HERE = Path(__file__).resolve().parent
-DRIVER = ROOT / "kernel" / "gpu_unlock"
-
-SOURCES = [
- HERE / "runner.c",
- HERE / "shim" / "shim.c",
- HERE / "device" / "gd.c",
- HERE / "device" / "aes.c",
- HERE / "device" / "wpr2.c",
- HERE / "device" / "services.c",
- DRIVER / "firmware_610.c",
- DRIVER / "unlock_610.c",
- DRIVER / "falcon.c",
- DRIVER / "vbios_610.c",
- DRIVER / "layout_610.c",
- DRIVER / "prep_610.c",
- DRIVER / "payload.c",
- DRIVER / "recipes.c",
- DRIVER / "gfw_eval.c",
-]
-
-FLAGS = [
- "-std=gnu11", "-O2", "-g", "-fPIC", "-shared", "-pthread",
- "-Wall", "-Wextra", "-Wno-unused-parameter", "-Wno-sign-compare",
- "-I" + str(HERE / "shim"),
- "-I" + str(ROOT),
- "-DGPU_UNLOCK_FWSEC_APP=0x45",
-]
-
-
-def build(output: Path | None = None, *, sanitize: bool = False,
- verbose: bool = False) -> Path:
- output = Path(output or ROOT / "build" / "integration")
- output.mkdir(parents=True, exist_ok=True)
- library = output / "libgdrunner.so"
- flags = list(FLAGS)
- if sanitize:
- flags[1] = "-O1"
- flags += ["-fsanitize=address,undefined", "-fno-omit-frame-pointer"]
- command = shlex.split(os.environ.get("CC", "cc")) + flags + [
- str(source) for source in SOURCES
- ] + ["-o", str(library)]
- if verbose:
- print("building:", shlex.join(command), file=sys.stderr)
- subprocess.run(command, check=True, cwd=ROOT)
- return library
-
-
-if __name__ == "__main__":
- print(build(verbose=bool(os.environ.get("GPU_UNLOCK_DIAG"))))
diff --git a/tests/integration/codetable.py b/tests/integration/codetable.py
deleted file mode 100644
index 4959990..0000000
--- a/tests/integration/codetable.py
+++ /dev/null
@@ -1,120 +0,0 @@
-# SPDX-License-Identifier: GPL-2.0-only
-"""Pre-decode the debug FWSEC IMEM with the Python reference decoder.
-
-The device still loads and decrypts the image itself. This table is input
-data for the C interpreter, so no Python runs after the transaction starts.
-"""
-from __future__ import annotations
-
-import ctypes as C
-import struct
-from pathlib import Path
-
-from emulator.falcon_emu.decode import _decode
-
-OPS = {
- "fallback": 0, "mov": 1, "sethi": 2, "add": 3, "adc": 4, "sub": 5,
- "sbb": 6, "cmpu": 7, "cmps": 8, "cmp": 9, "shl": 10, "shr": 11,
- "sar": 12, "shlc": 13, "shrc": 14, "not": 15, "neg": 16, "hswap": 17,
- "clear": 18, "setf": 19, "ld": 20, "st": 21, "mulu": 22, "muls": 23,
- "sext": 24, "extr": 25, "extrs": 26, "ins": 27, "and": 28, "or": 29,
- "xor": 30, "xbit": 31, "xbitf": 32, "bset": 33, "bclr": 34, "btgl": 35,
- "div": 36, "mod": 37, "bra": 38, "brcmp": 39, "jmp": 40, "addsp": 41,
- "nop": 42, "movsr": 43, "movrs": 44, "call": 45, "ret": 46, "push": 47,
- "pop": 48, "mpush": 49, "mpop": 50, "mpopret": 51, "mpopadd": 52,
- "mpopaddret": 53, "lbra": 65, "lcall": 66, "exit": 63, "trap": 64,
- "iord": 60, "iords": 61, "iowr": 62, "iowrs": 62,
- "sleep": 67, "xdwait": 78, "xdfence": 79, "xcwait": 80,
- "csecret": 70, "cenc": 71, "cxor": 72, "crnd": 73, "ckeyreg": 74,
- "cmov": 75, "xdst": 76, "xdld": 77, "cxsin": 81, "cxsout": 82,
- "vtlb": 83, "itlb": 84, "ptlb": 85, "cs0begin": 86, "cs0exec": 87,
- "csigclr": 88, "bsetf": 89, "bclrf": 90, "btglf": 91, "setp": 92, "cxset": 93,
- "iret": 94, "xcld": 95,
- "cadd": 96, "cand": 97, "crev": 98, "cgfmul": 99, "cchmod": 100,
- "ckexp": 101, "ckrexp": 102, "cdec": 103, "csigcmp": 104, "csigenc": 105,
- "cs1begin": 106, "cs1exec": 107,
- "cicmov": 120, "cixsin": 121, "cixsout": 122, "cicrnd": 123,
- "cics0begin": 124, "cics0exec": 125, "cics1begin": 126, "cics1exec": 127,
- "cichmod": 128, "cixor": 129, "cicadd": 130, "cicand": 131,
- "cicrev": 132, "cicgfmul": 133, "cicsecret": 134, "cikeyreg": 135,
- "cikexp": 136, "cikrexp": 137, "cicenc": 138, "cidec": 139,
- "cisigcmp": 140, "cisigenc": 141, "cisigclr": 142,
-}
-
-
-class Info(C.Structure):
- _fields_ = [("version", C.c_uint32), ("image_off", C.c_uint32),
- ("image_size", C.c_uint32), ("data_off", C.c_uint32),
- ("imem_size", C.c_uint32), ("dmem_size", C.c_uint32),
- ("interface_off", C.c_uint32)]
-
-
-def build(library: Path, rom: Path, key: bytes, output: Path) -> int:
- lib = C.CDLL(str(library))
- lib.gd_fwsec_extract.argtypes = [C.c_char_p, C.POINTER(Info)]
- info = Info()
- if lib.gd_fwsec_extract(str(rom).encode(), C.byref(info)) != 0:
- raise SystemExit("code table: FWSEC descriptor not found")
- image = bytearray(Path(rom).read_bytes()[info.image_off:info.image_off + info.image_size])
- if len(key) != 16:
- raise SystemExit("code table: key must be 16 bytes")
- imem = image[:info.imem_size]
- buffer = (C.c_ubyte * len(imem)).from_buffer(imem)
- if lib.gd_aes_decrypt_buffer(key, buffer, len(imem)) != 0:
- raise SystemExit("code table: AES failed")
- return _write_table(imem, output, 0)
-
-
-def _write_table(imem: bytes | bytearray, output: Path, base: int) -> int:
- records = []
- pc = base
- while pc < base + len(imem):
- rel = pc - base
- ins = _decode(pc, bytes(imem[rel:rel + 6]).ljust(6, b"\0"))
- if ins.op == "invalid":
- break
- target = ins.mem if isinstance(ins.mem, int) else 0
- if ins.op in ("bra", "jmp", "lbra", "lcall", "call") and target == 0:
- target = ins.a if isinstance(ins.a, int) else 0
- mem = ins.mem if hasattr(ins.mem, "base") else None
- mbase = 16 if mem is not None and mem.base == "sp" else (mem.base if mem is not None else 0)
- idx = mem.idx if mem is not None and mem.idx is not None else -1
- off = mem.off if mem is not None else 0
- scale = mem.scale if mem is not None else 1
- space = 1 if mem is not None and mem.space == "I" else 0
- records.append(struct.pack(
- "<IIiIIIiiiBBBBBB2x",
- pc & 0xffffffff, target & 0xffffffff, off,
- (ins.a & 0xffffffff) if isinstance(ins.a, int) else 0,
- (ins.b & 0xffffffff) if isinstance(ins.b, int) else 0,
- (ins.d & 0xffffffff) if isinstance(ins.d, int) else 0,
- mbase, idx, scale,
- ins.length, OPS.get(ins.op, 0), ins.sz, 1 if ins.sized else 0,
- 1 if ins.bimm else 0, space))
- pc += ins.length
- output.write_bytes(struct.pack("<4sII", b"GDC1", len(records), len(imem)) + b"".join(records))
- return len(records)
-
-
-def build_booter(library: Path, bundle: Path, key: bytes, output: Path) -> int:
- """Decode the SEC2 Booter application image, not the FWSEC image."""
- data = Path(bundle).read_bytes()
- if len(data) < 80:
- raise SystemExit("code table: short Booter bundle")
- fields = struct.unpack_from("<14I", data, 8)
- app_code_off, app_code_size = fields[5], fields[6]
- image = data[72:]
- if app_code_off + app_code_size > len(image):
- raise SystemExit("code table: Booter app code outside image")
- if len(key) != 16:
- raise SystemExit("code table: key must be 16 bytes")
- code = bytearray(image[app_code_off:app_code_off + app_code_size])
- lib = C.CDLL(str(library))
- lib.gd_aes_decrypt_buffer.argtypes = [C.c_char_p, C.c_void_p, C.c_size_t]
- buffer = (C.c_ubyte * len(code)).from_buffer(code)
- if lib.gd_aes_decrypt_buffer(key, buffer, len(code)) != 0:
- raise SystemExit("code table: Booter AES decrypt failed")
- count = _write_table(code, output, app_code_off)
- if not count:
- raise SystemExit("code table: Booter code is not decodable; install the external debug image")
- return count
diff --git a/tests/integration/device/aes.c b/tests/integration/device/aes.c
deleted file mode 100644
index fc5505c..0000000
--- a/tests/integration/device/aes.c
+++ /dev/null
@@ -1,252 +0,0 @@
-// SPDX-License-Identifier: GPL-2.0-only
-/* AES-128-ECB for the FWSEC load boundary and the crypto KDF. No padding: the
- * caller supplies whole blocks. */
-#include <stdbool.h>
-#include <stddef.h>
-#include <stdint.h>
-#include <string.h>
-
-#include "aes.h"
-
-static const uint8_t sbox[256] = {
- 0x63,0x7c,0x77,0x7b,0xf2,0x6b,0x6f,0xc5,0x30,0x01,0x67,0x2b,0xfe,0xd7,0xab,0x76,
- 0xca,0x82,0xc9,0x7d,0xfa,0x59,0x47,0xf0,0xad,0xd4,0xa2,0xaf,0x9c,0xa4,0x72,0xc0,
- 0xb7,0xfd,0x93,0x26,0x36,0x3f,0xf7,0xcc,0x34,0xa5,0xe5,0xf1,0x71,0xd8,0x31,0x15,
- 0x04,0xc7,0x23,0xc3,0x18,0x96,0x05,0x9a,0x07,0x12,0x80,0xe2,0xeb,0x27,0xb2,0x75,
- 0x09,0x83,0x2c,0x1a,0x1b,0x6e,0x5a,0xa0,0x52,0x3b,0xd6,0xb3,0x29,0xe3,0x2f,0x84,
- 0x53,0xd1,0x00,0xed,0x20,0xfc,0xb1,0x5b,0x6a,0xcb,0xbe,0x39,0x4a,0x4c,0x58,0xcf,
- 0xd0,0xef,0xaa,0xfb,0x43,0x4d,0x33,0x85,0x45,0xf9,0x02,0x7f,0x50,0x3c,0x9f,0xa8,
- 0x51,0xa3,0x40,0x8f,0x92,0x9d,0x38,0xf5,0xbc,0xb6,0xda,0x21,0x10,0xff,0xf3,0xd2,
- 0xcd,0x0c,0x13,0xec,0x5f,0x97,0x44,0x17,0xc4,0xa7,0x7e,0x3d,0x64,0x5d,0x19,0x73,
- 0x60,0x81,0x4f,0xdc,0x22,0x2a,0x90,0x88,0x46,0xee,0xb8,0x14,0xde,0x5e,0x0b,0xdb,
- 0xe0,0x32,0x3a,0x0a,0x49,0x06,0x24,0x5c,0xc2,0xd3,0xac,0x62,0x91,0x95,0xe4,0x79,
- 0xe7,0xc8,0x37,0x6d,0x8d,0xd5,0x4e,0xa9,0x6c,0x56,0xf4,0xea,0x65,0x7a,0xae,0x08,
- 0xba,0x78,0x25,0x2e,0x1c,0xa6,0xb4,0xc6,0xe8,0xdd,0x74,0x1f,0x4b,0xbd,0x8b,0x8a,
- 0x70,0x3e,0xb5,0x66,0x48,0x03,0xf6,0x0e,0x61,0x35,0x57,0xb9,0x86,0xc1,0x1d,0x9e,
- 0xe1,0xf8,0x98,0x11,0x69,0xd9,0x8e,0x94,0x9b,0x1e,0x87,0xe9,0xce,0x55,0x28,0xdf,
- 0x8c,0xa1,0x89,0x0d,0xbf,0xe6,0x42,0x68,0x41,0x99,0x2d,0x0f,0xb0,0x54,0xbb,0x16
-};
-
-static const uint8_t sbox_inv[256] = {
- 0x52,0x09,0x6a,0xd5,0x30,0x36,0xa5,0x38,0xbf,0x40,0xa3,0x9e,0x81,0xf3,0xd7,0xfb,
- 0x7c,0xe3,0x39,0x82,0x9b,0x2f,0xff,0x87,0x34,0x8e,0x43,0x44,0xc4,0xde,0xe9,0xcb,
- 0x54,0x7b,0x94,0x32,0xa6,0xc2,0x23,0x3d,0xee,0x4c,0x95,0x0b,0x42,0xfa,0xc3,0x4e,
- 0x08,0x2e,0xa1,0x66,0x28,0xd9,0x24,0xb2,0x76,0x5b,0xa2,0x49,0x6d,0x8b,0xd1,0x25,
- 0x72,0xf8,0xf6,0x64,0x86,0x68,0x98,0x16,0xd4,0xa4,0x5c,0xcc,0x5d,0x65,0xb6,0x92,
- 0x6c,0x70,0x48,0x50,0xfd,0xed,0xb9,0xda,0x5e,0x15,0x46,0x57,0xa7,0x8d,0x9d,0x84,
- 0x90,0xd8,0xab,0x00,0x8c,0xbc,0xd3,0x0a,0xf7,0xe4,0x58,0x05,0xb8,0xb3,0x45,0x06,
- 0xd0,0x2c,0x1e,0x8f,0xca,0x3f,0x0f,0x02,0xc1,0xaf,0xbd,0x03,0x01,0x13,0x8a,0x6b,
- 0x3a,0x91,0x11,0x41,0x4f,0x67,0xdc,0xea,0x97,0xf2,0xcf,0xce,0xf0,0xb4,0xe6,0x73,
- 0x96,0xac,0x74,0x22,0xe7,0xad,0x35,0x85,0xe2,0xf9,0x37,0xe8,0x1c,0x75,0xdf,0x6e,
- 0x47,0xf1,0x1a,0x71,0x1d,0x29,0xc5,0x89,0x6f,0xb7,0x62,0x0e,0xaa,0x18,0xbe,0x1b,
- 0xfc,0x56,0x3e,0x4b,0xc6,0xd2,0x79,0x20,0x9a,0xdb,0xc0,0xfe,0x78,0xcd,0x5a,0xf4,
- 0x1f,0xdd,0xa8,0x33,0x88,0x07,0xc7,0x31,0xb1,0x12,0x10,0x59,0x27,0x80,0xec,0x5f,
- 0x60,0x51,0x7f,0xa9,0x19,0xb5,0x4a,0x0d,0x2d,0xe5,0x7a,0x9f,0x93,0xc9,0x9c,0xef,
- 0xa0,0xe0,0x3b,0x4d,0xae,0x2a,0xf5,0xb0,0xc8,0xeb,0xbb,0x3c,0x83,0x53,0x99,0x61,
- 0x17,0x2b,0x04,0x7e,0xba,0x77,0xd6,0x26,0xe1,0x69,0x14,0x63,0x55,0x21,0x0c,0x7d
-};
-
-static uint8_t xtime(uint8_t x)
-{
- return (uint8_t)((x << 1) ^ ((x >> 7) * 0x1b));
-}
-
-static uint8_t mul(uint8_t a, uint8_t b)
-{
- uint8_t result = 0;
- while (b) {
- if (b & 1)
- result ^= a;
- a = xtime(a);
- b >>= 1;
- }
- return result;
-}
-
-static void add_round_key(uint8_t state[16], const uint8_t *key)
-{
- int i;
- for (i = 0; i < 16; i++)
- state[i] ^= key[i];
-}
-
-static void inv_shift_rows(uint8_t s[16])
-{
- uint8_t t;
- t = s[13]; s[13] = s[9]; s[9] = s[5]; s[5] = s[1]; s[1] = t;
- t = s[2]; s[2] = s[10]; s[10] = t;
- t = s[6]; s[6] = s[14]; s[14] = t;
- t = s[3]; s[3] = s[7]; s[7] = s[11]; s[11] = s[15]; s[15] = t;
-}
-
-static void inv_mix_columns(uint8_t s[16])
-{
- int c;
- for (c = 0; c < 4; c++) {
- uint8_t *p = s + c * 4;
- uint8_t a = p[0], b = p[1], d = p[2], e = p[3];
- p[0] = mul(a, 14) ^ mul(b, 11) ^ mul(d, 13) ^ mul(e, 9);
- p[1] = mul(a, 9) ^ mul(b, 14) ^ mul(d, 11) ^ mul(e, 13);
- p[2] = mul(a, 13) ^ mul(b, 9) ^ mul(d, 14) ^ mul(e, 11);
- p[3] = mul(a, 11) ^ mul(b, 13) ^ mul(d, 9) ^ mul(e, 14);
- }
-}
-
-static void expand_key(const uint8_t key[16], uint8_t round[176])
-{
- static const uint8_t rcon[10] = {1,2,4,8,16,32,64,128,27,54};
- uint8_t temp[4];
- int i, j;
- memcpy(round, key, 16);
- for (i = 16; i < 176; i += 4) {
- memcpy(temp, round + i - 4, 4);
- if (i % 16 == 0) {
- uint8_t t = temp[0];
- temp[0] = sbox[temp[1]];
- temp[1] = sbox[temp[2]];
- temp[2] = sbox[temp[3]];
- temp[3] = sbox[t];
- temp[0] ^= rcon[i / 16 - 1];
- }
- for (j = 0; j < 4; j++)
- round[i + j] = round[i - 16 + j] ^ temp[j];
- }
-}
-
-static void shift_rows(uint8_t s[16])
-{
- uint8_t t;
- t = s[1]; s[1] = s[5]; s[5] = s[9]; s[9] = s[13]; s[13] = t;
- t = s[2]; s[2] = s[10]; s[10] = t;
- t = s[6]; s[6] = s[14]; s[14] = t;
- t = s[3]; s[3] = s[15]; s[15] = s[11]; s[11] = s[7]; s[7] = t;
-}
-
-static void mix_columns(uint8_t s[16])
-{
- int c;
- for (c = 0; c < 4; c++) {
- uint8_t *p = s + c * 4;
- uint8_t a = p[0], b = p[1], d = p[2], e = p[3];
- p[0] = mul(a, 2) ^ mul(b, 3) ^ d ^ e;
- p[1] = a ^ mul(b, 2) ^ mul(d, 3) ^ e;
- p[2] = a ^ b ^ mul(d, 2) ^ mul(e, 3);
- p[3] = mul(a, 3) ^ b ^ d ^ mul(e, 2);
- }
-}
-
-void gd_aes128_expand_key(const uint8_t key[16], uint8_t round_keys[176])
-{
- expand_key(key, round_keys);
-}
-
-void gd_aes128_reverse_key(const uint8_t last_round[16], uint8_t key[16])
-{
- static const uint8_t rcon[10] = {1,2,4,8,16,32,64,128,27,54};
- uint8_t w[176], t[4];
- int i, j;
-
- memcpy(w + 160, last_round, 16);
- for (i = 43; i >= 4; i--) {
- memcpy(t, w + (i - 1) * 4, 4);
- if (i % 4 == 0) {
- uint8_t x = t[0];
- t[0] = sbox[t[1]];
- t[1] = sbox[t[2]];
- t[2] = sbox[t[3]];
- t[3] = sbox[x];
- t[0] ^= rcon[i / 4 - 1];
- }
- for (j = 0; j < 4; j++)
- w[(i - 4) * 4 + j] = w[i * 4 + j] ^ t[j];
- }
- memcpy(key, w, 16);
-}
-
-void gd_aes128_encrypt_block(const uint8_t key[16], const uint8_t in[16], uint8_t out[16])
-{
- uint8_t round[176], state[16];
- int r, i;
- expand_key(key, round);
- memcpy(state, in, 16);
- add_round_key(state, round);
- for (r = 1; r <= 10; r++) {
- for (i = 0; i < 16; i++)
- state[i] = sbox[state[i]];
- shift_rows(state);
- if (r != 10)
- mix_columns(state);
- add_round_key(state, round + r * 16);
- }
- memcpy(out, state, 16);
-}
-
-int gd_aes128_encrypt(const uint8_t key[16], uint8_t *data, size_t size)
-{
- uint8_t block[16];
- size_t at;
- if (size % 16)
- return -1;
- for (at = 0; at < size; at += 16) {
- gd_aes128_encrypt_block(key, data + at, block);
- memcpy(data + at, block, 16);
- }
- return 0;
-}
-
-void gd_aes128_decrypt_block(const uint8_t key[16], const uint8_t in[16], uint8_t out[16])
-{
- uint8_t round[176], state[16];
- int r, i;
- expand_key(key, round);
- memcpy(state, in, 16);
- add_round_key(state, round + 160);
- for (r = 9; r > 0; r--) {
- inv_shift_rows(state);
- for (i = 0; i < 16; i++)
- state[i] = sbox_inv[state[i]];
- add_round_key(state, round + r * 16);
- inv_mix_columns(state);
- }
- inv_shift_rows(state);
- for (i = 0; i < 16; i++)
- state[i] = sbox_inv[state[i]];
- add_round_key(state, round);
- memcpy(out, state, 16);
-}
-
-int gd_aes128_decrypt(const uint8_t key[16], uint8_t *data, size_t size)
-{
- uint8_t block[16];
- size_t at;
- if (size % 16)
- return -1;
- for (at = 0; at < size; at += 16) {
- gd_aes128_decrypt_block(key, data + at, block);
- memcpy(data + at, block, 16);
- }
- return 0;
-}
-
-bool gd_aes128_selftest(void)
-{
- /* NIST SP 800-38A F.1.2, first ECB block. */
- static const uint8_t key[16] = {
- 0x2b,0x7e,0x15,0x16,0x28,0xae,0xd2,0xa6,
- 0xab,0xf7,0x15,0x88,0x09,0xcf,0x4f,0x3c,
- };
- static const uint8_t cipher[16] = {
- 0x3a,0xd7,0x7b,0xb4,0x0d,0x7a,0x36,0x60,
- 0xa8,0x9e,0xca,0xf3,0x24,0x66,0xef,0x97,
- };
- static const uint8_t plain[16] = {
- 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
- 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,
- };
- uint8_t out[16];
- gd_aes128_encrypt_block(key, plain, out);
- if (memcmp(out, cipher, 16) != 0)
- return false;
- gd_aes128_decrypt_block(key, cipher, out);
- return memcmp(out, plain, 16) == 0;
-}
diff --git a/tests/integration/device/aes.h b/tests/integration/device/aes.h
deleted file mode 100644
index 1c1e299..0000000
--- a/tests/integration/device/aes.h
+++ /dev/null
@@ -1,17 +0,0 @@
-/* SPDX-License-Identifier: GPL-2.0-only */
-#ifndef GD_AES_H
-#define GD_AES_H
-
-#include <stdbool.h>
-#include <stddef.h>
-#include <stdint.h>
-
-void gd_aes128_decrypt_block(const uint8_t key[16], const uint8_t in[16], uint8_t out[16]);
-void gd_aes128_encrypt_block(const uint8_t key[16], const uint8_t in[16], uint8_t out[16]);
-int gd_aes128_encrypt(const uint8_t key[16], uint8_t *data, size_t size);
-int gd_aes128_decrypt(const uint8_t key[16], uint8_t *data, size_t size);
-void gd_aes128_expand_key(const uint8_t key[16], uint8_t round_keys[176]);
-void gd_aes128_reverse_key(const uint8_t last_round[16], uint8_t key[16]);
-bool gd_aes128_selftest(void);
-
-#endif
diff --git a/tests/integration/device/booter_regs.h b/tests/integration/device/booter_regs.h
deleted file mode 100644
index 6924b40..0000000
--- a/tests/integration/device/booter_regs.h
+++ /dev/null
@@ -1,238 +0,0 @@
-/* SPDX-License-Identifier: GPL-2.0-only */
-/* Generated from emulator/hw/booter.toml by tests/integration/hooks.py.
- * Reset values only; behavior lives in gd.c and the compiled hook table. */
-#ifndef GD_BOOTER_REGS_H
-#define GD_BOOTER_REGS_H
-struct gd_reg_default { unsigned addr, reset, mask; };
-static const struct gd_reg_default gd_bar0_defaults[] = {
- { 0x00000000u, 0x172000a1u, 0xffffffffu },
- { 0x00000200u, 0x00000002u, 0xffffffffu },
- { 0x00000a00u, 0x17600000u, 0xffffffffu },
- { 0x00001070u, 0x00000000u, 0x00000000u },
- { 0x00001074u, 0x00000000u, 0x00000000u },
- { 0x00001078u, 0x00000000u, 0x00000000u },
- { 0x00001438u, 0x00000000u, 0xffffffffu },
- { 0x00022430u, 0x00000007u, 0xffffffffu },
- { 0x00022434u, 0x00000006u, 0xffffffffu },
- { 0x00100ce0u, 0x00000000u, 0x7fffffffu },
- { 0x00110084u, 0x00000000u, 0xffffffffu },
- { 0x001100f4u, 0x80000000u, 0xffffffffu },
- { 0x00110100u, 0x00000010u, 0xffffffffu },
- { 0x0011010cu, 0x00000000u, 0xffffffffu },
- { 0x00110118u, 0x00000002u, 0xffffffffu },
- { 0x001103c0u, 0x00000000u, 0xffffffffu },
- { 0x001103c4u, 0xffffffffu, 0xffffffffu },
- { 0x00110600u, 0x00000000u, 0xffffffffu },
- { 0x00110624u, 0x00000000u, 0xffffffffu },
- { 0x00111180u, 0x00000000u, 0xffffffffu },
- { 0x00111198u, 0x00000000u, 0xffffffffu },
- { 0x0011119cu, 0x00000000u, 0xffffffffu },
- { 0x00111210u, 0x00000000u, 0xffffffffu },
- { 0x00111668u, 0x00000000u, 0xffffffffu },
- { 0x001180f8u, 0x00000000u, 0xffffffffu },
- { 0x00118128u, 0xffffff8fu, 0xffffffffu },
- { 0x0011812cu, 0xffffff88u, 0xffffffffu },
- { 0x0011821cu, 0x00000000u, 0xffffffffu },
- { 0x00118220u, 0x00000000u, 0xffffffffu },
- { 0x00118234u, 0x000000ffu, 0xffffffffu },
- { 0x00118244u, 0x00000000u, 0xffffffffu },
- { 0x00118248u, 0x00000000u, 0xffffffffu },
- { 0x0011824cu, 0x00000000u, 0xffffffffu },
- { 0x00118250u, 0x00000000u, 0xffffffffu },
- { 0x00118388u, 0xffffff88u, 0xffffffffu },
- { 0x0011838cu, 0xffffff88u, 0xffffffffu },
- { 0x00118390u, 0xffffff8fu, 0xffffffffu },
- { 0x0011839cu, 0x00000000u, 0xffffffffu },
- { 0x001183a0u, 0x00000000u, 0xffffffffu },
- { 0x001183a4u, 0x00006000u, 0xffffffffu },
- { 0x00118c6cu, 0x00000000u, 0xffffffffu },
- { 0x001f9000u, 0xfffffff0u, 0xffffffffu },
- { 0x001f9004u, 0x00000000u, 0xffffffffu },
- { 0x001f9008u, 0xfffffff0u, 0xffffffffu },
- { 0x001f900cu, 0x00000000u, 0xffffffffu },
- { 0x001f9010u, 0xfffffff0u, 0xffffffffu },
- { 0x001f9014u, 0x00000000u, 0xffffffffu },
- { 0x001f9018u, 0xfffffff0u, 0xffffffffu },
- { 0x001f901cu, 0x00000000u, 0xffffffffu },
- { 0x001f9020u, 0xfffffff0u, 0xffffffffu },
- { 0x001f9024u, 0x00000000u, 0xffffffffu },
- { 0x001f9028u, 0xfffffff0u, 0xffffffffu },
- { 0x001f902cu, 0x00000000u, 0xffffffffu },
- { 0x001f9030u, 0xfffffff0u, 0xffffffffu },
- { 0x001f9034u, 0x00000000u, 0xffffffffu },
- { 0x001f9038u, 0xfffffff0u, 0xffffffffu },
- { 0x001f903cu, 0x00000000u, 0xffffffffu },
- { 0x001f9040u, 0xfffffff0u, 0xffffffffu },
- { 0x001f9044u, 0x00000000u, 0xffffffffu },
- { 0x001f9048u, 0xfffffff0u, 0xffffffffu },
- { 0x001f904cu, 0x00000000u, 0xffffffffu },
- { 0x001f9050u, 0xfffffff0u, 0xffffffffu },
- { 0x001f9054u, 0x00000000u, 0xffffffffu },
- { 0x001f9058u, 0xfffffff0u, 0xffffffffu },
- { 0x001f905cu, 0x00000000u, 0xffffffffu },
- { 0x001f9060u, 0xfffffff0u, 0xffffffffu },
- { 0x001f9064u, 0x00000000u, 0xffffffffu },
- { 0x001f9068u, 0xfffffff0u, 0xffffffffu },
- { 0x001f906cu, 0x00000000u, 0xffffffffu },
- { 0x001f9070u, 0xfffffff0u, 0xffffffffu },
- { 0x001f9074u, 0x00000000u, 0xffffffffu },
- { 0x001f9078u, 0xfffffff0u, 0xffffffffu },
- { 0x001f907cu, 0x00000000u, 0xffffffffu },
- { 0x001f9080u, 0xfffffff0u, 0xffffffffu },
- { 0x001f9084u, 0x00000000u, 0xffffffffu },
- { 0x001f9088u, 0xfffffff0u, 0xffffffffu },
- { 0x001f908cu, 0x00000000u, 0xffffffffu },
- { 0x001f9090u, 0xfffffff0u, 0xffffffffu },
- { 0x001f9094u, 0x00000000u, 0xffffffffu },
- { 0x001f9098u, 0xfffffff0u, 0xffffffffu },
- { 0x001f909cu, 0x00000000u, 0xffffffffu },
- { 0x001f90a0u, 0xfffffff0u, 0xffffffffu },
- { 0x001f90a4u, 0x00000000u, 0xffffffffu },
- { 0x001f90a8u, 0xfffffff0u, 0xffffffffu },
- { 0x001f90acu, 0x00000000u, 0xffffffffu },
- { 0x001f90b0u, 0xfffffff0u, 0xffffffffu },
- { 0x001f90b4u, 0x00000000u, 0xffffffffu },
- { 0x001f90b8u, 0xfffffff0u, 0xffffffffu },
- { 0x001f90bcu, 0x00000000u, 0xffffffffu },
- { 0x001f91c0u, 0xfffffff0u, 0xffffffffu },
- { 0x001f91c4u, 0x00000000u, 0xffffffffu },
- { 0x001f91c8u, 0xfffffff0u, 0xffffffffu },
- { 0x001f91ccu, 0x00000000u, 0xffffffffu },
- { 0x001f91d0u, 0xfffffff0u, 0xffffffffu },
- { 0x001f91d4u, 0x00000000u, 0xffffffffu },
- { 0x001f91d8u, 0xfffffff0u, 0xffffffffu },
- { 0x001f91dcu, 0x00000000u, 0xffffffffu },
- { 0x001fa7c4u, 0xfffffbffu, 0xffffffffu },
- { 0x001fa7c8u, 0x0045cb8fu, 0xffffffffu },
- { 0x001fa7ccu, 0x0004c38fu, 0xffffffffu },
- { 0x001fa7d8u, 0xfffffbffu, 0xffffffffu },
- { 0x001fa7dcu, 0xfffffbffu, 0xffffffffu },
- { 0x001fa7e0u, 0xfffffbffu, 0xffffffffu },
- { 0x001fa7ecu, 0xfffffbffu, 0xffffffffu },
- { 0x001fa814u, 0x00000000u, 0xffffffffu },
- { 0x001fa818u, 0x00000000u, 0xffffffffu },
- { 0x001fa81cu, 0x1ffffe00u, 0xffffffffu },
- { 0x001fa820u, 0x00000000u, 0xffffffffu },
- { 0x001fa824u, 0x00000000u, 0xffffffffu },
- { 0x001fa828u, 0x00000000u, 0xffffffffu },
- { 0x001fa82cu, 0x00100000u, 0xffffffffu },
- { 0x001fa830u, 0x05cffff0u, 0xffffffffu },
- { 0x00625f04u, 0x00000001u, 0xffffffffu },
- { 0x00820000u, 0x00000000u, 0xffffffffu },
- { 0x00820004u, 0x0000023fu, 0x000003ffu },
- { 0x00820008u, 0x00000000u, 0xffffffffu },
- { 0x0082000cu, 0x12345678u, 0xffffffffu },
- { 0x00820028u, 0x00000000u, 0xffffffffu },
- { 0x00820040u, 0x00000001u, 0xffffffffu },
- { 0x00820050u, 0x00000000u, 0x03ff03ffu },
- { 0x00820060u, 0x00000000u, 0xffffffffu },
- { 0x00820078u, 0x00000000u, 0xffffffffu },
- { 0x0082007cu, 0x00000000u, 0xffffffffu },
- { 0x00820084u, 0x00000001u, 0xffffffffu },
- { 0x00820088u, 0x00000000u, 0x00000001u },
- { 0x008200d0u, 0xffffff8fu, 0xffffffffu },
- { 0x008200d8u, 0xffffff8fu, 0xffffffffu },
- { 0x008200dcu, 0xffffff8fu, 0xffffffffu },
- { 0x008200e0u, 0xffffff8fu, 0xffffffffu },
- { 0x008200f4u, 0xffffff8fu, 0xffffffffu },
- { 0x008200f8u, 0xffffff8fu, 0xffffffffu },
- { 0x008200fcu, 0xffffffffu, 0xffffffffu },
- { 0x00820350u, 0x00000000u, 0xffffffffu },
- { 0x00820508u, 0x00000000u, 0xffffffffu },
- { 0x0082050cu, 0x00000000u, 0xffffffffu },
- { 0x00820514u, 0x00000000u, 0xffffffffu },
- { 0x00820564u, 0x00000000u, 0xffffffffu },
- { 0x008205acu, 0x00000000u, 0xffffffffu },
- { 0x008205c8u, 0x00000000u, 0xffffffffu },
- { 0x00820760u, 0x00000001u, 0xffffffffu },
- { 0x00820764u, 0x00000000u, 0xffffffffu },
- { 0x00820768u, 0x00000000u, 0xffffffffu },
- { 0x0082076cu, 0x00000000u, 0xffffffffu },
- { 0x00820770u, 0x00000000u, 0xffffffffu },
- { 0x00820774u, 0x00000000u, 0xffffffffu },
- { 0x00820778u, 0x00000000u, 0xffffffffu },
- { 0x00820838u, 0x00000000u, 0xffffffffu },
- { 0x0082083cu, 0x00000000u, 0xffffffffu },
- { 0x00820840u, 0x00000000u, 0xffffffffu },
- { 0x00820844u, 0x00000000u, 0xffffffffu },
- { 0x00820848u, 0x00000000u, 0xffffffffu },
- { 0x0082084cu, 0x00000000u, 0xffffffffu },
- { 0x00820850u, 0x00000000u, 0xffffffffu },
- { 0x00820a40u, 0x00000000u, 0xffffffffu },
- { 0x00820a44u, 0x00000000u, 0xffffffffu },
- { 0x00820a48u, 0x00000000u, 0xffffffffu },
- { 0x00820a4cu, 0x00000000u, 0xffffffffu },
- { 0x00820a50u, 0x00000000u, 0xffffffffu },
- { 0x00820a54u, 0x00000000u, 0xffffffffu },
- { 0x00820a58u, 0x00000000u, 0xffffffffu },
- { 0x00820b84u, 0x00000000u, 0xffffffffu },
- { 0x00820c1cu, 0x00000000u, 0xffffffffu },
- { 0x00820c38u, 0x00000001u, 0xffffffffu },
- { 0x00820c3cu, 0x00000000u, 0xffffffffu },
- { 0x00820c40u, 0x00000000u, 0xffffffffu },
- { 0x00820c44u, 0x00000000u, 0xffffffffu },
- { 0x00820c48u, 0x00000000u, 0xffffffffu },
- { 0x00820c4cu, 0x00000000u, 0xffffffffu },
- { 0x00820c50u, 0x00000000u, 0xffffffffu },
- { 0x00823804u, 0xffffff8fu, 0xffffffffu },
- { 0x0082381cu, 0x40171022u, 0xffffffffu },
- { 0x00823820u, 0x00000002u, 0x0000000fu },
- { 0x00824148u, 0x00000001u, 0xffffffffu },
- { 0x008241e0u, 0x00000000u, 0xffffffffu },
- { 0x00824310u, 0x00000000u, 0xffffffffu },
- { 0x00824314u, 0x00000000u, 0xffffffffu },
- { 0x008400f4u, 0x80000000u, 0xffffffffu },
- { 0x0084010cu, 0x00000000u, 0xffffffffu },
- { 0x008403c0u, 0x00000000u, 0xffffffffu },
- { 0x008403c4u, 0xffffffffu, 0xffffffffu },
- { 0x00840600u, 0x00000000u, 0xffffffffu },
- { 0x00840624u, 0x00000000u, 0xffffffffu },
- { 0x00841180u, 0x00000000u, 0xffffffffu },
- { 0x00841198u, 0x00000000u, 0xffffffffu },
- { 0x0084119cu, 0x00000000u, 0xffffffffu },
- { 0x00841210u, 0x00000000u, 0xffffffffu },
- { 0x00841668u, 0x00000000u, 0xffffffffu },
-};
-static const struct gd_reg_default gd_io_defaults[] = {
- { 0x00000098u, 0x00000000u, 0xffffffffu },
- { 0x0000009cu, 0x00000000u, 0xffffffffu },
- { 0x000000b0u, 0x00000000u, 0xffffffffu },
- { 0x000000b4u, 0x00000000u, 0xffffffffu },
- { 0x000000b8u, 0x00000000u, 0xffffffffu },
- { 0x000000ecu, 0xffffff8fu, 0xffffffffu },
- { 0x00000128u, 0x00000000u, 0xffffffffu },
- { 0x00000138u, 0x00000000u, 0xffffffffu },
- { 0x0000013cu, 0x00000100u, 0xffffffffu },
- { 0x00000160u, 0x00000000u, 0xffffffffu },
- { 0x0000022cu, 0xffffffffu, 0xffffffffu },
- { 0x00000240u, 0x00003000u, 0xffffffffu },
- { 0x00000244u, 0x40000000u, 0xc0000000u },
- { 0x00000248u, 0x00000000u, 0x00000000u },
- { 0x0000024cu, 0x00000000u, 0x00000000u },
- { 0x00000340u, 0x001c0000u, 0xffffffffu },
- { 0x00000344u, 0x00000000u, 0xffffffffu },
- { 0x00000348u, 0x00000000u, 0xffffffffu },
- { 0x00000350u, 0x00000000u, 0xffffffffu },
- { 0x00000354u, 0x00000000u, 0xffffffffu },
- { 0x00000404u, 0x00000000u, 0xffffffffu },
- { 0x00000408u, 0x00100000u, 0xffffffffu },
- { 0x00000480u, 0x00000000u, 0xffffffffu },
- { 0x00000484u, 0x00000000u, 0xffffffffu },
- { 0x00000490u, 0x00000000u, 0xffffffffu },
- { 0x00000498u, 0x00000000u, 0xffffffffu },
- { 0x00000500u, 0x00000000u, 0xffffffffu },
- { 0x00000504u, 0x00000000u, 0xffffffffu },
- { 0x0000052cu, 0x00000000u, 0xffffffffu },
- { 0x00000600u, 0x00000000u, 0xffffffffu },
- { 0x00000604u, 0x00000000u, 0xffffffffu },
- { 0x00000608u, 0x00000000u, 0xffffffffu },
- { 0x00000624u, 0x00000000u, 0xffffffffu },
- { 0x0000066cu, 0x00000000u, 0xffffffffu },
- { 0x00000700u, 0x00000000u, 0x803000f3u },
- { 0x00000704u, 0x00000000u, 0xffffffffu },
- { 0x00000708u, 0x00000000u, 0xffffffffu },
- { 0x0000070cu, 0x00000000u, 0xffffffffu },
- { 0x00000b84u, 0x00000000u, 0xffffffffu },
- { 0x00000cc0u, 0x00000000u, 0xffffffffu },
-};
-#endif
diff --git a/tests/integration/device/gd.c b/tests/integration/device/gd.c
deleted file mode 100644
index 318e339..0000000
--- a/tests/integration/device/gd.c
+++ /dev/null
@@ -1,3306 +0,0 @@
-// SPDX-License-Identifier: GPL-2.0-only
-/* Native GA102 device model. Stage 1 models the host-visible register and
- * DMA behavior; execution is always by the Falcon core. */
-#include <errno.h>
-#include <pthread.h>
-#include <stdarg.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <time.h>
-
-#include "gd.h"
-#include "aes.h"
-#include "booter_regs.h"
-#include "wpr2.h"
-#include "services.h"
-
-#define SEC2_BASE 0x00840000u
-#define GSP_BASE 0x00110000u
-#define ENGINE_WINDOW 0x00004000u
-#define GD_IO_SIZE 0x02000000u
-/* The layout places every reservation below the top 256 MiB of usable
- * framebuffer; model that whole window so WPR2, the GSP image, the BL and
- * FRTS all have backing. The base is derived from the FB-size register. */
-#define GD_FB_SIZE (256u << 20)
-#define GD_DEVMAP_BASE 0x01000000u
-#define GD_DEVMAP_SIZE 0x00001000u
-#define PROM_OFFSET 0x00300000u
-#define PROM_SIZE 0x00100000u
-
-#define OFF_MAILBOX0 0x040u
-#define OFF_MAILBOX1 0x044u
-#define OFF_HWCFG2 0x0f4u
-#define OFF_CPUCTL 0x100u
-#define OFF_BOOTVEC 0x104u
-#define OFF_HWCFG 0x108u
-#define OFF_DMACTL 0x10cu
-#define OFF_DMABASE 0x110u
-#define OFF_DMAMOFFS 0x114u
-#define OFF_DMACMD 0x118u
-#define OFF_DMAFBOFFS 0x11cu
-#define OFF_DMABASE1 0x128u
-#define OFF_CPUCTL_ALT 0x130u
-#define OFF_IMEMC 0x180u
-#define OFF_IMEMD 0x184u
-#define OFF_IMEMT 0x188u
-#define OFF_DMEMC 0x1c0u
-#define OFF_DMEMD 0x1c4u
-#define OFF_ENGINE 0x3c0u
-#define OFF_RESET_PLM 0x3c4u
-
-#define GD_DIAG(...) do { } while (0)
-
-#define CPUCTL_START 0x2u
-#define CPUCTL_HALTED 0x10u
-#define HWCFG2_SCRUB (1u << 12)
-#define HWCFG2_RESET (1u << 31)
-
-struct gd_code_rec {
- uint32_t pc, target;
- int32_t off, a, b, d, base, idx, scale;
- uint8_t len, op, sz, sized, bimm, space;
-};
-
-struct gd_engine {
- struct gd_device *dev;
- int index;
- uint32_t base;
- bool enabled;
- bool started, halted, faulted;
- bool scrub_pending;
- uint64_t scrub_done_us;
- uint32_t cpuctl, hwcfg2, bootvec, hwcfg, dmactl;
- uint32_t dma_base, dma_base1, dma_moffs, dma_fboffs, dma_cmd, dma_stat;
- uint32_t mailbox[2], rm, scratch[2], reset_plm, engine_reg;
- uint32_t imemc, dmemc, pio_imem, pio_dmem;
- uint32_t pio_secure_lo, pio_secure_hi;
- bool csb_enable, csb_valid;
- uint32_t csb_pc;
- uint32_t imem_tag;
- uint8_t imem[0x10000];
- uint8_t dmem[0x10000];
- uint8_t devmap[GD_DEVMAP_SIZE];
- uint32_t io[GD_IO_SIZE / 4];
- uint8_t io_valid[GD_IO_SIZE / 4];
- pthread_t thread;
- pthread_mutex_t lock;
- pthread_cond_t wake;
- bool thread_up, stop, busy, pending_kick;
- pthread_cond_t idle;
- uint32_t dma_q[64];
- unsigned qhead, qtail;
- uint64_t instructions;
- uint32_t r[16], pc, sp, flags;
- bool core_fault;
- uint32_t sr[16];
- uint32_t call_depth;
- uint32_t call_pc[64];
- uint32_t pc_ring[64], pc_ring_at;
- uint32_t br_pc[64], br_tgt[64], br_sp[64], br_at;
- uint32_t ret_pc[64], ret_val[64], ret_sp[64], ret_at;
- uint32_t sh_ret[128], sh_sp[128], sh_at, sh_bad;
- uint32_t halt_pc, halt_reason;
- uint32_t token, owner, aux_owner, response, response_ready;
- uint8_t c[8][16];
- bool xfer_trap, xfer_shortcut;
- uint32_t seq_pending;
- bool crypto_recording;
- uint8_t crypto_record_slot, crypto_record_left;
- uint8_t crypto_script_op[2][64], crypto_script_d[2][64], crypto_script_a[2][64], crypto_script_imm[2][64];
- uint8_t crypto_script_count[2];
- uint8_t crypto_stream_in[64][16], crypto_stream_out[64][16];
- uint8_t fwsec_kdf_out[16], fwsec_expect[32], fwsec_digest[16];
- bool fwsec_kdf_seen, fwsec_digest_seen;
- uint8_t crypto_stream_in_count, crypto_stream_out_count;
- uint64_t crypto_rng;
- uint8_t tlb_flags[256], tlb_virt[256];
- uint32_t keyreg;
- uint32_t xfer_count;
- uint32_t sha_op, sha_status;
- uint32_t sha_mutex;
- uint32_t sha_h[8], sha_buf_len, sha_update_bits;
- uint8_t sha_buf[64];
- uint64_t sha_total_bits;
- bool sha_started;
- bool sha_stream;
- uint32_t sha_in_addr, sha_in_hi, sha_msg_len;
- uint32_t sha_result[8];
- uint32_t doc_addr, doc_data[3];
- uint32_t dic_count, dic_valid, dic_data[3];
- uint32_t se_regs[0x8000];
- struct gd_csec csec;
- struct gd_doc doc;
- struct gd_trng trng;
- struct gd_code_rec *code;
- uint32_t code_count;
- int32_t *code_index;
- bool c_hidden[8];
-};
-
-struct gd_csb {
- /* NV_SSE_SE_COMMON_MUTEX: 3 groups x 0x50 bytes. ID is an
- * 8-bit hardware-assigned token, MUTEX has 16 slots, and
- * ID_RELEASE returns the token. */
- uint32_t plm[3];
- uint8_t id[3];
- uint8_t mutex[3][16];
- bool released[3];
-};
-
-struct gd_device {
- const struct gd_board *board;
- struct gd_run_config config;
- uint32_t *bar;
- uint8_t *bar_valid;
- uint8_t *prom;
- size_t prom_size;
- struct gd_engine engine[2];
- pthread_mutex_t lock;
- pthread_mutex_t bar_lock;
- pthread_mutex_t dma_lock;
- struct gd_dma_span dma[GD_MAX_DMA];
- uint32_t dma_count;
- struct gd_csb csb;
- uint8_t *fb;
- size_t fb_size;
- uint64_t fb_base;
- uint32_t pci_command;
- uint32_t pci_saved_command;
- uint32_t devices;
- uint64_t time_us;
- int device_error;
- uint32_t fault_pc, fault_opcode;
- uint32_t denied_writes;
- uint32_t plm_violations;
- bool plm_opened_seen, target_write_seen;
- uint32_t plm_feat_latched, plm_wpr_cfg_latched;
- uint8_t secrets[256][16];
- bool secret_set[256];
- uint32_t unknown_count;
- struct gd_unknown unknown[GD_MAX_UNKNOWN];
- uint64_t host_writes, host_reads, engine_writes, engine_reads;
- uint32_t launches[2];
- uint32_t resets;
- bool flr_seen;
- bool fwsec_launch_reached, booter_launch_reached;
- bool fwsec_kdf_ok;
- bool fwsec_mac_ok;
- bool wpr2_normalized;
- uint32_t feats[8];
- uint32_t feat_count;
- uint32_t wpr2_lo, wpr2_hi;
- uint32_t ss0, ss1;
- struct gd_trace_record trace[GD_MAX_TRACE];
- uint32_t trace_head, trace_count;
- pthread_mutex_t trace_lock;
-};
-
-static uint64_t now_us(void);
-
-static void trace_add(struct gd_device *dev, enum gd_trace_kind kind, int source,
- uint32_t address, uint32_t value, uint32_t pc)
-{
- uint32_t slot;
- pthread_mutex_lock(&dev->trace_lock);
- slot = dev->trace_head++ % GD_MAX_TRACE;
- if (dev->trace_count < GD_MAX_TRACE)
- dev->trace_count++;
- dev->trace[slot].time_us = now_us();
- dev->trace[slot].kind = (uint16_t)kind;
- dev->trace[slot].source = (uint16_t)source;
- dev->trace[slot].address = address;
- dev->trace[slot].value = value;
- dev->trace[slot].pc = pc;
- dev->trace[slot].size = 4;
- pthread_mutex_unlock(&dev->trace_lock);
-}
-
-static uint64_t now_us(void)
-{
- struct timespec ts;
- clock_gettime(CLOCK_MONOTONIC, &ts);
- return (uint64_t)ts.tv_sec * 1000000u + ts.tv_nsec / 1000u;
-}
-
-void gd_log(struct gd_device *dev, enum gd_log_level level, const char *format, ...)
-{
- va_list args;
- (void)level;
- if (!dev || !dev->config.verbose)
- return;
- va_start(args, format);
- vfprintf(stderr, format, args);
- va_end(args);
-}
-
-static void device_error(struct gd_device *dev, const char *what, uint32_t address, uint32_t value)
-{
- if (!dev->device_error)
- dev->device_error = -ENODEV;
- gd_log(dev, GD_LOG_ERROR, "device: %s at %#x value=%#x\n", what, address, value);
-}
-
-static void record_unknown(struct gd_device *dev, bool write, uint32_t addr, uint32_t value, uint32_t pc)
-{
- if (dev->unknown_count < GD_MAX_UNKNOWN) {
- struct gd_unknown *u = &dev->unknown[dev->unknown_count++];
- u->write = write;
- u->address = addr;
- u->value = value;
- u->pc = pc;
- }
- device_error(dev, write ? "unknown write" : "unknown read", addr, value);
-}
-
-static struct gd_engine *engine_for(struct gd_device *dev, uint32_t address, uint32_t *offset)
-{
- uint32_t off;
-
- (void)off;
- if (address >= SEC2_BASE && address < SEC2_BASE + ENGINE_WINDOW) {
- *offset = address - SEC2_BASE;
- return &dev->engine[0];
- }
- if (address >= GSP_BASE && address < GSP_BASE + ENGINE_WINDOW) {
- *offset = address - GSP_BASE;
- return &dev->engine[1];
- }
- return NULL;
-}
-
-static struct gd_dma_span *dma_find(struct gd_device *dev, uint64_t address, size_t size)
-{
- uint32_t i;
- for (i = 0; i < dev->dma_count; i++) {
- struct gd_dma_span *s = &dev->dma[i];
- if (s->live && address >= s->address && address + size <= s->address + s->size)
- return s;
- }
- return NULL;
-}
-
-static struct gd_dma_span *dma_find_size(struct gd_device *dev, size_t size)
-{
- uint32_t i;
- for (i = 0; i < dev->dma_count; i++)
- if (dev->dma[i].live && dev->dma[i].size == size)
- return &dev->dma[i];
- return NULL;
-}
-
-static void plm_latch_write(struct gd_device *dev, uint32_t address, uint32_t value);
-
-static void set_bar_locked(struct gd_device *dev, uint32_t address, uint32_t value)
-{
- if (address + 4 > GD_BAR_SIZE) {
- record_unknown(dev, true, address, value, 0);
- return;
- }
- dev->bar[address / 4] = value;
- dev->bar_valid[address / 4] = 1;
- plm_latch_write(dev, address, value);
-}
-
-static void settle_scrub(struct gd_device *dev, struct gd_engine *eng)
-{
- if (!eng->scrub_pending)
- return;
- if (dev->time_us < eng->scrub_done_us)
- return;
- eng->scrub_pending = false;
- eng->hwcfg2 &= ~HWCFG2_SCRUB;
- memset(eng->imem, 0, sizeof(eng->imem));
- memset(eng->dmem, 0, sizeof(eng->dmem));
- gd_log(dev, GD_LOG_DEBUG, "device: engine %d scrub complete\n", eng->index);
-}
-
-static void engine_reset_state(struct gd_device *dev, struct gd_engine *eng)
-{
- eng->started = eng->faulted = false;
- /* A CPU reset leaves the core halted and not started; the next
- * CPUCTL.STARTCPU write clears HALTED. */
- eng->halted = true;
- eng->cpuctl = CPUCTL_HALTED;
- eng->bootvec = 0;
- eng->dmactl = 0;
- eng->dma_stat = 2;
- eng->rm = 0;
- eng->reset_plm = 0xffffffffu; /* all levels enabled, per RESET_PLM init */
- /* The CPU reset restores the per-register masks to all-levels; the
- * Booter locks them again before it starts. */
- eng->io[0x288 / 4] = 0xffffffffu;
- eng->io[0x194 / 4] = 0xffffffffu;
- eng->io[0x28c / 4] = 0xffffffffu;
- eng->io[0x190 / 4] = 0xffffffffu;
- eng->io[0x18c / 4] = 0xffffffffu;
- eng->io[0x2b0 / 4] = 0xffffffffu;
- eng->io[0x3c4 / 4] = eng->reset_plm;
- eng->engine_reg = 0;
- eng->pio_imem = eng->pio_dmem = 0;
- eng->pio_secure_lo = eng->pio_secure_hi = 0;
- eng->csb_enable = true;
- eng->csb_valid = false;
- eng->csb_pc = 0;
- eng->sha_started = false;
- eng->sha_buf_len = 0;
- eng->sha_update_bits = 0;
- eng->sha_total_bits = 0;
- eng->imemc = eng->dmemc = 0;
- eng->imem_tag = 0;
- eng->hwcfg2 = HWCFG2_RESET;
- eng->scrub_pending = false;
- eng->scrub_done_us = 0;
- memset(eng->imem, 0, sizeof(eng->imem));
- memset(eng->dmem, 0, sizeof(eng->dmem));
- memset(eng->se_regs, 0, sizeof(eng->se_regs));
- eng->se_regs[0x144 / 4] = 0xffffffffu; /* SE mutex timeout default */
- eng->se_regs[0x114 / 4] = 1; /* PKA mutex free/available */
- eng->se_regs[0x4020 / 4] = 1u << 30; /* PKA status IRQ */
-}
-
-static uint32_t mask_of(int sz) { return sz == 32 ? 0xffffffffu : (1u << sz) - 1; }
-
-static void set_r(struct gd_engine *eng, int d, uint32_t value, int sz)
-{
- uint32_t mask = mask_of(sz);
- if (sz == 32)
- eng->r[d] = value;
- else
- eng->r[d] = (eng->r[d] & ~mask) | (value & mask);
-}
-
-static void set_flags(struct gd_engine *eng, uint32_t result, int sz, int overflow, int carry)
-{
- uint32_t f = eng->flags & ~0xe00u;
- if (carry >= 0)
- f = (f & ~0x100u) | (uint32_t)carry << 8;
- eng->flags = f | (uint32_t)overflow << 9 |
- ((result >> (sz - 1)) & 1) << 10 | (result == 0) << 11;
-}
-
-static int condition(uint32_t f, int cc)
-{
- int c, o, s, z;
- if (cc < 8)
- return (f >> cc) & 1;
- if (cc >= 0x10 && cc < 0x18)
- return !((f >> (cc - 0x10)) & 1);
- c = (f >> 8) & 1; o = (f >> 9) & 1; s = (f >> 10) & 1; z = (f >> 11) & 1;
- switch (cc) {
- case 8: return c; case 9: return o; case 10: return s; case 11: return z;
- case 12: return !c && !z; case 13: return c || z; case 14: return 1;
- case 24: return !c; case 25: return !o; case 26: return !s; case 27: return !z;
- case 28: return !(o ^ s) && !z; case 29: return (o ^ s) || z;
- case 30: return o ^ s; case 31: return !(o ^ s); default: return -1;
- }
-}
-
-
-/* ---- crypto coprocessor and code TLB ---------------------------------- */
-
-static void crypto_gfmul2(const uint8_t in[16], uint8_t out[16])
-{
- uint8_t v[16];
- int carry = in[0] >> 7;
- for (int i = 0; i < 15; i++)
- v[i] = (uint8_t)((in[i] << 1) | (in[i + 1] >> 7));
- v[15] = (uint8_t)(in[15] << 1);
- if (carry)
- v[15] ^= 0x87;
- memcpy(out, v, 16);
-}
-
-static void crypto_record(struct gd_engine *eng, unsigned op, unsigned d,
- unsigned a, unsigned imm)
-{
- unsigned slot = eng->crypto_record_slot;
- unsigned n;
- if (!eng->crypto_recording || slot > 1)
- return;
- n = eng->crypto_script_count[slot];
- if (n >= 64)
- return;
- eng->crypto_script_op[slot][n] = (uint8_t)op;
- eng->crypto_script_d[slot][n] = (uint8_t)d;
- eng->crypto_script_a[slot][n] = (uint8_t)a;
- eng->crypto_script_imm[slot][n] = (uint8_t)imm;
- eng->crypto_script_count[slot] = (uint8_t)(n + 1);
- if (eng->crypto_record_left)
- eng->crypto_record_left--;
- if (!eng->crypto_record_left)
- eng->crypto_recording = false;
-}
-
-static void crypto_exec_script(struct gd_engine *eng, unsigned slot, unsigned count);
-
-static void crypto_apply(struct gd_engine *eng, unsigned op, unsigned d,
- unsigned a, unsigned imm)
-{
- uint8_t tmp[16], key[16], round[176];
- uint64_t lo;
- bool cs_op = op == 86 || op == 87 || op == 88 || op == 104 ||
- op == 105 || op == 106 || op == 107;
-
- if (!cs_op)
- crypto_record(eng, op, d, a, imm);
- if (eng->crypto_recording && !cs_op)
- return;
-
- switch (op) {
- case 70: /* csecret */
- memset(eng->c[d], 0, 16);
- if (imm < 256 && eng->dev->secret_set[imm])
- memcpy(eng->c[d], eng->dev->secrets[imm], 16);
- eng->c_hidden[d] = true;
- break;
- case 71: /* cenc */
- gd_aes128_encrypt_block(eng->c[eng->keyreg & 7], eng->c[a], eng->c[d]);
- eng->c_hidden[d] = eng->c_hidden[eng->keyreg & 7] || eng->c_hidden[a];
- break;
- case 72: /* cxor */
- for (int i = 0; i < 16; i++)
- eng->c[d][i] ^= eng->c[a][i];
- eng->c_hidden[d] = eng->c_hidden[d] || eng->c_hidden[a];
- break;
- case 73: /* crnd */
- eng->crypto_rng = eng->crypto_rng * 6364136223846793005ULL + 1;
- for (int i = 0; i < 16; i++) {
- eng->crypto_rng = eng->crypto_rng * 6364136223846793005ULL + 1;
- eng->c[d][i] = (uint8_t)(eng->crypto_rng >> 56);
- }
- eng->c_hidden[d] = false;
- break;
- case 74: /* ckeyreg */
- eng->keyreg = d;
- break;
- case 75: /* cmov */
- memcpy(eng->c[d], eng->c[a], 16);
- eng->c_hidden[d] = eng->c_hidden[a];
- break;
- case 81: /* cxsin */
- if (eng->crypto_stream_in_count) {
- eng->crypto_stream_in_count--;
- memcpy(eng->c[d], eng->crypto_stream_in[eng->crypto_stream_in_count], 16);
- } else {
- memset(eng->c[d], 0, 16);
- }
- eng->c_hidden[d] = false;
- break;
- case 82: /* cxsout */
- if (eng->crypto_stream_out_count < 8) {
- memcpy(eng->crypto_stream_out[eng->crypto_stream_out_count], eng->c[d], 16);
- eng->crypto_stream_out_count++;
- }
- break;
- case 86: /* cs0begin */
- eng->crypto_script_count[0] = 0;
- eng->crypto_stream_in_count = 0;
- eng->crypto_stream_out_count = 0;
- eng->seq_pending = 0;
- eng->crypto_recording = imm != 0;
- eng->crypto_record_slot = 0;
- eng->crypto_record_left = (uint8_t)imm;
- break;
- case 87: /* cs0exec: arm the sequencer; data flow drives it */
- eng->seq_pending = imm;
- break;
- case 88: /* csigclr */
- break;
- case 96: /* cadd: 64-bit big-endian counter in bytes 8..15 */
- lo = 0;
- for (int i = 8; i < 16; i++)
- lo = (lo << 8) | eng->c[d][i];
- lo += imm;
- for (int i = 15; i >= 8; i--) {
- eng->c[d][i] = (uint8_t)lo;
- lo >>= 8;
- }
- break;
- case 97: /* cand */
- for (int i = 0; i < 16; i++)
- eng->c[d][i] &= eng->c[a][i];
- eng->c_hidden[d] = eng->c_hidden[d] || eng->c_hidden[a];
- break;
- case 98: /* crev */
- for (int i = 0; i < 16; i++)
- tmp[i] = eng->c[a][15 - i];
- memcpy(eng->c[d], tmp, 16);
- eng->c_hidden[d] = eng->c_hidden[a];
- break;
- case 99: /* cgfmul */
- crypto_gfmul2(eng->c[a], eng->c[d]);
- eng->c_hidden[d] = eng->c_hidden[a];
- break;
- case 100: /* cchmod */
- break;
- case 101: /* ckexp: last round key */
- gd_aes128_expand_key(eng->c[a], round);
- memcpy(eng->c[d], round + 160, 16);
- eng->c_hidden[d] = eng->c_hidden[a];
- break;
- case 102: /* ckrexp */
- gd_aes128_reverse_key(eng->c[a], key);
- memcpy(eng->c[d], key, 16);
- eng->c_hidden[d] = eng->c_hidden[a];
- break;
- case 103: /* cdec */
- gd_aes128_reverse_key(eng->c[eng->keyreg & 7], key);
- gd_aes128_decrypt_block(key, eng->c[a], eng->c[d]);
- eng->c_hidden[d] = eng->c_hidden[eng->keyreg & 7] || eng->c_hidden[a];
- break;
- case 104: /* csigcmp: no signature model */
- break;
- case 105: /* csigenc: no code signature, zeros */
- memset(eng->c[d], 0, 16);
- eng->c_hidden[d] = true;
- break;
- case 106: /* cs1begin */
- eng->crypto_script_count[1] = 0;
- eng->crypto_recording = imm != 0;
- eng->crypto_record_slot = 1;
- eng->crypto_record_left = (uint8_t)imm;
- break;
- case 107: /* cs1exec */
- crypto_exec_script(eng, 1, imm);
- break;
- case 120: case 121: case 122: case 123: case 124: case 125:
- case 126: case 127: case 128: case 129: case 130: case 131:
- case 132: case 133: case 134: case 135: case 136: case 137:
- case 138: case 139: case 140: case 141: case 142: {
- static const uint8_t map[] = {
- 75, 81, 82, 73, 86, 87, 106, 107, 100, 72, 96, 97,
- 98, 99, 70, 74, 101, 102, 71, 103, 104, 105, 88
- };
- uint32_t v = eng->r[a];
- crypto_apply(eng, map[op - 120], (v >> 8) & 7, v & 7, v & 0x3f);
- break;
- }
- default:
- break;
- }
- (void)tmp;
-}
-
-static void crypto_exec_script(struct gd_engine *eng, unsigned slot, unsigned count)
-{
- for (unsigned n = 0; n < count; n++)
- for (unsigned i = 0; i < eng->crypto_script_count[slot]; i++)
- crypto_apply(eng, eng->crypto_script_op[slot][i],
- eng->crypto_script_d[slot][i],
- eng->crypto_script_a[slot][i],
- eng->crypto_script_imm[slot][i]);
-}
-
-static void tlb_set(struct gd_engine *eng, unsigned phys, unsigned virt, unsigned flags)
-{
- if (phys >= 256)
- return;
- eng->tlb_virt[phys] = (uint8_t)(virt & 0xff);
- eng->tlb_flags[phys] = (uint8_t)(flags & 7);
-}
-
-static void tlb_itlb(struct gd_engine *eng, unsigned phys)
-{
- if (phys < 256 && !(eng->tlb_flags[phys] & 4))
- tlb_set(eng, phys, 0, 0);
-}
-
-static uint32_t tlb_ptlb(struct gd_engine *eng, unsigned phys)
-{
- if (phys >= 256)
- return 0;
- return (uint32_t)eng->tlb_flags[phys] << 24 | (uint32_t)eng->tlb_virt[phys] << 8;
-}
-
-static uint32_t tlb_vtlb(struct gd_engine *eng, unsigned virt)
-{
- unsigned page = (virt >> 8) & 0xff;
- unsigned flags = 0, phys = 0, count = 0;
- for (unsigned i = 0; i < 256; i++)
- if (eng->tlb_flags[i] && eng->tlb_virt[i] == page) {
- flags |= eng->tlb_flags[i];
- phys = i;
- count++;
- }
- uint32_t res = phys | (flags << 24);
- if (!count)
- res |= 0x80000000u;
- if (count > 1)
- res |= 0x40000000u;
- return res;
-}
-
-static uint32_t dmem_read(struct gd_engine *eng, uint32_t address, int sz)
-{
- uint32_t value = 0;
- int width = sz / 8, i;
- if (address >= GD_DEVMAP_BASE && address < GD_DEVMAP_BASE + GD_DEVMAP_SIZE) {
- uint32_t at = address - GD_DEVMAP_BASE;
- for (i = 0; i < width && at + i < GD_DEVMAP_SIZE; i++)
- value |= (uint32_t)eng->devmap[at + i] << (8 * i);
- return value;
- }
- address &= 0xffffu;
- address &= ~(uint32_t)(width - 1);
- for (i = 0; i < width; i++)
- value |= (uint32_t)eng->dmem[(address + i) & 0xffffu] << (8 * i);
- return value;
-}
-
-static void dmem_write(struct gd_engine *eng, uint32_t address, int sz, uint32_t value)
-{
- int width = sz / 8, i;
- if (eng->index == 0 && address == 0xff50)
- GD_DIAG( "diag: w-ff50 val=%#x sz=%d pc=%#x\n", value, sz, eng->pc);
- if (eng->index == 0 && address >= 0xff40 && address <= 0xffe0 && value == 0xd44)
- GD_DIAG( "diag: write-d44 [%#x] pc=%#x sized=%d sz=%d\n", address, eng->pc, 0, sz);
- if (eng->index == 0 && address >= 0xff40 && address <= 0xffe0 && value == 0x1fce)
- GD_DIAG( "diag: push1fce [%#x] pc=%#x\n", address, eng->pc);
- if (eng->index == 0 && address >= 0xffa0 && address <= 0xffc8)
- GD_DIAG( "diag: dmem-write [%#x]=%#x sz=%d pc=%#x\n", address, value, sz, eng->pc);
- if (eng->index == 0 && (address == 0x1650 || address == 0x1654 ||
- address == 0x1658 || address == 0x165c))
- GD_DIAG( "diag: dmem1650[%#x] = %#x pc=%#x\n", address, value, eng->pc);
- if (address >= GD_DEVMAP_BASE && address < GD_DEVMAP_BASE + GD_DEVMAP_SIZE) {
- uint32_t at = address - GD_DEVMAP_BASE;
- for (i = 0; i < width && at + i < GD_DEVMAP_SIZE; i++)
- eng->devmap[at + i] = value >> (8 * i);
- return;
- }
- address &= 0xffffu;
- address &= ~(uint32_t)(width - 1);
- for (i = 0; i < width; i++)
- eng->dmem[(address + i) & 0xffffu] = value >> (8 * i);
-}
-
-static uint32_t rotr32(uint32_t x, int n)
-{
- return (x >> n) | (x << (32 - n));
-}
-
-static void sha256_block(uint32_t h[8], const uint8_t block[64])
-{
- static const uint32_t k[64] = {
- 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5,
- 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
- 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3,
- 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
- 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc,
- 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
- 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7,
- 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
- 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13,
- 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
- 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3,
- 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
- 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5,
- 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
- 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208,
- 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2,
- };
- uint32_t w[64], a, b, c, d, e, f, g, hh;
- int i;
-
- for (i = 0; i < 16; i++)
- w[i] = (uint32_t)block[i * 4] << 24 |
- (uint32_t)block[i * 4 + 1] << 16 |
- (uint32_t)block[i * 4 + 2] << 8 |
- block[i * 4 + 3];
- for (i = 16; i < 64; i++)
- w[i] = w[i - 16] + (rotr32(w[i - 15], 7) ^
- rotr32(w[i - 15], 18) ^ (w[i - 15] >> 3)) +
- w[i - 7] + (rotr32(w[i - 2], 17) ^
- rotr32(w[i - 2], 19) ^ (w[i - 2] >> 10));
- a = h[0]; b = h[1]; c = h[2]; d = h[3];
- e = h[4]; f = h[5]; g = h[6]; hh = h[7];
- for (i = 0; i < 64; i++) {
- uint32_t t1 = hh + (rotr32(e, 6) ^ rotr32(e, 11) ^ rotr32(e, 25)) +
- ((e & f) ^ (~e & g)) + k[i] + w[i];
- uint32_t t2 = (rotr32(a, 2) ^ rotr32(a, 13) ^ rotr32(a, 22)) +
- ((a & b) ^ (a & c) ^ (b & c));
- hh = g; g = f; f = e; e = d + t1;
- d = c; c = b; b = a; a = t1 + t2;
- }
- h[0] += a; h[1] += b; h[2] += c; h[3] += d;
- h[4] += e; h[5] += f; h[6] += g; h[7] += hh;
-}
-
-static void sha256_init(uint32_t h[8])
-{
- h[0] = 0x6a09e667; h[1] = 0xbb67ae85;
- h[2] = 0x3c6ef372; h[3] = 0xa54ff53a;
- h[4] = 0x510e527f; h[5] = 0x9b05688c;
- h[6] = 0x1f83d9ab; h[7] = 0x5be0cd19;
-}
-
-static void sha256_update(struct gd_engine *eng, const uint8_t *data, size_t length)
-{
- size_t at = 0;
- while (at < length) {
- size_t n = 64 - eng->sha_buf_len;
- if (n > length - at)
- n = length - at;
- memcpy(eng->sha_buf + eng->sha_buf_len, data + at, n);
- eng->sha_buf_len += n;
- at += n;
- if (eng->sha_buf_len == 64) {
- sha256_block(eng->sha_h, eng->sha_buf);
- eng->sha_buf_len = 0;
- }
- }
-}
-
-static void sha256_final(struct gd_engine *eng, uint8_t out[32])
-{
- uint8_t tail[128] = { 0 };
- size_t rem = eng->sha_buf_len;
- uint64_t bits = eng->sha_total_bits;
-
- memcpy(tail, eng->sha_buf, rem);
- tail[rem] = 0x80;
- if (rem >= 56) {
- for (int i = 0; i < 8; i++)
- tail[127 - i] = (uint8_t)(bits >> (8 * i));
- sha256_block(eng->sha_h, tail);
- sha256_block(eng->sha_h, tail + 64);
- } else {
- for (int i = 0; i < 8; i++)
- tail[63 - i] = (uint8_t)(bits >> (8 * i));
- sha256_block(eng->sha_h, tail);
- }
- for (int i = 0; i < 8; i++) {
- out[i * 4] = (uint8_t)(eng->sha_h[i] >> 24);
- out[i * 4 + 1] = (uint8_t)(eng->sha_h[i] >> 16);
- out[i * 4 + 2] = (uint8_t)(eng->sha_h[i] >> 8);
- out[i * 4 + 3] = (uint8_t)eng->sha_h[i];
- }
-}
-
-static void sha_feed(struct gd_engine *eng)
-{
- size_t length = eng->sha_in_hi & 0xffffffu;
-
- if (!eng->sha_update_bits)
- return;
- if (eng->sha_update_bits && length > eng->sha_update_bits / 8)
- length = eng->sha_update_bits / 8;
- if (!eng->sha_started) {
- sha256_init(eng->sha_h);
- eng->sha_buf_len = 0;
- eng->sha_started = true;
- }
- if (eng->dev->config.verbose)
- gd_log(eng->dev, GD_LOG_DEBUG, "device: sha-feed pc=%#x addr=%#x hi=%#x len=%#x total=%#x\n",
- eng->pc, eng->sha_in_addr, eng->sha_in_hi, eng->sha_update_bits, eng->sha_msg_len);
- if (length && eng->sha_in_addr + length <= sizeof(eng->dmem)) {
- if (eng->dev->config.verbose)
- gd_log(eng->dev, GD_LOG_DEBUG, "device: sha-feed-data addr=%#x len=%#zx bytes=%02x%02x%02x%02x %02x%02x%02x%02x\n",
- eng->sha_in_addr, length, eng->dmem[eng->sha_in_addr],
- eng->dmem[eng->sha_in_addr+1], eng->dmem[eng->sha_in_addr+2], eng->dmem[eng->sha_in_addr+3],
- eng->dmem[eng->sha_in_addr+4], eng->dmem[eng->sha_in_addr+5], eng->dmem[eng->sha_in_addr+6], eng->dmem[eng->sha_in_addr+7]);
- sha256_update(eng, eng->dmem + eng->sha_in_addr, length);
- }
- else if (length) {
- gd_log(eng->dev, GD_LOG_ERROR,
- "device: sha update outside DMEM addr=%#x len=%#x\n",
- eng->sha_in_addr, eng->sha_update_bits);
- eng->core_fault = true;
- }
-}
-
-static void sha_publish(struct gd_engine *eng, bool final)
-{
- uint8_t digest[32];
- uint32_t hsave[8];
- uint32_t blen = eng->sha_buf_len;
- memcpy(hsave, eng->sha_h, sizeof(hsave));
- sha256_final(eng, digest);
- if (!final) {
- memcpy(eng->sha_h, hsave, sizeof(hsave));
- eng->sha_buf_len = blen;
- }
- if (eng->dev->config.verbose) {
- gd_log(eng->dev, GD_LOG_DEBUG,
- "device: sha-final pc=%#x total=%#llx stream=%u words=%08x %08x %08x %08x %08x %08x %08x %08x\n",
- eng->pc, (unsigned long long)eng->sha_total_bits, eng->sha_stream,
- eng->sha_h[0], eng->sha_h[1], eng->sha_h[2], eng->sha_h[3],
- eng->sha_h[4], eng->sha_h[5], eng->sha_h[6], eng->sha_h[7]);
- }
- for (int i = 0; i < 8; i++)
- eng->sha_result[i] = (uint32_t)digest[i * 4] << 24 |
- (uint32_t)digest[i * 4 + 1] << 16 |
- (uint32_t)digest[i * 4 + 2] << 8 |
- (uint32_t)digest[i * 4 + 3];
- eng->sha_status = 0;
- eng->sha_mutex = 0;
- if (final) {
- eng->sha_started = false;
- eng->sha_buf_len = 0;
- }
-}
-
-static void sha_run(struct gd_engine *eng)
-{
- uint32_t op = eng->sha_op;
-
- if ((op & 0x40u) && !(op & 0x01u)) {
- if (!eng->sha_started) {
- sha256_init(eng->sha_h);
- eng->sha_buf_len = 0;
- eng->sha_total_bits = eng->sha_msg_len;
- }
- sha_publish(eng, true);
- } else if (op & 0x01u) {
- if (!eng->sha_started) {
- sha256_init(eng->sha_h);
- eng->sha_buf_len = 0;
- }
- {
- uint64_t chunk_bits = (uint64_t)(eng->sha_in_hi & 0xffffffu) * 8u;
- sha_publish(eng, eng->sha_update_bits <= chunk_bits);
- }
- }
- eng->sha_op = op & ~(0x20u | 0x40u | 0x01u);
-}
-
-static uint32_t se_read(struct gd_engine *eng, uint32_t address)
-{
- return gd_trng_read(&eng->trng, address);
-}
-
-static void pka_modexp(struct gd_engine *eng, uint32_t mod_addr, uint32_t exp_addr,
- uint32_t base_addr, uint32_t out_addr);
-
-static void se_write(struct gd_engine *eng, uint32_t address, uint32_t value)
-{
- if (address >= sizeof(eng->se_regs))
- return;
- eng->se_regs[address / 4] = value;
- if (address == 0x4000 && (value & 0x80000000u)) {
- if (eng->se_regs[0x4004 / 4] == 0x14)
- pka_modexp(eng, 0x5000, 0x5400, 0x4400, 0x4400);
- eng->se_regs[0x4008 / 4] = 0;
- eng->se_regs[0x4020 / 4] = 0x40000000u;
- }
-}
-
-
-#define PKA_LIMBS 96
-
-static int pka_cmp(const uint32_t *a, const uint32_t *b)
-{
- for (int i = PKA_LIMBS - 1; i >= 0; i--) {
- if (a[i] != b[i])
- return a[i] > b[i] ? 1 : -1;
- }
- return 0;
-}
-
-static void pka_mulmod(const uint32_t a[PKA_LIMBS], const uint32_t b[PKA_LIMBS],
- const uint32_t mod[PKA_LIMBS], uint32_t out[PKA_LIMBS])
-{
- uint32_t prod[PKA_LIMBS * 2] = { 0 };
- uint32_t rem[PKA_LIMBS + 1] = { 0 };
-
- for (int i = 0; i < PKA_LIMBS; i++) {
- uint64_t carry = 0;
- for (int j = 0; j < PKA_LIMBS; j++) {
- uint64_t cur = (uint64_t)a[i] * b[j] + prod[i + j] + carry;
- prod[i + j] = (uint32_t)cur;
- carry = cur >> 32;
- }
- int k = i + PKA_LIMBS;
- while (carry && k < PKA_LIMBS * 2) {
- uint64_t cur = (uint64_t)prod[k] + carry;
- prod[k] = (uint32_t)cur;
- carry = cur >> 32;
- k++;
- }
- }
-
- for (int bit = PKA_LIMBS * 64 - 1; bit >= 0; bit--) {
- uint32_t carry = 0;
- for (int i = 0; i <= PKA_LIMBS; i++) {
- uint64_t v = ((uint64_t)rem[i] << 1) | carry;
- rem[i] = (uint32_t)v;
- carry = (uint32_t)(v >> 32);
- }
- rem[0] |= (prod[bit >> 5] >> (bit & 31)) & 1u;
- if (rem[PKA_LIMBS] || pka_cmp(rem, mod) >= 0) {
- uint32_t borrow = 0;
- for (int i = 0; i < PKA_LIMBS; i++) {
- uint64_t sub = (uint64_t)mod[i] + borrow;
- uint32_t old = rem[i];
- rem[i] = old - (uint32_t)sub;
- borrow = (uint64_t)old < sub;
- }
- rem[PKA_LIMBS] -= borrow;
- }
- }
- memcpy(out, rem, PKA_LIMBS * sizeof(uint32_t));
-}
-
-static void pka_modexp(struct gd_engine *eng, uint32_t mod_addr, uint32_t exp_addr,
- uint32_t base_addr, uint32_t out_addr)
-{
- if (eng->dev->config.verbose)
- gd_log(eng->dev, GD_LOG_DEBUG,
- "device: pka-modexp pc=%#x mod=%#x exp=%#x base=%#x out=%#x\n",
- eng->pc, mod_addr, exp_addr, base_addr, out_addr);
- uint32_t mod[PKA_LIMBS], exp[PKA_LIMBS], base[PKA_LIMBS], result[PKA_LIMBS] = { 0 };
- int high = -1;
-
- for (int i = 0; i < PKA_LIMBS; i++) {
- mod[i] = eng->se_regs[(mod_addr / 4) + i];
- exp[i] = eng->se_regs[(exp_addr / 4) + i];
- base[i] = eng->se_regs[(base_addr / 4) + i];
- }
- for (int i = PKA_LIMBS - 1; i >= 0 && high < 0; i--)
- for (int bit = 31; bit >= 0; bit--)
- if (exp[i] & (1u << bit)) {
- high = i * 32 + bit;
- break;
- }
- result[0] = 1;
- for (int bit = high; bit >= 0; bit--) {
- pka_mulmod(result, result, mod, result);
- if (exp[bit >> 5] & (1u << (bit & 31)))
- pka_mulmod(result, base, mod, result);
- }
- for (int i = 0; i < PKA_LIMBS; i++)
- eng->se_regs[(out_addr / 4) + i] = result[i];
- if (eng->dev->config.verbose)
- gd_log(eng->dev, GD_LOG_DEBUG, "device: pka-result pc=%#x first=%08x %08x %08x %08x\n",
- eng->pc, result[0], result[1], result[2], result[3]);
-}
-
-static bool plm_mask_register(uint32_t address)
-{
- switch (address) {
- case 0x823804: case 0x1fa7c4: case 0x1fa7cc: case 0x1fa7c8:
- case 0x9a0148: case 0x88ff4: case 0x88ab4: case 0x88ff8:
- case 0x8200fc: case 0x8200d8: case 0xc840: case 0xc848:
- return true;
- default:
- return false;
- }
-}
-
-static uint32_t plm_mask_for(uint32_t address)
-{
- switch (address) {
- case 0x823818: case 0x82381c: case 0x823820:
- return 0x823804;
- /* NV_PFB_PRI_MMU_WPR2_ADDR_LO/HI__PRIV_LEVEL_MASK */
- case 0x1fa824: case 0x1fa828:
- return 0x1fa7cc;
- /* WPR1 and WPR_ALLOW are also gated by WPR_CFG_PLM. */
- case 0x1fa814: case 0x1fa818: case 0x1fa81c: case 0x1fa820:
- return 0x1fa7cc;
- /* NV_PFB_PRI_MMU_LOCK_ADDR_LO/HI__PRIV_LEVEL_MASK */
- case 0x1fa82c: case 0x1fa830:
- return 0x1fa7c8;
- /* NV_PFB_PRI_MMU_LOCAL_MEMORY_RANGE__PRIV_LEVEL_MASK */
- case 0x100ce0:
- return 0x1fa7c4;
- default:
- return 0;
- }
-}
-
-static void plm_latch_write(struct gd_device *dev, uint32_t address, uint32_t value)
-{
- if (address == 0x823804) {
- dev->plm_feat_latched = value;
- if (value == 0xffffffffu)
- dev->target_write_seen = true;
- if ((value & 0x11u) == 0x11u)
- dev->plm_opened_seen = true;
- } else if (address == 0x1fa7cc) {
- dev->plm_wpr_cfg_latched = value;
- if ((value & 0xfffff0ffu) == 0xfffff0ffu)
- dev->target_write_seen = true;
- if (value & 0x10u)
- dev->plm_opened_seen = true;
- }
-}
-
-static bool plm_allowed(struct gd_device *dev, uint32_t address, int privilege, bool write)
-{
- uint32_t mask_address, mask;
- int bit;
-
- if (plm_mask_register(address))
- return !write || privilege >= 3;
- mask_address = plm_mask_for(address);
- if (!mask_address)
- return true;
- mask = __atomic_load_n(&dev->bar[mask_address / 4], __ATOMIC_RELAXED);
- /* SOURCE_ENABLE[31:12] must name at least one source. The individual
- * source bits are not documented in the available GA102 headers. */
- if (!(mask >> 12))
- return false;
- bit = privilege + (write ? 4 : 0);
- return (mask >> bit) & 1;
-}
-
-/* Writable mask from the generated GA102 fixture. Registers with a
- * partial mask (e.g. SS1's low nibble) must not store the reserved bits. */
-static uint32_t bar_write_mask(uint32_t address)
-{
- size_t i;
- for (i = 0; i < sizeof(gd_bar0_defaults) / sizeof(gd_bar0_defaults[0]); i++)
- if (gd_bar0_defaults[i].addr == address)
- return gd_bar0_defaults[i].mask;
- return 0xffffffffu;
-}
-
-static uint32_t bar0_raw_read(struct gd_device *dev, uint32_t address)
-{
- uint32_t value;
- if (address >= PROM_OFFSET && address < PROM_OFFSET + PROM_SIZE) {
- const uint8_t *p = dev->prom + (address - PROM_OFFSET);
- return (uint32_t)p[0] | (uint32_t)p[1] << 8 |
- (uint32_t)p[2] << 16 | (uint32_t)p[3] << 24;
- }
- pthread_mutex_lock(&dev->bar_lock);
- value = (address + 4 <= GD_BAR_SIZE && dev->bar_valid[address / 4]) ?
- dev->bar[address / 4] : 0;
- pthread_mutex_unlock(&dev->bar_lock);
- if (!(address + 4 <= GD_BAR_SIZE) || !dev->bar_valid[address / 4])
- gd_log(dev, GD_LOG_DEBUG, "device: unmodelled secure-bus read %#x -> 0\n", address);
- return value;
-}
-
-static void bar0_raw_write(struct gd_device *dev, uint32_t address, uint32_t value)
-{
- value &= bar_write_mask(address);
- pthread_mutex_lock(&dev->bar_lock);
- if (address + 4 <= GD_BAR_SIZE) {
- dev->bar[address / 4] = value;
- dev->bar_valid[address / 4] = 1;
- }
- pthread_mutex_unlock(&dev->bar_lock);
-}
-
-static void csb_init(struct gd_device *dev)
-{
- unsigned g;
- for (g = 0; g < 3; g++) {
- dev->csb.plm[g] = g == 2 ? 4u : 0u;
- /* Hardware-assigned 8-bit token; deliberately not 1. */
- dev->csb.id[g] = (uint8_t)(0x5a + g);
- memset(dev->csb.mutex[g], 0, sizeof(dev->csb.mutex[g]));
- dev->csb.released[g] = false;
- }
-}
-
-/* Clear every held SE common mutex. Called by the Booter's release
- * primitive when it sets BAR0 0x1180f8 bit 24. */
-static void csb_release_all(struct gd_device *dev)
-{
- unsigned g, j;
- for (g = 0; g < 3; g++) {
- for (j = 0; j < 16; j++)
- dev->csb.mutex[g][j] = 0;
- dev->csb.released[g] = true;
- }
-}
-
-static bool csb_mutex_slot(uint32_t addr, unsigned *group, unsigned *slot)
-{
- uint32_t rem;
- if (addr < 0x8e00 || addr >= 0x8f00)
- return false;
- rem = addr - 0x8e00;
- *group = rem / 0x50;
- rem %= 0x50;
- if (rem < 0x0c || rem >= 0x0c + 16 * 4 || ((rem - 0x0c) & 3))
- return false;
- *slot = (rem - 0x0c) / 4;
- return *group < 3;
-}
-
-static bool csb_allowed(struct gd_device *dev, unsigned g, int priv, bool write)
-{
- uint32_t plm = dev->csb.plm[g];
- if (priv >= 3)
- return true;
- return (plm >> ((priv & 3) + (write ? 4 : 0))) & 1;
-}
-
-static uint32_t csb_read(struct gd_device *dev, uint32_t addr, int priv)
-{
- unsigned g, slot;
- uint32_t rem;
- if (addr >= 0x8e00 && addr < 0x8f00) {
- rem = (addr - 0x8e00) % 0x50;
- g = (addr - 0x8e00) / 0x50;
- if (!csb_allowed(dev, g, priv, false)) {
- dev->denied_writes++;
- return 0xbadf0000u;
- }
- if (rem == 0x00)
- return dev->csb.plm[g];
- if (rem == 0x04)
- return dev->csb.id[g];
- if (rem == 0x08)
- return 0;
- if (csb_mutex_slot(addr, &g, &slot))
- return dev->csb.mutex[g][slot];
- return 0;
- }
- return bar0_raw_read(dev, addr);
-}
-
-static void csb_write(struct gd_device *dev, uint32_t addr, uint32_t value, int priv)
-{
- unsigned g, slot, j;
- uint32_t rem;
- if (addr >= 0x8e00 && addr < 0x8f00) {
- rem = (addr - 0x8e00) % 0x50;
- g = (addr - 0x8e00) / 0x50;
- if (!csb_allowed(dev, g, priv, true)) {
- dev->denied_writes++;
- dev->plm_violations++;
- return;
- }
- if (rem == 0x00) {
- dev->csb.plm[g] = value;
- return;
- }
- if (rem == 0x04)
- return; /* read-only ID */
- if (rem == 0x08) {
- /* ID_RELEASE: returning a token releases it and any
- * slot still carrying it. */
- if ((value & 0xffu) == dev->csb.id[g]) {
- for (j = 0; j < 16; j++)
- if (dev->csb.mutex[g][j] == (value & 0xffu))
- dev->csb.mutex[g][j] = 0;
- dev->csb.released[g] = true;
- }
- return;
- }
- if (csb_mutex_slot(addr, &g, &slot)) {
- dev->csb.mutex[g][slot] = value & 0xffu;
- return;
- }
- return;
- }
- bar0_raw_write(dev, addr, value);
-}
-
-static uint8_t *fb_window(struct gd_device *dev, uint64_t addr, size_t size)
-{
- if (!dev->fb_base || addr < dev->fb_base || addr - dev->fb_base > dev->fb_size ||
- size > dev->fb_size - (addr - dev->fb_base))
- return NULL;
- return dev->fb + (addr - dev->fb_base);
-}
-
-static uint32_t dio_read(struct gd_device *dev, struct gd_engine *eng, uint32_t addr, int priv)
-{
- addr &= 0xffffu;
- if (addr >= 0x8e00 && addr < 0x8f00)
- return csb_read(dev, addr, priv);
- return se_read(eng, addr);
-}
-
-static void dio_write(struct gd_device *dev, struct gd_engine *eng, uint32_t addr, uint32_t value, int priv)
-{
- addr &= 0xffffu;
- if (addr >= 0x8e00 && addr < 0x8f00)
- csb_write(dev, addr, value, priv);
- else
- se_write(eng, addr, value);
-}
-
-/* Falcon per-register privilege masks (NV_PFALCON_FALCON_*__PRIV_LEVEL_MASK).
- * Offsets are identical for every Falcon engine; the mask register itself is
- * L3-write-only and its low bits gate the protected register. */
-static uint32_t engine_plm_off(uint32_t off)
-{
- switch (off) {
- case 0x3c0: return 0x3c4; /* ENGINE -> RESET_PRIV_LEVEL_MASK */
- case 0x100: case 0x130: return 0x288; /* CPUCTL/ALIAS */
- case 0x104: return 0x194; /* BOOTVEC */
- case 0x10c: return 0x28c; /* DMACTL */
- case 0x110: case 0x114: case 0x118: case 0x11c: case 0x128:
- return 0x190; /* DMATRF* */
- case 0x180: case 0x184: case 0x188: return 0x18c; /* PMB IMEM */
- case 0x1c0: case 0x1c4: case 0x1c8: case 0x1cc: return 0x2b0; /* PMB DMEM */
- default: return 0;
- }
-}
-
-static bool engine_plm_allowed(struct gd_engine *eng, uint32_t off, int priv, bool write)
-{
- uint32_t mask_off = engine_plm_off(off);
- uint32_t mask;
-
- if (!mask_off)
- return true;
- mask = eng->io[mask_off / 4];
- if (off == mask_off) {
- /* A PLM register cannot be opened by the level it gates. */
- if (write)
- return priv >= 3;
- return (mask >> (priv & 3)) & 1;
- }
- if (!(mask >> 12))
- return false;
- return (mask >> ((priv & 3) + (write ? 4 : 0))) & 1;
-}
-
-static uint32_t engine_read_locked(struct gd_device *dev, struct gd_engine *eng, uint32_t off, int priv);
-static bool engine_write_locked(struct gd_device *dev, struct gd_engine *eng, uint32_t off, uint32_t value, int priv);
-
-/* The firmware reaches the same Falcon registers through the secure-bus
- * (CSEC CSB) view, whose offsets differ from the host PFALCON view. Translate
- * the CSB aliases onto the one engine state so firmware writes and host reads
- * observe the same registers. dev_sec_csb.h carries the offsets; the PFALCON
- * view is defined in dev_falcon_v4.h. */
-static uint32_t csb_alias(uint32_t offset)
-{
- switch (offset) {
- case 0x1000: return OFF_MAILBOX0;
- case 0x1100: return OFF_MAILBOX1;
- case 0x2100: return 0x084; /* FALCON_RM */
- case 0x3d00: return OFF_HWCFG2;
- case 0x4000: return OFF_CPUCTL;
- case 0x4100: return OFF_BOOTVEC;
- case 0x4300: return OFF_DMACTL;
- case 0x4200: return OFF_HWCFG;
- case 0x4400: return OFF_DMABASE;
- case 0x4500: return OFF_DMAMOFFS;
- case 0x4600: return OFF_DMACMD;
- case 0x4700: return OFF_DMAFBOFFS;
- case 0x4a00: return OFF_DMABASE1;
- case 0xf000: return OFF_ENGINE;
- case 0xf100: return OFF_RESET_PLM;
- default: return offset;
- }
-}
-
-static uint32_t local_io(struct gd_device *dev, struct gd_engine *eng, uint32_t offset, uint32_t value, int write)
-{
- uint32_t result;
- uint32_t alias = csb_alias(offset & 0xffffffu);
- if (dev->config.verbose)
- gd_log(dev, GD_LOG_DEBUG, "device: io %s pc=%#x off=%#x\n",
- write ? "w" : "r", eng->pc, offset);
- pthread_mutex_lock(&eng->lock);
- if (write) {
- engine_write_locked(dev, eng, alias & (GD_IO_SIZE - 1), value, 3);
- result = value;
- } else {
- result = engine_read_locked(dev, eng, alias & (GD_IO_SIZE - 1), 3);
- }
- pthread_mutex_unlock(&eng->lock);
- return result;
-}
-
-/* Keep the last 64 executed PCs per engine so a halt or fault can be
- * reconstructed without a per-instruction trace. The ring is written from
- * the executing thread only. */
-static void engine_pc_history(struct gd_device *dev, struct gd_engine *eng,
- uint32_t reason, uint32_t pc)
-{
- if (eng->index == 1 && reason == 1) {
- uint32_t o;
- GD_DIAG( "diag: fwsec-final dmem:");
- for (o = 0; o < 0x20; o += 4) GD_DIAG( " %08x", *(uint32_t *)(eng->dmem+o));
- GD_DIAG( "\n hdr2:");
- for (o = 0x4e0; o < 0x520; o += 4) GD_DIAG( " %08x", *(uint32_t *)(eng->dmem+o));
- GD_DIAG( "\n digest:");
- for (o = 0x35f0; o < 0x3610; o += 4) GD_DIAG( " %08x", *(uint32_t *)(eng->dmem+o));
- GD_DIAG( "\n mac:");
- for (o = 0x540; o < 0x560; o += 4) GD_DIAG( " %08x", *(uint32_t *)(eng->dmem+o));
- GD_DIAG( "\n out:");
- for (o = 0x8df0; o < 0x8e10; o += 4) GD_DIAG( " %08x", *(uint32_t *)(eng->dmem+o));
- GD_DIAG( "\n seed:");
- for (o = 0xd710; o < 0xd720; o += 4) GD_DIAG( " %08x", *(uint32_t *)(eng->imem+o));
- GD_DIAG( " fifo in=%u out=%u seen=%d/%d kdf:", eng->crypto_stream_in_count,
- eng->crypto_stream_out_count, eng->fwsec_kdf_seen, eng->fwsec_digest_seen);
- for (o = 0; o < 16; o += 4) GD_DIAG( " %08x", *(uint32_t *)(eng->fwsec_kdf_out+o));
- GD_DIAG( " expect:");
- for (o = 0; o < 32; o += 4) GD_DIAG( " %08x", *(uint32_t *)(eng->fwsec_expect+o));
- GD_DIAG( " digest:");
- for (o = 0; o < 16; o += 4) GD_DIAG( " %08x", *(uint32_t *)(eng->fwsec_digest+o));
- GD_DIAG( "\n");
- }
- if (eng->index == 0 && reason == 3) {
- uint32_t i;
- GD_DIAG( "diag: retring:");
- for (i = 0; i < 32; i++) {
- uint32_t at = (eng->ret_at - 32 + i) & 63;
- (void)at;
- GD_DIAG( " [%#x->%#x sp=%#x]", eng->ret_pc[at], eng->ret_val[at], eng->ret_sp[at]);
- }
- GD_DIAG( "\n");
- GD_DIAG( "diag: brring:");
- for (i = 0; i < 32; i++) {
- uint32_t at = (eng->br_at - 32 + i) & 63;
- (void)at;
- GD_DIAG( " [%#x->%#x sp=%#x]", eng->br_pc[at], eng->br_tgt[at], eng->br_sp[at]);
- }
- GD_DIAG( "\n");
- }
- uint32_t n = eng->pc_ring_at < 64 ? eng->pc_ring_at : 64;
- uint32_t first = eng->pc_ring_at - n;
- uint32_t i;
-
- eng->halt_pc = pc;
- eng->halt_reason = reason;
- trace_add(dev, GD_TRACE_PC, eng->index + 1, 0x10000u | reason, pc, pc);
- for (i = 0; i < n; i++)
- trace_add(dev, GD_TRACE_PC, eng->index + 1, i,
- eng->pc_ring[(first + i) & 63], 0);
-}
-
-static int engine_execute(struct gd_device *dev, struct gd_engine *eng, unsigned budget)
-{
- unsigned step;
- uint32_t pc = eng->bootvec & 0xffffu;
-
- for (step = 0; step < budget; step++) {
- const struct gd_code_rec *rec;
- uint32_t d, a, b, result = 0, m, target, address;
- int sz, overflow = 0, carry = 0, taken = 0;
- int32_t index;
-
- eng->pc_ring[eng->pc_ring_at++ & 63] = pc;
- if (eng->index == 0 && (pc == 0x1fd4 || pc == 0x1fd7 || pc == 0x1fdb)) {
- uint32_t i;
- GD_DIAG( "diag: at1fd pc=%#x prev:", pc);
- for (i = 1; i <= 10; i++)
- GD_DIAG( " %#x", eng->pc_ring[(eng->pc_ring_at - 1 - i) & 63]);
- GD_DIAG( " sp=%#x depth=%u r15=%#x\n", eng->sp, eng->call_depth, eng->r[15]);
- }
- if (eng->index == 1 && (pc == 0x988 || pc == 0xa69 || pc == 0xa6c || pc == 0xa72)) {
- GD_DIAG( "diag: seq pc=%#x r10=%#x r11=%#x r14=%#x r9=%#x trap=%d pend=%u in=%u out=%u\n",
- pc, eng->r[10], eng->r[11], eng->r[14], eng->r[9],
- eng->xfer_trap, eng->seq_pending,
- eng->crypto_stream_in_count, eng->crypto_stream_out_count);
- if (pc == 0xa6c) {
- uint32_t off = eng->r[14] & 0xffff;
- (void)off;
- GD_DIAG( "diag: seq imem@%#x:", off);
- for (uint32_t k = 0; k < 16; k += 4) GD_DIAG( " %08x", *(uint32_t *)(eng->imem+off+k));
- GD_DIAG( "\n");
- }
- }
- if (eng->index == 0 && pc == 0x576b) {
- uint32_t i;
- GD_DIAG( "diag: at576b prev:");
- for (i = 1; i <= 8; i++)
- GD_DIAG( " %#x", eng->pc_ring[(eng->pc_ring_at - 1 - i) & 63]);
- GD_DIAG( " sp=%#x depth=%u\n", eng->sp, eng->call_depth);
- }
- if (eng->index == 0 && pc == 0x1fa3) {
- uint32_t i;
- GD_DIAG( "diag: at1fa3 prev:");
- for (i = 1; i <= 10; i++)
- GD_DIAG( " %#x", eng->pc_ring[(eng->pc_ring_at - 1 - i) & 63]);
- GD_DIAG( " sp=%#x depth=%u\n", eng->sp, eng->call_depth);
- }
- if (eng->index == 0 && (pc == 0x330f || pc == 0x576b || pc == 0x57b2))
- GD_DIAG( "diag: callframe pc=%#x sp=%#x w0=%#x w4=%#x w8=%#x depth=%u\n",
- pc, eng->sp, *(uint32_t *)(eng->dmem+((eng->sp)&0xffff)),
- *(uint32_t *)(eng->dmem+((eng->sp+4)&0xffff)),
- *(uint32_t *)(eng->dmem+((eng->sp+8)&0xffff)), eng->call_depth);
- if (eng->index == 0 && (pc == 0x15cd || pc == 0x193b || pc == 0x1940))
- GD_DIAG( "diag: retpath pc=%#x sp=%#x w0=%#x w4=%#x depth=%u\n",
- pc, eng->sp, *(uint32_t *)(eng->dmem+((eng->sp)&0xffff)),
- *(uint32_t *)(eng->dmem+((eng->sp+4)&0xffff)), eng->call_depth);
- if (eng->index == 0 && (pc == 0x29d3 || pc == 0x28b1 || pc == 0x28b8 || pc == 0x28aa)) {
- uint32_t i;
- GD_DIAG( "diag: errsite %#x r0=%#x r4=%#x r1=%#x r14=%#x r15=%#x r12=%#x r13=%#x stack:",
- pc, eng->r[0], eng->r[4], eng->r[1], eng->r[14], eng->r[15], eng->r[12], eng->r[13]);
- for (i = 0; i < eng->call_depth && i < 12; i++)
- GD_DIAG( " %#x", eng->call_pc[i]);
- GD_DIAG( " last:");
- for (i = 0; i < 64; i++)
- GD_DIAG( " %#x", eng->pc_ring[(eng->pc_ring_at - 64 + i) & 63]);
- GD_DIAG( "\n wpr2lo=%08x wpr2hi=%08x wprplm=%08x mmucfg=%08x g03=%08x g6a=%08x g6b=%08x\n",
- dev->bar[0x1fa824/4], dev->bar[0x1fa828/4], dev->bar[0x1fa7c4/4],
- dev->bar[0x1fa7c8/4], dev->bar[0x11821c/4], dev->bar[0x118244/4],
- dev->bar[0x118248/4]);
- GD_DIAG( " d400=%08x %08x d500=%08x %08x d504=%08x d1650=%08x %08x\n",
- *(uint32_t *)(eng->dmem+0x400), *(uint32_t *)(eng->dmem+0x404),
- *(uint32_t *)(eng->dmem+0x500), *(uint32_t *)(eng->dmem+0x504),
- *(uint32_t *)(eng->dmem+0x508),
- *(uint32_t *)(eng->dmem+0x1650), *(uint32_t *)(eng->dmem+0x1658));
- }
- if (eng->index == 0 && (pc == 0x18f9 || pc == 0x1900 || pc == 0x1907 ||
- pc == 0x190e || pc == 0x1915 || pc == 0x191c)) {
- uint32_t b4 = eng->r[4];
- (void)b4;
- GD_DIAG( "diag: errsite %#x r4=%#x d34=%#x d30=%#x d3c=%#x d38=%#x d44=%#x d40=%#x stack:",
- pc, b4,
- (b4+0x34 < 0x10000) ? *(uint32_t *)(eng->dmem+b4+0x34) : 0,
- (b4+0x30 < 0x10000) ? *(uint32_t *)(eng->dmem+b4+0x30) : 0,
- (b4+0x3c < 0x10000) ? *(uint32_t *)(eng->dmem+b4+0x3c) : 0,
- (b4+0x38 < 0x10000) ? *(uint32_t *)(eng->dmem+b4+0x38) : 0,
- (b4+0x44 < 0x10000) ? *(uint32_t *)(eng->dmem+b4+0x44) : 0,
- (b4+0x40 < 0x10000) ? *(uint32_t *)(eng->dmem+b4+0x40) : 0);
- for (uint32_t di = 0; di < eng->call_depth && di < 12; di++)
- GD_DIAG( " %#x", eng->call_pc[di]);
- GD_DIAG( "\n");
- }
- if (!eng->code_index || pc + 1 > 0x10000) {
- dev->fault_pc = pc;
- device_error(dev, "code fetch outside decoded table", pc, 0);
- eng->core_fault = true;
- engine_pc_history(dev, eng, 3, pc);
- return -EFAULT;
- }
- index = eng->code_index[pc];
- if (index < 0) {
- uint32_t opcode = (uint32_t)eng->imem[pc] | (uint32_t)eng->imem[(pc + 1) & 0xffff] << 8;
- dev->fault_pc = pc;
- dev->fault_opcode = opcode;
- device_error(dev, "no decoded instruction", pc, opcode);
- eng->core_fault = true;
- engine_pc_history(dev, eng, 3, pc);
- return -EINVAL;
- }
- rec = &eng->code[index];
- if (dev->config.verbose && pc == 0x8fc) {
- gd_log(dev, GD_LOG_DEBUG, "device: frts head %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x\n",
- eng->dmem[0], eng->dmem[1], eng->dmem[2], eng->dmem[3], eng->dmem[4],
- eng->dmem[5], eng->dmem[6], eng->dmem[7], eng->dmem[8], eng->dmem[9]);
- }
- if (dev->config.verbose && (pc == 0x1382 || pc == 0x13a5 || pc == 0x13c9 || pc == 0x22b || pc == 0x235 || pc == 0x23f || pc == 0x241 || pc == 0x271)) {
- gd_log(dev, GD_LOG_DEBUG, "device: status pc=%#x r10=%#x r0=%#x\n", pc, eng->r[10], eng->r[0]);
- }
- if (dev->config.verbose && (pc == 0x1c74 || pc == 0x1ed4)) {
- uint32_t v[8];
- for (int i = 0; i < 8; i++)
- memcpy(&v[i], eng->dmem + 0x3000 + i * 4, 4);
- gd_log(dev, GD_LOG_DEBUG,
- "device: page-state pc=%#x r10=%#x r11=%#x r12=%#x r13=%#x d3000=%08x d3004=%08x d3010=%08x d3014=%08x d301c=%08x d3020=%08x d3024=%08x\n",
- pc, eng->r[10], eng->r[11], eng->r[12], eng->r[13],
- v[0], v[1], v[4], v[5], v[7], *(uint32_t *)(eng->dmem+0x3020), *(uint32_t *)(eng->dmem+0x3024));
- for (unsigned di = 0; di < dev->dma_count; di++)
- if (dev->dma[di].live)
- gd_log(dev, GD_LOG_DEBUG, "device: dma-span %u addr=%#llx size=%#zx\n",
- di, (unsigned long long)dev->dma[di].address, dev->dma[di].size);
- }
- if (dev->config.verbose && (pc == 0xa8dd || pc == 0xa95b || pc == 0xa9d2 ||
- pc == 0xac3b || pc == 0xa6d1)) {
- gd_log(dev, GD_LOG_DEBUG,
- "device: sha-call pc=%#x r0=%#x r1=%#x r2=%#x r3=%#x r10=%#x r11=%#x r12=%#x r13=%#x r14=%#x in=%#x len=%#x\n",
- pc, eng->r[0], eng->r[1], eng->r[2], eng->r[3], eng->r[10], eng->r[11],
- eng->r[12], eng->r[13], eng->r[14], eng->sha_in_addr, eng->sha_msg_len);
- }
- if (dev->config.verbose && pc == 0xd0a1) {
- uint32_t a = 0, b = 0;
- if (0x8e04 + 8 < sizeof(eng->dmem)) {
- memcpy(&a, eng->dmem + 0x8e04, 4);
- memcpy(&b, eng->dmem + 0x8e08, 4);
- }
- gd_log(dev, GD_LOG_DEBUG, "device: stage-buffers d8e04=%08x d8e08=%08x d3600=%08x\n",
- a, b, *(uint32_t *)(eng->dmem + 0x3600));
- }
- if (dev->config.verbose && (pc == 0xcfec || pc == 0xd003 || pc == 0xd08a)) {
- uint32_t desc = eng->r[pc == 0xcfec ? 10 : 6];
- uint32_t off = 0, len = 0;
- if (desc + 12 < sizeof(eng->dmem)) {
- memcpy(&off, eng->dmem + desc + 4, 4);
- memcpy(&len, eng->dmem + desc + 8, 4);
- }
- gd_log(dev, GD_LOG_DEBUG,
- "device: cert-desc pc=%#x r10=%#x r11=%#x r6=%#x desc=%#x off=%#x len=%#x\n",
- pc, eng->r[10], eng->r[11], eng->r[6], desc, off, len);
- }
- if (dev->config.verbose && (pc == 0xd08a || pc == 0xd0a1 || pc == 0x1b3c ||
- pc == 0xa215 || pc == 0xd445 || pc == 0xd49a || pc == 0xd4f6 ||
- pc == 0xd50f || pc == 0xd51a)) {
- uint32_t h = 0;
- if (eng->r[10] + 4 <= sizeof(eng->dmem))
- memcpy(&h, eng->dmem + eng->r[10], 4);
- gd_log(dev, GD_LOG_DEBUG,
- "device: stage pc=%#x r0=%#x r1=%#x r2=%#x r3=%#x r4=%#x r5=%#x r10=%#x r11=%#x r12=%#x hash=%08x\n",
- pc, eng->r[0], eng->r[1], eng->r[2], eng->r[3], eng->r[4], eng->r[5],
- eng->r[10], eng->r[11], eng->r[12], h);
- }
- if (dev->config.verbose && (pc == 0xa09c || pc == 0xa0a0 || pc == 0xa0a4 || pc == 0xe33)) {
- unsigned i;
- gd_log(dev, GD_LOG_DEBUG, "device: sig-check pc=%#x r10=%#x r11=%#x r12=%#x\n",
- pc, eng->r[10], eng->r[11], eng->r[12]);
- for (i = 0; i < 32 && i < eng->r[12] && eng->r[10] + i < sizeof(eng->dmem); i += 4) {
- uint32_t av = 0, bv = 0;
- memcpy(&av, eng->dmem + eng->r[10] + i, 4);
- if (eng->r[11] + i < sizeof(eng->dmem))
- memcpy(&bv, eng->dmem + eng->r[11] + i, 4);
- gd_log(dev, GD_LOG_DEBUG, "device: sig-data %02x %08x %08x\n", i, av, bv);
- }
- }
- if (dev->config.verbose && (pc == 0xb6ef || pc == 0xb75c || pc == 0xa9b2 ||
- pc == 0xaa3b || pc == 0xaa4c)) {
- gd_log(dev, GD_LOG_DEBUG, "device: master-check pc=%#x r1=%#x r2=%#x d0=%#x d1=%#x d2=%#x d3=%#x\n",
- pc, eng->r[1], eng->r[2],
- eng->r[1] + 0 < sizeof(eng->dmem) ? *(uint32_t *)(eng->dmem + eng->r[1]) : 0,
- eng->r[1] + 4 < sizeof(eng->dmem) ? *(uint32_t *)(eng->dmem + eng->r[1] + 4) : 0,
- eng->r[1] + 8 < sizeof(eng->dmem) ? *(uint32_t *)(eng->dmem + eng->r[1] + 8) : 0,
- eng->r[1] + 12 < sizeof(eng->dmem) ? *(uint32_t *)(eng->dmem + eng->r[1] + 12) : 0);
- }
- if (dev->config.verbose && (pc == 0xd15d || pc == 0xd168 || pc == 0xd16c)) {
- unsigned off;
- gd_log(dev, GD_LOG_DEBUG, "device: cert-check pc=%#x r0=%#x r2=%#x r10=%#x r11=%#x r12=%#x\n",
- pc, eng->r[0], eng->r[2], eng->r[10], eng->r[11], eng->r[12]);
- for (off = 0; off < 0x20; off += 4) {
- uint32_t a = eng->r[0] + off, b = eng->r[2] + 0x14 + off;
- gd_log(dev, GD_LOG_DEBUG, "device: cert hash %02x %08x %08x\n", off,
- a < sizeof(eng->dmem) ? *(uint32_t *)(eng->dmem + a) : 0,
- b < sizeof(eng->dmem) ? *(uint32_t *)(eng->dmem + b) : 0);
- }
- }
- if (dev->config.verbose && (pc == 0x2e6 || pc == 0x2ea || pc == 0x2f4 ||
- pc == 0x74c || pc == 0x2e2)) {
- gd_log(dev, GD_LOG_DEBUG,
- "device: fwsec-check pc=%#x r0=%#x r10=%#x r11=%#x r15=%#x\n",
- pc, eng->r[0], eng->r[10], eng->r[11], eng->r[15]);
- }
- if (dev->config.verbose && pc == 0x959) {
- int i;
- gd_log(dev, GD_LOG_DEBUG, "device: sigcmp r1=%#x r10=%#x r11=%#x r12=%#x\n",
- eng->r[1], eng->r[10], eng->r[11], eng->r[12]);
- gd_log(dev, GD_LOG_DEBUG, "device: sig expected:");
- for (i = 0; i < 16; i++)
- gd_log(dev, GD_LOG_DEBUG, " %02x", eng->dmem[0x540 + i]);
- gd_log(dev, GD_LOG_DEBUG, "\n");
- gd_log(dev, GD_LOG_DEBUG, "device: sig computed:");
- for (i = 0; i < 16; i++)
- gd_log(dev, GD_LOG_DEBUG, " %02x", eng->dmem[0x8df0 + i]);
- gd_log(dev, GD_LOG_DEBUG, "\n");
- }
- sz = rec->sz ? rec->sz : 32;
- m = mask_of(sz);
- d = rec->d; a = rec->a; b = rec->b;
- target = rec->target;
- switch (rec->op) {
- case 1: /* mov */
- if (rec->sized) set_r(eng, d, eng->r[a], sz);
- else eng->r[d] = a;
- break;
- case 2: /* sethi */
- eng->r[d] = (eng->r[d] & 0xffffu) | (a << 16);
- break;
- case 3: case 4: case 5: case 6: { /* add adc sub sbb */
- uint32_t x = eng->r[a] & m;
- uint32_t y = (rec->bimm ? b : eng->r[b]) & m;
- uint32_t cin = (rec->op == 4 || rec->op == 6) ? (eng->flags >> 8) & 1 : 0;
- uint64_t wide;
- if (rec->op == 5 || rec->op == 6) {
- wide = (uint64_t)x - y - cin;
- carry = (uint64_t)x < (uint64_t)y + cin;
- result = (uint32_t)wide & m;
- overflow = (((x ^ y) & (x ^ result)) >> (sz - 1)) & 1;
- } else {
- wide = (uint64_t)x + y + cin;
- carry = (wide >> sz) & 1;
- result = (uint32_t)wide & m;
- overflow = (((x ^ result) & (y ^ result)) >> (sz - 1)) & 1;
- }
- set_r(eng, d, result, sz);
- set_flags(eng, result, sz, overflow, carry);
- break;
- }
- case 7: case 8: case 9: /* cmpu cmps cmp */
- a = eng->r[a] & m;
- b = (rec->bimm ? b : eng->r[b]) & m;
- result = (a - b) & m;
- if (rec->op == 8) {
- int64_t sa = (int64_t)a - ((uint64_t)(a >> (sz - 1)) << sz);
- int64_t sb = (int64_t)b - ((uint64_t)(b >> (sz - 1)) << sz);
- carry = sa < sb;
- } else carry = a < b;
- if (rec->op == 9) {
- overflow = (((a ^ b) & (a ^ result)) >> (sz - 1)) & 1;
- set_flags(eng, result, sz, overflow, carry);
- } else eng->flags = (eng->flags & ~0x900u) | (uint32_t)carry << 8 | (result == 0) << 11;
- break;
- case 10: case 11: case 12: case 13: case 14: { /* shifts */
- uint32_t x = eng->r[a] & m;
- uint32_t n = (rec->bimm ? b : eng->r[b]) & (sz - 1);
- if (rec->op == 10 || rec->op == 13) {
- result = (x << n) & m;
- if (rec->op == 13 && n)
- result |= ((eng->flags >> 8) & 1) << (n - 1);
- carry = n ? (x >> (sz - n)) & 1 : 0;
- } else {
- result = x >> n;
- if (rec->op == 14 && n)
- result |= ((eng->flags >> 8) & 1) << (sz - n);
- if (rec->op == 12 && (x >> (sz - 1)))
- result |= (uint32_t)((uint64_t)m << (sz - n)) & m;
- carry = n ? (x >> (n - 1)) & 1 : 0;
- }
- set_r(eng, d, result, sz);
- set_flags(eng, result, sz, 0, carry);
- break;
- }
- case 15: case 16: case 17: case 18: case 19:
- a = eng->r[a] & m;
- if (rec->op == 18) { set_r(eng, d, 0, sz); break; }
- if (rec->op == 15) result = ~a & m;
- else if (rec->op == 16) result = -a & m;
- else if (rec->op == 17) result = ((a >> (sz / 2)) | (a << (sz / 2))) & m;
- else result = a;
- if (rec->op != 19) set_r(eng, d, result, sz);
- set_flags(eng, result, sz, rec->op == 16 && result == (1u << (sz - 1)), -1);
- break;
- case 20: case 21: { /* ld st */
- uint32_t base = rec->base == 16 ? eng->sp : eng->r[rec->base];
- address = base + (rec->idx >= 0 ? eng->r[rec->idx] * (uint32_t)rec->scale : (uint32_t)rec->off);
- if (rec->space) {
- if (rec->op == 20) {
- result = local_io(dev, eng, address, 0, 0);
- set_r(eng, d, result, sz);
- } else {
- local_io(dev, eng, address, eng->r[a], 1);
- }
- } else if (rec->op == 20) {
- result = dmem_read(eng, address, sz);
- set_r(eng, d, result, sz);
- } else {
- dmem_write(eng, address, sz, eng->r[a]);
- }
- break;
- }
- case 22: case 23:
- a = eng->r[a]; b = rec->bimm ? b : eng->r[b];
- eng->r[d] = rec->op == 22 ? (a & 0xffffu) * (b & 0xffffu) :
- (uint32_t)((int32_t)(int16_t)a * (int32_t)(int16_t)b);
- break;
- case 24:
- a = eng->r[a]; b = (rec->bimm ? b : eng->r[b]) & 31;
- result = a & ((1u << b) - 1);
- if ((a >> b) & 1) result |= 0xffffffffu << b;
- eng->r[d] = result;
- eng->flags = (eng->flags & ~0xc00u) | (result >> 31) << 10 | (result == 0) << 11;
- break;
- case 25: case 26: {
- uint32_t low, sm1, bf, fill;
- a = eng->r[a]; b = rec->bimm ? b : eng->r[b];
- low = b & 31; sm1 = (b >> 5) & 31;
- bf = (a >> low) & ((2u << sm1) - 1);
- fill = rec->op == 26 ? (a >> ((low + sm1) & 31)) & 1 : 0;
- if (fill) bf |= (uint32_t)(-(2u << sm1));
- eng->r[d] = bf;
- eng->flags = (eng->flags & ~0xc00u) | fill << 10 | (bf == 0) << 11;
- break;
- }
- case 27: {
- uint32_t low, size, fm;
- b = rec->bimm ? b : eng->r[b];
- low = b & 31; size = ((b >> 5) & 31) + 1;
- if (low + size <= 32) {
- fm = ((1u << size) - 1) << low;
- eng->r[d] = (eng->r[d] & ~fm) | ((eng->r[a] << low) & fm);
- }
- break;
- }
- case 28: case 29: case 30:
- a = eng->r[a]; b = rec->bimm ? b : eng->r[b];
- result = rec->op == 28 ? a & b : rec->op == 29 ? a | b : a ^ b;
- eng->r[d] = result;
- set_flags(eng, result, 32, 0, 0);
- break;
- case 31: case 32:
- b = (rec->bimm ? b : eng->r[b]) & 31;
- result = ((rec->op == 31 ? eng->r[a] : eng->flags) >> b) & 1;
- eng->r[d] = result;
- eng->flags = (eng->flags & ~0xc00u) | (result == 0) << 11;
- break;
- case 33: case 34: case 35:
- b = 1u << ((rec->bimm ? b : eng->r[b]) & 31);
- if (rec->op == 33) eng->r[d] |= b;
- else if (rec->op == 34) eng->r[d] &= ~b;
- else eng->r[d] ^= b;
- break;
- case 36: case 37:
- a = eng->r[a]; b = rec->bimm ? b : eng->r[b];
- eng->r[d] = rec->op == 36 ? (b ? a / b : 0xffffffffu) : (b ? a % b : a);
- break;
- case 38: case 39: case 40:
- if (rec->op == 39)
- taken = ((eng->r[a] & m) == (uint32_t)b) != !!d;
- else if (rec->op == 38)
- taken = condition(eng->flags, d);
- else
- taken = 1;
- if (taken) {
- if (rec->op == 40) {
- eng->br_pc[eng->br_at & 63] = pc;
- eng->br_tgt[eng->br_at & 63] = rec->bimm ? (uint32_t)a : eng->r[a];
- eng->br_sp[eng->br_at & 63] = eng->sp;
- eng->br_at++;
- if (eng->index == 0 && (eng->br_tgt[(eng->br_at - 1) & 63] == 0x576b ||
- eng->br_tgt[(eng->br_at - 1) & 63] == 0x1fa3))
- GD_DIAG( "diag: enter-jmp pc=%#x tgt=%#x sp=%#x\n",
- pc, eng->br_tgt[(eng->br_at - 1) & 63], eng->sp);
- pc = (rec->bimm ? (uint32_t)a : eng->r[a]) & 0xffff;
- }
- else
- pc = target & 0xffff;
- } else {
- pc = (pc + rec->len) & 0xffff;
- }
- break;
- case 41:
- eng->sp = (eng->sp + (rec->bimm ? a : eng->r[a])) & ~3u;
- break;
- case 42: break;
- case 43: /* movsr */
- if (d == 4) eng->sp = eng->r[a] & ~3u;
- else if (d == 5) eng->pc = rec->pc;
- else eng->sr[d] = eng->r[a];
- break;
- case 44: /* movrs */
- if (a == 4) eng->r[d] = eng->sp;
- else if (a == 5) eng->r[d] = rec->pc;
- else if (a == 8) eng->r[d] = eng->flags;
- else eng->r[d] = eng->sr[a];
- break;
- case 45: case 65: case 66: /* call lbra lcall */
- eng->br_pc[eng->br_at & 63] = pc;
- eng->br_tgt[eng->br_at & 63] = (rec->bimm ? (uint32_t)a : eng->r[a]);
- eng->br_sp[eng->br_at & 63] = eng->sp;
- eng->br_at++;
- if (eng->index == 0 && eng->br_tgt[(eng->br_at - 1) & 63] >= 0x5760 &&
- eng->br_tgt[(eng->br_at - 1) & 63] <= 0x57b5)
- GD_DIAG( "diag: call-into-57 pc=%#x tgt=%#x op=%d sp=%#x\n",
- pc, eng->br_tgt[(eng->br_at - 1) & 63], rec->op, eng->sp);
- if (eng->index == 0 && (eng->br_tgt[(eng->br_at - 1) & 63] == 0x576b ||
- eng->br_tgt[(eng->br_at - 1) & 63] == 0x1fa3))
- GD_DIAG( "diag: enter pc=%#x tgt=%#x op=%d sp=%#x bimm=%d a=%d\n",
- pc, eng->br_tgt[(eng->br_at - 1) & 63], rec->op, eng->sp,
- rec->bimm, rec->a);
- if (rec->op == 45 || rec->op == 66) {
- uint32_t ret = (pc + rec->len) & 0xffff;
-
- eng->sp -= 4;
- dmem_write(eng, eng->sp, 32, ret);
- if (eng->call_depth < 64)
- eng->call_pc[eng->call_depth] = pc;
- eng->call_depth++;
- if (eng->sh_at < 128) {
- eng->sh_ret[eng->sh_at] = ret;
- eng->sh_sp[eng->sh_at] = eng->sp;
- eng->sh_at++;
- }
- }
- pc = (rec->op == 45 ? (rec->bimm ? a : eng->r[a]) : target) & 0xffff;
- break;
- case 46: /* ret */
- {
- uint32_t rv = dmem_read(eng, eng->sp, 32) & 0xffff;
- if (eng->sh_at && eng->index == 0) {
- eng->sh_at--;
- if ((rv != eng->sh_ret[eng->sh_at] || eng->sp != eng->sh_sp[eng->sh_at]) &&
- eng->sh_bad++ < 20)
- GD_DIAG( "diag: SHADOW ret pc=%#x rv=%#x exp=%#x sp=%#x expsp=%#x\n",
- pc, rv, eng->sh_ret[eng->sh_at], eng->sp, eng->sh_sp[eng->sh_at]);
- }
- eng->ret_pc[eng->ret_at & 63] = pc;
- eng->ret_val[eng->ret_at & 63] = rv;
- eng->ret_sp[eng->ret_at & 63] = eng->sp;
- eng->ret_at++;
- if (eng->index == 0 && pc == 0x1fdb) {
- uint32_t di;
- GD_DIAG( "diag: ret1fdb rv=%#x sp=%#x depth=%u calls:", rv, eng->sp, eng->call_depth);
- for (di = 0; di < eng->call_depth && di < 12; di++)
- GD_DIAG( " %#x", eng->call_pc[di]);
- GD_DIAG( "\n");
- }
- pc = rv;
- }
- eng->sp += 4;
- if (eng->call_depth) eng->call_depth--;
- break;
- case 47: case 48: /* push pop */
- if (rec->op == 47) {
- eng->sp -= 4;
- dmem_write(eng, eng->sp, 32, eng->r[a]);
- } else {
- eng->r[d] = dmem_read(eng, eng->sp, 32);
- eng->sp += 4;
- }
- break;
- case 49: case 50: case 51: case 52: case 53: {
- int i, count = a + 1;
- if (rec->op == 49) {
- for (i = 0; i < count; i++) {
- eng->sp -= 4;
- dmem_write(eng, eng->sp, 32, eng->r[i]);
- }
- } else {
- for (i = a; i >= 0; i--) {
- eng->r[i] = dmem_read(eng, eng->sp, 32);
- eng->sp += 4;
- }
- if (rec->op == 52 || rec->op == 53)
- eng->sp = (eng->sp + (uint32_t)b) & ~3u;
- if (rec->op == 51 || rec->op == 53) {
- uint32_t rv = dmem_read(eng, eng->sp, 32) & 0xffff;
- if (eng->sh_at && eng->index == 0) {
- eng->sh_at--;
- if ((rv != eng->sh_ret[eng->sh_at] || eng->sp != eng->sh_sp[eng->sh_at]) &&
- eng->sh_bad++ < 20)
- GD_DIAG( "diag: SHADOW mpopret pc=%#x rv=%#x exp=%#x sp=%#x expsp=%#x\n",
- pc, rv, eng->sh_ret[eng->sh_at], eng->sp, eng->sh_sp[eng->sh_at]);
- }
- eng->ret_pc[eng->ret_at & 63] = pc;
- eng->ret_val[eng->ret_at & 63] = rv;
- eng->ret_sp[eng->ret_at & 63] = eng->sp;
- eng->ret_at++;
- if (eng->index == 0 && pc == 0x1fdb) {
- uint32_t di;
- GD_DIAG( "diag: ret1fdb mpop op=%d rv=%#x sp=%#x depth=%u calls:", rec->op, rv, eng->sp, eng->call_depth);
- for (di = 0; di < eng->call_depth && di < 12; di++)
- GD_DIAG( " %#x", eng->call_pc[di]);
- GD_DIAG( "\n");
- }
- pc = rv;
- eng->sp += 4;
- if (eng->call_depth) eng->call_depth--;
- }
- }
- break;
- }
- case 60: case 61: /* iord/iords */
- address = eng->r[rec->base] + (rec->idx >= 0 ? eng->r[rec->idx] * (uint32_t)rec->scale : (uint32_t)rec->off);
- result = local_io(dev, eng, address, 0, 0);
- set_r(eng, d, result, 32);
- break;
- case 62: /* iowr/iowrs */
- address = eng->r[rec->base] + (rec->idx >= 0 ? eng->r[rec->idx] * (uint32_t)rec->scale : (uint32_t)rec->off);
- if (eng->index == 0 && (address & 0xffffu) == 0x1000u) {
- uint32_t di, dn = eng->call_depth < 12 ? eng->call_depth : 12;
- GD_DIAG( "diag: status %#x pc=%#x stack:", eng->r[a], pc);
- for (di = 0; di < dn; di++)
- GD_DIAG( " %#x", eng->call_pc[di]);
- GD_DIAG( "\n");
- }
- local_io(dev, eng, address, eng->r[a], 1);
- break;
- case 63: /* exit */
- gd_log(dev, GD_LOG_DEBUG, "device: engine %d exit at %#x after %llu instructions\n",
- eng->index, pc, (unsigned long long)eng->instructions);
- eng->pc = pc;
- eng->halted = true;
- eng->cpuctl = CPUCTL_HALTED;
- trace_add(dev, GD_TRACE_HALT, eng->index + 1, eng->base, pc, pc);
- engine_pc_history(dev, eng, 1, pc);
- __atomic_add_fetch(&eng->instructions, 1, __ATOMIC_RELAXED);
- return 0;
- case 64: /* trap */
- dev->fault_pc = pc;
- device_error(dev, "software trap", pc, a);
- eng->core_fault = true;
- engine_pc_history(dev, eng, 4, pc);
- return -EINVAL;
- case 67: /* sleep */
- if ((eng->flags >> a) & 1u) {
- eng->pc = pc;
- eng->halted = true;
- eng->cpuctl = CPUCTL_HALTED;
- trace_add(dev, GD_TRACE_HALT, eng->index + 1, eng->base, pc, pc);
- engine_pc_history(dev, eng, 2, pc);
- __atomic_add_fetch(&eng->instructions, 1, __ATOMIC_RELAXED);
- return 0;
- }
- break;
- case 70: case 71: case 72: case 73: case 74: case 75:
- crypto_apply(eng, rec->op, d, a, b);
- break;
- case 76: case 77: /* xdst / xdld */
- {
- uint32_t bv = eng->r[b];
- uint32_t av = eng->r[a];
- if (eng->xfer_trap) {
- uint32_t local = bv & 0xffffu;
- uint32_t size = 4u << ((bv >> 16) & 7);
-
- if (local + size > sizeof(eng->imem)) {
- device_error(dev, "SCP transfer overrun", local, size);
- eng->core_fault = true;
- return -EINVAL;
- }
- if (rec->op == 76) {
- uint32_t off;
- for (off = 0; off + 16 <= size && eng->crypto_stream_in_count < 64; off += 16) {
- memcpy(eng->crypto_stream_in[eng->crypto_stream_in_count],
- eng->imem + local + off, 16);
- eng->crypto_stream_in_count++;
- }
- eng->crypto_stream_out_count = 0;
- while (eng->seq_pending && eng->crypto_stream_in_count) {
- crypto_exec_script(eng, 0, 1);
- eng->seq_pending--;
- }
- } else {
- uint32_t off;
- uint32_t at = 0;
- for (off = 0; off + 16 <= size && at < eng->crypto_stream_out_count; off += 16) {
- if (eng->index == 0 && ((local + off) & 0xffff) == 0xff50)
- GD_DIAG( "diag: trap-xdld-ff50 pc=%#x block=%08x\n", pc,
- *(uint32_t *)eng->crypto_stream_out[at]);
- memcpy(eng->dmem + ((local + off) & 0xffff),
- eng->crypto_stream_out[at], 16);
- at++;
- }
- eng->crypto_stream_out_count = 0;
- }
- if (eng->xfer_count != UINT32_MAX && eng->xfer_count)
- eng->xfer_count--;
- break;
- }
- if (eng->xfer_count) {
- uint32_t creg = (bv >> 16) & 7;
- uint32_t local = bv & 0xffff;
- gd_log(dev, GD_LOG_DEBUG, "device: crypto %s pc=%#x b=%#x count=%u\n",
- rec->op == 76 ? "xdst" : "xdld", pc, b, eng->xfer_count);
- if (rec->op == 76) {
- for (int i = 0; i < 16; i++)
- eng->c[creg][i] = eng->dmem[(local + i) & 0xffff];
- eng->c_hidden[creg] = false;
- } else {
- for (int i = 0; i < 16; i++)
- eng->dmem[(local + i) & 0xffff] = eng->c[creg][i];
- if (eng->index == 1 && local == 0x8df0) {
- memcpy(eng->fwsec_kdf_out, eng->c[creg], 16);
- memcpy(eng->fwsec_expect, eng->dmem + 0x540, 32);
- eng->fwsec_kdf_seen = true;
- }
- if (eng->index == 1 && local == 0x35f0) {
- memcpy(eng->fwsec_digest, eng->c[creg], 16);
- eng->fwsec_digest_seen = true;
- }
- }
- eng->xfer_count--;
- } else {
- uint64_t base = ((uint64_t)eng->sr[15] << 32) | eng->sr[7];
- uint64_t ext = (base << 8) + av;
- uint32_t port = (rec->op == 77) ? (eng->sr[11] >> 8) & 7 :
- (eng->sr[11] >> 12) & 7;
- uint32_t local = bv & 0xffff;
- uint32_t size = 4u << ((bv >> 16) & 7);
- uint32_t i;
- struct gd_dma_span *span = NULL;
- if (dev->config.verbose)
- gd_log(dev, GD_LOG_DEBUG,
- "device: xfer %s pc=%#x port=%u ext=%#llx local=%#x size=%#x\n",
- rec->op == 76 ? "xdst" : "xdld", pc, port,
- (unsigned long long)ext, local, size);
- if (local + size > sizeof(eng->dmem)) {
- device_error(dev, "XDMA DMEM overrun", local, size);
- eng->core_fault = true;
- return -EINVAL;
- }
- uint8_t *fb = fb_window(dev, ext, size);
- if (fb) {
- if (eng->index == 0 && local <= 0xff50 && local + size > 0xff50)
- GD_DIAG( "diag: ext-xdld-fb-ff50 pc=%#x size=%#x\n", pc, size);
- if (rec->op == 77)
- memcpy(eng->dmem + local, fb, size);
- else
- memcpy(fb, eng->dmem + local, size);
- } else {
- pthread_mutex_lock(&dev->dma_lock);
- span = dma_find(dev, ext, size);
- if (span) {
- if (eng->index == 0 && local <= 0xff50 && local + size > 0xff50)
- GD_DIAG( "diag: ext-xdld-span-ff50 pc=%#x size=%#x\n", pc, size);
- if (rec->op == 77)
- memcpy(eng->dmem + local, span->cpu + (ext - span->address), size);
- else
- memcpy(span->cpu + (ext - span->address), eng->dmem + local, size);
- }
- pthread_mutex_unlock(&dev->dma_lock);
- if (!span) {
- for (i = 0; i < size; i += 4) {
- if (rec->op == 77) {
- uint32_t v = bar0_raw_read(dev, (uint32_t)ext + i);
- if (eng->index == 0 && local + i <= 0xff50 && local + i + 4 > 0xff50)
- GD_DIAG( "diag: ext-xdld-bar-ff50 pc=%#x v=%#x\n", pc, v);
- memcpy(eng->dmem + local + i, &v, 4);
- } else {
- uint32_t v;
- memcpy(&v, eng->dmem + local + i, 4);
- bar0_raw_write(dev, (uint32_t)ext + i, v);
- }
- }
- }
- }
- }
- }
- break;
- case 78: case 79: case 80:
- break; /* xdwait/xdfence/xcwait */
- case 81: case 82: case 86: case 87: case 88:
- crypto_apply(eng, rec->op, d, a, b);
- break;
- case 83: /* vtlb */
- eng->r[d] = tlb_vtlb(eng, eng->r[a] & 0xffffffu);
- break;
- case 84: /* itlb */
- tlb_itlb(eng, eng->r[a] & 0xffffffu);
- break;
- case 85: /* ptlb */
- eng->r[d] = tlb_ptlb(eng, eng->r[a] & 0xffffffu);
- break;
- case 94: { /* iret */
- eng->pc = pc = dmem_read(eng, eng->sp, 32) & 0xffffu;
- eng->sp += 4;
- uint32_t is = (eng->flags >> 20) & 7u;
- eng->flags = (eng->flags & ~(7u << 16)) | (is << 16);
- break;
- }
- case 95: { /* xcld: code-load XDMA */
- uint32_t av = eng->r[a], bv = eng->r[b];
- uint64_t base = ((uint64_t)eng->sr[14] << 32) | eng->sr[6];
- uint64_t ext = (base << 8) + av;
- uint32_t local = bv & 0xffffu;
- if (local + 0x100u > sizeof(eng->imem)) {
- device_error(dev, "XCLD IMEM overrun", local, 0x100);
- eng->core_fault = true;
- return -EINVAL;
- }
- for (uint32_t i = 0; i < 0x100; i += 4) {
- uint8_t *fbw = fb_window(dev, ext + i, 4);
- if (fbw) memcpy(eng->imem + local + i, fbw, 4);
- else {
- struct gd_dma_span *sp = dma_find(dev, ext + i, 4);
- if (sp) memcpy(eng->imem + local + i, sp->cpu + (ext + i - sp->address), 4);
- else {
- uint32_t v = bar0_raw_read(dev, (uint32_t)(ext + i));
- memcpy(eng->imem + local + i, &v, 4);
- }
- }
- }
- tlb_set(eng, local >> 8, local >> 8, (eng->sr[10] >> 16 & 1) ? 4 : 1);
- break;
- }
- case 96: case 97: case 98: case 99: case 100: case 101:
- case 102: case 103: case 104: case 105: case 106: case 107:
- case 120: case 121: case 122: case 123: case 124: case 125:
- case 126: case 127: case 128: case 129: case 130: case 131:
- case 132: case 133: case 134: case 135: case 136: case 137:
- case 138: case 139: case 140: case 141: case 142:
- crypto_apply(eng, rec->op, d, a, b);
- break;
- case 89: case 90: case 91: {
- uint32_t bit = 1u << ((rec->bimm ? b : eng->r[b]) & 31);
- if (rec->op == 89) eng->flags |= bit;
- else if (rec->op == 90) eng->flags &= ~bit;
- else eng->flags ^= bit;
- break;
- }
- case 93: /* cxset: bit7 trap, bit5 shortcut, low 5 bits count */
- {
- uint32_t count = a & 0x1f;
-
- eng->xfer_trap = !!(a & 0x20); /* 0xa1 and 0x22 */
- eng->xfer_shortcut = !!(a & 0x80); /* 0xa1 */
- if (count == 0x1f)
- eng->xfer_count = UINT32_MAX;
- else if (!count)
- eng->xfer_count = 0;
- else
- eng->xfer_count = count; /* hardware counts transfers */
- }
- break;
- case 92: {
- uint32_t bit = (rec->bimm ? b : eng->r[b]) & 31;
- eng->flags = (eng->flags & ~(1u << bit)) | ((eng->r[a] & 1) << bit);
- break;
- }
- default:
- dev->fault_pc = pc;
- device_error(dev, "unimplemented opcode", pc, rec->op);
- eng->core_fault = true;
- return -EINVAL;
- }
- if (rec->op != 38 && rec->op != 39 && rec->op != 40 &&
- rec->op != 45 && rec->op != 46 && rec->op != 51 && rec->op != 53 &&
- rec->op != 65 && rec->op != 66)
- pc = (pc + rec->len) & 0xffff;
- eng->pc = pc;
- __atomic_add_fetch(&eng->instructions, 1, __ATOMIC_RELAXED);
- }
- dev->fault_pc = pc;
- device_error(dev, "Falcon instruction budget exhausted", pc, budget);
- eng->core_fault = true;
- engine_pc_history(dev, eng, 5, pc);
- return -ETIMEDOUT;
-}
-
-static void load_code_table(struct gd_engine *eng, const char *path)
-{
- FILE *file;
- uint32_t header[3];
- size_t i;
- struct gd_device *dev = eng->dev;
- if (!path)
- return;
- file = fopen(path, "rb");
- if (!file) {
- device_error(dev, "cannot open decoded table", 0, 0);
- return;
- }
- if (fread(header, sizeof(header), 1, file) != 1 || header[0] != 0x31434447u) {
- fclose(file);
- device_error(dev, "bad decoded table", 0, 0);
- return;
- }
- eng->code_count = header[1];
- eng->code = calloc(eng->code_count, sizeof(*eng->code));
- eng->code_index = malloc(0x10000 * sizeof(*eng->code_index));
- if (!eng->code || !eng->code_index) {
- fclose(file);
- return;
- }
- for (i = 0; i < 0x10000; i++)
- eng->code_index[i] = -1;
- for (i = 0; i < eng->code_count; i++) {
- if (fread(&eng->code[i], sizeof(eng->code[i]), 1, file) != 1) {
- eng->code_count = i;
- break;
- }
- if (eng->code[i].pc < 0x10000)
- eng->code_index[eng->code[i].pc] = (int32_t)i;
- }
- fclose(file);
-}
-
-static bool decrypt_pio_code(struct gd_device *dev, struct gd_engine *eng)
-{
- uint32_t lo = eng->pio_secure_lo, hi = eng->pio_secure_hi;
-
- if (hi <= lo)
- return true;
- if (!dev->config.key_set || (lo & 15) || ((hi - lo) & 15)) {
- device_error(dev, "secure PIO code range is not AES-block aligned", lo, hi - lo);
- return false;
- }
- if (gd_aes128_decrypt(dev->config.key, eng->imem + lo, hi - lo) != 0) {
- device_error(dev, "secure PIO code decrypt failed", lo, hi - lo);
- return false;
- }
- eng->pio_secure_lo = eng->pio_secure_hi = 0;
- return true;
-}
-
-static void engine_kick(struct gd_device *dev, struct gd_engine *eng)
-{
- {
- if (eng->index == 0) {
- struct gd_dma_span *sp = dma_find_size(dev, 0x100);
- if (sp) {
- uint32_t i;
- GD_DIAG( "diag: wprmeta:");
- for (i = 0; i < 200; i += 8)
- GD_DIAG( " %016llx", (unsigned long long)*(uint64_t *)(sp->cpu+i));
- GD_DIAG( "\n");
- }
- }
- if (eng->index == 0 && (dev->bar[0x11821c / 4] & (1u << 17))) {
- uint32_t lo = dev->bar[0x1fa824 / 4], hi = dev->bar[0x1fa828 / 4];
- uint32_t want;
-
- /* The debug FWSEC image publishes an inclusive 4 KiB FRTS
- * end; the 610 Booter decodes WPR2_HI with the 128 KiB
- * alignment term from booter_wpr2_tu10x.c. Convert the
- * encoding and record that it happened. */
- if (!gd_wpr2_fwsec_hi(lo, &want) && want != hi) {
- dev->bar[0x1fa828 / 4] = want;
- dev->wpr2_normalized = true;
- }
- }
- if (eng->index == 0) {
- unsigned o;
- GD_DIAG( "diag: scratch:");
- for (o = 0x1400; o <= 0x1440; o += 4)
- GD_DIAG( " %#x=%#x", o, dev->bar[o / 4]);
- GD_DIAG( "\n");
- }
-
- if (eng->index == 1)
- dev->fwsec_launch_reached = true;
- else
- dev->booter_launch_reached = true;
- if (!dev->config.key_set) {
- device_error(dev, "debug firmware key missing; execution refused", 0, 0);
- eng->core_fault = true;
- eng->started = true;
- eng->halted = true;
- eng->cpuctl = CPUCTL_HALTED;
- return;
- }
- /* The real FWSEC needs more than the old 1M-instruction smoke
- * budget after the SE PKA path; keep the run bounded. */
- if (!decrypt_pio_code(dev, eng) ||
- engine_execute(dev, eng, 100000000) != 0) {
- eng->core_fault = true;
- eng->halted = true;
- eng->cpuctl = CPUCTL_HALTED;
- return;
- }
- }
- eng->started = true;
- eng->halted = true;
- eng->cpuctl = CPUCTL_HALTED;
- __atomic_add_fetch(&dev->launches[eng->index], 1, __ATOMIC_RELAXED);
- __atomic_add_fetch(&eng->instructions, 1, __ATOMIC_RELAXED);
- trace_add(dev, GD_TRACE_HALT, eng->index + 1, eng->base, eng->reset_plm, 0);
-}
-
-/* Resolve a DMA address against framebuffer or coherent sysmem. The same
- * address can only belong to one; a miss is a modelling gap, never a no-op. */
-static uint8_t *dma_target(struct gd_device *dev, uint64_t address, size_t size,
- struct gd_dma_span **span_out)
-{
- struct gd_dma_span *span;
- uint8_t *fb = fb_window(dev, address, size);
-
- if (fb) {
- *span_out = NULL;
- return fb;
- }
- span = dma_find(dev, address, size);
- *span_out = span;
- if (!span)
- return NULL;
- if ((uint64_t)(address - span->address) + size > span->size)
- return NULL;
- return span->cpu + (address - span->address);
-}
-
-/* Falcon DMATRFCMD: FINAL/IDLE status, SEC (2), IMEM (4), WRITE (5),
- * SIZE (10:8, byte count 4<<n), CTXDMA (14:12). */
-static void dma_execute(struct gd_device *dev, struct gd_engine *eng, uint32_t cmd)
-{
- uint64_t address;
- size_t size;
- bool imem = !!(cmd & (1u << 4));
- bool store = !!(cmd & (1u << 5));
- uint32_t base = eng->dma_base, base1 = eng->dma_base1;
- uint32_t moffs = eng->dma_moffs, fboffs = eng->dma_fboffs;
- struct gd_dma_span *span = NULL;
- uint8_t decoded[512];
- uint8_t *target;
- size_t limit = imem ? sizeof(eng->imem) : sizeof(eng->dmem);
-
- size = 4u << ((cmd >> 8) & 7);
- if (size > sizeof(decoded)) {
- device_error(dev, "DMA size", moffs, cmd);
- return;
- }
- address = ((uint64_t)(base1 & 0x1ffu) << 32 | base) << 8;
- address += fboffs;
- if (size == 0x100 && moffs >= 0x4e00)
- GD_DIAG( "diag: dmabounce eng=%d src=%#llx local=%#x cmd=%#x wr=%d base=%#x base1=%#x fboffs=%#x pc=%#x\n",
- eng->index, (unsigned long long)address, moffs, cmd, store, base, base1, fboffs, eng->pc);
- if (size == 0x100 && moffs == 0x400)
- GD_DIAG( "diag: dma100 src=%#llx store=%d eng=%d pc=%#x\n",
- (unsigned long long)address, store, eng->index, eng->pc);
- if ((size_t)moffs + size > limit) {
- device_error(dev, imem ? "DMA IMEM overrun" : "DMA DMEM overrun", moffs, cmd);
- return;
- }
- pthread_mutex_lock(&dev->dma_lock);
- target = dma_target(dev, address, size, &span);
- if (!target) {
- pthread_mutex_unlock(&dev->dma_lock);
- device_error(dev, store ? "DMA destination unmapped" : "DMA source unmapped",
- (uint32_t)address, cmd);
- return;
- }
- if (store) {
- memcpy(target, (imem ? eng->imem : eng->dmem) + moffs, size);
- } else {
- memcpy(decoded, target, size);
- if (imem && (cmd & (1u << 2))) {
- if (!dev->config.key_set ||
- gd_aes128_decrypt(dev->config.key, decoded, size) != 0) {
- pthread_mutex_unlock(&dev->dma_lock);
- device_error(dev, "secure code decrypt failed", moffs, cmd);
- return;
- }
- }
- if (!imem && moffs <= 0xffe0 && moffs + size > 0xff40)
- GD_DIAG( "diag: dma-stack moffs=%#x size=%zu src=%#llx cmd=%#x pc=%#x\n",
- moffs, size, (unsigned long long)address, cmd, eng->pc);
- memcpy((imem ? eng->imem : eng->dmem) + moffs, decoded, size);
- if (imem) {
- eng->imem_tag = fboffs >> 8;
- tlb_set(eng, moffs >> 8, fboffs >> 8,
- (cmd & (1u << 2)) ? 4 : 1);
- }
- }
- pthread_mutex_unlock(&dev->dma_lock);
- dev->engine_writes++;
- trace_add(dev, GD_TRACE_DMA, eng->index + 1, (uint32_t)address, cmd, 0);
- trace_add(dev, GD_TRACE_HALT, eng->index + 1, eng->base, eng->reset_plm, 0);
-}
-
-static void *engine_thread(void *arg)
-{
- struct gd_engine *eng = arg;
- struct gd_device *dev = eng->dev;
-
- pthread_mutex_lock(&eng->lock);
- while (!eng->stop) {
- int action = 0;
- uint32_t cmd = 0;
-
- if (eng->qhead != eng->qtail) {
- cmd = eng->dma_q[eng->qhead++];
- eng->qhead %= 64;
- action = 1;
- } else if (eng->started && !eng->halted && !eng->faulted) {
- action = 2;
- }
- if (!action) {
- pthread_cond_wait(&eng->wake, &eng->lock);
- continue;
- }
- eng->busy = true;
- pthread_mutex_unlock(&eng->lock);
- if (action == 1)
- dma_execute(dev, eng, cmd);
- else
- engine_kick(dev, eng);
- pthread_mutex_lock(&eng->lock);
- if (action == 1)
- eng->dma_stat = 2;
- eng->busy = false;
- pthread_cond_broadcast(&eng->idle);
- }
- pthread_mutex_unlock(&eng->lock);
- return NULL;
-}
-
-static void engine_start_locked(struct gd_device *dev, struct gd_engine *eng)
-{
- eng->started = true;
- eng->halted = false;
- if (eng->thread_up) {
- eng->pending_kick = false;
- pthread_cond_signal(&eng->wake);
- return;
- }
- eng->pending_kick = true;
-}
-
-static void engine_reset_locked(struct gd_device *dev, struct gd_engine *eng, uint32_t value)
-{
- while (eng->busy)
- pthread_cond_wait(&eng->idle, &eng->lock);
- eng->pending_kick = false;
- eng->engine_reg = value;
- if (value & 1) {
- engine_reset_state(dev, eng);
- eng->engine_reg = value | 1;
- eng->scrub_pending = true;
- eng->scrub_done_us = dev->time_us + 2000;
- eng->hwcfg2 |= HWCFG2_SCRUB;
- gd_log(dev, GD_LOG_DEBUG, "device: engine %d reset asserted\n", eng->index);
- } else {
- eng->engine_reg = value & ~1u;
- settle_scrub(dev, eng);
- eng->hwcfg2 &= ~HWCFG2_SCRUB;
- gd_log(dev, GD_LOG_DEBUG, "device: engine %d reset released\n", eng->index);
- }
-}
-
-static uint32_t engine_read_locked(struct gd_device *dev, struct gd_engine *eng, uint32_t off, int priv)
-{
- settle_scrub(dev, eng);
- if (!engine_plm_allowed(eng, off, priv, false))
- return 0xbadf0000u;
- switch (off) {
- case OFF_MAILBOX0: return eng->mailbox[0];
- case OFF_MAILBOX1: return eng->mailbox[1];
- case OFF_HWCFG2: return eng->hwcfg2 | (eng->scrub_pending ? HWCFG2_SCRUB : 0);
- case OFF_CPUCTL:
- case OFF_CPUCTL_ALT:
- return eng->halted ? CPUCTL_HALTED : 0;
- case OFF_BOOTVEC: return eng->bootvec;
- case OFF_HWCFG: return eng->hwcfg;
- case OFF_DMACTL: return eng->dmactl;
- case OFF_DMABASE: return eng->dma_base;
- case OFF_DMABASE1: return eng->dma_base1;
- case OFF_DMAMOFFS: return eng->dma_moffs;
- case OFF_DMAFBOFFS: return eng->dma_fboffs;
- case OFF_DMACMD: return eng->dma_stat;
- case OFF_ENGINE: return eng->engine_reg;
- case OFF_RESET_PLM: return eng->reset_plm;
- case OFF_IMEMC: return eng->imemc;
- case 0x1100: return 0x40000000u; /* FWSEC boot-progress gate */
- case 0x9000: return 0x2u; /* crypto engine available */
- case 0x1000: return eng->io[0x1000 / 4]; /* NV_CSEC_FALCON_MAILBOX0 */
- case 0x4f00: return gd_falcon_engid();
- case 0x1070: return gd_pbus_emulation_rev(0);
- case 0x1074: return gd_pbus_emulation_rev(1);
- case 0x10200: return dev->config.key_set ? 0x100000u : 0; /* SCP_CTL_STAT.DEBUG_MODE */
- case 0x9100: return gd_csberrstat(eng->csb_enable, eng->csb_valid, eng->csb_pc);
- case 0x1c000: case 0x1c100: case 0x1c200:
- return gd_csec_read(&eng->csec, off);
- case 0x1c300: return 0x0063bb0bu;
- case 0x40300: return eng->sha_in_addr;
- case 0x40400: return eng->sha_in_hi;
- case 0x40700: return eng->sha_msg_len;
- case 0x41f00: return eng->sha_op;
- case 0x42000: return eng->sha_status;
- case 0x44200: return eng->sha_mutex;
- case 0x47200: return 2; /* SHA hash selection/version: SHA-256 */
- case 0xd000: case 0xd100: case 0xd200: case 0xd300:
- case 0xd400: case 0xd500: case 0xd600: case 0xd700:
- return gd_doc_read(&eng->doc, off, priv);
- case 0x344: return eng->io[0x344 / 4];
- case 0x348: return eng->io[0x348 / 4];
- case 0x350: return eng->response_ready;
- case 0x354: return eng->response;
- case 0x700: return eng->io[0x700 / 4];
- case 0x704: return eng->io[0x704 / 4];
- case 0x708: return eng->io[0x708 / 4];
- case OFF_DMEMC: return eng->dmemc;
- case 0x404: case 0x10100: return eng->io[0x404 / 4];
- case 0x008: return 0;
- case 0x00c: return 0;
- case 0x018: return 0;
- case 0x01c: return 0;
- case 0x04c: return eng->started && !eng->halted;
- default:
- if (off >= 0x40f00 && off < 0x40f00 + 8 * 0x100 &&
- ((off - 0x40f00) & 0xff) == 0)
- return eng->sha_result[(off - 0x40f00) / 0x100];
- if (off < GD_IO_SIZE) {
- if (eng->io_valid[off / 4])
- return eng->io[off / 4];
- gd_log(dev, GD_LOG_DEBUG, "device: unmodelled engine read %#x -> 0\n", eng->base + off);
- trace_add(dev, GD_TRACE_BAR_READ, eng->index + 1, eng->base + off, 0, 0);
- return 0;
- }
- record_unknown(dev, false, eng->base + off, 0, 0);
- return 0xffffffffu;
- }
-}
-
-static bool engine_write_locked(struct gd_device *dev, struct gd_engine *eng, uint32_t off, uint32_t value, int priv)
-{
- settle_scrub(dev, eng);
- if (!engine_plm_allowed(eng, off, priv, true)) {
- dev->denied_writes++;
- dev->plm_violations++;
- gd_log(dev, GD_LOG_DEBUG, "device: PLM denied engine write base=%#x off=%#x priv=%d\n",
- eng->base, off, priv);
- return false;
- }
- switch (off) {
- case OFF_MAILBOX0: eng->mailbox[0] = value; break;
- case OFF_MAILBOX1: eng->mailbox[1] = value; break;
- case OFF_HWCFG2: eng->hwcfg2 = value; break;
- case OFF_CPUCTL:
- case OFF_CPUCTL_ALT:
- eng->cpuctl = value;
- if (value & CPUCTL_START)
- engine_start_locked(dev, eng);
- break;
- case OFF_BOOTVEC: eng->bootvec = value; break;
- case OFF_DMACTL: eng->dmactl = value; break;
- case OFF_DMABASE:
- eng->dma_base = value;
- GD_DIAG( "diag: dma_base=%#x pc=%#x eng=%d\n", value, eng->pc, eng->index);
- break;
- case OFF_DMABASE1:
- eng->dma_base1 = value;
- GD_DIAG( "diag: dma_base1=%#x pc=%#x eng=%d\n", value, eng->pc, eng->index);
- break;
- case OFF_DMAMOFFS: eng->dma_moffs = value; break;
- case OFF_DMAFBOFFS: eng->dma_fboffs = value; break;
- case OFF_DMACMD:
- eng->dma_cmd = value;
- GD_DIAG( "diag: dmacmd=%#x pc=%#x eng=%d base=%#x fboffs=%#x\n",
- value, eng->pc, eng->index, eng->dma_base, eng->dma_fboffs);
- if (eng->thread_up) {
- eng->dma_stat = 1;
- eng->dma_q[eng->qtail++] = value;
- eng->qtail %= 64;
- pthread_cond_signal(&eng->wake);
- } else {
- pthread_mutex_unlock(&eng->lock);
- dma_execute(dev, eng, value);
- pthread_mutex_lock(&eng->lock);
- eng->dma_stat = 2;
- }
- break;
- case OFF_ENGINE: engine_reset_locked(dev, eng, value); break;
- case OFF_RESET_PLM: eng->reset_plm = value; break;
- case OFF_IMEMC:
- eng->imemc = value;
- eng->pio_imem = value & 0xffffu;
- break;
- case OFF_IMEMD:
- if (eng->pio_imem + 4 <= sizeof(eng->imem))
- memcpy(eng->imem + eng->pio_imem, &value, 4);
- else
- device_error(dev, "IMEM PIO overrun", eng->pio_imem, value);
- if (eng->imemc & (1u << 28)) {
- uint32_t at = eng->pio_imem;
- if (!eng->pio_secure_hi) {
- eng->pio_secure_lo = at;
- eng->pio_secure_hi = at + 4;
- } else {
- if (at < eng->pio_secure_lo)
- eng->pio_secure_lo = at;
- if (at + 4 > eng->pio_secure_hi)
- eng->pio_secure_hi = at + 4;
- }
- }
- if (eng->imemc & (1u << 24))
- eng->pio_imem += 4;
- break;
- case OFF_IMEMT: eng->imem_tag = value; break;
- case 0x1000: eng->io[0x1000 / 4] = value; break;
- case 0x9100:
- if (value & 0x40000000u)
- eng->csb_enable = true;
- if (value & 0x80000000u)
- eng->csb_valid = true;
- eng->csb_pc = value & 0xffffffu;
- break;
- case 0x344: {
- uint32_t address = value & 0xffffu;
- eng->io[0x344 / 4] = value;
- if (value & 0x10000u) {
- eng->response = (address >= 0x8e00 && address < 0x8f00) ?
- csb_read(dev, address, priv) : bar0_raw_read(dev, address);
- eng->response_ready = 1;
- } else {
- uint32_t data = eng->io[0x348 / 4];
- if (address >= 0x8e00 && address < 0x8f00)
- csb_write(dev, address, data, priv);
- else
- bar0_raw_write(dev, address, data);
- }
- break;
- }
- case 0x348: eng->io[0x348 / 4] = value; break;
- case 0x350:
- eng->io[0x350 / 4] = value;
- if (value & 0x100000u)
- eng->response_ready = 0;
- break;
- case 0x700: {
- eng->io[0x700 / 4] = value;
- if (value & 0x80000000u) {
- uint32_t address = eng->io[0x704 / 4];
- if ((value & 3u) == 1u) {
- eng->io[0x708 / 4] = bar0_raw_read(dev, address);
- eng->io[0x700 / 4] = 0x400u;
- } else {
- bar0_raw_write(dev, address, eng->io[0x708 / 4]);
- eng->io[0x700 / 4] = 0x400u;
- }
- }
- break;
- }
- case 0x704: eng->io[0x704 / 4] = value; break;
- case 0x708: eng->io[0x708 / 4] = value; break;
- case 0x1c000: case 0x1c100: case 0x1c200:
- gd_csec_write(&eng->csec, off, value);
- break;
- case 0x41f00:
- eng->sha_op = value;
- if (dev->config.verbose) gd_log(dev, GD_LOG_DEBUG, "device: sha-op pc=%#x value=%#x\n", eng->pc, value);
- sha_run(eng);
- break;
- case 0x42000: eng->sha_status = value; break;
- case 0x44000: eng->sha_mutex = value; break;
- case 0x40300: eng->sha_in_addr = value; break;
- case 0x40400: eng->sha_in_hi = value; break;
- case 0x40700:
- eng->sha_msg_len = value;
- eng->sha_total_bits = value;
- if (dev->config.verbose) gd_log(dev, GD_LOG_DEBUG, "device: sha-total pc=%#x value=%#x\n", eng->pc, value);
- break;
- case 0x40b00:
- eng->sha_update_bits = value;
- if (dev->config.verbose) gd_log(dev, GD_LOG_DEBUG, "device: sha-update pc=%#x value=%#x\n", eng->pc, value);
- sha_feed(eng);
- break;
- case 0xd000: case 0xd100: case 0xd200: case 0xd300:
- case 0xd400: case 0xd500: case 0xd600: case 0xd700:
- gd_doc_write(&eng->doc, off, value, priv);
- break;
- case 0x404: case 0x10100:
- eng->io[0x404 / 4] = value & ~0x100u;
- break;
- case OFF_DMEMC:
- eng->dmemc = value;
- eng->pio_dmem = value & 0xffffu;
- break;
- case OFF_DMEMD:
- if (eng->pio_dmem + 4 <= sizeof(eng->dmem))
- memcpy(eng->dmem + eng->pio_dmem, &value, 4);
- else
- device_error(dev, "DMEM PIO overrun", eng->pio_dmem, value);
- if (eng->dmemc & (1u << 24))
- eng->pio_dmem += 4;
- break;
- default:
- if (off < GD_IO_SIZE) {
- eng->io[off / 4] = value;
- eng->io_valid[off / 4] = 1;
- } else {
- record_unknown(dev, true, eng->base + off, value, 0);
- }
- break;
- }
- dev->engine_writes++;
- return true;
-}
-
-uint32_t gd_bar0_read(struct gd_device *dev, uint32_t address)
-{
- struct gd_engine *eng;
- uint32_t off, value;
-
- dev->host_reads++;
- dev->time_us = now_us();
- trace_add(dev, GD_TRACE_BAR_READ, 0, address, 0, 0);
- if (address >= PROM_OFFSET && address < PROM_OFFSET + PROM_SIZE)
- return 0xffffffffu;
- eng = engine_for(dev, address, &off);
- if (eng) {
- pthread_mutex_lock(&eng->lock);
- value = engine_read_locked(dev, eng, off, 0);
- pthread_mutex_unlock(&eng->lock);
- return value;
- }
- if (!plm_allowed(dev, address, 0, false))
- return 0;
- pthread_mutex_lock(&dev->bar_lock);
- bool valid = address + 4 <= GD_BAR_SIZE && dev->bar_valid[address / 4];
- value = valid ? dev->bar[address / 4] : 0xffffffffu;
- pthread_mutex_unlock(&dev->bar_lock);
- if (!valid)
- record_unknown(dev, false, address, 0, 0);
- return value;
-}
-
-void gd_bar0_write(struct gd_device *dev, uint32_t address, uint32_t value)
-{
- struct gd_engine *eng;
- uint32_t off;
-
- dev->host_writes++;
- dev->time_us = now_us();
- trace_add(dev, GD_TRACE_BAR_WRITE, 0, address, value, 0);
- if (address >= PROM_OFFSET && address < PROM_OFFSET + PROM_SIZE) {
- record_unknown(dev, true, address, value, 0);
- return;
- }
- eng = engine_for(dev, address, &off);
- if (eng) {
- pthread_mutex_lock(&eng->lock);
- bool allowed = engine_write_locked(dev, eng, off, value, 0);
- bool kick = eng->pending_kick;
- eng->pending_kick = false;
- pthread_mutex_unlock(&eng->lock);
- if (!allowed) {
- dev->denied_writes++;
- gd_log(dev, GD_LOG_DEBUG, "device: PLM denied host engine write %#x=%#x\n",
- address, value);
- return;
- }
- if (kick) {
- gd_log(dev, GD_LOG_DEBUG, "device: host kick engine %d\n", eng->index);
- engine_kick(dev, eng);
- }
- return;
- }
- if (!plm_allowed(dev, address, 0, true)) {
- uint32_t ma = plm_mask_register(address) ? address : plm_mask_for(address);
- dev->denied_writes++;
- if (ma && ((dev->bar[ma / 4] >> 9) & 1))
- dev->plm_violations++;
- gd_log(dev, GD_LOG_DEBUG, "device: PLM denied host write %#x=%#x\n", address, value);
- return;
- }
- value &= bar_write_mask(address);
- if (address == 0x82381c || address == 0x823820)
- dev->plm_opened_seen = true;
- pthread_mutex_lock(&dev->bar_lock);
- if (address + 4 <= GD_BAR_SIZE) {
- dev->bar[address / 4] = value;
- dev->bar_valid[address / 4] = 1;
- if (address == 0x1180f8 && (value & (1u << 24)))
- csb_release_all(dev);
- plm_latch_write(dev, address, value);
- if (address == 0x823804 && dev->feat_count < 8)
- dev->feats[dev->feat_count++] = value;
- } else {
- record_unknown(dev, true, address, value, 0);
- }
- pthread_mutex_unlock(&dev->bar_lock);
-}
-
-int gd_prom_copy(struct gd_device *dev, uint32_t offset, void *dest, size_t size)
-{
- if (offset < PROM_OFFSET || offset >= PROM_OFFSET + PROM_SIZE)
- return -EINVAL;
- offset -= PROM_OFFSET;
- if (offset + size > PROM_SIZE)
- size = PROM_SIZE - offset;
- memcpy(dest, dev->prom + offset, size);
- return (int)size;
-}
-
-int gd_pci_read(struct gd_device *dev, int space, int offset, int width, uint32_t *value)
-{
- uint32_t v = 0xffffffffu;
- (void)width;
-
- if (space == 0) {
- switch (offset) {
- case 0x00: v = 0x10deu | (uint32_t)dev->board->device << 16; break;
- case 0x04: v = dev->pci_command; break;
- case 0x08: v = 0x030000a1u; break;
- case 0x10: case 0x14: case 0x18: case 0x1c: case 0x20: case 0x24:
- v = 0xf0000000u + (uint32_t)(offset - 0x10) * 4; break;
- default:
- if (offset >= 0x40 && offset < 0x60)
- v = 0;
- else
- v = 0;
- break;
- }
- } else if (space == 1) {
- switch (offset) {
- case 4: v = 1u << 28; break;
- case 10: v = 0; break;
- default: v = 0; break;
- }
- } else {
- return -EINVAL;
- }
- *value = v;
- return 0;
-}
-
-int gd_pci_write(struct gd_device *dev, int space, int offset, int width, uint32_t value)
-{
- (void)width;
- if (space != 0)
- return -EINVAL;
- if (offset == 0x04) {
- dev->pci_command = value;
- dev->bar[0x200 / 4] = value;
- dev->bar_valid[0x200 / 4] = 1;
- }
- return 0;
-}
-
-int gd_dma_map(struct gd_device *dev, uint64_t address, void *cpu, size_t size)
-{
- uint32_t i;
- if (dev->dma_count >= GD_MAX_DMA)
- return -ENOMEM;
- for (i = 0; i < dev->dma_count; i++) {
- struct gd_dma_span *s = &dev->dma[i];
- if (s->live && address < s->address + s->size && s->address < address + size)
- return -EINVAL;
- }
- struct gd_dma_span *s = &dev->dma[dev->dma_count++];
- s->address = address;
- s->cpu = cpu;
- s->size = size;
- s->live = true;
- return 0;
-}
-
-void gd_dma_unmap(struct gd_device *dev, uint64_t address)
-{
- uint32_t i;
- for (i = 0; i < dev->dma_count; i++)
- if (dev->dma[i].live && dev->dma[i].address == address) {
- dev->dma[i].live = false;
- return;
- }
-}
-
-void gd_dma_barrier(struct gd_device *dev)
-{
- __sync_synchronize();
- (void)dev;
-}
-
-static void load_bar_defaults(struct gd_device *dev)
-{
- size_t i;
- uint32_t chip = dev->board->chip;
-
- for (i = 0; i < sizeof(gd_bar0_defaults) / sizeof(gd_bar0_defaults[0]); i++) {
- uint32_t addr = gd_bar0_defaults[i].addr;
- dev->bar[addr / 4] = gd_bar0_defaults[i].reset;
- dev->bar_valid[addr / 4] = 1;
- }
- /* The register table is a GA102 fixture; override identity per board. */
- set_bar_locked(dev, 0x0, (chip << 20) | 0xa1u);
- dev->wpr2_lo = 0x1ffffe00u;
- dev->wpr2_hi = 0;
- set_bar_locked(dev, 0x1fa824, dev->wpr2_lo);
- set_bar_locked(dev, 0x1fa828, dev->wpr2_hi);
- set_bar_locked(dev, 0x110100, CPUCTL_HALTED);
- set_bar_locked(dev, 0x1100f4, HWCFG2_RESET);
- set_bar_locked(dev, 0x118234, 0xff);
- set_bar_locked(dev, 0x1183a4, 0x6000);
- set_bar_locked(dev, 0x2240c, 0x2);
- set_bar_locked(dev, 0xa00, chip << 20);
- set_bar_locked(dev, 0x1180fc, 1u << 20);
- set_bar_locked(dev, 0x1180f8, 0u); /* BSI scratch 14: no level/error */
- set_bar_locked(dev, 0x118220, 0u);
- set_bar_locked(dev, 0x118244, 0u); /* declared pre-FWSEC boundary: FRTS not cached */
- set_bar_locked(dev, 0x118248, 0u);
- set_bar_locked(dev, 0x118c6c, 0u);
-
- /* GC6 AON secure-scratch bootstrap latches (groups 40/41). The real
- * early boot/SCP flow writes these before FWSEC; we do not emulate that
- * flow, so seed the post-bootstrap values here. This is a test fixture,
- * not evidence of the hardware sequence.
- */
- set_bar_locked(dev, 0x11839c, 0xff);
- set_bar_locked(dev, 0x1183a0, 0xff);
- dev->ss0 = dev->bar[0x82381c / 4];
- dev->ss1 = dev->bar[0x823820 / 4];
-}
-
-static uint32_t gd_doc_io_read(void *ctx, uint32_t address, int priv)
-{
- struct gd_engine *eng = ctx;
- return dio_read(eng->dev, eng, address, priv);
-}
-
-static void gd_doc_io_write(void *ctx, uint32_t address, uint32_t value, int priv)
-{
- struct gd_engine *eng = ctx;
- dio_write(eng->dev, eng, address, value, priv);
-}
-
-static bool csec_bar_read(void *ctx, uint32_t address, uint32_t *value)
-{
- struct gd_device *dev = ctx;
- if (address + 4 > GD_BAR_SIZE || !plm_allowed(dev, address, 3, false))
- return false;
- *value = bar0_raw_read(dev, address);
- if (address == 0x118244 || address == 0x118248)
- GD_DIAG( "diag: group6 read [%#x] -> %#x booterpc=%#x\n",
- address, *value, dev->engine[0].pc);
- return true;
-}
-
-static bool csec_bar_write(void *ctx, uint32_t address, uint32_t value)
-{
- struct gd_device *dev = ctx;
- if (address + 4 > GD_BAR_SIZE || !plm_allowed(dev, address, 3, true)) {
- dev->denied_writes++;
- dev->plm_violations++;
- return false;
- }
- if (address == 0x118244 || address == 0x118248)
- GD_DIAG( "diag: group6 write [%#x]=%#x fwsec=%d pc=%#x\n",
- address, value, !!dev->engine[1].pc, dev->engine[1].pc);
- if (address == 0x1fa824 || address == 0x1fa828) {
- uint32_t di;
- GD_DIAG( "diag: wpr2 write [%#x]=%#x pc=%#x r0=%#x r1=%#x r10=%#x r11=%#x stack:",
- address, value, dev->engine[1].pc, dev->engine[1].r[0],
- dev->engine[1].r[1], dev->engine[1].r[10], dev->engine[1].r[11]);
- for (di = 0; di < dev->engine[1].call_depth && di < 12; di++)
- GD_DIAG( " %#x", dev->engine[1].call_pc[di]);
- GD_DIAG( "\n");
- }
- /* The payload writes through the secure-bus bridge, not the host
- * register path; record the same target-write evidence. */
- /* The Booter's release primitive pulses BAR0 0x1180f8 bit 24;
- * that is the SE common-mutex release doorbell. */
- value &= bar_write_mask(address);
- if (address == 0x1180f8 && (value & (1u << 24)))
- csb_release_all(dev);
- plm_latch_write(dev, address, value);
- bar0_raw_write(dev, address, value);
- return true;
-}
-
-static void load_engine_defaults(struct gd_device *dev, struct gd_engine *eng, uint32_t base, int index)
-{
- size_t i;
- memset(eng, 0, sizeof(*eng));
- eng->dev = dev;
- gd_doc_init(&eng->doc, gd_doc_io_read, gd_doc_io_write, eng);
- gd_trng_init(&eng->trng, eng->se_regs, sizeof(eng->se_regs) / 4);
- gd_csec_init(&eng->csec, csec_bar_read, csec_bar_write, dev);
- eng->index = index;
- eng->base = base;
- eng->hwcfg = 0x20100u;
- eng->hwcfg2 = HWCFG2_RESET;
- eng->dma_stat = 2;
- eng->csb_enable = true;
- eng->csb_valid = false;
- eng->cpuctl = 0;
- pthread_mutex_init(&eng->lock, NULL);
- pthread_cond_init(&eng->wake, NULL);
- pthread_cond_init(&eng->idle, NULL);
- for (i = 0; i < sizeof(gd_io_defaults) / sizeof(gd_io_defaults[0]); i++) {
- uint32_t addr = gd_io_defaults[i].addr;
- if (addr < 0x1000) {
- eng->io[addr / 4] = gd_io_defaults[i].reset;
- eng->io_valid[addr / 4] = 1;
- }
- }
- /* PLM reset values are all-levels until the Booter locks them. */
- eng->reset_plm = 0xffffffffu;
- eng->io[0x288 / 4] = 0xffffffffu;
- eng->io[0x194 / 4] = 0xffffffffu;
- eng->io[0x28c / 4] = 0xffffffffu;
- eng->io[0x190 / 4] = 0xffffffffu;
- eng->io[0x18c / 4] = 0xffffffffu;
- eng->io[0x2b0 / 4] = 0xffffffffu;
- eng->io[0x3c4 / 4] = eng->reset_plm;
- if (index == 1) {
- eng->cpuctl = CPUCTL_HALTED;
- eng->halted = true;
- }
-}
-
-void *gd_bar_pointer(struct gd_device *dev)
-{
- return dev ? dev->bar : NULL;
-}
-
-const char *gd_asset_root(struct gd_device *dev)
-{
- return dev && dev->config.asset_root ? dev->config.asset_root : ".";
-}
-
-struct gd_device *gd_create(const struct gd_board *board, const struct gd_run_config *config)
-{
- struct gd_device *dev = calloc(1, sizeof(*dev));
- FILE *file;
- long size;
-
- if (!dev)
- return NULL;
- csb_init(dev);
- dev->fb_size = GD_FB_SIZE;
- dev->fb = calloc(dev->fb_size, 1);
- if (!dev->fb)
- goto fail;
- dev->board = board;
- dev->config = *config;
- if (dev->config.key_set) {
- /* Debug-fused SCP slots: 37 decrypts the HS image, 0 authenticates
- * the debug DMEM MAC. The debug secret 0 is the HS key with its
- * first byte cleared; production slot 0x28 stays unavailable. */
- memcpy(dev->secrets[37], dev->config.key, 16);
- memcpy(dev->secrets[0], dev->config.key, 16);
- dev->secrets[0][0] = 0;
- dev->secret_set[37] = true;
- dev->secret_set[0] = true;
- }
- dev->bar = calloc(GD_BAR_WORDS, sizeof(uint32_t));
- dev->bar_valid = calloc(GD_BAR_WORDS, 1);
- dev->prom = calloc(PROM_SIZE, 1);
- if (!dev->bar || !dev->bar_valid || !dev->prom)
- goto fail;
- pthread_mutex_init(&dev->lock, NULL);
- pthread_mutex_init(&dev->bar_lock, NULL);
- pthread_mutex_init(&dev->dma_lock, NULL);
- pthread_mutex_init(&dev->trace_lock, NULL);
- if (!gd_aes128_selftest())
- gd_log(dev, GD_LOG_ERROR, "device: AES-128 self-test failed\n");
- load_bar_defaults(dev);
- /* The layout constrains every reservation to the top 256 MiB of usable
- * framebuffer, so one window derived from the strap covers all of them. */
- dev->fb_base = (uint64_t)dev->bar[0x1183a4 / 4] << 20;
- dev->fb_base = dev->fb_base > GD_FB_SIZE ? dev->fb_base - GD_FB_SIZE : 0;
- load_engine_defaults(dev, &dev->engine[0], SEC2_BASE, 0);
- load_engine_defaults(dev, &dev->engine[1], GSP_BASE, 1);
- load_code_table(&dev->engine[1], config->code_path);
- load_code_table(&dev->engine[0], config->booter_code_path);
- dev->pci_command = 0x2;
- set_bar_locked(dev, 0x200, 0x2);
- dev->time_us = now_us();
-
- memset(dev->prom, 0xff, PROM_SIZE);
- file = fopen(config->rom_path, "rb");
- if (!file) {
- gd_log(dev, GD_LOG_ERROR, "device: cannot open ROM %s\n", config->rom_path);
- goto fail;
- }
- fseek(file, 0, SEEK_END);
- size = ftell(file);
- fseek(file, 0, SEEK_SET);
- if (size > 0) {
- if (size > (long)PROM_SIZE)
- size = PROM_SIZE;
- if (fread(dev->prom, 1, (size_t)size, file) != (size_t)size) {
- fclose(file);
- goto fail;
- }
- }
- fclose(file);
- return dev;
-fail:
- gd_destroy(dev);
- return NULL;
-}
-
-void gd_destroy(struct gd_device *dev)
-{
- int i;
- if (!dev)
- return;
- for (i = 0; i < 2; i++) {
- struct gd_engine *eng = &dev->engine[i];
- if (eng->thread_up) {
- pthread_mutex_lock(&eng->lock);
- eng->stop = true;
- pthread_cond_signal(&eng->wake);
- pthread_mutex_unlock(&eng->lock);
- pthread_join(eng->thread, NULL);
- eng->thread_up = false;
- }
- pthread_mutex_destroy(&eng->lock);
- pthread_cond_destroy(&eng->wake);
- pthread_cond_destroy(&eng->idle);
- }
- pthread_mutex_destroy(&dev->trace_lock);
- for (i = 0; i < 2; i++) {
- free(dev->engine[i].code);
- free(dev->engine[i].code_index);
- }
- free(dev->fb);
- free(dev->bar);
- free(dev->bar_valid);
- free(dev->prom);
- free(dev);
-}
-
-void gd_set_engines(struct gd_device *dev, bool enabled)
-{
- int i;
- for (i = 0; i < 2; i++) {
- struct gd_engine *eng = &dev->engine[i];
- eng->enabled = enabled;
- if (enabled && !eng->thread_up) {
- eng->stop = false;
- if (pthread_create(&eng->thread, NULL, engine_thread, eng) == 0) {
- eng->thread_up = true;
- pthread_mutex_lock(&eng->lock);
- pthread_cond_signal(&eng->wake);
- pthread_mutex_unlock(&eng->lock);
- } else {
- eng->enabled = false;
- }
- }
- }
-}
-
-bool gd_engine_progress(struct gd_device *dev, int engine, uint64_t *instructions)
-{
- struct gd_engine *eng;
- if (engine < 0 || engine > 1)
- return false;
- eng = &dev->engine[engine];
- pthread_mutex_lock(&eng->lock);
- if (instructions)
- *instructions = eng->instructions;
- bool progress = eng->instructions != 0;
- pthread_mutex_unlock(&eng->lock);
- return progress;
-}
-
-void gd_advance_us(struct gd_device *dev, uint64_t usec)
-{
- int i;
- dev->time_us += usec;
- for (i = 0; i < 2; i++) {
- struct gd_engine *eng = &dev->engine[i];
- pthread_mutex_lock(&eng->lock);
- settle_scrub(dev, eng);
- pthread_mutex_unlock(&eng->lock);
- }
-}
-
-int gd_reset(struct gd_device *dev)
-{
- int i;
- dev->resets++;
- dev->flr_seen = true;
- for (i = 0; i < 2; i++) {
- struct gd_engine *eng = &dev->engine[i];
- pthread_mutex_lock(&eng->lock);
- while (eng->busy)
- pthread_cond_wait(&eng->idle, &eng->lock);
- engine_reset_state(dev, eng);
- /* FLR resets the function; MAILBOX0/1 DATA_INIT is 0. */
- eng->mailbox[0] = eng->mailbox[1] = 0;
- eng->io[0x0f4 / 4] = HWCFG2_RESET;
- eng->io_valid[0x0f4 / 4] = 1;
- pthread_mutex_unlock(&eng->lock);
- }
- pthread_mutex_lock(&dev->bar_lock);
- uint32_t ss0 = dev->bar[0x82381c / 4];
- uint32_t ss1 = dev->bar[0x823820 / 4];
- load_bar_defaults(dev);
- set_bar_locked(dev, 0x2240c, 0x2);
- set_bar_locked(dev, 0xa00, dev->board->chip << 20);
- set_bar_locked(dev, 0x1180fc, 1u << 20);
- set_bar_locked(dev, 0x1180f8, 0u); /* BSI scratch 14: no level/error */
- set_bar_locked(dev, 0x118220, 0u);
- set_bar_locked(dev, 0x118244, 0u); /* declared pre-FWSEC boundary: FRTS not cached */
- set_bar_locked(dev, 0x118248, 0u);
- set_bar_locked(dev, 0x118c6c, 0u);
-
- /* GC6 AON secure-scratch bootstrap latches (groups 40/41). The real
- * early boot/SCP flow writes these before FWSEC; we do not emulate that
- * flow, so seed the post-bootstrap values here. This is a test fixture,
- * not evidence of the hardware sequence.
- */
- set_bar_locked(dev, 0x11839c, 0xff);
- set_bar_locked(dev, 0x1183a0, 0xff);
- set_bar_locked(dev, 0x82381c, ss0);
- set_bar_locked(dev, 0x823820, ss1);
- dev->ss0 = ss0;
- dev->ss1 = ss1;
- dev->pci_command &= ~0x4u;
- dev->bar[0x200 / 4] = dev->pci_command;
- dev->pci_saved_command = dev->pci_command;
- pthread_mutex_unlock(&dev->bar_lock);
- gd_log(dev, GD_LOG_INFO, "device: FLR\n");
- trace_add(dev, GD_TRACE_FLR, 0, 0, 0, 0);
- return 0;
-}
-
-uint32_t gd_trace_count(struct gd_device *dev)
-{
- return dev ? dev->trace_count : 0;
-}
-
-int gd_trace_get(struct gd_device *dev, uint32_t index, struct gd_trace_record *record)
-{
- uint32_t first;
- if (!dev || !record || index >= dev->trace_count)
- return -EINVAL;
- first = dev->trace_head >= GD_MAX_TRACE ? dev->trace_head - GD_MAX_TRACE : 0;
- *record = dev->trace[(first + index) % GD_MAX_TRACE];
- return 0;
-}
-
-int gd_trace_dump(struct gd_device *dev, const char *path)
-{
- FILE *file;
- uint32_t i;
- if (!path)
- return 0;
- file = fopen(path, "wb");
- if (!file)
- return -EIO;
- fwrite(&dev->trace_count, sizeof(dev->trace_count), 1, file);
- for (i = 0; i < dev->trace_count; i++) {
- struct gd_trace_record record;
- gd_trace_get(dev, i, &record);
- fwrite(&record, sizeof(record), 1, file);
- }
- fclose(file);
- return 0;
-}
-
-void gd_collect(struct gd_device *dev, struct gd_result *out)
-{
- uint32_t i;
- memset(out, 0, sizeof(*out));
- out->driver_result = 0;
- out->device_error = dev->device_error;
- out->fwsec_launch_reached = dev->fwsec_launch_reached;
- out->booter_launch_reached = dev->booter_launch_reached;
- out->fwsec_kdf_ok = dev->fwsec_kdf_ok;
- out->flr_seen = dev->flr_seen;
- out->launches[0] = dev->launches[0];
- out->launches[1] = dev->launches[1];
- out->resets = dev->resets;
- out->host_writes = dev->host_writes;
- out->host_reads = dev->host_reads;
- out->engine_writes = dev->engine_writes;
- out->engine_reads = dev->engine_reads;
- out->instructions[0] = dev->engine[0].instructions;
- out->instructions[1] = dev->engine[1].instructions;
- out->unknown_count = dev->unknown_count;
- for (i = 0; i < dev->unknown_count && i < GD_MAX_UNKNOWN; i++)
- out->unknown[i] = dev->unknown[i];
- out->feat_count = dev->feat_count;
- for (i = 0; i < dev->feat_count && i < 8; i++)
- out->feats[i] = dev->feats[i];
- out->wpr2_lo = dev->bar[0x1fa824 / 4];
- out->wpr2_hi = dev->bar[0x1fa828 / 4];
- out->ss0 = dev->bar[0x82381c / 4];
- out->ss1 = dev->bar[0x823820 / 4];
- out->pci_command = dev->pci_command;
- out->dma_live = dev->dma_count;
- out->fault_pc = dev->fault_pc;
- out->fault_opcode = dev->fault_opcode;
- out->plm_feat = dev->plm_feat_latched;
- out->plm_wpr_cfg = dev->plm_wpr_cfg_latched;
- out->plm_wpr = dev->bar[0x1fa7c4 / 4];
- out->denied_writes = dev->denied_writes;
- out->booter_error = dev->engine[0].io[0x1000 / 4];
- out->fwsec_executed = dev->engine[1].instructions > 0 &&
- !dev->engine[1].core_fault;
- out->wpr2_normalized = dev->wpr2_normalized;
- out->fwsec_mac_ok = dev->engine[1].fwsec_kdf_seen &&
- (!memcmp(dev->engine[1].fwsec_kdf_out, dev->engine[1].fwsec_expect, 16) ||
- !memcmp(dev->engine[1].fwsec_kdf_out, dev->engine[1].fwsec_expect + 16, 16));
- out->fwsec_kdf_ok = out->fwsec_mac_ok && !(dev->bar[0x1438 / 4] >> 16);
- out->booter_executed = dev->engine[0].instructions > 0 &&
- !dev->engine[0].core_fault;
- out->booter_halted = dev->engine[0].instructions > 0 &&
- dev->engine[0].halted;
- out->plm_opened = dev->plm_opened_seen;
- out->target_written = dev->target_write_seen;
- out->unlock_verified = out->fwsec_executed &&
- out->booter_executed && out->plm_opened && out->target_written &&
- out->ss0 == 0x88888888u && out->ss1 == 0x8u &&
- dev->engine[0].reset_plm == 0xffffffffu;
- out->fwsec_halt_pc = dev->engine[1].halt_pc;
- out->booter_halt_pc = dev->engine[0].halt_pc;
- out->fwsec_status = dev->bar[0x1438 / 4];
- out->booter_status = dev->engine[0].mailbox[0];
- out->booter_mailbox0 = dev->engine[0].mailbox[0];
- out->booter_mailbox1 = dev->engine[0].mailbox[1];
- out->booter_reset_plm = dev->engine[0].reset_plm;
- out->group03 = dev->bar[0x11821c / 4];
- if (!dev->fwsec_launch_reached)
- out->stage = GD_STAGE_PRE_FWSEC;
- else if (!dev->engine[1].halted || dev->engine[1].core_fault)
- out->stage = GD_STAGE_FWSEC_RUNNING;
- else if (out->wpr2_hi >> 4 && (out->group03 & (3u << 16)) == (3u << 16))
- out->stage = dev->booter_launch_reached ? GD_STAGE_BOOTER_RUNNING
- : GD_STAGE_WPR2_READY;
- else
- out->stage = GD_STAGE_FWSEC_DONE;
- if (dev->booter_launch_reached && dev->engine[0].halted) {
- if (dev->engine[0].core_fault || out->booter_status)
- out->stage = GD_STAGE_BOOTER_FAILED;
- else if (out->stage == GD_STAGE_BOOTER_RUNNING)
- out->stage = GD_STAGE_BOOTER_HALTED;
- }
-}
diff --git a/tests/integration/device/gd.h b/tests/integration/device/gd.h
deleted file mode 100644
index e103bfa..0000000
--- a/tests/integration/device/gd.h
+++ /dev/null
@@ -1,160 +0,0 @@
-/* SPDX-License-Identifier: GPL-2.0-only */
-/* Native GA102 device library used by the driver integration harness.
- *
- * The device owns the BAR0 shadow, two Falcon execution contexts, DMA
- * mappings and the reset/interrupt state. The host driver reaches it through
- * the kernel shim in tests/integration/shim. No Python is involved in an
- * access.
- */
-#ifndef GD_DEVICE_H
-#define GD_DEVICE_H
-
-#include <stdbool.h>
-#include <stddef.h>
-#include <stdint.h>
-
-#define GD_BAR_SIZE 0x01000000u
-#define GD_BAR_WORDS (GD_BAR_SIZE / 4u)
-#define GD_MAX_DMA 32768
-#define GD_MAX_UNKNOWN 16
-#define GD_MAX_TRACE 65536
-
-enum gd_trace_kind { GD_TRACE_BAR_READ, GD_TRACE_BAR_WRITE, GD_TRACE_DMA, GD_TRACE_START, GD_TRACE_HALT, GD_TRACE_FLR, GD_TRACE_FAULT, GD_TRACE_PC };
-
-struct gd_trace_record {
- uint64_t time_us;
- uint32_t pc, address, value;
- uint16_t kind, source;
- uint16_t size;
-};
-
-enum gd_log_level { GD_LOG_DEBUG, GD_LOG_INFO, GD_LOG_ERROR };
-
-struct gd_device;
-
-struct gd_board {
- uint16_t device; /* PCI device id, selects the driver recipe */
- uint32_t chip; /* PMC_BOOT_0 architecture+implementation */
-};
-
-struct gd_run_config {
- const char *asset_root; /* directory holding the driver's firmware names */
- const char *rom_path;
- bool verbose;
- bool key_set; /* debug key present; execution permitted */
- uint8_t key[16]; /* AES-128 debug key for FWSEC IMEM code */
- const char *code_path; /* pre-decoded FWSEC instruction table, optional */
- const char *booter_code_path; /* pre-decoded SEC2 Booter instruction table */
-};
-
-struct gd_dma_span {
- uint64_t address;
- uint8_t *cpu;
- size_t size;
- bool live;
-};
-
-struct gd_unknown {
- bool write;
- uint32_t address;
- uint32_t value;
- uint32_t pc;
-};
-
-struct gd_result {
- int driver_result; /* the driver's return value */
- int device_error; /* first device-side failure */
- bool fwsec_launch_reached;
- bool booter_launch_reached;
- bool fwsec_executed;
- bool fwsec_kdf_ok;
- bool booter_executed;
- bool booter_halted;
- bool plm_opened;
- bool target_written;
- bool unlock_verified;
- bool flr_seen;
- uint32_t launches[2];
- uint32_t resets;
- uint64_t host_writes;
- uint64_t host_reads;
- uint64_t engine_writes;
- uint64_t engine_reads;
- uint64_t instructions[2];
- uint32_t unknown_count;
- struct gd_unknown unknown[GD_MAX_UNKNOWN];
- uint32_t feats[8];
- uint32_t feat_count;
- uint32_t wpr2_lo, wpr2_hi;
- uint32_t ss0, ss1;
- uint32_t pci_command;
- uint32_t dma_live;
- uint32_t fault_pc, fault_opcode;
- uint32_t plm_feat, plm_wpr_cfg, plm_wpr;
- uint32_t denied_writes;
- uint32_t booter_error; /* NV_CSEC_FALCON_MAILBOX0 at 0x1000 */
- /* Boot-chain evidence. The stage is derived from the same registers
- * and scratch the driver observes. */
- uint32_t stage; /* enum gd_stage */
- uint32_t fwsec_halt_pc;
- uint32_t booter_halt_pc;
- uint32_t fwsec_status; /* SW_SCRATCH(14) value left by FWSEC */
- uint32_t booter_status; /* SEC2 CSB MAILBOX0 status */
- uint32_t booter_mailbox0;
- uint32_t booter_mailbox1;
- uint32_t booter_reset_plm;
- uint32_t group03; /* AON secure scratch group 03(2) */
- uint32_t fwsec_mac_ok; /* debug/prod MAC matched the image */
- uint32_t wpr2_normalized; /* debug FWSEC end encoding converted */
-};
-
-enum gd_stage {
- GD_STAGE_PRE_FWSEC = 0,
- GD_STAGE_FWSEC_RUNNING,
- GD_STAGE_FWSEC_DONE,
- GD_STAGE_WPR2_READY,
- GD_STAGE_BOOTER_RUNNING,
- GD_STAGE_BOOTER_HALTED,
- GD_STAGE_BOOTER_FAILED,
-};
-
-struct gd_device *gd_create(const struct gd_board *board, const struct gd_run_config *config);
-void gd_destroy(struct gd_device *dev);
-
-/* Host BAR0 access. Unknown registers are recorded and fail closed. */
-uint32_t gd_bar0_read(struct gd_device *dev, uint32_t address);
-void gd_bar0_write(struct gd_device *dev, uint32_t address, uint32_t value);
-
-/* PROM window copy for memcpy_fromio(). */
-int gd_prom_copy(struct gd_device *dev, uint32_t offset, void *dest, size_t size);
-
-/* PCI configuration space. space 0 is config, 1 is the PCIe capability. */
-int gd_pci_read(struct gd_device *dev, int space, int offset, int width, uint32_t *value);
-int gd_pci_write(struct gd_device *dev, int space, int offset, int width, uint32_t value);
-
-/* DMA mappings are identified by their device address. */
-int gd_dma_map(struct gd_device *dev, uint64_t address, void *cpu, size_t size);
-void gd_dma_unmap(struct gd_device *dev, uint64_t address);
-void gd_dma_barrier(struct gd_device *dev);
-
-/* FLR / device reset. */
-int gd_reset(struct gd_device *dev);
-
-void gd_advance_us(struct gd_device *dev, uint64_t usec);
-void gd_log(struct gd_device *dev, enum gd_log_level level, const char *format, ...)
- __attribute__((format(printf, 3, 4)));
-
-/* Engine threads start when enabled and are joined by gd_destroy(). */
-void gd_set_engines(struct gd_device *dev, bool enabled);
-bool gd_engine_progress(struct gd_device *dev, int engine, uint64_t *instructions);
-
-void gd_collect(struct gd_device *dev, struct gd_result *out);
-uint32_t gd_trace_count(struct gd_device *dev);
-int gd_trace_get(struct gd_device *dev, uint32_t index, struct gd_trace_record *record);
-int gd_trace_dump(struct gd_device *dev, const char *path);
-
-/* BAR0 shadow used by the shim to turn a kernel pointer into an offset. */
-void *gd_bar_pointer(struct gd_device *dev);
-const char *gd_asset_root(struct gd_device *dev);
-
-#endif
diff --git a/tests/integration/device/services.c b/tests/integration/device/services.c
deleted file mode 100644
index 353195e..0000000
--- a/tests/integration/device/services.c
+++ /dev/null
@@ -1,215 +0,0 @@
-// SPDX-License-Identifier: GPL-2.0-only
-#include <string.h>
-
-#include "services.h"
-
-/* ---- CSEC BAR0 bridge ------------------------------------------------- */
-
-void gd_csec_init(struct gd_csec *csec,
- bool (*bar_read)(void *, uint32_t, uint32_t *),
- bool (*bar_write)(void *, uint32_t, uint32_t), void *ctx)
-{
- memset(csec, 0, sizeof(*csec));
- csec->bar_read = bar_read;
- csec->bar_write = bar_write;
- csec->ctx = ctx;
- csec->csr = 0x00000400u; /* XACT_FINISHED, STATUS=IDLE */
-}
-
-uint32_t gd_csec_read(struct gd_csec *csec, uint32_t offset)
-{
- switch (offset) {
- case 0x1c000:
- /* CMD, byte enables and the completed/idle status. */
- return (csec->csr & 0x800070f3u) | 0x00000400u;
- case 0x1c100:
- return csec->addr;
- case 0x1c200:
- return csec->data;
- default:
- return 0;
- }
-}
-
-void gd_csec_write(struct gd_csec *csec, uint32_t offset, uint32_t value)
-{
- uint32_t address, word, out, i;
-
- switch (offset) {
- case 0x1c100:
- csec->addr = value;
- return;
- case 0x1c200:
- csec->data = value;
- return;
- case 0x1c000:
- break;
- default:
- return;
- }
-
- csec->csr = value & ~0x80000000u;
- if (!(value & 0x80000000u))
- return;
-
- csec->cmd = value & 3u;
- csec->byte_enable = (value >> 4) & 0xfu;
- address = csec->addr;
- word = 0;
- if (!csec->byte_enable)
- csec->byte_enable = 0xfu;
-
- if (csec->cmd == 1u) { /* read */
- if (csec->bar_read && csec->bar_read(csec->ctx, address & ~3u, &word)) {
- out = 0;
- for (i = 0; i < 4; i++)
- if (csec->byte_enable & (1u << i))
- out |= ((word >> (8 * ((address + i) & 3))) & 0xffu) << (8 * i);
- csec->data = out;
- }
- } else if (csec->cmd == 2u || csec->cmd == 3u) { /* write */
- if (csec->bar_write && csec->bar_read &&
- csec->bar_read(csec->ctx, address & ~3u, &word)) {
- for (i = 0; i < 4; i++)
- if (csec->byte_enable & (1u << i))
- word = (word & ~(0xffu << (8 * ((address + i) & 3)))) |
- (((csec->data >> (8 * i)) & 0xffu) <<
- (8 * ((address + i) & 3)));
- csec->bar_write(csec->ctx, address & ~3u, word);
- }
- }
- csec->finished = true;
- csec->csr = 0x00000400u;
-}
-
-/* ---- DOC / DIC output and input queues -------------------------------- */
-
-void gd_doc_reset(struct gd_doc *doc)
-{
- memset(doc, 0, sizeof(*doc));
- /*
- * The debug FWSEC/Booter expect the pre-existing DIO idle count
- * (0x1c0004). CSEC's hardware reset is 0x001c0000; COUNT is a queue
- * property and is firmware-visible, so keep the observed value here.
- */
- doc->ctrl = 0x001c0004u;
-}
-
-void gd_doc_init(struct gd_doc *doc,
- uint32_t (*dio_read)(void *, uint32_t, int),
- void (*dio_write)(void *, uint32_t, uint32_t, int), void *ctx)
-{
- gd_doc_reset(doc);
- doc->dio_read = dio_read;
- doc->dio_write = dio_write;
- doc->ctx = ctx;
-}
-
-uint32_t gd_doc_read(struct gd_doc *doc, uint32_t offset, int priv)
-{
- (void)priv;
- switch (offset) {
- case 0xd000:
- return doc->ctrl;
- case 0xd100:
- return doc->addr;
- case 0xd200:
- case 0xd300:
- return doc->d[(offset - 0xd200) / 0x100];
- case 0xd400:
- return (doc->dic_ctrl & 0xffu) |
- ((doc->dic_ctrl & (1u << 19)) ? (1u << 19) : 0);
- case 0xd500:
- case 0xd600:
- case 0xd700:
- return doc->dic_d[(offset - 0xd500) / 0x100];
- default:
- return 0;
- }
-}
-
-void gd_doc_write(struct gd_doc *doc, uint32_t offset, uint32_t value, int priv)
-{
- switch (offset) {
- case 0xd000:
- /* The debug firmware treats DOC_CTRL writes as error clears. The
- * queue status is read-only from its point of view. */
- return;
- case 0xd100:
- doc->addr = value & 0xffffu;
- if (value & 0x10000u) {
- if (doc->dio_read)
- doc->dic_d[0] = doc->dio_read(doc->ctx, doc->addr, priv);
- doc->dic_ctrl = (doc->dic_ctrl & ~0xffu) | 1u;
- doc->dic_ctrl |= 1u << 19;
- } else if (doc->dio_write) {
- doc->dio_write(doc->ctx, doc->addr, doc->d[0], priv);
- }
- return;
- case 0xd200:
- case 0xd300:
- doc->d[(offset - 0xd200) / 0x100] = value;
- return;
- case 0xd400:
- doc->dic_ctrl = value;
- return;
- case 0xd500:
- case 0xd600:
- case 0xd700:
- doc->dic_d[(offset - 0xd500) / 0x100] = value;
- return;
- default:
- return;
- }
-}
-
-/* ---- SE TRNG ---------------------------------------------------------- */
-
-void gd_trng_init(struct gd_trng *trng, uint32_t *regs, size_t reg_count)
-{
- memset(trng, 0, sizeof(*trng));
- trng->regs = regs;
- trng->reg_count = reg_count;
-}
-
-uint32_t gd_trng_read(struct gd_trng *trng, uint32_t address)
-{
- if (!trng->regs || address / 4 >= trng->reg_count)
- return 0;
- /* The debug image only requires the post-reseed ready state. Full
- * entropy generation remains in emulator/falcon_emu/se_trng.py. */
- if (address == 0x3f04)
- return 0x300u;
- if (address == 0x104)
- return trng->regs[0x104 / 4] | 1u;
- return trng->regs[address / 4];
-}
-
-void gd_trng_write(struct gd_trng *trng, uint32_t address, uint32_t value)
-{
- if (!trng->regs || address / 4 >= trng->reg_count)
- return;
- trng->regs[address / 4] = value;
-}
-
-/* ---- Source-defined identification registers -------------------------- */
-
-uint32_t gd_falcon_engid(void)
-{
- /* NV_CSEC_FALCON_ENGID: FAMILYID_SEC (1), instance 0. */
- return 0x00000100u;
-}
-
-uint32_t gd_csberrstat(bool enable, bool valid, uint32_t pc)
-{
- return (valid ? 0x80000000u : 0) |
- (enable ? 0x40000000u : 0) |
- (pc & 0x00ffffffu);
-}
-
-uint32_t gd_pbus_emulation_rev(unsigned index)
-{
- (void)index;
- /* NV_PBUS_EMULATION_REV0/1_VALUE_HARDWARE. */
- return 0;
-}
diff --git a/tests/integration/device/services.h b/tests/integration/device/services.h
deleted file mode 100644
index 79ca006..0000000
--- a/tests/integration/device/services.h
+++ /dev/null
@@ -1,62 +0,0 @@
-/* SPDX-License-Identifier: GPL-2.0-only */
-/* Address-services shared by the Falcon engine models.
- *
- * The Falcon core only performs an MMIO access. CSEC BAR0, DOC/DIC and the
- * SE TRNG are separate hardware blocks; keeping their state here avoids
- * teaching the instruction interpreter about individual addresses.
- */
-#ifndef GD_SERVICES_H
-#define GD_SERVICES_H
-
-#include <stdbool.h>
-#include <stddef.h>
-#include <stdint.h>
-
-struct gd_csec {
- bool (*bar_read)(void *ctx, uint32_t address, uint32_t *value);
- bool (*bar_write)(void *ctx, uint32_t address, uint32_t value);
- void *ctx;
- uint32_t csr, addr, data;
- uint32_t cmd, byte_enable;
- bool finished;
-};
-
-void gd_csec_init(struct gd_csec *csec,
- bool (*bar_read)(void *, uint32_t, uint32_t *),
- bool (*bar_write)(void *, uint32_t, uint32_t), void *ctx);
-uint32_t gd_csec_read(struct gd_csec *csec, uint32_t offset);
-void gd_csec_write(struct gd_csec *csec, uint32_t offset, uint32_t value);
-
-struct gd_doc {
- uint32_t ctrl;
- uint32_t d[3];
- uint32_t dic_ctrl;
- uint32_t dic_d[3];
- uint32_t addr;
- uint32_t (*dio_read)(void *ctx, uint32_t addr, int priv);
- void (*dio_write)(void *ctx, uint32_t addr, uint32_t value, int priv);
- void *ctx;
-};
-
-void gd_doc_reset(struct gd_doc *doc);
-void gd_doc_init(struct gd_doc *doc,
- uint32_t (*dio_read)(void *, uint32_t, int),
- void (*dio_write)(void *, uint32_t, uint32_t, int), void *ctx);
-uint32_t gd_doc_read(struct gd_doc *doc, uint32_t offset, int priv);
-void gd_doc_write(struct gd_doc *doc, uint32_t offset, uint32_t value, int priv);
-
-struct gd_trng {
- uint32_t *regs; /* SE register file, owned by the engine */
- size_t reg_count;
-};
-
-void gd_trng_init(struct gd_trng *trng, uint32_t *regs, size_t reg_count);
-uint32_t gd_trng_read(struct gd_trng *trng, uint32_t address);
-void gd_trng_write(struct gd_trng *trng, uint32_t address, uint32_t value);
-
-/* Source-defined identity values. */
-uint32_t gd_falcon_engid(void);
-uint32_t gd_csberrstat(bool enable, bool valid, uint32_t pc);
-uint32_t gd_pbus_emulation_rev(unsigned index);
-
-#endif
diff --git a/tests/integration/device/wpr2.c b/tests/integration/device/wpr2.c
deleted file mode 100644
index 0268311..0000000
--- a/tests/integration/device/wpr2.c
+++ /dev/null
@@ -1,130 +0,0 @@
-// SPDX-License-Identifier: GPL-2.0-only
-#include <errno.h>
-#include <stddef.h>
-
-#include "wpr2.h"
-
-void gd_wpr2_decode(uint32_t lo, uint32_t hi, struct gd_wpr2_range *out)
-{
- out->start = (uint64_t)(lo & 0xfffffff0u) << 8;
- out->end = ((uint64_t)(hi & 0xfffffff0u) << 8) + GD_WPR2_ALIGN;
-}
-
-int gd_wpr2_encode(uint64_t start, uint64_t end, uint32_t *lo, uint32_t *hi)
-{
- if (!lo || !hi || start >= end || (start & (GD_WPR2_ALIGN - 1)) ||
- (end & (GD_WPR2_ALIGN - 1)) ||
- (end < GD_WPR2_ALIGN) || ((end - GD_WPR2_ALIGN) >> 12) > 0x0fffffffu ||
- (start >> 12) > 0x0fffffffu)
- return -EINVAL;
- *lo = (uint32_t)((start >> 12) << 4);
- *hi = (uint32_t)(((end - GD_WPR2_ALIGN) >> 12) << 4);
- return 0;
-}
-
-int gd_wpr2_fwsec_hi(uint32_t lo, uint32_t *hi)
-{
- uint64_t start = (uint64_t)(lo & 0xfffffff0u) << 8;
- uint32_t dummy;
-
- if (!hi)
- return -EINVAL;
- /* The debug FWSEC image writes the inclusive 4 KiB end. The Booter's
- * decode adds the 128 KiB alignment term, so the conforming HI is the
- * last aligned block of a 1 MiB FRTS window. */
- return gd_wpr2_encode(start, start + GD_WPR2_FRTS_SIZE, &dummy, hi);
-}
-
-int gd_wpr2_validate(const struct gd_wpr2_meta *m)
-{
- uint64_t gsp_end, boot_end;
-
- if (!m || m->wpr_start >= m->wpr_end ||
- (m->wpr_start & (GD_WPR2_ALIGN - 1)) ||
- (m->wpr_end & (GD_WPR2_ALIGN - 1)))
- return -EINVAL;
- if (!m->frts_size || m->frts_offset >= m->wpr_end ||
- (m->frts_offset & 0xfff))
- return -EINVAL;
- if (m->frts_offset != m->wpr_end - m->frts_size)
- return -EINVAL;
- if (m->gsp_offset <= m->wpr_start || m->gsp_offset >= m->wpr_end ||
- m->gsp_size == 0)
- return -EINVAL;
- gsp_end = m->gsp_offset + m->gsp_size;
- if (gsp_end < m->gsp_offset || gsp_end > m->wpr_end)
- return -EINVAL;
- if (m->boot_offset <= m->wpr_start || m->boot_offset >= m->wpr_end ||
- m->boot_size == 0)
- return -EINVAL;
- boot_end = m->boot_offset + m->boot_size;
- if (boot_end < m->boot_offset || boot_end > m->wpr_end)
- return -EINVAL;
- if (m->gsp_offset < m->boot_offset) {
- if (m->gsp_offset - m->wpr_start < m->meta_size)
- return -EINVAL;
- if (boot_end > m->frts_offset ||
- m->boot_offset - m->gsp_offset < m->gsp_size)
- return -EINVAL;
- } else {
- if (m->boot_offset - m->wpr_start < m->meta_size)
- return -EINVAL;
- if (gsp_end > m->frts_offset ||
- m->gsp_offset - m->boot_offset < m->boot_size)
- return -EINVAL;
- }
- return 0;
-}
-
-int gd_wpr2_selftest(void)
-{
- struct gd_wpr2_range r;
- struct gd_wpr2_meta m;
- const uint64_t start = 0x5f5400000ull;
- const uint64_t end = 0x5fff00000ull;
- uint32_t lo, hi, norm;
- int ret = 0;
-
- if (gd_wpr2_encode(start, end, &lo, &hi) || lo != 0x5f54000u ||
- hi != 0x5ffee00u)
- ret |= 1;
- gd_wpr2_decode(lo, hi, &r);
- if (r.start != start || r.end != end)
- ret |= 2;
- if (gd_wpr2_encode(0x5ffe00000ull, 0x5ffe00000ull + GD_WPR2_FRTS_SIZE,
- &lo, &hi) || hi != 0x5ffee00u)
- ret |= 4;
- if (gd_wpr2_fwsec_hi(lo, &norm) || norm != hi)
- ret |= 8;
- /* Unaligned ranges are refused. */
- if (!gd_wpr2_encode(start + 0x1000, end, &lo, &hi))
- ret |= 16;
- if (!gd_wpr2_encode(start, end - 0x1000, &lo, &hi))
- ret |= 32;
-
- m = (struct gd_wpr2_meta){
- .wpr_start = 0x5f5400000ull, .wpr_end = 0x5fff00000ull,
- .frts_offset = 0x5ffe00000ull, .frts_size = 0x100000ull,
- .gsp_offset = 0x5f5400000ull + 0x100000ull,
- .gsp_size = 0x4000000ull,
- .boot_offset = 0x5fda00000ull, .boot_size = 0x6000ull,
- .meta_size = 0x100ull,
- };
- if (gd_wpr2_validate(&m))
- ret |= 64;
- m.frts_offset -= 0x1000; /* FRTS no longer at the end of WPR2 */
- if (!gd_wpr2_validate(&m))
- ret |= 128;
- m = (struct gd_wpr2_meta){
- .wpr_start = 0x5f5400000ull, .wpr_end = 0x5fff00000ull,
- .frts_offset = 0x5ffe00000ull, .frts_size = 0x100000ull,
- .gsp_offset = 0x5f5400000ull + 0x100000ull,
- .gsp_size = 0x4000000ull,
- .boot_offset = 0x5fda00000ull, .boot_size = 0x6000ull,
- .meta_size = 0x100ull,
- };
- m.boot_offset = m.gsp_offset + 0x100; /* GSP image overlaps the BL */
- if (!gd_wpr2_validate(&m))
- ret |= 256;
- return ret;
-}
diff --git a/tests/integration/device/wpr2.h b/tests/integration/device/wpr2.h
deleted file mode 100644
index e565715..0000000
--- a/tests/integration/device/wpr2.h
+++ /dev/null
@@ -1,49 +0,0 @@
-/* SPDX-License-Identifier: GPL-2.0-only */
-/* WPR2 register encoding and range rules.
- *
- * The values and tests follow booter_wpr2_tu10x.c and
- * booter_load_tu10x.c: the LO/HI fields are address >> 8, the end carries a
- * 128 KiB (0x20000) alignment term, and the FRTS window must be exactly the
- * 1 MiB the FWSEC command requests. The debug FWSEC image publishes an
- * inclusive 4 KiB end; gd_wpr2_fwsec_hi() converts that convention for the
- * Booter, and the caller reports the conversion as evidence.
- */
-#ifndef GD_WPR2_H
-#define GD_WPR2_H
-
-#include <stdbool.h>
-#include <stdint.h>
-
-#define GD_WPR2_ALIGN 0x20000u
-#define GD_WPR2_FRTS_SIZE 0x100000u
-
-struct gd_wpr2_range {
- uint64_t start;
- uint64_t end; /* exclusive */
-};
-
-/* Booter view: start = LO << 12; end = (HI << 12) + 0x20000. */
-void gd_wpr2_decode(uint32_t lo, uint32_t hi, struct gd_wpr2_range *out);
-
-/* Booter view encoder. end must be 128 KiB aligned. */
-int gd_wpr2_encode(uint64_t start, uint64_t end, uint32_t *lo, uint32_t *hi);
-
-/* FWSEC publishes the inclusive 4 KiB end as ((end - 1) >> 12) << 4.
- * Return the HI value the Booter expects for a 1 MiB FRTS window. */
-int gd_wpr2_fwsec_hi(uint32_t lo, uint32_t *hi);
-
-struct gd_wpr2_meta {
- uint64_t wpr_start, wpr_end;
- uint64_t frts_offset, frts_size;
- uint64_t gsp_offset, gsp_size;
- uint64_t boot_offset, boot_size;
- uint64_t meta_size;
-};
-
-/* Rules the Booter enforces on a WprMeta-proposed range. */
-int gd_wpr2_validate(const struct gd_wpr2_meta *meta);
-
-/* Unit-test entry: returns 0 when every vector passes. */
-int gd_wpr2_selftest(void);
-
-#endif
diff --git a/tests/integration/hooks.py b/tests/integration/hooks.py
deleted file mode 100644
index cd98977..0000000
--- a/tests/integration/hooks.py
+++ /dev/null
@@ -1,71 +0,0 @@
-# SPDX-License-Identifier: GPL-2.0-only
-"""Hook parity for the device model.
-
-booter.toml is the source of the register inventory. The generated header in
-device/booter_regs.h must match its resets and masks, and every non-storage
-operation must have a compiled counterpart in gd.c. Unsupported operations
-fail the parity check instead of silently drifting.
-"""
-from __future__ import annotations
-
-import re
-import sys
-import tomllib
-from pathlib import Path
-
-HERE = Path(__file__).resolve().parent
-ROOT = HERE.parents[1]
-TOML = ROOT / "emulator" / "hw" / "booter.toml"
-HEADER = HERE / "device" / "booter_regs.h"
-DEVICE = HERE / "device" / "gd.c"
-
-# These are the only non-storage operations in booter.toml.
-COMPILED_HOOKS = {0x404, 0x700}
-
-
-def inventory():
- with TOML.open("rb") as file:
- data = tomllib.load(file)
- rows = {"bar0": [], "io": []}
- special = []
- for name, reg in data.get("reg", {}).items():
- space = reg.get("space", "io")
- rows[space].append((reg["addr"], reg.get("reset", 0), reg.get("mask", 0xFFFFFFFF)))
- known = {"space", "addr", "reset", "mask", "w1c", "plm", "read_levels",
- "write_levels", "flr_reset", "denied_read", "host", "io"}
- if set(reg) - known:
- special.append((name, reg))
- for space in rows:
- rows[space].sort()
- return rows, special
-
-
-def header_rows(space: str):
- text = HEADER.read_text()
- start = text.index("gd_%s_defaults[]" % space)
- body = text[start:text.index("};", start)]
- return [(int(a, 16), int(v, 16), int(m, 16))
- for a, v, m in re.findall(r"\{ 0x([0-9a-f]+)u, 0x([0-9a-f]+)u, 0x([0-9a-f]+)u \}", body)]
-
-
-def check() -> list[str]:
- failures = []
- rows, special = inventory()
- for space in ("bar0", "io"):
- if header_rows(space) != rows[space]:
- failures.append(f"{space} reset/mask inventory drifted from booter.toml")
- addresses = {reg["addr"] for _, reg in special}
- if addresses != COMPILED_HOOKS:
- failures.append(f"TOML hooks {sorted(addresses)} != compiled {sorted(COMPILED_HOOKS)}")
- device = DEVICE.read_text()
- for address in sorted(COMPILED_HOOKS):
- if "0x%x" % address not in device:
- failures.append(f"compiled hook 0x{address:x} missing from gd.c")
- return failures
-
-
-if __name__ == "__main__":
- problems = check()
- for problem in problems:
- print(problem, file=sys.stderr)
- raise SystemExit(1 if problems else 0)
diff --git a/tests/integration/inputs.py b/tests/integration/inputs.py
deleted file mode 100644
index f0a754e..0000000
--- a/tests/integration/inputs.py
+++ /dev/null
@@ -1,16 +0,0 @@
-# SPDX-License-Identifier: GPL-2.0-only
-"""Run-directory inputs: prepared ROM, assets and the per-run manifest."""
-from __future__ import annotations
-
-import json
-from pathlib import Path
-
-from . import assets
-
-
-def prepare(family: str, run_dir: Path) -> dict:
- manifest = assets.prepare_run(family, run_dir)
- manifest["rom_sha256"] = __import__("hashlib").sha256(
- (Path(run_dir) / "rom.bin").read_bytes()).hexdigest()
- (Path(run_dir) / "manifest.json").write_text(json.dumps(manifest, indent=2) + "\n")
- return manifest
diff --git a/tests/integration/run.py b/tests/integration/run.py
deleted file mode 100644
index 7a78156..0000000
--- a/tests/integration/run.py
+++ /dev/null
@@ -1,228 +0,0 @@
-# SPDX-License-Identifier: GPL-2.0-only
-"""Run one transaction through the native runner and write evidence."""
-from __future__ import annotations
-
-import ctypes as C
-import json
-import os
-import time
-from pathlib import Path
-
-from . import assets, build, codetable, inputs
-
-MAX_UNKNOWN = 16
-
-
-class Unknown(C.Structure):
- _fields_ = [
- ("write", C.c_bool),
- ("address", C.c_uint32),
- ("value", C.c_uint32),
- ("pc", C.c_uint32),
- ]
-
-
-class Result(C.Structure):
- _fields_ = [
- ("driver_result", C.c_int),
- ("device_error", C.c_int),
- ("fwsec_launch_reached", C.c_bool),
- ("booter_launch_reached", C.c_bool),
- ("fwsec_executed", C.c_bool),
- ("fwsec_kdf_ok", C.c_bool),
- ("booter_executed", C.c_bool),
- ("booter_halted", C.c_bool),
- ("plm_opened", C.c_bool),
- ("target_written", C.c_bool),
- ("unlock_verified", C.c_bool),
- ("flr_seen", C.c_bool),
- ("launches", C.c_uint32 * 2),
- ("resets", C.c_uint32),
- ("host_writes", C.c_uint64),
- ("host_reads", C.c_uint64),
- ("engine_writes", C.c_uint64),
- ("engine_reads", C.c_uint64),
- ("instructions", C.c_uint64 * 2),
- ("unknown_count", C.c_uint32),
- ("unknown", Unknown * MAX_UNKNOWN),
- ("feats", C.c_uint32 * 8),
- ("feat_count", C.c_uint32),
- ("wpr2_lo", C.c_uint32),
- ("wpr2_hi", C.c_uint32),
- ("ss0", C.c_uint32),
- ("ss1", C.c_uint32),
- ("pci_command", C.c_uint32),
- ("dma_live", C.c_uint32),
- ("fault_pc", C.c_uint32),
- ("fault_opcode", C.c_uint32),
- ("plm_feat", C.c_uint32),
- ("plm_wpr_cfg", C.c_uint32),
- ("plm_wpr", C.c_uint32),
- ("denied_writes", C.c_uint32),
- ("booter_error", C.c_uint32),
- ("stage", C.c_uint32),
- ("fwsec_halt_pc", C.c_uint32),
- ("booter_halt_pc", C.c_uint32),
- ("fwsec_status", C.c_uint32),
- ("booter_status", C.c_uint32),
- ("booter_mailbox0", C.c_uint32),
- ("booter_mailbox1", C.c_uint32),
- ("booter_reset_plm", C.c_uint32),
- ("group03", C.c_uint32),
- ("fwsec_mac_ok", C.c_uint32),
- ("wpr2_normalized", C.c_uint32),
- ]
-
-
-def load_runner(library: Path):
- lib = C.CDLL(str(library))
- lib.gd_run_transaction.argtypes = [
- C.c_uint16, C.c_uint32, C.c_char_p, C.c_char_p, C.c_char_p, C.c_char_p,
- C.c_char_p, C.c_char_p,
- C.c_int, C.c_int, C.POINTER(Result),
- ]
- lib.gd_run_transaction.restype = C.c_int
- return lib
-
-
-def as_dict(family: str, manifest: dict, result: Result, elapsed: float) -> dict:
- return {
- "family": family,
- "run_dir": manifest.get("run_dir"),
- "device": manifest["device"],
- "chip": manifest["chip"],
- "fwsec_executed": bool(result.fwsec_executed),
- "fwsec_kdf_ok": bool(result.fwsec_kdf_ok),
- "booter_executed": bool(result.booter_executed),
- "booter_halted": bool(result.booter_halted),
- "plm_opened": bool(result.plm_opened),
- "target_written": bool(result.target_written),
- "unlock_verified": bool(result.unlock_verified),
- "driver_result": result.driver_result,
- "device_error": result.device_error,
- "fwsec_launch_reached": bool(result.fwsec_launch_reached),
- "booter_launch_reached": bool(result.booter_launch_reached),
- "flr_seen": bool(result.flr_seen),
- "launches": list(result.launches),
- "resets": result.resets,
- "host_writes": result.host_writes,
- "host_reads": result.host_reads,
- "engine_writes": result.engine_writes,
- "engine_reads": result.engine_reads,
- "instructions": list(result.instructions),
- "unknown": [
- {
- "write": bool(result.unknown[i].write),
- "address": result.unknown[i].address,
- "value": result.unknown[i].value,
- "pc": result.unknown[i].pc,
- }
- for i in range(min(result.unknown_count, MAX_UNKNOWN))
- ],
- "feats": list(result.feats)[: result.feat_count],
- "wpr2": [result.wpr2_lo, result.wpr2_hi],
- "selectors": [result.ss0, result.ss1],
- "pci_command": result.pci_command,
- "dma_live": result.dma_live,
- "fault_pc": result.fault_pc,
- "fault_opcode": result.fault_opcode,
- "plm_feat": result.plm_feat,
- "plm_wpr_cfg": result.plm_wpr_cfg,
- "plm_wpr": result.plm_wpr,
- "denied_writes": result.denied_writes,
- "booter_error": result.booter_error,
- "stage": STAGES.get(result.stage, result.stage),
- "fwsec_halt_pc": result.fwsec_halt_pc,
- "booter_halt_pc": result.booter_halt_pc,
- "fwsec_status": result.fwsec_status,
- "booter_status": result.booter_status,
- "booter_mailbox0": result.booter_mailbox0,
- "booter_mailbox1": result.booter_mailbox1,
- "booter_reset_plm": result.booter_reset_plm,
- "group03": result.group03,
- "fwsec_mac_ok": bool(result.fwsec_mac_ok),
- "wpr2_normalized": bool(result.wpr2_normalized),
- "elapsed_s": round(elapsed, 3),
- }
-
-
-def run_transaction(family: str, run_dir: Path, *,
- threads: bool = False, verbose: bool = False,
- library: Path | None = None) -> dict:
- run_dir = Path(run_dir)
- manifest = inputs.prepare(family, run_dir)
- library = Path(library) if library else build.build()
- lib = load_runner(library)
- key_hex = os.environ.get("GPU_UNLOCK_DEBUG_KEY")
- key = bytes.fromhex(key_hex) if key_hex else None
- if key is not None and len(key) != 16:
- raise SystemExit("GPU_UNLOCK_DEBUG_KEY must be 16 bytes of hex")
- code_path = ""
- booter_code_path = ""
- if key is not None:
- codetable.build(library, run_dir / "rom.bin", key, run_dir / "code.bin")
- code_path = str(run_dir / "code.bin")
- booter = run_dir / "assets" / assets.FAMILIES[family]["booter"]
- codetable.build_booter(library, booter, key, run_dir / "booter_code.bin")
- booter_code_path = str(run_dir / "booter_code.bin")
- result = Result()
- started = time.monotonic()
- ret = lib.gd_run_transaction(
- manifest["device"], manifest["chip"],
- str(run_dir / "rom.bin").encode(),
- str(run_dir / "assets").encode(),
- str(run_dir / "trace.bin").encode(),
- code_path.encode(), booter_code_path.encode(), key,
- 1 if threads else 0, 1 if verbose else 0, C.byref(result),
- )
- elapsed = time.monotonic() - started
- record = as_dict(family, manifest, result, elapsed)
- record["return"] = ret
- (run_dir / "result.json").write_text(json.dumps(record, indent=2) + "\n")
- decode_trace(run_dir / "trace.bin", run_dir / "trace.jsonl")
- return record
-
-
-TRACE_KINDS = {0: "bar-read", 1: "bar-write", 2: "dma", 3: "start", 4: "halt", 5: "flr", 6: "fault", 7: "pc"}
-
-STAGES = {
- 0: "pre-fwsec",
- 1: "fwsec-running",
- 2: "fwsec-done",
- 3: "wpr2-ready",
- 4: "booter-running",
- 5: "booter-halted",
- 6: "booter-failed",
-}
-
-
-def decode_trace(binary: Path, text: Path):
- data = Path(binary).read_bytes()
- if len(data) < 4:
- return
- count = int.from_bytes(data[:4], "little")
- lines = []
- offset = 4
- for _ in range(count):
- if offset + 32 > len(data):
- break
- time_us, pc, address, value, kind, source, size = __import__("struct").unpack_from("<QIIIHHH", data, offset)
- offset += 32
- lines.append({"time_us": time_us, "pc": pc, "address": address,
- "value": value, "kind": TRACE_KINDS.get(kind, kind),
- "source": source, "size": size})
- Path(text).write_text("".join(json.dumps(line) + "\n" for line in lines))
-
-
-if __name__ == "__main__":
- import argparse
-
- parser = argparse.ArgumentParser()
- parser.add_argument("family", choices=sorted(assets.FAMILIES))
- parser.add_argument("--run-dir", default=None)
- parser.add_argument("--threads", action="store_true")
- parser.add_argument("--verbose", action="store_true")
- args = parser.parse_args()
- directory = Path(args.run_dir or (Path("build") / "runs" / args.family))
- print(json.dumps(run_transaction(
- args.family, directory, threads=args.threads, verbose=args.verbose), indent=2))
diff --git a/tests/integration/runner.c b/tests/integration/runner.c
deleted file mode 100644
index 626dc6b..0000000
--- a/tests/integration/runner.c
+++ /dev/null
@@ -1,153 +0,0 @@
-// SPDX-License-Identifier: GPL-2.0-only
-/* One exported entry for the integration harness. The driver, shim and
- * device are linked into one shared object; this function is the only
- * boundary Python crosses. */
-#include <errno.h>
-#include <stdint.h>
-#include <string.h>
-
-#include "device/gd.h"
-#include "shim/compat.h"
-#include "../../kernel/gpu_unlock/firmware_610.h"
-#include "../../kernel/gpu_unlock/recipes.h"
-#include "../../kernel/gpu_unlock/unlock_610.h"
-#include "../../kernel/gpu_unlock/vbios_610.h"
-#include "device/aes.h"
-#include "device/wpr2.h"
-
-#if defined(__GNUC__)
-#define GD_EXPORT __attribute__((visibility("default")))
-#else
-#define GD_EXPORT
-#endif
-
-struct gd_fwsec_info {
- uint32_t version, image_off, image_size, data_off, imem_size, dmem_size, interface_off;
-};
-
-GD_EXPORT int gd_fwsec_extract(const char *rom_path, struct gd_fwsec_info *info)
-{
- FILE *file;
- long size;
- uint8_t *rom;
- struct gpu_unlock_fwsec fw;
- int ret;
- if (!rom_path || !info)
- return -EINVAL;
- file = fopen(rom_path, "rb");
- if (!file)
- return -ENOENT;
- fseek(file, 0, SEEK_END);
- size = ftell(file);
- fseek(file, 0, SEEK_SET);
- rom = malloc((size_t)size);
- if (!rom) {
- fclose(file);
- return -ENOMEM;
- }
- if (fread(rom, 1, (size_t)size, file) != (size_t)size) {
- free(rom);
- fclose(file);
- return -EIO;
- }
- fclose(file);
- ret = gpu_unlock_vbios_fwsec(rom, (size_t)size, &fw);
- free(rom);
- if (ret)
- return ret;
- info->version = fw.version;
- info->image_off = fw.image_off;
- info->image_size = fw.image_size;
- info->data_off = fw.data_off;
- info->imem_size = fw.imem_size;
- info->dmem_size = fw.dmem_size;
- info->interface_off = fw.interface_off;
- return 0;
-}
-
-GD_EXPORT int gd_aes_decrypt_buffer(const uint8_t *key, uint8_t *data, size_t size)
-{
- if (!key || !data)
- return -EINVAL;
- return gd_aes128_decrypt(key, data, size);
-}
-
-GD_EXPORT int gd_test_aes(void)
-{
- return gd_aes128_selftest() ? 1 : 0;
-}
-
-GD_EXPORT int gd_test_wpr2(void)
-{
- return gd_wpr2_selftest();
-}
-
-GD_EXPORT int gd_run_transaction(uint16_t device, uint32_t chip,
- const char *rom_path, const char *asset_root, const char *trace_path, const char *code_path,
- const char *booter_code_path, const uint8_t *key,
- int threads, int verbose,
- struct gd_result *out)
-{
- struct gd_board board = {
- .device = device,
- .chip = chip,
- };
- struct gd_run_config config = {
- .asset_root = asset_root,
- .rom_path = rom_path,
- .verbose = verbose != 0,
- .code_path = code_path,
- .booter_code_path = booter_code_path,
- };
- if (key) {
- memcpy(config.key, key, sizeof(config.key));
- config.key_set = true;
- }
- struct gd_device *dev;
- struct pci_dev pdev;
- struct gu_device gpu;
- struct gpu_unlock_610_shared shared;
- uint64_t mask = 0xffffffffull;
- int ret;
-
- if (!out)
- return -EINVAL;
- memset(out, 0, sizeof(*out));
- memset(&shared, 0, sizeof(shared));
- dev = gd_create(&board, &config);
- if (!dev) {
- out->driver_result = -ENODEV;
- out->device_error = -ENODEV;
- return -ENODEV;
- }
- memset(&pdev, 0, sizeof(pdev));
- pdev.dev.gd = dev;
- pdev.dev.dma_mask = &mask;
- pdev.dev.coherent_dma_mask = 0xffffffffull;
- pdev.vendor = 0x10de;
- pdev.device = device;
- pdev.current_state = 0; /* PCI_D0 */
- pdev.pm_cap = 0;
- pdev.refs = 1;
- gpu.pdev = &pdev;
- gpu.recipe = gu_card_find(device);
- if (!gpu.recipe) {
- out->driver_result = -ENODEV;
- out->device_error = -ENODEV;
- gd_destroy(dev);
- return -ENODEV;
- }
- /* Stage 1 runs the completion path synchronously; stage 2 turns the
- * free-running engine threads on through the same device. */
- gd_set_engines(dev, threads != 0);
-
- ret = gpu_unlock_610_load_shared(&gpu, &shared);
- if (!ret)
- ret = gpu_unlock_610_unlock(&gpu, &shared, false);
- gpu_unlock_610_release_shared(&shared);
- gd_collect(dev, out);
- gd_trace_dump(dev, trace_path);
- out->driver_result = ret;
- gd_destroy(dev);
- return ret;
-}
diff --git a/tests/integration/shim/compat.h b/tests/integration/shim/compat.h
deleted file mode 100644
index 165ffc7..0000000
--- a/tests/integration/shim/compat.h
+++ /dev/null
@@ -1,161 +0,0 @@
-/* SPDX-License-Identifier: GPL-2.0-only */
-/* Userspace kernel API shim for the gpu_unlock driver. Every call below is a
- * plain C call into the device library; no Python and no mock driver. */
-#ifndef GD_SHIM_COMPAT_H
-#define GD_SHIM_COMPAT_H
-
-#include <errno.h>
-#include <stdarg.h>
-#include <stdbool.h>
-#include <stddef.h>
-#include <stdint.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-
-#include "../device/gd.h"
-
-typedef uint8_t u8;
-typedef uint16_t u16;
-typedef uint32_t u32;
-typedef uint64_t u64;
-typedef int32_t s32;
-typedef uint64_t dma_addr_t;
-typedef uint64_t resource_size_t;
-typedef int64_t ktime_t;
-
-#define __iomem
-#define __init
-#define __exit
-#define GFP_KERNEL 0
-#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
-#define min_t(t, a, b) ((t)(a) < (t)(b) ? (t)(a) : (t)(b))
-#define max_t(t, a, b) ((t)(a) > (t)(b) ? (t)(a) : (t)(b))
-#define lower_32_bits(v) ((u32)(v))
-#define upper_32_bits(v) ((u32)((u64)(v) >> 32))
-#define DMA_BIT_MASK(n) ((1ull << (n)) - 1)
-#define MODULE_FIRMWARE(x)
-#define MODULE_LICENSE(x)
-#define MODULE_DESCRIPTION(x)
-#define MODULE_VERSION(x)
-#define MODULE_PARM_DESC(x, y)
-#define THIS_MODULE NULL
-
-/* dev_* receives a struct device *, which always belongs to a pci_dev. */
-#define dev_err(dev, ...) gd_log((dev)->gd, GD_LOG_ERROR, __VA_ARGS__)
-#define dev_info(dev, ...) gd_log((dev)->gd, GD_LOG_INFO, __VA_ARGS__)
-#define dev_dbg(dev, ...) gd_log((dev)->gd, GD_LOG_DEBUG, __VA_ARGS__)
-/* The firmware/driver diagnostics are useful when investigating a failured
- * run but make the acceptance output unreadable. They are opt-in. */
-static inline bool gd_diag_on(void)
-{
- const char *on = getenv("GPU_UNLOCK_DIAG");
- return on && *on && strcmp(on, "0");
-}
-#define pr_info(...) do { if (gd_diag_on()) fprintf(stderr, __VA_ARGS__); } while (0)
-#define pr_err(...) do { if (gd_diag_on()) fprintf(stderr, __VA_ARGS__); } while (0)
-#define pr_debug(...) do { if (gd_diag_on()) fprintf(stderr, __VA_ARGS__); } while (0)
-
-#define PCI_D0 0
-#define PCI_UNKNOWN 5
-#define PCI_DEV_FLAGS_NO_FLR_RESET (1u << 10)
-#define PCI_VENDOR_ID 0x00
-#define PCI_COMMAND 0x04
-#define PCI_COMMAND_MEMORY 0x02
-#define PCI_COMMAND_MASTER 0x04
-#define PCI_PM_CTRL 0x04
-#define PCI_PM_CTRL_STATE_MASK 0x03
-#define PCI_EXP_DEVCAP 0x04
-#define PCI_EXP_DEVCAP_FLR (1u << 28)
-#define PCI_EXP_DEVSTA 0x0a
-#define PCI_EXP_DEVSTA_TRPND 0x20
-#define PCI_BASE_ADDRESS_0 0x10
-#define IORESOURCE_MEM 0x200
-
-struct list_head { unsigned int count; };
-
-static inline bool list_empty(const struct list_head *h)
-{
- return !h->count;
-}
-
-#define spin_lock_irqsave(lock, flags) do { (void)(lock); (flags) = 0; } while (0)
-#define spin_unlock_irqrestore(lock, flags) do { (void)(lock); (void)(flags); } while (0)
-
-struct device {
- struct gd_device *gd;
- u64 *dma_mask;
- u64 coherent_dma_mask;
- struct list_head devres_head;
- int devres_lock;
-};
-
-struct pci_dev {
- struct device dev;
- unsigned short vendor, device;
- int current_state, pm_cap, refs;
- unsigned int dev_flags;
- unsigned int saved_command;
- unsigned int bars[6];
-};
-
-struct firmware {
- size_t size;
- const u8 *data;
-};
-
-void *devres_open_group(struct device *, void *, int);
-static inline void __module_get(void *module) { (void)module; }
-static inline void module_put(void *module) { (void)module; }
-int devres_release_group(struct device *, void *);
-
-void usleep_range(unsigned long min, unsigned long max);
-ktime_t ktime_get(void);
-static inline ktime_t ktime_add_us(ktime_t t, unsigned long us) { return t + (ktime_t)us; }
-static inline int ktime_compare(ktime_t a, ktime_t b) { return a > b ? 1 : a < b ? -1 : 0; }
-
-u32 readl(const void __iomem *address);
-void writel(u32 value, void __iomem *address);
-int memcpy_fromio(void *dest, const void __iomem *source, size_t size);
-
-void *dma_alloc_coherent(struct device *, size_t, dma_addr_t *, int);
-void dma_free_coherent(struct device *, size_t, void *, dma_addr_t);
-int dma_set_mask(struct device *, u64);
-int dma_set_coherent_mask(struct device *, u64);
-void dma_wmb(void);
-
-void *kzalloc(size_t, int);
-static inline void *kvcalloc(size_t n, size_t s, int flags) { (void)flags; return calloc(n, s); }
-static inline void *kvmalloc(size_t n, int flags) { (void)flags; return malloc(n); }
-#define kfree free
-#define kvfree free
-
-int request_firmware(const struct firmware **, const char *, struct device *);
-void release_firmware(const struct firmware *);
-
-int pci_read_config_word(struct pci_dev *, int, u16 *);
-int pci_read_config_dword(struct pci_dev *, int, u32 *);
-int pci_write_config_word(struct pci_dev *, int, u16);
-int pcie_capability_read_word(struct pci_dev *, int, u16 *);
-int pcie_capability_read_dword(struct pci_dev *, int, u32 *);
-int pci_is_pcie(struct pci_dev *);
-int pci_select_bars(struct pci_dev *, int);
-int pci_request_region(struct pci_dev *, int, const char *);
-void pci_release_region(struct pci_dev *, int);
-int aperture_remove_conflicting_pci_devices(struct pci_dev *, const char *);
-int pci_enable_device_mem(struct pci_dev *);
-void pci_disable_device(struct pci_dev *);
-resource_size_t pci_resource_len(struct pci_dev *, int);
-void *pci_iomap(struct pci_dev *, int, unsigned long);
-void pci_iounmap(struct pci_dev *, void *);
-void *pci_map_rom(struct pci_dev *, size_t *);
-void pci_unmap_rom(struct pci_dev *, void *);
-int pci_save_state(struct pci_dev *);
-void pci_restore_state(struct pci_dev *);
-void pci_set_master(struct pci_dev *);
-void pci_clear_master(struct pci_dev *);
-int pcie_flr(struct pci_dev *);
-struct pci_dev *pci_dev_get(struct pci_dev *);
-void pci_dev_put(struct pci_dev *);
-
-#endif
diff --git a/tests/integration/shim/linux/aperture.h b/tests/integration/shim/linux/aperture.h
deleted file mode 100644
index 4715430..0000000
--- a/tests/integration/shim/linux/aperture.h
+++ /dev/null
@@ -1,2 +0,0 @@
-/* SPDX-License-Identifier: GPL-2.0-only */
-#include "../compat.h"
diff --git a/tests/integration/shim/linux/delay.h b/tests/integration/shim/linux/delay.h
deleted file mode 100644
index 4715430..0000000
--- a/tests/integration/shim/linux/delay.h
+++ /dev/null
@@ -1,2 +0,0 @@
-/* SPDX-License-Identifier: GPL-2.0-only */
-#include "../compat.h"
diff --git a/tests/integration/shim/linux/device.h b/tests/integration/shim/linux/device.h
deleted file mode 100644
index 4715430..0000000
--- a/tests/integration/shim/linux/device.h
+++ /dev/null
@@ -1,2 +0,0 @@
-/* SPDX-License-Identifier: GPL-2.0-only */
-#include "../compat.h"
diff --git a/tests/integration/shim/linux/dma-mapping.h b/tests/integration/shim/linux/dma-mapping.h
deleted file mode 100644
index 4715430..0000000
--- a/tests/integration/shim/linux/dma-mapping.h
+++ /dev/null
@@ -1,2 +0,0 @@
-/* SPDX-License-Identifier: GPL-2.0-only */
-#include "../compat.h"
diff --git a/tests/integration/shim/linux/firmware.h b/tests/integration/shim/linux/firmware.h
deleted file mode 100644
index 4715430..0000000
--- a/tests/integration/shim/linux/firmware.h
+++ /dev/null
@@ -1,2 +0,0 @@
-/* SPDX-License-Identifier: GPL-2.0-only */
-#include "../compat.h"
diff --git a/tests/integration/shim/linux/io.h b/tests/integration/shim/linux/io.h
deleted file mode 100644
index 4715430..0000000
--- a/tests/integration/shim/linux/io.h
+++ /dev/null
@@ -1,2 +0,0 @@
-/* SPDX-License-Identifier: GPL-2.0-only */
-#include "../compat.h"
diff --git a/tests/integration/shim/linux/list.h b/tests/integration/shim/linux/list.h
deleted file mode 100644
index 4715430..0000000
--- a/tests/integration/shim/linux/list.h
+++ /dev/null
@@ -1,2 +0,0 @@
-/* SPDX-License-Identifier: GPL-2.0-only */
-#include "../compat.h"
diff --git a/tests/integration/shim/linux/module.h b/tests/integration/shim/linux/module.h
deleted file mode 100644
index 4715430..0000000
--- a/tests/integration/shim/linux/module.h
+++ /dev/null
@@ -1,2 +0,0 @@
-/* SPDX-License-Identifier: GPL-2.0-only */
-#include "../compat.h"
diff --git a/tests/integration/shim/linux/pci.h b/tests/integration/shim/linux/pci.h
deleted file mode 100644
index 4715430..0000000
--- a/tests/integration/shim/linux/pci.h
+++ /dev/null
@@ -1,2 +0,0 @@
-/* SPDX-License-Identifier: GPL-2.0-only */
-#include "../compat.h"
diff --git a/tests/integration/shim/linux/printk.h b/tests/integration/shim/linux/printk.h
deleted file mode 100644
index b5a2194..0000000
--- a/tests/integration/shim/linux/printk.h
+++ /dev/null
@@ -1,3 +0,0 @@
-/* SPDX-License-Identifier: GPL-2.0-only */
-#include <stdio.h>
-#include "../compat.h"
diff --git a/tests/integration/shim/linux/slab.h b/tests/integration/shim/linux/slab.h
deleted file mode 100644
index 4715430..0000000
--- a/tests/integration/shim/linux/slab.h
+++ /dev/null
@@ -1,2 +0,0 @@
-/* SPDX-License-Identifier: GPL-2.0-only */
-#include "../compat.h"
diff --git a/tests/integration/shim/linux/string.h b/tests/integration/shim/linux/string.h
deleted file mode 100644
index 3b2f590..0000000
--- a/tests/integration/shim/linux/string.h
+++ /dev/null
@@ -1 +0,0 @@
-#include <string.h>
diff --git a/tests/integration/shim/linux/timekeeping.h b/tests/integration/shim/linux/timekeeping.h
deleted file mode 100644
index 4715430..0000000
--- a/tests/integration/shim/linux/timekeeping.h
+++ /dev/null
@@ -1,2 +0,0 @@
-/* SPDX-License-Identifier: GPL-2.0-only */
-#include "../compat.h"
diff --git a/tests/integration/shim/linux/types.h b/tests/integration/shim/linux/types.h
deleted file mode 100644
index 4715430..0000000
--- a/tests/integration/shim/linux/types.h
+++ /dev/null
@@ -1,2 +0,0 @@
-/* SPDX-License-Identifier: GPL-2.0-only */
-#include "../compat.h"
diff --git a/tests/integration/shim/linux/version.h b/tests/integration/shim/linux/version.h
deleted file mode 100644
index 59cacba..0000000
--- a/tests/integration/shim/linux/version.h
+++ /dev/null
@@ -1,3 +0,0 @@
-/* SPDX-License-Identifier: GPL-2.0-only */
-#define KERNEL_VERSION(a, b, c) (((a) << 16) + ((b) << 8) + (c))
-#define LINUX_VERSION_CODE KERNEL_VERSION(6, 0, 0)
diff --git a/tests/integration/shim/shim.c b/tests/integration/shim/shim.c
deleted file mode 100644
index d14f417..0000000
--- a/tests/integration/shim/shim.c
+++ /dev/null
@@ -1,383 +0,0 @@
-// SPDX-License-Identifier: GPL-2.0-only
-/* Userspace kernel shim: every operation is a plain C call into the device. */
-#include <errno.h>
-#include <pthread.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <time.h>
-
-#include "compat.h"
-
-struct bar_window {
- void *base;
- struct gd_device *dev;
-};
-
-static struct bar_window windows[8];
-static pthread_mutex_t window_lock = PTHREAD_MUTEX_INITIALIZER;
-
-static struct bar_window *window_for(const void *address)
-{
- unsigned int i;
- for (i = 0; i < ARRAY_SIZE(windows); i++) {
- struct bar_window *w = &windows[i];
- if (w->base && (const char *)address >= (const char *)w->base &&
- (const char *)address < (const char *)w->base + GD_BAR_SIZE)
- return w;
- }
- return NULL;
-}
-
-static u32 pci_command(struct pci_dev *pdev)
-{
- u32 command = 0;
- gd_pci_read(pdev->dev.gd, 0, PCI_COMMAND, 2, &command);
- return command;
-}
-
-static void pci_set_command(struct pci_dev *pdev, u32 command)
-{
- gd_pci_write(pdev->dev.gd, 0, PCI_COMMAND, 2, command);
-}
-
-void *devres_open_group(struct device *dev, void *id, int flags)
-{
- (void)flags;
- unsigned int *group;
- if (id)
- return NULL;
- group = malloc(sizeof(*group));
- if (!group)
- return NULL;
- *group = dev->devres_head.count;
- dev->devres_head.count++;
- return group;
-}
-
-int devres_release_group(struct device *dev, void *id)
-{
- unsigned int *group = id;
- int released;
- if (!group)
- return 0;
- released = (int)dev->devres_head.count - (int)*group - 1;
- if (released < 0)
- released = 0;
- dev->devres_head.count = *group;
- free(group);
- return released;
-}
-
-ktime_t ktime_get(void)
-{
- struct timespec ts;
- clock_gettime(CLOCK_MONOTONIC, &ts);
- return (ktime_t)ts.tv_sec * 1000000 + ts.tv_nsec / 1000;
-}
-
-void usleep_range(unsigned long min, unsigned long max)
-{
- struct timespec ts;
- (void)max;
- ts.tv_sec = min / 1000000;
- ts.tv_nsec = (min % 1000000) * 1000;
- while (nanosleep(&ts, &ts) == -1 && errno == EINTR)
- ;
-}
-
-u32 readl(const void __iomem *address)
-{
- struct bar_window *w = window_for(address);
- if (!w)
- return 0xffffffffu;
- return gd_bar0_read(w->dev, (u32)((const char *)address - (const char *)w->base));
-}
-
-void writel(u32 value, void __iomem *address)
-{
- struct bar_window *w = window_for(address);
- if (!w)
- return;
- gd_bar0_write(w->dev, (u32)((char *)address - (char *)w->base), value);
-}
-
-int memcpy_fromio(void *dest, const void __iomem *source, size_t size)
-{
- struct bar_window *w = window_for(source);
- if (!w)
- return -EINVAL;
- return gd_prom_copy(w->dev, (u32)((const char *)source - (const char *)w->base), dest, size);
-}
-
-void *dma_alloc_coherent(struct device *dev, size_t size, dma_addr_t *address, int flags)
-{
- static uint64_t next = 0x10000000;
- void *cpu;
- (void)flags;
- size = (size + 255) & ~(size_t)255;
- if (!size)
- size = 256;
- cpu = calloc(1, size);
- if (!cpu)
- return NULL;
- *address = next;
- next += (size + 0xfff) & ~0xfffull;
- if (gd_dma_map(dev->gd, *address, cpu, size)) {
- free(cpu);
- return NULL;
- }
- return cpu;
-}
-
-void dma_free_coherent(struct device *dev, size_t size, void *cpu, dma_addr_t address)
-{
- (void)size;
- gd_dma_unmap(dev->gd, address);
- free(cpu);
-}
-
-int dma_set_mask(struct device *dev, u64 mask)
-{
- if (mask < 0xffffffffull)
- return -EIO;
- *dev->dma_mask = mask;
- return 0;
-}
-
-int dma_set_coherent_mask(struct device *dev, u64 mask)
-{
- if (mask < 0xffffffffull)
- return -EIO;
- dev->coherent_dma_mask = mask;
- return 0;
-}
-
-void dma_wmb(void)
-{
- __sync_synchronize();
-}
-
-void *kzalloc(size_t size, int flags)
-{
- (void)flags;
- return calloc(1, size ? size : 1);
-}
-
-int request_firmware(const struct firmware **out, const char *name, struct device *dev)
-{
- char path[4096];
- const char *base = strrchr(name, '/');
- struct firmware *fw;
- FILE *file;
- long size;
-
- base = base ? base + 1 : name;
- snprintf(path, sizeof(path), "%s/%s", gd_asset_root(dev->gd), base);
- file = fopen(path, "rb");
- if (!file)
- return -ENOENT;
- if (fseek(file, 0, SEEK_END) || (size = ftell(file)) <= 0 || fseek(file, 0, SEEK_SET)) {
- fclose(file);
- return -EIO;
- }
- fw = malloc(sizeof(*fw));
- if (!fw) {
- fclose(file);
- return -ENOMEM;
- }
- fw->size = (size_t)size;
- fw->data = malloc(fw->size);
- if (!fw->data) {
- free(fw);
- fclose(file);
- return -ENOMEM;
- }
- if (fread((void *)fw->data, 1, fw->size, file) != fw->size) {
- free((void *)fw->data);
- free(fw);
- fclose(file);
- return -EIO;
- }
- fclose(file);
- *out = fw;
- return 0;
-}
-
-void release_firmware(const struct firmware *fw)
-{
- if (!fw)
- return;
- free((void *)fw->data);
- free((void *)fw);
-}
-
-int pci_read_config_word(struct pci_dev *pdev, int offset, u16 *value)
-{
- u32 v;
- int ret = gd_pci_read(pdev->dev.gd, 0, offset, 2, &v);
- *value = (u16)v;
- return ret;
-}
-
-int pci_read_config_dword(struct pci_dev *pdev, int offset, u32 *value)
-{
- return gd_pci_read(pdev->dev.gd, 0, offset, 4, value);
-}
-
-int pci_write_config_word(struct pci_dev *pdev, int offset, u16 value)
-{
- return gd_pci_write(pdev->dev.gd, 0, offset, 2, value);
-}
-
-int pcie_capability_read_word(struct pci_dev *pdev, int offset, u16 *value)
-{
- u32 v;
- int ret = gd_pci_read(pdev->dev.gd, 1, offset, 2, &v);
- *value = (u16)v;
- return ret;
-}
-
-int pcie_capability_read_dword(struct pci_dev *pdev, int offset, u32 *value)
-{
- return gd_pci_read(pdev->dev.gd, 1, offset, 4, value);
-}
-
-int pci_is_pcie(struct pci_dev *pdev)
-{
- (void)pdev;
- return 1;
-}
-
-int pci_select_bars(struct pci_dev *pdev, int flags)
-{
- (void)pdev;
- (void)flags;
- return 1;
-}
-
-int pci_request_region(struct pci_dev *pdev, int bar, const char *name)
-{
- (void)pdev;
- (void)bar;
- (void)name;
- return 0;
-}
-
-void pci_release_region(struct pci_dev *pdev, int bar)
-{
- (void)pdev;
- (void)bar;
-}
-
-int aperture_remove_conflicting_pci_devices(struct pci_dev *pdev, const char *name)
-{
- (void)pdev;
- (void)name;
- return 0;
-}
-
-int pci_enable_device_mem(struct pci_dev *pdev)
-{
- pci_set_command(pdev, pci_command(pdev) | PCI_COMMAND_MEMORY);
- return 0;
-}
-
-void pci_disable_device(struct pci_dev *pdev)
-{
- pci_set_command(pdev, pci_command(pdev) & ~PCI_COMMAND_MEMORY);
-}
-
-resource_size_t pci_resource_len(struct pci_dev *pdev, int bar)
-{
- (void)pdev;
- (void)bar;
- return GD_BAR_SIZE;
-}
-
-void *pci_iomap(struct pci_dev *pdev, int bar, unsigned long maxlen)
-{
- unsigned int i;
- void *base;
- (void)bar;
- (void)maxlen;
- base = gd_bar_pointer(pdev->dev.gd);
- pthread_mutex_lock(&window_lock);
- for (i = 0; i < ARRAY_SIZE(windows); i++) {
- if (!windows[i].base) {
- windows[i].base = base;
- windows[i].dev = pdev->dev.gd;
- pthread_mutex_unlock(&window_lock);
- return base;
- }
- }
- pthread_mutex_unlock(&window_lock);
- return NULL;
-}
-
-void pci_iounmap(struct pci_dev *pdev, void *address)
-{
- unsigned int i;
- (void)pdev;
- pthread_mutex_lock(&window_lock);
- for (i = 0; i < ARRAY_SIZE(windows); i++) {
- if (windows[i].base == address) {
- windows[i].base = NULL;
- windows[i].dev = NULL;
- }
- }
- pthread_mutex_unlock(&window_lock);
-}
-
-void *pci_map_rom(struct pci_dev *pdev, size_t *size)
-{
- (void)pdev;
- if (size)
- *size = 0;
- return NULL;
-}
-
-void pci_unmap_rom(struct pci_dev *pdev, void *rom)
-{
- (void)pdev;
- (void)rom;
-}
-
-int pci_save_state(struct pci_dev *pdev)
-{
- pdev->saved_command = pci_command(pdev);
- return 0;
-}
-
-void pci_restore_state(struct pci_dev *pdev)
-{
- pci_set_command(pdev, pdev->saved_command);
-}
-
-void pci_set_master(struct pci_dev *pdev)
-{
- pci_set_command(pdev, pci_command(pdev) | PCI_COMMAND_MASTER);
-}
-
-void pci_clear_master(struct pci_dev *pdev)
-{
- pci_set_command(pdev, pci_command(pdev) & ~PCI_COMMAND_MASTER);
-}
-
-int pcie_flr(struct pci_dev *pdev)
-{
- return gd_reset(pdev->dev.gd);
-}
-
-struct pci_dev *pci_dev_get(struct pci_dev *pdev)
-{
- if (pdev)
- __atomic_add_fetch(&pdev->refs, 1, __ATOMIC_SEQ_CST);
- return pdev;
-}
-
-void pci_dev_put(struct pci_dev *pdev)
-{
- if (pdev)
- __atomic_sub_fetch(&pdev->refs, 1, __ATOMIC_SEQ_CST);
-}
diff --git a/tests/integration/test_integration.py b/tests/integration/test_integration.py
deleted file mode 100644
index 654417f..0000000
--- a/tests/integration/test_integration.py
+++ /dev/null
@@ -1,119 +0,0 @@
-# SPDX-License-Identifier: GPL-2.0-only
-"""Behavioral gates for the native integration runner.
-
-The keyed GA10x run executes the real FWSEC and SEC2 Booter and injects the
-real V67 payload. The payload does not yet reach a clean Booter halt, so the
-final unlock checks fail honestly while the FWSEC and Booter execution evidence
-is still asserted.
-"""
-from __future__ import annotations
-
-import os
-from contextlib import contextmanager
-from pathlib import Path
-
-from . import build, hooks
-from .run import load_runner, run_transaction
-
-ROOT = Path(__file__).resolve().parents[2]
-RUNS = ROOT / "build" / "runs" / "tests"
-FAILURES: list[str] = []
-CHECKS = 0
-
-
-def check(condition, message, detail=None, fail=None):
- global CHECKS
-
- CHECKS += 1
- if not condition:
- text = fail or f"not: {message}"
- FAILURES.append(text)
- print("FAIL:", text)
- if detail:
- print(" ", detail)
- else:
- print("ok:", message)
-
-
-@contextmanager
-def section(title):
- print(f"\n== {title} ==")
- checks = CHECKS
- failures = len(FAILURES)
- yield
- count = CHECKS - checks
- print(f"-- {title}: {count} check{'s' if count != 1 else ''}, "
- f"{len(FAILURES) - failures} failures")
-
-
-def report(record, label):
- print(f"[{label}] driver={record['driver_result']} stage={record['stage']} "
- f"launches={record['launches']} unlocked={record['unlock_verified']}")
- print(f" fwsec={record['fwsec_executed']} mac={record['fwsec_mac_ok']} "
- f"booter_insns={record['instructions'][0]} "
- f"halt={record['booter_halt_pc']:#x} fault={record['fault_pc']:#x} "
- f"status={record['booter_status']} reset_plm={record['booter_reset_plm']:#x}")
-
-
-def main():
- print("gpu-unlock integration tests")
- print("driver diagnostics: off (set GPU_UNLOCK_DIAG=1 to show them)")
- RUNS.mkdir(parents=True, exist_ok=True)
-
- with section("build"):
- lib = build.build()
- print(f"library: {lib}")
-
- with section("unit self-tests"):
- runner = load_runner(lib)
- check(runner.gd_test_aes() == 1, "AES-128 FIPS-197 vector round-trips")
- check(runner.gd_test_wpr2() == 0, "WPR2 encoding and range vectors pass")
- check(hooks.check() == [], "TOML inventory and compiled hooks agree")
-
- key = os.environ.get("GPU_UNLOCK_DEBUG_KEY")
- if not key:
- check(False, "real path: GPU_UNLOCK_DEBUG_KEY is required")
- return 1
-
- with section("real path: FWSEC + Booter + V67 payload"):
- record = run_transaction("GA10x", RUNS / "GA10x", threads=True, library=lib)
- report(record, "GA10x real")
- check(record["fwsec_launch_reached"], "FWSEC launch observed")
- check(record["instructions"][1] > 1000, "real FWSEC executed")
- check(record["fwsec_executed"], "real FWSEC completed")
- check(record["fwsec_mac_ok"], "real FWSEC DMEM MAC matched")
- check(record["fwsec_kdf_ok"], "SCP op4 KDF produced the debug MAC")
- check(record["fwsec_status"] == 0, "real FWSEC left a clean status")
- check(record["booter_launch_reached"], "real Booter launch observed")
- check(record["instructions"][0] > 1000, "real Booter executed")
- check(record["booter_halt_pc"] != 0 or record["fault_pc"] != 0,
- "real Booter stop PC recorded")
- check(Path(record["run_dir"], "trace.jsonl").exists(), "trace decoded")
- # Final acceptance. Report each failed precondition with the observed
- # state so the verdict is readable without decoding raw booleans.
- check(record["booter_halted"] and record["booter_status"] == 0 and
- (record["booter_reset_plm"] & 0xff) == 0xff,
- "real payload: Booter halted cleanly",
- f"halted={record['booter_halted']} status={record['booter_status']:#x} "
- f"reset_plm={record['booter_reset_plm']:#x} "
- f"halt_pc={record['booter_halt_pc']:#x} fault_pc={record['fault_pc']:#x}",
- fail="real payload: Booter did not halt cleanly")
- check(record["target_written"], "real payload: V67 wrote the target",
- f"target_written={record['target_written']} "
- f"plm_opened={record['plm_opened']}",
- fail="real payload: V67 did not write the target")
- check(record["unlock_verified"], "real payload: card reached the unlocked state",
- "requires a clean Booter halt, PLM open, target write, "
- f"selectors 0x88888888/0x8 and RESET_PLM=0xffffffff; observed "
- f"plm_opened={record['plm_opened']} "
- f"target_written={record['target_written']} "
- f"selectors={record['selectors'][0]:#010x}/{record['selectors'][1]:#x} "
- f"reset_plm={record['booter_reset_plm']:#x}",
- fail="real payload: card is not unlocked")
-
- print(f"\n{len(FAILURES)} failure(s)")
- return 1 if FAILURES else 0
-
-
-if __name__ == "__main__":
- raise SystemExit(main())