omega-gpu-driver
aien-dev/omega-gpu-driver
Brought by drakestapleton
omega-gpu-driver / window / d2-run.sh
| 1 | #!/bin/sh |
| 2 | # window/d2-run.sh --go W0|D2|D3 -- W2..W7, run by the boot unit after the reboot that d2-pre.sh --go started. |
| 3 | # Status: NOT_RUN. |
| 4 | # W2 state "restore" and remove the hold-back file FIRST (any later reboot is a normal one that ends in R), |
| 5 | # check no nvidia module, kernel.panic=10 + panic_on_oops=1 (an oops reboots into R), bind vfio-pci |
| 6 | # W3 runtime PM on and active, build/w0 (config identity, BOOT_0/BOOT_1/BOOT_42, read-only), build/ogd ident |
| 7 | # D2: W4 build/ogd boot (GSP boot through SEC2 to INIT_DONE, static info, unload) |
| 8 | # W5 build/ogd hello (objects, channel, one semaphore release, teardown) |
| 9 | # D3: W6 build/ogd exec -- <CUDApp gate> (needs ogd 'exec' and CUDApp's aien provider; NOT_RUN without them) |
| 10 | # Each driver step runs only if build/ogd lists it under 'modes'; otherwise it is logged NOT_RUN. |
| 11 | # W7 unbind vfio-pci (releasing the device SBRs root port 000f:00:00.0, which resets the GPU), clear |
| 12 | # driver_override, power/control=auto, |
| 13 | # then by default reboot (the boot unit runs R), or, if W1 was given RESTORE_PATH=rebind (window/restore-path), |
| 14 | # run window/d2-restore.sh --go now (modprobe nvidia, bind; reboot fallback once). |
| 15 | # Any failure: the EXIT trap does W7. No privilege is granted: /dev/vfio/$GRP stays root 0600 and the |
| 16 | # driver programs run as root through sudo -n. |
| 17 | set -eu |
| 18 | R=$(CDPATH= cd -- "$(dirname "$0")/.." && pwd) |
| 19 | . "$R/window/common.sh" |
| 20 | LOG=$LOGDIR/d2-$(date -u +%Y%m%dT%H%M%SZ)-run.log |
| 21 | [ "${1:-}" = --go ] || { echo "usage: $0 --go W0|D2|D3 (window only)"; exit 2; } |
| 22 | STAGE=${2:-} |
| 23 | case $STAGE in W0 | D2 | D3) ;; *) echo "usage: $0 --go W0|D2|D3"; exit 2 ;; esac |
| 24 | |
| 25 | # W2 ------------------------------------------------------------------------- |
| 26 | release() { |
| 27 | rc=$? |
| 28 | trap - EXIT |
| 29 | log "W7 release (rc=$rc)" |
| 30 | if [ "$(drv)" = vfio-pci ]; then echo "$BDF" | sudo -n tee "$PCI/drivers/vfio-pci/unbind" >/dev/null || true; fi |
| 31 | echo | sudo -n tee "$DEV/driver_override" >/dev/null || true |
| 32 | pm_auto |
| 33 | log "W7 driver=$(drv) override='$(cat "$DEV/driver_override")' power=$(cat "$DEV/power/control")" |
| 34 | if [ "$STAGE" != W0 ] && [ "$(cat "$RPATH" 2>/dev/null || true)" = rebind ]; then |
| 35 | board "$STAGE run ended rc=$rc, log $LOG; restoring by rebind (reboot fallback inside d2-restore)" |
| 36 | exec sh "$R/window/d2-restore.sh" --go |
| 37 | fi |
| 38 | board "$STAGE run ended rc=$rc, log $LOG; rebooting into the normal nvidia boot; the boot unit runs R" |
| 39 | sync |
| 40 | sudo -n systemctl reboot |
| 41 | } |
| 42 | # From here on every exit, including a refusal below, goes through W7 and ends in R. |
| 43 | if [ "$STAGE" = W0 ]; then |
| 44 | echo "restore W0" > "$STATE" |
| 45 | else |
| 46 | echo restore > "$STATE" |
| 47 | fi |
| 48 | trap release EXIT |
| 49 | sudo -n rm -f "$HOLD" |
| 50 | [ ! -e "$HOLD" ] && log "W2 ($STAGE) state 'restore'; hold-back removed: the next boot loads nvidia again and runs R" || |
| 51 | die "could not remove $HOLD (R0 removes it after the reboot)" |
| 52 | need_approval |
| 53 | exec 9>>"$GB10_LOCK" |
| 54 | flock -n 9 || die "$GB10_LOCK is held: something started a GPU job after the reboot" |
| 55 | if [ "$(nv_modules)" -ne 0 ]; then die "nvidia is loaded: the hold-back did not take"; fi |
| 56 | # Recheck after reboot, but only after recovery is armed and before any VFIO binding. |
| 57 | if [ "$STAGE" = W0 ]; then |
| 58 | case $(cat "$IDENTITY") in up | down) ;; *) die "missing identity service state" ;; esac |
| 59 | [ ! -e "$BUILD/ogd" ] && [ ! -L "$BUILD/ogd" ] || die "identity-only checkout contains build/ogd" |
| 60 | sha256sum -c --quiet "$W0SUM" || die "identity executable changed" |
| 61 | else |
| 62 | [ "$STAGE" = D2 ] || has_mode exec || die "D3 needs build/ogd with a successful exact 'exec' mode" |
| 63 | fi |
| 64 | |
| 65 | |
| 66 | sudo -n sysctl -q kernel.panic=10 kernel.panic_on_oops=1 |
| 67 | log "kernel.panic=10 panic_on_oops=1 (saved values in $SAVED, R puts them back)" |
| 68 | # Run one driver program, keep its output in the log, fail the window step on its exit code. |
| 69 | run_logged() { |
| 70 | o=$("$@" 2>&1) && rc=0 || rc=$? |
| 71 | printf '%s\n' "$o" | tee -a "$LOG" |
| 72 | [ "$rc" -eq 0 ] || die "$* exit $rc" |
| 73 | } |
| 74 | |
| 75 | sudo -n modprobe vfio-pci |
| 76 | ischr "$VFIODEV/vfio" || die "/dev/vfio/vfio missing" |
| 77 | echo vfio-pci | sudo -n tee "$DEV/driver_override" >/dev/null |
| 78 | echo "$BDF" | sudo -n tee "$PCI/drivers_probe" >/dev/null |
| 79 | [ "$(drv)" = vfio-pci ] || die "driver is $(drv), not vfio-pci" |
| 80 | ischr "$VFIODEV/$GRP" || die "/dev/vfio/$GRP missing" |
| 81 | m=$(stat -c '%U:%G:%a' "$VFIODEV/$GRP") |
| 82 | [ "$m" = root:root:600 ] || die "/dev/vfio/$GRP is $m, expected root:root:600" |
| 83 | log "W2 PASS: $BDF on vfio-pci, /dev/vfio/$GRP root:root:600 (programs run as root via sudo -n)" |
| 84 | |
| 85 | # W3 ------------------------------------------------------------------------- |
| 86 | pm_on |
| 87 | run_logged sudo -n "$BUILD/w0" "$BDF" "$GRP" |
| 88 | if [ "$STAGE" = W0 ]; then |
| 89 | log "W3 PASS: identity read; closing the identity-only window" |
| 90 | exit 0 |
| 91 | fi |
| 92 | if has_mode ident; then |
| 93 | pm_on |
| 94 | run_logged sudo -n "$BUILD/ogd" ident "$BDF" "$GRP" "$FW" "$BUILD/fw" |
| 95 | fi |
| 96 | log "W3 PASS: identity read (BOOT_42 above settles the chip id)" |
| 97 | |
| 98 | # W4..W6 ---------------------------------------------------------------------- |
| 99 | if [ "$STAGE" = D2 ]; then steps="boot hello"; else steps="exec"; fi |
| 100 | for step in $steps; do |
| 101 | if ! has_mode "$step"; then |
| 102 | log "W-$step NOT_RUN: build/ogd has no '$step' mode" |
| 103 | continue |
| 104 | fi |
| 105 | pm_on |
| 106 | log "W-$step start" |
| 107 | if [ "$step" = exec ]; then |
| 108 | D3_CMD=$(cat "$LOGDIR/d3-cmd" 2>/dev/null || true) |
| 109 | [ -n "$D3_CMD" ] || die "D3 needs $LOGDIR/d3-cmd (the CUDApp gate command line, written by d2-pre.sh --go D3)" |
| 110 | # shellcheck disable=SC2086 |
| 111 | run_logged sudo -n "$BUILD/ogd" exec "$BDF" "$GRP" "$FW" "$BUILD/fw" -- $D3_CMD |
| 112 | else |
| 113 | run_logged sudo -n "$BUILD/ogd" "$step" "$BDF" "$GRP" "$FW" "$BUILD/fw" |
| 114 | fi |
| 115 | log "W-$step PASS" |
| 116 | done |
| 117 | log "W7 release" |
| 118 | # the EXIT trap does W7 |