Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
263 changes: 263 additions & 0 deletions include/drm-uapi/apple_sgx_drm.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,263 @@
/* SPDX-License-Identifier: MIT */
/*
* Apple S5L8940X (A5) PowerVR SGX543MP2: the render node's interface.
*
* EXPERIMENTAL. This is the first interface Mesa and sgx2d talk to the GPU
* through (docs/research/p105-mesa.md, M9); it will change while the Mesa
* driver is written, and nothing outside this repository should depend on
* it yet. APPLE_SGX_PARAM_UAPI_VERSION says which revision a kernel has.
*
* The model, kept as small as the hardware allows:
*
* - One GPU address space, shared by every client (the microkernel's own
* buffers are in it too, at 0x80000000). Buffers are GEM objects mapped
* into it for their whole life; the kernel picks the address (64 KiB
* aligned for buffers of 64 KiB or more: a render target needs it), or
* the caller asks for one (APPLE_SGX_BO_FIXED_VA). The GPU's own pointer
* encodings decide where things may go -- PDS data pointers only reach
* 0x80000000 and up, and USSE code is addressed relative to a code base
* register -- so the usable range and the code zone are parameters.
*
* - USE_CODE_BASE_3 and USE_CODE_BASE_5 (the ones iOS's GL driver uses)
* point at APPLE_SGX_PARAM_CODE_BASE. A DOUTU word for code at GPU
* address A is ((A - CODE_BASE) / 8) << 4 | 3, the index in 20 bits:
* code has to be within 8 MiB of the base. Buffers created with
* APPLE_SGX_BO_USSE_CODE land there (kernels before 2026-10-04 gave
* CODE_VA_END as base + 16 MiB and used the top of it: out of reach).
*
* - A render (the TA, then the 3D pass) is submitted as the TA command iOS
* puts in a render context's CCB, built by the caller as for debugfs's
* "rkick" (docs/research/p105-gpu.md, M4): the kernel copies it into the
* render queue, points its completion at its own memory, writes the
* render context into the render details (+0x20, +0xa4), sets the
* parameter buffer descriptor, and kicks. The job is done when the
* microkernel clears the render details' word at +0x24 after the 3D pass.
* Renders run one at a time, in submission order across all clients.
*
* - Synchronisation is syncobjs: a submit waits for in_syncs and signals
* out_sync when its 3D pass is over. Every buffer the render touches
* must be listed in bo_handles; they stay alive and mapped until then.
*/
#ifndef _APPLE_SGX_DRM_H_
#define _APPLE_SGX_DRM_H_

#include "drm.h"

