1package drm
2
3import (
4 "os"
5 "sync/atomic"
6 "time"
7
8 "github.com/bnema/neferwl/internal/ports"
9)
10
11// frameLifecycle is owned by Output.Run. KMS and the event reader do not
12// schedule frames: Run feeds this state with commit results, flip serials and
13// deadline ticks. Only Run uses the timer; the transitions take plain values.
14type frameLifecycle struct {
15 pending bool
16 pendingFrame pendingFrame
17 serials *atomic.Uint64 // shared across outputs on the card
18 pendingSerial, nextSerial uint64
19 flipStart time.Time
20 stuckAfter time.Duration
21 stuckAt time.Time
22 fenceWarned bool
23 busySince, lastBusy time.Time
24 stuckTimer *time.Timer
25}
26
27// pendingFrame is what the pending frame commit shows.
28type pendingFrame struct {
29 security ports.SecurityState // admission epoch checked again at KMS boundary
30 frame bool
31 queued uint64
32 zeroCopy ports.WindowID // window shown without composition
33 async bool
34 shows map[ports.WindowID]uint64
35 // fences are duplicates of the fences the commit waits on, owned until
36 // the commit ends. composed is set when the frame was rendered.
37 fences []*os.File
38 composed bool
39 // fenceReady: every fence had signalled at commit (flip tracing only).
40 fenceReady bool
41}
42
43func (f *pendingFrame) closeFences() {
44 for _, fd := range f.fences {
45 fd.Close()
46 }
47 f.fences = nil
48}
49
50// Commit event userData: the commit serial above userKindBits, the kind below.
51const (
52 userFrame = 1
53 userState = 2
54 userKindBits = 2
55)
56
57const (
58 stuckTimeout = time.Second
59 busyRetry = 50 * time.Millisecond
60)
61
62// stuckLimit returns the configured recovery deadline or its default.
63func (l *frameLifecycle) stuckLimit() time.Duration {
64 if l.stuckAfter > 0 {
65 return l.stuckAfter
66 }
67 return stuckTimeout
68}
69
70// userData numbers a commit across all outputs of the card.
71func (l *frameLifecycle) userData(kind uint64) uint64 {
72 l.nextSerial = l.serials.Add(1)
73 return l.nextSerial<<userKindBits | kind
74}
75
76// endPending releases the in-flight commit and its duplicated fences.
77func (l *frameLifecycle) endPending() {
78 l.pendingFrame.closeFences()
79 l.pending, l.pendingFrame = false, pendingFrame{}
80}
81
82// begin records a successful nonblocking commit awaiting its event.
83func (l *frameLifecycle) begin(f pendingFrame, now time.Time) {
84 l.pendingSerial = l.nextSerial
85 l.pendingFrame.closeFences()
86 l.pending, l.pendingFrame, l.flipStart = true, f, now
87 l.stuckAt, l.fenceWarned = now.Add(l.stuckLimit()), false
88 l.busySince = time.Time{}
89}
90
91// pendingCommit reports whether the output must wait before another commit.
92func (l *frameLifecycle) pendingCommit() bool { return l.pending }
93
94// resetAfterModeset ends a replaced commit and clears the old busy run.
95func (l *frameLifecycle) resetAfterModeset() {
96 l.endPending()
97 l.busySince = time.Time{}
98}
99
100// deadlineReached reports whether a pending commit is ready for recovery.
101func (l *frameLifecycle) deadlineReached(now time.Time) bool {
102 return l.pending && !now.Before(l.stuckAt)
103}
104
105// deadlineInfo is the pending commit's log context and fences at a tick.
106// The caller polls the fences; the transition itself consumes a plain bool.
107func (l *frameLifecycle) deadlineInfo(now time.Time) (serial uint64, frame bool, age, busy time.Duration, fences []*os.File) {
108 return l.pendingSerial, l.pendingFrame.frame, now.Sub(l.flipStart), now.Sub(l.busySince), l.pendingFrame.fences
109}
110
111// busy records an EBUSY refusal. No commit of ours is known to be in
112// flight: any event of this CRTC may unblock the retry.
113// It returns true when this is a new run, for the caller's log.
114func (l *frameLifecycle) busy(now time.Time) bool {
115 l.endPending()
116 l.pending, l.flipStart, l.stuckAt = true, now, now.Add(busyRetry)
117 l.pendingSerial = 0
118 newRun := l.busySince.IsZero() || now.Sub(l.lastBusy) > 4*busyRetry
119 if newRun {
120 l.busySince = now
121 }
122 l.lastBusy = now
123 return newRun
124}
125
126// ours reports whether user is the event of the pending commit with a
127// known serial (not the end of an EBUSY wait).
128func (l *frameLifecycle) ours(user uint64) bool {
129 return l.pending && l.pendingSerial != 0 && user>>userKindBits == l.pendingSerial
130}
131
132// flip consumes only the event for the pending commit; while waiting after
133// EBUSY there is no known serial, so any CRTC event ends that wait. The
134// returned start lets Output measure flip age after pending fences close.
135func (l *frameLifecycle) flip(user uint64) (pendingFrame, time.Time, bool) {
136 if !l.pending || l.pendingSerial != 0 && user>>userKindBits != l.pendingSerial {
137 return pendingFrame{}, time.Time{}, false
138 }
139 f, start := l.pendingFrame, l.flipStart
140 l.endPending()
141 l.busySince = time.Time{}
142 return f, start, true
143}
144
145// timeoutResult describes the action after a deadline tick.
146type timeoutResult uint8
147
148const (
149 timeoutEarly timeoutResult = iota // deadline not reached (or a warned fence still waits)
150 timeoutFence // first wait for an unsignalled fence
151 timeoutRetry // EBUSY without an event: retry the commit
152 timeoutBusy // EBUSY past its limit: modeset
153 timeoutMissing // committed event missing: modeset
154)
155
156// timeout takes the current fence status as a value. The caller polls only
157// when a known commit reaches its deadline; no clock, fence or event source
158// is mocked in transition tests.
159func (l *frameLifecycle) timeout(now time.Time, fencesReady bool) timeoutResult {
160 if !l.pending || now.Before(l.stuckAt) {
161 return timeoutEarly
162 }
163 if l.pendingSerial != 0 && !fencesReady {
164 l.stuckAt = now.Add(l.stuckLimit())
165 if !l.fenceWarned {
166 l.fenceWarned = true
167 return timeoutFence
168 }
169 return timeoutEarly
170 }
171 l.endPending()
172 if l.pendingSerial != 0 {
173 return timeoutMissing
174 }
175 if l.busySince.IsZero() || now.Sub(l.busySince) < l.stuckLimit() {
176 return timeoutRetry
177 }
178 return timeoutBusy
179}
180
181// startTimer creates the per-output deadline timer for Run, not kms.
182func (l *frameLifecycle) startTimer() {
183 l.stuckTimer = time.NewTimer(time.Hour)
184 l.stuckTimer.Stop()
185}
186
187// wait arms the pending deadline, or disables it while idle or away.
188// Go 1.27 guarantees a stopped/reset timer cannot deliver an old value.
189func (l *frameLifecycle) wait(enabled bool) <-chan time.Time {
190 if !enabled || !l.pending {
191 l.stuckTimer.Stop()
192 return nil
193 }
194 l.stuckTimer.Reset(max(0, time.Until(l.stuckAt)))
195 return l.stuckTimer.C
196}
197
198// stopTimer releases the timer when Run exits.
199func (l *frameLifecycle) stopTimer() { l.stuckTimer.Stop() }