Files
geramyloveless 347a44ec16 display: no op that can sleep runs on a client's call
On 241 a GPU ring hung (no recovery), and the display agent's OUTPUT then
pinned a framebuffer: TTM evicted, the blit waited for an IB from a pool
whose fences never signal, with no timeout, on the call itself. That was
the driver's incoming-call thread, so every client froze with it, the
observer's reads and Linux-file calls included, and the driver could not
even take its own Stop after the GPU was powered off.

Every display op that can sleep (PROBE, SHOW, OFF, STATUS, MODES, IMPORT,
VERIFY, RELEASE, OUTPUT) now needs an async completion: the call takes the
display gate, queues the op on a wait-pool thread (rt_wait_pool, the
threads that already sleep for event waits) and returns at once with a
token. The thread runs the op, keeps its structure output for RESULT
(op 10, once) in a slot the op shares with the client (an op may end after
the client stopped), and completes the call with the token, the op's
IOReturn, its two scalars and its output length. A synchronous call of
one is refused; one while another runs is kIOReturnBusy.

PRESENT stays synchronous and never sleeps: it takes no display gate, and
rt_display_present and rt_display_stats take the display lock with
trylock, -EBUSY while an op holds it. The agent then carries that frame's
damage (moves as rectangles) to the next PRESENT and forgets its tile
hashes, so no later frame moves or skips rows the driver never drew.
A stopped client's imports and output are released on a pool thread once
the display gate is free (after any op of its own that is finishing).
The daemon skips a STATUS poll that finds the display busy instead of
closing its clients.

The host app's display calls are async: IOConnectCallAsyncMethod with a
notification port, the completion received on the calling thread (up to
60 s, then kIOReturnTimeout), and RESULT for the output.

test-display-async runs the production display_call machinery, extracted
from the dext source, with DriverKit mocked and an OUTPUT that never
finishes: the call returns at once, the op does not run on it, PRESENT
and another client are answered meanwhile, a second op is busy, a stop
waits for the op, the completion and RESULT (once) carry its result, and
a refused thread leaves the display free. With the op run on the call
(the 241 behaviour) it fails within 20 s instead of hanging.
2026-10-04 19:12:11 -07:00

253 lines
12 KiB
C