#if defined(__cplusplus)
extern "C" {
#endif

#define DRM_APPLE_SGX_GET_PARAM 0x00
#define DRM_APPLE_SGX_GEM_CREATE 0x01
#define DRM_APPLE_SGX_GEM_MMAP_OFFSET 0x02
#define DRM_APPLE_SGX_GEM_WAIT 0x03
#define DRM_APPLE_SGX_SUBMIT 0x04
#define DRM_APPLE_SGX_GEM_INFO 0x05
#define DRM_APPLE_SGX_OVERLAY 0x06

#define DRM_IOCTL_APPLE_SGX_GET_PARAM \
DRM_IOWR(DRM_COMMAND_BASE + DRM_APPLE_SGX_GET_PARAM, struct drm_apple_sgx_get_param)
#define DRM_IOCTL_APPLE_SGX_GEM_CREATE \
DRM_IOWR(DRM_COMMAND_BASE + DRM_APPLE_SGX_GEM_CREATE, struct drm_apple_sgx_gem_create)
#define DRM_IOCTL_APPLE_SGX_GEM_MMAP_OFFSET \
DRM_IOWR(DRM_COMMAND_BASE + DRM_APPLE_SGX_GEM_MMAP_OFFSET, struct drm_apple_sgx_gem_mmap_offset)
#define DRM_IOCTL_APPLE_SGX_GEM_WAIT \
DRM_IOW(DRM_COMMAND_BASE + DRM_APPLE_SGX_GEM_WAIT, struct drm_apple_sgx_gem_wait)
#define DRM_IOCTL_APPLE_SGX_SUBMIT \
DRM_IOW(DRM_COMMAND_BASE + DRM_APPLE_SGX_SUBMIT, struct drm_apple_sgx_submit)
#define DRM_IOCTL_APPLE_SGX_GEM_INFO \
DRM_IOWR(DRM_COMMAND_BASE + DRM_APPLE_SGX_GEM_INFO, struct drm_apple_sgx_gem_info)
#define DRM_IOCTL_APPLE_SGX_OVERLAY \
DRM_IOWR(DRM_COMMAND_BASE + DRM_APPLE_SGX_OVERLAY, struct drm_apple_sgx_overlay)

/* ---- GET_PARAM ---------------------------------------------------------- */

enum drm_apple_sgx_param {
APPLE_SGX_PARAM_UAPI_VERSION = 0, /* 3 (2: no kernel PB, 1: no GEM_INFO) */
APPLE_SGX_PARAM_CORE_ID = 1, /* MASTER_CORE_ID: 0x01194201 */
APPLE_SGX_PARAM_CORE_REVISION = 2, /* 0x00010202 = 1.2.2 */
APPLE_SGX_PARAM_NUM_CORES = 3, /* 2 */
APPLE_SGX_PARAM_CLOCK_KHZ = 4, /* the GPU clock */
APPLE_SGX_PARAM_UKERNEL_UP = 5, /* 1 while the microkernel runs */

/* GPU addresses: [VA_START, VA_END) is where buffers may be mapped,
* [CODE_VA_START, CODE_VA_END) the code zone, CODE_BASE what
* USE_CODE_BASE_3 and _5 hold (see above). */
APPLE_SGX_PARAM_VA_START = 6,
APPLE_SGX_PARAM_VA_END = 7,
APPLE_SGX_PARAM_CODE_BASE = 8,
APPLE_SGX_PARAM_CODE_VA_START = 9,
APPLE_SGX_PARAM_CODE_VA_END = 10,

/* The framebuffer (simplefb's memory), mapped by the kernel for the 2D
* engine; a render can write straight into it. 0 if there is none.
* Linear a8r8g8b8; stride in bytes. */
APPLE_SGX_PARAM_FB_VA = 11,
APPLE_SGX_PARAM_FB_WIDTH = 12,
APPLE_SGX_PARAM_FB_HEIGHT = 13,
APPLE_SGX_PARAM_FB_STRIDE = 14,

/* Renders submitted, completed, and timed out since the module loaded;
* microkernel (re)starts. */
APPLE_SGX_PARAM_RENDERS_SUBMITTED = 15,
APPLE_SGX_PARAM_RENDERS_DONE = 16,
APPLE_SGX_PARAM_RENDERS_TIMED_OUT = 17,
APPLE_SGX_PARAM_UKERNEL_BOOTS = 18,

/* UAPI 3: the kernel's parameter buffer (a submit with pb_va 0 uses
* it), and where APPLE_SGX_BO_TA_HEAP buffers go -- above the TA's
* base, 0x87800000, within the 256 MiB the TA's addresses reach. */
APPLE_SGX_PARAM_PB_VA = 19,
APPLE_SGX_PARAM_TA_HEAP_START = 20,
APPLE_SGX_PARAM_TA_HEAP_END = 21,

/* The APPLE_SGX_BO_* flags GEM_CREATE takes; kernels before
* APPLE_SGX_BO_SCANOUT do not have this parameter (EINVAL), and every
* flag before it. Still UAPI 3: a flag more is no new interface. */
APPLE_SGX_PARAM_BO_FLAGS = 22,
};

struct drm_apple_sgx_get_param {
__u32 param; /* in: enum drm_apple_sgx_param */
__u32 pad; /* in: 0 */
__u64 value; /* out */
};

/* ---- GEM_CREATE --------------------------------------------------------- */

/* Map at drm_apple_sgx_gem_create.va instead of an address the kernel
* picks. Fails with EEXIST if anything is mapped there already. */
#define APPLE_SGX_BO_FIXED_VA (1 << 0)
/* USSE code: in the code zone, where a DOUTU can reach it. */
#define APPLE_SGX_BO_USSE_CODE (1 << 1)
/* The GPU may only read it (PTE READONLY). */
#define APPLE_SGX_BO_GPU_READONLY (1 << 2)
/* Without the PTE's CACHECONSISTENT bit, which every other buffer has (the
* microkernel only sees CPU writes to cache-consistent pages; the
* framebuffer is mapped without it). */
#define APPLE_SGX_BO_NOT_CACHE_CONSISTENT (1 << 3)
/* In the TA's heap (UAPI 3): render target data, whose state buffer the 3D
* register block names by its offset from the TA's base. */
#define APPLE_SGX_BO_TA_HEAP (1 << 4)
/* A buffer the screen may show (APPLE_SGX_PARAM_BO_FLAGS lists it): one of
* the screen's size is made physically contiguous when memory allows, and
* KMS then shows it by pointing the display at it instead of copying it
* onto the screen. Not an error when it cannot be: the buffer is an
* ordinary one, and GEM_INFO's flags leave this out. */
#define APPLE_SGX_BO_SCANOUT (1 << 5)

#define APPLE_SGX_BO_FLAGS 0x3f

struct drm_apple_sgx_gem_create {
__u64 size; /* in: bytes, rounded up to 4 KiB */
__u32 flags; /* in: APPLE_SGX_BO_* */
__u32 handle; /* out */
__u32 va; /* in with APPLE_SGX_BO_FIXED_VA, out: the GPU address */
__u32 pad; /* in: 0 */
};

/* ---- GEM_MMAP_OFFSET: the offset to mmap() the buffer at (write-combined) */

struct drm_apple_sgx_gem_mmap_offset {
__u32 handle; /* in */
__u32 flags; /* in: 0 */
__u64 offset; /* out */
};

/* ---- GEM_WAIT: until no render that lists the buffer is running -------- */

struct drm_apple_sgx_gem_wait {
__u32 handle; /* in */
__u32 pad; /* in: 0 */
__s64 timeout_ns; /* in: absolute, CLOCK_MONOTONIC; 0 just polls */
};

/* ---- SUBMIT: one render, the TA and then the 3D pass -------------------- */

#define APPLE_SGX_TA_CMD_MIN 0x11c
#define APPLE_SGX_TA_CMD_MAX 0x200

struct drm_apple_sgx_submit {
/* in: user pointer to the TA command, as iOS's render submit builds
* it (0x80bfca20): word 0 its size in bytes, a multiple of 8 between
* APPLE_SGX_TA_CMD_MIN and _MAX; the kernel rewrites words 0, 0x64,
* 0x68 and 0x6c. */
__u64 cmd;
__u32 cmd_size; /* in: bytes copied from cmd; >= word 0 */
__u32 flags; /* in: 0 */

__u32 pb_va; /* in: the parameter buffer's descriptor; 0: the
* kernel's, shared by every client (UAPI 3) */
__u32 details_handle; /* in: the buffer holding the render details */
__u32 details_offset; /* in: where in it, 4-byte aligned */
__u32 cache_control; /* in: SGXMKIF_CC_* to add to the kick, normally 0 */

__u64 bo_handles; /* in: user pointer to __u32 handles */
__u32 bo_handle_count;
__u32 in_sync_count;
__u64 in_syncs; /* in: user pointer to __u32 syncobj handles */
__u32 out_sync; /* in: syncobj to signal when it is done, or 0 */
__u32 pad; /* in: 0 */
};

/* ---- GEM_INFO: what a handle is, for one that came from a dma-buf ------ */

/* Every buffer is the render node's own (dma-bufs of other devices are not
* taken), so a dma-buf's handle names a buffer already mapped: this says
* where. UAPI version 2 on. */
struct drm_apple_sgx_gem_info {
__u32 handle; /* in */
__u32 flags; /* out: the APPLE_SGX_BO_* it was made with */
__u32 va; /* out: its GPU address */
__u32 pad; /* in: 0 */
__u64 size; /* out: bytes */
};

/* ---- OVERLAY: a picture over the screen, whatever shows it -------------- */

/*
* A buffer of the render node blended over the screen -- straight alpha,
* colour = overlay * a + screen * (1 - a) -- after every copy of KMS's
* plane onto it, and at once when it is set: an on-screen control over a
* fullscreen game, with no compositor (tools/screenpad). Its pixels are
* the plane's, turned as the plane is; width x height must be the plane's
* size now (out_width x out_height), or it is not shown until it is set
* again for the new size. The rectangles listed (out to 16 pixels on every
* side) say where it is: a copy that meets any of them blends the box
* round those it meets, so the buffer's pixels outside them must be
* transparent. A change to its pixels shows when it is set again. handle 0 takes it away, and so does closing the
* file that set it; one file at a time has it (-EBUSY for the others).
* Shown while a KMS client has a framebuffer on the plane (X, a game on
* KMS), not on the text console. Root only.
*
* Kernels from 2026-10-23 on. It changed nothing else, so it did not
* change APPLE_SGX_PARAM_UAPI_VERSION: Mesa (sgx_device.c) refuses a
* version it does not know and falls back to software -- XFCE on softpipe
* -- so a kernel that only adds to the interface keeps the number, and a
* program asks for the addition itself (QUERY: -EINVAL where there is
* none).
*/
#define APPLE_SGX_OVERLAY_QUERY (1 << 0) /* only the out fields */
#define APPLE_SGX_OVERLAY_MAX_RECTS 16

struct drm_apple_sgx_overlay_rect {
__u16 x, y, w, h;
};

struct drm_apple_sgx_overlay {
__u32 flags; /* in: APPLE_SGX_OVERLAY_* */
__u32 handle; /* in: the buffer (BGRA8888, its alpha used), or 0 */
__u32 pitch; /* in: bytes a line, a multiple of 64 */
__u16 width, height; /* in: pixels: the plane's size */
__u32 nrects; /* in: how many rects are used */
struct drm_apple_sgx_overlay_rect rects[APPLE_SGX_OVERLAY_MAX_RECTS];
__u16 out_width; /* out: the plane's size now; 0 x 0 with no */
__u16 out_height; /* framebuffer shown */
__u32 out_rotation; /* out: the plane's DRM_MODE_ROTATE_* */
};

#if defined(__cplusplus)
}
#endif