/* The in-driver display test (linuxu/src/amdgpu-rt/display.c): an
* in-kernel DRM client on the GPU's DRM device, as fbdev emulation is on
* Linux, that shows a static test pattern on the GPU's own outputs through
* upstream code only:
*
* drm_client_init() a client file on the primary node
* drm_client_modeset_probe() detect, EDID modes, preferred mode,
* one CRTC per connected connector
* drm_client_buffer_create_dumb() an XRGB8888 dumb buffer (amdgpu
* places it in CPU-visible VRAM) and
* its framebuffer
* drm_client_buffer_vmap() the pattern written through the BAR
* drm_client_modeset_commit() the atomic commit (amdgpu_dm/DC)
*
* Before the first commit the current configuration is recorded: each
* CRTC's mode and enable/active flags, each plane's CRTC, framebuffer and
* rectangles, each connector's CRTC. Turning the pattern off commits that
* configuration as one new atomic state (built like
* drm_client_modeset_commit_atomic() builds one, so the driver checks it
* against its current state), then deletes the framebuffer and releases
* the client. A duplicated drm_atomic_state is not used: it would commit
* amdgpu_dm's private DC context back stale. The client is never
* registered, so it gets no hotplug callbacks and is never released by
* upstream; rt_display_stop() releases it before the driver is removed.
* A failing step is reported with its errno; no other mechanism is tried
* in its place.
*
* A device that left the bus (rt/removal.h) has no display: every call
* returns -ENODEV, and turning a pattern or output off commits nothing,
* releasing the buffers and the client as a Linux unplug does.
*
* Every wait is upstream's own bounded wait (flip_done and vblank waits
* time out, DC's register and DMUB waits time out). Display must be
* enabled (amdgpu.dc, rt/bootstrap.h); otherwise the device has no CRTC
* and every call fails with -ENODEV.
*
* Shared with DriverKit and C++ callers: no kernel types. */
#ifndef LINUXU_RT_DISPLAY_H
#define LINUXU_RT_DISPLAY_H
#include <stdint.h>
#ifdef __cplusplus
extern "C" {
#endif
struct pci_dev;
#define RT_DISPLAY_VERSION 1u
#define RT_DISPLAY_CONNECTORS_MAX 8u
#define RT_DISPLAY_NAME_BYTES 32u
enum rt_display_pattern {
RT_DISPLAY_PATTERN_BARS = 0, /* eight colour bars over a grey ramp, white frame */
RT_DISPLAY_PATTERN_WHITE = 1,
RT_DISPLAY_PATTERN_GRADIENT = 2, /* red across, green down, blue opposite red */
RT_DISPLAY_PATTERNS
};
/* One DRM connector (writeback connectors are not listed). */
struct rt_display_connector {
char name[RT_DISPLAY_NAME_BYTES]; /* connector->name, e.g. "DP-1" */
uint32_t id; /* DRM object id */
uint32_t status; /* enum drm_connector_status: 1 connected, 2 disconnected, 3 unknown */
uint32_t modes; /* probed modes (connector->modes) */
uint32_t edid_bytes; /* the EDID property blob, 0 without one */
uint32_t preferred_width, preferred_height, preferred_refresh; /* Hz */
uint32_t lit; /* scanning out the test pattern */
uint32_t lit_width, lit_height, lit_refresh;
uint32_t crtc; /* index of the CRTC that drives it when lit */
};
struct rt_display_report {
uint32_t version; /* RT_DISPLAY_VERSION */
uint32_t connectors; /* entries of connector[] filled */
uint32_t showing; /* the test pattern is on screen */
uint32_t pattern; /* enum rt_display_pattern while showing */
uint32_t fb_width, fb_height, fb_pitch;
uint32_t crtcs; /* CRTCs the device has */
uint64_t fb_gpu_addr; /* the framebuffer's VRAM address while showing */
uint64_t fill_ns; /* writing the pattern */
uint64_t commit_ns; /* the last commit (pattern on, or restore) */
int32_t probe_status; /* drm_client_modeset_probe() */
int32_t commit_status; /* drm_client_modeset_commit() */
int32_t restore_status; /* commit of the recorded configuration */
uint32_t hotplug_epoch; /* hotplug events seen (STATUS; 0 before the first) */
struct rt_display_connector connector[RT_DISPLAY_CONNECTORS_MAX];
};
/* Probe every connector (detect, EDID, modes) with a transient client and
* report them; nothing is committed. Returns 0 or a negative errno. */
int rt_display_probe(struct pci_dev *pdev, struct rt_display_report *report);
/* Show @pattern on the connected outputs, or only on the connector named
* @connector ("DP-1" or "card0-DP-1") when it is not NULL or empty, each
* at its preferred mode. A pattern already showing is turned off first.
* Returns 0, -ENODEV (no display), -ENOENT (no connected output, or no
* such connector), -EINVAL, or the failing upstream call's errno; on any
* failure the recorded configuration is committed again and nothing stays
* allocated. */
int rt_display_show(struct pci_dev *pdev, const char *connector, uint32_t pattern,
struct rt_display_report *report);
/* Turn the pattern off: commit the configuration recorded before it was
* shown (restore_status reports that commit), delete
* the framebuffer, release the client. 0 when nothing was showing. */
int rt_display_off(struct pci_dev *pdev, struct rt_display_report *report);
/* rt_display_off() for the session close, before upstream removal. */
void rt_display_stop(void);
/* Cached connector state for a display agent: no detection, no AUX or DDC
* traffic, only what the last probe or hotplug left in each connector,
* plus report->hotplug_epoch, which counts hotplug events. The first call
* registers a monitor client (drm_client_register) whose hotplug callback
* counts them: DM's HPD interrupt handling (drm_kms_helper_connector_
* hotplug_event) reaches it. Upstream unregisters and frees it with the
* DRM device. A changed epoch means: probe again. */
int rt_display_status(struct pci_dev *pdev, struct rt_display_report *report);
#define RT_DISPLAY_MODES_MAX 56u
#define RT_DISPLAY_MODE_PREFERRED (1u << 0)
#define RT_DISPLAY_MODE_INTERLACE (1u << 1)
struct rt_display_mode {
uint16_t width, height;
uint32_t refresh_mhz; /* vertical refresh in mHz */
uint32_t clock_khz;
uint32_t flags; /* RT_DISPLAY_MODE_* */
};
/* One connector's probed modes (connector->modes after the last probe), in
* the order upstream sorted them (preferred first). */
struct rt_display_modes {
uint32_t version; /* RT_DISPLAY_VERSION */
uint32_t status; /* enum drm_connector_status */
uint32_t count; /* entries of mode[] filled */
uint32_t total; /* modes the connector has */
uint32_t width_mm, height_mm; /* display_info physical size */
struct rt_display_mode mode[RT_DISPLAY_MODES_MAX];
};
/* The modes of the connector named @connector. -ENOENT for no such
* connector, -ENODEV without display. */
int rt_display_modes(struct pci_dev *pdev, const char *connector, struct rt_display_modes *out);
/* The monitor on one connector, from the state the last probe left (no
* detection, no AUX or DDC traffic): the EDID's monitor name descriptor
* (drm_edid_get_monitor_name; empty without an EDID or without the
* descriptor) and the display_info physical size. -ENOENT for no such
* connector, -ENODEV without display. */
#define RT_DISPLAY_MONITOR_NAME_BYTES 14u
struct rt_display_monitor {
uint32_t status; /* enum drm_connector_status */
uint32_t width_mm, height_mm;
char name[RT_DISPLAY_MONITOR_NAME_BYTES];
};
int rt_display_monitor(struct pci_dev *pdev, const char *connector,
struct rt_display_monitor *out);
/* ---- an output a display agent feeds (docs/macos-displays.md) ----
* rt_display_output() lights one connector at the mode @width x @height at
* @refresh_mhz (one of its probed modes, matched within 50 mHz; -EINVAL
* when it has no such mode) with two VRAM framebuffers, recording the
* configuration before it as the test pattern does; rt_display_off() and
* rt_display_stop() restore it. While it runs, report->pattern is
* RT_DISPLAY_PATTERN_OUTPUT.
*
* rt_display_present() hands a frame to the output's worker and returns:
* it takes over one hold on @surface (rt_surface_get_hold; released even
* on failure), the dirty rectangles and the capture time (CLOCK_UPTIME_RAW
* ns, the clock ktime_get_ns() reads, for the latency statistics), and
* replaces a frame the worker has not taken yet (their damage adds up).
* The worker copies into one of three framebuffers with SDMA and flips to
* it with a nonblocking commit (display.c describes the pipeline). No
* rectangle means nothing changed: only the statistics come back. A
* worker error is returned by every later call. It never sleeps: while
* an operation that can (OUTPUT, OFF, SHOW, a probe) holds the display,
* it returns -EBUSY at once, as rt_display_stats() does.
*
* @moves are rows that scrolled: each says that @width x @height pixels at
* (@x, @y) of this frame are the pixels at (@x, @src_y) of the frame
* presented before it, so the worker copies them from the framebuffer it
* drew last (VRAM to VRAM) instead of from the surface over PCIe. Their
* destinations are not in @rects. A frame that replaces one the worker
* has not taken, or that comes before any frame was drawn, has its moves
* (and those of the frame it replaces) copied from the surface instead:
* the rows they come from were never drawn (counted in moves_dropped).
* Each move must lie inside the frame (-EINVAL). */
#define RT_DISPLAY_MOVES_MAX 32u
struct rt_display_move {
uint32_t x, y, width, height;
uint32_t src_y;
uint32_t reserved; /* 0 */
};
#define RT_DISPLAY_PATTERN_OUTPUT 0xffu
struct rt_surface;
struct rt_surface_rect;
struct rt_display_present_stats {
uint32_t version; /* 3 */
uint32_t engines; /* SDMA engines the output copies with */
uint64_t frames_received; /* presents with damage */
uint64_t frames_flipped; /* frames that reached the screen */
uint64_t frames_replaced; /* replaced in the mailbox by a newer frame */
uint64_t copy_jobs;
uint64_t bytes; /* copied */
uint64_t copy_submit_ns; /* the worker's CPU time building and submitting copies */
uint64_t copy_gpu_ns; /* submit to the copies' fences, summed over flipped frames */
uint64_t latency_ns; /* capture to flip (vblank timestamp), summed */
uint64_t latency_max_ns;
uint64_t last_bytes, last_copy_gpu_ns, last_latency_ns; /* the last flipped frame */
int32_t error; /* the worker's first error, 0 */
uint32_t full_frames; /* frames copied whole */
uint64_t aperture_ops; /* CPU string operations on the VRAM aperture
* (rt/device_string.h), all users, since boot */
/* Version 3. */
uint64_t vram_bytes; /* copied from the framebuffer drawn before
* (what a buffer missed, moves); bytes is
* what came from the surface over PCIe */
uint64_t moved_rows; /* rows of moves copied in VRAM */
uint64_t moves_dropped; /* moves copied from the surface instead */
};
int rt_display_output(struct pci_dev *pdev, const char *connector, uint32_t width,
uint32_t height, uint32_t refresh_mhz, struct rt_display_report *report);
int rt_display_present(struct pci_dev *pdev, struct rt_surface *surface,
const struct rt_surface_rect *rects, uint32_t count,
const struct rt_display_move *moves, uint32_t move_count, uint64_t capture_ns,
struct rt_display_present_stats *stats);
/* The output's statistics; -ENOENT without an output. */
int rt_display_stats(struct pci_dev *pdev, struct rt_display_present_stats *stats);
/* Whether a pattern is showing (cached; takes no lock). */
int rt_display_showing(void);
/* What a Linux-file client's primary-node files and LX_SCANOUT need from
* the display (rt_lx_client_set_display): clients open the primary node
* without becoming DRM master and show their framebuffers on the output
* through its worker (display.c, rt/lx_abi.h). */
struct rt_lx_display_hooks;
extern const struct rt_lx_display_hooks rt_display_lx_hooks;
#ifdef __cplusplus
}
#endif
#endif