#endif /* _APPLE_SGX_DRM_H_ */
1 change: 1 addition & 0 deletions meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,7 @@ with_gallium_i915 = gallium_drivers.contains('i915')
with_gallium_svga = gallium_drivers.contains('svga')
with_gallium_virgl = gallium_drivers.contains('virgl')
with_gallium_lima = gallium_drivers.contains('lima')
with_gallium_sgx = gallium_drivers.contains('sgx')
with_gallium_zink = gallium_drivers.contains('zink')
with_gallium_d3d12 = gallium_drivers.contains('d3d12')
with_gallium_asahi = gallium_drivers.contains('asahi')
Expand Down
2 changes: 1 addition & 1 deletion meson.options
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ option(
'all', 'auto',
'asahi', 'crocus', 'd3d12', 'ethosu', 'etnaviv', 'freedreno', 'i915', 'iris',
'lima', 'llvmpipe', 'nouveau', 'panfrost', 'r300', 'r600', 'radeonsi',
'rocket', 'softpipe', 'svga', 'tegra', 'v3d', 'vc4', 'virgl', 'zink',
'rocket', 'sgx', 'softpipe', 'svga', 'tegra', 'v3d', 'vc4', 'virgl', 'zink',
],
description : 'List of gallium drivers to build. If this is set to auto ' +
'all drivers applicable to the target OS/architecture ' +
Expand Down
8 changes: 6 additions & 2 deletions src/compiler/nir/nir_lower_uniforms_to_ubo.c
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,13 @@ nir_lower_uniforms_to_ubo_instr(nir_builder *b, nir_instr *instr, void *data)
* nir_lower_ubo_vec4.c)
*/
if (nir_src_is_const(intr->src[0])) {
/* (the offset in the multiplier's units too, as the address
* has it: an offset of 1 vec4 is 16 bytes, not 1 --
* nir_lower_ubo_vec4 asserted on a matrix's second column)
*/
nir_intrinsic_set_align(load, NIR_ALIGN_MUL_MAX,
(nir_src_as_uint(intr->src[0]) +
nir_intrinsic_base(intr) * multiplier) %
((nir_src_as_uint(intr->src[0]) +
nir_intrinsic_base(intr)) * multiplier) %
NIR_ALIGN_MUL_MAX);
} else {
nir_intrinsic_set_align(load, MAX2(multiplier, intr->def.bit_size / 8), 0);
Expand Down
1 change: 1 addition & 0 deletions src/gallium/auxiliary/pipe-loader/pipe_loader_drm.c
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ static const struct drm_driver_descriptor *driver_descriptors[] = {
&ethosu_driver_descriptor,
&tegra_driver_descriptor,
&lima_driver_descriptor,
&apple_sgx_driver_descriptor,
&zink_driver_descriptor,
&kmsro_driver_descriptor,
};
Expand Down
17 changes: 17 additions & 0 deletions src/gallium/auxiliary/target-helpers/drm_helper.h
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,23 @@ DRM_DRIVER_DESCRIPTOR(lima, lima_driconf, ARRAY_SIZE(lima_driconf))
DRM_DRIVER_DESCRIPTOR_STUB(lima)
#endif

#ifdef GALLIUM_SGX
#include "sgx/drm/sgx_drm_public.h"

static struct pipe_screen *
pipe_apple_sgx_create_screen(int fd, const struct pipe_screen_config *config)
{
struct pipe_screen *screen;

screen = sgx_drm_screen_create(fd, config);
return screen ? debug_screen_wrap(screen) : NULL;
}
DRM_DRIVER_DESCRIPTOR(apple_sgx, NULL, 0)

#else
DRM_DRIVER_DESCRIPTOR_STUB(apple_sgx)
#endif

#ifdef GALLIUM_ZINK
#include "zink/zink_public.h"

Expand Down
1 change: 1 addition & 0 deletions src/gallium/auxiliary/target-helpers/drm_helper_public.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ extern const struct drm_driver_descriptor rocket_driver_descriptor;
extern const struct drm_driver_descriptor ethosu_driver_descriptor;
extern const struct drm_driver_descriptor tegra_driver_descriptor;
extern const struct drm_driver_descriptor lima_driver_descriptor;
extern const struct drm_driver_descriptor apple_sgx_driver_descriptor;
extern const struct drm_driver_descriptor zink_driver_descriptor;
extern const struct drm_driver_descriptor kmsro_driver_descriptor;

Expand Down
11 changes: 11 additions & 0 deletions src/gallium/drivers/sgx/drm-shim/meson.build
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Copyright 2026 Project Cascadia
# SPDX-License-Identifier: MIT

libsgx_noop_drm_shim = shared_library(
'sgx_noop_drm_shim',
'sgx_noop.c',
include_directories : [inc_include, inc_src],
dependencies : [dep_drm_shim, idep_mesautil],
gnu_symbol_visibility : 'hidden',
install : true,
)
Loading
Loading