From 18ee9371c45da7d588f7b055f95693e31a8e9cdc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Berkay=20K=C3=B6sebay?= Date: Sat, 21 Nov 2026 20:47:15 +0100 Subject: [PATCH 1/8] kernel: the A8 (T7000) GPU in powervr's Apple glue The A8's GX6450 runs the same family of firmware as the A9's, so it becomes a variant of the S8003 chip file rather than a new driver (drivers/gpu/0063-0073): - 0063-0064: the S8003 chip described in a variant table, and chip hooks for its own GPU ID read and heap table. These touch the A9 and A10 paths; their values and functions are unchanged. - 0065: dt-bindings for apple,t7000-gpu. - 0066: a GPU node without power domains, and the failing probe step named (shared powervr code; nodes with power domains are unchanged). - 0067-0069: the A8's firmware boot and power up and down in Apple's order, compute jobs, and TA/3D render jobs with the parameter-manager pool (a8_pm_blocks, a8_pm_grow) and the region-header ring kept in the HWRT data set. - 0070: fault and hang reports and recovery. - 0071: completion checks in the firmware interrupt's thread, the re-check after an early interrupt, a publish that cannot leave a failed job in the firmware's ring, Apple's doorbell. - 0072: a fragment job submitted behind its geometry job, with two alternating 3D records (a8_joint, off by default). - 0073: statistics and the A8's job timeline in debugfs apple_jobs, last. It also points the firmware's TA and 3D time words at the channel's times; the series without it has not run on the device. Config: CONFIG_DRM_POWERVR_APPLE goes from unset to =y on apple-4k, as on apple-16k, so the A8 gets powervr's Apple glue. It builds into the powervr module on every A7, A8 and A8X kernel; only boards whose device tree enables an apple,t7000-gpu node bind it. Assisted-by: LLM --- kernel/configs/apple-4k/config | 2 +- ...apple-Describe-the-S8003-chip-in-a-v.patch | 832 ++++++++ ...apple-Let-a-chip-read-its-GPU-ID-and.patch | 170 ++ ...apple-s8003-gpu-Add-the-A8-T7000-GPU.patch | 125 ++ ...Allow-a-GPU-node-without-power-domai.patch | 82 + ...apple-Boot-the-A8-T7000-GPU-firmware.patch | 1433 ++++++++++++++ ...apple-Run-compute-jobs-on-the-A8-T70.patch | 255 +++ ...ination-apple-Render-on-the-A8-T7000.patch | 943 +++++++++ ...apple-Report-the-A8-s-faults-and-han.patch | 522 +++++ ...apple-Check-the-A8-s-completions-in-.patch | 484 +++++ ...apple-Submit-an-A8-fragment-job-behi.patch | 270 +++ ...apple-Show-where-an-A8-job-s-time-go.patch | 1708 +++++++++++++++++ 12 files changed, 6825 insertions(+), 1 deletion(-) create mode 100644 kernel/patches/drivers/gpu/0063-drm-imagination-apple-Describe-the-S8003-chip-in-a-v.patch create mode 100644 kernel/patches/drivers/gpu/0064-drm-imagination-apple-Let-a-chip-read-its-GPU-ID-and.patch create mode 100644 kernel/patches/drivers/gpu/0065-dt-bindings-gpu-apple-s8003-gpu-Add-the-A8-T7000-GPU.patch create mode 100644 kernel/patches/drivers/gpu/0066-drm-imagination-Allow-a-GPU-node-without-power-domai.patch create mode 100644 kernel/patches/drivers/gpu/0067-drm-imagination-apple-Boot-the-A8-T7000-GPU-firmware.patch create mode 100644 kernel/patches/drivers/gpu/0068-drm-imagination-apple-Run-compute-jobs-on-the-A8-T70.patch create mode 100644 kernel/patches/drivers/gpu/0069-drm-imagination-apple-Render-on-the-A8-T7000.patch create mode 100644 kernel/patches/drivers/gpu/0070-drm-imagination-apple-Report-the-A8-s-faults-and-han.patch create mode 100644 kernel/patches/drivers/gpu/0071-drm-imagination-apple-Check-the-A8-s-completions-in-.patch create mode 100644 kernel/patches/drivers/gpu/0072-drm-imagination-apple-Submit-an-A8-fragment-job-behi.patch create mode 100644 kernel/patches/drivers/gpu/0073-drm-imagination-apple-Show-where-an-A8-job-s-time-go.patch diff --git a/kernel/configs/apple-4k/config b/kernel/configs/apple-4k/config index e738fed1..fd4e5070 100644 --- a/kernel/configs/apple-4k/config +++ b/kernel/configs/apple-4k/config @@ -3969,7 +3969,7 @@ CONFIG_DRM_PANEL_BRIDGE=y # CONFIG_DRM_HISI_HIBMC is not set # CONFIG_DRM_HISI_KIRIN is not set CONFIG_DRM_POWERVR=m -# CONFIG_DRM_POWERVR_APPLE is not set +CONFIG_DRM_POWERVR_APPLE=y # CONFIG_DRM_LIMA is not set # CONFIG_DRM_LOGICVC is not set # CONFIG_DRM_MGAG200 is not set diff --git a/kernel/patches/drivers/gpu/0063-drm-imagination-apple-Describe-the-S8003-chip-in-a-v.patch b/kernel/patches/drivers/gpu/0063-drm-imagination-apple-Describe-the-S8003-chip-in-a-v.patch new file mode 100644 index 00000000..7cadea8c --- /dev/null +++ b/kernel/patches/drivers/gpu/0063-drm-imagination-apple-Describe-the-S8003-chip-in-a-v.patch @@ -0,0 +1,832 @@ +From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 +From: =?UTF-8?q?Berkay=20K=C3=B6sebay?= +Date: Sat, 21 Nov 2026 12:01:00 +0100 +Subject: [PATCH] drm/imagination: apple: Describe the S8003 chip in a variant + table + +The A8 (T7000) runs the A9's GFX firmware family on the same META: the +same LDR image format, loader, windows, rings, events, slots and job +records, with different sizes, counts, register choices and a different +parameter manager. Rather than a second chip file that copies the boot, +ring and slot code, give the S8003 chip file a table of what differs. + +struct s8003_variant holds the sizes (image, root and shared memory, the +object table, a ring lane's indices), the slots (count, first, how many +a VM takes), where the channels are, whether the firmware needs the +host's cache-line reads, and the steps whose structure differs: image +configuration, mapping, init data, boot, release, stop, idle, doorbell, +the first command, a VM's mappings and PM sets, job preparation and +submission, completion and the hang report. The chip's ops call through +it. + +The A9's table holds today's constants and functions unchanged, so the +A9 runs exactly as before: every value the shared code now reads from +the table is the constant it used, and every hook is the function that +ran in its place. + +Assisted-by: LLM +--- + drivers/gpu/drm/imagination/pvr_apple_s8003.c | 417 +++++++++++++----- + 1 file changed, 298 insertions(+), 119 deletions(-) + +diff --git a/drivers/gpu/drm/imagination/pvr_apple_s8003.c b/drivers/gpu/drm/imagination/pvr_apple_s8003.c +index e396067cc2..f29035a445 100644 +--- a/drivers/gpu/drm/imagination/pvr_apple_s8003.c ++++ b/drivers/gpu/drm/imagination/pvr_apple_s8003.c +@@ -218,6 +218,63 @@ struct s8003_layout { + u8 shared[S8003_OBJECTS]; /* in the shared memory, not the root */ + }; + ++/* An object of the root or shared memory and its size, in the order Apple's kext packs them */ ++struct s8003_object_size { ++ u8 object; ++ bool shared; ++ u32 size; ++}; ++ ++struct pvr_apple_s8003; ++struct s8003_vm; ++ ++/* ++ * What differs between the chips that run this firmware: sizes, counts, and ++ * the steps whose structure differs. The A9's values are the constants above; ++ * a chip's start, jobs and VMs go through the same functions otherwise. ++ */ ++struct s8003_variant { ++ const char *name; ++ u32 page_size; /* the GPU page size the firmware's tables have */ ++ u32 image_size; /* the unpacked firmware image */ ++ u32 root_size, shared_size; /* the root and shared memory as mapped */ ++ u32 root_objects_end; /* where the root's objects must end */ ++ u32 shared_objects_end; /* where the shared memory's objects must end */ ++ const struct s8003_object_size *objects; ++ unsigned int nr_objects; ++ u32 ring_ctl_size; /* one lane's ring indices */ ++ u32 slots; /* the firmware's slots for VMs */ ++ u32 slot_first; /* the first slot the host may use */ ++ unsigned int vm_slots; /* a VM's slots: 2 (geometry and compute, fragment) or 1 */ ++ u32 channel[3]; /* the engines' channels: TA, 3D, CU offsets */ ++ bool channels_root; /* the channels are in the root memory, not the shared */ ++ u32 ldr_conf_count; /* the image's register writes, if a fixed number */ ++ bool touch; /* the firmware needs s8003_touch() */ ++ const struct pvr_meta_ldr_target *ldr; ++ ++ int (*init)(struct pvr_apple *apple); ++ void (*image_configure)(struct pvr_apple_s8003 *s); ++ int (*map)(struct pvr_apple *apple); ++ void (*unmap)(struct pvr_apple *apple); ++ void (*init_build)(struct pvr_apple *apple, u32 q); ++ void (*channels_init)(struct pvr_apple_s8003 *s); ++ int (*boot)(struct pvr_apple *apple); ++ int (*release)(struct pvr_apple *apple, u32 q); ++ int (*core_off)(struct pvr_apple *apple); ++ void (*idle)(struct pvr_apple *apple); ++ int (*doorbell)(struct pvr_apple *apple, u32 dm); ++ int (*first_command)(struct pvr_apple *apple); ++ int (*vm_map)(struct pvr_apple *apple, struct pvr_vm_context *vm_ctx); ++ void (*vm_put)(struct pvr_apple *apple, struct s8003_vm *vm); ++ void (*vm_unmap)(struct pvr_apple *apple, struct pvr_vm_context *vm_ctx); ++ u32 (*engine_done)(struct pvr_apple *apple, enum pvr_apple_engine engine); ++ int (*prepare_job)(struct pvr_apple *apple, struct pvr_job *job); ++ int (*submit_geom)(struct pvr_apple *apple, struct s8003_vm *vm, struct pvr_job *job); ++ int (*submit_frag)(struct pvr_apple *apple, struct s8003_vm *vm, struct pvr_job *job); ++ int (*submit_compute)(struct pvr_apple *apple, struct s8003_vm *vm, struct pvr_job *job); ++ void (*report_hang)(struct pvr_apple *apple); ++}; ++ + struct s8003_mem { + struct pvr_gem_object *obj; + u8 *map; +@@ -276,6 +333,7 @@ struct s8003_vm { + + struct pvr_apple_s8003 { + struct pvr_apple *apple; ++ const struct s8003_variant *v; + struct delayed_work drain_work; /* reads the rings while the firmware runs */ + struct mutex event_lock; /* the rings the firmware posts to */ + ktime_t touched; /* s8003_touch() */ +@@ -358,57 +416,56 @@ static u32 s8003_meta_addr(u64 va) + return (lower_32_bits(va) + 0x40000000) | range; + } + +-/* Apple's kext packs each group in this order, every object on 64 bytes. */ +-static int s8003_layout_plan(struct s8003_layout *layout) ++/* The A9's objects, as Apple's kext packs them */ ++static const struct s8003_object_size s8003_objects_a9[] = { ++ { S8003_INIT, false, 0x1490 }, ++ { S8003_CHANNEL, false, 0x990 }, ++ { S8003_CHANNEL + 1, false, 0x8e0 }, ++ { S8003_CHANNEL + 2, false, 0x8c0 }, ++ { S8003_POLICY, false, 0x938 }, ++ { 0x4b, false, 0x28 }, ++ { 0x4c, false, 8 }, ++ { S8003_PLATFORM, false, 0x78c }, ++ { 0x4e, false, 0x84 }, ++ { S8003_CONTROL, true, 0x2058 }, ++ { S8003_EVENT_CTL, true, S8003_RING_CTL_SIZE }, ++ { S8003_LOG_CTL, true, 4 * S8003_RING_CTL_SIZE }, ++ { S8003_STATS_CTL, true, S8003_RING_CTL_SIZE }, ++ { S8003_TRACE_CTL, true, S8003_RING_CTL_SIZE }, ++ { S8003_EVENTS, true, 0x2c00 }, ++ { S8003_LOGS, true, 0xb000 }, ++ { 0x4f, true, 0x36000 }, ++ { S8003_STATS, true, 0x2c00 }, ++ { S8003_TRACE, true, 0x2400 }, ++ { S8003_DC_CTL, true, S8003_RING_CTL_SIZE }, ++ { S8003_DC_RING, true, 0x1400 }, ++ { S8003_WORK_CTL, true, S8003_RING_CTL_SIZE }, ++ { S8003_WORK_CTL + 4, true, S8003_RING_CTL_SIZE }, ++ { S8003_WORK_CTL + 8, true, S8003_RING_CTL_SIZE }, ++ { S8003_WORK_RING, true, 0x1400 }, ++ { S8003_WORK_RING + 4, true, 0x1400 }, ++ { S8003_WORK_RING + 8, true, 0x1400 }, ++ { 0x5c, true, 8 }, ++ { 0x85, true, 0x400 }, ++ { 0x84, true, 0x400 }, ++ { S8003_MMU_ROOT, true, 0x800 }, ++}; ++ ++/* Apple's kext packs each group in the variant's order, every object on 64 bytes. */ ++static int s8003_layout_plan(const struct s8003_variant *v, struct s8003_layout *layout) + { +- static const struct { +- u8 object; +- bool shared; +- u32 size; +- } objects[] = { +- { S8003_INIT, false, 0x1490 }, +- { S8003_CHANNEL, false, 0x990 }, +- { S8003_CHANNEL + 1, false, 0x8e0 }, +- { S8003_CHANNEL + 2, false, 0x8c0 }, +- { S8003_POLICY, false, 0x938 }, +- { 0x4b, false, 0x28 }, +- { 0x4c, false, 8 }, +- { S8003_PLATFORM, false, 0x78c }, +- { 0x4e, false, 0x84 }, +- { S8003_CONTROL, true, 0x2058 }, +- { S8003_EVENT_CTL, true, S8003_RING_CTL_SIZE }, +- { S8003_LOG_CTL, true, 4 * S8003_RING_CTL_SIZE }, +- { S8003_STATS_CTL, true, S8003_RING_CTL_SIZE }, +- { S8003_TRACE_CTL, true, S8003_RING_CTL_SIZE }, +- { S8003_EVENTS, true, 0x2c00 }, +- { S8003_LOGS, true, 0xb000 }, +- { 0x4f, true, 0x36000 }, +- { S8003_STATS, true, 0x2c00 }, +- { S8003_TRACE, true, 0x2400 }, +- { S8003_DC_CTL, true, S8003_RING_CTL_SIZE }, +- { S8003_DC_RING, true, 0x1400 }, +- { S8003_WORK_CTL, true, S8003_RING_CTL_SIZE }, +- { S8003_WORK_CTL + 4, true, S8003_RING_CTL_SIZE }, +- { S8003_WORK_CTL + 8, true, S8003_RING_CTL_SIZE }, +- { S8003_WORK_RING, true, 0x1400 }, +- { S8003_WORK_RING + 4, true, 0x1400 }, +- { S8003_WORK_RING + 8, true, 0x1400 }, +- { 0x5c, true, 8 }, +- { 0x85, true, 0x400 }, +- { 0x84, true, 0x400 }, +- { S8003_MMU_ROOT, true, 0x800 }, +- }; ++ const struct s8003_object_size *objects = v->objects; + u32 used[2] = {}; + unsigned int i; + +- for (i = 0; i < ARRAY_SIZE(objects); i++) { ++ for (i = 0; i < v->nr_objects; i++) { + u32 offset = ALIGN(used[objects[i].shared], 64); + + layout->offset[objects[i].object] = offset; + layout->shared[objects[i].object] = objects[i].shared; + used[objects[i].shared] = offset + objects[i].size; + } +- return used[0] <= S8003_ROOT_SIZE && used[1] <= S8003_TA_CHANNEL ? 0 : -EINVAL; ++ return used[0] <= v->root_objects_end && used[1] <= v->shared_objects_end ? 0 : -EINVAL; + } + + static u64 s8003_object_va(const struct s8003_layout *layout, u32 object) +@@ -483,12 +540,6 @@ static int s8003_ldr_start_threads(void *data, const u32 *words, u32 count) + return 0; + } + +-static const struct pvr_meta_ldr_target s8003_ldr_target = { +- .mem = s8003_ldr_mem, +- .config = s8003_ldr_config, +- .start_threads = s8003_ldr_start_threads, +-}; +- + /* + * The bootloader's configuration: the segment MMU's view of the image (code, + * data, the bootloader itself, one more data range), the image's own register +@@ -733,7 +784,8 @@ err_fini: + return err; + } + +-static void pvr_apple_s8003_fini(struct pvr_apple *apple) ++/* The registers' windows in the kernel VM */ ++static void s8003_unmap(struct pvr_apple *apple) + { + struct pvr_vm_context *vm_ctx = apple->pvr_dev->kernel_vm_ctx; + struct pvr_apple_s8003 *s = apple->chip_data; +@@ -743,6 +795,13 @@ static void pvr_apple_s8003_fini(struct pvr_apple *apple) + pvr_vm_unmap_io(vm_ctx, S8003_PIO_VA + S8003_PIO_AIC, S8003_PIO_SIZE); + s->pio_mapped = false; + } ++} ++ ++static void pvr_apple_s8003_fini(struct pvr_apple *apple) ++{ ++ struct pvr_apple_s8003 *s = apple->chip_data; ++ ++ s->v->unmap(apple); + if (s->park_vm) { + pvr_vm_context_put(s->park_vm); + s->park_vm = NULL; +@@ -986,7 +1045,7 @@ static int s8003_drain(struct pvr_apple *apple, bool *alive) + guard(mutex)(&s->event_lock); + for (lane = 0; lane < 4 && !err; lane++) + err = s8003_ring_discard(s8003_object(s, S8003_LOG_CTL) + +- lane * S8003_RING_CTL_SIZE); ++ lane * s->v->ring_ctl_size); + if (err) + return err; + +@@ -1151,21 +1210,20 @@ static u32 s8003_engine(enum pvr_apple_engine engine) + return engine == APPLE_ENGINE_GEOM ? 0 : engine == APPLE_ENGINE_FRAG ? 1 : 2; + } + +-static u32 s8003_channel_offset(enum pvr_apple_engine engine) ++static u32 s8003_channel_offset(const struct pvr_apple_s8003 *s, enum pvr_apple_engine engine) + { +- static const u32 offset[] = { S8003_TA_CHANNEL, S8003_3D_CHANNEL, S8003_CU_CHANNEL }; +- +- return offset[s8003_engine(engine)]; ++ return s->v->channel[s8003_engine(engine)]; + } + + static u8 *s8003_channel(struct pvr_apple_s8003 *s, enum pvr_apple_engine engine) + { +- return s->shared.map + s8003_channel_offset(engine); ++ return (s->v->channels_root ? s->root.map : s->shared.map) + s8003_channel_offset(s, engine); + } + +-static u32 s8003_channel_addr(enum pvr_apple_engine engine) ++static u32 s8003_channel_addr(const struct pvr_apple_s8003 *s, enum pvr_apple_engine engine) + { +- return s8003_meta_addr(S8003_SHARED_VA + s8003_channel_offset(engine)); ++ return s8003_meta_addr((s->v->channels_root ? S8003_ROOT_VA : S8003_SHARED_VA) + ++ s8003_channel_offset(s, engine)); + } + + /* A channel before its first job: where its parts are, an empty ring */ +@@ -1190,11 +1248,11 @@ static void s8003_channel_init(u8 *p, u32 addr, u32 tq, u32 dep) + static void s8003_channels_init(struct pvr_apple_s8003 *s) + { + s8003_channel_init(s8003_channel(s, APPLE_ENGINE_GEOM), +- s8003_channel_addr(APPLE_ENGINE_GEOM), S8003_CH_TQ, S8003_CH_DEP); ++ s8003_channel_addr(s, APPLE_ENGINE_GEOM), S8003_CH_TQ, S8003_CH_DEP); + s8003_channel_init(s8003_channel(s, APPLE_ENGINE_FRAG), +- s8003_channel_addr(APPLE_ENGINE_FRAG), S8003_CH_TQ, S8003_CH_DEP); ++ s8003_channel_addr(s, APPLE_ENGINE_FRAG), S8003_CH_TQ, S8003_CH_DEP); + s8003_channel_init(s8003_channel(s, APPLE_ENGINE_COMPUTE), +- s8003_channel_addr(APPLE_ENGINE_COMPUTE), S8003_CU_TQ, S8003_CU_DEP); ++ s8003_channel_addr(s, APPLE_ENGINE_COMPUTE), S8003_CU_TQ, S8003_CU_DEP); + } + + /* How often s8003_touch() runs while the firmware works for the host */ +@@ -1275,7 +1333,8 @@ static void pvr_apple_s8003_events(struct pvr_apple *apple) + if (!READ_ONCE(apple->booted) || !s->shared.map) + return; + /* An eighth early, so that a check every S8003_TOUCH_US always touches */ +- if (ktime_us_delta(now, READ_ONCE(s->touched)) + S8003_TOUCH_US / 8 >= S8003_TOUCH_US) { ++ if (s->v->touch && ++ ktime_us_delta(now, READ_ONCE(s->touched)) + S8003_TOUCH_US / 8 >= S8003_TOUCH_US) { + WRITE_ONCE(s->touched, now); + s8003_touch(s); + } +@@ -1300,7 +1359,7 @@ static u32 s8003_submitted(struct pvr_apple *apple, enum pvr_apple_engine engine + * end of the pointer ring and that of the last job submitted. With the + * firmware off nothing is in flight. + */ +-static u32 pvr_apple_s8003_engine_done(struct pvr_apple *apple, enum pvr_apple_engine engine) ++static u32 s8003_engine_done(struct pvr_apple *apple, enum pvr_apple_engine engine) + { + struct pvr_apple_s8003 *s = apple->chip_data; + u32 latest = s8003_submitted(apple, engine); +@@ -1327,6 +1386,13 @@ static u32 pvr_apple_s8003_engine_done(struct pvr_apple *apple, enum pvr_apple_e + return done; + } + ++static u32 pvr_apple_s8003_engine_done(struct pvr_apple *apple, enum pvr_apple_engine engine) ++{ ++ struct pvr_apple_s8003 *s = apple->chip_data; ++ ++ return s->v->engine_done(apple, engine); ++} ++ + static u32 s8003_done_poll(struct pvr_apple *apple, enum pvr_apple_engine engine) + { + pvr_apple_s8003_events(apple); +@@ -1401,7 +1467,7 @@ static int s8003_device_control(struct pvr_apple *apple, const u32 (*cmd)[2], un + WRITE_ONCE(*(__le32 *)(ctl + S8003_RING_PRODUCER), cpu_to_le32(target)); + mb(); /* the index is in memory before the firmware is told */ + +- err = s8003_doorbell(apple, ROGUE_CR_MTS_SCHEDULE_DM_DM0); ++ err = s->v->doorbell(apple, ROGUE_CR_MTS_SCHEDULE_DM_DM0); + if (err) + return err; + return read_poll_timeout(s8003_ring_consumer, consumer, consumer == target, 100, +@@ -1423,18 +1489,18 @@ static u64 s8003_slot_tag_bump(struct pvr_apple_s8003 *s, u32 slot) + return (u32)(next - (tag + S8003_TAG_CATALOGUE)) / S8003_TAG_CATALOGUE; + } + +-/* Point a pair of slots at a page-table root (0: at none) under @tag */ ++/* Point a VM's slots (a pair, or one) at a page-table root (0: at none) under @tag */ + static int s8003_slots_load(struct pvr_apple *apple, const u32 *slot, u64 root, u32 tag) + { +- const u32 cmd[2][2] = { { S8003_DC_SLOT, slot[1] }, { S8003_DC_SLOT, slot[0] } }; + struct pvr_apple_s8003 *s = apple->chip_data; ++ unsigned int e, n = s->v->vm_slots; ++ const u32 cmd[2][2] = { { S8003_DC_SLOT, slot[n - 1] }, { S8003_DC_SLOT, slot[0] } }; + unsigned long flags; + u64 steps = 0, total; +- unsigned int e; + int err; + + spin_lock_irqsave(&s->tag_lock, flags); +- for (e = 0; e < 2; e++) { ++ for (e = 0; e < n; e++) { + __set_bit(slot[e], s->loading); + put64(s8003_object(s, S8003_MMU_ROOT) + 8 * slot[e], root); + put32(s8003_object(s, S8003_TAG_HOST) + 4 * slot[e], tag); +@@ -1442,11 +1508,11 @@ static int s8003_slots_load(struct pvr_apple *apple, const u32 *slot, u64 root, + } + spin_unlock_irqrestore(&s->tag_lock, flags); + +- err = s8003_device_control(apple, cmd, 2); ++ err = s8003_device_control(apple, cmd, n); + if (err) + dev_err(apple_dev(apple), "GFX firmware slot load failed: %d\n", err); + /* The firmware must copy the tag we held stable while loading the root. */ +- for (e = 0; e < 2 && !err; e++) { ++ for (e = 0; e < n && !err; e++) { + u32 seen = get32(s8003_object(s, S8003_TAG_SEEN) + 4 * slot[e]); + + if (seen != tag) { +@@ -1458,7 +1524,7 @@ static int s8003_slots_load(struct pvr_apple *apple, const u32 *slot, u64 root, + } + /* The attach holds submit_lock: deferred flushes precede the VM's job. */ + spin_lock_irqsave(&s->tag_lock, flags); +- for (e = 0; e < 2; e++) { ++ for (e = 0; e < n; e++) { + __clear_bit(slot[e], s->loading); + if (test_bit(slot[e], s->bump_pending)) { + steps += s8003_slot_tag_bump(s, slot[e]); +@@ -1476,7 +1542,7 @@ static int s8003_slots_load(struct pvr_apple *apple, const u32 *slot, u64 root, + } + + /* +- * The slot pair of a VM for this start of the firmware. Slots are handed out ++ * The slots of a VM for this start of the firmware. Slots are handed out + * in turn, so that one a VM gave back is used again as late as possible; if + * it was left on the park tables (pvr_apple_s8003_vm_fini()) it is emptied + * first, as Apple's kext does when an address space goes. +@@ -1484,33 +1550,37 @@ static int s8003_slots_load(struct pvr_apple *apple, const u32 *slot, u64 root, + static int s8003_vm_attach(struct pvr_apple *apple, struct s8003_vm *vm) + { + struct pvr_apple_s8003 *s = apple->chip_data; ++ const struct s8003_variant *v = s->v; ++ bool parked = false; + unsigned int e; + int err = 0; + + lockdep_assert_held(&apple->submit_lock); + if (vm->attached == s->gen) + return 0; +- for (e = 2; e-- > 0 && !vm->slot[e];) { +- u32 slot = find_next_zero_bit(s->slots, S8003_SLOTS, s->next_slot); ++ for (e = v->vm_slots; e-- > 0 && !vm->slot[e];) { ++ u32 slot = find_next_zero_bit(s->slots, v->slots, s->next_slot); + +- if (slot >= S8003_SLOTS) +- slot = find_next_zero_bit(s->slots, S8003_SLOTS, S8003_SLOT_FIRST); +- if (slot >= S8003_SLOTS) { ++ if (slot >= v->slots) ++ slot = find_next_zero_bit(s->slots, v->slots, v->slot_first); ++ if (slot >= v->slots) { + if (vm->slot[1]) + __clear_bit(vm->slot[1], s->slots); + vm->slot[1] = 0; + return -EBUSY; + } + __set_bit(slot, s->slots); +- s->next_slot = slot + 1 < S8003_SLOTS ? slot + 1 : S8003_SLOT_FIRST; ++ s->next_slot = slot + 1 < v->slots ? slot + 1 : v->slot_first; + vm->slot[e] = slot; + } + /* No job runs while a slot changes */ + err = pvr_apple_wait_idle(apple, S8003_JOB_TIMEOUT_US); +- if (!err && (test_bit(vm->slot[0], s->parked) || test_bit(vm->slot[1], s->parked))) { ++ for (e = 0; e < v->vm_slots; e++) ++ parked |= test_bit(vm->slot[e], s->parked); ++ if (!err && parked) { + err = s8003_slots_load(apple, vm->slot, 0, 0); +- __clear_bit(vm->slot[0], s->parked); +- __clear_bit(vm->slot[1], s->parked); ++ for (e = 0; e < v->vm_slots; e++) ++ __clear_bit(vm->slot[e], s->parked); + } + if (!err) + err = s8003_slots_load(apple, vm->slot, +@@ -1535,7 +1605,7 @@ static void pvr_apple_s8003_mmu_flush(struct pvr_apple *apple) + if (!s->shared.map) + return; + spin_lock_irqsave(&s->tag_lock, flags); +- for (slot = S8003_SLOT_FIRST; slot < S8003_SLOTS; slot++) { ++ for (slot = s->v->slot_first; slot < s->v->slots; slot++) { + if (test_bit(slot, s->loading)) + __set_bit(slot, s->bump_pending); + else +@@ -1550,14 +1620,22 @@ static void pvr_apple_s8003_mmu_flush(struct pvr_apple *apple) + "MMU tag steps-past=%llu total=%llu\n", steps, total); + } + ++/* The PM reaches its sets through the job's slot, at the firmware's addresses */ ++static int s8003_vm_map(struct pvr_apple *apple, struct pvr_vm_context *vm_ctx) ++{ ++ struct pvr_apple_s8003 *s = apple->chip_data; ++ ++ if (!s->pmsets.obj) ++ return -ENODEV; ++ return pvr_vm_map_reserved(vm_ctx, s->pmsets.obj, S8003_PM_SETS_VA, s->pmsets.size); ++} ++ + static int pvr_apple_s8003_vm_init(struct pvr_apple *apple, struct pvr_vm_context *vm_ctx) + { + struct pvr_apple_s8003 *s = apple->chip_data; + struct s8003_vm *vm; + int err; + +- if (!s->pmsets.obj) +- return -ENODEV; + vm = kzalloc(sizeof(*vm), GFP_KERNEL); + if (!vm) + return -ENOMEM; +@@ -1565,8 +1643,7 @@ static int pvr_apple_s8003_vm_init(struct pvr_apple *apple, struct pvr_vm_contex + mutex_init(&vm->prepare_lock); + vm->pm[0].set = -1; + vm->pm[1].set = -1; +- /* The PM reaches its sets through the job's slot, at the firmware's addresses */ +- err = pvr_vm_map_reserved(vm_ctx, s->pmsets.obj, S8003_PM_SETS_VA, s->pmsets.size); ++ err = s->v->vm_map(apple, vm_ctx); + if (!err) + err = xa_err(xa_store(&s->vms, (unsigned long)vm_ctx, vm, GFP_KERNEL)); + if (err) +@@ -1675,8 +1752,8 @@ static void pvr_apple_s8003_vm_fini(struct pvr_apple *apple, struct pvr_vm_conte + !pvr_apple_wait_idle(apple, S8003_JOB_TIMEOUT_US) && + !s8003_slots_load(apple, vm->slot, pvr_vm_get_page_table_root_addr(s->park_vm), + S8003_TAG_PARK)) { +- __set_bit(vm->slot[0], s->parked); +- __set_bit(vm->slot[1], s->parked); ++ for (e = 0; e < s->v->vm_slots; e++) ++ __set_bit(vm->slot[e], s->parked); + } + if (s->pm_last == vm) + s->pm_last = NULL; +@@ -1686,12 +1763,23 @@ static void pvr_apple_s8003_vm_fini(struct pvr_apple *apple, struct pvr_vm_conte + for (e = 0; e < 2; e++) { + if (vm->slot[e]) + __clear_bit(vm->slot[e], s->slots); +- s8003_pm_put(s, &vm->pm[e]); + } ++ s->v->vm_put(apple, vm); + mutex_unlock(&apple->submit_lock); ++ if (s->v->vm_unmap) ++ s->v->vm_unmap(apple, vm_ctx); + kfree(vm); + } + ++/* A VM's PM sets go with it */ ++static void s8003_vm_put(struct pvr_apple *apple, struct s8003_vm *vm) ++{ ++ unsigned int e; ++ ++ for (e = 0; e < 2; e++) ++ s8003_pm_put(apple->chip_data, &vm->pm[e]); ++} ++ + static u64 s8003_pm_set_va(const struct s8003_pm *pm, u32 page) + { + return S8003_PM_SETS_VA + pm->set * S8003_PM_SET_SIZE + page * PVR_DEVICE_PAGE_SIZE; +@@ -2224,7 +2312,7 @@ static int s8003_publish(struct pvr_apple *apple, enum pvr_apple_engine engine, + entry = ring + start * S8003_DC_SIZE; + memset(entry, 0, S8003_DC_SIZE); + put32(entry, fw_engine); +- put32(entry + 4, s8003_channel_addr(engine)); ++ put32(entry + 4, s8003_channel_addr(s, engine)); + put32(entry + 8, end); + wmb(); + put32(p + S8003_CH_CTL + S8003_CH_CTL_HOST, end); +@@ -2524,10 +2612,10 @@ static int s8003_submit_geom(struct pvr_apple *apple, struct s8003_vm *vm, struc + pm_record = true; + } + s8003_fill_ta(p + s8003_slot_of(seq, false).w, job->cmd, job->hwrt, &rt); +- s8003_job_write(p, s8003_channel_addr(APPLE_ENGINE_GEOM), ++ s8003_job_write(p, s8003_channel_addr(s, APPLE_ENGINE_GEOM), + s8003_object_addr(&s->layout, S8003_CHANNEL), + s8003_meta_addr(s8003_pm_set_va(pm, 0)), pm->set + 1, vm->slot[0], false, +- seq, s8003_channel_addr(APPLE_ENGINE_GEOM) + S8003_CH_STAMP + 4, ++ seq, s8003_channel_addr(s, APPLE_ENGINE_GEOM) + S8003_CH_STAMP + 4, + pm_record ? 0 : pm->rec_seq, pm_record); + if (pm_record) + pm->rec_seq = seq; +@@ -2578,10 +2666,10 @@ static int s8003_submit_frag(struct pvr_apple *apple, struct s8003_vm *vm, struc + err = s8003_fill_3d(p + s8003_slot_of(seq, true).w, job->cmd, job->hwrt, &rt); + if (err) + return s8003_submit_error(apple, vm, "fragment", err, "3D record fill"); +- s8003_job_write(p, s8003_channel_addr(APPLE_ENGINE_FRAG), ++ s8003_job_write(p, s8003_channel_addr(s, APPLE_ENGINE_FRAG), + s8003_object_addr(&s->layout, S8003_CHANNEL + 1), + s8003_meta_addr(s8003_pm_set_va(pm, 0)), pm->set + 1, vm->slot[1], true, seq, +- s8003_channel_addr(APPLE_ENGINE_GEOM) + S8003_CH_STAMP + 4, pm->ta_seq, true); ++ s8003_channel_addr(s, APPLE_ENGINE_GEOM) + S8003_CH_STAMP + 4, pm->ta_seq, true); + pm->td_seq = seq; + WRITE_ONCE(s->set_frag[pm->set], seq); + apple->frag_seq = seq; +@@ -2602,7 +2690,7 @@ static int s8003_submit_compute(struct pvr_apple *apple, struct s8003_vm *vm, st + const struct pvr_apple_g5p_cmd_compute *cmd = job->cmd; + const struct pvr_apple_g5p_compute_regs *g = &cmd->g5p; + struct pvr_apple_s8003 *s = apple->chip_data; +- u32 addr = s8003_channel_addr(APPLE_ENGINE_COMPUTE); ++ u32 addr = s8003_channel_addr(s, APPLE_ENGINE_COMPUTE); + u32 global = s8003_object_addr(&s->layout, S8003_CHANNEL + 2); + u32 w = addr + S8003_CH_W, slot = vm->slot[0], seq; + u8 *p = s8003_channel(s, APPLE_ENGINE_COMPUTE); +@@ -2678,17 +2766,17 @@ static int pvr_apple_s8003_submit(struct pvr_apple *apple, struct pvr_job *job) + if (err) + goto out_unlock; + if (job->type == DRM_PVR_JOB_TYPE_GEOMETRY) +- err = s8003_submit_geom(apple, vm, job); ++ err = s->v->submit_geom(apple, vm, job); + else if (job->type == DRM_PVR_JOB_TYPE_FRAGMENT) +- err = s8003_submit_frag(apple, vm, job); ++ err = s->v->submit_frag(apple, vm, job); + else +- err = s8003_submit_compute(apple, vm, job); ++ err = s->v->submit_compute(apple, vm, job); + out_unlock: + mutex_unlock(&apple->submit_lock); + return err; + } + +-static void pvr_apple_s8003_report_hang(struct pvr_apple *apple) ++static void s8003_report_hang(struct pvr_apple *apple) + { + static const char *const names[] = { "compute", "geometry", "fragment" }; + struct pvr_apple_s8003 *s = apple->chip_data; +@@ -2754,8 +2842,9 @@ static void s8003_jobs_reset(struct pvr_apple_s8003 *s) + WRITE_ONCE(s->apple->fault, false); + memset(s->set_geom, 0, sizeof(s->set_geom)); + memset(s->set_frag, 0, sizeof(s->set_frag)); +- memset(s->pmsets.map, 0, s->pmsets.size); +- s8003_channels_init(s); ++ if (s->pmsets.map) ++ memset(s->pmsets.map, 0, s->pmsets.size); ++ s->v->channels_init(s); + } + + /* +@@ -2834,28 +2923,33 @@ static void s8003_irq_init(struct pvr_apple *apple) + dev_warn(dev, "firmware interrupt not taken (%d): jobs are polled for\n", err); + } + ++static const struct s8003_variant *s8003_variant_of(const struct pvr_apple_chip *chip); ++ + static int pvr_apple_s8003_init(struct pvr_apple *apple) + { ++ const struct s8003_variant *v = s8003_variant_of(apple->chip); + struct device *dev = apple_dev(apple); + struct pvr_apple_layout *layout = &apple->layout; + struct pvr_apple_s8003 *s; + unsigned int i; + int err; + +- /* The firmware's page tables have 16 KiB pages, as the kernel's must */ +- if (PVR_DEVICE_PAGE_SIZE != SZ_16K) +- return dev_err_probe(dev, -ENODEV, "the A9 GPU needs a 16 KiB page kernel\n"); ++ /* The firmware's page tables have the chip's page size, as the kernel's must */ ++ if (PVR_DEVICE_PAGE_SIZE != v->page_size) ++ return dev_err_probe(dev, -ENODEV, "the %s GPU needs a %u KiB page kernel\n", ++ v->name, v->page_size / SZ_1K); + + s = devm_kzalloc(dev, sizeof(*s), GFP_KERNEL); + if (!s) + return -ENOMEM; + apple->chip_data = s; + s->apple = apple; ++ s->v = v; + INIT_DELAYED_WORK(&s->drain_work, s8003_drain_work); + spin_lock_init(&s->tag_lock); + spin_lock_init(&s->set_lock); + xa_init(&s->vms); +- s->next_slot = S8003_SLOT_FIRST; ++ s->next_slot = v->slot_first; + err = devm_mutex_init(dev, &s->event_lock); + if (err) + return err; +@@ -2863,12 +2957,17 @@ static int pvr_apple_s8003_init(struct pvr_apple *apple) + err = s8003_parse_dt(apple); + if (err) + return err; ++ if (v->init) { ++ err = v->init(apple); ++ if (err) ++ return err; ++ } + s8003_irq_init(apple); +- err = s8003_layout_plan(&s->layout); ++ err = s8003_layout_plan(v, &s->layout); + if (err) + return err; + +- s->image = kvzalloc(S8003_IMAGE_SIZE, GFP_KERNEL); ++ s->image = kvzalloc(v->image_size, GFP_KERNEL); + if (!s->image) + return -ENOMEM; + err = devm_add_action_or_reset(dev, s8003_free_image, s->image); +@@ -2878,12 +2977,12 @@ static int pvr_apple_s8003_init(struct pvr_apple *apple) + if (err) + return err; + err = pvr_meta_ldr_process(from_pvr_device(apple->pvr_dev), apple->fw_image, +- apple->fw_size, &s8003_ldr_target, s, NULL); +- if (!err && !s->thread_pc) ++ apple->fw_size, v->ldr, s, NULL); ++ if (!err && (!s->thread_pc || (v->ldr_conf_count && s->conf_count != v->ldr_conf_count))) + err = -EINVAL; + if (err) +- return dev_err_probe(dev, err, "not the A9's GFX firmware image\n"); +- s8003_image_configure(s); ++ return dev_err_probe(dev, err, "not the %s's GFX firmware image\n", v->name); ++ v->image_configure(s); + + apple->heap = &s->idle; + for (i = 0; i < APPLE_ENGINES; i++) { +@@ -2903,7 +3002,7 @@ static int pvr_apple_s8003_init(struct pvr_apple *apple) + * Wake Fender and power the core in state 1, the META held in reset: the + * Rogue registers answer (the core's ID is read next), no firmware runs. + */ +-static int pvr_apple_s8003_boot(struct pvr_apple *apple) ++static int s8003_boot(struct pvr_apple *apple) + { + int err; + +@@ -2916,32 +3015,41 @@ static int pvr_apple_s8003_boot(struct pvr_apple *apple) + return 0; + } + ++static int pvr_apple_s8003_boot(struct pvr_apple *apple) ++{ ++ struct pvr_apple_s8003 *s = apple->chip_data; ++ ++ return s->v->boot(apple); ++} ++ + static int pvr_apple_s8003_start(struct pvr_apple *apple) + { + struct pvr_apple_s8003 *s = apple->chip_data; ++ const struct s8003_variant *v = s->v; + u32 q = pvr_apple_start_power_state(apple); + int err; + + if (!s->shared.obj) { +- err = s8003_map(apple); ++ err = v->map(apple); + if (err) + return err; + } + /* The firmware writes into its image while it runs: start from the copy */ +- memcpy(s->fw.map, s->image, S8003_IMAGE_SIZE); +- s8003_init_build(apple, q); ++ memcpy(s->fw.map, s->image, v->image_size); ++ v->init_build(apple, q); + s8003_jobs_reset(s); + +- err = s8003_release(apple, q); ++ err = v->release(apple, q); + if (!err) + err = s8003_wait_alive(apple); + if (!err) { + dev_dbg(apple_dev(apple), "GFX firmware is up (power state %u)\n", q); +- err = s8003_first_command(apple); ++ if (v->first_command) ++ err = v->first_command(apple); + } + if (err) { + /* It must not run on once the kernel VM, its page tables, may go */ +- s8003_core_off(apple); ++ v->core_off(apple); + return err; + } + dev_dbg(apple_dev(apple), "GFX firmware takes commands\n"); +@@ -2973,14 +3081,21 @@ static void pvr_apple_s8003_stop(struct pvr_apple *apple, bool recover) + cancel_delayed_work_sync(&s->drain_work); + if (pm_runtime_status_suspended(apple_dev(apple))) + return; +- s8003_core_off(apple); ++ s->v->core_off(apple); + } + +-static void pvr_apple_s8003_idle(struct pvr_apple *apple) ++static void s8003_idle(struct pvr_apple *apple) + { + /* nothing of the firmware's keeps the GPU's power domain on */ + } + ++static void pvr_apple_s8003_idle(struct pvr_apple *apple) ++{ ++ struct pvr_apple_s8003 *s = apple->chip_data; ++ ++ s->v->idle(apple); ++} ++ + static void pvr_apple_s8003_restore_image(struct pvr_apple *apple) + { + /* start() copies the image and builds the init data anew each time */ +@@ -2993,6 +3108,70 @@ static u32 pvr_apple_s8003_max_power_state(struct pvr_apple *apple) + return get32(s8003_object(s, S8003_PLATFORM) + 0x710); + } + ++static void pvr_apple_s8003_report_hang(struct pvr_apple *apple) ++{ ++ struct pvr_apple_s8003 *s = apple->chip_data; ++ ++ s->v->report_hang(apple); ++} ++ ++static int pvr_apple_s8003_prepare_job(struct pvr_apple *apple, struct pvr_job *job) ++{ ++ struct pvr_apple_s8003 *s = apple->chip_data; ++ ++ return s->v->prepare_job(apple, job); ++} ++ ++static const struct pvr_meta_ldr_target s8003_ldr_target = { ++ .mem = s8003_ldr_mem, ++ .config = s8003_ldr_config, ++ .start_threads = s8003_ldr_start_threads, ++}; ++ ++/* The A9: its values are the S8003_* constants, its steps the s8003_* functions */ ++static const struct s8003_variant s8003_variant_a9 = { ++ .name = "A9", ++ .page_size = SZ_16K, ++ .image_size = S8003_IMAGE_SIZE, ++ .root_size = S8003_ROOT_SIZE, ++ .shared_size = S8003_SHARED_SIZE, ++ .root_objects_end = S8003_ROOT_SIZE, ++ .shared_objects_end = S8003_TA_CHANNEL, ++ .objects = s8003_objects_a9, ++ .nr_objects = ARRAY_SIZE(s8003_objects_a9), ++ .ring_ctl_size = S8003_RING_CTL_SIZE, ++ .slots = S8003_SLOTS, ++ .slot_first = S8003_SLOT_FIRST, ++ .vm_slots = 2, ++ .channel = { S8003_TA_CHANNEL, S8003_3D_CHANNEL, S8003_CU_CHANNEL }, ++ .touch = true, ++ .ldr = &s8003_ldr_target, ++ .image_configure = s8003_image_configure, ++ .map = s8003_map, ++ .unmap = s8003_unmap, ++ .init_build = s8003_init_build, ++ .channels_init = s8003_channels_init, ++ .boot = s8003_boot, ++ .release = s8003_release, ++ .core_off = s8003_core_off, ++ .idle = s8003_idle, ++ .doorbell = s8003_doorbell, ++ .first_command = s8003_first_command, ++ .vm_map = s8003_vm_map, ++ .vm_put = s8003_vm_put, ++ .engine_done = s8003_engine_done, ++ .prepare_job = s8003_prepare_job, ++ .submit_geom = s8003_submit_geom, ++ .submit_frag = s8003_submit_frag, ++ .submit_compute = s8003_submit_compute, ++ .report_hang = s8003_report_hang, ++}; ++ ++static const struct s8003_variant *s8003_variant_of(const struct pvr_apple_chip *chip) ++{ ++ return &s8003_variant_a9; ++} ++ + const struct pvr_apple_chip pvr_apple_chip_s8003 = { + .fw_name = "apple/gfx-s8003.bin", + .perf_state_max = S8003_PERF_STATE_MAX, +@@ -3016,7 +3195,7 @@ const struct pvr_apple_chip pvr_apple_chip_s8003 = { + .fini = pvr_apple_s8003_fini, + .restore_image = pvr_apple_s8003_restore_image, + .report_hang = pvr_apple_s8003_report_hang, +- .prepare_job = s8003_prepare_job, ++ .prepare_job = pvr_apple_s8003_prepare_job, + .submit = pvr_apple_s8003_submit, + .engine_done = pvr_apple_s8003_engine_done, + .max_power_state = pvr_apple_s8003_max_power_state, diff --git a/kernel/patches/drivers/gpu/0064-drm-imagination-apple-Let-a-chip-read-its-GPU-ID-and.patch b/kernel/patches/drivers/gpu/0064-drm-imagination-apple-Let-a-chip-read-its-GPU-ID-and.patch new file mode 100644 index 00000000..76f587c4 --- /dev/null +++ b/kernel/patches/drivers/gpu/0064-drm-imagination-apple-Let-a-chip-read-its-GPU-ID-and.patch @@ -0,0 +1,170 @@ +From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 +From: =?UTF-8?q?Berkay=20K=C3=B6sebay?= +Date: Sat, 21 Nov 2026 12:02:00 +0100 +Subject: [PATCH] drm/imagination: apple: Let a chip read its GPU ID and lay + out its heaps + +Two things the next chip, the A8 (T7000), does its own way: + +- Its Rogue registers may be read only while the core is in Fender + state 1 and the GPU's PLL is locked; an access outside that hangs the + whole SoC. Its ID is CORE_REVISION and CORE_ID, read as 32-bit words. + A chip can now provide read_gpu_id(), which pvr_load_gpu_id() uses in + place of the generic register decode; the gpuid module parameter + still overrides both. + +- Its firmware puts the code heaps at 0x3_00000000 and the general heap + at 0x1_00000000, not below powervr's Apple heaps. A chip can now give + its own heap table, which DEV_QUERY and every heap check use. + +The A9 and A10 provide neither, so their ID read and heaps are as before. + +Assisted-by: LLM +--- + drivers/gpu/drm/imagination/pvr_apple.c | 28 ++++++++++++++++++++ + drivers/gpu/drm/imagination/pvr_apple.h | 12 +++++++++ + drivers/gpu/drm/imagination/pvr_apple_chip.h | 7 +++++ + drivers/gpu/drm/imagination/pvr_device.c | 8 +++++- + drivers/gpu/drm/imagination/pvr_vm.c | 8 +++++- + 5 files changed, 61 insertions(+), 2 deletions(-) + +diff --git a/drivers/gpu/drm/imagination/pvr_apple.c b/drivers/gpu/drm/imagination/pvr_apple.c +index 2f3105c6a3..56e8ac1f93 100644 +--- a/drivers/gpu/drm/imagination/pvr_apple.c ++++ b/drivers/gpu/drm/imagination/pvr_apple.c +@@ -2125,6 +2125,34 @@ void pvr_apple_device_info_init(struct pvr_device *pvr_dev) + pvr_dev->features.isp_samples_per_pixel = 2; + } + ++/** ++ * pvr_apple_read_gpu_id() - Read the GPU ID the chip's own way ++ * @pvr_dev: Target device, powered. ++ * @gpu_id: Where the ID goes. ++ * ++ * Return: 0, -%ENOENT for a chip whose ID is read from the Rogue registers ++ * as on any Rogue core, or another negative error. ++ */ ++int pvr_apple_read_gpu_id(struct pvr_device *pvr_dev, struct pvr_gpu_id *gpu_id) ++{ ++ const struct pvr_apple_chip *chip = pvr_dev->device_data->apple_chip; ++ ++ if (!pvr_device_is_apple(pvr_dev) || !chip->read_gpu_id) ++ return -ENOENT; ++ return chip->read_gpu_id(to_apple(pvr_dev), gpu_id); ++} ++ ++/** ++ * pvr_apple_chip_heaps() - The chip's own userspace heaps ++ * @pvr_dev: Target device. ++ * ++ * Return: the chip's heap table, or %NULL for powervr's Apple heaps. ++ */ ++const struct drm_pvr_heap *pvr_apple_chip_heaps(const struct pvr_device *pvr_dev) ++{ ++ return pvr_dev->device_data->apple_chip->heaps; ++} ++ + /** + * pvr_apple_takes_jobs() - Whether the chip's firmware is given jobs + * @pvr_dev: Target device. +diff --git a/drivers/gpu/drm/imagination/pvr_apple.h b/drivers/gpu/drm/imagination/pvr_apple.h +index 09a21e5d93..850c4e8d02 100644 +--- a/drivers/gpu/drm/imagination/pvr_apple.h ++++ b/drivers/gpu/drm/imagination/pvr_apple.h +@@ -14,7 +14,9 @@ + + struct dentry; + struct dma_fence; ++struct drm_pvr_heap; + struct pvr_apple_chip; ++struct pvr_gpu_id; + struct pvr_context; + struct pvr_device; + struct pvr_job; +@@ -168,6 +170,8 @@ int pvr_apple_reset(struct pvr_device *pvr_dev); + int pvr_apple_suspend(struct pvr_device *pvr_dev); + int pvr_apple_resume(struct pvr_device *pvr_dev); + void pvr_apple_device_info_init(struct pvr_device *pvr_dev); ++int pvr_apple_read_gpu_id(struct pvr_device *pvr_dev, struct pvr_gpu_id *gpu_id); ++const struct drm_pvr_heap *pvr_apple_chip_heaps(const struct pvr_device *pvr_dev); + bool pvr_apple_takes_jobs(struct pvr_device *pvr_dev); + bool pvr_apple_free_list_has_pages(struct pvr_device *pvr_dev); + u32 pvr_apple_stream_musthave(struct pvr_device *pvr_dev, enum pvr_stream_type type, +@@ -208,6 +212,14 @@ static inline int pvr_apple_reset(struct pvr_device *pvr_dev) { return -ENODEV; + static inline int pvr_apple_suspend(struct pvr_device *pvr_dev) { return 0; } + static inline int pvr_apple_resume(struct pvr_device *pvr_dev) { return 0; } + static inline void pvr_apple_device_info_init(struct pvr_device *pvr_dev) {} ++static inline int pvr_apple_read_gpu_id(struct pvr_device *pvr_dev, struct pvr_gpu_id *gpu_id) ++{ ++ return -ENOENT; ++} ++static inline const struct drm_pvr_heap *pvr_apple_chip_heaps(const struct pvr_device *pvr_dev) ++{ ++ return NULL; ++} + static inline bool pvr_apple_takes_jobs(struct pvr_device *pvr_dev) { return false; } + static inline bool pvr_apple_free_list_has_pages(struct pvr_device *pvr_dev) { return true; } + static inline u32 pvr_apple_stream_musthave(struct pvr_device *pvr_dev, +diff --git a/drivers/gpu/drm/imagination/pvr_apple_chip.h b/drivers/gpu/drm/imagination/pvr_apple_chip.h +index 7ee08eae97..655fa5b375 100644 +--- a/drivers/gpu/drm/imagination/pvr_apple_chip.h ++++ b/drivers/gpu/drm/imagination/pvr_apple_chip.h +@@ -220,6 +220,11 @@ struct pvr_apple { + * @write_geom: Write geometry job @seq and its work-ring entry. + * @write_frag: Write fragment job @seq and its work-ring entry. + * @report_hang: Log what else the firmware recorded about a job that timed out. ++ * @heaps: The userspace heaps (DRM_PVR_HEAP_*), if not powervr's Apple heaps ++ * below 2^38. Optional. ++ * @read_gpu_id: Read the GPU's ID (BVNC) the chip's own way, with the core ++ * powered by @boot. Optional: the Rogue registers are read as on any ++ * Rogue core. + * + * The functions from @kick to @write_frag are for a firmware that takes one + * command per engine at a time from a fixed place, named by the work ring's +@@ -277,6 +282,8 @@ struct pvr_apple_chip { + unsigned int gate_off_ms; + /* With the firmware's interrupt taken: microseconds between checks while jobs are pending */ + unsigned int irq_poll_us; ++ const struct drm_pvr_heap *heaps; ++ int (*read_gpu_id)(struct pvr_apple *apple, struct pvr_gpu_id *gpu_id); + + int (*init)(struct pvr_apple *apple); + int (*boot)(struct pvr_apple *apple); +diff --git a/drivers/gpu/drm/imagination/pvr_device.c b/drivers/gpu/drm/imagination/pvr_device.c +index e5065f8042..01f9f6041c 100644 +--- a/drivers/gpu/drm/imagination/pvr_device.c ++++ b/drivers/gpu/drm/imagination/pvr_device.c +@@ -605,7 +605,13 @@ pvr_load_gpu_id(struct pvr_device *pvr_dev) + struct pvr_gpu_id *gpu_id = &pvr_dev->gpu_id; + + if (!pvr_gpuid_override || !pvr_gpuid_override[0]) { +- pvr_gpuid_decode_reg(pvr_dev, gpu_id); ++ /* An Apple chip may read its ID its own way */ ++ int err = pvr_apple_read_gpu_id(pvr_dev, gpu_id); ++ ++ if (err == -ENOENT) ++ pvr_gpuid_decode_reg(pvr_dev, gpu_id); ++ else if (err) ++ return err; + } else { + drm_warn(from_pvr_device(pvr_dev), + "Using custom GPU ID (BVNC) provided by the user!"); +diff --git a/drivers/gpu/drm/imagination/pvr_vm.c b/drivers/gpu/drm/imagination/pvr_vm.c +index 37cf116ce0..05b1b965b2 100644 +--- a/drivers/gpu/drm/imagination/pvr_vm.c ++++ b/drivers/gpu/drm/imagination/pvr_vm.c +@@ -1250,7 +1250,13 @@ static_assert(ARRAY_SIZE(pvr_apple_heaps) == ARRAY_SIZE(pvr_heaps)); + + static const struct drm_pvr_heap *pvr_device_heaps(const struct pvr_device *pvr_dev) + { +- return pvr_device_is_apple(pvr_dev) ? pvr_apple_heaps : pvr_heaps; ++ const struct drm_pvr_heap *heaps; ++ ++ if (!pvr_device_is_apple(pvr_dev)) ++ return pvr_heaps; ++ /* A chip may lay its heaps out as its firmware does */ ++ heaps = pvr_apple_chip_heaps(pvr_dev); ++ return heaps ?: pvr_apple_heaps; + } + + int diff --git a/kernel/patches/drivers/gpu/0065-dt-bindings-gpu-apple-s8003-gpu-Add-the-A8-T7000-GPU.patch b/kernel/patches/drivers/gpu/0065-dt-bindings-gpu-apple-s8003-gpu-Add-the-A8-T7000-GPU.patch new file mode 100644 index 00000000..7503f2cc --- /dev/null +++ b/kernel/patches/drivers/gpu/0065-dt-bindings-gpu-apple-s8003-gpu-Add-the-A8-T7000-GPU.patch @@ -0,0 +1,125 @@ +From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 +From: =?UTF-8?q?Berkay=20K=C3=B6sebay?= +Date: Sat, 21 Nov 2026 12:03:00 +0100 +Subject: [PATCH] dt-bindings: gpu: apple,s8003-gpu: Add the A8 (T7000) GPU + +The A8's GPU, a GX6450 (Apple's G4P), runs the A9's family of GFX +firmware on its own META processor and is booted the same way. Its +driver powers it in the order Apple's kernel does: the power manager's +two global enables (clock gating, the SoC voltage manager), the GPU +power state, the stores to the two bus bridges in front of the GPU, then +Fender. Every access to the Rogue registers waits for the GPU's PLL to +lock, which the power manager reports. So the node names the power +manager's and the bridges' registers and has no power domain. + +Assisted-by: LLM +--- + .../bindings/gpu/apple,s8003-gpu.yaml | 65 ++++++++++++++++++- + 1 file changed, 62 insertions(+), 3 deletions(-) + +diff --git a/Documentation/devicetree/bindings/gpu/apple,s8003-gpu.yaml b/Documentation/devicetree/bindings/gpu/apple,s8003-gpu.yaml +index b29f915299..677ac589ee 100644 +--- a/Documentation/devicetree/bindings/gpu/apple,s8003-gpu.yaml ++++ b/Documentation/devicetree/bindings/gpu/apple,s8003-gpu.yaml +@@ -4,7 +4,7 @@ + $id: http://devicetree.org/schemas/gpu/apple,s8003-gpu.yaml# + $schema: http://devicetree.org/meta-schemas/core.yaml# + +-title: Apple S8003 (A9) GPU ++title: Apple S8003 (A9) and T7000 (A8) GPU + + maintainers: + - Berkay Kösebay +@@ -15,19 +15,33 @@ description: | + META processor; the host loads it. The node has the shape of the A10's + (apple,t8010-gpu), without that chip's coprocessor and mailbox. + ++ The A8's GPU (GX6450, Apple's G4P) runs the same family of firmware the ++ same way. Its driver powers the GPU itself, in the order Apple's kernel ++ does: the power manager's GPU power state, its PLL and two of its global ++ enables, and the two bus bridges in front of the GPU, so the node names ++ those registers instead of a power domain. ++ + properties: + compatible: +- const: apple,s8003-gpu ++ enum: ++ - apple,s8003-gpu ++ - apple,t7000-gpu + + reg: ++ minItems: 2 + items: + - description: The Rogue core's registers + - description: Fender ++ - description: The power manager's registers (A8) ++ - description: The GPU's two bus bridges (A8) + + reg-names: ++ minItems: 2 + items: + - const: rgx + - const: fender ++ - const: pmgr ++ - const: bridges + + interrupts: + items: +@@ -57,7 +71,27 @@ required: + - reg + - reg-names + - interrupts +- - power-domains ++ ++allOf: ++ - if: ++ properties: ++ compatible: ++ const: apple,t7000-gpu ++ then: ++ properties: ++ reg: ++ minItems: 4 ++ reg-names: ++ minItems: 4 ++ power-domains: false ++ else: ++ properties: ++ reg: ++ maxItems: 2 ++ reg-names: ++ maxItems: 2 ++ required: ++ - power-domains + + additionalProperties: false + +@@ -85,3 +119,28 @@ examples: + firmware-name = "apple/gfx-s8003.bin"; + }; + }; ++ ++ - | ++ #include ++ #include ++ ++ soc { ++ #address-cells = <2>; ++ #size-cells = <2>; ++ ++ gpu@201000000 { ++ compatible = "apple,t7000-gpu"; ++ reg = <0x2 0x01000000 0x0 0x100000>, ++ <0x2 0x01e00000 0x0 0x100000>, ++ <0x2 0x0e000000 0x0 0x40000>, ++ <0x2 0x01f00000 0x0 0x81000>; ++ reg-names = "rgx", "fender", "pmgr", "bridges"; ++ interrupt-parent = <&aic>; ++ interrupts = , ++ , ++ , ++ , ++ ; ++ firmware-name = "apple/gfx-t7000.bin"; ++ }; ++ }; diff --git a/kernel/patches/drivers/gpu/0066-drm-imagination-Allow-a-GPU-node-without-power-domai.patch b/kernel/patches/drivers/gpu/0066-drm-imagination-Allow-a-GPU-node-without-power-domai.patch new file mode 100644 index 00000000..0b68224a --- /dev/null +++ b/kernel/patches/drivers/gpu/0066-drm-imagination-Allow-a-GPU-node-without-power-domai.patch @@ -0,0 +1,82 @@ +From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 +From: =?UTF-8?q?Berkay=20K=C3=B6sebay?= +Date: Sat, 21 Nov 2026 12:04:00 +0100 +Subject: [PATCH] drm/imagination: Allow a GPU node without power-domains, and + say which probe step failed + +pvr_power_domains_init() returned of_count_phandle_with_args()'s -ENOENT +when the node has no power-domains property, with no message. The A8 +(T7000) node has none: its chip powers the GPU up itself, in Apple's +order. A node without the property has no domains to attach. + +Name the step in pvr_probe() when one fails: the A8's first probe failed +with only "probe with driver powervr failed with error -2". + +Nodes with power domains take the same path as before. + +Assisted-by: LLM +--- + drivers/gpu/drm/imagination/pvr_drv.c | 14 ++++++++++---- + drivers/gpu/drm/imagination/pvr_power.c | 4 ++++ + 2 files changed, 14 insertions(+), 4 deletions(-) + +diff --git a/drivers/gpu/drm/imagination/pvr_drv.c b/drivers/gpu/drm/imagination/pvr_drv.c +index f27d9b3151..d97e9d59f7 100644 +--- a/drivers/gpu/drm/imagination/pvr_drv.c ++++ b/drivers/gpu/drm/imagination/pvr_drv.c +@@ -1431,15 +1431,17 @@ pvr_probe(struct platform_device *plat_dev) + + err = pvr_power_domains_init(pvr_dev); + if (err) +- return err; ++ return dev_err_probe(&plat_dev->dev, err, "power domains\n"); + + init_rwsem(&pvr_dev->reset_sem); + + pvr_context_device_init(pvr_dev); + + err = pvr_queue_device_init(pvr_dev); +- if (err) ++ if (err) { ++ dev_err_probe(&plat_dev->dev, err, "queue init\n"); + goto err_context_fini; ++ } + + devm_pm_runtime_enable(&plat_dev->dev); + pm_runtime_mark_last_busy(&plat_dev->dev); +@@ -1449,12 +1451,16 @@ pvr_probe(struct platform_device *plat_dev) + pvr_watchdog_init(pvr_dev); + + err = pvr_device_init(pvr_dev); +- if (err) ++ if (err) { ++ dev_err_probe(&plat_dev->dev, err, "device init\n"); + goto err_watchdog_fini; ++ } + + err = drm_dev_register(drm_dev, 0); +- if (err) ++ if (err) { ++ dev_err_probe(&plat_dev->dev, err, "DRM device registration\n"); + goto err_device_fini; ++ } + + xa_init_flags(&pvr_dev->free_list_ids, XA_FLAGS_ALLOC1); + xa_init_flags(&pvr_dev->job_ids, XA_FLAGS_ALLOC1); +diff --git a/drivers/gpu/drm/imagination/pvr_power.c b/drivers/gpu/drm/imagination/pvr_power.c +index 54dba0ae18..b2b94de98a 100644 +--- a/drivers/gpu/drm/imagination/pvr_power.c ++++ b/drivers/gpu/drm/imagination/pvr_power.c +@@ -643,8 +643,12 @@ int pvr_power_domains_init(struct pvr_device *pvr_dev) + + domain_count = of_count_phandle_with_args(dev->of_node, "power-domains", + "#power-domain-cells"); ++ /* No power-domains property: no domains (the A8 powers itself up) */ ++ if (domain_count == -ENOENT) ++ domain_count = 0; + if (domain_count < 0) { + err = domain_count; ++ drm_err(drm_dev, "cannot count the power domains (%d)\n", err); + goto out; + } + diff --git a/kernel/patches/drivers/gpu/0067-drm-imagination-apple-Boot-the-A8-T7000-GPU-firmware.patch b/kernel/patches/drivers/gpu/0067-drm-imagination-apple-Boot-the-A8-T7000-GPU-firmware.patch new file mode 100644 index 00000000..fe721499 --- /dev/null +++ b/kernel/patches/drivers/gpu/0067-drm-imagination-apple-Boot-the-A8-T7000-GPU-firmware.patch @@ -0,0 +1,1433 @@ +From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 +From: =?UTF-8?q?Berkay=20K=C3=B6sebay?= +Date: Sat, 21 Nov 2026 12:05:00 +0100 +Subject: [PATCH] drm/imagination: apple: Boot the A8 (T7000) GPU firmware + +The A8's GPU, a GX6450 (Apple's G4P), runs the A9's family of GFX +firmware on the core's own META. Boot it through the S8003 chip file +with an A8 variant, following what a standalone bring-up module proved +on the iPod touch 6: + +- Power up in Apple's order: the power manager's clock gating and SoC + voltage manager enables, the GPU power state with automatic power + management, Apple's 33 bridge stores, then Fender's wake. Every access + to the Rogue registers waits for Fender state 1 and the GPU's PLL lock + (PMGR PLL5): an access with the PLL unlocked hangs the whole SoC. The + ID is read that way, as CORE_REVISION and CORE_ID, and the core goes + back to Fender state 0 right after it; the GPU is powered off if the + gate kept the ID from being read. +- Power down the same way on every stop: Apple's stop (Fender core word + 0, state 0, the boot stream cut back), then the GPU's power state 0, + also after a failed probe and at unload. A GPU found powered at probe + is refused rather than woken: nothing of the driver's leaves it on, so + it is in a state this driver did not set up. +- The bring-up module paused 100 ms after SOC_TVM, before Fender's wake, + around the first Rogue register read, before the boot stream and + before the release. Every run that worked had these pauses, so the + driver keeps them. No Fender read after a boot without an alive + event. +- The image: 0x24000 bytes with 4 KiB segments, no core memory, the + handover at the end of the bootloader's segment, the BIF data master + in the segments' OUTA1, thread 1's entry. +- The objects at the A8's sizes, ring lanes of 0x30 bytes, and PLATFORM, + INIT, POLICY and CONTROL as Apple's kext fills them (16-entry clock and + voltage arrays, three performance states, pinned to the start state). +- The boot stream: GPU context 0's catalogue in BIF_CAT_BASE0 and the + A8's SLC setup, no spare page; written without read-back, as Apple's + Fender writers do. The start and stop are Apple's; a Fender that stays + busy leaves the GPU alone, as Apple panics there. +- The doorbell: Apple's, which rings a core the firmware idled once + Fender's snoop is armed. +- The hardware objects Apple maps at 0x8_00000000 (YUV CSC, BRN44455, + BorderColorTable, FenceBuffer), named to the firmware and mapped at the + same addresses in GPU context 0 and in every VM: the MCU writes the + fence through the running job's context. BorderColorTable's contents + are kext data, shipped as apple/gfx-t7000-border.bin; without it the + GPU is not started. +- The heaps as the A8's firmware lays out a VM: general at 0x1_00000000, + PDS at the execution base 0x3_00000000, USC at 0x3_80000000. + +The driver's data page (terminate stream at +0, PDS HALT at +0x1000) is +at least 8 KiB: one PVR_DEVICE_PAGE_SIZE is 4 KiB on 4 KiB kernels and +the HALT overran it. 16 KiB kernels keep one 16 KiB page. + +The GPU starts in power state 1, Apple's start state and the only one +with a proven PLL lock. It takes no jobs yet. + +The A9 and A10 paths are unchanged. + +Assisted-by: LLM +--- + drivers/gpu/drm/imagination/Kconfig | 7 +- + drivers/gpu/drm/imagination/pvr_apple.c | 11 +- + drivers/gpu/drm/imagination/pvr_apple.h | 1 + + drivers/gpu/drm/imagination/pvr_apple_s8003.c | 1191 ++++++++++++++++- + drivers/gpu/drm/imagination/pvr_drv.c | 10 + + 5 files changed, 1211 insertions(+), 9 deletions(-) + +diff --git a/drivers/gpu/drm/imagination/Kconfig b/drivers/gpu/drm/imagination/Kconfig +index 515a31bed5..45e1bbcd99 100644 +--- a/drivers/gpu/drm/imagination/Kconfig ++++ b/drivers/gpu/drm/imagination/Kconfig +@@ -20,16 +20,15 @@ config DRM_POWERVR + If "M" is selected, the module will be called powervr. + + config DRM_POWERVR_APPLE +- bool "Apple A9 (S8003) and A10 (T8010) GPU support" ++ bool "Apple A8 (T7000), A9 (S8003) and A10 (T8010) GPU support" + depends on DRM_POWERVR + depends on APPLE_RTKIT && (ARCH_APPLE || COMPILE_TEST) + depends on DRM_POWERVR=m || APPLE_RTKIT=y + help + Support the Rogue-based GPU of the Apple A10 (T8010), which runs + Apple's GFX firmware on an RTKit coprocessor instead of +- Imagination's firmware, and start the firmware of the Apple A9 +- (S8003), which runs on the GPU's own processor. The A9's GPU does +- not render yet. ++ Imagination's firmware, and of the Apple A9 (S8003) and A8 ++ (T7000), whose GFX firmware runs on the GPU's own processor. + + If in doubt, say "N". + +diff --git a/drivers/gpu/drm/imagination/pvr_apple.c b/drivers/gpu/drm/imagination/pvr_apple.c +index 56e8ac1f93..98de5178a3 100644 +--- a/drivers/gpu/drm/imagination/pvr_apple.c ++++ b/drivers/gpu/drm/imagination/pvr_apple.c +@@ -574,11 +574,18 @@ static void pvr_apple_gpu_pages_fini(struct pvr_apple *apple) + pvr_apple_gpu_page_fini(apple, &apple->data); + } + ++/* ++ * The data buffer holds the terminate-only stream at +0 and the PDS HALT at ++ * +0x1000: two 4 KiB pages, one page of a 16 KiB kernel. ++ */ ++#define PVR_APPLE_GPU_DATA_SIZE max_t(size_t, PVR_DEVICE_PAGE_SIZE, SZ_8K) ++ + static int pvr_apple_gpu_pages_init(struct pvr_apple *apple) + { + int err; + +- err = pvr_apple_gpu_page_init(apple, &apple->data, PVR_APPLE_GPU_DATA); ++ err = pvr_apple_gpu_buf_init_vm(apple, apple->pvr_dev->kernel_vm_ctx, &apple->data, ++ PVR_APPLE_GPU_DATA, PVR_APPLE_GPU_DATA_SIZE); + if (!err) + err = pvr_apple_gpu_page_init(apple, &apple->pds, APPLE_GPU_PDS); + if (!err) +@@ -1772,7 +1779,7 @@ int pvr_apple_vm_init(struct pvr_device *pvr_dev, struct pvr_vm_context *vm_ctx) + int err; + + err = pvr_vm_map_reserved(vm_ctx, apple->data.obj, PVR_APPLE_GPU_DATA, +- PVR_DEVICE_PAGE_SIZE); ++ PVR_APPLE_GPU_DATA_SIZE); + if (!err) + err = pvr_vm_map_reserved(vm_ctx, apple->zero_obj, 0, PVR_DEVICE_PAGE_SIZE); + if (!err && apple->chip->vm_init) +diff --git a/drivers/gpu/drm/imagination/pvr_apple.h b/drivers/gpu/drm/imagination/pvr_apple.h +index 850c4e8d02..752e1a7290 100644 +--- a/drivers/gpu/drm/imagination/pvr_apple.h ++++ b/drivers/gpu/drm/imagination/pvr_apple.h +@@ -27,6 +27,7 @@ struct pvr_vm_context; + + extern const struct pvr_power_sequence_ops pvr_power_sequence_ops_apple; + extern const struct pvr_apple_chip pvr_apple_chip_s8003; ++extern const struct pvr_apple_chip pvr_apple_chip_t7000; + extern const struct pvr_apple_chip pvr_apple_chip_t8010; + + /* +diff --git a/drivers/gpu/drm/imagination/pvr_apple_s8003.c b/drivers/gpu/drm/imagination/pvr_apple_s8003.c +index f29035a445..0b7ba98a47 100644 +--- a/drivers/gpu/drm/imagination/pvr_apple_s8003.c ++++ b/drivers/gpu/drm/imagination/pvr_apple_s8003.c +@@ -53,10 +53,12 @@ + + #include + #include ++#include + #include + #include + #include + #include ++#include + #include + #include + #include +@@ -264,6 +266,7 @@ struct s8003_variant { + void (*idle)(struct pvr_apple *apple); + int (*doorbell)(struct pvr_apple *apple, u32 dm); + int (*first_command)(struct pvr_apple *apple); ++ void (*no_alive)(struct pvr_apple *apple); /* instead of the stop after a boot without alive */ + int (*vm_map)(struct pvr_apple *apple, struct pvr_vm_context *vm_ctx); + void (*vm_put)(struct pvr_apple *apple, struct s8003_vm *vm); + void (*vm_unmap)(struct pvr_apple *apple, struct pvr_vm_context *vm_ctx); +@@ -392,6 +395,14 @@ struct pvr_apple_s8003 { + struct pvr_gem_object *spare; + bool pio_mapped; + ++ /* The A8's (T7000_*) */ ++ void __iomem *pmgr; /* the power manager: GPU power state, PLL5 */ ++ void __iomem *bridges; /* the GPU's two bus bridges */ ++ u8 *border; /* BorderColorTable's contents (firmware file) */ ++ struct s8003_mem hwobj[4]; /* the hardware objects at 0x8_00000000 */ ++ bool t7000_pio_mapped; ++ bool stuck; /* Fender stayed busy: the GPU is left alone */ ++ + struct s8003_idle idle; + }; + +@@ -801,6 +812,9 @@ static void pvr_apple_s8003_fini(struct pvr_apple *apple) + { + struct pvr_apple_s8003 *s = apple->chip_data; + ++ /* A firmware left running keeps its memory (t7000_stuck()) */ ++ if (s->stuck) ++ return; + s->v->unmap(apple); + if (s->park_vm) { + pvr_vm_context_put(s->park_vm); +@@ -1099,6 +1113,7 @@ static void s8003_drain_work(struct work_struct *work) + + static int s8003_wait_alive(struct pvr_apple *apple) + { ++ struct pvr_apple_s8003 *s = apple->chip_data; + bool alive = false; + int err, drained = 0; + +@@ -1106,7 +1121,9 @@ static int s8003_wait_alive(struct pvr_apple *apple) + S8003_ALIVE_TIMEOUT_US, false, apple, &alive); + if (!err) + err = drained; +- if (err) ++ if (err && s->v->no_alive) /* no Rogue or Fender access after a failed boot */ ++ dev_err(apple_dev(apple), "GFX firmware did not come up: %d\n", err); ++ else if (err) + dev_err(apple_dev(apple), "GFX firmware did not come up: %d (Fender state %#x)\n", + err, readl(apple->fender + FENDER_POWER_STATE)); + return err; +@@ -3040,8 +3057,13 @@ static int pvr_apple_s8003_start(struct pvr_apple *apple) + s8003_jobs_reset(s); + + err = v->release(apple, q); +- if (!err) ++ if (!err) { + err = s8003_wait_alive(apple); ++ if (err && v->no_alive) { ++ v->no_alive(apple); ++ return err; ++ } ++ } + if (!err) { + dev_dbg(apple_dev(apple), "GFX firmware is up (power state %u)\n", q); + if (v->first_command) +@@ -3122,6 +3144,1062 @@ static int pvr_apple_s8003_prepare_job(struct pvr_apple *apple, struct pvr_job * + return s->v->prepare_job(apple, job); + } + ++/* ++ * The A8 (T7000): the GX6450 (Apple's G4P) runs the same family of firmware ++ * on its META (AGXFirmwareKextG4P, "METAFW_G4P g4p_b0"), booted the same way. ++ * What differs is ported from the standalone bring-up module that boots it ++ * on the iPod touch 6, stage by stage (an out-of-tree module that is not ++ * part of this series): power-on and ID, firmware boot to the alive event, slot ++ * load, compute, and the first TA + 3D render. Addresses in the comments ++ * are Apple's iOS 12.4 (16G77) iPod7,1 kernelcache (AGXG4P, unslid) and the ++ * firmware's; "P4"/"P5" are the live iOS objects dumped on that device. ++ */ ++ ++/* The core this driver boots: CORE_REVISION major 3, minor 10 (readChipInfo 0x8479d14) */ ++#define T7000_CORE_REVISION 0x00030a00 ++ ++/* The power manager's registers, from its base (ApplePMGR) */ ++#define T7000_PMGR_PLL5 0x5004 /* the GPU's PLL: bit 30 locked */ ++#define T7000_PLL_LOCKED BIT(30) ++#define T7000_PMGR_GPU_TABLE 0x1c000 /* GPU performance records 1-3 at +0x10 each */ ++#define T7000_GPU_TABLE_VALID BIT(21) ++#define T7000_PMGR_VM_CTL 0x23000 /* the voltage manager: on with [11:8] clear */ ++#define T7000_PMGR_PS_GFX 0x20320 /* the GPU's power state */ ++#define T7000_PMGR_SOC_TVM 0x2d000 /* SoC voltage manager enable (0x849d340) */ ++#define T7000_PMGR_MISC_ACG 0x32000 /* clock gating enable (0x849d1bc) */ ++#define T7000_PS_TARGET GENMASK(3, 0) ++#define T7000_PS_ACTUAL GENMASK(7, 4) ++#define T7000_PS_HIST GENMASK(9, 8) ++#define T7000_PS_DEV_DISABLE BIT(10) ++#define T7000_PS_MIN GENMASK(19, 16) ++#define T7000_PS_AUTO_PM BIT(28) ++#define T7000_PS_BIT30 BIT(30) ++#define T7000_PS_ON 0xf ++#define T7000_PS_POLL_US 200000 ++ ++#define T7000_FENDER_POLL_US 100000 ++/* ++ * The bring-up module paused this long before each group of new accesses ++ * (its log crumbs): before the GPU's power state goes on (after SOC_TVM), ++ * before Fender's wake, before the first Rogue register read, before Fender ++ * state 0 after it, before the boot stream and before the release. Every ++ * run had these pauses, so the driver keeps them. ++ */ ++#define T7000_SETTLE_MS 100 ++#define T7000_FENDER_SNOOP 0x0014 /* FENDER_WAKE: bit 1 snoop armed */ ++#define T7000_FENDER_CTRL_SNOOP BIT(29) ++ ++/* Rogue registers of the boot stream that powervr has no name for */ ++#define T7000_CR_SLC_3808 0x3808 ++ ++/* ++ * The unpacked image as 0x84727c8 lays it out with iOS's 4 KiB pages: the ++ * bootloader's segment (2) first, then code, data and one more data range; ++ * no core memory. The handover is at the end of segment 2. ++ */ ++#define T7000_IMAGE_SIZE 0x24000 ++#define T7000_IMAGE_HANDOVER 0xff8 ++#define T7000_SEGMMU_ALLTHRS_WRITEABLE 0xf02 /* 0x8471b0c: BASE | 0xf02 */ ++#define T7000_SEGMMU_OUTA1_BIFDM (7u << 8) /* (PLATFORM +0x4bc = 7) << 8 */ ++ ++/* The firmware's register windows (PLATFORM +0x190, P4): Fender, AIC + 0x1000, AIC + 0x4000 */ ++#define T7000_PIO_FENDER 0x0000 ++#define T7000_PIO_FENDER_SIZE 0x5000 ++#define T7000_PIO_AIC_TIME 0x8000 ++#define T7000_PIO_AIC_SET 0xc000 ++#define T7000_PIO_AIC_SIZE 0x1000 ++ ++/* ++ * Root memory: the objects, then the channels and the PM sets' control ++ * blocks, all in META's uncached window as Apple's (P5: channel 0x43318200, ++ * PM control 0x4331c000). Shared memory: the objects, then the PM sets' ++ * lists in the cached window as Apple's (P5b: 0xc0284000). ++ */ ++#define T7000_ROOT_OBJECTS_END 0x18000 ++#define T7000_CU_CHANNEL 0x18000 /* 0x1000 */ ++#define T7000_TA_CHANNEL 0x20000 /* 0x3000 each */ ++#define T7000_3D_CHANNEL 0x24000 ++#define T7000_PM_CONTROL 0x28000 /* 0x2000 per set */ ++#define T7000_SHARED_OBJECTS_END 0x60000 ++#define T7000_PM_LIST 0x60000 /* 0x2000 per set */ ++#define T7000_PM_SETS 7 /* one per user slot */ ++#define T7000_PM_SET_SIZE 0x2000 ++#define T7000_ROOT_SIZE (T7000_PM_CONTROL + T7000_PM_SETS * T7000_PM_SET_SIZE) ++#define T7000_SHARED_SIZE (T7000_PM_LIST + T7000_PM_SETS * T7000_PM_SET_SIZE) ++ ++/* Slots: 8, the host's from 1, one per VM (vt+0x888/0x890/0x898) */ ++#define T7000_SLOTS 8 ++#define T7000_SLOT_FIRST 1 ++ ++/* ++ * The hardware-specific objects Apple's kext maps at firmware init ++ * (0x847950c) at 0x8_00000000 and names to the firmware (PLATFORM +0x30, ++ * +0x188, +0x4a8; object 0x4c +0/+8): the YUV CSC buffer, BRN44455Object ++ * (0x8475964), BorderColorTable (0x847586c: 0x800 bytes of the kext's data) ++ * and FenceBuffer (0x8475a40). Each GART reserves 0x8_00000000 + 4 MiB ++ * (0x8463138), so every VM sees them at these addresses: the firmware loads ++ * the FenceBuffer into MCU_FENCE and the MCU writes it through the running ++ * job's context. Plain flags: with PM/META protect the firmware never ++ * posts its alive event. ++ */ ++#define T7000_HWOBJ_VA 0x800000000ull ++#define T7000_HWOBJ_SIZE 0x4000 ++#define T7000_HWOBJ_WINDOW SZ_4M ++#define T7000_BORDER_SIZE 0x800 ++#define T7000_BORDER_FW "apple/gfx-t7000-border.bin" ++enum { T7000_HW_YUV, T7000_HW_BRN44455, T7000_HW_BORDER, T7000_HW_FENCE, T7000_HW_OBJS }; ++static const u32 t7000_hwobj_offset[T7000_HW_OBJS] = { 0x0, 0x4000, 0x8000, 0x10000 }; ++ ++/* GPU memory of the firmware's: past the device cache, PM/firmware protected */ ++#define T7000_FW_FLAGS (DRM_PVR_BO_BYPASS_DEVICE_CACHE | DRM_PVR_BO_PM_FW_PROTECT) ++/* What a VM's jobs reach: past the device cache, nothing else */ ++#define T7000_VM_FLAGS DRM_PVR_BO_BYPASS_DEVICE_CACHE ++ ++/* ++ * Objects, as the A8's kext sizes them (allocateSharedRingGroups 0x84653e8, ++ * packed-group table 0x712fa38..fab0) and packs them, checked against ++ * every live P4 object. A ring lane's indices take 0x30 bytes (A9 0x20). ++ */ ++#define T7000_RING_CTL_SIZE 0x30 ++ ++static const struct s8003_object_size s8003_objects_a8[] = { ++ { S8003_INIT, false, 0x1118 }, ++ { S8003_CHANNEL, false, 0x990 }, ++ { S8003_CHANNEL + 1, false, 0x8e0 }, ++ { S8003_CHANNEL + 2, false, 0x8c0 }, ++ { S8003_POLICY, false, 0x1058 }, ++ { 0x4b, false, 0x28 }, ++ { 0x4c, false, 0x10 }, ++ { S8003_PLATFORM, false, 0x4dc }, ++ { 0x4e, false, 0x84 }, ++ { S8003_TAG_SEEN, false, 0x20 }, ++ { S8003_CONTROL, true, 0x1528 }, ++ { S8003_EVENT_CTL, true, T7000_RING_CTL_SIZE }, ++ { S8003_LOG_CTL, true, 4 * T7000_RING_CTL_SIZE }, ++ { S8003_STATS_CTL, true, T7000_RING_CTL_SIZE }, ++ { S8003_TRACE_CTL, true, T7000_RING_CTL_SIZE }, ++ { S8003_EVENTS, true, 0x2c00 }, ++ { S8003_LOGS, true, 0xb000 }, ++ { 0x4f, true, 0x36000 }, ++ { S8003_STATS, true, 0x2c00 }, ++ { S8003_TRACE, true, 0x2400 }, ++ { S8003_WORK_CTL, true, T7000_RING_CTL_SIZE }, ++ { S8003_WORK_CTL + 4, true, T7000_RING_CTL_SIZE }, ++ { S8003_WORK_CTL + 8, true, T7000_RING_CTL_SIZE }, ++ { S8003_DC_CTL, true, T7000_RING_CTL_SIZE }, ++ { S8003_WORK_RING, true, 0x1400 }, ++ { S8003_WORK_RING + 4, true, 0x1400 }, ++ { S8003_WORK_RING + 8, true, 0x1400 }, ++ { S8003_DC_RING, true, 0x1400 }, ++ { S8003_TAG_HOST, true, 0x20 }, ++ { S8003_MMU_ROOT, true, 0x40 }, ++}; ++ ++/* The image's segments: META address (bit 31 clear), size, offset in the image */ ++static const struct { ++ u32 base, size, offset; ++} t7000_segments[4] = { ++ { 0x10000000, 0x1e000, 0x01000 }, ++ { 0x20000000, 0x03000, 0x1f000 }, ++ { 0x40000000, 0x01000, 0x00000 }, /* the bootloader's: not configured (0x8471b0c) */ ++ { 0x60000000, 0x02000, 0x22000 }, ++}; ++ ++/* Where a section of the LDR image goes, as 0x8472a00 reads it */ ++static int t7000_ldr_mem(void *data, u32 addr, u32 size, bool zero, void **write_addr) ++{ ++ struct pvr_apple_s8003 *s = data; ++ u32 seg = (addr >> 29) & 3, a = addr & 0x7fffffff; ++ ++ /* core memory (0x80000080..): the A8's image has none */ ++ if ((u32)(addr + 0x7fffff80) >> 7 < 0x17f || !((addr >> 27) & 0xf)) ++ return -EINVAL; ++ if (a < t7000_segments[seg].base || size > t7000_segments[seg].size || ++ a - t7000_segments[seg].base > t7000_segments[seg].size - size) ++ return -EINVAL; ++ *write_addr = s->image + t7000_segments[seg].offset + a - t7000_segments[seg].base; ++ return 0; ++} ++ ++/* Thread 1's stack pointer and entry (0x8472a00 case 3), from entries {thread, sp, pc, catch} */ ++static int t7000_ldr_start_threads(void *data, const u32 *words, u32 count) ++{ ++ struct pvr_apple_s8003 *s = data; ++ u32 i; ++ ++ for (i = 0; i + 4 <= count; i += 4) { ++ if (words[i] != 1) ++ continue; ++ if (s->thread_pc) ++ return -EINVAL; ++ s->thread_sp = words[i + 1]; ++ s->thread_pc = words[i + 2]; ++ } ++ return 0; ++} ++ ++static const struct pvr_meta_ldr_target t7000_ldr_target = { ++ .mem = t7000_ldr_mem, ++ .config = s8003_ldr_config, ++ .start_threads = t7000_ldr_start_threads, ++}; ++ ++/* ++ * The bootloader's configuration (0x84724c4 + 0x8471b0c): segments 0, 1 and ++ * 3 with the BIF data master in OUTA1, the image's 27 writes, a zero u64 and ++ * the META address of the handover, which holds INIT's and CONTROL's. ++ */ ++static void t7000_image_configure(struct pvr_apple_s8003 *s) ++{ ++ u8 *conf = s->image + S8003_IMAGE_CONF; ++ unsigned int i; ++ ++ for (i = 0; i < ARRAY_SIZE(t7000_segments); i++) { ++ u64 out = S8003_IMAGE_VA + t7000_segments[i].offset; ++ u32 limit = max(t7000_segments[i].size - 1, 0xfffu); ++ ++ if (i == 2) ++ continue; ++ put32(conf, META_CR_MMCU_SEGMENT_N_BASE(i)); ++ put32(conf + 4, t7000_segments[i].base | T7000_SEGMMU_ALLTHRS_WRITEABLE); ++ put32(conf + 8, META_CR_MMCU_SEGMENT_N_LIMIT(i)); ++ put32(conf + 12, limit); ++ put32(conf + 16, META_CR_MMCU_SEGMENT_N_OUTA0(i)); ++ put32(conf + 20, lower_32_bits(out)); ++ put32(conf + 24, META_CR_MMCU_SEGMENT_N_OUTA1(i)); ++ put32(conf + 28, T7000_SEGMMU_OUTA1_BIFDM | upper_32_bits(out)); ++ conf += 32; ++ } ++ for (i = 0; i < s->conf_count; i++, conf += 8) { ++ put32(conf, s->conf[i][0]); ++ put32(conf + 4, s->conf[i][1]); ++ } ++ put64(conf, 0); ++ put32(conf + 8, s8003_meta_addr(S8003_IMAGE_VA + T7000_IMAGE_HANDOVER)); ++ put32(s->image + T7000_IMAGE_HANDOVER, s8003_object_addr(&s->layout, S8003_INIT)); ++ put32(s->image + T7000_IMAGE_HANDOVER + 4, s8003_object_addr(&s->layout, S8003_CONTROL)); ++} ++ ++/* The registers' windows and the hardware objects in the kernel VM */ ++static void t7000_unmap(struct pvr_apple *apple) ++{ ++ struct pvr_vm_context *vm_ctx = apple->pvr_dev->kernel_vm_ctx; ++ struct pvr_apple_s8003 *s = apple->chip_data; ++ unsigned int i; ++ ++ if (s->t7000_pio_mapped) { ++ pvr_vm_unmap_io(vm_ctx, S8003_PIO_VA + T7000_PIO_FENDER, T7000_PIO_FENDER_SIZE); ++ pvr_vm_unmap_io(vm_ctx, S8003_PIO_VA + T7000_PIO_AIC_TIME, T7000_PIO_AIC_SIZE); ++ pvr_vm_unmap_io(vm_ctx, S8003_PIO_VA + T7000_PIO_AIC_SET, T7000_PIO_AIC_SIZE); ++ s->t7000_pio_mapped = false; ++ } ++ for (i = 0; i < T7000_HW_OBJS; i++) ++ s8003_mem_fini(apple, &s->hwobj[i]); ++} ++ ++/* ++ * The firmware's memory and registers in the kernel VM, as the bring-up ++ * module maps them: the image, root and shared memory and a zeroed page at ++ * address 0 for the firmware only and past the device cache (the CPU writes ++ * them uncached), the register windows as pvr_vm_map_io() maps registers, ++ * and the hardware objects. ++ */ ++static int t7000_map(struct pvr_apple *apple) ++{ ++ struct pvr_vm_context *vm_ctx = apple->pvr_dev->kernel_vm_ctx; ++ struct pvr_apple_s8003 *s = apple->chip_data; ++ unsigned int i; ++ int err; ++ ++ err = s8003_mem_init(apple, &s->fw, S8003_IMAGE_VA, T7000_IMAGE_SIZE, T7000_FW_FLAGS); ++ if (!err) ++ err = s8003_mem_init(apple, &s->root, S8003_ROOT_VA, T7000_ROOT_SIZE, T7000_FW_FLAGS); ++ if (!err) ++ err = s8003_mem_init(apple, &s->shared, S8003_SHARED_VA, T7000_SHARED_SIZE, ++ T7000_FW_FLAGS); ++ if (!err) ++ err = s8003_mem_init(apple, &s->zero, 0, PVR_DEVICE_PAGE_SIZE, T7000_FW_FLAGS); ++ for (i = 0; !err && i < T7000_HW_OBJS; i++) ++ err = s8003_mem_init(apple, &s->hwobj[i], T7000_HWOBJ_VA + t7000_hwobj_offset[i], ++ T7000_HWOBJ_SIZE, T7000_VM_FLAGS); ++ if (!err) { ++ err = pvr_vm_map_io(vm_ctx, s->fender_phys, S8003_PIO_VA + T7000_PIO_FENDER, ++ T7000_PIO_FENDER_SIZE); ++ if (!err) { ++ err = pvr_vm_map_io(vm_ctx, s->aic_phys + 0x1000, ++ S8003_PIO_VA + T7000_PIO_AIC_TIME, T7000_PIO_AIC_SIZE); ++ if (err) ++ pvr_vm_unmap_io(vm_ctx, S8003_PIO_VA + T7000_PIO_FENDER, ++ T7000_PIO_FENDER_SIZE); ++ } ++ if (!err) { ++ err = pvr_vm_map_io(vm_ctx, s->aic_phys + AIC_SW_SET, ++ S8003_PIO_VA + T7000_PIO_AIC_SET, T7000_PIO_AIC_SIZE); ++ if (err) { ++ pvr_vm_unmap_io(vm_ctx, S8003_PIO_VA + T7000_PIO_FENDER, ++ T7000_PIO_FENDER_SIZE); ++ pvr_vm_unmap_io(vm_ctx, S8003_PIO_VA + T7000_PIO_AIC_TIME, ++ T7000_PIO_AIC_SIZE); ++ } ++ } ++ if (!err) ++ s->t7000_pio_mapped = true; ++ } ++ if (err) ++ pvr_apple_s8003_fini(apple); ++ return err; ++} ++ ++/* PLATFORM +0x38..+0xaf: the first of the two 0xa8-byte blocks 0x8466578 copies from acc+0x1798 (P4) */ ++static const u32 t7000_platform_38[] = { ++ 0x00000000, 0x00002000, 0x00002000, 0x00000000, 0x20000000, 0x00000000, ++ 0x00002543, 0xe4233312, 0xf3772543, 0x10f2e5fd, 0x408d2543, 0xdd650000, ++ 0x00002543, 0xe0fd395e, 0xf92d2543, 0x099ceef2, 0x43962543, 0xdbe10000, ++ 0x00002000, 0xe9912cdd, 0xf4fd2000, 0x10eee926, 0x38b42000, 0xe3a60000, ++ 0x00002000, 0xe73b3189, 0xfa022000, 0x0a7df105, 0x3b5e2000, 0xe2510000, ++}; ++ ++/* POLICY +0x40: the 0x160 bytes 0x8467770 copies from fwctl +0x3f0 (P4: the nonzero words) */ ++static const u32 t7000_policy_40[][2] = { ++ { 0x40, 0x4294fc36 }, { 0x44, 0x40041a37 }, { 0x48, 0x4219ba44 }, { 0xe0, 0x42849d8b }, ++ { 0x164, 0x3f800000 }, { 0x168, 0x3faab021 }, { 0x16c, 0x3fc820c5 }, { 0x170, 0x3fe9d495 }, ++}; ++ ++/* ++ * The governor's words 0x8467770 computes from fwctl +0x598..+0x63c (P4), with ++ * the result it stores; where iOS's firmware changed a word later (+0x678, ++ * +0x67c, +0x774, +0x78c), Apple's initial value. ++ */ ++static const u32 t7000_policy_gov[][2] = { ++ { 0x634, 0x271 }, { 0x640, 0x3f7f2e9f }, { 0x648, 0x3b516154 }, { 0x650, 0x3ca59586 }, ++ { 0x65c, 0x452ba000 }, { 0x660, 0x40a90fdb }, { 0x668, 0xbddfbe5e }, { 0x66c, 0xfffffefc }, ++ { 0x674, 0x12c }, { 0x67c, 0xaba }, { 0x694, 0x12c }, { 0x72c, 0x5f }, { 0x730, 0x6 }, ++ { 0x734, 0x1 }, { 0x73c, 0x3f4ccccd }, { 0x744, 0x3e4ccccd }, { 0x740, 0x3f7ae148 }, ++ { 0x748, 0x3ca3d70a }, { 0x74c, 0x40fca970 }, { 0x750, 0x3e4a2121 }, { 0x758, 0x42be0000 }, ++ { 0x75c, 0x416b53d1 }, { 0x760, 0x40db53d0 }, { 0x764, 0x4006bca2 }, { 0x76c, 0xc8 }, ++ { 0x770, 0x64 }, { 0x778, 0x5f }, { 0x790, 0x64 }, { 0x7a8, 0x42c80000 }, { 0x7ac, 300 }, ++ { 0x7b4, 0x3f4ccccd }, { 0x7b8, 0x3e4ccccd }, { 0x904, 300 }, ++}; ++ ++/* MHz and mV per row (kext arrays acc+0x19d4, P4): states 1-3 are 365, 450 and 500 MHz */ ++static const u32 t7000_mhz[7] = { 0, 365, 450, 500, 182, 225, 250 }; ++static const u32 t7000_mv[7] = { 600, 871, 896, 912, 871, 896, 912 }; ++ ++/* ++ * The root and shared memory as the A8's firmware finds it when it starts ++ * (0x8466578, 0x8467770; values from P4): where its objects are, the heaps' ++ * bases, the registers it may use, the interrupt it raises, its thread, the ++ * performance states and the hardware objects. @q is the power state the ++ * core starts in, to which the power interface's two clock targets are ++ * pinned (Apple: state 1, 0x846eb10). CONTROL +0xcd0..+0xcd8, the ++ * performance controller's inputs, stay zero as in Apple's live CONTROL. ++ */ ++static void t7000_init_build(struct pvr_apple *apple, u32 q) ++{ ++ static const u8 rings[] = { ++ S8003_WORK_CTL, S8003_WORK_RING, S8003_WORK_CTL + 4, S8003_WORK_RING + 4, ++ S8003_WORK_CTL + 8, S8003_WORK_RING + 8, S8003_DC_CTL, S8003_DC_RING, ++ S8003_EVENT_CTL, S8003_EVENTS, S8003_LOG_CTL, S8003_LOGS, ++ S8003_STATS_CTL, S8003_STATS, S8003_TRACE_CTL, S8003_TRACE, ++ }; ++ static const u8 objects[] = { ++ S8003_CHANNEL, S8003_CHANNEL + 1, S8003_CHANNEL + 2, S8003_POLICY, 0x4b, ++ }; ++ static const u8 tail[] = { 0x4e, 0x4c, S8003_PLATFORM, S8003_TAG_SEEN, 0x4f }; ++ struct pvr_apple_s8003 *s = apple->chip_data; ++ const struct s8003_layout *layout = &s->layout; ++ const struct { ++ u64 phys; ++ u32 addr; ++ u32 size; ++ } pio[] = { ++ { s->fender_phys, s8003_meta_addr(S8003_PIO_VA + T7000_PIO_FENDER), ++ T7000_PIO_FENDER_SIZE }, ++ { s->aic_phys + 0x1000, s8003_meta_addr(S8003_PIO_VA + T7000_PIO_AIC_TIME), ++ T7000_PIO_AIC_SIZE }, ++ { s->aic_phys + AIC_SW_SET, s8003_meta_addr(S8003_PIO_VA + T7000_PIO_AIC_SET), ++ T7000_PIO_AIC_SIZE }, ++ { s->rgx_phys, 0, 0x20000 }, ++ }; ++ u8 *init, *platform, *policy, *control, *o4c; ++ unsigned int i; ++ ++ memset(s->root.map, 0, T7000_ROOT_SIZE); ++ memset(s->shared.map, 0, T7000_SHARED_SIZE); ++ init = s8003_object(s, S8003_INIT); ++ platform = s8003_object(s, S8003_PLATFORM); ++ policy = s8003_object(s, S8003_POLICY); ++ control = s8003_object(s, S8003_CONTROL); ++ o4c = s8003_object(s, 0x4c); ++ ++ for (i = 0; i < ARRAY_SIZE(rings); i++) ++ put32(init + 4 * i, s8003_object_addr(layout, rings[i])); ++ for (i = 0; i < ARRAY_SIZE(objects); i++) ++ put32(init + 0x88 + 4 * i, s8003_object_addr(layout, objects[i])); ++ for (i = 0; i < ARRAY_SIZE(tail); i++) ++ put32(init + 0x470 + 4 * i, s8003_object_addr(layout, tail[i])); ++ /* No core memory: the image's end, size 0 (0x84724c4; P4 0x40024000, 0xe1c0024000, 0) */ ++ put32(init + 0x4a4, (lower_32_bits(S8003_IMAGE_VA + T7000_IMAGE_SIZE) + 0x40000000) | ++ 0x40000000); ++ put64(init + 0x4a8, S8003_IMAGE_VA + T7000_IMAGE_SIZE); ++ /* Channel headers {0, self, 0, self + 8}; no channel 1 +0x778 on the A8 */ ++ for (i = 0; i < 3; i++) { ++ u8 *channel = s8003_object(s, S8003_CHANNEL + i); ++ u32 addr = s8003_object_addr(layout, S8003_CHANNEL + i); ++ ++ put32(channel + 4, addr); ++ put32(channel + 12, addr + 8); ++ } ++ ++ /* The heaps' bases (DAT 0x712f910): +0x20/+0x28 is the PDS and USC execution base */ ++ put64(platform + 0x00, 0x500000000ull); ++ put64(platform + 0x08, 0x600000000ull); ++ put64(platform + 0x10, 0x100000000ull); ++ put64(platform + 0x18, 0x100000000ull); ++ put64(platform + 0x20, 0x300000000ull); ++ put64(platform + 0x28, 0x300000000ull); ++ for (i = 0; i < ARRAY_SIZE(t7000_platform_38); i++) ++ put32(platform + 0x38 + 4 * i, t7000_platform_38[i]); ++ for (i = 0; i < ARRAY_SIZE(pio); i++) { ++ u8 *entry = platform + 0x190 + 16 * i; ++ ++ put64(entry, pio[i].phys); ++ put32(entry + 8, pio[i].addr); ++ put32(entry + 12, pio[i].size); ++ } ++ put32(platform + 0x250, 1); /* Fender boot */ ++ put32(platform + 0x254, 1); /* gpu_lockup_detection */ ++ put32(platform + 0x258, 1); /* PIO supported */ ++ put32(platform + 0x274, 1); /* agf_timeslice */ ++ put32(platform + 0x278, 1); ++ put32(platform + 0x27c, 1); /* agf_timer */ ++ put32(platform + 0x280, 1000 * 0x18); /* agf_timer_period, 24 MHz ticks */ ++ put32(platform + 0x284, 0x10); ++ put32(platform + 0x290, 1); /* agf_dwb */ ++ put32(platform + 0x294, 1); /* agf_itr_cache */ ++ put32(platform + 0x29c, 1); /* agf_deferred_opaque */ ++ put32(platform + 0x2b4, 0x1f); /* agf_max_tiles_per_pipe */ ++ put32(platform + 0x2dc, 1); /* agf_frg_vce_inval */ ++ put32(platform + 0x300, 2); /* chip info {2, 4, 0, 4} */ ++ put32(platform + 0x304, 4); ++ put32(platform + 0x308, 4); ++ put32(platform + 0x30c, 3); /* the highest performance state */ ++ put32(platform + 0x310, 16); ++ for (i = 0; i < ARRAY_SIZE(t7000_mhz); i++) { ++ put32(platform + 0x314 + 4 * i, t7000_mhz[i]); ++ put32(platform + 0x354 + 4 * i, t7000_mv[i]); ++ } ++ /* The interrupt to the host: its word and bit among the AIC's software sets (P4 0x10, 0x400) */ ++ put32(platform + 0x494, 4 * (s->irq / 32)); ++ put32(platform + 0x498, BIT(s->irq % 32)); ++ put32(platform + 0x49c, AIC_TIME & 0xfff); /* in the time window */ ++ put32(platform + 0x4a0, s->thread_pc); ++ put32(platform + 0x4a4, s->thread_sp); ++ put64(platform + 0x4b0, S8003_IMAGE_VA); ++ put64(platform + 0x4b8, 0x700000000ull); /* DAT 0x712fab8 */ ++ put32(platform + 0x4c8, 0xffff); /* agf_gtp_ks */ ++ put32(platform + 0x4cc, 7); /* agf_frg_ks */ ++ put32(platform + 0x4d0, 7); /* agf_compute_ks */ ++ ++ put32(policy + 0x04, 0x10); ++ put64(policy + 0x10, 0x3f800000); ++ put64(policy + 0x28, 0x100000001ull); ++ put32(policy + 0x3c, 100); ++ for (i = 0; i < ARRAY_SIZE(t7000_policy_40); i++) ++ put32(policy + t7000_policy_40[i][0], t7000_policy_40[i][1]); ++ for (i = 0; i < ARRAY_SIZE(t7000_policy_gov); i++) ++ put32(policy + t7000_policy_gov[i][0], t7000_policy_gov[i][1]); ++ put64(policy + 0x1008, arch_timer_read_counter()); /* the governor's last sample */ ++ ++ put32(control + 0x28, 1); ++ put32(control + 0x34, 0x78); /* gpu_temperature_limit */ ++ put32(control + 0x4a, 1); ++ put32(control + 0xc66, 1867); /* power target */ ++ put32(control + 0xc6a, 100 * q); /* the power interface's clock targets */ ++ put32(control + 0xc6e, 100 * q); ++ put32(control + 0xd0e, s8003_object_addr(layout, S8003_TAG_HOST)); ++ put32(control + 0xd12, s8003_object_addr(layout, S8003_MMU_ROOT)); ++ put32(control + 0xd16, 1); ++ /* GPU context 0 runs on the kernel VM, whose catalogue the boot stream gives the BIF */ ++ put64(s8003_object(s, S8003_MMU_ROOT), ++ pvr_vm_get_page_table_root_addr(apple->pvr_dev->kernel_vm_ctx)); ++ ++ /* The hardware objects, fresh, and their names */ ++ for (i = 0; i < T7000_HW_OBJS; i++) ++ memset(s->hwobj[i].map, 0, T7000_HWOBJ_SIZE); ++ memcpy(s->hwobj[T7000_HW_BORDER].map, s->border, T7000_BORDER_SIZE); ++ put64(platform + 0x30, T7000_HWOBJ_VA + t7000_hwobj_offset[T7000_HW_YUV]); ++ put64(platform + 0x188, T7000_HWOBJ_VA + t7000_hwobj_offset[T7000_HW_BORDER]); ++ put64(platform + 0x4a8, T7000_HWOBJ_VA + t7000_hwobj_offset[T7000_HW_FENCE]); ++ put64(o4c, T7000_HWOBJ_VA + t7000_hwobj_offset[T7000_HW_BRN44455]); ++ put64(o4c + 8, T7000_HWOBJ_VA + t7000_hwobj_offset[T7000_HW_FENCE]); ++} ++ ++/* ++ * A channel page. The compute channel has the A9's layout (s8003_channel_init()), ++ * which the A8's firmware takes. The TA and 3D channels have Apple's header ++ * and ring control (P5b chfw_ta/chfw_3d, chring: five watermarks 0x10 apart, ++ * 0x144 entries, the ring right behind at +0x60); their dependency objects, ++ * stamps and records are where the A9's layout has them. ++ */ ++#define T7000_RCH_CMD 0x300 /* the 3D job's barrier command */ ++#define T7000_RCH_W 0x400 ++#define T7000_RCH_TA_USEQ 0xa00 ++#define T7000_RCH_3D_USEQ 0xd00 ++#define T7000_RCH_TIMES 0x2800 /* the 3D record's times, 16 bytes */ ++#define T7000_RCH_SIZE 0x3000 ++#define T7000_RCH_CTL 0x2000 /* Apple's ring control: 0x60 + 0x144 entries */ ++#define T7000_RCH_RING (T7000_RCH_CTL + 0x60) ++#define T7000_RCH_ENTRIES 0x144 ++#define T7000_CU_SIZE 0x1000 ++ ++static void t7000_channel_init(u8 *p, u32 size, u32 addr, u32 tq, u32 dep) ++{ ++ memset(p, 0, size); ++ s8003_channel_init(p, addr, tq, dep); ++} ++ ++/* Apple's TA/3D channel header: @control_knob is CONTROL + 0xc, @object the engine's channel object */ ++static void t7000_render_channel_init(u8 *p, u32 addr, u32 control_knob, u32 object) ++{ ++ t7000_channel_init(p, T7000_RCH_SIZE, addr, S8003_CH_TQ, S8003_CH_DEP); ++ memset(p, 0, 0x80); ++ put32(p + 0x00, addr + T7000_RCH_CTL); ++ put32(p + 0x04, addr + T7000_RCH_RING); ++ put32(p + 0x08, control_knob); ++ put32(p + 0x0c, 2); ++ put32(p + 0x10, 0xff000000); ++ put32(p + 0x18, 0xffffffff); ++ put32(p + 0x2c, object); ++ put32(p + 0x34, object + 8); ++ put32(p + 0x4c, 0x21); ++ put32(p + 0x54, 0x640); ++ memset(p + S8003_CH_CTL, 0, 0x60); ++ put32(p + T7000_RCH_CTL + S8003_CH_CTL_ENTRIES, T7000_RCH_ENTRIES); ++} ++ ++static void t7000_channels_init(struct pvr_apple_s8003 *s) ++{ ++ u32 knob = s8003_object_addr(&s->layout, S8003_CONTROL) + 0xc; ++ ++ t7000_channel_init(s8003_channel(s, APPLE_ENGINE_COMPUTE), T7000_CU_SIZE, ++ s8003_channel_addr(s, APPLE_ENGINE_COMPUTE), S8003_CU_TQ, S8003_CU_DEP); ++ t7000_render_channel_init(s8003_channel(s, APPLE_ENGINE_GEOM), ++ s8003_channel_addr(s, APPLE_ENGINE_GEOM), knob, ++ s8003_object_addr(&s->layout, S8003_CHANNEL)); ++ t7000_render_channel_init(s8003_channel(s, APPLE_ENGINE_FRAG), ++ s8003_channel_addr(s, APPLE_ENGINE_FRAG), knob, ++ s8003_object_addr(&s->layout, S8003_CHANNEL + 1)); ++} ++ ++static u32 pmgr_read(struct pvr_apple_s8003 *s, u32 reg) ++{ ++ return readl(s->pmgr + reg); ++} ++ ++/* ++ * Every access to the Rogue registers waits for this: Fender in state 1, no ++ * request in flight, the GPU's PLL locked, all read right before. An access ++ * with the PLL unlocked hung the whole SoC in bring-up. ++ */ ++static bool t7000_r0_gate(struct pvr_apple *apple) ++{ ++ struct pvr_apple_s8003 *s = apple->chip_data; ++ u32 state = readl(apple->fender + FENDER_POWER_STATE); ++ u32 pll = pmgr_read(s, T7000_PMGR_PLL5); ++ ++ if ((state & FENDER_POWER_STATE_MASK) == 1 && (pll & T7000_PLL_LOCKED)) ++ return true; ++ dev_err_ratelimited(apple_dev(apple), ++ "GPU registers not accessed: Fender state %#x, PLL5 %#x\n", state, pll); ++ return false; ++} ++ ++/* ++ * Where Apple panics (a GPC state transition stuck, no alive event): leave ++ * the GPU as it is, as the bring-up module does. No Fender, PMGR or Rogue ++ * register is touched again, and the firmware's memory and page tables are ++ * kept until a reboot (pvr_apple_s8003_fini()): the META may still run in ++ * them. ++ */ ++static void t7000_stuck(struct pvr_apple *apple, const char *why) ++{ ++ struct pvr_apple_s8003 *s = apple->chip_data; ++ ++ if (s->stuck) ++ return; ++ s->stuck = true; ++ if (apple->pvr_dev->kernel_vm_ctx) ++ pvr_vm_context_get(apple->pvr_dev->kernel_vm_ctx); ++ dev_err(apple_dev(apple), "%s: the GPU is left as it is, with its memory, until a reboot\n", ++ why); ++} ++ ++/* Ask Fender for power state @state and wait until no request is in flight */ ++static int t7000_fender_state(struct pvr_apple *apple, u32 state) ++{ ++ u32 val; ++ int err; ++ ++ writel(FENDER_POWER_REQUEST | state, apple->fender + FENDER_POWER_STATE); ++ err = readl_poll_timeout(apple->fender + FENDER_POWER_STATE, val, ++ !(val & FENDER_POWER_REQUEST), 5, T7000_FENDER_POLL_US); ++ if (err) { ++ dev_err(apple_dev(apple), "Fender stayed busy going to state %u (%#x)\n", state, val); ++ return err; ++ } ++ apple->perf_state = state; ++ return 0; ++} ++ ++/* Apple's GPU power state writer (0x8497520): on (auto power management) or off */ ++static int t7000_ps_set(struct pvr_apple *apple, u32 target) ++{ ++ struct pvr_apple_s8003 *s = apple->chip_data; ++ void __iomem *ps = s->pmgr + T7000_PMGR_PS_GFX; ++ u32 r = readl(ps), val; ++ int err; ++ ++ if (FIELD_GET(T7000_PS_TARGET, r) == target && !(r & (T7000_PS_BIT30 | T7000_PS_DEV_DISABLE)) && ++ !!(r & T7000_PS_AUTO_PM) == (target == T7000_PS_ON)) ++ return 0; ++ if (r & T7000_PS_AUTO_PM) { ++ r &= 0xeffffcff; ++ writel(r, ps); ++ } else { ++ r &= 0xfffffcff; ++ } ++ r = (r & 0xfffffcf0) | target; ++ if (target < T7000_PS_ON) ++ r |= T7000_PS_HIST; ++ writel(r, ps); ++ err = readl_poll_timeout(ps, val, FIELD_GET(T7000_PS_ACTUAL, val) == target, 5, ++ T7000_PS_POLL_US); ++ if (err) { ++ dev_err(apple_dev(apple), "GPU power state %#x not reached: %#x\n", target, val); ++ return err; ++ } ++ if (target != T7000_PS_ON) ++ return 0; ++ r = (r & ~T7000_PS_HIST) | T7000_PS_AUTO_PM; ++ writel(r, ps); ++ if (r & T7000_PS_BIT30) { ++ r &= ~T7000_PS_BIT30; ++ writel(r, ps); ++ } ++ if (r & T7000_PS_DEV_DISABLE) ++ writel(r & ~T7000_PS_DEV_DISABLE, ps); ++ return 0; ++} ++ ++static bool t7000_ps_on(struct pvr_apple_s8003 *s) ++{ ++ return FIELD_GET(T7000_PS_ACTUAL, pmgr_read(s, T7000_PMGR_PS_GFX)) == T7000_PS_ON; ++} ++ ++/* ++ * Apple's bridge settings for the GPU (0x84a0098, from the ADT): one store to ++ * bridge 10, 32 to bridge 11, after the GPU powers on. ++ */ ++static const u32 t7000_bridge11[32] = { ++ 0x007c7c01, 0x007c7c01, 0x007c7c01, 0x007c7901, 0x007c7901, 0x007c7c01, 0x007c7c01, ++ 0x007c7c01, 0x007c7c01, 0x007c7c01, 0x007c7c01, 0x007c7c01, 0x006c6901, 0x007c7901, ++ 0x007c7c01, 0x007c7c01, 0x00747201, 0x00747201, 0x00747201, 0x00747201, 0x007c7c01, ++ 0x007c7c01, 0x007c7c01, 0x006c6901, 0x007c7c01, 0x00797901, 0x00797901, 0x007c7c01, ++ 0x006c6901, 0x00797901, 0x00797901, 0x00797901, ++}; ++ ++static void t7000_bridges(struct pvr_apple_s8003 *s) ++{ ++ unsigned int i; ++ ++ writel(0x80010004, s->bridges + 0x18); ++ for (i = 0; i < ARRAY_SIZE(t7000_bridge11); i++) ++ writel(t7000_bridge11[i], s->bridges + 0x80000 + 4 * i); ++} ++ ++/* ++ * Power the GPU on as the bring-up module does, in Apple's order: the ++ * power manager's clock gating (0x849d1bc) and SoC voltage manager ++ * (0x849d340) enables, then the GPU's power state with automatic power ++ * management (K3-K5), the bridge settings (K6), and Fender's wake into ++ * state 1 with the META held back by an empty boot stream (K7). The core's ++ * ID can be read then. A GPU already on (a restart without power-off) is ++ * only woken. ++ */ ++static int t7000_boot(struct pvr_apple *apple) ++{ ++ struct pvr_apple_s8003 *s = apple->chip_data; ++ void __iomem *ps = s->pmgr + T7000_PMGR_PS_GFX; ++ unsigned int n; ++ u32 val; ++ int err; ++ ++ if (s->stuck) ++ return -EIO; ++ /* ++ * Every stop powers the GPU off (t7000_core_off()): one that is on was ++ * left on by something else. The module refuses such a GPU, and so ++ * does this. ++ */ ++ if (FIELD_GET(T7000_PS_ACTUAL, pmgr_read(s, T7000_PMGR_PS_GFX))) { ++ dev_err(apple_dev(apple), "the GPU is powered already: not touched\n"); ++ return -EBUSY; ++ } ++ for (n = 1; n <= 3; n++) { ++ if (!(pmgr_read(s, T7000_PMGR_GPU_TABLE + 0x10 * n) & T7000_GPU_TABLE_VALID)) { ++ dev_err(apple_dev(apple), "GPU performance record %u not valid\n", n); ++ return -EIO; ++ } ++ } ++ val = pmgr_read(s, T7000_PMGR_MISC_ACG); ++ if (!(val & 1)) ++ writel(val | 1, s->pmgr + T7000_PMGR_MISC_ACG); ++ if (pmgr_read(s, T7000_PMGR_VM_CTL) & 0xf00) { ++ dev_err(apple_dev(apple), "the voltage manager is off: GPU not powered\n"); ++ return -EIO; ++ } ++ val = pmgr_read(s, T7000_PMGR_SOC_TVM); ++ if (!(val & 1)) { ++ writel(val | 1, s->pmgr + T7000_PMGR_SOC_TVM); ++ udelay(100); ++ } ++ msleep(T7000_SETTLE_MS); /* the SRAM rail SOC_TVM raises, before the GPU powers */ ++ /* K3 (0x84973ac): minimum state 0, history cleared */ ++ val = readl(ps); ++ writel((val & 0xfff00000) | (val & 0xfcff), ps); ++ err = t7000_ps_set(apple, T7000_PS_ON); ++ if (err) ++ goto ps_off; ++ t7000_bridges(s); ++ ++ msleep(T7000_SETTLE_MS); ++ writel(FENDER_STREAM_END, apple->fender + FENDER_STREAM); ++ writel(FENDER_CTRL_BOOT, apple->fender + FENDER_CTRL); ++ mutex_lock(&apple->perf_lock); ++ err = t7000_fender_state(apple, 1); ++ mutex_unlock(&apple->perf_lock); ++ if (!err) { ++ usleep_range(10, 20); ++ msleep(T7000_SETTLE_MS); ++ return 0; ++ } ++ if (t7000_fender_state(apple, 0)) { ++ /* Apple panics here ("GPC state transition is stuck") */ ++ t7000_stuck(apple, "Fender stuck at the wake"); ++ return err; ++ } ++ps_off: ++ t7000_ps_set(apple, 0); ++ return err; ++} ++ ++static int t7000_core_off(struct pvr_apple *apple); ++ ++/* The core's ID, as the bring-up module reads it: two gated 32-bit reads */ ++static int pvr_apple_t7000_read_gpu_id(struct pvr_apple *apple, struct pvr_gpu_id *gpu_id) ++{ ++ struct pvr_device *pvr_dev = apple->pvr_dev; ++ u32 rev = 0, id = 0; ++ int err = -EIO; ++ ++ if (t7000_r0_gate(apple)) { ++ rev = pvr_cr_read32(pvr_dev, ROGUE_CR_CORE_REVISION); ++ if (t7000_r0_gate(apple)) { ++ id = pvr_cr_read32(pvr_dev, ROGUE_CR_CORE_ID); ++ err = 0; ++ } ++ } ++ /* ++ * As the module: the core goes back to Fender state 0 at once, and ++ * the GPU off if the ID was not read. The release starts from state 0. ++ */ ++ if (err) { ++ t7000_core_off(apple); ++ return err; ++ } ++ msleep(T7000_SETTLE_MS); ++ mutex_lock(&apple->perf_lock); ++ err = t7000_fender_state(apple, 0); ++ mutex_unlock(&apple->perf_lock); ++ if (err) { ++ t7000_stuck(apple, "Fender stuck after the ID read"); ++ return err; ++ } ++ if (rev != T7000_CORE_REVISION) { ++ dev_err(apple_dev(apple), "CORE_REVISION %#x is not the A8's\n", rev); ++ return -ENODEV; ++ } ++ gpu_id->b = FIELD_GET(GENMASK(23, 16), rev); ++ gpu_id->v = FIELD_GET(GENMASK(15, 8), rev); ++ gpu_id->n = FIELD_GET(GENMASK(15, 8), id); ++ gpu_id->c = FIELD_GET(GENMASK(7, 0), id); ++ return 0; ++} ++ ++/* ++ * One 32- or 64-bit write of the boot stream, as Apple's Fender writers ++ * (0x847a858, 0x847a8b4) store them: no read-back. ++ */ ++static void t7000_stream_word(struct s8003_stream *stream, u32 word) ++{ ++ if (stream->offset >= FENDER_STREAM_SIZE) { ++ stream->err = -ENOSPC; ++ return; ++ } ++ writel(word, stream->apple->fender + FENDER_STREAM + stream->offset); ++ stream->offset += 4; ++} ++ ++static void t7000_stream_write32(struct s8003_stream *stream, u32 reg, u32 value) ++{ ++ t7000_stream_word(stream, reg | 0xff << 16); ++ t7000_stream_word(stream, value); ++} ++ ++static void t7000_stream_write64(struct s8003_stream *stream, u32 reg, u64 value) ++{ ++ t7000_stream_word(stream, reg + 4); ++ t7000_stream_word(stream, upper_32_bits(value)); ++ t7000_stream_word(stream, reg | 0xff << 16); ++ t7000_stream_word(stream, lower_32_bits(value)); ++} ++ ++/* ++ * Apple's stop (0x846ebf8): Fender's core word 0, then state 0, waiting for ++ * each; then the stored boot stream is cut back to its end marker, so that ++ * no later power-up replays it, and the GPU's power state goes off: every ++ * boot starts from a GPU that is off, in the full power-on order. A Fender that stays busy (Apple panics) is ++ * left alone, and so is everything after it. ++ */ ++static int t7000_core_off(struct pvr_apple *apple) ++{ ++ struct pvr_apple_s8003 *s = apple->chip_data; ++ u32 val; ++ int err; ++ ++ if (s->stuck || !t7000_ps_on(s)) ++ return s->stuck ? -EIO : 0; ++ writel(0, apple->fender + FENDER_CORE); ++ err = readl_poll_timeout(apple->fender + FENDER_POWER_STATE, val, ++ !(val & FENDER_POWER_REQUEST), 5, T7000_FENDER_POLL_US); ++ if (!err) { ++ mutex_lock(&apple->perf_lock); ++ err = t7000_fender_state(apple, 0); ++ mutex_unlock(&apple->perf_lock); ++ } ++ if (err) { ++ t7000_stuck(apple, "Fender stuck at the stop"); ++ return err; ++ } ++ writel(FENDER_STREAM_END, apple->fender + FENDER_STREAM); ++ /* and the GPU's power state off, as the module ends every run */ ++ return t7000_ps_set(apple, 0); ++} ++ ++/* ++ * Boot the META (Apple's prepareBoot 0x846f044 and start 0x846eb10): with ++ * the core in state 0, store the boot stream (reset, boot mode, the page ++ * catalogue of GPU context 0 in BIF_CAT_BASE0 as configureMMUForBoot ++ * 0x8477e8c does, the SLC setup of populateInitSequence 0x8477eb0; no spare ++ * page), then release the core word and power the core up in state @q. ++ * Fender replays the stream, whose last write releases the reset. ++ */ ++static int t7000_release(struct pvr_apple *apple, u32 q) ++{ ++ struct pvr_apple_s8003 *s = apple->chip_data; ++ struct s8003_stream stream = { .apple = apple }; ++ u32 val; ++ int err; ++ ++ if (s->stuck) ++ return -EIO; ++ mutex_lock(&apple->perf_lock); ++ err = t7000_fender_state(apple, 0); ++ mutex_unlock(&apple->perf_lock); ++ if (err) { ++ t7000_stuck(apple, "Fender stuck before the boot"); ++ return err; ++ } ++ msleep(T7000_SETTLE_MS); ++ (void)readl(apple->fender + FENDER_STREAM_STATE); ++ writel(0, apple->fender + FENDER_STREAM_STATE); ++ (void)readl(apple->fender + FENDER_STREAM_STATE); ++ writel(FENDER_CTRL_BOOT, apple->fender + FENDER_CTRL); ++ t7000_stream_write64(&stream, ROGUE_CR_SOFT_RESET, S8003_SOFT_RESET_ALL); ++ t7000_stream_write64(&stream, ROGUE_CR_SOFT_RESET, S8003_SOFT_RESET_CORE); ++ t7000_stream_write64(&stream, ROGUE_CR_SOFT_RESET, ROGUE_CR_SOFT_RESET_GARTEN_EN); ++ t7000_stream_write32(&stream, ROGUE_CR_META_BOOT, ROGUE_CR_META_BOOT_MODE_EN); ++ t7000_stream_write64(&stream, ROGUE_CR_BIF_CAT_BASE0, ++ pvr_vm_get_page_table_root_addr(apple->pvr_dev->kernel_vm_ctx)); ++ t7000_stream_write64(&stream, ROGUE_CR_SLC_CTRL_MISC, 0x200001); ++ t7000_stream_write64(&stream, T7000_CR_SLC_3808, 1); ++ t7000_stream_write64(&stream, ROGUE_CR_SOFT_RESET, 0); ++ t7000_stream_word(&stream, FENDER_STREAM_END); ++ if (stream.err) { ++ dev_err(apple_dev(apple), "Fender did not take the boot stream: %d\n", stream.err); ++ return stream.err; ++ } ++ wmb(); /* the image and init data are in memory before the META runs */ ++ ++ msleep(T7000_SETTLE_MS); ++ writel(0, apple->fender + FENDER_CORE); ++ err = readl_poll_timeout(apple->fender + FENDER_POWER_STATE, val, ++ !(val & FENDER_POWER_REQUEST), 5, T7000_FENDER_POLL_US); ++ if (err) { ++ t7000_stuck(apple, "Fender busy before the release"); ++ return err; ++ } ++ /* As Apple's start: the request, then the alive event is waited for in memory */ ++ mutex_lock(&apple->perf_lock); ++ writel(FENDER_POWER_REQUEST | q, apple->fender + FENDER_POWER_STATE); ++ apple->perf_state = q; ++ mutex_unlock(&apple->perf_lock); ++ return 0; ++} ++ ++/* Apple panics in every caller of its alive wait (0x8471040, 0x846dc1c, 0x846f9a0) */ ++static void t7000_no_alive(struct pvr_apple *apple) ++{ ++ t7000_stuck(apple, "no alive event from the firmware"); ++} ++ ++/* After the stop, for runtime suspend: the GPU's power state goes off */ ++static void t7000_idle(struct pvr_apple *apple) ++{ ++ struct pvr_apple_s8003 *s = apple->chip_data; ++ ++ if (!s->stuck && t7000_ps_on(s)) ++ t7000_ps_set(apple, 0); ++} ++ ++/* ++ * Apple's kick, the same in all five of its MTS_SCHEDULE writers (TA ++ * 0x846f220, 3D 0x846f408, CL 0x846f5f0, device control 0x846f9a0; COUNTED ++ * and the data master): Fender's wake, the power state read, then the META's ++ * scheduler register. In state 0 (the firmware idled the core) Apple rings ++ * as long as Fender is set up and the snoop is armed; with that write Fender ++ * wakes the core (on the iPod: state 1 and the PLL locked within 1 ms). ++ * Apple restarts the firmware when Fender's control is 0 and warns when the ++ * snoop is not armed; this driver refuses both. In state 1 the PLL must be ++ * locked, as for every access to the Rogue registers. ++ */ ++static int t7000_doorbell(struct pvr_apple *apple, u32 dm) ++{ ++ struct pvr_apple_s8003 *s = apple->chip_data; ++ u32 state, pll, ctrl, snoop; ++ ++ return -EIO; ++ mb(); /* what the firmware is rung for is in memory */ ++ writel(FENDER_WAKE_ALL, apple->fender + FENDER_WAKE); ++ state = readl(apple->fender + FENDER_POWER_STATE); ++ pll = pmgr_read(s, T7000_PMGR_PLL5); ++ if (!(state & 0xf)) { ++ ctrl = readl(apple->fender + FENDER_CTRL); ++ snoop = readl(apple->fender + T7000_FENDER_SNOOP); ++ if (!ctrl || (!(ctrl & T7000_FENDER_CTRL_SNOOP) && !(snoop & BIT(1)))) { ++ dev_err_ratelimited(apple_dev(apple), ++ "doorbell not rung: Fender state %#x, control %#x, snoop %#x\n", ++ state, ctrl, snoop); ++ return -EIO; ++ } ++ } else if (!(pll & T7000_PLL_LOCKED)) { ++ dev_err_ratelimited(apple_dev(apple), ++ "doorbell not rung: Fender state %#x, PLL5 %#x\n", state, pll); ++ return -EIO; ++ } ++ pvr_cr_write32(apple->pvr_dev, ROGUE_CR_MTS_SCHEDULE, ROGUE_CR_MTS_SCHEDULE_TASK_COUNTED | dm); ++ return 0; ++} ++ ++/* The hardware objects at their addresses in a VM */ ++static int t7000_vm_map(struct pvr_apple *apple, struct pvr_vm_context *vm_ctx) ++{ ++ struct pvr_apple_s8003 *s = apple->chip_data; ++ unsigned int i; ++ int err = 0; ++ ++ for (i = 0; !err && i < T7000_HW_OBJS; i++) { ++ if (!s->hwobj[i].obj) ++ return -ENODEV; ++ err = pvr_vm_map_reserved(vm_ctx, s->hwobj[i].obj, ++ T7000_HWOBJ_VA + t7000_hwobj_offset[i], T7000_HWOBJ_SIZE); ++ } ++ return err; ++} ++ ++static void t7000_vm_unmap(struct pvr_apple *apple, struct pvr_vm_context *vm_ctx) ++{ ++ struct pvr_apple_s8003 *s = apple->chip_data; ++ unsigned int i; ++ ++ for (i = 0; i < T7000_HW_OBJS; i++) ++ if (s->hwobj[i].obj) ++ pvr_vm_unmap_obj(vm_ctx, s->hwobj[i].obj, ++ T7000_HWOBJ_VA + t7000_hwobj_offset[i], T7000_HWOBJ_SIZE); ++} ++ ++static void t7000_vm_put(struct pvr_apple *apple, struct s8003_vm *vm) ++{ ++} ++ ++static void t7000_iounmap(void *addr) ++{ ++ iounmap((void __iomem *)addr); ++} ++ ++/* ++ * Registers another driver owns as well (the power manager's): mapped, not ++ * requested, and non-posted as every Apple device's. ++ */ ++static void __iomem *t7000_ioremap(struct device *dev, const char *name) ++{ ++ struct resource *res = platform_get_resource_byname(to_platform_device(dev), ++ IORESOURCE_MEM, name); ++ void __iomem *addr; ++ ++ if (!res) ++ return NULL; ++ addr = ioremap_np(res->start, resource_size(res)); ++ if (!addr || devm_add_action_or_reset(dev, t7000_iounmap, (void __force *)addr)) ++ return NULL; ++ return addr; ++} ++ ++/* ++ * The power manager's and the bridges' registers, and BorderColorTable's ++ * contents: 0x800 bytes of the kext's data (0xfffffff0088f1460), shipped ++ * beside the firmware. Without them the GPU is not started: the firmware ++ * runs only as the bring-up module ran it. ++ */ ++static int t7000_init(struct pvr_apple *apple) ++{ ++ struct device *dev = apple_dev(apple); ++ struct pvr_apple_s8003 *s = apple->chip_data; ++ const struct firmware *border; ++ int err; ++ ++ s->pmgr = t7000_ioremap(dev, "pmgr"); ++ s->bridges = t7000_ioremap(dev, "bridges"); ++ if (!s->pmgr || !s->bridges) ++ return dev_err_probe(dev, -EINVAL, "no power manager or bridge registers\n"); ++ ++ err = request_firmware(&border, T7000_BORDER_FW, dev); ++ if (err) ++ return dev_err_probe(dev, err, "no %s\n", T7000_BORDER_FW); ++ if (border->size != T7000_BORDER_SIZE) { ++ dev_err(dev, "%s has %zu bytes, not %u\n", T7000_BORDER_FW, border->size, ++ T7000_BORDER_SIZE); ++ err = -EINVAL; ++ } else { ++ s->border = devm_kmemdup(dev, border->data, T7000_BORDER_SIZE, GFP_KERNEL); ++ if (!s->border) ++ err = -ENOMEM; ++ } ++ release_firmware(border); ++ return err; ++} ++ + static const struct pvr_meta_ldr_target s8003_ldr_target = { + .mem = s8003_ldr_mem, + .config = s8003_ldr_config, +@@ -3167,9 +4245,45 @@ static const struct s8003_variant s8003_variant_a9 = { + .report_hang = s8003_report_hang, + }; + ++/* The A8: its own sizes and boot; its jobs as the A9's where the firmware takes them the same way */ ++static const struct s8003_variant s8003_variant_a8 = { ++ .name = "A8", ++ .page_size = SZ_4K, ++ .image_size = T7000_IMAGE_SIZE, ++ .root_size = T7000_ROOT_SIZE, ++ .shared_size = T7000_SHARED_SIZE, ++ .root_objects_end = T7000_ROOT_OBJECTS_END, ++ .shared_objects_end = T7000_SHARED_OBJECTS_END, ++ .objects = s8003_objects_a8, ++ .nr_objects = ARRAY_SIZE(s8003_objects_a8), ++ .ring_ctl_size = T7000_RING_CTL_SIZE, ++ .slots = T7000_SLOTS, ++ .slot_first = T7000_SLOT_FIRST, ++ .vm_slots = 1, ++ .channel = { T7000_TA_CHANNEL, T7000_3D_CHANNEL, T7000_CU_CHANNEL }, ++ .channels_root = true, ++ .ldr_conf_count = S8003_LDR_CONF_MAX, ++ .ldr = &t7000_ldr_target, ++ .init = t7000_init, ++ .image_configure = t7000_image_configure, ++ .map = t7000_map, ++ .unmap = t7000_unmap, ++ .init_build = t7000_init_build, ++ .channels_init = t7000_channels_init, ++ .boot = t7000_boot, ++ .release = t7000_release, ++ .core_off = t7000_core_off, ++ .idle = t7000_idle, ++ .doorbell = t7000_doorbell, ++ .no_alive = t7000_no_alive, ++ .vm_map = t7000_vm_map, ++ .vm_put = t7000_vm_put, ++ .vm_unmap = t7000_vm_unmap, ++}; ++ + static const struct s8003_variant *s8003_variant_of(const struct pvr_apple_chip *chip) + { +- return &s8003_variant_a9; ++ return chip == &pvr_apple_chip_t7000 ? &s8003_variant_a8 : &s8003_variant_a9; + } + + const struct pvr_apple_chip pvr_apple_chip_s8003 = { +@@ -3207,3 +4321,74 @@ const struct pvr_apple_chip pvr_apple_chip_s8003 = { + .mmu_flush = pvr_apple_s8003_mmu_flush, + .irq_ack = pvr_apple_s8003_irq_ack, + }; ++ ++/* ++ * The A8's heaps, as its firmware lays out a VM (heap setup 0x847efb8; the ++ * bases PLATFORM tells the firmware): the general heap at 0x1_00000000, the ++ * PDS heap at the PDS and USC execution base 0x3_00000000, USC code behind ++ * it at 0x3_80000000, then region headers, visibility test and transfer ++ * fragments. The general heap ends at 0x1_f0000000: the 256 MiB behind it ++ * hold the driver's PM tables in every VM (T7000_PM_*_VA). The hardware ++ * objects' 4 MiB at 0x8_00000000, reserved in every GART by Apple's kext, ++ * are outside every heap, so userspace cannot map there. ++ */ ++static const struct drm_pvr_heap t7000_heaps[] = { ++ [DRM_PVR_HEAP_GENERAL] = { ++ .base = 0x100000000ull, ++ .size = 0xf0000000ull, ++ .page_size_log2 = PVR_DEVICE_PAGE_SHIFT, ++ }, ++ [DRM_PVR_HEAP_PDS_CODE_DATA] = { ++ .base = 0x300000000ull, ++ .size = 0x10000000ull, ++ .page_size_log2 = PVR_DEVICE_PAGE_SHIFT, ++ }, ++ [DRM_PVR_HEAP_USC_CODE] = { ++ .base = 0x380000000ull, ++ .size = 0x10000000ull, ++ .page_size_log2 = PVR_DEVICE_PAGE_SHIFT, ++ }, ++ [DRM_PVR_HEAP_RGNHDR] = { ++ .base = 0x500000000ull, ++ .size = 0x10000000ull, ++ .page_size_log2 = PVR_DEVICE_PAGE_SHIFT, ++ }, ++ [DRM_PVR_HEAP_VIS_TEST] = { ++ .base = 0x580000000ull, ++ .size = 0x10000000ull, ++ .page_size_log2 = PVR_DEVICE_PAGE_SHIFT, ++ }, ++ [DRM_PVR_HEAP_TRANSFER_FRAG] = { ++ .base = 0x600000000ull, ++ .size = 0x10000000ull, ++ .page_size_log2 = PVR_DEVICE_PAGE_SHIFT, ++ }, ++}; ++ ++/* ++ * The A8 (T7000). Its firmware is booted and runs as the A9's is, with the ++ * variant above. It starts in power state 1, Apple's start state and the ++ * only one whose PLL lock is proven on Linux; the firmware is told all three ++ * of its states, and the host pins it there (PLATFORM, CONTROL). ++ */ ++const struct pvr_apple_chip pvr_apple_chip_t7000 = { ++ .fw_name = "apple/gfx-t7000.bin", ++ .perf_state_max = 1, ++ .own_pm_pages = true, ++ .gate_off_ms = 10, ++ .irq_poll_us = 1000, ++ .heaps = t7000_heaps, ++ .read_gpu_id = pvr_apple_t7000_read_gpu_id, ++ .init = pvr_apple_s8003_init, ++ .boot = pvr_apple_s8003_boot, ++ .start = pvr_apple_s8003_start, ++ .stop = pvr_apple_s8003_stop, ++ .idle = pvr_apple_s8003_idle, ++ .fini = pvr_apple_s8003_fini, ++ .restore_image = pvr_apple_s8003_restore_image, ++ .events = pvr_apple_s8003_events, ++ .vm_init = pvr_apple_s8003_vm_init, ++ .vm_fini = pvr_apple_s8003_vm_fini, ++ .mmu_flush = pvr_apple_s8003_mmu_flush, ++ .irq_ack = pvr_apple_s8003_irq_ack, ++}; +diff --git a/drivers/gpu/drm/imagination/pvr_drv.c b/drivers/gpu/drm/imagination/pvr_drv.c +index d97e9d59f7..48cfc9043a 100644 +--- a/drivers/gpu/drm/imagination/pvr_drv.c ++++ b/drivers/gpu/drm/imagination/pvr_drv.c +@@ -1537,6 +1537,12 @@ static const struct pvr_device_data pvr_device_data_apple_s8003 = { + .apple_fw = true, + .apple_chip = &pvr_apple_chip_s8003, + }; ++ ++static const struct pvr_device_data pvr_device_data_apple_t7000 = { ++ .pwr_ops = &pvr_power_sequence_ops_apple, ++ .apple_fw = true, ++ .apple_chip = &pvr_apple_chip_t7000, ++}; + #endif + + static const struct of_device_id dt_match[] = { +@@ -1549,6 +1555,10 @@ static const struct of_device_id dt_match[] = { + .compatible = "apple,t8010-gpu", + .data = &pvr_device_data_apple, + }, ++ { ++ .compatible = "apple,t7000-gpu", ++ .data = &pvr_device_data_apple_t7000, ++ }, + #endif + { + .compatible = "thead,th1520-gpu", diff --git a/kernel/patches/drivers/gpu/0068-drm-imagination-apple-Run-compute-jobs-on-the-A8-T70.patch b/kernel/patches/drivers/gpu/0068-drm-imagination-apple-Run-compute-jobs-on-the-A8-T70.patch new file mode 100644 index 00000000..f98c8a73 --- /dev/null +++ b/kernel/patches/drivers/gpu/0068-drm-imagination-apple-Run-compute-jobs-on-the-A8-T70.patch @@ -0,0 +1,255 @@ +From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 +From: =?UTF-8?q?Berkay=20K=C3=B6sebay?= +Date: Sat, 21 Nov 2026 12:06:00 +0100 +Subject: [PATCH] drm/imagination: apple: Run compute jobs on the A8 (T7000) + +Give the A8's firmware compute jobs, in the form a bring-up module ran +Mesa's compute jobs with on the iPod touch 6: + +- Slots: eight, the host's from 1, one per VM (Apple's kext has no + slot pair on the A8). A VM's slot is loaded with device control 3 as on + the A9, and its tag moves when its page tables change. +- The compute channel in the root memory's uncached window, in the A9's + channel layout, which the A8's firmware takes. +- The A8's record (the A9's with the offsets from 0x10 on moved by 8, + the terminate descriptor 0x00900004) and Apple's microsequence, which + passes the stamp's previous value; the userspace registers placed + where Mesa's job expects them: context store, resume and terminate + programs, the configuration word, context state, control stream and + its terminate word. Jobs take the A9's G5P command stream. +- The work-ring entry, then Apple's kick with the CL data master (DM2), + the doorbell that rings a core the firmware idled. +- A job is done when its finish stamp, the microsequence's last write, + reaches its number. + +Render jobs are refused until the A8's parameter manager is in. + +Assisted-by: LLM +--- + drivers/gpu/drm/imagination/pvr_apple_s8003.c | 188 +++++++++++++++++- + 1 file changed, 187 insertions(+), 1 deletion(-) + +diff --git a/drivers/gpu/drm/imagination/pvr_apple_s8003.c b/drivers/gpu/drm/imagination/pvr_apple_s8003.c +index 0b7ba98a47..4e1c8f0d27 100644 +--- a/drivers/gpu/drm/imagination/pvr_apple_s8003.c ++++ b/drivers/gpu/drm/imagination/pvr_apple_s8003.c +@@ -4143,6 +4143,174 @@ static void t7000_vm_put(struct pvr_apple *apple, struct s8003_vm *vm) + { + } + ++/* ++ * The last job of @engine that is done: its finish stamp at its number or ++ * past it. The finish stamp is the last word a job's microsequence writes ++ * (TA 0x24, 3D 0x26, CU 0x33), and what the bring-up module waits for; the ++ * mirror stamp and the stamp event follow it. ++ */ ++static u32 t7000_engine_done(struct pvr_apple *apple, enum pvr_apple_engine engine) ++{ ++ struct pvr_apple_s8003 *s = apple->chip_data; ++ u32 latest = s8003_submitted(apple, engine); ++ u32 stamp = engine == APPLE_ENGINE_COMPUTE ? S8003_CU_STAMP : S8003_CH_STAMP; ++ s32 behind; ++ ++ if (!READ_ONCE(apple->booted) || !s->root.map) ++ return latest; ++ behind = get32(s8003_channel(s, engine) + stamp + 4) - latest; ++ return latest + min(behind, 0); ++} ++ ++/* ++ * Tell the firmware that @engine's pointer ring is filled up to @end: an ++ * entry of the engine's work ring {engine, channel, end}, with Apple's 1 in ++ * its fourth word on a render channel (P5b), the host's watermark, the work ++ * ring's producer index, and Apple's kick with the engine's data master (TA ++ * DM0, 3D DM1, CL DM2: 0x846f220, 0x846f408, 0x846f5f0). ++ */ ++static int t7000_publish(struct pvr_apple *apple, enum pvr_apple_engine engine, u32 end) ++{ ++ static const u32 dm[] = { ++ ROGUE_CR_MTS_SCHEDULE_DM_DM0, ROGUE_CR_MTS_SCHEDULE_DM_DM1, ROGUE_CR_MTS_SCHEDULE_DM_DM2, ++ }; ++ struct pvr_apple_s8003 *s = apple->chip_data; ++ u32 fw_engine = s8003_engine(engine); ++ u8 *ctl = s8003_object(s, S8003_WORK_CTL + 4 * fw_engine); ++ u8 *ring = s8003_object(s, S8003_WORK_RING + 4 * fw_engine); ++ u32 host = engine == APPLE_ENGINE_COMPUTE ? S8003_CH_CTL : T7000_RCH_CTL; ++ u32 start = ring_read(ctl, S8003_RING_PRODUCER), consumer; ++ u8 *p = s8003_channel(s, engine), *entry; ++ int err; ++ ++ if (start >= APPLE_RING_ENTRIES) ++ return -EPROTO; ++ /* The firmware takes a work-ring entry at once; the ring never holds two */ ++ err = read_poll_timeout(s8003_ring_consumer, consumer, consumer == start, 20, ++ S8003_RING_TIMEOUT_US, false, apple, ctl); ++ if (err) ++ return err; ++ entry = ring + start * S8003_DC_SIZE; ++ memset(entry, 0, S8003_DC_SIZE); ++ put32(entry, fw_engine); ++ put32(entry + 4, s8003_channel_addr(s, engine)); ++ put32(entry + 8, end); ++ if (engine != APPLE_ENGINE_COMPUTE) ++ put32(entry + 12, 1); ++ wmb(); ++ put32(p + host + S8003_CH_CTL_HOST, end); ++ wmb(); ++ WRITE_ONCE(*(__le32 *)(ctl + S8003_RING_PRODUCER), ++ cpu_to_le32((start + 1) % APPLE_RING_ENTRIES)); ++ return t7000_doorbell(apple, dm[fw_engine]); ++} ++ ++/* The A8's compute record: the A9's with every offset from 0x10 on 8 further */ ++#define T7000_CU_W_SIZE 0x298 ++#define T7000_CU_TERMINATE1 0x00900004 /* the terminate program's descriptor (A9 0x00a00004) */ ++ ++/* ++ * A compute job on its VM's slot, one at a time: the record and the ++ * microsequence word for word as Apple's in P5's CL slots (start 0x32 with ++ * the stamp's previous value k twice, the timestamps around the wait for ++ * the engine's finished event, finish 0x33), userspace's registers placed ++ * as the bring-up module ran Mesa's records: the context store, resume ++ * and terminate programs and their descriptors, the configuration word, ++ * the context state and the control stream with its terminate word. ++ */ ++static int t7000_submit_compute(struct pvr_apple *apple, struct s8003_vm *vm, struct pvr_job *job) ++{ ++ const struct pvr_apple_g5p_cmd_compute *cmd = job->cmd; ++ const struct pvr_apple_g5p_compute_regs *g = &cmd->g5p; ++ struct pvr_apple_s8003 *s = apple->chip_data; ++ u32 addr = s8003_channel_addr(s, APPLE_ENGINE_COMPUTE); ++ u32 global = s8003_object_addr(&s->layout, S8003_CHANNEL + 2); ++ u32 w = addr + S8003_CH_W, stamp = addr + S8003_CU_STAMP, slot = vm->slot[0], seq, k; ++ u8 *p = s8003_channel(s, APPLE_ENGINE_COMPUTE); ++ u8 *work = p + S8003_CH_W; ++ unsigned int i; ++ int err; ++ ++ err = s8003_wait_done(apple, APPLE_ENGINE_COMPUTE, apple->last_seq); ++ if (err) ++ return s8003_submit_error(apple, vm, "compute", err, "record slot wait"); ++ seq = apple->last_seq + 1; ++ k = seq - 1; ++ { ++ const u32 useq[] = { ++ 0x32, w + 0x18, w + 0x68, global + 0x720, addr, slot, k, k, 0, w + 0x1f0, ++ 1, 0, seq, ++ 0x80000018, w + 0x280, w + 0x288, w + 0x288, seq, ++ 0x101, 0x004a000a, 1, 1, ++ 0x18, w + 0x280, w + 0x288, w + 0x28c, seq, ++ 0x33, global + 0x720, addr, slot, w + 0x1f0, 1, 0, seq, ++ stamp + 4, seq, (u32)-0x6c, ++ 0x40000017, ++ }; ++ ++ BUILD_BUG_ON(sizeof(useq) > S8003_CU_USEQ_SIZE); ++ memset(p + S8003_CU_USEQ, 0, S8003_CU_USEQ_SIZE); ++ useq_put(p + S8003_CU_USEQ, useq, ARRAY_SIZE(useq)); ++ } ++ memset(work, 0, T7000_CU_W_SIZE); ++ put32(work, 3); ++ put32(work + 8, slot); ++ put32(work + 0xc, addr + S8003_CU_DEP); ++ put32(work + 0x18, g->config[0]); ++ put32(work + 0x1c, g->config[1]); ++ put64(work + 0x68, g->tail); ++ put64(work + 0x70, g->pds_resume); ++ put64(work + 0x78, cmd->cmd.regs.cdm_ctrl_stream_base); ++ put32(work + 0x1e8, addr + S8003_CU_USEQ); ++ put32(work + 0x1ec, S8003_CU_USEQ_SIZE); ++ for (i = 0; i < 4; i++) ++ put64(work + 0x1f4 + i * 8, g->state[i]); ++ put64(work + 0x214, g->tail); ++ put64(work + 0x21c, g->terminate); ++ put64(work + 0x224, g->pds_store); ++ put32(work + 0x22c, g->pds_store1); ++ put64(work + 0x234, g->pds_resume); ++ put32(work + 0x23c, g->pds_resume1); ++ put64(work + 0x244, g->pds_terminate); ++ put32(work + 0x24c, T7000_CU_TERMINATE1); ++ put32(work + 0x260, stamp); ++ put32(work + 0x264, stamp + 4); ++ put32(work + 0x268, seq); ++ put32(work + 0x278, seq); ++ put32(work + 0x27c, k); ++ put32(p + S8003_CH_RING + ((seq - 1) % S8003_CH_ENTRIES) * 4, w); ++ apple->last_seq = seq; ++ err = t7000_publish(apple, APPLE_ENGINE_COMPUTE, seq % S8003_CH_ENTRIES); ++ if (err) ++ return s8003_submit_error(apple, vm, "compute", err, "publish"); ++ return seq; ++} ++ ++/* Render jobs come with the A8's parameter manager */ ++static int t7000_submit_render(struct pvr_apple *apple, struct s8003_vm *vm, struct pvr_job *job) ++{ ++ return -EOPNOTSUPP; ++} ++ ++static int t7000_prepare_job(struct pvr_apple *apple, struct pvr_job *job) ++{ ++ return job->type == DRM_PVR_JOB_TYPE_COMPUTE ? 0 : -EOPNOTSUPP; ++} ++ ++static void t7000_report_hang(struct pvr_apple *apple) ++{ ++ struct pvr_apple_s8003 *s = apple->chip_data; ++ const u8 *p = s8003_channel(s, APPLE_ENGINE_COMPUTE); ++ const u8 *ctl = s8003_object(s, S8003_WORK_CTL + 8); ++ ++ dev_err(apple_dev(apple), ++ "hang: compute: submitted %u, stamps %u %u, work ring %u/%u; firmware %s\n", ++ apple->last_seq, get32(p + S8003_CU_STAMP), get32(p + S8003_CU_STAMP + 4), ++ get32(ctl + S8003_RING_CONSUMER), get32(ctl + S8003_RING_PRODUCER), ++ s->failed ? "rings unreadable" : READ_ONCE(apple->fault) ? "posted a fault" : ++ "posted no fault"); ++} ++ + static void t7000_iounmap(void *addr) + { + iounmap((void __iomem *)addr); +@@ -4279,6 +4447,12 @@ static const struct s8003_variant s8003_variant_a8 = { + .vm_map = t7000_vm_map, + .vm_put = t7000_vm_put, + .vm_unmap = t7000_vm_unmap, ++ .engine_done = t7000_engine_done, ++ .prepare_job = t7000_prepare_job, ++ .submit_geom = t7000_submit_render, ++ .submit_frag = t7000_submit_render, ++ .submit_compute = t7000_submit_compute, ++ .report_hang = t7000_report_hang, + }; + + static const struct s8003_variant *s8003_variant_of(const struct pvr_apple_chip *chip) +@@ -4369,11 +4543,19 @@ static const struct drm_pvr_heap t7000_heaps[] = { + * The A8 (T7000). Its firmware is booted and runs as the A9's is, with the + * variant above. It starts in power state 1, Apple's start state and the + * only one whose PLL lock is proven on Linux; the firmware is told all three +- * of its states, and the host pins it there (PLATFORM, CONTROL). ++ * of its states, and the host pins it there (PLATFORM, CONTROL). Jobs take ++ * the A9's command streams: the A8's records hold the same registers. + */ + const struct pvr_apple_chip pvr_apple_chip_t7000 = { + .fw_name = "apple/gfx-t7000.bin", + .perf_state_max = 1, ++ .geom_stream = &pvr_apple_g5p_cmd_geom_stream, ++ .frag_stream = &pvr_apple_g5p_cmd_frag_stream, ++ .geom_ext_type = PVR_STREAM_EXTHDR_TYPE_GEOM_G5P, ++ .geom_ext_mask = PVR_STREAM_EXTHDR_GEOM_G5P_REGS, ++ .frag_ext_type = PVR_STREAM_EXTHDR_TYPE_FRAG_G5P, ++ .frag_ext_mask = PVR_STREAM_EXTHDR_FRAG_G5P_REGS, ++ .compute_stream = &pvr_apple_g5p_cmd_compute_stream, + .own_pm_pages = true, + .gate_off_ms = 10, + .irq_poll_us = 1000, +@@ -4386,6 +4568,10 @@ const struct pvr_apple_chip pvr_apple_chip_t7000 = { + .idle = pvr_apple_s8003_idle, + .fini = pvr_apple_s8003_fini, + .restore_image = pvr_apple_s8003_restore_image, ++ .report_hang = pvr_apple_s8003_report_hang, ++ .prepare_job = pvr_apple_s8003_prepare_job, ++ .submit = pvr_apple_s8003_submit, ++ .engine_done = pvr_apple_s8003_engine_done, + .events = pvr_apple_s8003_events, + .vm_init = pvr_apple_s8003_vm_init, + .vm_fini = pvr_apple_s8003_vm_fini, diff --git a/kernel/patches/drivers/gpu/0069-drm-imagination-apple-Render-on-the-A8-T7000.patch b/kernel/patches/drivers/gpu/0069-drm-imagination-apple-Render-on-the-A8-T7000.patch new file mode 100644 index 00000000..7f77b678 --- /dev/null +++ b/kernel/patches/drivers/gpu/0069-drm-imagination-apple-Render-on-the-A8-T7000.patch @@ -0,0 +1,943 @@ +From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 +From: =?UTF-8?q?Berkay=20K=C3=B6sebay?= +Date: Sat, 21 Nov 2026 12:07:00 +0100 +Subject: [PATCH] drm/imagination: apple: Render on the A8 (T7000) + +Give the A8's firmware geometry and fragment jobs: + +- The parameter manager, one set per VM, as Apple's fresh queue: a + control block with 36 state records in the root memory and the pool's + page list in slices of 32 in the shared memory (at the VM's slot's + place in each), the PM's own tables in the VM, and the free-list page + stack: a page number array mapped in the VM, filled on the CPU, which + the firmware loads into the PM at every TA start. Without it the TA + faulted in the PM's context. The tables and the stack lie behind the + general heap, where userspace cannot map; the pool's pages are never + mapped. A scene uses S[1], as Apple's first frame; the set is + built for each start of the firmware and again after a scene whose + fragment job never came. Apple's A8 kext sends no PM command. +- The pool's size. Apple's fresh queue starts with 2 blocks (64 pages, + 256 KiB) and grows inside a scene ("Synchronous PB Grow" records) and + with partial renders; this driver has neither. With 2 blocks, scenes + of 16384 small triangles at 256x256 and a lit 3072-triangle sphere + never finished their geometry job: the firmware's out-of-memory + handler marks the TA and waits, and its watchdog skips an engine with + PM_OUT_OF_MEMORY pending, so nothing is reported. A new VM's pool now + starts at a8_pm_blocks (default 32 blocks, 4 MiB; at most 61, what one + set's list holds). Growing a live VM's pool from the firmware's usage + report between scenes, as the A9 and Apple's + growPoolFromFirmwareFeedback do, is behind a8_pm_grow, off by default: + it rebuilds the set under the same PM id, and whether a TA start + reloads the stack then is not known yet. Off, the usage words are + logged at most once a second per VM. +- Region headers as Apple's mirrored ring: userspace maps one buffer + twice back to back; a frame takes the next of its 16 slices (the + target's region headers rounded up to 4 KiB), never cleared. The ring + and its cursor live in the HWRT data set (struct pvr_hwrt_dataset + apple_rgn), as Apple keeps the cursor in each render target's RTM: + set when its first geometry job is prepared at the ioctl, gone with + the data set. A queued job holds its data set, so its ring is there + when the job worker submits it, however many targets are in flight. + (A per-VM table of four rings refused thousands of jobs under vkmark + and glmark2 through Zink: a later prepare took the slot of a target + whose geometry job was still queued.) A data set whose region headers + are not such a ring is refused at its first geometry job. +- The records: the A9's fill at the A8's offsets with Apple's A8 values + where they differ (no TA scratch base, resume or TA PDS words; no + scale words; +0x3c8 0x22000; the 3D ISP and output words for one + sample and an output width of 2), and the render targets' PBE register + words from the Rogue command at Apple's three places. Then the + kernel's links and Apple's microsequences: TA start 0x23, 3D start + 0x25 with 23 arguments, the 3D wait block, both finishes. +- The TA and 3D channels in Apple's form (its header and a 0x144-entry + ring), one record each: TA one pointer-ring entry, 3D the barrier + command on the TA's finish stamp and then the record. Apple's kicks: + TA DM0, 3D DM1. + +The firmware keeps one scene at a time: a geometry job waits for the +last fragment job, a fragment job for its geometry job. One sample and +single-layer targets only. + +Assisted-by: LLM +--- + drivers/gpu/drm/imagination/pvr_apple_s8003.c | 781 +++++++++++++++++- + drivers/gpu/drm/imagination/pvr_hwrt.h | 18 + + 2 files changed, 789 insertions(+), 10 deletions(-) + +diff --git a/drivers/gpu/drm/imagination/pvr_apple_s8003.c b/drivers/gpu/drm/imagination/pvr_apple_s8003.c +index 4e1c8f0d27..db0bf67245 100644 +--- a/drivers/gpu/drm/imagination/pvr_apple_s8003.c ++++ b/drivers/gpu/drm/imagination/pvr_apple_s8003.c +@@ -323,6 +323,8 @@ struct s8003_pm { + bool clean; /* both of its jobs were handed to the firmware */ + }; + ++ ++ + struct s8003_vm { + struct pvr_vm_context *vm_ctx; + /* Only ioctl preparers take this lock; the job worker never waits on it. */ +@@ -332,6 +334,22 @@ struct s8003_vm { + struct s8003_pm pm[2]; + u32 fb_blocks; /* pool size the firmware's feedback asked for */ + ktime_t fb_at; /* when that feedback was last taken */ ++ ++ /* The A8's: its PM set (submit_lock), its scene (rings: &pvr_hwrt_dataset.apple_rgn) */ ++ struct { ++ struct pvr_gem_object *aux, *stack, *pool; ++ u8 *aux_map, *stack_map, *pool_map; ++ u32 blocks; /* the pool's 128 KiB blocks (32 pages each) */ ++ u32 want; /* blocks the feedback or a stall asked for */ ++ struct pvr_gem_object *next_pool; /* @want blocks, taken at a geometry job */ ++ u8 *next_pool_map; ++ u32 next_blocks; ++ u32 built; /* the firmware start the set was built for */ ++ bool open; /* a geometry job waits for its fragment job */ ++ const struct pvr_hwrt_data *hwrt; ++ u32 ta_seq; ++ u64 rgn_addr; /* the scene's region-header slice */ ++ } t7000; + }; + + struct pvr_apple_s8003 { +@@ -402,8 +420,13 @@ struct pvr_apple_s8003 { + struct s8003_mem hwobj[4]; /* the hardware objects at 0x8_00000000 */ + bool t7000_pio_mapped; + bool stuck; /* Fender stayed busy: the GPU is left alone */ ++ u32 t7000_trace; /* the records' trace ID, one per job */ + + struct s8003_idle idle; ++ /* The A8: the smallest PM pool a stall taught; the VM of the last geometry job */ ++ u32 t7000_pm_floor; ++ struct s8003_vm *t7000_geom_vm; ++ + }; + + static void put32(u8 *p, u32 value) +@@ -1774,6 +1797,8 @@ static void pvr_apple_s8003_vm_fini(struct pvr_apple *apple, struct pvr_vm_conte + } + if (s->pm_last == vm) + s->pm_last = NULL; ++ if (s->t7000_geom_vm == vm) ++ s->t7000_geom_vm = NULL; + if (s->pm_prev == vm) + s->pm_prev = NULL; + s8003_wb_sets_update(s); +@@ -4139,10 +4164,6 @@ static void t7000_vm_unmap(struct pvr_apple *apple, struct pvr_vm_context *vm_ct + T7000_HWOBJ_VA + t7000_hwobj_offset[i], T7000_HWOBJ_SIZE); + } + +-static void t7000_vm_put(struct pvr_apple *apple, struct s8003_vm *vm) +-{ +-} +- + /* + * The last job of @engine that is done: its finish stamp at its number or + * past it. The finish stamp is the last word a job's microsequence writes +@@ -4286,15 +4307,754 @@ static int t7000_submit_compute(struct pvr_apple *apple, struct s8003_vm *vm, st + return seq; + } + +-/* Render jobs come with the A8's parameter manager */ +-static int t7000_submit_render(struct pvr_apple *apple, struct s8003_vm *vm, struct pvr_job *job) ++/* ++ * Render jobs. The A8's parameter manager keeps one set per VM: a control ++ * block with 36 state records "S" of 0xc0 bytes behind its first 0x80 (root ++ * memory), a list of the pool's pages in slices of 32 with a link to the ++ * previous slice (shared memory), and in the VM the PM's own tables (36 ++ * macrotile arrays, 36 MLists, the vheap) and the free-list page stack: a ++ * u32 page number array the host fills on the CPU, which the firmware loads ++ * into the PM at every TA start (0x1001a418 -> 0x10025a28). Without the ++ * stack the TA faulted at VA 0x180 in the PM's context. P5b shows Apple's ++ * fresh queue built this way (kext 0x847f58c, 0x8480a28, 0x847fef0); its ++ * first frame uses S[1] for the TA and the 3D job. The pool's pages are ++ * never mapped: the PM writes their numbers to the hardware. ++ */ ++#define T7000_PM_S_COUNT 36 ++#define T7000_PM_S_SIZE 0xc0 ++#define T7000_PM_SLICE 0x84 ++#define T7000_PM_SLICE_PAGES 32 ++/* ++ * The pool, in 128 KiB blocks (a slice of 32 pages each). Apple's fresh ++ * queue starts at 2 and its steady PMs held 8 (0x100 pages), as dumped: ++ * iOS grows the pool, from the firmware's feedback between scenes and with ++ * tag-5 grow records ("Synchronous PB Grow" in the kext's firmware ++ * state dump), and this driver has no grow inside a scene and no partial ++ * render. So it starts larger (a8_pm_blocks; on the iPod, 2 blocks held a ++ * 256x256 mesh of 8192 small triangles but not 16384, some 20-32 bytes a ++ * triangle, so 32 blocks hold about 128k), grows from the feedback ++ * between scenes as the A9 does, and doubles after a stall for ++ * pages. The most is what one set's list page holds: 61 slices of 0x84 ++ * after its 0x80 header (Apple sizes its list for 0x300 blocks). ++ */ ++#define T7000_PM_BLOCKS_MAX ((T7000_PM_SET_SIZE - 0x80) / T7000_PM_SLICE) ++#define T7000_PM_FEEDBACK 0x40 /* list: high-water pages, margin, -, request */ ++#define T7000_PM_FEEDBACK_TAKEN 0x60 /* list: 1 once the host took them */ ++static unsigned int a8_pm_blocks = 32; ++module_param(a8_pm_blocks, uint, 0644); ++MODULE_PARM_DESC(a8_pm_blocks, "A8: a new VM's PM pool in 128 KiB blocks (2..61, default 32 = 4 MiB)"); ++/* ++ * Growing a live VM's pool rebuilds its set under the same PM id while the ++ * firmware may hold that id loaded; whether a TA start reloads the stack ++ * then is not read yet (10007eec, 10007fd8), and the A8's usage words are ++ * the A9's by analogy. Until a test shows both, the feedback is only ++ * logged, and a stall raises the pool of later VMs only. ++ */ ++static bool a8_pm_grow; ++module_param(a8_pm_grow, bool, 0644); ++MODULE_PARM_DESC(a8_pm_grow, "A8: grow a live VM's PM pool from the firmware's feedback (default 0: log the feedback only)"); ++#define T7000_PM_MACROTILE 0x80 ++#define T7000_PM_MLIST 0x10000 ++#define T7000_PM_VHEAP 0x600 ++#define T7000_PM_AUX_SIZE ALIGN(T7000_PM_S_COUNT * T7000_PM_MACROTILE + \ ++ T7000_PM_S_COUNT * T7000_PM_MLIST + T7000_PM_VHEAP, SZ_4K) ++#define T7000_PM_STACK_SIZE 0x18000 /* 0x300 blocks x 32 pages (PM object +0x28) */ ++#define T7000_PM_SCENE_S 1 ++/* In every VM, behind the general heap (t7000_heaps) */ ++#define T7000_PM_AUX_VA 0x1f0000000ull ++#define T7000_PM_STACK_VA 0x1f0400000ull ++ ++static u64 t7000_pm_mlist(unsigned int i) ++{ ++ return T7000_PM_AUX_VA + T7000_PM_S_COUNT * T7000_PM_MACROTILE + (u64)i * T7000_PM_MLIST; ++} ++ ++/* The VM's set at its slot's place in the root and shared memory */ ++static u32 t7000_pm_control_va(const struct s8003_vm *vm) ++{ ++ return T7000_PM_CONTROL + (vm->slot[0] - T7000_SLOT_FIRST) * T7000_PM_SET_SIZE; ++} ++ ++static u32 t7000_pm_list_va(const struct s8003_vm *vm) ++{ ++ return T7000_PM_LIST + (vm->slot[0] - T7000_SLOT_FIRST) * T7000_PM_SET_SIZE; ++} ++ ++static u32 t7000_pm_s(const struct s8003_vm *vm) ++{ ++ return s8003_meta_addr(S8003_ROOT_VA + t7000_pm_control_va(vm)) + 0x80 + ++ T7000_PM_SCENE_S * T7000_PM_S_SIZE; ++} ++ ++/* ++ * Build the VM's PM set for this start of the firmware (a8_pm_fill(): P5b's ++ * fresh queue, its control fields, the slices, S records with the VM's ++ * tables, the stack from index 0, slice 0 first). The usage word's flag ++ * (list +0x60) is 1: the host took the firmware's feedback. ++ */ ++static int t7000_pm_build(struct pvr_apple *apple, struct s8003_vm *vm) ++{ ++ struct pvr_apple_s8003 *s = apple->chip_data; ++ u8 *control = s->root.map + t7000_pm_control_va(vm); ++ u8 *list = s->shared.map + t7000_pm_list_va(vm); ++ u32 list_meta = s8003_meta_addr(S8003_SHARED_VA + t7000_pm_list_va(vm)); ++ u32 blocks = vm->t7000.blocks, pages = blocks * T7000_PM_SLICE_PAGES; ++ u8 *stack = vm->t7000.stack_map; ++ unsigned int i, j, k; ++ int err; ++ ++ if (!blocks || blocks > T7000_PM_BLOCKS_MAX) ++ return -EINVAL; ++ vm->t7000.built = 0; ++ memset(stack, 0, T7000_PM_STACK_SIZE); ++ /* The stack first: the pool's page numbers from index 0 (0x847fef0) */ ++ for (i = 0; i < pages; i++) { ++ dma_addr_t page; ++ ++ err = pvr_gem_get_dma_addr(vm->t7000.pool, i * SZ_4K, &page); ++ if (err) ++ return err; ++ put32(stack + 4 * i, page >> 12); ++ } ++ memset(vm->t7000.pool_map, 0, (size_t)pages * SZ_4K); ++ memset(vm->t7000.aux_map, 0, T7000_PM_AUX_SIZE); ++ memset(control, 0, T7000_PM_SET_SIZE); ++ memset(list, 0, T7000_PM_SET_SIZE); ++ for (j = 0; j < blocks; j++) { ++ u8 *slice = list + 0x80 + j * T7000_PM_SLICE; ++ ++ put32(slice, j ? list_meta + 0x80 + (j - 1) * T7000_PM_SLICE : 0); ++ for (k = 0; k < T7000_PM_SLICE_PAGES; k++) ++ put32(slice + 4 + 4 * k, get32(stack + 4 * (j * T7000_PM_SLICE_PAGES + k))); ++ } ++ put32(control + 0xc, 0x7f); ++ put32(control + 0x14, list_meta + 0x80 + (blocks - 1) * T7000_PM_SLICE); ++ put64(control + 0x18, T7000_PM_STACK_VA); ++ put32(control + 0x20, T7000_PM_STACK_SIZE); ++ put32(list + T7000_PM_FEEDBACK_TAKEN, 1); ++ put32(control + 0x24, pages); ++ put32(control + 0x28, pages - 1); ++ put32(control + 0x38, SZ_4K); ++ put32(control + 0x44, list_meta); ++ put32(control + 0x58, 1); ++ put32(control + 0x5c, 0x6000); ++ put32(control + 0x60, 0x2000); ++ for (i = 0; i < T7000_PM_S_COUNT; i++) { ++ u8 *rec = control + 0x80 + i * T7000_PM_S_SIZE; ++ ++ put64(rec + 0xa0, t7000_pm_mlist(i)); ++ put64(rec + 0xa8, T7000_PM_AUX_VA + i * T7000_PM_MACROTILE); ++ put64(rec + 0xb0, t7000_pm_mlist(T7000_PM_S_COUNT)); ++ put32(rec + 0xb8, list_meta + 0x40); ++ } ++ wmb(); ++ vm->t7000.built = s->gen; ++ return 0; ++} ++ ++static void t7000_pm_obj_put(struct pvr_apple *apple, struct pvr_gem_object **obj, u8 **map) ++{ ++ if (!*obj) ++ return; ++ if (*map) ++ pvr_gem_object_vunmap(*obj); ++ pvr_gem_object_put(*obj); ++ *obj = NULL; ++ *map = NULL; ++} ++ ++/* A VM's PM objects go with it; their VM mappings with the VM */ ++static void t7000_vm_put(struct pvr_apple *apple, struct s8003_vm *vm) ++{ ++ t7000_pm_obj_put(apple, &vm->t7000.aux, &vm->t7000.aux_map); ++ t7000_pm_obj_put(apple, &vm->t7000.stack, &vm->t7000.stack_map); ++ t7000_pm_obj_put(apple, &vm->t7000.pool, &vm->t7000.pool_map); ++ t7000_pm_obj_put(apple, &vm->t7000.next_pool, &vm->t7000.next_pool_map); ++} ++ ++static int t7000_pm_obj(struct pvr_apple *apple, size_t size, struct pvr_gem_object **obj, u8 **map) ++{ ++ *obj = pvr_gem_object_create(apple->pvr_dev, size, T7000_VM_FLAGS); ++ if (IS_ERR(*obj)) { ++ int err = PTR_ERR(*obj); ++ ++ *obj = NULL; ++ return err; ++ } ++ *map = pvr_gem_object_vmap(*obj); ++ if (IS_ERR(*map)) { ++ int err = PTR_ERR(*map); ++ ++ *map = NULL; ++ t7000_pm_obj_put(apple, obj, map); ++ return err; ++ } ++ return 0; ++} ++ ++/* ++ * Region headers as a mirrored ring (Apple's resource with flag 0x200000, ++ * mapped page by page at VA and VA + its length, 0x847d738): a buffer of ++ * R bytes mapped twice back to back, of 16 slices of the target's region ++ * headers rounded up to 4 KiB at least. Each frame takes the next slice; ++ * slices are never cleared. A dataset's ring: where it starts, R, the ++ * slice, and the dataset's cursor (Apple's RTM +0x19c), kept in the data set ++ * itself (&pvr_hwrt_dataset.apple_rgn) for as long as it exists. ++ */ ++ ++static int t7000_rgn_ring(struct pvr_vm_context *vm_ctx, u64 base, u32 slice, u64 *size) + { +- return -EOPNOTSUPP; ++ struct pvr_gem_object *obj, *mirror; ++ u64 offset, range, mirror_offset, mirror_range; ++ int err = -EINVAL; ++ ++ obj = pvr_vm_find_gem_object(vm_ctx, base, &offset, &range); ++ if (!obj) ++ return -EINVAL; ++ mirror = pvr_vm_find_gem_object(vm_ctx, base + range, &mirror_offset, &mirror_range); ++ if (mirror == obj && !offset && !mirror_offset && mirror_range == range && ++ range == pvr_gem_object_size(obj) && is_power_of_2(range) && ++ range >= roundup_pow_of_two(16 * slice)) { ++ *size = range; ++ err = 0; ++ } ++ if (mirror) ++ pvr_gem_object_put(mirror); ++ pvr_gem_object_put(obj); ++ return err; + } + ++/* ++ * Before a geometry job, in the submitting process: the target (one sample ++ * so far), its region-header ring, and the VM's PM objects, mapped in the VM ++ * once (the stack, the PM's tables) or kept unmapped (the pool). ++ */ + static int t7000_prepare_job(struct pvr_apple *apple, struct pvr_job *job) + { +- return job->type == DRM_PVR_JOB_TYPE_COMPUTE ? 0 : -EOPNOTSUPP; ++ struct pvr_apple_s8003 *s = apple->chip_data; ++ struct s8003_vm *vm; ++ struct pvr_hwrt_dataset *dataset; ++ struct s8003_rt rt; ++ u64 ring; ++ u32 slice, blocks; ++ int err; ++ ++ if (job->type != DRM_PVR_JOB_TYPE_GEOMETRY) ++ return 0; ++ vm = xa_load(&s->vms, (unsigned long)job->ctx->vm_ctx); ++ if (!vm) ++ return -EINVAL; ++ dataset = job->hwrt->hwrt_dataset; ++ err = s8003_rt_derive(dataset, &rt); ++ if (err) { ++ return err; ++ } ++ if (dataset->samples != 1) ++ return -EOPNOTSUPP; ++ slice = ALIGN(rt.header, SZ_4K); ++ err = t7000_rgn_ring(vm->vm_ctx, job->hwrt->data.rgn_header_dev_addr, slice, &ring); ++ if (err) { ++ dev_warn_ratelimited(apple_dev(apple), ++ "geometry job refused: region headers at %#llx are not a mirrored ring of 16 slices of %#x\n", ++ job->hwrt->data.rgn_header_dev_addr, slice); ++ return err; ++ } ++ ++ mutex_lock(&vm->prepare_lock); ++ if (!vm->t7000.stack) { ++ struct pvr_gem_object *aux = NULL, *stack = NULL, *pool = NULL; ++ u8 *aux_map = NULL, *stack_map = NULL, *pool_map = NULL; ++ ++ err = t7000_pm_obj(apple, T7000_PM_AUX_SIZE, &aux, &aux_map); ++ if (!err) ++ err = t7000_pm_obj(apple, T7000_PM_STACK_SIZE, &stack, &stack_map); ++ blocks = clamp(max(READ_ONCE(a8_pm_blocks), READ_ONCE(s->t7000_pm_floor)), 2u, ++ (u32)T7000_PM_BLOCKS_MAX); ++ if (!err) ++ err = t7000_pm_obj(apple, (size_t)blocks * T7000_PM_SLICE_PAGES * SZ_4K, ++ &pool, &pool_map); ++ if (!err) ++ err = pvr_vm_map_reserved(vm->vm_ctx, aux, T7000_PM_AUX_VA, T7000_PM_AUX_SIZE); ++ if (!err) { ++ err = pvr_vm_map_reserved(vm->vm_ctx, stack, T7000_PM_STACK_VA, ++ T7000_PM_STACK_SIZE); ++ if (err) ++ pvr_vm_unmap_obj(vm->vm_ctx, aux, T7000_PM_AUX_VA, T7000_PM_AUX_SIZE); ++ } ++ if (err) { ++ t7000_pm_obj_put(apple, &aux, &aux_map); ++ t7000_pm_obj_put(apple, &stack, &stack_map); ++ t7000_pm_obj_put(apple, &pool, &pool_map); ++ goto out; ++ } ++ mutex_lock(&apple->submit_lock); ++ vm->t7000.aux = aux; ++ vm->t7000.aux_map = aux_map; ++ vm->t7000.stack = stack; ++ vm->t7000.stack_map = stack_map; ++ vm->t7000.pool = pool; ++ vm->t7000.pool_map = pool_map; ++ vm->t7000.blocks = blocks; ++ vm->t7000.want = blocks; ++ mutex_unlock(&apple->submit_lock); ++ } ++ /* ++ * A larger pool the feedback or a stall asked for: allocated here, in ++ * the submitting process, and taken by a geometry job once the scenes ++ * on the old one are done (t7000_submit_geom()). ++ */ ++ mutex_lock(&apple->submit_lock); ++ blocks = max(vm->t7000.want, READ_ONCE(s->t7000_pm_floor)); ++ blocks = !READ_ONCE(a8_pm_grow) || vm->t7000.next_pool || blocks <= vm->t7000.blocks ? 0 : ++ min_t(u32, blocks, T7000_PM_BLOCKS_MAX); ++ mutex_unlock(&apple->submit_lock); ++ if (blocks) { ++ struct pvr_gem_object *pool = NULL; ++ u8 *pool_map = NULL; ++ ++ if (!t7000_pm_obj(apple, (size_t)blocks * T7000_PM_SLICE_PAGES * SZ_4K, &pool, ++ &pool_map)) { ++ mutex_lock(&apple->submit_lock); ++ if (!vm->t7000.next_pool) { ++ vm->t7000.next_pool = pool; ++ vm->t7000.next_pool_map = pool_map; ++ vm->t7000.next_blocks = blocks; ++ pool = NULL; ++ } ++ mutex_unlock(&apple->submit_lock); ++ t7000_pm_obj_put(apple, &pool, &pool_map); ++ } ++ } ++ /* ++ * The ring lives with the data set, which this job holds until it is ++ * freed: no other target's job can take it before this one is ++ * submitted. Its cursor starts at 0 with the data set. ++ */ ++ mutex_lock(&apple->submit_lock); ++ dataset->apple_rgn.ring = ring; ++ dataset->apple_rgn.slice = slice; ++ mutex_unlock(&apple->submit_lock); ++out: ++ mutex_unlock(&vm->prepare_lock); ++ return err; ++} ++ ++/* The A8's records */ ++#define T7000_TA_W_SIZE 0x568 ++#define T7000_3D_W_SIZE 0x8b0 ++#define T7000_TA_USEQ_SIZE 0xc0 ++#define T7000_3D_USEQ_SIZE 0x140 ++ ++/* ++ * A geometry job's record: the A9's fill moved to the A8's offsets (+8 from ++ * 0x20 and from 0x3a4), with Apple's A8 values where they differ from the ++ * A9's (the TA scratch base, the resume and TA PDS words and three scratch ++ * pointers are zero, +0x3c8 is 0x22000, +0x530 is 0), and the region ++ * headers' slice of this frame. The values are those of the record the ++ * first triangle drew with (Mesa's twiddled-triangle job, recorded on ++ * the iPod touch 6). ++ */ ++static void t7000_fill_ta(u8 *w, const struct pvr_apple_g5p_cmd_geom *cmd, ++ const struct pvr_hwrt_data *hwrt, const struct s8003_rt *rt, u64 rgn) ++{ ++ const struct pvr_apple_g5p_geom_regs *g = &cmd->g5p; ++ u64 tpc = hwrt->data.tail_ptrs_dev_addr; ++ unsigned int i; ++ ++ memset(w, 0, T7000_TA_W_SIZE); ++ put32(w + 0x28, 0x1e3ce508); ++ put32(w + 0x2c, 0x1e3ce508); ++ put64(w + 0x30, rgn); ++ put64(w + 0x38, tpc); ++ put64(w + 0x40, rt->ta_state); ++ put64(w + 0x48, g->sample_positions ?: rt->positions); ++ put64(w + 0x50, g->scratch_ptr[4]); ++ put64(w + 0x58, g->scratch_ptr[5]); ++ for (i = 0; i < 6; i++) { ++ put32(w + 0x60 + i * 4, g->context_pds[0][i]); ++ put32(w + 0x78 + i * 4, g->context_pds[1][i]); ++ } ++ put64(w + 0x90, g->ta_program); ++ put64(w + 0xa0, cmd->cmd.regs.vdm_ctrl_stream_base & ~3ull); ++ put32(w + 0x3ac, 0x00000088); ++ put32(w + 0x3b0, 0x00000002); ++ put64(w + 0x3b4, rt->ta_tiling[0]); ++ put64(w + 0x3bc, rt->ta_tiling[1]); ++ put32(w + 0x3c4, rt->ta_tiling_word); ++ put32(w + 0x3c8, 0x00022000); ++ put64(w + 0x44c, tpc); ++ put32(w + 0x454, rt->tpc); ++ put64(w + 0x470, g->scratch_ptr[0]); ++ put64(w + 0x490, g->scratch_ptr[4]); ++} ++ ++/* ++ * A fragment job's record, the same way: the A9's fill at the A8's offsets, ++ * without the scale words (zero in Apple's records), Apple's ISP and output ++ * words for one sample and an output width of 2 (+0x3c8..+0x3d4), and the ++ * render targets' PBE register words from the Rogue command (set A at ++ * +0x118 and +0x4c0, set B at +0x540, two qwords per target, as Apple's ++ * fillers 0x8475dbc and 0x844aeb0 write them). ++ */ ++static int t7000_fill_3d(u8 *w, const struct pvr_apple_g5p_cmd_frag *cmd, const struct s8003_rt *rt, ++ u64 rgn) ++{ ++ /* The register block's ZLS images and their copies further on (as recorded from the first triangle) */ ++ static const u16 zls[5][2] = { ++ { 0x80, 0x618 }, { 0x88, 0x628 }, { 0x90, 0x630 }, { 0x98, 0x648 }, { 0xa0, 0x650 }, ++ }; ++ const struct rogue_fwif_frag_regs *regs = &cmd->cmd.regs; ++ const struct pvr_apple_g5p_frag_regs *g = &cmd->g5p; ++ u64 positions = g->sample_positions ?: rt->positions; ++ unsigned int i; ++ ++ if ((g->pixel_output_width && g->pixel_output_width != 2) || g->flags) ++ return -EINVAL; ++ ++ memset(w, 0, T7000_3D_W_SIZE); ++ put64(w + 0x20, rgn); ++ put64(w + 0x28, rt->positions); ++ put32(w + 0x30, rt->state); ++ put32(w + 0x34, rt->padded); ++ put32(w + 0x38, rt->region); ++ put64(w + 0x60, positions); ++ put64(w + 0x68, regs->isp_scissor_base & ~3ull); ++ put64(w + 0x70, regs->isp_dbias_base & ~3ull); ++ put64(w + 0x78, regs->isp_oclqry_base); ++ for (i = 0; i < ARRAY_SIZE(zls); i++) { ++ put64(w + zls[i][0], g->zls[i]); ++ put64(w + zls[i][1], g->zls[i]); ++ } ++ put32(w + 0xa8, rt->region >> 2); ++ put32(w + 0xb0, g->background[0]); ++ put32(w + 0xb4, g->background[1]); ++ put64(w + 0xb8, g->background_texture); ++ for (i = 4; i < 8; i++) ++ put32(w + 0xc0 + (i - 4) * 4, g->background[i]); ++ for (i = 0; i < 8; i++) { ++ put64(w + 0x118 + i * 16, regs->pbe_word[i][0]); ++ put64(w + 0x120 + i * 16, regs->pbe_word[i][1]); ++ put64(w + 0x4c0 + i * 16, regs->pbe_word[i][0]); ++ put64(w + 0x4c8 + i * 16, regs->pbe_word[i][1]); ++ put64(w + 0x540 + i * 16, regs->pbe_word[i][0]); ++ put64(w + 0x548 + i * 16, regs->pbe_word[i][1]); ++ } ++ put32(w + 0x3a8, rt->padded); ++ put32(w + 0x3ac, regs->isp_bgobjdepth); ++ put32(w + 0x3b0, regs->isp_bgobjvals); ++ put32(w + 0x3b8, 0x0000001f); ++ /* The kext's upload has the halves of the first end-of-tile pair swapped */ ++ put32(w + 0x3bc, g->eot[1]); ++ put32(w + 0x3c0, g->eot[0]); ++ put32(w + 0x3c4, g->eot[2]); ++ put32(w + 0x3c8, 0x0001700f | BIT(17)); /* ISP_CTL; +PROCESS_EMPTY_TILES, as the A9 (0x0023000f) */ ++ put32(w + 0x3cc, 0x000001fc); ++ put32(w + 0x3d4, 0x00001200); ++ put64(w + 0x498, regs->isp_dbias_base); ++ put64(w + 0x4a8, regs->isp_scissor_base); ++ for (i = 0; i < 8; i++) ++ put32(w + 0x5c0 + i * 4, g->background[i]); ++ put64(w + 0x5e0, g->background_texture); ++ put32(w + 0x608, g->restore[0]); ++ put32(w + 0x60c, g->restore[1]); ++ for (i = 0; i < 3; i++) { ++ put32(w + 0x670 + i * 4, g->eot[i]); ++ put32(w + 0x684 + i * 4, g->eot_resume[i]); ++ } ++ put32(w + 0x698, regs->isp_bgobjdepth); ++ put32(w + 0x69c, regs->isp_bgobjvals); ++ put32(w + 0x6a0, 2); ++ put32(w + 0x858, 1); ++ return 0; ++} ++ ++/* What the 3D engine is waited for between a fragment job's start and finish (P5 ms_3d) */ ++static const u32 t7000_3d_wait[24] = { ++ 0x201, 0x138204, 0x10, 0xf, 0 /* the channel object + 0x724 */, 0x14, 0x109f, 0x104000a, ++ 0x1f, 0x1f, 0xf48008, 0x10000015, 4, 0, 0x14, 0x109f, 0x13000a, 0x4000, 0x4000, 0x138204, ++ 0x4000, 0x104000a, 8, 8, ++}; ++ ++/* ++ * The kernel's part of a render job (a8_ta_write()/a8_3d_write(), Apple's ++ * P5 form): the record's links (tag, slot, dependency object, PM set and ++ * state record, microsequence, stamps, the trace ID Metal passes per job) ++ * and the microsequence: start (TA 0x23, 3D 0x25 with 23 arguments), the ++ * timestamps around the wait, finish (0x24, 0x26). @k is the stamp's value ++ * before this job; the job's finish writes k + 1. ++ */ ++struct t7000_rjob { ++ u32 ch, w, useq, object, slot, pm_id, control, s, dep, seq, k, trace, times; ++ u32 pr_ch, pr_index; ++}; ++ ++static void t7000_ta_write(u8 *work, u8 *useq, const struct t7000_rjob *j) ++{ ++ const u32 w = j->w, g = j->object + 0x724; ++ const u32 mirror = j->ch + S8003_CH_STAMP, finish = mirror + 4; ++ const u32 words[] = { ++ 0x23, w + 0x3ac, w + 0x28, j->s, g, j->ch, j->slot, j->k, j->pm_id, 0, j->k, 0, ++ w + 0x46c, 0, 0, 0, j->seq, ++ 0x80000018, w + 0x558, w + 0x560, w + 0x560, j->seq, ++ 0x801, ++ 0x18, w + 0x558, w + 0x560, w + 0x564, j->seq, ++ 0x24, j->s, j->control, g, j->ch, j->slot, 0, w + 0x46c, 0, 0, j->seq, finish, ++ j->k + 1, (u32)-0x70, ++ 0x40000017, ++ }; ++ ++ BUILD_BUG_ON(sizeof(words) > T7000_TA_USEQ_SIZE); ++ put32(work + 0x4, j->slot); ++ put32(work + 0xc, j->dep); ++ put32(work + 0x10, j->pm_id); ++ put32(work + 0x14, j->control); ++ put32(work + 0x18, j->s); ++ put32(work + 0x45c, j->useq); ++ put32(work + 0x460, T7000_TA_USEQ_SIZE); ++ put32(work + 0x464, j->pr_ch); ++ put32(work + 0x468, j->pr_index); ++ put32(work + 0x51c, j->trace); ++ put32(work + 0x534, mirror); ++ put32(work + 0x538, finish); ++ put32(work + 0x53c, j->k + 1); ++ put32(work + 0x54c, j->seq); ++ put32(work + 0x550, j->k); ++ memset(useq, 0, T7000_TA_USEQ_SIZE); ++ useq_put(useq, words, ARRAY_SIZE(words)); ++} ++ ++static void t7000_3d_write(u8 *work, u8 *useq, const struct t7000_rjob *j) ++{ ++ const u32 w = j->w, g = j->object + 0x724; ++ const u32 mirror = j->ch + S8003_CH_STAMP, finish = mirror + 4; ++ const u32 start[] = { ++ 0x25, w + 0x3a8, w + 0x60, j->s, g, w + 0x7d8, w + 0x7dc, w + 0x860, j->ch, w, j->slot, ++ j->k, j->pm_id, 0, j->k, 0, w + 0x6c0, w + 0x894, 0, 0, 0, 0, 0, j->seq, ++ 0x80000018, w + 0x8a0, w + 0x8a8, w + 0x8a8, j->seq, ++ }; ++ const u32 finish_words[] = { ++ 0x18, w + 0x8a0, w + 0x8a8, w + 0x8ac, j->seq, ++ 0x26, j->seq, finish, j->k + 1, j->s, j->control, 1, g, w + 0x860, w + 0x7d8, j->ch, w, ++ j->slot, w + 0x6c0, 0, 0, 0, (u32)-0xe8, ++ 0x40000017, ++ }; ++ u32 wait[ARRAY_SIZE(t7000_3d_wait)]; ++ unsigned int i; ++ u8 *out; ++ ++ BUILD_BUG_ON(sizeof(start) + sizeof(wait) + sizeof(finish_words) > T7000_3D_USEQ_SIZE); ++ for (i = 0; i < ARRAY_SIZE(wait); i++) ++ wait[i] = i == 4 ? g : t7000_3d_wait[i]; ++ put32(work + 0x0, 1); ++ put32(work + 0x4, j->slot); ++ put32(work + 0xc, j->useq); ++ put32(work + 0x10, T7000_3D_USEQ_SIZE); ++ put32(work + 0x14, j->dep); ++ put32(work + 0x18, j->control); ++ put32(work + 0x1c, j->s); ++ put32(work + 0x850, j->trace); ++ put32(work + 0x870, mirror); ++ put32(work + 0x874, finish); ++ put32(work + 0x878, j->k + 1); ++ put32(work + 0x888, j->seq); ++ put32(work + 0x88c, j->k); ++ put32(work + 0x8ac, j->times); ++ memset(useq, 0, T7000_3D_USEQ_SIZE); ++ out = useq_put(useq, start, ARRAY_SIZE(start)); ++ out = useq_put(out, wait, ARRAY_SIZE(wait)); ++ useq_put(out, finish_words, ARRAY_SIZE(finish_words)); ++} ++ ++/* ++ * A geometry job: one record at a time on the TA channel (one pointer-ring ++ * entry, its record; no PM command on the A8), on the VM's PM set, built ++ * for this start of the firmware or again after a scene whose fragment ++ * job never came, and S[1] for its scene as Apple's first frame. Its ++ * partial-render index names the 3D ring entry of the frame's fragment ++ * record. ++ */ ++/* ++ * The firmware's usage report at list +0x40 after a scene (high-water ++ * pages, margin, -, request in blocks; +0x60 is 1 once taken, and the ++ * firmware clears both when it starts a scene, 10008150), read as the A9 ++ * does (s8003_pm_feedback(): Apple's growPoolFromFirmwareFeedback): the ++ * pool should hold three times the high-water mark or the margin plus a ++ * block, else grow by the request. Memory only; submit_lock held. ++ */ ++static void t7000_pm_feedback(struct pvr_apple *apple, struct s8003_vm *vm) ++{ ++ struct pvr_apple_s8003 *s = apple->chip_data; ++ u8 *list = s->shared.map + t7000_pm_list_va(vm); ++ u32 high, margin, request, target, blocks = vm->t7000.blocks; ++ ++ lockdep_assert_held(&apple->submit_lock); ++ if (get32(list + T7000_PM_FEEDBACK_TAKEN) == 1) ++ return; ++ rmb(); /* the flag before the words it covers */ ++ high = get32(list + T7000_PM_FEEDBACK); ++ margin = get32(list + T7000_PM_FEEDBACK + 4); ++ request = min(get32(list + T7000_PM_FEEDBACK + 12), (u32)S8003_PM_GROW_MAX); ++ if (!READ_ONCE(a8_pm_grow)) { ++ /* read only, at most once a second a VM: what the A8 reports */ ++ if (!vm->fb_at || ktime_ms_delta(ktime_get(), vm->fb_at) >= 1000) { ++ vm->fb_at = ktime_get(); ++ dev_info(apple_dev(apple), ++ "A8 PM usage (pool %u blocks): %u %u %u %u\n", blocks, high, ++ margin, get32(list + T7000_PM_FEEDBACK + 8), ++ get32(list + T7000_PM_FEEDBACK + 12)); ++ } ++ return; ++ } ++ /* as the A9: at most every S8003_PM_FEEDBACK_MS */ ++ if (vm->fb_at && ktime_ms_delta(ktime_get(), vm->fb_at) < S8003_PM_FEEDBACK_MS) ++ return; ++ vm->fb_at = ktime_get(); ++ put32(list + T7000_PM_FEEDBACK_TAKEN, 1); ++ wmb(); ++ /* Not from this pool: ignored */ ++ if (high > blocks * T7000_PM_SLICE_PAGES || ++ margin > T7000_PM_BLOCKS_MAX * T7000_PM_SLICE_PAGES) ++ return; ++ target = DIV_ROUND_UP(max(3 * high, T7000_PM_SLICE_PAGES + margin), T7000_PM_SLICE_PAGES); ++ if (target <= blocks && request) ++ target = blocks + request; ++ target = min_t(u32, target, T7000_PM_BLOCKS_MAX); ++ if (target > vm->t7000.want) { ++ dev_dbg(apple_dev(apple), ++ "A8 PM pool feedback: high-water %u pages, margin %u, request %u: %u -> %u blocks\n", ++ high, margin, request, blocks, target); ++ vm->t7000.want = target; ++ } ++} ++ ++static int t7000_submit_geom(struct pvr_apple *apple, struct s8003_vm *vm, struct pvr_job *job) ++{ ++ struct pvr_hwrt_dataset *dataset = job->hwrt->hwrt_dataset; ++ struct pvr_apple_s8003 *s = apple->chip_data; ++ u32 addr = s8003_channel_addr(s, APPLE_ENGINE_GEOM); ++ u8 *p = s8003_channel(s, APPLE_ENGINE_GEOM); ++ struct t7000_rjob j; ++ struct s8003_rt rt; ++ u64 slice_addr; ++ u32 seq; ++ int err; ++ ++ return s8003_submit_error(apple, vm, "geometry", -EINVAL, "not prepared"); ++ if (!dataset->apple_rgn.ring || !vm->t7000.stack) ++ err = s8003_rt_derive(dataset, &rt); ++ if (err) ++ return s8003_submit_error(apple, vm, "geometry", err, "rt derive"); ++ /* ++ * Every earlier scene is done here (a geometry job waits for the last ++ * fragment job, pvr_apple_job_add_deps()): the pool is the firmware's ++ * no more. Take the last scene's feedback, then a larger pool if one ++ * is ready; the set is built anew on it. ++ */ ++ if (vm->t7000.built == s->gen && !vm->t7000.open) ++ t7000_pm_feedback(apple, vm); ++ if (vm->t7000.next_pool && !vm->t7000.open) { ++ dev_info(apple_dev(apple), "A8 PM pool: %u -> %u blocks (%u KiB)\n", vm->t7000.blocks, ++ vm->t7000.next_blocks, vm->t7000.next_blocks * 128); ++ t7000_pm_obj_put(apple, &vm->t7000.pool, &vm->t7000.pool_map); ++ vm->t7000.pool = vm->t7000.next_pool; ++ vm->t7000.pool_map = vm->t7000.next_pool_map; ++ vm->t7000.blocks = vm->t7000.next_blocks; ++ vm->t7000.next_pool = NULL; ++ vm->t7000.next_pool_map = NULL; ++ vm->t7000.built = 0; ++ } ++ s->t7000_geom_vm = vm; ++ if (vm->t7000.built != s->gen || vm->t7000.open) { ++ err = t7000_pm_build(apple, vm); ++ if (err) ++ return s8003_submit_error(apple, vm, "geometry", err, "PM set build"); ++ } ++ err = s8003_wait_done(apple, APPLE_ENGINE_GEOM, apple->ta_seq); ++ if (err) ++ return s8003_submit_error(apple, vm, "geometry", err, "record slot wait"); ++ ++ seq = apple->ta_seq + 1; ++ slice_addr = job->hwrt->data.rgn_header_dev_addr + ++ (dataset->apple_rgn.cursor & (dataset->apple_rgn.ring - 1)); ++ t7000_fill_ta(p + T7000_RCH_W, job->cmd, job->hwrt, &rt, slice_addr); ++ j = (struct t7000_rjob) { ++ .ch = addr, .w = addr + T7000_RCH_W, .useq = addr + T7000_RCH_TA_USEQ, ++ .object = s8003_object_addr(&s->layout, S8003_CHANNEL), .slot = vm->slot[0], ++ .pm_id = 0, .control = s8003_meta_addr(S8003_ROOT_VA + t7000_pm_control_va(vm)), ++ .s = t7000_pm_s(vm), .dep = addr + S8003_CH_DEP, .seq = seq, .k = seq - 1, ++ .trace = ++s->t7000_trace, .pr_ch = s8003_channel_addr(s, APPLE_ENGINE_FRAG), ++ .pr_index = (2 * (apple->frag_seq + 1) - 1) % T7000_RCH_ENTRIES, ++ }; ++ t7000_ta_write(p + T7000_RCH_W, p + T7000_RCH_TA_USEQ, &j); ++ put32(p + T7000_RCH_RING + ((seq - 1) % T7000_RCH_ENTRIES) * 4, j.w); ++ ++ dataset->apple_rgn.cursor += dataset->apple_rgn.slice; ++ vm->t7000.open = true; ++ vm->t7000.hwrt = job->hwrt; ++ vm->t7000.ta_seq = seq; ++ vm->t7000.rgn_addr = slice_addr; ++ apple->ta_seq = seq; ++ err = t7000_publish(apple, APPLE_ENGINE_GEOM, seq % T7000_RCH_ENTRIES); ++ if (err) ++ return s8003_submit_error(apple, vm, "geometry", err, "publish"); ++ return seq; ++} ++ ++/* ++ * A fragment job: its scene's geometry job was submitted before it. Two ++ * pointer-ring entries, the barrier command {4, the TA's finish stamp, the ++ * value it reaches, this job's number} and the record (P5b), the scene's ++ * region-header slice, S[1] of the set. ++ */ ++static int t7000_submit_frag(struct pvr_apple *apple, struct s8003_vm *vm, struct pvr_job *job) ++{ ++ struct pvr_apple_s8003 *s = apple->chip_data; ++ u32 addr = s8003_channel_addr(s, APPLE_ENGINE_FRAG); ++ u32 ta_addr = s8003_channel_addr(s, APPLE_ENGINE_GEOM); ++ u8 *p = s8003_channel(s, APPLE_ENGINE_FRAG), *cmd = p + T7000_RCH_CMD; ++ struct t7000_rjob j; ++ struct s8003_rt rt; ++ u32 seq, ring; ++ int err; ++ ++ if (!vm->t7000.open || vm->t7000.hwrt != job->hwrt || vm->t7000.built != s->gen) ++ return s8003_submit_error(apple, vm, "fragment", -ECANCELED, ++ "its geometry job failed or was not submitted"); ++ err = s8003_rt_derive(job->hwrt->hwrt_dataset, &rt); ++ if (err) ++ return s8003_submit_error(apple, vm, "fragment", err, "rt derive"); ++ err = s8003_wait_done(apple, APPLE_ENGINE_FRAG, apple->frag_seq); ++ if (err) ++ return s8003_submit_error(apple, vm, "fragment", err, "record slot wait"); ++ ++ seq = apple->frag_seq + 1; ++ err = t7000_fill_3d(p + T7000_RCH_W, job->cmd, &rt, vm->t7000.rgn_addr); ++ if (err) ++ return s8003_submit_error(apple, vm, "fragment", err, "3D record fill"); ++ memset(p + T7000_RCH_TIMES, 0, 16); ++ j = (struct t7000_rjob) { ++ .ch = addr, .w = addr + T7000_RCH_W, .useq = addr + T7000_RCH_3D_USEQ, ++ .object = s8003_object_addr(&s->layout, S8003_CHANNEL + 1), .slot = vm->slot[0], ++ .pm_id = 0, .control = s8003_meta_addr(S8003_ROOT_VA + t7000_pm_control_va(vm)), ++ .s = t7000_pm_s(vm), .dep = ta_addr + S8003_CH_DEP, .seq = seq, .k = seq - 1, ++ .trace = ++s->t7000_trace, .times = addr + T7000_RCH_TIMES, ++ }; ++ t7000_3d_write(p + T7000_RCH_W, p + T7000_RCH_3D_USEQ, &j); ++ memset(cmd, 0, S8003_CMD_SIZE); ++ put32(cmd, S8003_CMD_WAIT); ++ put32(cmd + 4, ta_addr + S8003_CH_STAMP + 4); ++ put32(cmd + 8, vm->t7000.ta_seq); ++ put32(cmd + 12, seq); ++ ring = (2 * (seq - 1)) % T7000_RCH_ENTRIES; ++ put32(p + T7000_RCH_RING + ring * 4, addr + T7000_RCH_CMD); ++ put32(p + T7000_RCH_RING + ((ring + 1) % T7000_RCH_ENTRIES) * 4, j.w); ++ ++ vm->t7000.open = false; ++ apple->frag_seq = seq; ++ err = t7000_publish(apple, APPLE_ENGINE_FRAG, (2 * seq) % T7000_RCH_ENTRIES); ++ if (err) ++ return s8003_submit_error(apple, vm, "fragment", err, "publish"); ++ return seq; + } + + static void t7000_report_hang(struct pvr_apple *apple) +@@ -4311,6 +5071,7 @@ static void t7000_report_hang(struct pvr_apple *apple) + "posted no fault"); + } + ++ + static void t7000_iounmap(void *addr) + { + iounmap((void __iomem *)addr); +@@ -4449,8 +5210,8 @@ static const struct s8003_variant s8003_variant_a8 = { + .vm_unmap = t7000_vm_unmap, + .engine_done = t7000_engine_done, + .prepare_job = t7000_prepare_job, +- .submit_geom = t7000_submit_render, +- .submit_frag = t7000_submit_render, ++ .submit_geom = t7000_submit_geom, ++ .submit_frag = t7000_submit_frag, + .submit_compute = t7000_submit_compute, + .report_hang = t7000_report_hang, + }; +diff --git a/drivers/gpu/drm/imagination/pvr_hwrt.h b/drivers/gpu/drm/imagination/pvr_hwrt.h +index 2914275b0a..ceb595a32c 100644 +--- a/drivers/gpu/drm/imagination/pvr_hwrt.h ++++ b/drivers/gpu/drm/imagination/pvr_hwrt.h +@@ -110,6 +110,24 @@ struct pvr_hwrt_dataset { + * @vm_ctx while this data set exists. + */ + struct pvr_vm_pin pins[ROGUE_FWIF_NUM_RTDATAS + ROGUE_FWIF_NUM_RTDATA_FREELISTS]; ++ ++ /** ++ * @apple_rgn: Region-header ring of this target on a firmware that ++ * gives each frame its own slice of it (Apple A8), like the cursor in ++ * Apple's per-render-target RTM structure. It lives as long as the ++ * data set, so a queued job always finds it. Zero until the first ++ * geometry job on the data set is prepared; under the chip's lock. ++ */ ++ struct { ++ /** @apple_rgn.ring: Size of the mirrored ring in bytes; 0: not prepared. */ ++ u64 ring; ++ ++ /** @apple_rgn.cursor: Offset of the next frame's slice, unmasked. */ ++ u64 cursor; ++ ++ /** @apple_rgn.slice: Bytes of one frame's region headers. */ ++ u32 slice; ++ } apple_rgn; + }; + + struct drm_gem_object *pvr_hwrt_data_resv_obj(struct pvr_hwrt_data *hwrt_data); diff --git a/kernel/patches/drivers/gpu/0070-drm-imagination-apple-Report-the-A8-s-faults-and-han.patch b/kernel/patches/drivers/gpu/0070-drm-imagination-apple-Report-the-A8-s-faults-and-han.patch new file mode 100644 index 00000000..ca02208a --- /dev/null +++ b/kernel/patches/drivers/gpu/0070-drm-imagination-apple-Report-the-A8-s-faults-and-han.patch @@ -0,0 +1,522 @@ +From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 +From: =?UTF-8?q?Berkay=20K=C3=B6sebay?= +Date: Sat, 21 Nov 2026 12:08:00 +0100 +Subject: [PATCH] drm/imagination: apple: Report the A8's faults and hangs and + recover from them + +On the A8 a fault in a job's context can leave the firmware silent: a +write the MCU made through a job's context to an unmapped page left +EVENT_STATUS bit 9 (MMU page fault) latched for 1.5 s with no event and +no watchdog. A firmware fault event printed its raw bytes and then "GPU +MMU fault licence refused: no licence for this chip", as the A10's +licence check has no A8 counterpart, so the fault registers were never +read at the event. And a fault reset went through the wrong job: + +- At a job timeout or a firmware fault, report each engine's submitted + job, stamps and work ring from memory, then read EVENT_STATUS once, + gated as every access to the A8's Rogue registers (Fender state 1, + PLL locked), and only with bit 9 set the BIF's two fault banks once + each: context, address, read or write, tag and sideband. Once per + firmware start, never in a loop: a tight poll of these registers + during a job froze the whole SoC, while single reads after an event + and at a timeout are safe. The timeout report skips the registers + only when the fault event's read happened. +- A chip may capture the fault itself (&pvr_apple_chip.report_fault) + in place of the licence-checked bank read; only the T7000 sets it. +- A type-4 event (the firmware's lockup recovery) is decoded from + memory: the arm (+8: 0 the DM handler's MMU-fault arm, which took + bit 9 itself, so the banks are read on its word; 1 the TA/3D + watchdog), the +0x24 word one byte per group (TA, 3D, CL) with the + locked one named, object 0x4b raw, and each engine whose submitted and + done counts differ. +- A hang reports the parameter manager's state from memory (the TA + object's out-of-memory and watchdog words, the last geometry VM's PM + control, usage words and stack top), then the PM's live top index once + through the gate. A stall for pages doubles the pool of later VMs. +- A firmware fault reset through the oldest pending job of any engine, + including one its engine had finished but the poll had not retired + yet: on a texture test the geometry job was done and the fragment job + hung, the fault went to the geometry scheduler, found no job there, + and the fragment job waited for its 5 s timeout. A chip with + &pvr_apple_chip.fault_skips_done (the T7000) takes each engine's first + unfinished job instead. +- pvr_apple_wait_idle() waited up to its timeout per engine, so a + closing client and the VM it frees each held submit_lock for up to + 3 x 5 s. A chip with &pvr_apple_chip.wait_deadline waits once, with + one deadline for all engines, and a wait that gives up names the + engine, its counts and the fault flag, from memory only. A submit + refused before the chip's own path (firmware not running, VM unknown, + every VM slot taken) says why, and the A8's geometry preparation takes + its VM's lock killably, as the holder may wait on the GPU. + +The A9 and A10 take the unchanged paths: the new hooks and flags are set +only in the T7000 table. + +Assisted-by: LLM +--- + drivers/gpu/drm/imagination/pvr_apple.c | 56 ++++ + drivers/gpu/drm/imagination/pvr_apple_chip.h | 12 + + drivers/gpu/drm/imagination/pvr_apple_s8003.c | 260 +++++++++++++++++- + 3 files changed, 318 insertions(+), 10 deletions(-) + +diff --git a/drivers/gpu/drm/imagination/pvr_apple.c b/drivers/gpu/drm/imagination/pvr_apple.c +index 98de5178a3..02cfd5698b 100644 +--- a/drivers/gpu/drm/imagination/pvr_apple.c ++++ b/drivers/gpu/drm/imagination/pvr_apple.c +@@ -759,12 +759,50 @@ static int pvr_apple_wait_engine(struct pvr_apple *apple, enum pvr_apple_engine + return !err && (s32)(done - seq) < 0 ? -EIO : err; + } + ++/* ++ * pvr_apple_wait_idle() for a chip with &pvr_apple_chip.wait_deadline: one ++ * deadline for all engines, so a closing client or a VM that goes waits at ++ * most @timeout_us under submit_lock, not one timeout per engine. A wait ++ * that gives up names the engine, from memory only: the driver's counts ++ * and the stamps the firmware wrote. No Fender, PMGR or Rogue register is ++ * read, as after every job. ++ */ ++static int pvr_apple_wait_idle_deadline(struct pvr_apple *apple, unsigned int timeout_us) ++{ ++ static const char *const names[APPLE_ENGINES] = { ++ [APPLE_ENGINE_GEOM] = "geometry", ++ [APPLE_ENGINE_FRAG] = "fragment", ++ [APPLE_ENGINE_COMPUTE] = "compute", ++ }; ++ ktime_t deadline = ktime_add_us(ktime_get(), timeout_us); ++ int engine, err = 0; ++ ++ for (engine = 0; engine < APPLE_ENGINES; engine++) { ++ s64 left = ktime_us_delta(deadline, ktime_get()); ++ ++ /* Never 0: read_poll_timeout() takes 0 as no timeout at all */ ++ err = pvr_apple_wait_engine(apple, engine, pvr_apple_engine_last(apple, engine), ++ max_t(s64, min_t(s64, left, timeout_us), 1)); ++ if (err) ++ break; ++ } ++ if (err) ++ dev_err_ratelimited(apple_dev(apple), ++ "GPU not idle within %u us: %s submitted %u done %u%s (%d)\n", ++ timeout_us, names[engine], pvr_apple_engine_last(apple, engine), ++ pvr_apple_engine_done(apple, engine), ++ READ_ONCE(apple->fault) ? ", firmware fault" : "", err); ++ return err; ++} ++ + /* Wait until every engine retired its submitted jobs; submit_lock held */ + int pvr_apple_wait_idle(struct pvr_apple *apple, unsigned int timeout_us) + { + int engine, err = 0; + + lockdep_assert_held(&apple->submit_lock); ++ if (apple->chip->wait_deadline) ++ return pvr_apple_wait_idle_deadline(apple, timeout_us); + for (engine = 0; engine < APPLE_ENGINES && !err; engine++) + err = pvr_apple_wait_engine(apple, engine, pvr_apple_engine_last(apple, engine), + timeout_us); +@@ -961,6 +999,10 @@ void pvr_apple_report_mmu_fault(struct pvr_apple *apple) + + if (xchg(&apple->fault_reported, true)) + return; ++ if (apple->chip->report_fault) { ++ apple->chip->report_fault(apple); ++ return; ++ } + if (!apple->chip->max_power_state) { + dev_warn(apple_dev(apple), "GPU MMU fault licence refused: no licence for this chip\n"); + return; +@@ -1029,6 +1071,20 @@ static void pvr_apple_fault_pending(struct pvr_apple *apple) + for (engine = 0; engine < APPLE_ENGINES; engine++) { + fence = list_first_entry_or_null(&apple->pending[engine], + struct pvr_apple_fence, node); ++ /* ++ * A job its engine finished but the poll has not retired yet ++ * is not the one the firmware faulted on: faulting its ++ * scheduler found no job left, and the faulted one waited for ++ * its timeout (a texture test: geometry done, fragment ++ * hung 5 s). Take each engine's first unfinished job. ++ */ ++ if (apple->chip->fault_skips_done) { ++ u32 done = pvr_apple_engine_done(apple, engine); ++ ++ while (fence && (s32)(done - (u32)fence->base.seqno) >= 0) ++ fence = list_is_last(&fence->node, &apple->pending[engine]) ? NULL : ++ list_next_entry(fence, node); ++ } + if (!fence || !fence->sched) + continue; + if (!oldest || ktime_before(fence->submitted, oldest->submitted)) { +diff --git a/drivers/gpu/drm/imagination/pvr_apple_chip.h b/drivers/gpu/drm/imagination/pvr_apple_chip.h +index 655fa5b375..4e2d9a4870 100644 +--- a/drivers/gpu/drm/imagination/pvr_apple_chip.h ++++ b/drivers/gpu/drm/imagination/pvr_apple_chip.h +@@ -242,12 +242,21 @@ struct pvr_apple { + * free list has none (pvr_apple_free_list_has_pages()). + * @gate_off_ms: How long the GPU's power domain stays off before the firmware + * is started again, and that a reset takes the domain off as well. ++ * @wait_deadline: A wait for the GPU to go idle (pvr_apple_wait_idle()) has ++ * one deadline for all engines, not one per engine, and a wait that ++ * gives up says so, from memory only. A client that closes, or a VM ++ * that goes, then waits at most that one timeout under submit_lock. A ++ * job refused before the chip's own submit path says why. ++ * @fault_skips_done: A firmware fault resets through the oldest job its ++ * engine has not finished, not one finished but not yet retired. + * @submit: Hand @job to the firmware. Takes &pvr_apple.submit_lock itself. + * Return: the job's sequence number on its engine, which @engine_done + * reaches when it is finished, or a negative error. + * @engine_done: The sequence number of the last finished job of @engine. No + * lock held; may be called with interrupts off. + * @max_power_state: Highest state licensed for fault capture; optional. ++ * @report_fault: Capture the MMU fault state after a firmware fault, in place ++ * of the licence-checked bank read (pvr_apple_report_mmu_fault()); optional. + * @events: Read what the firmware posted. Before @engine_done wherever a job + * is waited for; may sleep. + * @vm_init: A userspace VM was created: map what the firmware needs in it. +@@ -280,6 +289,8 @@ struct pvr_apple_chip { + unsigned int scenes; + bool own_pm_pages; + unsigned int gate_off_ms; ++ bool wait_deadline; ++ bool fault_skips_done; + /* With the firmware's interrupt taken: microseconds between checks while jobs are pending */ + unsigned int irq_poll_us; + const struct drm_pvr_heap *heaps; +@@ -299,6 +310,7 @@ struct pvr_apple_chip { + void (*write_geom)(struct pvr_apple *apple, u32 seq, const void *hw); + void (*write_frag)(struct pvr_apple *apple, u32 seq, const void *hw); + void (*report_hang)(struct pvr_apple *apple); ++ void (*report_fault)(struct pvr_apple *apple); + + int (*prepare_job)(struct pvr_apple *apple, struct pvr_job *job); + int (*submit)(struct pvr_apple *apple, struct pvr_job *job); +diff --git a/drivers/gpu/drm/imagination/pvr_apple_s8003.c b/drivers/gpu/drm/imagination/pvr_apple_s8003.c +index db0bf67245..f6a74fad8f 100644 +--- a/drivers/gpu/drm/imagination/pvr_apple_s8003.c ++++ b/drivers/gpu/drm/imagination/pvr_apple_s8003.c +@@ -276,6 +276,8 @@ struct s8003_variant { + int (*submit_frag)(struct pvr_apple *apple, struct s8003_vm *vm, struct pvr_job *job); + int (*submit_compute)(struct pvr_apple *apple, struct s8003_vm *vm, struct pvr_job *job); + void (*report_hang)(struct pvr_apple *apple); ++ /* A fault event @e (S8003_EVENT_SIZE bytes) was posted: say what it is; optional */ ++ void (*event_fault)(struct pvr_apple *apple, const u8 *e); + }; + + struct s8003_mem { +@@ -426,7 +428,9 @@ struct pvr_apple_s8003 { + /* The A8: the smallest PM pool a stall taught; the VM of the last geometry job */ + u32 t7000_pm_floor; + struct s8003_vm *t7000_geom_vm; ++ bool t7000_mmu_arm; /* a lockup event came from the MMU-fault arm */ + ++ bool bif_read; /* t7000_report_fault() read the fault registers */ + }; + + static void put32(u8 *p, u32 value) +@@ -1103,6 +1107,8 @@ static int s8003_drain(struct pvr_apple *apple, bool *alive) + dev_warn_ratelimited(apple_dev(apple), "firmware fault event type=%u slot=%u: %*ph\n", + type, get_unaligned_le32(e + 0x0c), + S8003_EVENT_SIZE - 4, e + 4); ++ if (s->v->event_fault) ++ s->v->event_fault(apple, e); + pvr_apple_firmware_fault(apple); + } + consumer = (consumer + 1) % APPLE_RING_ENTRIES; +@@ -2805,8 +2811,16 @@ static int pvr_apple_s8003_submit(struct pvr_apple *apple, struct pvr_job *job) + err = -ENODEV; + else + err = s8003_vm_attach(apple, vm); +- if (err) ++ if (err) { ++ /* the chip's own refusals say why below; these said nothing */ ++ if (apple->chip->wait_deadline) ++ dev_warn_ratelimited(apple_dev(apple), "job not submitted: %s (%d)\n", ++ !apple->booted ? "the firmware is not running" : ++ !vm ? "its VM is not known to the chip" : ++ err == -EBUSY ? "every VM slot is taken" : ++ "its VM did not attach", err); + goto out_unlock; ++ } + if (job->type == DRM_PVR_JOB_TYPE_GEOMETRY) + err = s->v->submit_geom(apple, vm, job); + else if (job->type == DRM_PVR_JOB_TYPE_FRAGMENT) +@@ -4560,7 +4574,14 @@ static int t7000_prepare_job(struct pvr_apple *apple, struct pvr_job *job) + return err; + } + +- mutex_lock(&vm->prepare_lock); ++ /* ++ * The submitting client's own context: a holder may wait on the GPU ++ * (up to the job timeout under submit_lock), so a killed client ++ * leaves instead of sleeping uninterruptibly behind it. ++ */ ++ err = mutex_lock_killable(&vm->prepare_lock); ++ if (err) ++ return err; + if (!vm->t7000.stack) { + struct pvr_gem_object *aux = NULL, *stack = NULL, *pool = NULL; + u8 *aux_map = NULL, *stack_map = NULL, *pool_map = NULL; +@@ -5057,20 +5078,235 @@ static int t7000_submit_frag(struct pvr_apple *apple, struct s8003_vm *vm, struc + return seq; + } + +-static void t7000_report_hang(struct pvr_apple *apple) ++/* ++ * A fault in a job's context can leave the A8's firmware silent with ++ * EVENT_STATUS bit 9 (MMU page fault) latched: no event and no watchdog. So ++ * after the firmware's state, read EVENT_STATUS once, gated as every access ++ * to the Rogue registers, and only with bit 9 set the BIF's two fault banks ++ * once each: the faulting address, read or write, the requester's tag and ++ * sideband, and the context (CAT_BASE: 0 the firmware's, a slot's, or a ++ * PM's). The firmware's fault arm reads the same banks (0x10022c98). Never ++ * in a loop: a tight poll of these registers during a job froze the whole ++ * SoC. @why prefixes the lines. ++ * ++ * Return: whether EVENT_STATUS was read (the gate let it). ++ */ ++static bool t7000_report_bif(struct pvr_apple *apple, const char *why) ++{ ++ struct pvr_apple_s8003 *s = apple->chip_data; ++ struct pvr_device *pvr_dev = apple->pvr_dev; ++ u32 status, bank; ++ bool fw_fault; ++ ++ if (s->stuck || !t7000_r0_gate(apple)) ++ return false; ++ status = pvr_cr_read32(pvr_dev, ROGUE_CR_EVENT_STATUS); ++ /* ++ * A lockup event from the firmware's MMU-fault arm (+8 = 0): the ++ * firmware took bit 9 off already (10005138), so the banks are read ++ * on its word, once, as the bit would have had them read. ++ */ ++ fw_fault = xchg(&s->t7000_mmu_arm, false); ++ dev_err(apple_dev(apple), "%s: EVENT_STATUS %#x%s\n", why, status, ++ status & ROGUE_CR_EVENT_STATUS_MMU_PAGE_FAULT_EN ? " (MMU page fault)" : ++ fw_fault ? " (bit 9 taken by the firmware's MMU-fault arm)" : ++ " (no MMU page fault: BIF banks not read)"); ++ if (!(status & ROGUE_CR_EVENT_STATUS_MMU_PAGE_FAULT_EN || fw_fault) || ++ !t7000_r0_gate(apple)) ++ return true; ++ for (bank = 0; bank < 2; bank++) { ++ u32 reg = bank ? ROGUE_CR_BIF_FAULT_BANK1_MMU_STATUS : ROGUE_CR_BIF_FAULT_BANK0_MMU_STATUS; ++ u32 req_reg = bank ? ROGUE_CR_BIF_FAULT_BANK1_REQ_STATUS : ++ ROGUE_CR_BIF_FAULT_BANK0_REQ_STATUS; ++ u64 mmu = pvr_cr_read32(pvr_dev, reg) | (u64)pvr_cr_read32(pvr_dev, reg + 4) << 32; ++ u64 req = pvr_cr_read32(pvr_dev, req_reg) | ++ (u64)pvr_cr_read32(pvr_dev, req_reg + 4) << 32; ++ ++ dev_err(apple_dev(apple), ++ "GPU MMU fault bank %u: %s, context %llu, VA %#llx %s, tag %llu, sideband %#llx (mmu %#llx req %#llx)\n", ++ bank, mmu & ROGUE_CR_BIF_FAULT_BANK0_MMU_STATUS_FAULT_EN ? "fault" : "no fault", ++ (mmu >> ROGUE_CR_BIF_FAULT_BANK0_MMU_STATUS_CAT_BASE_SHIFT) & 0xf, ++ req & ~ROGUE_CR_BIF_FAULT_BANK0_REQ_STATUS_ADDRESS_CLRMSK, ++ req & ROGUE_CR_BIF_FAULT_BANK0_REQ_STATUS_RNW_EN ? "read" : "write", ++ (req >> ROGUE_CR_BIF_FAULT_BANK0_REQ_STATUS_TAG_ID_SHIFT) & 0xf, ++ (req >> ROGUE_CR_BIF_FAULT_BANK0_REQ_STATUS_TAG_SB_SHIFT) & 0x3f, mmu, req); ++ } ++ return true; ++} ++ ++/* ++ * The firmware stopped on a fault (a fault event, or a job's stamp that will ++ * not come): the gated registers once, at the event, as iOS reads the banks ++ * before it resets the firmware. The A10's licence check (the epoch's power ++ * states) has no A8 counterpart; Fender state 1 and the PLL lock are the ++ * A8's gate. pvr_apple_report_mmu_fault() calls this once per firmware start. ++ */ ++static void t7000_report_fault(struct pvr_apple *apple) ++{ ++ struct pvr_apple_s8003 *s = apple->chip_data; ++ ++ WRITE_ONCE(s->bif_read, t7000_report_bif(apple, "fault")); ++} ++ ++/* ++ * A type-4 event: the firmware's lockup recovery. The lockup checker ++ * 1000f538 recovers once every active engine has its lockup flag; 100290a4 ++ * builds the event on its stack with 1000f800: ++ * +0 type 4, +0xc and +0x18 from 10002ed0, +0x10 and +0x14 zero, +0x1c the ++ * checker's argument, and +0x20/+0x24 from 10017c54. +0x28 is not written by ++ * the builder: what the stack held (0x1001b5a8 and 0x1001afd8 were seen; ++ * neither follows a call, the first is mid-instruction). +8 (written by ++ * the undecoded 1000f820) is 0 when the lockup state was set to 2 by the ++ * DM handler's MMU-fault arm (10005138: the firmware's status word had bit ++ * 0x200, EVENT_STATUS bit 9, which it clears after reading the BIF banks ++ * itself, 10022c98) and 1 from the watchdog count in 1000f538 (TA +0x830 ++ * flags); these are the only two writers of state 2. So arm 0 is an MMU ++ * page fault the firmware took, not a stall for pages: a stall has no event. ++ * ++ * 10017c54 fills +0x24 one byte per group of the object at [GbP+260], each ++ * byte the same four fields: bit 0 the group's +0, bit 1 its +4, bit 2 its ++ * +0xc non-zero, bits 3-7 its +0x10 (groups at +0x318, +0x30, +0x600 and ++ * +0x8e8 for bytes 0-3). The byte with bit 2 set moved with the locked ++ * engine: 05 81 81 81 for a TA lockup (one run), 81 05 81 81 for a 3D one ++ * (another), so bytes 0-2 are named TA, 3D and CL here [inferred from those ++ * two]. The lockup state the firmware keeps in host memory, object 0x4b ++ * (INIT +0x98: +0 state 0/1/2, +8/+0xc/+0x10 per-engine lockup counts, +0x14 ++ * the total, +0x18 the first locked engine; not yet seen on Linux), is ++ * printed raw beside it, and each engine whose submitted and done counts ++ * differ. Memory only: no register is read here. ++ */ ++static void t7000_event_fault(struct pvr_apple *apple, const u8 *e) + { ++ static const char *const groups[4] = { "TA", "3D", "CL", "group3" }; ++ static const char *const names[APPLE_ENGINES] = { ++ [APPLE_ENGINE_GEOM] = "geometry", ++ [APPLE_ENGINE_FRAG] = "fragment", ++ [APPLE_ENGINE_COMPUTE] = "compute", ++ }; + struct pvr_apple_s8003 *s = apple->chip_data; +- const u8 *p = s8003_channel(s, APPLE_ENGINE_COMPUTE); +- const u8 *ctl = s8003_object(s, S8003_WORK_CTL + 8); ++ u32 arm = get_unaligned_le32(e + 8), w = get_unaligned_le32(e + 0x24); ++ const u8 *lk = s8003_object(s, 0x4b); ++ char locked[32] = ""; ++ int i, engine; ++ ++ if (get_unaligned_le32(e) != 4) ++ return; ++ /* the fault registers read next (t7000_report_fault()) take the banks */ ++ if (arm == 0) ++ WRITE_ONCE(s->t7000_mmu_arm, true); ++ for (i = 0; i < 4; i++) { ++ u8 b = w >> (8 * i); + ++ if (b & BIT(2)) { ++ strlcat(locked, groups[i], sizeof(locked)); ++ strlcat(locked, " ", sizeof(locked)); ++ } ++ } + dev_err(apple_dev(apple), +- "hang: compute: submitted %u, stamps %u %u, work ring %u/%u; firmware %s\n", +- apple->last_seq, get32(p + S8003_CU_STAMP), get32(p + S8003_CU_STAMP + 4), +- get32(ctl + S8003_RING_CONSUMER), get32(ctl + S8003_RING_PRODUCER), +- s->failed ? "rings unreadable" : READ_ONCE(apple->fault) ? "posted a fault" : +- "posted no fault"); ++ "firmware lockup recovery: arm %u (%s), locked %s(+0x24 %#x: %s %u/%u/%u/%u %s %u/%u/%u/%u %s %u/%u/%u/%u %s %u/%u/%u/%u as +0/+4/+0xc set/+0x10), +0x1c %#x +0x20 %#x\n", ++ arm, arm == 0 ? "the DM handler's MMU-fault arm" : arm == 1 ? "the TA/3D watchdog" : "?", ++ locked[0] ? locked : "none ", w, ++ groups[0], w & 1, (w >> 1) & 1, (w >> 2) & 1, (w >> 3) & 0x1f, ++ groups[1], (w >> 8) & 1, (w >> 9) & 1, (w >> 10) & 1, (w >> 11) & 0x1f, ++ groups[2], (w >> 16) & 1, (w >> 17) & 1, (w >> 18) & 1, (w >> 19) & 0x1f, ++ groups[3], (w >> 24) & 1, (w >> 25) & 1, (w >> 26) & 1, (w >> 27) & 0x1f, ++ get_unaligned_le32(e + 0x1c), get_unaligned_le32(e + 0x20)); ++ dev_err(apple_dev(apple), ++ "firmware lockup recovery: object 0x4b %08x %08x %08x %08x %08x %08x %08x %08x %08x %08x\n", ++ get32(lk), get32(lk + 4), get32(lk + 8), get32(lk + 0xc), get32(lk + 0x10), ++ get32(lk + 0x14), get32(lk + 0x18), get32(lk + 0x1c), get32(lk + 0x20), ++ get32(lk + 0x24)); ++ for (engine = 0; engine < APPLE_ENGINES; engine++) { ++ u32 submitted = s8003_submitted(apple, engine); ++ u32 done = t7000_engine_done(apple, engine); ++ ++ if (submitted != done) ++ dev_err(apple_dev(apple), "firmware lockup recovery: %s submitted %u done %u\n", ++ names[engine], submitted, done); ++ } ++} ++ ++/* ++ * At a hang: the parameter manager's state, for a TA that ran out of pages: ++ * the TA object (INIT +0x88; +0x728 1 while the TA is active, +0x72c 2 after the out-of-memory handler 10012b24, the ++ * watchdog's words +0x828..+0x840), the last geometry VM's PM control ++ * (+0x24 pages, +0x28 top index, +0x3c/+0x40 the saved hardware words), ++ * its list's usage report (+0x40..+0x4c, taken flag +0x60) and the stack ++ * at the top index; all memory. Then R0+0x348, the PM's live top index, ++ * read once through the gate. A stall for pages doubles the pool of the ++ * next VM (t7000_pm_floor) and of this one, if it lives on. ++ */ ++static void t7000_report_pm(struct pvr_apple *apple) ++{ ++ struct pvr_apple_s8003 *s = apple->chip_data; ++ const u8 *ta = s8003_object(s, S8003_CHANNEL); ++ struct s8003_vm *vm = s->t7000_geom_vm; ++ u32 oom = get32(ta + 0x72c); ++ ++ dev_err(apple_dev(apple), ++ "hang: TA object +0x728 %#x +0x72c %#x%s; watchdog +0x828.. %08x %08x %08x %08x %08x %08x %08x\n", ++ get32(ta + 0x728), oom, oom == 2 ? " (out of parameter memory)" : "", ++ get32(ta + 0x828), get32(ta + 0x82c), get32(ta + 0x830), get32(ta + 0x834), ++ get32(ta + 0x838), get32(ta + 0x83c), get32(ta + 0x840)); ++ if (vm && vm->slot[0] && vm->t7000.stack_map) { ++ const u8 *control = s->root.map + t7000_pm_control_va(vm); ++ const u8 *list = s->shared.map + t7000_pm_list_va(vm); ++ u32 top = get32(control + 0x28); ++ ++ dev_err(apple_dev(apple), ++ "hang: PM pool %u blocks: control pages %u top %u saved %#x %#x; usage %u %u %u %u taken %u; stack[top] %#x\n", ++ vm->t7000.blocks, get32(control + 0x24), top, get32(control + 0x3c), ++ get32(control + 0x40), get32(list + T7000_PM_FEEDBACK), ++ get32(list + T7000_PM_FEEDBACK + 4), get32(list + T7000_PM_FEEDBACK + 8), ++ get32(list + T7000_PM_FEEDBACK + 12), get32(list + T7000_PM_FEEDBACK_TAKEN), ++ top < T7000_PM_STACK_SIZE / 4 ? get32(vm->t7000.stack_map + 4 * top) : 0); ++ if (oom == 2) { ++ u32 blocks = min_t(u32, 2 * vm->t7000.blocks, T7000_PM_BLOCKS_MAX); ++ ++ WRITE_ONCE(s->t7000_pm_floor, max(s->t7000_pm_floor, blocks)); ++ if (READ_ONCE(a8_pm_grow)) ++ vm->t7000.want = max(vm->t7000.want, blocks); ++ dev_err(apple_dev(apple), "hang: new VMs' PM pools from now on at least %u blocks\n", ++ s->t7000_pm_floor); ++ } ++ } ++ if (s->stuck || !t7000_r0_gate(apple)) ++ return; ++ dev_err(apple_dev(apple), "hang: R0+0x348 (PM top index) %#x\n", ++ pvr_cr_read32(apple->pvr_dev, 0x348)); + } + ++/* ++ * A job timed out: the firmware's state from memory, then the fault ++ * registers (t7000_report_bif()) unless the fault event read them already. ++ */ ++static void t7000_report_hang(struct pvr_apple *apple) ++{ ++ static const char *const names[] = { "compute", "geometry", "fragment" }; ++ struct pvr_apple_s8003 *s = apple->chip_data; ++ int engine; ++ ++ for (engine = 0; engine < APPLE_ENGINES; engine++) { ++ const u8 *p = s8003_channel(s, engine); ++ const u8 *ctl = s8003_object(s, S8003_WORK_CTL + 4 * s8003_engine(engine)); ++ u32 stamp = engine == APPLE_ENGINE_COMPUTE ? S8003_CU_STAMP : S8003_CH_STAMP; ++ ++ dev_err(apple_dev(apple), "hang: %s: submitted %u, stamps %u %u, work ring %u/%u\n", ++ names[engine], s8003_submitted(apple, engine), get32(p + stamp), ++ get32(p + stamp + 4), get32(ctl + S8003_RING_CONSUMER), ++ get32(ctl + S8003_RING_PRODUCER)); ++ } ++ dev_err(apple_dev(apple), "hang: firmware %s, Fender state %#x\n", ++ s->failed ? "rings unreadable" : READ_ONCE(apple->fault) ? "posted a fault" : ++ "posted no fault", s->stuck ? 0 : readl(apple->fender + FENDER_POWER_STATE)); ++ t7000_report_pm(apple); ++ /* once: the fault event's read, if the gate let it, else here */ ++ if (READ_ONCE(apple->fault_reported) && xchg(&s->bif_read, false)) { ++ dev_err(apple_dev(apple), "hang: fault registers were read at the fault (above)\n"); ++ return; ++ } ++ t7000_report_bif(apple, "hang"); ++} + + static void t7000_iounmap(void *addr) + { +@@ -5214,6 +5450,7 @@ static const struct s8003_variant s8003_variant_a8 = { + .submit_frag = t7000_submit_frag, + .submit_compute = t7000_submit_compute, + .report_hang = t7000_report_hang, ++ .event_fault = t7000_event_fault, + }; + + static const struct s8003_variant *s8003_variant_of(const struct pvr_apple_chip *chip) +@@ -5319,9 +5556,12 @@ const struct pvr_apple_chip pvr_apple_chip_t7000 = { + .compute_stream = &pvr_apple_g5p_cmd_compute_stream, + .own_pm_pages = true, + .gate_off_ms = 10, ++ .wait_deadline = true, ++ .fault_skips_done = true, + .irq_poll_us = 1000, + .heaps = t7000_heaps, + .read_gpu_id = pvr_apple_t7000_read_gpu_id, ++ .report_fault = t7000_report_fault, + .init = pvr_apple_s8003_init, + .boot = pvr_apple_s8003_boot, + .start = pvr_apple_s8003_start, diff --git a/kernel/patches/drivers/gpu/0071-drm-imagination-apple-Check-the-A8-s-completions-in-.patch b/kernel/patches/drivers/gpu/0071-drm-imagination-apple-Check-the-A8-s-completions-in-.patch new file mode 100644 index 00000000..c4cab800 --- /dev/null +++ b/kernel/patches/drivers/gpu/0071-drm-imagination-apple-Check-the-A8-s-completions-in-.patch @@ -0,0 +1,484 @@ +From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 +From: =?UTF-8?q?Berkay=20K=C3=B6sebay?= +Date: Sat, 21 Nov 2026 12:09:00 +0100 +Subject: [PATCH] drm/imagination: apple: Check the A8's completions in the + interrupt's thread + +On the A8 a job's completion waited on the host: the firmware +interrupt's hard handler queued the poll work item, and a job not yet +seen done was found by the backup check up to 1 ms later. A compute job +took 6.9 ms wall clock, and most firmware interrupts came before their +job's finish stamp was in memory, so their check found nothing and the +job waited for the next period. And a publish could leave a job in the +firmware's ring whose fence had already failed: + +- On a chip with &pvr_apple_chip.irq_thread (the T7000) the firmware + interrupt is threaded. The hard handler acknowledges it and wakes its + thread, which runs the completion check itself, as Apple's kext drains + its rings in its interrupt event source's work loop. The work item + stays the backup, every a8_poll_us (default 250, 20..1000) while jobs + are pending. Both may run at once: fences are retired under fence_lock + and the rings drained under the chip's lock. Teardown waits for the + thread before the firmware's pages go. +- An interrupt whose check finds its job not yet done in memory checks + again after a8_recheck_us (default 20, 5..250), then in the fast + period as before. +- A soak faulted after about 92k jobs with "doorbell not rung: Fender + state 0x1, PLL5 0x0", a failed fragment job and then a 3D read at VA + 0x40 in the job's context. The publish had written the work-ring + entry, the host watermark and the producer index and only then + refused the doorbell, so the firmware ran a job whose fence had + already failed. Every refusal (stuck firmware, the work ring's slot) + is now checked in t7000_publish_check() before the job's numbers, + record or VM state move, and the publish itself cannot fail. +- The doorbell is Apple's kick: Fender's wake, the state read, the + scheduler register written. In state 1 Apple reads no PLL (the + firmware's idle gating and DVFS relock PLL5 while the state reads 1), + so a doorbell with PLL5 relocking is rung and logged. State 0 with + Fender's control 0 or the snoop not armed is still not rung; the job + then waits for the reset. + +The A9 and A10 keep the plain handler, the work item, their doorbell and +a 1 ms backup period: the new hooks are set only in the T7000 table. + +Assisted-by: LLM +--- + drivers/gpu/drm/imagination/pvr_apple.c | 57 ++++++- + drivers/gpu/drm/imagination/pvr_apple_chip.h | 11 ++ + drivers/gpu/drm/imagination/pvr_apple_s8003.c | 161 +++++++++++++----- + 3 files changed, 187 insertions(+), 42 deletions(-) + +diff --git a/drivers/gpu/drm/imagination/pvr_apple.c b/drivers/gpu/drm/imagination/pvr_apple.c +index 02cfd5698b..0bc190df8f 100644 +--- a/drivers/gpu/drm/imagination/pvr_apple.c ++++ b/drivers/gpu/drm/imagination/pvr_apple.c +@@ -148,6 +148,7 @@ int pvr_apple_fender_power_on(struct pvr_apple *apple) + } + + static void pvr_apple_poll_work(struct work_struct *work); ++static void pvr_apple_check(struct pvr_apple *apple, bool from_irq); + static enum hrtimer_restart pvr_apple_poll_timer(struct hrtimer *timer); + static u32 pvr_apple_frag_retired(struct pvr_apple *apple); + +@@ -1209,10 +1210,23 @@ static irqreturn_t pvr_apple_firmware_irq(int irq, void *data) + + /* At its source first, as Apple's kext does before it reads the rings */ + apple->chip->irq_ack(apple); ++ if (apple->chip->irq_thread) ++ return READ_ONCE(apple->stopping) ? IRQ_HANDLED : IRQ_WAKE_THREAD; + pvr_apple_firmware_event(apple); + return IRQ_HANDLED; + } + ++/* ++ * &pvr_apple_chip.irq_thread: the completion check runs in the interrupt's ++ * thread, as Apple's kext drains its rings in its interrupt event source's ++ * work loop, not in a work item behind it. ++ */ ++static irqreturn_t pvr_apple_firmware_irq_thread(int irq, void *data) ++{ ++ pvr_apple_check(data, true); ++ return IRQ_HANDLED; ++} ++ + /** + * pvr_apple_firmware_irq_init() - Take the firmware's interrupt + * @apple: Apple firmware state. +@@ -1230,8 +1244,13 @@ int pvr_apple_firmware_irq_init(struct pvr_apple *apple, int irq) + + if (!apple->chip->irq_ack || !apple->chip->irq_poll_us) + return -EINVAL; +- err = devm_request_irq(dev, irq, pvr_apple_firmware_irq, IRQF_NO_AUTOEN, +- "apple-gfx-fw", apple); ++ if (apple->chip->irq_thread) ++ err = devm_request_threaded_irq(dev, irq, pvr_apple_firmware_irq, ++ pvr_apple_firmware_irq_thread, ++ IRQF_NO_AUTOEN | IRQF_ONESHOT, "apple-gfx-fw", apple); ++ else ++ err = devm_request_irq(dev, irq, pvr_apple_firmware_irq, IRQF_NO_AUTOEN, ++ "apple-gfx-fw", apple); + if (err) + return err; + apple->fw_irq = irq; +@@ -1254,7 +1273,17 @@ static void pvr_apple_fw_irq_set_locked(struct pvr_apple *apple, bool on) + /* Signal the fences of retired jobs; check again while any are pending. */ + static void pvr_apple_poll_work(struct work_struct *work) + { +- struct pvr_apple *apple = container_of(work, struct pvr_apple, poll_work.work); ++ pvr_apple_check(container_of(work, struct pvr_apple, poll_work.work), false); ++} ++ ++/* ++ * The completion check: from the poll work, or on a chip with ++ * &pvr_apple_chip.irq_thread from the firmware interrupt's thread ++ * (@from_irq). Both may run at once: the fences are retired under ++ * fence_lock, the rings drained under the chip's own lock. ++ */ ++static void pvr_apple_check(struct pvr_apple *apple, bool from_irq) ++{ + struct pvr_apple_fence *fence, *tmp; + unsigned int retired = 0, us; + bool pending = false, irq_on; +@@ -1308,6 +1337,20 @@ static void pvr_apple_poll_work(struct work_struct *work) + + if (!pending || READ_ONCE(apple->stopping)) + return; ++ /* ++ * An interrupt that found its job not yet done in memory: the ++ * firmware is ahead of its stamp. Check again soon, not only at the ++ * backup period. ++ */ ++ if (from_irq && !retired) { ++ pvr_apple_firmware_progress(apple); ++ /* a chip's own, shorter first re-check */ ++ if (apple->chip->recheck_us) { ++ us = apple->chip->recheck_us(apple); ++ hrtimer_start(&apple->poll_timer, us_to_ktime(max(us, 5U)), HRTIMER_MODE_REL); ++ return; ++ } ++ } + /* + * Without the firmware's interrupt (the A10's firmware sends no + * completion message in this setup) poll. With it, check again +@@ -1316,7 +1359,7 @@ static void pvr_apple_poll_work(struct work_struct *work) + */ + us = READ_ONCE(pvr_apple_poll_us); + if (irq_on && !ktime_before(ktime_get(), READ_ONCE(apple->fast_until))) +- us = apple->chip->irq_poll_us; ++ us = apple->chip->poll_us ? apple->chip->poll_us(apple) : apple->chip->irq_poll_us; + hrtimer_start(&apple->poll_timer, us_to_ktime(max(us, 10U)), HRTIMER_MODE_REL); + } + +@@ -2154,6 +2197,12 @@ void pvr_apple_fw_fini(struct pvr_device *pvr_dev) + cancel_delayed_work_sync(&apple->poll_work); + pvr_apple_forget_scenes(apple); + pvr_apple_cancel_fences(apple, -ENODEV); ++ /* the interrupt is off now; a check its thread runs reads the rings */ ++ if (apple->chip->irq_thread && apple->fw_irq) { ++ synchronize_irq(apple->fw_irq); ++ hrtimer_cancel(&apple->poll_timer); ++ cancel_delayed_work_sync(&apple->poll_work); ++ } + pvr_apple_gpu_pages_fini(apple); + if (apple->pm_pages) { + /* free lists still holding the pages keep their own references */ +diff --git a/drivers/gpu/drm/imagination/pvr_apple_chip.h b/drivers/gpu/drm/imagination/pvr_apple_chip.h +index 4e2d9a4870..53850fd129 100644 +--- a/drivers/gpu/drm/imagination/pvr_apple_chip.h ++++ b/drivers/gpu/drm/imagination/pvr_apple_chip.h +@@ -247,6 +247,10 @@ struct pvr_apple { + * gives up says so, from memory only. A client that closes, or a VM + * that goes, then waits at most that one timeout under submit_lock. A + * job refused before the chip's own submit path says why. ++ * @irq_thread: The completion check runs in the firmware interrupt's thread ++ * (and the poll work as the backup), not only in a work item the ++ * interrupt queues; an interrupt that finds its job not yet in memory ++ * checks again within the fast period. + * @fault_skips_done: A firmware fault resets through the oldest job its + * engine has not finished, not one finished but not yet retired. + * @submit: Hand @job to the firmware. Takes &pvr_apple.submit_lock itself. +@@ -257,6 +261,10 @@ struct pvr_apple { + * @max_power_state: Highest state licensed for fault capture; optional. + * @report_fault: Capture the MMU fault state after a firmware fault, in place + * of the licence-checked bank read (pvr_apple_report_mmu_fault()); optional. ++ * @poll_us: The backup check period with the interrupt taken, in place of ++ * @irq_poll_us (a tunable); optional. ++ * @recheck_us: After an interrupt whose check found nothing, the delay of ++ * the first re-check, in place of the fast period; optional. + * @events: Read what the firmware posted. Before @engine_done wherever a job + * is waited for; may sleep. + * @vm_init: A userspace VM was created: map what the firmware needs in it. +@@ -291,6 +299,7 @@ struct pvr_apple_chip { + unsigned int gate_off_ms; + bool wait_deadline; + bool fault_skips_done; ++ bool irq_thread; + /* With the firmware's interrupt taken: microseconds between checks while jobs are pending */ + unsigned int irq_poll_us; + const struct drm_pvr_heap *heaps; +@@ -311,6 +320,8 @@ struct pvr_apple_chip { + void (*write_frag)(struct pvr_apple *apple, u32 seq, const void *hw); + void (*report_hang)(struct pvr_apple *apple); + void (*report_fault)(struct pvr_apple *apple); ++ unsigned int (*poll_us)(struct pvr_apple *apple); ++ unsigned int (*recheck_us)(struct pvr_apple *apple); + + int (*prepare_job)(struct pvr_apple *apple, struct pvr_job *job); + int (*submit)(struct pvr_apple *apple, struct pvr_job *job); +diff --git a/drivers/gpu/drm/imagination/pvr_apple_s8003.c b/drivers/gpu/drm/imagination/pvr_apple_s8003.c +index f6a74fad8f..6babaec38a 100644 +--- a/drivers/gpu/drm/imagination/pvr_apple_s8003.c ++++ b/drivers/gpu/drm/imagination/pvr_apple_s8003.c +@@ -4115,39 +4115,63 @@ static void t7000_idle(struct pvr_apple *apple) + /* + * Apple's kick, the same in all five of its MTS_SCHEDULE writers (TA + * 0x846f220, 3D 0x846f408, CL 0x846f5f0, device control 0x846f9a0; COUNTED +- * and the data master): Fender's wake, the power state read, then the META's +- * scheduler register. In state 0 (the firmware idled the core) Apple rings +- * as long as Fender is set up and the snoop is armed; with that write Fender +- * wakes the core (on the iPod: state 1 and the PLL locked within 1 ms). +- * Apple restarts the firmware when Fender's control is 0 and warns when the +- * snoop is not armed; this driver refuses both. In state 1 the PLL must be +- * locked, as for every access to the Rogue registers. ++ * and the data master): Fender's wake (+0x14 = 7, 0x847a640), the power ++ * state read (+0x1000 & 0xf, 0x8477020), then the META's scheduler ++ * register, written in every case. Only in state 0 (the firmware idled the ++ * core) does Apple look further: with Fender's control 0 it restarts the ++ * firmware (0x846dc1c), with the snoop not armed it warns; with the wake ++ * write Fender wakes the core (on the iPod: state 1 and the PLL locked ++ * within 1 ms). In state 1 Apple reads no PLL: the firmware's own power ++ * and clock changes (its idle gating, DVFS) can leave PLL5 relocking while ++ * the state reads 1 (iOS shows that in 30 of 200 draw rounds), and Apple ++ * rings then too, and so does this driver. It ++ * still does not write the register with Fender's control 0 or the snoop ++ * not armed in state 0 (never seen here; Apple's restart is not ++ * implemented): the job is then in the ring and its wait ends in the reset. ++ * ++ * Nothing here fails: a job's publish checks first (t7000_publish_check()). + */ +-static int t7000_doorbell(struct pvr_apple *apple, u32 dm) ++static void t7000_ring(struct pvr_apple *apple, u32 dm) + { + struct pvr_apple_s8003 *s = apple->chip_data; + u32 state, pll, ctrl, snoop; + +- return -EIO; ++ if (s->stuck) { ++ dev_err_ratelimited(apple_dev(apple), ++ "doorbell not rung: the GPU is stuck; the job waits for the reset\n"); ++ return; ++ } + mb(); /* what the firmware is rung for is in memory */ + writel(FENDER_WAKE_ALL, apple->fender + FENDER_WAKE); + state = readl(apple->fender + FENDER_POWER_STATE); +- pll = pmgr_read(s, T7000_PMGR_PLL5); + if (!(state & 0xf)) { + ctrl = readl(apple->fender + FENDER_CTRL); + snoop = readl(apple->fender + T7000_FENDER_SNOOP); + if (!ctrl || (!(ctrl & T7000_FENDER_CTRL_SNOOP) && !(snoop & BIT(1)))) { + dev_err_ratelimited(apple_dev(apple), +- "doorbell not rung: Fender state %#x, control %#x, snoop %#x\n", ++ "doorbell not rung: Fender state %#x, control %#x, snoop %#x; the job waits for the reset\n", + state, ctrl, snoop); +- return -EIO; ++ return; + } +- } else if (!(pll & T7000_PLL_LOCKED)) { +- dev_err_ratelimited(apple_dev(apple), +- "doorbell not rung: Fender state %#x, PLL5 %#x\n", state, pll); +- return -EIO; ++ } else { ++ /* logged only: Apple reads no PLL here */ ++ pll = pmgr_read(s, T7000_PMGR_PLL5); ++ if (!(pll & T7000_PLL_LOCKED)) ++ dev_info_ratelimited(apple_dev(apple), ++ "doorbell rung with PLL5 relocking, as Apple's kext rings: Fender state %#x, PLL5 %#x\n", ++ state, pll); + } + pvr_cr_write32(apple->pvr_dev, ROGUE_CR_MTS_SCHEDULE, ROGUE_CR_MTS_SCHEDULE_TASK_COUNTED | dm); ++} ++ ++/* The variant's doorbell, for device-control commands */ ++static int t7000_doorbell(struct pvr_apple *apple, u32 dm) ++{ ++ struct pvr_apple_s8003 *s = apple->chip_data; ++ ++ if (s->stuck) ++ return -EIO; ++ t7000_ring(apple, dm); + return 0; + } + +@@ -4197,14 +4221,42 @@ static u32 t7000_engine_done(struct pvr_apple *apple, enum pvr_apple_engine engi + return latest + min(behind, 0); + } + ++/* ++ * Everything that can refuse a job's publish, checked before the job's ++ * record, numbers or VM state move: a refused job leaves nothing the ++ * firmware could run. Once, a publish refused after the producer index was ++ * written (the doorbell's PLL check) left the 3D job in the work ring; the ++ * firmware took it un-rung while its fence had already failed, and the 3D ++ * engine faulted in the job's context. The work ring's slot is free once ++ * the firmware took the previous entry (it takes one at once); submit_lock ++ * keeps it so until t7000_publish(). ++ */ ++static int t7000_publish_check(struct pvr_apple *apple, enum pvr_apple_engine engine) ++{ ++ struct pvr_apple_s8003 *s = apple->chip_data; ++ u8 *ctl = s8003_object(s, S8003_WORK_CTL + 4 * s8003_engine(engine)); ++ u32 start = ring_read(ctl, S8003_RING_PRODUCER), consumer; ++ ++ lockdep_assert_held(&apple->submit_lock); ++ if (s->stuck) ++ return -EIO; ++ if (start >= APPLE_RING_ENTRIES) ++ return -EPROTO; ++ return read_poll_timeout(s8003_ring_consumer, consumer, consumer == start, 20, ++ S8003_RING_TIMEOUT_US, false, apple, ctl); ++} ++ + /* + * Tell the firmware that @engine's pointer ring is filled up to @end: an + * entry of the engine's work ring {engine, channel, end}, with Apple's 1 in + * its fourth word on a render channel (P5b), the host's watermark, the work + * ring's producer index, and Apple's kick with the engine's data master (TA +- * DM0, 3D DM1, CL DM2: 0x846f220, 0x846f408, 0x846f5f0). ++ * DM0, 3D DM1, CL DM2: 0x846f220, 0x846f408, 0x846f5f0). After ++ * t7000_publish_check() under the same hold of submit_lock; it does not ++ * fail: once the producer index is written the job is the firmware's, and ++ * its fence follows the firmware's progress, a fault or the reset. + */ +-static int t7000_publish(struct pvr_apple *apple, enum pvr_apple_engine engine, u32 end) ++static void t7000_publish(struct pvr_apple *apple, enum pvr_apple_engine engine, u32 end) + { + static const u32 dm[] = { + ROGUE_CR_MTS_SCHEDULE_DM_DM0, ROGUE_CR_MTS_SCHEDULE_DM_DM1, ROGUE_CR_MTS_SCHEDULE_DM_DM2, +@@ -4214,17 +4266,9 @@ static int t7000_publish(struct pvr_apple *apple, enum pvr_apple_engine engine, + u8 *ctl = s8003_object(s, S8003_WORK_CTL + 4 * fw_engine); + u8 *ring = s8003_object(s, S8003_WORK_RING + 4 * fw_engine); + u32 host = engine == APPLE_ENGINE_COMPUTE ? S8003_CH_CTL : T7000_RCH_CTL; +- u32 start = ring_read(ctl, S8003_RING_PRODUCER), consumer; ++ u32 start = ring_read(ctl, S8003_RING_PRODUCER); + u8 *p = s8003_channel(s, engine), *entry; +- int err; + +- if (start >= APPLE_RING_ENTRIES) +- return -EPROTO; +- /* The firmware takes a work-ring entry at once; the ring never holds two */ +- err = read_poll_timeout(s8003_ring_consumer, consumer, consumer == start, 20, +- S8003_RING_TIMEOUT_US, false, apple, ctl); +- if (err) +- return err; + entry = ring + start * S8003_DC_SIZE; + memset(entry, 0, S8003_DC_SIZE); + put32(entry, fw_engine); +@@ -4237,7 +4281,7 @@ static int t7000_publish(struct pvr_apple *apple, enum pvr_apple_engine engine, + wmb(); + WRITE_ONCE(*(__le32 *)(ctl + S8003_RING_PRODUCER), + cpu_to_le32((start + 1) % APPLE_RING_ENTRIES)); +- return t7000_doorbell(apple, dm[fw_engine]); ++ t7000_ring(apple, dm[fw_engine]); + } + + /* The A8's compute record: the A9's with every offset from 0x10 on 8 further */ +@@ -4269,6 +4313,9 @@ static int t7000_submit_compute(struct pvr_apple *apple, struct s8003_vm *vm, st + err = s8003_wait_done(apple, APPLE_ENGINE_COMPUTE, apple->last_seq); + if (err) + return s8003_submit_error(apple, vm, "compute", err, "record slot wait"); ++ err = t7000_publish_check(apple, APPLE_ENGINE_COMPUTE); ++ if (err) ++ return s8003_submit_error(apple, vm, "compute", err, "publish"); + seq = apple->last_seq + 1; + k = seq - 1; + { +@@ -4315,9 +4362,7 @@ static int t7000_submit_compute(struct pvr_apple *apple, struct s8003_vm *vm, st + put32(work + 0x27c, k); + put32(p + S8003_CH_RING + ((seq - 1) % S8003_CH_ENTRIES) * 4, w); + apple->last_seq = seq; +- err = t7000_publish(apple, APPLE_ENGINE_COMPUTE, seq % S8003_CH_ENTRIES); +- if (err) +- return s8003_submit_error(apple, vm, "compute", err, "publish"); ++ t7000_publish(apple, APPLE_ENGINE_COMPUTE, seq % S8003_CH_ENTRIES); + return seq; + } + +@@ -4993,6 +5038,9 @@ static int t7000_submit_geom(struct pvr_apple *apple, struct s8003_vm *vm, struc + err = s8003_wait_done(apple, APPLE_ENGINE_GEOM, apple->ta_seq); + if (err) + return s8003_submit_error(apple, vm, "geometry", err, "record slot wait"); ++ err = t7000_publish_check(apple, APPLE_ENGINE_GEOM); ++ if (err) ++ return s8003_submit_error(apple, vm, "geometry", err, "publish"); + + seq = apple->ta_seq + 1; + slice_addr = job->hwrt->data.rgn_header_dev_addr + +@@ -5015,9 +5063,7 @@ static int t7000_submit_geom(struct pvr_apple *apple, struct s8003_vm *vm, struc + vm->t7000.ta_seq = seq; + vm->t7000.rgn_addr = slice_addr; + apple->ta_seq = seq; +- err = t7000_publish(apple, APPLE_ENGINE_GEOM, seq % T7000_RCH_ENTRIES); +- if (err) +- return s8003_submit_error(apple, vm, "geometry", err, "publish"); ++ t7000_publish(apple, APPLE_ENGINE_GEOM, seq % T7000_RCH_ENTRIES); + return seq; + } + +@@ -5047,6 +5093,9 @@ static int t7000_submit_frag(struct pvr_apple *apple, struct s8003_vm *vm, struc + err = s8003_wait_done(apple, APPLE_ENGINE_FRAG, apple->frag_seq); + if (err) + return s8003_submit_error(apple, vm, "fragment", err, "record slot wait"); ++ err = t7000_publish_check(apple, APPLE_ENGINE_FRAG); ++ if (err) ++ return s8003_submit_error(apple, vm, "fragment", err, "publish"); + + seq = apple->frag_seq + 1; + err = t7000_fill_3d(p + T7000_RCH_W, job->cmd, &rt, vm->t7000.rgn_addr); +@@ -5072,9 +5121,7 @@ static int t7000_submit_frag(struct pvr_apple *apple, struct s8003_vm *vm, struc + + vm->t7000.open = false; + apple->frag_seq = seq; +- err = t7000_publish(apple, APPLE_ENGINE_FRAG, (2 * seq) % T7000_RCH_ENTRIES); +- if (err) +- return s8003_submit_error(apple, vm, "fragment", err, "publish"); ++ t7000_publish(apple, APPLE_ENGINE_FRAG, (2 * seq) % T7000_RCH_ENTRIES); + return seq; + } + +@@ -5308,6 +5355,36 @@ static void t7000_report_hang(struct pvr_apple *apple) + t7000_report_bif(apple, "hang"); + } + ++/* ++ * The backup completion check's period while jobs are pending (with the ++ * interrupt taken): the firmware raises its interrupt some time after it ++ * writes a job's stamp, and in one measurement the 250 us check found 71% of ++ * the jobs first. A tunable, to measure the trade against CPU wake-ups. ++ */ ++static unsigned int a8_poll_us = 250; ++module_param(a8_poll_us, uint, 0644); ++MODULE_PARM_DESC(a8_poll_us, "A8: backup completion check period in us while jobs are pending (20..1000, default 250)"); ++ ++static unsigned int t7000_poll_us(struct pvr_apple *apple) ++{ ++ return clamp(READ_ONCE(a8_poll_us), 20u, 1000u); ++} ++ ++/* ++ * An interrupt whose check finds its job not done in memory yet: measurements show ++ * that is most of them (interrupts that found nothing ~5200 against ~1950 ++ * that found their job), and the 100 us fast period then found the job. ++ * The first re-check comes sooner; the fast period follows. ++ */ ++static unsigned int a8_recheck_us = 20; ++module_param(a8_recheck_us, uint, 0644); ++MODULE_PARM_DESC(a8_recheck_us, "A8: first re-check after an interrupt that found nothing, in us (5..250, default 20)"); ++ ++static unsigned int t7000_recheck_us(struct pvr_apple *apple) ++{ ++ return clamp(READ_ONCE(a8_recheck_us), 5u, 250u); ++} ++ + static void t7000_iounmap(void *addr) + { + iounmap((void __iomem *)addr); +@@ -5558,10 +5635,18 @@ const struct pvr_apple_chip pvr_apple_chip_t7000 = { + .gate_off_ms = 10, + .wait_deadline = true, + .fault_skips_done = true, +- .irq_poll_us = 1000, ++ .irq_thread = true, ++ /* ++ * Between the firmware's interrupts the completion check runs every ++ * 250 us while jobs are pending: the A9's 1 ms was a sixth of an A8 ++ * compute job's wall clock. Memory only: the stamps and the rings. ++ */ ++ .irq_poll_us = 250, + .heaps = t7000_heaps, + .read_gpu_id = pvr_apple_t7000_read_gpu_id, + .report_fault = t7000_report_fault, ++ .poll_us = t7000_poll_us, ++ .recheck_us = t7000_recheck_us, + .init = pvr_apple_s8003_init, + .boot = pvr_apple_s8003_boot, + .start = pvr_apple_s8003_start, diff --git a/kernel/patches/drivers/gpu/0072-drm-imagination-apple-Submit-an-A8-fragment-job-behi.patch b/kernel/patches/drivers/gpu/0072-drm-imagination-apple-Submit-an-A8-fragment-job-behi.patch new file mode 100644 index 00000000..e092d32c --- /dev/null +++ b/kernel/patches/drivers/gpu/0072-drm-imagination-apple-Submit-an-A8-fragment-job-behi.patch @@ -0,0 +1,270 @@ +From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 +From: =?UTF-8?q?Berkay=20K=C3=B6sebay?= +Date: Sat, 21 Nov 2026 12:10:00 +0100 +Subject: [PATCH] drm/imagination: apple: Submit an A8 fragment job behind its + geometry job + +On the A8 every render pass crosses the host twice. A fragment job waits +for its geometry job's fence, so between the TA's end and the 3D's start +sit the firmware's interrupt, the completion check, the signal, the +fragment job's run_job and doorbell, and the firmware's pickup of the 3D +record: a fragment job's doorbell to start of work averaged 295 us +against 88 us of 3D work. + +The A8's firmware orders the pair itself: every 3D record goes behind +the barrier command {4, the TA's finish stamp, the value it reaches, the +job's number}, as Apple's A8 kext writes it. The A9 relies on the same +and submits a fragment job once its geometry job is submitted +(pvr_apple_chip_s8003.scenes = 2); Apple's A9 kext kicks a command +buffer's TA and 3D back to back. + +A chip hook, &pvr_apple_chip.joint, asks for that order per job without +two scenes. The T7000 answers the parameter a8_joint (default 0, read +per job; change it only with the GPU idle). A geometry job still waits +for the last fragment job's fence: the A8 holds one scene per VM, and +its PM feedback and set build run between scenes. + +With a8_joint a fragment job is written some tens of microseconds after +the previous 3D's finish stamp is seen, while the firmware may still be +retiring that job: it reads the record (the mirror stamp's address and +value) and runs its microsequence to the end op after the stamp, and +its interrupt came 80 us after the stamp on average. Rewriting the one +3D record in that window froze the whole SoC under vkmark. So with +a8_joint the 3D channel alternates two records, the second in the +0x1000 the channel has before the PM control blocks, as Apple's kext +writes each command buffer's records apart and the A9 here alternates +two: a fragment job never rewrites the record of the job before it. A +partial render reads the 3D ring entry its TA names (pr_index); the +geometry job points it at the last fragment job's complete record, and +the fragment job names its own only after a write barrier, so a partial +render never reads a record being written. Without a8_joint the single +record is kept as before. + +With a8_joint=1, vkmark at 256x256 ran at 910 against 842 with it off, +over 118k jobs with no fault and no refused job. It stays off by default +until it has had wider testing. + +Assisted-by: LLM +--- + drivers/gpu/drm/imagination/pvr_apple.c | 6 +- + drivers/gpu/drm/imagination/pvr_apple_chip.h | 6 + + drivers/gpu/drm/imagination/pvr_apple_s8003.c | 104 ++++++++++++++++-- + 3 files changed, 107 insertions(+), 9 deletions(-) + +diff --git a/drivers/gpu/drm/imagination/pvr_apple.c b/drivers/gpu/drm/imagination/pvr_apple.c +index 0bc190df8f..6a963f2bfb 100644 +--- a/drivers/gpu/drm/imagination/pvr_apple.c ++++ b/drivers/gpu/drm/imagination/pvr_apple.c +@@ -1801,7 +1801,9 @@ struct dma_fence *pvr_apple_submit_job(struct pvr_job *job) + * &pvr_apple_chip.vm_scenes): a geometry job then waits for the fragment job + * before the last one if the last one is its own VM's, and a fragment job + * for its geometry job to be submitted rather than finished +- * (pvr_apple_job_armed()). ++ * (pvr_apple_job_armed()). A chip with one scene whose firmware orders the ++ * pair (&pvr_apple_chip.joint) keeps the geometry job's wait for the last ++ * fragment job and lets the fragment job follow its geometry job's submit. + * + * Return: 0 or a negative error. + */ +@@ -1837,7 +1839,7 @@ int pvr_apple_job_add_deps(struct pvr_job *job) + void pvr_apple_job_armed(struct pvr_job *job, struct dma_fence *finished) + { + struct pvr_apple *apple = to_apple(job->pvr_dev); +- bool joint = apple->chip->scenes > 1; ++ bool joint = apple->chip->scenes > 1 || (apple->chip->joint && apple->chip->joint(apple)); + struct dma_fence **last, *old; + + if (job->type == DRM_PVR_JOB_TYPE_GEOMETRY) +diff --git a/drivers/gpu/drm/imagination/pvr_apple_chip.h b/drivers/gpu/drm/imagination/pvr_apple_chip.h +index 53850fd129..2a94dd8d26 100644 +--- a/drivers/gpu/drm/imagination/pvr_apple_chip.h ++++ b/drivers/gpu/drm/imagination/pvr_apple_chip.h +@@ -332,6 +332,12 @@ struct pvr_apple_chip { + void (*vm_fini)(struct pvr_apple *apple, struct pvr_vm_context *vm_ctx); + bool (*vm_evictable)(struct pvr_apple *apple, struct pvr_vm_context *vm_ctx); + unsigned int (*vm_scenes)(struct pvr_apple *apple, struct pvr_vm_context *vm_ctx); ++ /* ++ * Whether the firmware orders a fragment job after its geometry job ++ * itself, so that the fragment job need wait only for its geometry job ++ * to be submitted, as with two scenes (&scenes > 1); asked per job. ++ */ ++ bool (*joint)(struct pvr_apple *apple); + void (*mmu_flush)(struct pvr_apple *apple); + /* Acknowledge the firmware's interrupt at its source; hard IRQ context */ + void (*irq_ack)(struct pvr_apple *apple); +diff --git a/drivers/gpu/drm/imagination/pvr_apple_s8003.c b/drivers/gpu/drm/imagination/pvr_apple_s8003.c +index 6babaec38a..977d36cb56 100644 +--- a/drivers/gpu/drm/imagination/pvr_apple_s8003.c ++++ b/drivers/gpu/drm/imagination/pvr_apple_s8003.c +@@ -429,6 +429,8 @@ struct pvr_apple_s8003 { + u32 t7000_pm_floor; + struct s8003_vm *t7000_geom_vm; + bool t7000_mmu_arm; /* a lockup event came from the MMU-fault arm */ ++ /* The 3D record the last fragment job was written to (T7000_RCH_W or T7000_RCH1_W) */ ++ u32 t7000_frag_w; + + bool bif_read; /* t7000_report_fault() read the fault registers */ + }; +@@ -3687,6 +3689,16 @@ static void t7000_init_build(struct pvr_apple *apple, u32 q) + #define T7000_RCH_3D_USEQ 0xd00 + #define T7000_RCH_TIMES 0x2800 /* the 3D record's times, 16 bytes */ + #define T7000_RCH_SIZE 0x3000 ++/* ++ * The 3D channel's second record (a8_joint): its barrier command, record, ++ * microsequence and times behind the first's, in the 0x1000 the 3D channel ++ * has before the PM control blocks (T7000_PM_CONTROL). ++ */ ++#define T7000_RCH_SLOT1 0x3000 ++#define T7000_RCH1_CMD (T7000_RCH_SLOT1 + 0x000) ++#define T7000_RCH1_W (T7000_RCH_SLOT1 + 0x100) ++#define T7000_RCH1_3D_USEQ (T7000_RCH_SLOT1 + 0xa00) ++#define T7000_RCH1_TIMES (T7000_RCH_SLOT1 + 0xc00) + #define T7000_RCH_CTL 0x2000 /* Apple's ring control: 0x60 + 0x144 entries */ + #define T7000_RCH_RING (T7000_RCH_CTL + 0x60) + #define T7000_RCH_ENTRIES 0x144 +@@ -4993,6 +5005,47 @@ static void t7000_pm_feedback(struct pvr_apple *apple, struct s8003_vm *vm) + } + } + ++/* ++ * A fragment job is submitted once its geometry job is (pvr_apple_job_armed()), ++ * not once it finished: the firmware holds the 3D itself. Each 3D record ++ * goes behind the barrier command {4, the TA's finish stamp, the value it ++ * reaches, the job's number} (t7000_submit_frag()), Apple's form, so ++ * the 3D starts when its TA is done without the host's interrupt, check, ++ * signal and the fragment job's run_job in between. That barrier is what ++ * Apple's A8 kext puts in front of each 3D record (seen in an iOS capture), so iOS ++ * needs no host wait there; the A9's kext kicks a command buffer's TA and 3D ++ * back to back (submitTAWorkAndKickMTS 0xfffffff006ca00a0 and its 3D twin ++ * 0xfffffff006ca01f8), as the A9 here does (pvr_apple_chip_s8003.scenes). ++ * A partial render then finds the 3D record its TA names (pr_index) in the ++ * ring, as on iOS. 0 waits for the geometry job's fence as before. ++ */ ++static bool a8_joint; ++module_param(a8_joint, bool, 0644); ++MODULE_PARM_DESC(a8_joint, "A8: submit a fragment job right after its geometry job, the firmware holds the 3D until the TA is done; change it only with the GPU idle (default 0)"); ++ ++static bool t7000_joint(struct pvr_apple *apple) ++{ ++ return READ_ONCE(a8_joint); ++} ++ ++/* ++ * The 3D record of fragment job @seq: with a8_joint the two alternate. A ++ * fragment job is then written as soon as its geometry job is submitted, ++ * some tens of microseconds after the job before it was seen done, while ++ * the firmware may still retire that job: its interrupt came 80 us after ++ * its stamp was visible on average, when measured, and the firmware reads the ++ * record for the retirement (the mirror stamp's address and value at ++ * W+0x870..0x878) and runs its microsequence to the end op (0x40000017). ++ * Apple's kext writes each command buffer's records apart and reclaims ++ * them on the firmware's completion event; the A9 here alternates two ++ * (s8003_slot_of()). Without a8_joint the next fragment job came a geometry ++ * job later (over 600 us), and the one record is kept as it was. ++ */ ++static bool t7000_frag_slot1(struct pvr_apple_s8003 *s) ++{ ++ return READ_ONCE(a8_joint) && s->t7000_frag_w != T7000_RCH1_W; ++} ++ + static int t7000_submit_geom(struct pvr_apple *apple, struct s8003_vm *vm, struct pvr_job *job) + { + struct pvr_hwrt_dataset *dataset = job->hwrt->hwrt_dataset; +@@ -5056,6 +5109,16 @@ static int t7000_submit_geom(struct pvr_apple *apple, struct s8003_vm *vm, struc + }; + t7000_ta_write(p + T7000_RCH_W, p + T7000_RCH_TA_USEQ, &j); + put32(p + T7000_RCH_RING + ((seq - 1) % T7000_RCH_ENTRIES) * 4, j.w); ++ /* ++ * The 3D ring entry this TA names for a partial render (pr_index) is ++ * written by its fragment job, which with a8_joint is written while ++ * this TA runs. Until then it names the last fragment job's record, ++ * complete and not rewritten (t7000_frag_slot1()), as it did before ++ * a8_joint; never a record being written. ++ */ ++ if (READ_ONCE(a8_joint) && s->t7000_frag_w) ++ put32(s8003_channel(s, APPLE_ENGINE_FRAG) + T7000_RCH_RING + j.pr_index * 4, ++ j.pr_ch + s->t7000_frag_w); + + dataset->apple_rgn.cursor += dataset->apple_rgn.slice; + vm->t7000.open = true; +@@ -5078,12 +5141,18 @@ static int t7000_submit_frag(struct pvr_apple *apple, struct s8003_vm *vm, struc + struct pvr_apple_s8003 *s = apple->chip_data; + u32 addr = s8003_channel_addr(s, APPLE_ENGINE_FRAG); + u32 ta_addr = s8003_channel_addr(s, APPLE_ENGINE_GEOM); +- u8 *p = s8003_channel(s, APPLE_ENGINE_FRAG), *cmd = p + T7000_RCH_CMD; ++ u8 *p = s8003_channel(s, APPLE_ENGINE_FRAG), *cmd; ++ u32 cmd_off, w_off, useq_off, times_off; + struct t7000_rjob j; + struct s8003_rt rt; + u32 seq, ring; + int err; + ++ BUILD_BUG_ON(T7000_RCH1_W - T7000_RCH1_CMD < S8003_CMD_SIZE); ++ BUILD_BUG_ON(T7000_RCH1_3D_USEQ - T7000_RCH1_W < T7000_3D_W_SIZE); ++ BUILD_BUG_ON(T7000_RCH1_TIMES - T7000_RCH1_3D_USEQ < T7000_3D_USEQ_SIZE); ++ BUILD_BUG_ON(T7000_3D_CHANNEL + T7000_RCH1_TIMES + 16 > T7000_PM_CONTROL); ++ + if (!vm->t7000.open || vm->t7000.hwrt != job->hwrt || vm->t7000.built != s->gen) + return s8003_submit_error(apple, vm, "fragment", -ECANCELED, + "its geometry job failed or was not submitted"); +@@ -5098,26 +5167,46 @@ static int t7000_submit_frag(struct pvr_apple *apple, struct s8003_vm *vm, struc + return s8003_submit_error(apple, vm, "fragment", err, "publish"); + + seq = apple->frag_seq + 1; +- err = t7000_fill_3d(p + T7000_RCH_W, job->cmd, &rt, vm->t7000.rgn_addr); ++ /* ++ * Which record (t7000_frag_slot1()): with a8_joint the two alternate, so ++ * this job never rewrites the record of the job before it, which the ++ * firmware may still be retiring. ++ */ ++ if (t7000_frag_slot1(s)) { ++ cmd_off = T7000_RCH1_CMD; ++ w_off = T7000_RCH1_W; ++ useq_off = T7000_RCH1_3D_USEQ; ++ times_off = T7000_RCH1_TIMES; ++ } else { ++ cmd_off = T7000_RCH_CMD; ++ w_off = T7000_RCH_W; ++ useq_off = T7000_RCH_3D_USEQ; ++ times_off = T7000_RCH_TIMES; ++ } ++ cmd = p + cmd_off; ++ err = t7000_fill_3d(p + w_off, job->cmd, &rt, vm->t7000.rgn_addr); + if (err) + return s8003_submit_error(apple, vm, "fragment", err, "3D record fill"); +- memset(p + T7000_RCH_TIMES, 0, 16); ++ memset(p + times_off, 0, 16); + j = (struct t7000_rjob) { +- .ch = addr, .w = addr + T7000_RCH_W, .useq = addr + T7000_RCH_3D_USEQ, ++ .ch = addr, .w = addr + w_off, .useq = addr + useq_off, + .object = s8003_object_addr(&s->layout, S8003_CHANNEL + 1), .slot = vm->slot[0], + .pm_id = 0, .control = s8003_meta_addr(S8003_ROOT_VA + t7000_pm_control_va(vm)), + .s = t7000_pm_s(vm), .dep = ta_addr + S8003_CH_DEP, .seq = seq, .k = seq - 1, +- .trace = ++s->t7000_trace, .times = addr + T7000_RCH_TIMES, ++ .trace = ++s->t7000_trace, .times = addr + times_off, + }; +- t7000_3d_write(p + T7000_RCH_W, p + T7000_RCH_3D_USEQ, &j); ++ t7000_3d_write(p + w_off, p + useq_off, &j); + memset(cmd, 0, S8003_CMD_SIZE); + put32(cmd, S8003_CMD_WAIT); + put32(cmd + 4, ta_addr + S8003_CH_STAMP + 4); + put32(cmd + 8, vm->t7000.ta_seq); + put32(cmd + 12, seq); ++ /* the whole record before the ring entries that name it (a partial render reads them) */ ++ wmb(); + ring = (2 * (seq - 1)) % T7000_RCH_ENTRIES; +- put32(p + T7000_RCH_RING + ring * 4, addr + T7000_RCH_CMD); ++ put32(p + T7000_RCH_RING + ring * 4, addr + cmd_off); + put32(p + T7000_RCH_RING + ((ring + 1) % T7000_RCH_ENTRIES) * 4, j.w); ++ s->t7000_frag_w = w_off; + + vm->t7000.open = false; + apple->frag_seq = seq; +@@ -5647,6 +5736,7 @@ const struct pvr_apple_chip pvr_apple_chip_t7000 = { + .report_fault = t7000_report_fault, + .poll_us = t7000_poll_us, + .recheck_us = t7000_recheck_us, ++ .joint = t7000_joint, + .init = pvr_apple_s8003_init, + .boot = pvr_apple_s8003_boot, + .start = pvr_apple_s8003_start, diff --git a/kernel/patches/drivers/gpu/0073-drm-imagination-apple-Show-where-an-A8-job-s-time-go.patch b/kernel/patches/drivers/gpu/0073-drm-imagination-apple-Show-where-an-A8-job-s-time-go.patch new file mode 100644 index 00000000..057df3f3 --- /dev/null +++ b/kernel/patches/drivers/gpu/0073-drm-imagination-apple-Show-where-an-A8-job-s-time-go.patch @@ -0,0 +1,1708 @@ +From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 +From: =?UTF-8?q?Berkay=20K=C3=B6sebay?= +Date: Sat, 21 Nov 2026 12:11:00 +0100 +Subject: [PATCH] drm/imagination: apple: Show where an A8 job's time goes in + apple_jobs + +The A8 spends most of a small job outside the GPU's work, and debugfs +apple_jobs does not say where. These statistics are what the A8's +completion and submission changes before this were measured with. They +are kept apart, at the end of the series. Apart from the firmware's time +words below, nothing here changes what the driver does; without this +patch those words stay 0 except the 3D's end, as the A8 ran before +them, but the series without this patch has not run on the device. +Times are in microseconds, as last, max, average and count. + +- Per engine: wait_us (a job's arming at submit to its run_job), run_us + (run_job itself), job_us (run_job's fence bound to its signal). +- The interrupt path: kick_to_irq_us, irq_to_check_us, + irq_to_signal_us and irq_recheck_to_signal_us, the jobs retired after + an interrupt, after its re-check and by the backup poll, and the + interrupts whose check found nothing. +- geometry_signal_to_fragment_run_us, firmware_start_us, + chip_prepare_us, and the time inside CREATE_BO, VM_MAP, + CREATE_VM_CONTEXT, CREATE_CONTEXT, CREATE_FREE_LIST, + CREATE_HWRT_DATASET and SUBMIT_JOBS (wrapped in pvr_drv.c for every + chip, recorded only on an Apple GPU). +- What is alive now, for leak checks: jobs, VM contexts, GEM objects and + their bytes, MMU page-table pages (counted in the shared GEM, MMU and + VM create and free paths). +- New chip hooks: debugfs_show and debugfs_reset for a chip's own lines, + job_retired at each retirement and irq_checked after each interrupt's + check, all optional and set only by the T7000. +- The A8's own lines: per-VM states and PM objects alive, the PM pool + size a new VM gets and the PM set builds; the doorbells, with how many + found Fender in state 0 and with PLL5 relocking or not rung; the jobs + refused by reason ("a8 refused not_prepared N frag_no_geom N ..."), + including the refusals at the ioctl; the interrupts by what their + check found, with the stamp events against the retirements; a8_joint. +- The A8's job timeline, per render engine, from the firmware's own + words: each record's op-0x18 result is the engine's work time in the + firmware's 24 MHz clock, and the firmware stores the absolute start + and end where the record's two pointer words point. Those words now + point at the channel's times, the start at +8 and the end at +0, as + the A9's records point theirs, and the TA channel's times are zeroed + for each job; before, only the 3D's end was set. + submit_to_doorbell, doorbell_to_start, start_to_end and end_to_signal + add up to the job's total, with the details (PM set build, slot and + ring waits, a doorbell that woke Fender, a fragment job's wait for its + TA with a8_joint, the tail by interrupt or by check). The first four + samples per engine whose times are unusable are logged raw. + +apple_jobs is now 0600, and a write to it zeroes the statistics, so one +workload can be measured without the firmware start before it. + +Assisted-by: LLM +--- + drivers/gpu/drm/imagination/pvr_apple.c | 270 ++++++++- + drivers/gpu/drm/imagination/pvr_apple.h | 15 + + drivers/gpu/drm/imagination/pvr_apple_chip.h | 70 +++ + drivers/gpu/drm/imagination/pvr_apple_queue.c | 3 + + drivers/gpu/drm/imagination/pvr_apple_s8003.c | 563 ++++++++++++++++-- + drivers/gpu/drm/imagination/pvr_device.h | 11 + + drivers/gpu/drm/imagination/pvr_drv.c | 36 +- + drivers/gpu/drm/imagination/pvr_gem.c | 5 + + drivers/gpu/drm/imagination/pvr_job.h | 3 + + drivers/gpu/drm/imagination/pvr_mmu.c | 2 + + drivers/gpu/drm/imagination/pvr_vm.c | 2 + + 11 files changed, 934 insertions(+), 46 deletions(-) + +diff --git a/drivers/gpu/drm/imagination/pvr_apple.c b/drivers/gpu/drm/imagination/pvr_apple.c +index 6a963f2bfb..c06528dc18 100644 +--- a/drivers/gpu/drm/imagination/pvr_apple.c ++++ b/drivers/gpu/drm/imagination/pvr_apple.c +@@ -87,6 +87,12 @@ static struct pvr_apple *to_apple(struct pvr_device *pvr_dev) + return pvr_dev->apple; + } + ++static void pvr_apple_stat_show(struct seq_file *s, const char *name, const struct pvr_apple_stat *st) ++{ ++ seq_printf(s, "%s last %llu max %llu avg %llu n %llu\n", name, st->last, st->max, ++ st->n ? div64_u64(st->sum, st->n) : 0, st->n); ++} ++ + /* Ask Fender for power state @state and wait until it reports it */ + int pvr_apple_request_power_state(struct pvr_apple *apple, u32 state) + { +@@ -447,12 +453,20 @@ static int pvr_apple_power_on(struct pvr_device *pvr_dev) + int pvr_apple_prepare_job(struct pvr_device *pvr_dev) + { + struct pvr_apple *apple = to_apple(pvr_dev); ++ ktime_t start = 0; + int err; + + mutex_lock(&apple->submit_lock); ++ if (!READ_ONCE(apple->stopping) && !apple->booted) ++ start = ktime_get(); + err = READ_ONCE(apple->stopping) ? -ENODEV : + apple->booted ? 0 : pvr_apple_fw_restart(apple); + mutex_unlock(&apple->submit_lock); ++ if (start) { ++ spin_lock_irq(&apple->fence_lock); ++ pvr_apple_stat_add(&apple->boot_us, ktime_us_delta(ktime_get(), start)); ++ spin_unlock_irq(&apple->fence_lock); ++ } + if (err) + dev_err(apple_dev(apple), "GPU did not start: %d\n", err); + else if (READ_ONCE(apple->devfreq_suspended)) +@@ -685,6 +699,24 @@ static int pvr_apple_jobs_show(struct seq_file *s, void *unused) + unsigned int pending[APPLE_ENGINES] = {}; + struct pvr_apple_fence *fence; + u64 retired_total, busy_ns; ++ u64 job_last[APPLE_ENGINES], job_max[APPLE_ENGINES], job_sum[APPLE_ENGINES], job_n[APPLE_ENGINES]; ++ struct pvr_apple_stat wait_us[APPLE_ENGINES], run_us[APPLE_ENGINES], irq_us; ++ struct pvr_apple_stat irq_to_signal_us, geom_to_frag_us, boot_us, prepare_us; ++ struct pvr_apple_stat ioctl_us[APPLE_IOCTLS]; ++ u64 retired_after_irq, retired_by_poll, irq_empty, retired_after_irq_recheck; ++ struct pvr_apple_stat irq_to_check_us, irq_recheck_to_signal_us; ++ static const char *const ioctl_names[APPLE_IOCTLS] = { ++ [APPLE_IOCTL_CREATE_BO] = "ioctl CREATE_BO us", ++ [APPLE_IOCTL_VM_MAP] = "ioctl VM_MAP us", ++ [APPLE_IOCTL_CREATE_VM_CONTEXT] = "ioctl CREATE_VM_CONTEXT us", ++ [APPLE_IOCTL_CREATE_CONTEXT] = "ioctl CREATE_CONTEXT us", ++ [APPLE_IOCTL_CREATE_FREE_LIST] = "ioctl CREATE_FREE_LIST us", ++ [APPLE_IOCTL_CREATE_HWRT_DATASET] = "ioctl CREATE_HWRT_DATASET us", ++ [APPLE_IOCTL_SUBMIT_JOBS] = "ioctl SUBMIT_JOBS us", ++ }; ++ struct pvr_device *pvr_dev = apple->pvr_dev; ++ unsigned long jobs = 0, id; ++ struct pvr_job *job; + bool irq_on; + unsigned long flags; + bool booted, fault = false; +@@ -704,6 +736,26 @@ static int pvr_apple_jobs_show(struct seq_file *s, void *unused) + pending[engine]++; + retired_total = apple->retired_total; + busy_ns = apple->busy_ns; ++ for (engine = 0; engine < APPLE_ENGINES; engine++) { ++ job_last[engine] = apple->job_us_last[engine]; ++ job_max[engine] = apple->job_us_max[engine]; ++ job_sum[engine] = apple->job_us_sum[engine]; ++ job_n[engine] = apple->job_us_n[engine]; ++ } ++ memcpy(wait_us, apple->wait_us, sizeof(wait_us)); ++ memcpy(run_us, apple->run_us, sizeof(run_us)); ++ irq_us = apple->irq_us; ++ irq_to_signal_us = apple->irq_to_signal_us; ++ retired_after_irq = apple->retired_after_irq; ++ retired_by_poll = apple->retired_by_poll; ++ irq_empty = apple->irq_empty; ++ retired_after_irq_recheck = apple->retired_after_irq_recheck; ++ irq_recheck_to_signal_us = apple->irq_recheck_to_signal_us; ++ irq_to_check_us = apple->irq_to_check_us; ++ geom_to_frag_us = apple->geom_to_frag_us; ++ boot_us = apple->boot_us; ++ prepare_us = apple->prepare_us; ++ memcpy(ioctl_us, apple->ioctl_us, sizeof(ioctl_us)); + irq_on = apple->fw_irq_on; + spin_unlock_irqrestore(&apple->fence_lock, flags); + mutex_unlock(&apple->submit_lock); +@@ -726,19 +778,106 @@ static int pvr_apple_jobs_show(struct seq_file *s, void *unused) + for (engine = 0; engine < APPLE_ENGINES; engine++) + seq_printf(s, "%s submitted %u done %u pending %u\n", names[engine], + submitted[engine], done[engine], pending[engine]); ++ for (engine = 0; engine < APPLE_ENGINES; engine++) ++ seq_printf(s, "%s job_us last %llu max %llu avg %llu n %llu\n", names[engine], ++ job_last[engine], job_max[engine], ++ job_n[engine] ? div64_u64(job_sum[engine], job_n[engine]) : 0, job_n[engine]); + seq_printf(s, "retired_total %llu\nbusy_ns %llu\n", retired_total, busy_ns); + seq_printf(s, "fault %u\n", fault); + seq_printf(s, "runtime_pm_status %s\n", runtime_status); + seq_printf(s, "fw_irq %s\n", !apple->fw_irq ? "none" : irq_on ? "on" : "off"); + ++ /* Where a job's time goes, in us; statistics only */ ++ for (engine = 0; engine < APPLE_ENGINES; engine++) { ++ char name[32]; ++ ++ snprintf(name, sizeof(name), "%s wait_us", names[engine]); ++ pvr_apple_stat_show(s, name, &wait_us[engine]); ++ snprintf(name, sizeof(name), "%s run_us", names[engine]); ++ pvr_apple_stat_show(s, name, &run_us[engine]); ++ } ++ pvr_apple_stat_show(s, "kick_to_irq_us", &irq_us); ++ pvr_apple_stat_show(s, "irq_to_signal_us", &irq_to_signal_us); ++ pvr_apple_stat_show(s, "geometry_signal_to_fragment_run_us", &geom_to_frag_us); ++ pvr_apple_stat_show(s, "firmware_start_us", &boot_us); ++ pvr_apple_stat_show(s, "chip_prepare_us", &prepare_us); ++ for (engine = 0; engine < APPLE_IOCTLS; engine++) ++ pvr_apple_stat_show(s, ioctl_names[engine], &ioctl_us[engine]); ++ seq_printf(s, "irqs %lld\npolls %lld\n", (long long)atomic64_read(&apple->irqs), ++ (long long)atomic64_read(&apple->polls)); ++ seq_printf(s, "retired after_irq %llu after_irq_recheck %llu by_poll %llu; interrupts that found nothing %llu\n", ++ retired_after_irq, retired_after_irq_recheck, retired_by_poll, irq_empty); ++ pvr_apple_stat_show(s, "irq_recheck_to_signal_us", &irq_recheck_to_signal_us); ++ pvr_apple_stat_show(s, "irq_to_check_us", &irq_to_check_us); ++ ++ /* What is alive now, for leak checks */ ++ xa_for_each(&pvr_dev->job_ids, id, job) ++ jobs++; ++ /* vm_ctxs includes the driver's own: the kernel VM, and a chip's (the A8's park VM) */ ++ seq_printf(s, "live jobs %lu vm_ctxs %d (driver's included) gem_objs %d gem_bytes %lld mmu_pages %d\n", ++ jobs, atomic_read(&pvr_dev->live.vm_ctxs), ++ atomic_read(&pvr_dev->live.gem_objs), ++ (long long)atomic64_read(&pvr_dev->live.gem_bytes), ++ atomic_read(&pvr_dev->live.mmu_pages)); ++ if (apple->chip->debugfs_show) ++ apple->chip->debugfs_show(apple, s); ++ + return 0; + } +-DEFINE_SHOW_ATTRIBUTE(pvr_apple_jobs); ++static int pvr_apple_jobs_open(struct inode *inode, struct file *file) ++{ ++ return single_open(file, pvr_apple_jobs_show, inode->i_private); ++} ++ ++/* Any write zeroes the statistics (not the counts of what is alive) */ ++static ssize_t pvr_apple_jobs_write(struct file *file, const char __user *buf, size_t len, ++ loff_t *ppos) ++{ ++ struct pvr_apple *apple = ((struct seq_file *)file->private_data)->private; ++ int engine; ++ ++ spin_lock_irq(&apple->fence_lock); ++ for (engine = 0; engine < APPLE_ENGINES; engine++) { ++ apple->job_us_last[engine] = 0; ++ apple->job_us_max[engine] = 0; ++ apple->job_us_sum[engine] = 0; ++ apple->job_us_n[engine] = 0; ++ } ++ memset(apple->wait_us, 0, sizeof(apple->wait_us)); ++ memset(apple->run_us, 0, sizeof(apple->run_us)); ++ memset(&apple->irq_us, 0, sizeof(apple->irq_us)); ++ memset(&apple->irq_to_signal_us, 0, sizeof(apple->irq_to_signal_us)); ++ memset(&apple->geom_to_frag_us, 0, sizeof(apple->geom_to_frag_us)); ++ memset(&apple->boot_us, 0, sizeof(apple->boot_us)); ++ memset(&apple->prepare_us, 0, sizeof(apple->prepare_us)); ++ memset(apple->ioctl_us, 0, sizeof(apple->ioctl_us)); ++ apple->retired_after_irq = 0; ++ apple->retired_by_poll = 0; ++ apple->irq_empty = 0; ++ apple->retired_after_irq_recheck = 0; ++ memset(&apple->irq_to_check_us, 0, sizeof(apple->irq_to_check_us)); ++ memset(&apple->irq_recheck_to_signal_us, 0, sizeof(apple->irq_recheck_to_signal_us)); ++ if (apple->chip->debugfs_reset) ++ apple->chip->debugfs_reset(apple); ++ atomic64_set(&apple->irqs, 0); ++ atomic64_set(&apple->polls, 0); ++ spin_unlock_irq(&apple->fence_lock); ++ return len; ++} ++ ++static const struct file_operations pvr_apple_jobs_fops = { ++ .owner = THIS_MODULE, ++ .open = pvr_apple_jobs_open, ++ .read = seq_read, ++ .write = pvr_apple_jobs_write, ++ .llseek = seq_lseek, ++ .release = single_release, ++}; + + void pvr_apple_debugfs_init(struct pvr_device *pvr_dev, struct dentry *root) + { + if (pvr_dev->apple) +- debugfs_create_file("apple_jobs", 0400, root, pvr_dev->apple, ++ debugfs_create_file("apple_jobs", 0600, root, pvr_dev->apple, + &pvr_apple_jobs_fops); + } + +@@ -865,6 +1004,38 @@ static const struct dma_fence_ops pvr_apple_fence_ops = { + .get_timeline_name = pvr_apple_fence_timeline_name, + }; + ++static enum pvr_apple_engine pvr_apple_job_engine(const struct pvr_job *job) ++{ ++ return job->type == DRM_PVR_JOB_TYPE_GEOMETRY ? APPLE_ENGINE_GEOM : ++ job->type == DRM_PVR_JOB_TYPE_FRAGMENT ? APPLE_ENGINE_FRAG : APPLE_ENGINE_COMPUTE; ++} ++ ++/** ++ * pvr_apple_job_ran() - Account a job's run_job (statistics only) ++ * @job: The job. ++ * @start: When its run_job began. ++ * ++ * Its wait from arming to run_job, and the time run_job took. ++ */ ++void pvr_apple_job_ran(struct pvr_job *job, ktime_t start) ++{ ++ struct pvr_apple *apple = to_apple(job->pvr_dev); ++ enum pvr_apple_engine engine = pvr_apple_job_engine(job); ++ ktime_t now = ktime_get(); ++ unsigned long flags; ++ ++ spin_lock_irqsave(&apple->fence_lock, flags); ++ if (job->apple_armed) ++ pvr_apple_stat_add(&apple->wait_us[engine], ktime_us_delta(start, job->apple_armed)); ++ pvr_apple_stat_add(&apple->run_us[engine], ktime_us_delta(now, start)); ++ if (engine == APPLE_ENGINE_FRAG && apple->geom_signalled) { ++ pvr_apple_stat_add(&apple->geom_to_frag_us, ++ ktime_us_delta(start, apple->geom_signalled)); ++ apple->geom_signalled = 0; ++ } ++ spin_unlock_irqrestore(&apple->fence_lock, flags); ++} ++ + bool pvr_apple_fence_faulted(struct dma_fence *base) + { + struct pvr_apple_fence *fence; +@@ -889,6 +1060,7 @@ void pvr_apple_fence_bind(struct pvr_device *pvr_dev, struct dma_fence *base, + spin_lock_irqsave(&apple->fence_lock, flags); + fence->sched = sched; + fence->submitted = ktime_get(); ++ WRITE_ONCE(apple->kick, fence->submitted); + spin_unlock_irqrestore(&apple->fence_lock, flags); + } + +@@ -1207,9 +1379,17 @@ void pvr_apple_firmware_progress(struct pvr_apple *apple) + static irqreturn_t pvr_apple_firmware_irq(int irq, void *data) + { + struct pvr_apple *apple = data; ++ ktime_t kick = READ_ONCE(apple->kick), now = ktime_get(); + + /* At its source first, as Apple's kext does before it reads the rings */ + apple->chip->irq_ack(apple); ++ atomic64_inc(&apple->irqs); ++ if (kick) { ++ WRITE_ONCE(apple->kick, 0); ++ pvr_apple_stat_add(&apple->irq_us, ktime_us_delta(now, kick)); ++ } ++ if (!READ_ONCE(apple->irq_at)) ++ WRITE_ONCE(apple->irq_at, now); + if (apple->chip->irq_thread) + return READ_ONCE(apple->stopping) ? IRQ_HANDLED : IRQ_WAKE_THREAD; + pvr_apple_firmware_event(apple); +@@ -1287,15 +1467,18 @@ static void pvr_apple_check(struct pvr_apple *apple, bool from_irq) + struct pvr_apple_fence *fence, *tmp; + unsigned int retired = 0, us; + bool pending = false, irq_on; ++ ktime_t now, irq_at; + int engine; + + if (READ_ONCE(apple->stopping)) + return; ++ atomic64_inc(&apple->polls); + if (!apple->chip->events) + pvr_apple_ack_events(apple); + if (atomic_xchg(&apple->fault_pending, 0)) + pvr_apple_fault_pending(apple); + spin_lock_irq(&apple->fence_lock); ++ now = ktime_get(); + for (engine = 0; engine < APPLE_ENGINES; engine++) { + u32 done = pvr_apple_engine_done(apple, engine); + +@@ -1315,12 +1498,65 @@ static void pvr_apple_check(struct pvr_apple *apple, bool from_irq) + if ((s32)(done - (u32)fence->base.seqno) < 0) + break; + list_del_init(&fence->node); ++ if (fence->submitted) { ++ u64 job_us = ktime_us_delta(ktime_get(), fence->submitted); ++ ++ apple->job_us_last[engine] = job_us; ++ apple->job_us_max[engine] = max(apple->job_us_max[engine], job_us); ++ apple->job_us_sum[engine] += job_us; ++ apple->job_us_n[engine]++; ++ } ++ if (apple->chip->job_retired) ++ apple->chip->job_retired(apple, engine, (u32)fence->base.seqno); + dma_fence_signal_locked(&fence->base); + dma_fence_put(&fence->base); + retired++; ++ if (engine == APPLE_ENGINE_GEOM) ++ apple->geom_signalled = now; + } + pending |= !list_empty(&apple->pending[engine]); + } ++ /* statistics: the interrupt that led to these signals; none kept once idle */ ++ /* ++ * Statistics: an interrupt that came before this check is this ++ * check's, whether it found a job or not (one whose job a poll had ++ * retired first must not be charged to the next job); one that came ++ * while it ran (after @now) is the next check's. ++ */ ++ irq_at = READ_ONCE(apple->irq_at); ++ if (irq_at && !ktime_after(irq_at, now)) { ++ WRITE_ONCE(apple->irq_at, 0); ++ pvr_apple_stat_add(&apple->irq_to_check_us, ktime_us_delta(now, irq_at)); ++ if (retired) { ++ pvr_apple_stat_add(&apple->irq_to_signal_us, ++ ktime_us_delta(ktime_get(), irq_at)); ++ apple->retired_after_irq += retired; ++ } else { ++ apple->irq_empty++; ++ /* the checks it starts are its own (below) */ ++ if (from_irq) ++ apple->irq_recheck_at = irq_at; ++ } ++ if (apple->chip->irq_checked) { ++ apple->chip->irq_checked(apple, retired, apple->retired_since_irq); ++ apple->retired_since_irq = false; ++ } ++ } else if (retired && apple->irq_recheck_at) { ++ /* found by the re-check an interrupt that came too early started */ ++ pvr_apple_stat_add(&apple->irq_recheck_to_signal_us, ++ ktime_us_delta(ktime_get(), apple->irq_recheck_at)); ++ apple->irq_recheck_at = 0; ++ apple->retired_after_irq_recheck += retired; ++ apple->retired_since_irq = true; ++ } else if (retired) { ++ apple->retired_by_poll += retired; ++ apple->retired_since_irq = true; ++ } ++ if (!pending) { ++ WRITE_ONCE(apple->kick, 0); ++ WRITE_ONCE(apple->irq_at, 0); ++ apple->irq_recheck_at = 0; ++ } + if (!pending && apple->busy_since) { + apple->busy_ns += ktime_to_ns(ktime_sub(ktime_get(), apple->busy_since)); + apple->busy_since = 0; +@@ -1842,6 +2078,8 @@ void pvr_apple_job_armed(struct pvr_job *job, struct dma_fence *finished) + bool joint = apple->chip->scenes > 1 || (apple->chip->joint && apple->chip->joint(apple)); + struct dma_fence **last, *old; + ++ job->apple_armed = ktime_get(); /* statistics: pvr_apple_job_ran() */ ++ + if (job->type == DRM_PVR_JOB_TYPE_GEOMETRY) + last = &apple->last_geom; + else if (job->type == DRM_PVR_JOB_TYPE_FRAGMENT) +@@ -2355,6 +2593,32 @@ bool pvr_apple_vm_evictable(struct pvr_device *pvr_dev, struct pvr_vm_context *v + int pvr_apple_job_prepare_resources(struct pvr_job *job) + { + struct pvr_apple *apple = to_apple(job->pvr_dev); ++ ktime_t start = ktime_get(); ++ int err; + +- return apple->chip->prepare_job ? apple->chip->prepare_job(apple, job) : 0; ++ if (!apple->chip->prepare_job) ++ return 0; ++ err = apple->chip->prepare_job(apple, job); ++ spin_lock_irq(&apple->fence_lock); ++ pvr_apple_stat_add(&apple->prepare_us, ktime_us_delta(ktime_get(), start)); ++ spin_unlock_irq(&apple->fence_lock); ++ return err; ++} ++ ++/** ++ * pvr_apple_ioctl_time() - Account an ioctl's time (statistics only) ++ * @pvr_dev: Target device. ++ * @ioctl: Which one. ++ * @start: When it began. ++ */ ++void pvr_apple_ioctl_time(struct pvr_device *pvr_dev, enum pvr_apple_ioctl ioctl, ktime_t start) ++{ ++ struct pvr_apple *apple = pvr_dev->apple; ++ unsigned long flags; ++ ++ if (!apple || ioctl >= APPLE_IOCTLS) ++ return; ++ spin_lock_irqsave(&apple->fence_lock, flags); ++ pvr_apple_stat_add(&apple->ioctl_us[ioctl], ktime_us_delta(ktime_get(), start)); ++ spin_unlock_irqrestore(&apple->fence_lock, flags); + } +diff --git a/drivers/gpu/drm/imagination/pvr_apple.h b/drivers/gpu/drm/imagination/pvr_apple.h +index 752e1a7290..8bf106e042 100644 +--- a/drivers/gpu/drm/imagination/pvr_apple.h ++++ b/drivers/gpu/drm/imagination/pvr_apple.h +@@ -162,8 +162,21 @@ struct pvr_apple_g5p_cmd_compute { + struct pvr_apple_g5p_compute_regs g5p; + }; + ++/* The ioctls whose time debugfs apple_jobs shows (pvr_apple_ioctl_time()) */ ++enum pvr_apple_ioctl { ++ APPLE_IOCTL_CREATE_BO, ++ APPLE_IOCTL_VM_MAP, ++ APPLE_IOCTL_CREATE_VM_CONTEXT, ++ APPLE_IOCTL_CREATE_CONTEXT, ++ APPLE_IOCTL_CREATE_FREE_LIST, ++ APPLE_IOCTL_CREATE_HWRT_DATASET, ++ APPLE_IOCTL_SUBMIT_JOBS, ++ APPLE_IOCTLS ++}; ++ + #if IS_ENABLED(CONFIG_DRM_POWERVR_APPLE) + void pvr_apple_debugfs_init(struct pvr_device *pvr_dev, struct dentry *root); ++void pvr_apple_ioctl_time(struct pvr_device *pvr_dev, enum pvr_apple_ioctl ioctl, ktime_t start); + int pvr_apple_fw_init(struct pvr_device *pvr_dev); + void pvr_apple_fw_fini(struct pvr_device *pvr_dev); + struct pvr_gem_object *pvr_apple_pm_pages(struct pvr_device *pvr_dev, size_t size); +@@ -203,6 +216,8 @@ int pvr_apple_queue_job_init(struct pvr_job *job, u64 drm_client_id); + void pvr_apple_queue_job_cleanup(struct pvr_job *job); + #else + static inline void pvr_apple_debugfs_init(struct pvr_device *pvr_dev, struct dentry *root) {} ++static inline void pvr_apple_ioctl_time(struct pvr_device *pvr_dev, enum pvr_apple_ioctl ioctl, ++ ktime_t start) {} + static inline int pvr_apple_fw_init(struct pvr_device *pvr_dev) { return -ENODEV; } + static inline void pvr_apple_fw_fini(struct pvr_device *pvr_dev) {} + static inline struct pvr_gem_object *pvr_apple_pm_pages(struct pvr_device *pvr_dev, size_t size) +diff --git a/drivers/gpu/drm/imagination/pvr_apple_chip.h b/drivers/gpu/drm/imagination/pvr_apple_chip.h +index 2a94dd8d26..4c4583080f 100644 +--- a/drivers/gpu/drm/imagination/pvr_apple_chip.h ++++ b/drivers/gpu/drm/imagination/pvr_apple_chip.h +@@ -79,6 +79,19 @@ struct pvr_apple_layout { + + struct seq_file; + ++/* A time in microseconds: the last, the largest, the sum and the count */ ++struct pvr_apple_stat { ++ u64 last, max, sum, n; ++}; ++ ++static inline void pvr_apple_stat_add(struct pvr_apple_stat *stat, u64 us) ++{ ++ stat->last = us; ++ stat->max = max(stat->max, us); ++ stat->sum += us; ++ stat->n++; ++} ++ + struct pvr_apple { + struct pvr_device *pvr_dev; + const struct pvr_apple_chip *chip; +@@ -157,6 +170,50 @@ struct pvr_apple { + ktime_t busy_since; + u64 busy_ns; + u64 retired_total; /* retired or cancelled jobs, fence_lock */ ++ /* per engine: run (fence bound) to fence signal, in us; fence_lock */ ++ u64 job_us_last[APPLE_ENGINES], job_us_max[APPLE_ENGINES]; ++ u64 job_us_sum[APPLE_ENGINES], job_us_n[APPLE_ENGINES]; ++ /* ++ * Where else a job's time goes (debugfs apple_jobs), statistics only. ++ * Per engine: from the job's arming at submit to its run_job (its ++ * dependencies and the scheduler), and run_job itself (the power ++ * reference, the chip's prepare and submit with the doorbell); under ++ * fence_lock. The firmware's interrupt: how many were taken, and the ++ * time from the last job bound (@kick) to the next one, written in ++ * hard IRQ context without a lock. Completion checks run (@polls). ++ */ ++ struct pvr_apple_stat wait_us[APPLE_ENGINES], run_us[APPLE_ENGINES]; ++ struct pvr_apple_stat irq_us; ++ ktime_t kick; ++ atomic64_t irqs, polls; ++ /* ++ * More of the same, under fence_lock: an interrupt to the fence ++ * signals it led to (@irq_at, 0 once used); a geometry job's signal ++ * (@geom_signalled) to the next fragment job's run_job; the firmware ++ * starts a job's run_job paid (pvr_apple_prepare_job()); the chip's ++ * job preparation in the submitting process; and the ioctls in ++ * enum pvr_apple_ioctl. Writing apple_jobs zeroes them all. ++ */ ++ ktime_t irq_at, geom_signalled; ++ struct pvr_apple_stat irq_to_signal_us, geom_to_frag_us, boot_us, prepare_us; ++ struct pvr_apple_stat ioctl_us[APPLE_IOCTLS]; ++ /* ++ * Jobs retired by a check an interrupt led to, and by the backup ++ * poll; interrupts whose check found nothing; the interrupt to its ++ * check (the work item's or the thread's wake-up). ++ */ ++ u64 retired_after_irq, retired_by_poll, irq_empty; ++ struct pvr_apple_stat irq_to_check_us; ++ /* ++ * Jobs found by the re-checks an interrupt started after finding ++ * nothing (its job's stamp not yet in memory), and that interrupt to ++ * their signal; fence_lock. ++ */ ++ ktime_t irq_recheck_at; ++ u64 retired_after_irq_recheck; ++ /* a check without an interrupt retired a job since the last interrupt's */ ++ bool retired_since_irq; ++ struct pvr_apple_stat irq_recheck_to_signal_us; + + ktime_t stat_since; + +@@ -261,10 +318,18 @@ struct pvr_apple { + * @max_power_state: Highest state licensed for fault capture; optional. + * @report_fault: Capture the MMU fault state after a firmware fault, in place + * of the licence-checked bank read (pvr_apple_report_mmu_fault()); optional. ++ * @debugfs_show: More lines for debugfs apple_jobs; optional. ++ * @debugfs_reset: Zero the chip's statistics with apple_jobs'; optional. ++ * @job_retired: Job @seq of @engine left the pending list, under fence_lock ++ * with interrupts off (statistics); optional. + * @poll_us: The backup check period with the interrupt taken, in place of + * @irq_poll_us (a tunable); optional. + * @recheck_us: After an interrupt whose check found nothing, the delay of + * the first re-check, in place of the fast period; optional. ++ * @irq_checked: The check an interrupt started is done: @retired jobs found, ++ * @polled whether a check without an interrupt retired one since the ++ * previous interrupt's; before the rings are drained, under fence_lock, ++ * statistics only; optional. + * @events: Read what the firmware posted. Before @engine_done wherever a job + * is waited for; may sleep. + * @vm_init: A userspace VM was created: map what the firmware needs in it. +@@ -320,8 +385,12 @@ struct pvr_apple_chip { + void (*write_frag)(struct pvr_apple *apple, u32 seq, const void *hw); + void (*report_hang)(struct pvr_apple *apple); + void (*report_fault)(struct pvr_apple *apple); ++ void (*debugfs_show)(struct pvr_apple *apple, struct seq_file *s); ++ void (*debugfs_reset)(struct pvr_apple *apple); ++ void (*job_retired)(struct pvr_apple *apple, enum pvr_apple_engine engine, u32 seq); + unsigned int (*poll_us)(struct pvr_apple *apple); + unsigned int (*recheck_us)(struct pvr_apple *apple); ++ void (*irq_checked)(struct pvr_apple *apple, unsigned int retired, bool polled); + + int (*prepare_job)(struct pvr_apple *apple, struct pvr_job *job); + int (*submit)(struct pvr_apple *apple, struct pvr_job *job); +@@ -367,6 +436,7 @@ static inline struct device *apple_dev(struct pvr_apple *apple) + void pvr_apple_report_mmu_fault(struct pvr_apple *apple); + void pvr_apple_firmware_fault(struct pvr_apple *apple); + bool pvr_apple_fence_faulted(struct dma_fence *fence); ++void pvr_apple_job_ran(struct pvr_job *job, ktime_t start); + void pvr_apple_fence_bind(struct pvr_device *pvr_dev, struct dma_fence *fence, + struct drm_gpu_scheduler *sched); + int pvr_apple_fw_load(struct pvr_apple *apple, size_t max_size); +diff --git a/drivers/gpu/drm/imagination/pvr_apple_queue.c b/drivers/gpu/drm/imagination/pvr_apple_queue.c +index 8e511cc270..a4daad7a07 100644 +--- a/drivers/gpu/drm/imagination/pvr_apple_queue.c ++++ b/drivers/gpu/drm/imagination/pvr_apple_queue.c +@@ -48,6 +48,7 @@ static struct dma_fence *pvr_apple_submit(struct pvr_job *job) + static struct dma_fence *pvr_apple_run_job(struct drm_sched_job *sched_job) + { + struct pvr_job *job = container_of(sched_job, struct pvr_job, base); ++ ktime_t start = ktime_get(); + struct dma_fence *fence; + int err; + +@@ -60,6 +61,8 @@ static struct dma_fence *pvr_apple_run_job(struct drm_sched_job *sched_job) + pvr_power_put(job->pvr_dev); + else + pvr_apple_fence_bind(job->pvr_dev, fence, sched_job->sched); ++ if (!IS_ERR(fence)) ++ pvr_apple_job_ran(job, start); + return fence; + } + +diff --git a/drivers/gpu/drm/imagination/pvr_apple_s8003.c b/drivers/gpu/drm/imagination/pvr_apple_s8003.c +index 977d36cb56..c751de6bf3 100644 +--- a/drivers/gpu/drm/imagination/pvr_apple_s8003.c ++++ b/drivers/gpu/drm/imagination/pvr_apple_s8003.c +@@ -52,6 +52,7 @@ + #include "pvr_vm.h" + + #include ++#include + #include + #include + #include +@@ -325,7 +326,36 @@ struct s8003_pm { + bool clean; /* both of its jobs were handed to the firmware */ + }; + ++/* ++ * Why a job was refused (s8003_submit_error(), t7000_prepare_job()), for the ++ * counters in debugfs apple_jobs. Names as printed there. ++ */ ++enum s8003_refusal { ++ S8003_REFUSED_NOT_PREPARED, /* geometry: no region ring or PM objects */ ++ S8003_REFUSED_NO_GEOM, /* fragment: its geometry job failed or was not submitted */ ++ S8003_REFUSED_RT, /* the target's geometry */ ++ S8003_REFUSED_PM, /* no PM set, or its pool or set not built */ ++ S8003_REFUSED_SLOT_WAIT, /* the engine's record slot stayed busy */ ++ S8003_REFUSED_PUBLISH, /* the firmware's ring was not ours to publish to */ ++ S8003_REFUSED_FILL, /* the 3D record */ ++ S8003_REFUSED_PREPARE_RT, /* at the ioctl: the target */ ++ S8003_REFUSED_PREPARE_SAMPLES, /* at the ioctl: more than one sample */ ++ S8003_REFUSED_PREPARE_RING, /* at the ioctl: region headers not a mirrored ring */ ++ S8003_REFUSALS ++}; + ++static const char *const s8003_refusal_name[S8003_REFUSALS] = { ++ [S8003_REFUSED_NOT_PREPARED] = "not_prepared", ++ [S8003_REFUSED_NO_GEOM] = "frag_no_geom", ++ [S8003_REFUSED_RT] = "rt_derive", ++ [S8003_REFUSED_PM] = "pm", ++ [S8003_REFUSED_SLOT_WAIT] = "slot_wait", ++ [S8003_REFUSED_PUBLISH] = "publish", ++ [S8003_REFUSED_FILL] = "fill", ++ [S8003_REFUSED_PREPARE_RT] = "prepare_rt", ++ [S8003_REFUSED_PREPARE_SAMPLES] = "prepare_samples", ++ [S8003_REFUSED_PREPARE_RING] = "prepare_ring", ++}; + + struct s8003_vm { + struct pvr_vm_context *vm_ctx; +@@ -354,6 +384,30 @@ struct s8003_vm { + } t7000; + }; + ++/* ++ * An A8 engine's jobs, phase by phase, in us (debugfs apple_jobs "a8 ++ * phases"), over the jobs whose firmware timestamps were usable, so that ++ * host + wait + run + tail = total for each job: from the chip's submit to ++ * the fence's signal. Inside host: the PM pool's feedback and build ++ * (geometry), the wait for the engine's record slot, the wait for the work ++ * ring's slot. Inside wait (doorbell to the firmware's start of the work): ++ * the doorbells that found the GPU awake and those that found Fender in ++ * state 0 (powered down, woken by the doorbell); a fragment job kicked ++ * before its geometry job's end instead: its wait for that end and the ++ * firmware's hand-off from there to the 3D's start. Inside tail (the ++ * firmware's end to the signal): ended by a check that the firmware's ++ * interrupt led to (end to interrupt, interrupt to signal), or by a check ++ * without one (the backup poll or a re-check). ++ */ ++struct t7000_phases { ++ struct pvr_apple_stat total, host, wait, run, tail; ++ struct pvr_apple_stat pm, slot, ring; ++ struct pvr_apple_stat wait_awake, wait_woke, ta_wait, ta_to_3d; ++ struct pvr_apple_stat end_to_irq, irq_to_signal, end_to_check; ++ /* the firmware's stamped start (+8): none, or more than 2 us from end - result */ ++ u64 start_stamp_none, start_stamp_off; ++}; ++ + struct pvr_apple_s8003 { + struct pvr_apple *apple; + const struct s8003_variant *v; +@@ -425,13 +479,52 @@ struct pvr_apple_s8003 { + u32 t7000_trace; /* the records' trace ID, one per job */ + + struct s8003_idle idle; ++ + /* The A8: the smallest PM pool a stall taught; the VM of the last geometry job */ + u32 t7000_pm_floor; + struct s8003_vm *t7000_geom_vm; + bool t7000_mmu_arm; /* a lockup event came from the MMU-fault arm */ ++ /* ++ * The A8's job timeline (debugfs apple_jobs), per engine: the system ++ * counter at the doorbell; the firmware's own timestamps of the job ++ * (start, end), read once when the job retires; under fence_lock. ++ */ ++ u64 t7000_kick[APPLE_ENGINES]; ++ struct pvr_apple_stat t7000_fw_wait_us[APPLE_ENGINES], t7000_fw_run_us[APPLE_ENGINES]; ++ struct pvr_apple_stat t7000_fw_tail_us[APPLE_ENGINES]; ++ u64 t7000_fw_bad[APPLE_ENGINES], t7000_pm_builds; ++ /* ++ * Where each job's time goes (t7000_phases()), in system counter ticks: ++ * set at submit under submit_lock (the A8 has one job per engine with ++ * the firmware, and an engine's next job is submitted only after this ++ * one retired), read when it retires under fence_lock. The geometry ++ * job's firmware end, for the hand-off to its fragment job. ++ */ ++ u64 t7000_submit_at[APPLE_ENGINES]; ++ u32 t7000_pm_ticks[APPLE_ENGINES], t7000_slot_ticks[APPLE_ENGINES]; ++ u32 t7000_ring_ticks[APPLE_ENGINES]; ++ bool t7000_kick_woke[APPLE_ENGINES]; ++ u32 t7000_geom_end; ++ struct t7000_phases t7000_ph[APPLE_ENGINES]; + /* The 3D record the last fragment job was written to (T7000_RCH_W or T7000_RCH1_W) */ + u32 t7000_frag_w; +- ++ /* ++ * The A8's firmware interrupts by what the check that took each one ++ * found (t7000_irq_checked()): its job, or nothing after a poll had ++ * retired a job since the previous interrupt, or nothing with a stamp ++ * event posted, or with only log/statistics/trace entries, or with ++ * nothing new at all; under fence_lock. ++ */ ++ u64 t7000_irq_found, t7000_irq_polled, t7000_irq_stamp_event, t7000_irq_rings, t7000_irq_none; ++ /* stamp events posted and jobs retired (statistics), their difference (kept) */ ++ u64 t7000_stamp_events, t7000_jobs_retired; ++ s64 t7000_ev_lead; ++ u32 t7000_ev_cursor, t7000_ev_gen; /* the next event to count, for start @t7000_ev_gen */ ++ ++ /* Statistics (debugfs apple_jobs): live per-VM states and A8 PM objects, doorbells */ ++ atomic_t vms_live, pm_objs_live; ++ atomic64_t doorbells, doorbell_wakes, doorbell_pll_unlocked, doorbell_refused; ++ atomic64_t refused[S8003_REFUSALS]; /* jobs refused, by reason */ + bool bif_read; /* t7000_report_fault() read the fault registers */ + }; + +@@ -1696,6 +1789,8 @@ static int pvr_apple_s8003_vm_init(struct pvr_apple *apple, struct pvr_vm_contex + err = xa_err(xa_store(&s->vms, (unsigned long)vm_ctx, vm, GFP_KERNEL)); + if (err) + kfree(vm); ++ else ++ atomic_inc(&s->vms_live); + return err; + } + +@@ -1818,6 +1913,7 @@ static void pvr_apple_s8003_vm_fini(struct pvr_apple *apple, struct pvr_vm_conte + mutex_unlock(&apple->submit_lock); + if (s->v->vm_unmap) + s->v->vm_unmap(apple, vm_ctx); ++ atomic_dec(&s->vms_live); + kfree(vm); + } + +@@ -2592,8 +2688,12 @@ out: + } + + static int s8003_submit_error(struct pvr_apple *apple, struct s8003_vm *vm, +- const char *engine, int err, const char *reason) ++ const char *engine, int err, enum s8003_refusal why, ++ const char *reason) + { ++ struct pvr_apple_s8003 *s = apple->chip_data; ++ ++ atomic64_inc(&s->refused[why]); + dev_warn_ratelimited(apple_dev(apple), + "%s job not submitted: error=%d VM=%p: %s\n", + engine, err, vm->vm_ctx, reason); +@@ -2614,7 +2714,8 @@ static int s8003_submit_geom(struct pvr_apple *apple, struct s8003_vm *vm, struc + + err = s8003_rt_derive(hwrt, &rt); + if (err) +- return s8003_submit_error(apple, vm, "geometry", err, "rt derive"); ++ return s8003_submit_error(apple, vm, "geometry", err, S8003_REFUSED_RT, ++ "rt derive"); + /* The VM's sets were taken when the job was ordered (pvr_apple_s8003_vm_scenes()) */ + for (i = 0; i < 2 && !pm; i++) + if (vm->pm[i].set >= 0 && vm->pm[i].ready && +@@ -2629,7 +2730,8 @@ static int s8003_submit_geom(struct pvr_apple *apple, struct s8003_vm *vm, struc + !s8003_wait_done(apple, APPLE_ENGINE_GEOM, vm->pm[i].ta_seq)) + pm = &vm->pm[i]; + if (!pm) +- return s8003_submit_error(apple, vm, "geometry", -EBUSY, "no PM set"); ++ return s8003_submit_error(apple, vm, "geometry", -EBUSY, S8003_REFUSED_PM, ++ "no PM set"); + + /* + * Take the firmware's feedback. The worker does not allocate: a larger +@@ -2646,12 +2748,14 @@ static int s8003_submit_geom(struct pvr_apple *apple, struct s8003_vm *vm, struc + pm->clean = false; + err = s8003_pm_build(apple, vm, pm, min(blocks, S8003_PM_BLOCKS_MAX)); + if (err) +- return s8003_submit_error(apple, vm, "geometry", err, "PM pool build failed"); ++ return s8003_submit_error(apple, vm, "geometry", err, S8003_REFUSED_PM, ++ "PM pool build failed"); + /* The record slot of the job before the last one is free again */ + if (apple->ta_seq) + err = s8003_wait_done(apple, APPLE_ENGINE_GEOM, apple->ta_seq - 1); + if (err) +- return s8003_submit_error(apple, vm, "geometry", err, "record slot wait"); ++ return s8003_submit_error(apple, vm, "geometry", err, S8003_REFUSED_SLOT_WAIT, ++ "record slot wait"); + + seq = apple->ta_seq + 1; + if (!pm_record) { +@@ -2683,7 +2787,8 @@ static int s8003_submit_geom(struct pvr_apple *apple, struct s8003_vm *vm, struc + apple->ta_seq = seq; + err = s8003_publish(apple, APPLE_ENGINE_GEOM, seq, (2 * seq) % S8003_CH_ENTRIES); + if (err) +- return s8003_submit_error(apple, vm, "geometry", err, "publish"); ++ return s8003_submit_error(apple, vm, "geometry", err, S8003_REFUSED_PUBLISH, ++ "publish"); + return seq; + } + +@@ -2702,20 +2807,23 @@ static int s8003_submit_frag(struct pvr_apple *apple, struct s8003_vm *vm, struc + if (vm->pm[i].scene == s->gen && vm->pm[i].hwrt == job->hwrt && !vm->pm[i].td_seq) + pm = &vm->pm[i]; + if (!pm) +- return s8003_submit_error(apple, vm, "fragment", -ECANCELED, ++ return s8003_submit_error(apple, vm, "fragment", -ECANCELED, S8003_REFUSED_NO_GEOM, + "its geometry job failed or was not submitted"); + err = s8003_rt_derive(job->hwrt->hwrt_dataset, &rt); + if (err) +- return s8003_submit_error(apple, vm, "fragment", err, "rt derive"); ++ return s8003_submit_error(apple, vm, "fragment", err, S8003_REFUSED_RT, ++ "rt derive"); + if (apple->frag_seq) + err = s8003_wait_done(apple, APPLE_ENGINE_FRAG, apple->frag_seq - 1); + if (err) +- return s8003_submit_error(apple, vm, "fragment", err, "record slot wait"); ++ return s8003_submit_error(apple, vm, "fragment", err, S8003_REFUSED_SLOT_WAIT, ++ "record slot wait"); + + seq = apple->frag_seq + 1; + err = s8003_fill_3d(p + s8003_slot_of(seq, true).w, job->cmd, job->hwrt, &rt); + if (err) +- return s8003_submit_error(apple, vm, "fragment", err, "3D record fill"); ++ return s8003_submit_error(apple, vm, "fragment", err, S8003_REFUSED_FILL, ++ "3D record fill"); + s8003_job_write(p, s8003_channel_addr(s, APPLE_ENGINE_FRAG), + s8003_object_addr(&s->layout, S8003_CHANNEL + 1), + s8003_meta_addr(s8003_pm_set_va(pm, 0)), pm->set + 1, vm->slot[1], true, seq, +@@ -2725,7 +2833,8 @@ static int s8003_submit_frag(struct pvr_apple *apple, struct s8003_vm *vm, struc + apple->frag_seq = seq; + err = s8003_publish(apple, APPLE_ENGINE_FRAG, seq, (2 * seq) % S8003_CH_ENTRIES); + if (err) +- return s8003_submit_error(apple, vm, "fragment", err, "publish"); ++ return s8003_submit_error(apple, vm, "fragment", err, S8003_REFUSED_PUBLISH, ++ "publish"); + pm->clean = true; + return seq; + } +@@ -2750,7 +2859,8 @@ static int s8003_submit_compute(struct pvr_apple *apple, struct s8003_vm *vm, st + + err = s8003_wait_done(apple, APPLE_ENGINE_COMPUTE, apple->last_seq); + if (err) +- return s8003_submit_error(apple, vm, "compute", err, "record slot wait"); ++ return s8003_submit_error(apple, vm, "compute", err, S8003_REFUSED_SLOT_WAIT, ++ "record slot wait"); + seq = apple->last_seq + 1; + { + const u32 useq[] = { +@@ -2797,7 +2907,8 @@ static int s8003_submit_compute(struct pvr_apple *apple, struct s8003_vm *vm, st + apple->last_seq = seq; + err = s8003_publish(apple, APPLE_ENGINE_COMPUTE, seq, seq % S8003_CH_ENTRIES); + if (err) +- return s8003_submit_error(apple, vm, "compute", err, "publish"); ++ return s8003_submit_error(apple, vm, "compute", err, S8003_REFUSED_PUBLISH, ++ "publish"); + return seq; + } + +@@ -3687,7 +3798,7 @@ static void t7000_init_build(struct pvr_apple *apple, u32 q) + #define T7000_RCH_W 0x400 + #define T7000_RCH_TA_USEQ 0xa00 + #define T7000_RCH_3D_USEQ 0xd00 +-#define T7000_RCH_TIMES 0x2800 /* the 3D record's times, 16 bytes */ ++#define T7000_RCH_TIMES 0x2800 /* the record's times: end, -, start, - */ + #define T7000_RCH_SIZE 0x3000 + /* + * The 3D channel's second record (a8_joint): its barrier command, record, +@@ -4136,44 +4247,53 @@ static void t7000_idle(struct pvr_apple *apple) + * within 1 ms). In state 1 Apple reads no PLL: the firmware's own power + * and clock changes (its idle gating, DVFS) can leave PLL5 relocking while + * the state reads 1 (iOS shows that in 30 of 200 draw rounds), and Apple +- * rings then too, and so does this driver. It ++ * rings then too. So does this driver; such doorbells are counted. It + * still does not write the register with Fender's control 0 or the snoop + * not armed in state 0 (never seen here; Apple's restart is not + * implemented): the job is then in the ring and its wait ends in the reset. + * + * Nothing here fails: a job's publish checks first (t7000_publish_check()). ++ * ++ * Return: whether the doorbell found Fender in state 0 (statistics). + */ +-static void t7000_ring(struct pvr_apple *apple, u32 dm) ++static bool t7000_ring(struct pvr_apple *apple, u32 dm) + { + struct pvr_apple_s8003 *s = apple->chip_data; +- u32 state, pll, ctrl, snoop; ++ u32 state, pll = 0, ctrl = 0, snoop; + + if (s->stuck) { ++ atomic64_inc(&s->doorbell_refused); + dev_err_ratelimited(apple_dev(apple), + "doorbell not rung: the GPU is stuck; the job waits for the reset\n"); +- return; ++ return false; + } + mb(); /* what the firmware is rung for is in memory */ + writel(FENDER_WAKE_ALL, apple->fender + FENDER_WAKE); + state = readl(apple->fender + FENDER_POWER_STATE); ++ atomic64_inc(&s->doorbells); + if (!(state & 0xf)) { ++ atomic64_inc(&s->doorbell_wakes); + ctrl = readl(apple->fender + FENDER_CTRL); + snoop = readl(apple->fender + T7000_FENDER_SNOOP); + if (!ctrl || (!(ctrl & T7000_FENDER_CTRL_SNOOP) && !(snoop & BIT(1)))) { ++ atomic64_inc(&s->doorbell_refused); + dev_err_ratelimited(apple_dev(apple), + "doorbell not rung: Fender state %#x, control %#x, snoop %#x; the job waits for the reset\n", + state, ctrl, snoop); +- return; ++ return true; + } + } else { +- /* logged only: Apple reads no PLL here */ ++ /* statistics: Apple reads no PLL here */ + pll = pmgr_read(s, T7000_PMGR_PLL5); +- if (!(pll & T7000_PLL_LOCKED)) ++ if (!(pll & T7000_PLL_LOCKED)) { ++ atomic64_inc(&s->doorbell_pll_unlocked); + dev_info_ratelimited(apple_dev(apple), + "doorbell rung with PLL5 relocking, as Apple's kext rings: Fender state %#x, PLL5 %#x\n", + state, pll); ++ } + } + pvr_cr_write32(apple->pvr_dev, ROGUE_CR_MTS_SCHEDULE, ROGUE_CR_MTS_SCHEDULE_TASK_COUNTED | dm); ++ return !(state & 0xf); + } + + /* The variant's doorbell, for device-control commands */ +@@ -4293,7 +4413,8 @@ static void t7000_publish(struct pvr_apple *apple, enum pvr_apple_engine engine, + wmb(); + WRITE_ONCE(*(__le32 *)(ctl + S8003_RING_PRODUCER), + cpu_to_le32((start + 1) % APPLE_RING_ENTRIES)); +- t7000_ring(apple, dm[fw_engine]); ++ s->t7000_kick[engine] = arch_timer_read_counter(); ++ s->t7000_kick_woke[engine] = t7000_ring(apple, dm[fw_engine]); + } + + /* The A8's compute record: the A9's with every offset from 0x10 on 8 further */ +@@ -4324,10 +4445,12 @@ static int t7000_submit_compute(struct pvr_apple *apple, struct s8003_vm *vm, st + + err = s8003_wait_done(apple, APPLE_ENGINE_COMPUTE, apple->last_seq); + if (err) +- return s8003_submit_error(apple, vm, "compute", err, "record slot wait"); ++ return s8003_submit_error(apple, vm, "compute", err, S8003_REFUSED_SLOT_WAIT, ++ "record slot wait"); + err = t7000_publish_check(apple, APPLE_ENGINE_COMPUTE); + if (err) +- return s8003_submit_error(apple, vm, "compute", err, "publish"); ++ return s8003_submit_error(apple, vm, "compute", err, S8003_REFUSED_PUBLISH, ++ "publish"); + seq = apple->last_seq + 1; + k = seq - 1; + { +@@ -4477,6 +4600,7 @@ static int t7000_pm_build(struct pvr_apple *apple, struct s8003_vm *vm) + if (!blocks || blocks > T7000_PM_BLOCKS_MAX) + return -EINVAL; + vm->t7000.built = 0; ++ s->t7000_pm_builds++; + memset(stack, 0, T7000_PM_STACK_SIZE); + /* The stack first: the pool's page numbers from index 0 (0x847fef0) */ + for (i = 0; i < pages; i++) { +@@ -4525,8 +4649,11 @@ static int t7000_pm_build(struct pvr_apple *apple, struct s8003_vm *vm) + + static void t7000_pm_obj_put(struct pvr_apple *apple, struct pvr_gem_object **obj, u8 **map) + { ++ struct pvr_apple_s8003 *s = apple->chip_data; ++ + if (!*obj) + return; ++ atomic_dec(&s->pm_objs_live); + if (*map) + pvr_gem_object_vunmap(*obj); + pvr_gem_object_put(*obj); +@@ -4545,6 +4672,8 @@ static void t7000_vm_put(struct pvr_apple *apple, struct s8003_vm *vm) + + static int t7000_pm_obj(struct pvr_apple *apple, size_t size, struct pvr_gem_object **obj, u8 **map) + { ++ struct pvr_apple_s8003 *s = apple->chip_data; ++ + *obj = pvr_gem_object_create(apple->pvr_dev, size, T7000_VM_FLAGS); + if (IS_ERR(*obj)) { + int err = PTR_ERR(*obj); +@@ -4552,6 +4681,7 @@ static int t7000_pm_obj(struct pvr_apple *apple, size_t size, struct pvr_gem_obj + *obj = NULL; + return err; + } ++ atomic_inc(&s->pm_objs_live); + *map = pvr_gem_object_vmap(*obj); + if (IS_ERR(*map)) { + int err = PTR_ERR(*map); +@@ -4618,13 +4748,17 @@ static int t7000_prepare_job(struct pvr_apple *apple, struct pvr_job *job) + dataset = job->hwrt->hwrt_dataset; + err = s8003_rt_derive(dataset, &rt); + if (err) { ++ atomic64_inc(&s->refused[S8003_REFUSED_PREPARE_RT]); + return err; + } +- if (dataset->samples != 1) ++ if (dataset->samples != 1) { ++ atomic64_inc(&s->refused[S8003_REFUSED_PREPARE_SAMPLES]); + return -EOPNOTSUPP; ++ } + slice = ALIGN(rt.header, SZ_4K); + err = t7000_rgn_ring(vm->vm_ctx, job->hwrt->data.rgn_header_dev_addr, slice, &ring); + if (err) { ++ atomic64_inc(&s->refused[S8003_REFUSED_PREPARE_RING]); + dev_warn_ratelimited(apple_dev(apple), + "geometry job refused: region headers at %#llx are not a mirrored ring of 16 slices of %#x\n", + job->hwrt->data.rgn_header_dev_addr, slice); +@@ -4860,6 +4994,14 @@ static const u32 t7000_3d_wait[24] = { + * and the microsequence: start (TA 0x23, 3D 0x25 with 23 arguments), the + * timestamps around the wait, finish (0x24, 0x26). @k is the stamp's value + * before this job; the job's finish writes k + 1. ++ * ++ * The timestamps are Apple's op 0x18 pair with Apple's operands (TA ++ * FUN_fffffff00848d3e0: {W+0x558, W+0x560, W+0x560} after 0x23, {W+0x558, ++ * W+0x560, W+0x564} after the wait; 3D {W+0x8a0, W+0x8a8, W+0x8ac}): the ++ * result word gets the work's duration, and the firmware stores the ++ * absolute start and end where the two pointer words point. Both engines ++ * point them at their channel's @times, the start at +8 and the end at +0, ++ * as the A9's s8003_job_write() points its own pair at two times per job. + */ + struct t7000_rjob { + u32 ch, w, useq, object, slot, pm_id, control, s, dep, seq, k, trace, times; +@@ -4897,6 +5039,8 @@ static void t7000_ta_write(u8 *work, u8 *useq, const struct t7000_rjob *j) + put32(work + 0x53c, j->k + 1); + put32(work + 0x54c, j->seq); + put32(work + 0x550, j->k); ++ put32(work + 0x560, j->times + 8); ++ put32(work + 0x564, j->times); + memset(useq, 0, T7000_TA_USEQ_SIZE); + useq_put(useq, words, ARRAY_SIZE(words)); + } +@@ -4936,6 +5080,7 @@ static void t7000_3d_write(u8 *work, u8 *useq, const struct t7000_rjob *j) + put32(work + 0x878, j->k + 1); + put32(work + 0x888, j->seq); + put32(work + 0x88c, j->k); ++ put32(work + 0x8a8, j->times + 8); + put32(work + 0x8ac, j->times); + memset(useq, 0, T7000_3D_USEQ_SIZE); + out = useq_put(useq, start, ARRAY_SIZE(start)); +@@ -5005,6 +5150,21 @@ static void t7000_pm_feedback(struct pvr_apple *apple, struct s8003_vm *vm) + } + } + ++/* ++ * A job's submit phases for t7000_phases(), in system counter ticks: from ++ * the chip's submit (@t0) to the PM step's end (@t1), to the record slot ++ * wait's end (@t2), to now, after the work ring's slot wait. Statistics ++ * only; submit_lock held, right before the job's publish. ++ */ ++static void t7000_submit_times(struct pvr_apple_s8003 *s, enum pvr_apple_engine engine, ++ u64 t0, u64 t1, u64 t2) ++{ ++ s->t7000_submit_at[engine] = t0; ++ s->t7000_pm_ticks[engine] = t1 - t0; ++ s->t7000_slot_ticks[engine] = t2 - t1; ++ s->t7000_ring_ticks[engine] = arch_timer_read_counter() - t2; ++} ++ + /* + * A fragment job is submitted once its geometry job is (pvr_apple_job_armed()), + * not once it finished: the firmware holds the 3D itself. Each 3D record +@@ -5052,17 +5212,20 @@ static int t7000_submit_geom(struct pvr_apple *apple, struct s8003_vm *vm, struc + struct pvr_apple_s8003 *s = apple->chip_data; + u32 addr = s8003_channel_addr(s, APPLE_ENGINE_GEOM); + u8 *p = s8003_channel(s, APPLE_ENGINE_GEOM); ++ u64 t0 = arch_timer_read_counter(), t1, t2; + struct t7000_rjob j; + struct s8003_rt rt; + u64 slice_addr; + u32 seq; + int err; + +- return s8003_submit_error(apple, vm, "geometry", -EINVAL, "not prepared"); + if (!dataset->apple_rgn.ring || !vm->t7000.stack) ++ return s8003_submit_error(apple, vm, "geometry", -EINVAL, S8003_REFUSED_NOT_PREPARED, ++ "not prepared"); + err = s8003_rt_derive(dataset, &rt); + if (err) +- return s8003_submit_error(apple, vm, "geometry", err, "rt derive"); ++ return s8003_submit_error(apple, vm, "geometry", err, S8003_REFUSED_RT, ++ "rt derive"); + /* + * Every earlier scene is done here (a geometry job waits for the last + * fragment job, pvr_apple_job_add_deps()): the pool is the firmware's +@@ -5086,19 +5249,26 @@ static int t7000_submit_geom(struct pvr_apple *apple, struct s8003_vm *vm, struc + if (vm->t7000.built != s->gen || vm->t7000.open) { + err = t7000_pm_build(apple, vm); + if (err) +- return s8003_submit_error(apple, vm, "geometry", err, "PM set build"); ++ return s8003_submit_error(apple, vm, "geometry", err, S8003_REFUSED_PM, ++ "PM set build"); + } ++ t1 = arch_timer_read_counter(); + err = s8003_wait_done(apple, APPLE_ENGINE_GEOM, apple->ta_seq); + if (err) +- return s8003_submit_error(apple, vm, "geometry", err, "record slot wait"); ++ return s8003_submit_error(apple, vm, "geometry", err, S8003_REFUSED_SLOT_WAIT, ++ "record slot wait"); ++ t2 = arch_timer_read_counter(); + err = t7000_publish_check(apple, APPLE_ENGINE_GEOM); + if (err) +- return s8003_submit_error(apple, vm, "geometry", err, "publish"); ++ return s8003_submit_error(apple, vm, "geometry", err, S8003_REFUSED_PUBLISH, ++ "publish"); ++ t7000_submit_times(s, APPLE_ENGINE_GEOM, t0, t1, t2); + + seq = apple->ta_seq + 1; + slice_addr = job->hwrt->data.rgn_header_dev_addr + + (dataset->apple_rgn.cursor & (dataset->apple_rgn.ring - 1)); + t7000_fill_ta(p + T7000_RCH_W, job->cmd, job->hwrt, &rt, slice_addr); ++ memset(p + T7000_RCH_TIMES, 0, 16); + j = (struct t7000_rjob) { + .ch = addr, .w = addr + T7000_RCH_W, .useq = addr + T7000_RCH_TA_USEQ, + .object = s8003_object_addr(&s->layout, S8003_CHANNEL), .slot = vm->slot[0], +@@ -5106,6 +5276,7 @@ static int t7000_submit_geom(struct pvr_apple *apple, struct s8003_vm *vm, struc + .s = t7000_pm_s(vm), .dep = addr + S8003_CH_DEP, .seq = seq, .k = seq - 1, + .trace = ++s->t7000_trace, .pr_ch = s8003_channel_addr(s, APPLE_ENGINE_FRAG), + .pr_index = (2 * (apple->frag_seq + 1) - 1) % T7000_RCH_ENTRIES, ++ .times = addr + T7000_RCH_TIMES, + }; + t7000_ta_write(p + T7000_RCH_W, p + T7000_RCH_TA_USEQ, &j); + put32(p + T7000_RCH_RING + ((seq - 1) % T7000_RCH_ENTRIES) * 4, j.w); +@@ -5142,6 +5313,7 @@ static int t7000_submit_frag(struct pvr_apple *apple, struct s8003_vm *vm, struc + u32 addr = s8003_channel_addr(s, APPLE_ENGINE_FRAG); + u32 ta_addr = s8003_channel_addr(s, APPLE_ENGINE_GEOM); + u8 *p = s8003_channel(s, APPLE_ENGINE_FRAG), *cmd; ++ u64 t0 = arch_timer_read_counter(), t2; + u32 cmd_off, w_off, useq_off, times_off; + struct t7000_rjob j; + struct s8003_rt rt; +@@ -5154,17 +5326,22 @@ static int t7000_submit_frag(struct pvr_apple *apple, struct s8003_vm *vm, struc + BUILD_BUG_ON(T7000_3D_CHANNEL + T7000_RCH1_TIMES + 16 > T7000_PM_CONTROL); + + if (!vm->t7000.open || vm->t7000.hwrt != job->hwrt || vm->t7000.built != s->gen) +- return s8003_submit_error(apple, vm, "fragment", -ECANCELED, ++ return s8003_submit_error(apple, vm, "fragment", -ECANCELED, S8003_REFUSED_NO_GEOM, + "its geometry job failed or was not submitted"); + err = s8003_rt_derive(job->hwrt->hwrt_dataset, &rt); + if (err) +- return s8003_submit_error(apple, vm, "fragment", err, "rt derive"); ++ return s8003_submit_error(apple, vm, "fragment", err, S8003_REFUSED_RT, ++ "rt derive"); + err = s8003_wait_done(apple, APPLE_ENGINE_FRAG, apple->frag_seq); + if (err) +- return s8003_submit_error(apple, vm, "fragment", err, "record slot wait"); ++ return s8003_submit_error(apple, vm, "fragment", err, S8003_REFUSED_SLOT_WAIT, ++ "record slot wait"); ++ t2 = arch_timer_read_counter(); + err = t7000_publish_check(apple, APPLE_ENGINE_FRAG); + if (err) +- return s8003_submit_error(apple, vm, "fragment", err, "publish"); ++ return s8003_submit_error(apple, vm, "fragment", err, S8003_REFUSED_PUBLISH, ++ "publish"); ++ t7000_submit_times(s, APPLE_ENGINE_FRAG, t0, t0, t2); + + seq = apple->frag_seq + 1; + /* +@@ -5186,7 +5363,8 @@ static int t7000_submit_frag(struct pvr_apple *apple, struct s8003_vm *vm, struc + cmd = p + cmd_off; + err = t7000_fill_3d(p + w_off, job->cmd, &rt, vm->t7000.rgn_addr); + if (err) +- return s8003_submit_error(apple, vm, "fragment", err, "3D record fill"); ++ return s8003_submit_error(apple, vm, "fragment", err, S8003_REFUSED_FILL, ++ "3D record fill"); + memset(p + times_off, 0, 16); + j = (struct t7000_rjob) { + .ch = addr, .w = addr + w_off, .useq = addr + useq_off, +@@ -5444,6 +5622,231 @@ static void t7000_report_hang(struct pvr_apple *apple) + t7000_report_bif(apple, "hang"); + } + ++/* ++ * A retired job's timeline from its record, which a later job of the ++ * engine overwrites only once this one is done (s8003_wait_done()); the ++ * record's sequence word tells. Its microsequence times the engine's work: ++ * op 0x80000018 {result, start pointer, start pointer, seq} after the start ++ * packet, op 0x18 {result, start pointer, end pointer, seq} after the wait, ++ * with Apple's operands (TA W+0x558, W+0x560, W+0x564; 3D W+0x8a0, W+0x8a8, ++ * W+0x8ac; CL W+0x280, W+0x288, W+0x28c). The result word is the work's ++ * duration in the firmware's 24 MHz time (the firmware converts the Rogue ++ * timer through its correlation and compares such times against 0x5dbf, ++ * 1 ms; PLATFORM +0x280 = 24000; the A9's same op measured 24 ticks/us) ++ * [I]: measured raw words were 0x25e..0x2e5 (TA) and 0x35a..0x424 (3D), 25..44 ++ * us. The pointer words say where the firmware stores the absolute start ++ * and end (t7000_ta_write(), t7000_3d_write(): T7000_RCH_TIMES +8 and +0, ++ * zeroed per job); measurements showed the 3D's end there in the system counter's ++ * clock. Both render engines: start_to_end from the result, kick_to_start ++ * and end_to_signal from the end, the phases of the job (t7000_phases()), ++ * and the first four whose end is missing or off by more than a second are ++ * logged raw. Memory only, under fence_lock. ++ */ ++#define T7000_FW_TICKS_PER_US 24 ++ ++/* The phases of a job whose timeline is usable (struct t7000_phases), in counter ticks */ ++static void t7000_phases(struct pvr_apple *apple, enum pvr_apple_engine engine, u32 kick, ++ u32 start, u32 end, u32 now, u32 stamped_start, ktime_t now_kt, u32 freq) ++{ ++ struct pvr_apple_s8003 *s = apple->chip_data; ++ struct t7000_phases *ph = &s->t7000_ph[engine]; ++ u32 submit = (u32)s->t7000_submit_at[engine], host = kick - submit; ++ u32 tail_us = (now - end) / freq; ++ ktime_t irq_at = READ_ONCE(apple->irq_at); ++ s64 irq_us; ++ ++ if (!stamped_start) ++ ph->start_stamp_none++; ++ else if (abs((s32)(stamped_start - start)) > 2 * (s32)freq) ++ ph->start_stamp_off++; ++ if (!s->t7000_submit_at[engine] || host / freq > USEC_PER_SEC) ++ return; ++ pvr_apple_stat_add(&ph->total, (now - submit) / freq); ++ pvr_apple_stat_add(&ph->host, host / freq); ++ pvr_apple_stat_add(&ph->wait, (start - kick) / freq); ++ pvr_apple_stat_add(&ph->run, (end - start) / freq); ++ pvr_apple_stat_add(&ph->tail, tail_us); ++ if (engine == APPLE_ENGINE_GEOM) ++ pvr_apple_stat_add(&ph->pm, s->t7000_pm_ticks[engine] / freq); ++ pvr_apple_stat_add(&ph->slot, s->t7000_slot_ticks[engine] / freq); ++ pvr_apple_stat_add(&ph->ring, s->t7000_ring_ticks[engine] / freq); ++ /* a fragment job the firmware held for its TA (a8_joint): that wait, then the hand-off */ ++ if (engine == APPLE_ENGINE_FRAG && s->t7000_geom_end && ++ (s32)(s->t7000_geom_end - kick) > 0 && (s32)(start - s->t7000_geom_end) >= 0) { ++ pvr_apple_stat_add(&ph->ta_wait, (s->t7000_geom_end - kick) / freq); ++ pvr_apple_stat_add(&ph->ta_to_3d, (start - s->t7000_geom_end) / freq); ++ } else { ++ pvr_apple_stat_add(s->t7000_kick_woke[engine] ? &ph->wait_woke : &ph->wait_awake, ++ (start - kick) / freq); ++ } ++ /* the check that retires this job: one an interrupt after the end led to, or not */ ++ irq_us = irq_at && !ktime_after(irq_at, now_kt) ? ktime_us_delta(now_kt, irq_at) : -1; ++ if (irq_us >= 0 && irq_us <= tail_us) { ++ pvr_apple_stat_add(&ph->end_to_irq, tail_us - irq_us); ++ pvr_apple_stat_add(&ph->irq_to_signal, irq_us); ++ } else { ++ pvr_apple_stat_add(&ph->end_to_check, tail_us); ++ } ++} ++ ++static void t7000_job_retired(struct pvr_apple *apple, enum pvr_apple_engine engine, u32 seq) ++{ ++ static const struct { u32 w, seq, result; } at[APPLE_ENGINES] = { ++ [APPLE_ENGINE_GEOM] = { T7000_RCH_W, 0x54c, 0x558 }, ++ [APPLE_ENGINE_FRAG] = { T7000_RCH_W, 0x888, 0x8a0 }, ++ [APPLE_ENGINE_COMPUTE] = { S8003_CH_W, 0x278, 0x280 }, ++ }; ++ struct pvr_apple_s8003 *s = apple->chip_data; ++ const u8 *p = s8003_channel(s, engine), *w = p + at[engine].w; ++ u32 times = T7000_RCH_TIMES; ++ ktime_t now_kt = ktime_get(); ++ u32 freq = arch_timer_get_cntfrq() / 1000000, now = (u32)arch_timer_read_counter(); ++ u32 kick = (u32)s->t7000_kick[engine], end, start; ++ u64 run = get_unaligned_le64(w + at[engine].result); ++ s32 wait, tail; ++ ++ s->t7000_jobs_retired++; ++ s->t7000_ev_lead--; ++ if (engine == APPLE_ENGINE_GEOM) ++ s->t7000_geom_end = 0; ++ /* the record this fragment job was written to: no later one is written before it retires */ ++ if (engine == APPLE_ENGINE_FRAG && s->t7000_frag_w == T7000_RCH1_W) { ++ w = p + T7000_RCH1_W; ++ times = T7000_RCH1_TIMES; ++ } ++ if (get32(w + at[engine].seq) != seq) ++ return; ++ if (run / T7000_FW_TICKS_PER_US <= USEC_PER_SEC) ++ pvr_apple_stat_add(&s->t7000_fw_run_us[engine], run / T7000_FW_TICKS_PER_US); ++ if (engine == APPLE_ENGINE_COMPUTE || !freq) ++ return; ++ end = get32(p + times); ++ start = end - (u32)run * (freq / T7000_FW_TICKS_PER_US ?: 1); ++ wait = start - kick; ++ tail = now - end; ++ if (!end || wait < 0 || tail < 0 || wait / freq > USEC_PER_SEC || ++ tail / freq > USEC_PER_SEC) { ++ /* the first few raw, to tell what the firmware stored and in which clock */ ++ if (s->t7000_fw_bad[engine]++ < 4) ++ dev_info(apple_dev(apple), ++ "A8 timeline raw: engine %d seq %u kick %#x now %#x (counter %u MHz); result %#llx; times %08x %08x %08x %08x\n", ++ engine, seq, kick, now, freq, run, get32(p + times), ++ get32(p + times + 4), get32(p + times + 8), ++ get32(p + times + 12)); ++ return; ++ } ++ pvr_apple_stat_add(&s->t7000_fw_wait_us[engine], wait / freq); ++ pvr_apple_stat_add(&s->t7000_fw_tail_us[engine], tail / freq); ++ if (engine == APPLE_ENGINE_GEOM) ++ s->t7000_geom_end = end; ++ t7000_phases(apple, engine, kick, start, end, now, get32(p + times + 8), ++ now_kt, freq); ++} ++ ++static void t7000_debugfs_reset(struct pvr_apple *apple) ++{ ++ struct pvr_apple_s8003 *s = apple->chip_data; ++ unsigned int i; ++ ++ if (!s) ++ return; ++ memset(s->t7000_fw_wait_us, 0, sizeof(s->t7000_fw_wait_us)); ++ memset(s->t7000_fw_run_us, 0, sizeof(s->t7000_fw_run_us)); ++ memset(s->t7000_fw_tail_us, 0, sizeof(s->t7000_fw_tail_us)); ++ memset(s->t7000_fw_bad, 0, sizeof(s->t7000_fw_bad)); ++ memset(s->t7000_ph, 0, sizeof(s->t7000_ph)); ++ s->t7000_pm_builds = 0; ++ s->t7000_irq_found = 0; ++ s->t7000_irq_polled = 0; ++ s->t7000_irq_stamp_event = 0; ++ s->t7000_irq_rings = 0; ++ s->t7000_irq_none = 0; ++ s->t7000_stamp_events = 0; ++ s->t7000_jobs_retired = 0; ++ atomic64_set(&s->doorbells, 0); ++ atomic64_set(&s->doorbell_wakes, 0); ++ atomic64_set(&s->doorbell_pll_unlocked, 0); ++ atomic64_set(&s->doorbell_refused, 0); ++ for (i = 0; i < S8003_REFUSALS; i++) ++ atomic64_set(&s->refused[i], 0); ++} ++ ++/* ++ * Which A8 interrupts find their job (&pvr_apple_chip.irq_checked), from ++ * memory only, before the rings are drained. A private cursor over the ++ * event ring counts every stamp event the firmware posts; t7000_job_retired() ++ * counts every retired job. Their running difference (never reset) tells, ++ * at an interrupt whose check found nothing, whether the firmware had posted ++ * more stamp events than jobs retired (stamp_event_ahead: the event came ++ * before the stamp it reports; the first four are logged with the event and ++ * the stamps) or not (for_retired_job: the interrupt of a job a check ++ * without an interrupt had retired first, when one had since the previous ++ * interrupt; else one with only log/statistics/trace entries, or with ++ * nothing new). Meaningful while the firmware posts one stamp event per job: ++ * stamp_events against jobs_retired shows it. Under fence_lock. ++ */ ++static void t7000_irq_checked(struct pvr_apple *apple, unsigned int retired, bool polled) ++{ ++ struct pvr_apple_s8003 *s = apple->chip_data; ++ u8 *ctl = s8003_object(s, S8003_EVENT_CTL); ++ const u8 *events = s8003_object(s, S8003_EVENTS), *stamp_event = NULL; ++ u32 producer = ring_read(ctl, S8003_RING_PRODUCER), cursor = s->t7000_ev_cursor; ++ unsigned int lane; ++ bool rings = false; ++ ++ if (producer >= APPLE_RING_ENTRIES) ++ return; ++ /* a new start of the firmware: its rings begin anew; count from here */ ++ if (s->t7000_ev_gen != s->gen || cursor >= APPLE_RING_ENTRIES) { ++ s->t7000_ev_gen = s->gen; ++ s->t7000_ev_lead = 0; ++ cursor = producer; ++ } ++ rmb(); /* the index before the entries it covers */ ++ for (; cursor != producer; cursor = (cursor + 1) % APPLE_RING_ENTRIES) { ++ const u8 *e = events + cursor * S8003_EVENT_SIZE; ++ ++ if (get_unaligned_le32(e) == S8003_EVENT_STAMP) { ++ s->t7000_stamp_events++; ++ s->t7000_ev_lead++; ++ stamp_event = e; ++ } ++ } ++ s->t7000_ev_cursor = cursor; ++ for (lane = 0; lane < 4 && !rings; lane++) { ++ u8 *log = s8003_object(s, S8003_LOG_CTL) + lane * s->v->ring_ctl_size; ++ ++ rings = ring_read(log, S8003_RING_CONSUMER) != ring_read(log, S8003_RING_PRODUCER); ++ } ++ rings |= ring_read(s8003_object(s, S8003_STATS_CTL), S8003_RING_CONSUMER) != ++ ring_read(s8003_object(s, S8003_STATS_CTL), S8003_RING_PRODUCER); ++ rings |= ring_read(s8003_object(s, S8003_TRACE_CTL), S8003_RING_CONSUMER) != ++ ring_read(s8003_object(s, S8003_TRACE_CTL), S8003_RING_PRODUCER); ++ ++ if (retired) { ++ s->t7000_irq_found++; ++ } else if (s->t7000_ev_lead > 0) { ++ if (s->t7000_irq_stamp_event++ < 4) { ++ const u8 *ta = s8003_channel(s, APPLE_ENGINE_GEOM); ++ const u8 *td = s8003_channel(s, APPLE_ENGINE_FRAG); ++ ++ dev_info(apple_dev(apple), ++ "A8 interrupt before its stamp: events ahead of retirements by %lld, last new stamp event %*ph; TA submitted %u stamps %u %u; 3D submitted %u stamps %u %u\n", ++ s->t7000_ev_lead, S8003_EVENT_SIZE - 4, ++ stamp_event ? stamp_event + 4 : events, s8003_submitted(apple, APPLE_ENGINE_GEOM), ++ get32(ta + S8003_CH_STAMP), get32(ta + S8003_CH_STAMP + 4), ++ s8003_submitted(apple, APPLE_ENGINE_FRAG), get32(td + S8003_CH_STAMP), ++ get32(td + S8003_CH_STAMP + 4)); ++ } ++ } else if (polled) { ++ s->t7000_irq_polled++; ++ } else if (rings) { ++ s->t7000_irq_rings++; ++ } else { ++ s->t7000_irq_none++; ++ } ++} ++ + /* + * The backup completion check's period while jobs are pending (with the + * interrupt taken): the firmware raises its interrupt some time after it +@@ -5474,6 +5877,90 @@ static unsigned int t7000_recheck_us(struct pvr_apple *apple) + return clamp(READ_ONCE(a8_recheck_us), 5u, 250u); + } + ++static u64 t7000_avg(const struct pvr_apple_stat *st) ++{ ++ return st->n ? div64_u64(st->sum, st->n) : 0; ++} ++ ++/* ++ * debugfs apple_jobs: an engine's phases (struct t7000_phases), averages in ++ * us over the same jobs, so the first line adds up to its total; the second ++ * splits its parts, each with its own count. ++ */ ++static void t7000_phases_show(struct seq_file *m, const char *name, const struct t7000_phases *ph) ++{ ++ seq_printf(m, "a8 %s phases_us n %llu total %llu = submit_to_doorbell %llu + doorbell_to_start %llu + start_to_end %llu + end_to_signal %llu\n", ++ name, ph->total.n, t7000_avg(&ph->total), t7000_avg(&ph->host), ++ t7000_avg(&ph->wait), t7000_avg(&ph->run), t7000_avg(&ph->tail)); ++ seq_printf(m, "a8 %s phases_detail_us submit: pm %llu slot_wait %llu ring_wait %llu; doorbell_to_start: awake %llu (n %llu) after_fender_wake %llu (n %llu) behind_ta %llu + ta_end_to_3d_start %llu (n %llu); end_to_signal: irq %llu + irq_to_signal %llu (n %llu) by_check %llu (n %llu); max total %llu host %llu wait %llu run %llu tail %llu; start_stamp none %llu off %llu\n", ++ name, t7000_avg(&ph->pm), t7000_avg(&ph->slot), t7000_avg(&ph->ring), ++ t7000_avg(&ph->wait_awake), ph->wait_awake.n, ++ t7000_avg(&ph->wait_woke), ph->wait_woke.n, ++ t7000_avg(&ph->ta_wait), t7000_avg(&ph->ta_to_3d), ph->ta_wait.n, ++ t7000_avg(&ph->end_to_irq), t7000_avg(&ph->irq_to_signal), ph->end_to_irq.n, ++ t7000_avg(&ph->end_to_check), ph->end_to_check.n, ++ ph->total.max, ph->host.max, ph->wait.max, ph->run.max, ph->tail.max, ++ ph->start_stamp_none, ph->start_stamp_off); ++} ++ ++/* debugfs apple_jobs: per-VM state alive and the doorbells, statistics only */ ++static void t7000_debugfs_show(struct pvr_apple *apple, struct seq_file *m) ++{ ++ static const char *const names[APPLE_ENGINES] = { ++ [APPLE_ENGINE_GEOM] = "geometry", ++ [APPLE_ENGINE_FRAG] = "fragment", ++ [APPLE_ENGINE_COMPUTE] = "compute", ++ }; ++ struct pvr_apple_s8003 *s = apple->chip_data; ++ unsigned int r; ++ int engine; ++ ++ if (!s) ++ return; ++ /* the park VM is a VM context with a VM state of its own: the idle baseline */ ++ seq_printf(m, "a8 live vm_states %d pm_objs %d (park VM %d)\n", atomic_read(&s->vms_live), ++ atomic_read(&s->pm_objs_live), s->park_vm ? 1 : 0); ++ seq_printf(m, "a8 pm_blocks new VMs %u (a8_pm_blocks %u, stall floor %u, max %u)\n", ++ clamp(max(READ_ONCE(a8_pm_blocks), READ_ONCE(s->t7000_pm_floor)), 2u, ++ (u32)T7000_PM_BLOCKS_MAX), ++ READ_ONCE(a8_pm_blocks), READ_ONCE(s->t7000_pm_floor), (u32)T7000_PM_BLOCKS_MAX); ++ seq_printf(m, "a8 doorbells %lld in_fender_state_0 %lld pll5_relocking %lld not_rung %lld\n", ++ (long long)atomic64_read(&s->doorbells), ++ (long long)atomic64_read(&s->doorbell_wakes), ++ (long long)atomic64_read(&s->doorbell_pll_unlocked), ++ (long long)atomic64_read(&s->doorbell_refused)); ++ /* jobs refused, by reason: all 0 when every job reached the firmware */ ++ seq_puts(m, "a8 refused"); ++ for (r = 0; r < S8003_REFUSALS; r++) ++ seq_printf(m, " %s %lld", s8003_refusal_name[r], ++ (long long)atomic64_read(&s->refused[r])); ++ seq_putc(m, '\n'); ++ seq_printf(m, "a8 irqs found_job %llu; found nothing: stamp_event_ahead %llu for_retired_job %llu other_rings_only %llu nothing_new %llu; stamp_events %llu jobs_retired %llu lead %lld\n", ++ s->t7000_irq_found, s->t7000_irq_stamp_event, s->t7000_irq_polled, ++ s->t7000_irq_rings, s->t7000_irq_none, s->t7000_stamp_events, ++ s->t7000_jobs_retired, s->t7000_ev_lead); ++ seq_printf(m, "a8 pm_builds %llu poll_us %u\n", s->t7000_pm_builds, t7000_poll_us(apple)); ++ for (engine = 0; engine < APPLE_ENGINES; engine++) { ++ const struct pvr_apple_stat *st[3] = { ++ &s->t7000_fw_wait_us[engine], &s->t7000_fw_run_us[engine], ++ &s->t7000_fw_tail_us[engine], ++ }; ++ static const char *const what[3] = { "kick_to_start", "start_to_end", "end_to_signal" }; ++ int i; ++ ++ for (i = 0; i < 3; i++) ++ seq_printf(m, "a8 %s fw %s_us last %llu max %llu avg %llu n %llu\n", ++ names[engine], what[i], st[i]->last, st[i]->max, ++ st[i]->n ? div64_u64(st[i]->sum, st[i]->n) : 0, st[i]->n); ++ seq_printf(m, "a8 %s fw timestamps unusable %llu\n", names[engine], ++ s->t7000_fw_bad[engine]); ++ if (engine != APPLE_ENGINE_COMPUTE) ++ t7000_phases_show(m, names[engine], &s->t7000_ph[engine]); ++ } ++ seq_printf(m, "a8 joint %d (fragment job submitted after its geometry job's submit)\n", ++ READ_ONCE(a8_joint)); ++} ++ + static void t7000_iounmap(void *addr) + { + iounmap((void __iomem *)addr); +@@ -5734,8 +6221,12 @@ const struct pvr_apple_chip pvr_apple_chip_t7000 = { + .heaps = t7000_heaps, + .read_gpu_id = pvr_apple_t7000_read_gpu_id, + .report_fault = t7000_report_fault, ++ .debugfs_show = t7000_debugfs_show, ++ .debugfs_reset = t7000_debugfs_reset, ++ .job_retired = t7000_job_retired, + .poll_us = t7000_poll_us, + .recheck_us = t7000_recheck_us, ++ .irq_checked = t7000_irq_checked, + .joint = t7000_joint, + .init = pvr_apple_s8003_init, + .boot = pvr_apple_s8003_boot, +diff --git a/drivers/gpu/drm/imagination/pvr_device.h b/drivers/gpu/drm/imagination/pvr_device.h +index 98be4c27b5..2e17bdb21d 100644 +--- a/drivers/gpu/drm/imagination/pvr_device.h ++++ b/drivers/gpu/drm/imagination/pvr_device.h +@@ -259,6 +259,17 @@ struct pvr_device { + */ + struct xarray job_ids; + ++ /** ++ * @live: Objects alive now, for leak checks (debugfs apple_jobs): ++ * GEM objects and their bytes, MMU page-table pages, VM contexts. ++ */ ++ struct { ++ atomic_t gem_objs; ++ atomic64_t gem_bytes; ++ atomic_t mmu_pages; ++ atomic_t vm_ctxs; ++ } live; ++ + /** + * @queues: Queue-related fields. + */ +diff --git a/drivers/gpu/drm/imagination/pvr_drv.c b/drivers/gpu/drm/imagination/pvr_drv.c +index 48cfc9043a..c5a37622e9 100644 +--- a/drivers/gpu/drm/imagination/pvr_drv.c ++++ b/drivers/gpu/drm/imagination/pvr_drv.c +@@ -1272,6 +1272,28 @@ pvr_set_uobj_array(const struct drm_pvr_obj_array *out, u32 min_stride, u32 obj_ + return 0; + } + ++/* Apple: the ioctl's time in debugfs apple_jobs (statistics only) */ ++#define PVR_IOCTL_TIMED(_name, _func) \ ++static int pvr_ioctl_timed_##_func(struct drm_device *drm_dev, void *raw_args, \ ++ struct drm_file *file) \ ++{ \ ++ ktime_t start = ktime_get(); \ ++ int err = pvr_ioctl_##_func(drm_dev, raw_args, file); \ ++ \ ++ pvr_apple_ioctl_time(to_pvr_device(drm_dev), APPLE_IOCTL_##_name, start); \ ++ return err; \ ++} ++ ++PVR_IOCTL_TIMED(CREATE_BO, create_bo) ++PVR_IOCTL_TIMED(VM_MAP, vm_map) ++PVR_IOCTL_TIMED(CREATE_VM_CONTEXT, create_vm_context) ++PVR_IOCTL_TIMED(CREATE_CONTEXT, create_context) ++PVR_IOCTL_TIMED(CREATE_FREE_LIST, create_free_list) ++PVR_IOCTL_TIMED(CREATE_HWRT_DATASET, create_hwrt_dataset) ++PVR_IOCTL_TIMED(SUBMIT_JOBS, submit_jobs) ++ ++#undef PVR_IOCTL_TIMED ++ + #define DRM_PVR_IOCTL(_name, _func, _flags) \ + DRM_IOCTL_DEF_DRV(PVR_##_name, pvr_ioctl_##_func, _flags) + +@@ -1279,19 +1301,19 @@ pvr_set_uobj_array(const struct drm_pvr_obj_array *out, u32 min_stride, u32 obj_ + + static const struct drm_ioctl_desc pvr_drm_driver_ioctls[] = { + DRM_PVR_IOCTL(DEV_QUERY, dev_query, DRM_RENDER_ALLOW), +- DRM_PVR_IOCTL(CREATE_BO, create_bo, DRM_RENDER_ALLOW), ++ DRM_PVR_IOCTL(CREATE_BO, timed_create_bo, DRM_RENDER_ALLOW), + DRM_PVR_IOCTL(GET_BO_MMAP_OFFSET, get_bo_mmap_offset, DRM_RENDER_ALLOW), +- DRM_PVR_IOCTL(CREATE_VM_CONTEXT, create_vm_context, DRM_RENDER_ALLOW), ++ DRM_PVR_IOCTL(CREATE_VM_CONTEXT, timed_create_vm_context, DRM_RENDER_ALLOW), + DRM_PVR_IOCTL(DESTROY_VM_CONTEXT, destroy_vm_context, DRM_RENDER_ALLOW), +- DRM_PVR_IOCTL(VM_MAP, vm_map, DRM_RENDER_ALLOW), ++ DRM_PVR_IOCTL(VM_MAP, timed_vm_map, DRM_RENDER_ALLOW), + DRM_PVR_IOCTL(VM_UNMAP, vm_unmap, DRM_RENDER_ALLOW), +- DRM_PVR_IOCTL(CREATE_CONTEXT, create_context, DRM_RENDER_ALLOW), ++ DRM_PVR_IOCTL(CREATE_CONTEXT, timed_create_context, DRM_RENDER_ALLOW), + DRM_PVR_IOCTL(DESTROY_CONTEXT, destroy_context, DRM_RENDER_ALLOW), +- DRM_PVR_IOCTL(CREATE_FREE_LIST, create_free_list, DRM_RENDER_ALLOW), ++ DRM_PVR_IOCTL(CREATE_FREE_LIST, timed_create_free_list, DRM_RENDER_ALLOW), + DRM_PVR_IOCTL(DESTROY_FREE_LIST, destroy_free_list, DRM_RENDER_ALLOW), +- DRM_PVR_IOCTL(CREATE_HWRT_DATASET, create_hwrt_dataset, DRM_RENDER_ALLOW), ++ DRM_PVR_IOCTL(CREATE_HWRT_DATASET, timed_create_hwrt_dataset, DRM_RENDER_ALLOW), + DRM_PVR_IOCTL(DESTROY_HWRT_DATASET, destroy_hwrt_dataset, DRM_RENDER_ALLOW), +- DRM_PVR_IOCTL(SUBMIT_JOBS, submit_jobs, DRM_RENDER_ALLOW), ++ DRM_PVR_IOCTL(SUBMIT_JOBS, timed_submit_jobs, DRM_RENDER_ALLOW), + }; + + /* clang-format on */ +diff --git a/drivers/gpu/drm/imagination/pvr_gem.c b/drivers/gpu/drm/imagination/pvr_gem.c +index cf061a5d5e..c905479767 100644 +--- a/drivers/gpu/drm/imagination/pvr_gem.c ++++ b/drivers/gpu/drm/imagination/pvr_gem.c +@@ -31,6 +31,8 @@ static void pvr_gem_object_free(struct drm_gem_object *obj) + struct pvr_gem_object *bo = gem_to_pvr_gem(obj); + + drm_gem_lru_remove(obj); ++ atomic_dec(&to_pvr_device(obj->dev)->live.gem_objs); ++ atomic64_sub(obj->size, &to_pvr_device(obj->dev)->live.gem_bytes); + if (bo->pages_evicted) + atomic64_sub(obj->size, &to_pvr_device(obj->dev)->gem_reclaim.evicted_bytes); + shmem_obj->pages_mark_dirty_on_put = true; +@@ -425,6 +427,9 @@ struct drm_gem_object *pvr_gem_create_object(struct drm_device *drm_dev, size_t + + gem_obj = gem_from_pvr_gem(pvr_obj); + gem_obj->funcs = &pvr_gem_object_funcs; ++ /* Counted here and in pvr_gem_object_free(): created and imported alike */ ++ atomic_inc(&to_pvr_device(drm_dev)->live.gem_objs); ++ atomic64_add(size, &to_pvr_device(drm_dev)->live.gem_bytes); + + return gem_obj; + } +diff --git a/drivers/gpu/drm/imagination/pvr_job.h b/drivers/gpu/drm/imagination/pvr_job.h +index 44ac047fcf..1b6dacf8cb 100644 +--- a/drivers/gpu/drm/imagination/pvr_job.h ++++ b/drivers/gpu/drm/imagination/pvr_job.h +@@ -67,6 +67,9 @@ struct pvr_job { + /** @pvr_dev: Device pointer. */ + struct pvr_device *pvr_dev; + ++ /** @apple_armed: Apple: when the job was armed at submit (statistics). */ ++ ktime_t apple_armed; ++ + /** @ctx: Pointer to owning context. */ + struct pvr_context *ctx; + +diff --git a/drivers/gpu/drm/imagination/pvr_mmu.c b/drivers/gpu/drm/imagination/pvr_mmu.c +index 0d40c8a262..b1a57146be 100644 +--- a/drivers/gpu/drm/imagination/pvr_mmu.c ++++ b/drivers/gpu/drm/imagination/pvr_mmu.c +@@ -300,6 +300,7 @@ pvr_mmu_backing_page_init(struct pvr_mmu_backing_page *page, + page->pvr_dev = pvr_dev; + page->raw_page = raw_page; + kmemleak_alloc(page->host_ptr, PAGE_SIZE, 1, GFP_KERNEL); ++ atomic_inc(&pvr_dev->live.mmu_pages); + + return 0; + +@@ -345,6 +346,7 @@ pvr_mmu_backing_page_fini(struct pvr_mmu_backing_page *page) + vunmap(page->host_ptr); + + __free_page(page->raw_page); ++ atomic_dec(&page->pvr_dev->live.mmu_pages); + + memset(page, 0, sizeof(*page)); + } +diff --git a/drivers/gpu/drm/imagination/pvr_vm.c b/drivers/gpu/drm/imagination/pvr_vm.c +index 05b1b965b2..476337a507 100644 +--- a/drivers/gpu/drm/imagination/pvr_vm.c ++++ b/drivers/gpu/drm/imagination/pvr_vm.c +@@ -631,6 +631,7 @@ pvr_vm_create_context(struct pvr_device *pvr_dev, bool is_userspace_context) + vm_ctx->last_retired = ktime_get(); + INIT_LIST_HEAD(&vm_ctx->pins); + kref_init(&vm_ctx->ref_count); ++ atomic_inc(&pvr_dev->live.vm_ctxs); /* until pvr_vm_context_release() */ + + if (is_userspace_context && pvr_device_is_apple(pvr_dev)) { + err = pvr_apple_vm_init(pvr_dev, vm_ctx); +@@ -664,6 +665,7 @@ pvr_vm_context_release(struct kref *ref_count) + struct pvr_vm_context *vm_ctx = + container_of(ref_count, struct pvr_vm_context, ref_count); + ++ atomic_dec(&vm_ctx->pvr_dev->live.vm_ctxs); + /* Apple: GPU context 0 must not keep running on these page tables. */ + if (pvr_device_is_apple(vm_ctx->pvr_dev)) + pvr_apple_vm_fini(vm_ctx->pvr_dev, vm_ctx); From a63562af30c6e1277a14322556246cf25f886007 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Berkay=20K=C3=B6sebay?= Date: Sat, 21 Nov 2026 12:21:46 +0100 Subject: [PATCH 2/8] devices: add the iPod touch 6 (N102) with its GPU The iPod touch 6 RAM-boots through checkm8, PongoOS and m1n1 to the USB serial shell. Its descriptor, boot assets and firmware recipe pin the public iOS 12.5.8 IPSW. devices/t7000/0011 describes the A8's GPU in t7000.dtsi (the Rogue core, Fender, the power manager and GPU bridge registers, five interrupts, the firmware name) and enables it on the N102, where powervr's A8 variant runs Vulkan and OpenGL ES (through Zink) jobs. Orchard's feature list has no GPU entry and the chip's [gpu] table records dEQP results, which the A8 does not have yet, so the GPU state is in devices/n102/README.md. docs/compatibility.md is regenerated. Assisted-by: LLM --- devices/n102/README.md | 26 ++++++++ devices/n102/boot.json | 43 +++++++++++++ devices/n102/device.toml | 59 ++++++++++++++++++ devices/n102/firmware.json | 20 +++++++ docs/compatibility.md | 4 +- ...m64-dts-apple-t7000-Describe-the-GPU.patch | 60 +++++++++++++++++++ 6 files changed, 211 insertions(+), 1 deletion(-) create mode 100644 devices/n102/README.md create mode 100644 devices/n102/boot.json create mode 100644 devices/n102/device.toml create mode 100644 devices/n102/firmware.json create mode 100644 kernel/patches/devices/t7000/0011-arm64-dts-apple-t7000-Describe-the-GPU.patch diff --git a/devices/n102/README.md b/devices/n102/README.md new file mode 100644 index 00000000..0ff5908b --- /dev/null +++ b/devices/n102/README.md @@ -0,0 +1,26 @@ +# iPod touch 6, N102 + +Hardware identity, boot assets and the firmware recipe are in +[`device.toml`](device.toml). This file tracks what still needs testing, +open work, and contributed code that is not in the tree yet. + +## Needs testing + +**GPU (A8, PowerVR GX6450).** powervr's A8 variant +(`kernel/patches/drivers/gpu/0063`–`0073`, `devices/t7000/0011`) probes, +boots the GPU firmware and runs compute and render jobs on this board: +Vulkan 1.2 through Mesa's powervr driver and OpenGL ES 3.1 through Zink, +no faults over a 514k-job soak. Experimental: dEQP has not run, so the +chip has no `[gpu]` entry yet. Test with +[`examples/ipod-gpu-drm`](../../examples/ipod-gpu-drm/README.md). + +## Open work + +- GPU: per-job firmware overhead, parameter-manager pool growth inside a + scene, and submitting a fragment job behind its geometry job + (`a8_joint`) by default. +- Storage, touch, Wi-Fi and the battery gauge are not described yet. + +## Unmerged code + +None. diff --git a/devices/n102/boot.json b/devices/n102/boot.json new file mode 100644 index 00000000..2721852a --- /dev/null +++ b/devices/n102/boot.json @@ -0,0 +1,43 @@ +{ + "schema": 1, + "device": "iPod7,1", + "firmware": [ + { + "ipsw": { + "version": "12.5.8", + "build": "16H88", + "url": "https://updates.cdn-apple.com/2026WinterFCS/fullrestores/047-28554/C3D5CA8F-8B6F-4B05-9456-C5E632723337/iPodtouch_12.5.8_16H88_Restore.ipsw", + "bytes": 2821634289, + "sha256": "53a645c03332cd132f52612ffdf2aba3e6908ca937ab7f6c7b81aa8e15a14ec6" + }, + "selected_by": "firmware-recipe", + "inputs": { + "BuildManifest.plist": { + "ipsw": { + "zip": "BuildManifest.plist" + }, + "sha256": "97fe52925b0fbc3c2062019f831a18db7d6aeba60742df4be74106b3ea33d0e3" + }, + "iBSS": { + "ipsw": { + "zip": "Firmware/dfu/iBSS.n102.RELEASE.im4p" + }, + "sha256": "874d5fc6384ffee72e18b89020f1396702735c879d39014f39314c7ce2b84aec" + }, + "iBEC": { + "ipsw": { + "zip": "Firmware/dfu/iBEC.n102.RELEASE.im4p" + }, + "sha256": "5715e383b1e3eced6202eb47d515b13f1b10c41348239c6a800019111493a07c" + }, + "DeviceTree": { + "ipsw": { + "zip": "Firmware/all_flash/DeviceTree.n102ap.im4p" + }, + "sha256": "f59b40ce4fa554f4bb24415201c61dd2e8a6767558ca6404a4afa74dd2ec2d35" + } + } + } + ], + "methods": {} +} diff --git a/devices/n102/device.toml b/devices/n102/device.toml new file mode 100644 index 00000000..eb8e23b7 --- /dev/null +++ b/devices/n102/device.toml @@ -0,0 +1,59 @@ +# iPod touch 6 (N102): identity from public sources (tools/catalog/generate's read_firmware); +# A8/T7000 like the iPhone 6. RAM-boots to a USB serial shell; GPU notes in README.md. +schema = 1 +kind = "hardware.device" + +[device] +profile = "n102" +name = "iPod touch 6" +apple_products = ["iPod7,1"] +dt_compatible = ["apple,n102"] + +[match.dfu] +chip = 0x7000 +board = 0x10 + +[boot] +methods = ["checkm8-pongo"] # checkra1n then PongoOS and m1n1, RAM only. +entries = ["checkra1n"] + +[kernel] +platform = "apple-4k" +dtb = "t7000-n102" # arch/arm64/boot/dts/apple/.dtb + +[firmware] +recipe = "firmware.json" # Pins this board's public IPSW; converts nothing yet. + +[support] +status = "experimental" + +# States: working, untested, missing, none (features.toml). "untested" means a driver +# binds in its device tree (tools/catalog/drivers.json), plus the display's boot console. +[features] +display = "untested" +screen_off = "missing" +backlight = "untested" +touch = "missing" +buttons = "untested" +accelerometer = "missing" +gyroscope = "missing" +light_sensor = "missing" +touch_id = "missing" +storage_read = "missing" +storage_write = "missing" +wifi = "missing" +bluetooth_input = "missing" +bluetooth_audio = "missing" +cellular = "missing" +battery = "missing" # No fuel-gauge node in the ADT where the iPhone 6 has its HDQ gauge on uart5; how the iPod reads its battery is unknown. +charging = "missing" +charging_control = "missing" +suspend = "untested" +resume = "untested" +usb_serial = "working" # The RAM shell over USB serial (CDC ACM). +usb_network = "untested" +usb_host = "missing" +audio_output = "missing" +audio_input = "missing" +camera = "missing" +video_decode_hevc = "missing" diff --git a/devices/n102/firmware.json b/devices/n102/firmware.json new file mode 100644 index 00000000..bc51c803 --- /dev/null +++ b/devices/n102/firmware.json @@ -0,0 +1,20 @@ +{ + "schema": 2, + "device": "iPod7,1", + "ipsw": { + "version": "12.5.8", + "build": "16H88", + "url": "https://updates.cdn-apple.com/2026WinterFCS/fullrestores/047-28554/C3D5CA8F-8B6F-4B05-9456-C5E632723337/iPodtouch_12.5.8_16H88_Restore.ipsw", + "bytes": 2821634289, + "sha256": "53a645c03332cd132f52612ffdf2aba3e6908ca937ab7f6c7b81aa8e15a14ec6" + }, + "inputs": { + "DeviceTree.im4p": { + "ipsw": { + "zip": "Firmware/all_flash/DeviceTree.n102ap.im4p" + }, + "sha256": "f59b40ce4fa554f4bb24415201c61dd2e8a6767558ca6404a4afa74dd2ec2d35" + } + }, + "outputs": [] +} diff --git a/docs/compatibility.md b/docs/compatibility.md index 23a88477..467bb4b3 100644 --- a/docs/compatibility.md +++ b/docs/compatibility.md @@ -19,7 +19,7 @@ Generated by `tools/catalog/generate matrix` from `devices/`, `chips/` and `meth | A6 | S5L8950X | `0x8950` | – | – | apple-armv7 | iBSS + iBEC | – | 4 | 0 | 0 | | A6X | S5L8955X | `0x8955` | – | – | apple-armv7 | iBSS + iBEC | – | 3 | 0 | 0 | | A7 | S5L8960X | `0x8960` | cyclone | 4096 | apple-4k | iBSS + iBEC | – | 10 | 10 | 10 | -| A8 | T7000 | `0x7000` | typhoon | 4096 | apple-4k | iBSS + iBEC | – | 4 | 4 | 4 | +| A8 | T7000 | `0x7000` | typhoon | 4096 | apple-4k | iBSS + iBEC | – | 5 | 5 | 5 | | A8X | T7001 | `0x7001` | typhoon | 4096 | apple-4k | iBSS + iBEC | – | 2 | 2 | 2 | | A9 | S8000 | `0x8000` | twister | 16384 | apple-16k | iBSS + iBEC | – | 5 | 5 | 5 | | A9 | S8003 | `0x8003` | twister | 16384 | apple-16k | iBSS + iBEC | – | 5 | 5 | 5 | @@ -65,6 +65,7 @@ Generated by `tools/catalog/generate matrix` from `devices/`, `chips/` and `meth | A7 | `n53` | iPhone 5s (Global) | iPhone6,2 | experimental | – | checkm8-pongo | yes | yes | 16H88 | | A8 | `j96` | iPad mini 4 (Wi-Fi) | iPad5,1 | experimental | – | checkm8-pongo | yes | yes | 19H422 | | A8 | `j97` | iPad mini 4 (Cellular) | iPad5,2 | experimental | – | checkm8-pongo | yes | yes | 19H422 | +| A8 | `n102` | iPod touch 6 | iPod7,1 | experimental | – | checkm8-pongo | yes | yes | 16H88 | | A8 | `n56` | iPhone 6+ | iPhone7,1 | experimental | – | checkm8-pongo | yes | yes | 16H88 | | A8 | `n61` | iPhone 6 | iPhone7,2 | experimental | – | checkm8-pongo | yes | yes | 16H22 | | A8X | `j81` | iPad Air 2 (Wi-Fi) | iPad5,3 | experimental | yes | checkm8-pongo | yes | yes | 19H422 | @@ -152,6 +153,7 @@ Boards Orchard builds for. States: working, untested, missing, none (no such har | `n53` | untested | missing | untested | missing | untested | missing | missing | missing | missing | missing | missing | missing | missing | missing | missing | missing | missing | missing | untested | untested | untested | untested | missing | missing | missing | missing | missing | | `j96` | working | missing | working | untested | working | missing | missing | missing | missing | missing | missing | working | missing | missing | none | working | missing | missing | untested | untested | working | working | missing | missing | missing | missing | missing | | `j97` | untested | missing | untested | untested | untested | missing | missing | missing | missing | missing | missing | untested | missing | missing | missing | untested | missing | missing | untested | untested | untested | untested | missing | missing | missing | missing | missing | +| `n102` | untested | missing | untested | missing | untested | missing | missing | missing | missing | missing | missing | missing | missing | missing | missing | missing | missing | missing | untested | untested | working | untested | missing | missing | missing | missing | missing | | `n56` | untested | missing | untested | missing | untested | missing | missing | missing | missing | missing | missing | missing | missing | missing | missing | untested | missing | missing | untested | untested | untested | untested | missing | missing | missing | missing | missing | | `n61` | working | untested | untested | working | untested | missing | missing | missing | missing | missing | missing | missing | missing | missing | missing | untested | missing | missing | untested | untested | working | untested | missing | missing | missing | missing | missing | | `j81` | working | untested | working | working | working | missing | missing | missing | missing | working | working | working | untested | untested | none | working | missing | missing | untested | untested | working | working | missing | missing | missing | missing | missing | diff --git a/kernel/patches/devices/t7000/0011-arm64-dts-apple-t7000-Describe-the-GPU.patch b/kernel/patches/devices/t7000/0011-arm64-dts-apple-t7000-Describe-the-GPU.patch new file mode 100644 index 00000000..ebea5b73 --- /dev/null +++ b/kernel/patches/devices/t7000/0011-arm64-dts-apple-t7000-Describe-the-GPU.patch @@ -0,0 +1,60 @@ +From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 +From: =?UTF-8?q?Berkay=20K=C3=B6sebay?= +Date: Fri, 20 Nov 2026 17:36:03 +0100 +Subject: [PATCH] arm64: dts: apple: t7000: Describe the GPU + +The A8's GPU: the Rogue core's registers and Fender, the power manager's +and the two GPU bus bridges' registers (the driver powers the GPU +itself, in Apple's order), and its five interrupts, the fifth the +firmware's: AIC 138, the second of the ADT's meta-sw-interrupt tuples. +Enable it on the iPod touch 6 (N102), where it boots. + +Assisted-by: LLM +--- + arch/arm64/boot/dts/apple/t7000-n102.dts | 4 ++++ + arch/arm64/boot/dts/apple/t7000.dtsi | 17 +++++++++++++++++ + 2 files changed, 21 insertions(+) + +diff --git a/arch/arm64/boot/dts/apple/t7000-n102.dts b/arch/arm64/boot/dts/apple/t7000-n102.dts +index 447d46e..2893af6 100644 +--- a/arch/arm64/boot/dts/apple/t7000-n102.dts ++++ b/arch/arm64/boot/dts/apple/t7000-n102.dts +@@ -51,6 +51,10 @@ + power-domains = <&ps_disp0 &ps_mipi_dsi>; + }; + ++&gpu { ++ status = "okay"; ++}; ++ + &i2c0 { + status = "okay"; + +diff --git a/arch/arm64/boot/dts/apple/t7000.dtsi b/arch/arm64/boot/dts/apple/t7000.dtsi +index 72a2e7d..1a165a7 100644 +--- a/arch/arm64/boot/dts/apple/t7000.dtsi ++++ b/arch/arm64/boot/dts/apple/t7000.dtsi +@@ -129,6 +129,23 @@ + nonposted-mmio; + ranges; + ++ gpu: gpu@201000000 { ++ compatible = "apple,t7000-gpu"; ++ reg = <0x2 0x01000000 0x0 0x100000>, ++ <0x2 0x01e00000 0x0 0x100000>, ++ <0x2 0x0e000000 0x0 0x40000>, ++ <0x2 0x01f00000 0x0 0x81000>; ++ reg-names = "rgx", "fender", "pmgr", "bridges"; ++ interrupt-parent = <&aic>; ++ interrupts = , ++ , ++ , ++ , ++ ; ++ firmware-name = "apple/gfx-t7000.bin"; ++ status = "disabled"; ++ }; ++ + cpufreq: performance-controller@202220000 { + compatible = "apple,t7000-cluster-cpufreq", "apple,s5l8960x-cluster-cpufreq"; + reg = <0x2 0x02220000 0 0x1000>; From da883a7d3be228e9c878e1974290ab5338b42dd1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Berkay=20K=C3=B6sebay?= Date: Sat, 21 Nov 2026 12:21:52 +0100 Subject: [PATCH 3/8] devices: t7000: enable the GPU on the iPad mini 4 and iPhone 6 boards The iPad mini 4 (J96, J97) and the iPhone 6 and 6 Plus (N61, N56) have the iPod touch 6's A8 GPU. devices/t7000/0012 enables the GPU node in their shared board files (t7000-mini4.dtsi, t7000-6.dtsi) so people with these boards can test it; the driver has run only on the iPod touch 6. Each board's README asks for the probe and ladder results. The A8X (T7001) has a different GPU and is unchanged. Assisted-by: LLM --- devices/j96/README.md | 18 +++++++ devices/j97/README.md | 18 +++++++ devices/n56/README.md | 18 +++++++ devices/n61/README.md | 18 +++++++ ...t7000-Enable-the-GPU-on-the-other-T7.patch | 50 +++++++++++++++++++ 5 files changed, 122 insertions(+) create mode 100644 devices/j96/README.md create mode 100644 devices/j97/README.md create mode 100644 devices/n56/README.md create mode 100644 devices/n61/README.md create mode 100644 kernel/patches/devices/t7000/0012-arm64-dts-apple-t7000-Enable-the-GPU-on-the-other-T7.patch diff --git a/devices/j96/README.md b/devices/j96/README.md new file mode 100644 index 00000000..200efb3f --- /dev/null +++ b/devices/j96/README.md @@ -0,0 +1,18 @@ +# iPad mini 4 (Wi-Fi), J96 + +Hardware identity, boot assets and the firmware recipe are in +[`device.toml`](device.toml). This file tracks what still needs testing, +open work, and contributed code that is not in the tree yet. + +## Needs testing + +**GPU (A8, PowerVR GX6450).** The board has the iPod touch 6's A8 GPU, and +its device tree enables it (`kernel/patches/devices/t7000/0012`). The +driver has only run on the iPod touch 6 (N102) so far. Test with +[`examples/ipod-gpu-drm`](../../examples/ipod-gpu-drm/README.md) +(`--device j96`) and report the output of `a8-gpu-drm-test probe` with +its kernel lines, and the ladder's `RESULT` lines. + +## Unmerged code + +None. diff --git a/devices/j97/README.md b/devices/j97/README.md new file mode 100644 index 00000000..ddea4382 --- /dev/null +++ b/devices/j97/README.md @@ -0,0 +1,18 @@ +# iPad mini 4 (Cellular), J97 + +Hardware identity, boot assets and the firmware recipe are in +[`device.toml`](device.toml). This file tracks what still needs testing, +open work, and contributed code that is not in the tree yet. + +## Needs testing + +**GPU (A8, PowerVR GX6450).** The board has the iPod touch 6's A8 GPU, and +its device tree enables it (`kernel/patches/devices/t7000/0012`). The +driver has only run on the iPod touch 6 (N102) so far. Test with +[`examples/ipod-gpu-drm`](../../examples/ipod-gpu-drm/README.md) +(`--device j97`) and report the output of `a8-gpu-drm-test probe` with +its kernel lines, and the ladder's `RESULT` lines. + +## Unmerged code + +None. diff --git a/devices/n56/README.md b/devices/n56/README.md new file mode 100644 index 00000000..5f4ffbd8 --- /dev/null +++ b/devices/n56/README.md @@ -0,0 +1,18 @@ +# iPhone 6 Plus, N56 + +Hardware identity, boot assets and the firmware recipe are in +[`device.toml`](device.toml). This file tracks what still needs testing, +open work, and contributed code that is not in the tree yet. + +## Needs testing + +**GPU (A8, PowerVR GX6450).** The board has the iPod touch 6's A8 GPU, and +its device tree enables it (`kernel/patches/devices/t7000/0012`). The +driver has only run on the iPod touch 6 (N102) so far. Test with +[`examples/ipod-gpu-drm`](../../examples/ipod-gpu-drm/README.md) +(`--device n56`) and report the output of `a8-gpu-drm-test probe` with +its kernel lines, and the ladder's `RESULT` lines. + +## Unmerged code + +None. diff --git a/devices/n61/README.md b/devices/n61/README.md new file mode 100644 index 00000000..c8318c7b --- /dev/null +++ b/devices/n61/README.md @@ -0,0 +1,18 @@ +# iPhone 6, N61 + +Hardware identity, boot assets and the firmware recipe are in +[`device.toml`](device.toml). This file tracks what still needs testing, +open work, and contributed code that is not in the tree yet. + +## Needs testing + +**GPU (A8, PowerVR GX6450).** The board has the iPod touch 6's A8 GPU, and +its device tree enables it (`kernel/patches/devices/t7000/0012`). The +driver has only run on the iPod touch 6 (N102) so far. Test with +[`examples/ipod-gpu-drm`](../../examples/ipod-gpu-drm/README.md) +(`--device n61`) and report the output of `a8-gpu-drm-test probe` with +its kernel lines, and the ladder's `RESULT` lines. + +## Unmerged code + +None. diff --git a/kernel/patches/devices/t7000/0012-arm64-dts-apple-t7000-Enable-the-GPU-on-the-other-T7.patch b/kernel/patches/devices/t7000/0012-arm64-dts-apple-t7000-Enable-the-GPU-on-the-other-T7.patch new file mode 100644 index 00000000..dfbede40 --- /dev/null +++ b/kernel/patches/devices/t7000/0012-arm64-dts-apple-t7000-Enable-the-GPU-on-the-other-T7.patch @@ -0,0 +1,50 @@ +From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 +From: =?UTF-8?q?Berkay=20K=C3=B6sebay?= +Date: Sat, 21 Nov 2026 13:00:00 +0100 +Subject: [PATCH] arm64: dts: apple: t7000: Enable the GPU on the other T7000 + boards + +The iPad mini 4 (J96, J97) and the iPhone 6 and 6 Plus (N61, N56) have +the iPod touch 6's A8 GPU: the same SoC, GPU registers, power manager +and interrupts, all in t7000.dtsi. Enable the node in the two shared +board files so these boards can be tested; the driver has only run on +the iPod touch 6 so far. + +The A8X (T7001) has a different GPU and is not touched. + +Assisted-by: LLM +--- + arch/arm64/boot/dts/apple/t7000-6.dtsi | 4 ++++ + arch/arm64/boot/dts/apple/t7000-mini4.dtsi | 4 ++++ + 2 files changed, 8 insertions(+) + +diff --git a/arch/arm64/boot/dts/apple/t7000-6.dtsi b/arch/arm64/boot/dts/apple/t7000-6.dtsi +index 976051b..20c2212 100644 +--- a/arch/arm64/boot/dts/apple/t7000-6.dtsi ++++ b/arch/arm64/boot/dts/apple/t7000-6.dtsi +@@ -72,6 +72,10 @@ + power-domains = <&ps_disp0 &ps_mipi_dsi>; + }; + ++&gpu { ++ status = "okay"; ++}; ++ + &typhoon_opp06 { + status = "okay"; + }; +diff --git a/arch/arm64/boot/dts/apple/t7000-mini4.dtsi b/arch/arm64/boot/dts/apple/t7000-mini4.dtsi +index 6e0a5ad..08d3b41 100644 +--- a/arch/arm64/boot/dts/apple/t7000-mini4.dtsi ++++ b/arch/arm64/boot/dts/apple/t7000-mini4.dtsi +@@ -171,6 +171,10 @@ + power-domains = <&ps_disp0 &ps_dp>; + }; + ++&gpu { ++ status = "okay"; ++}; ++ + &typhoon_opp06 { + status = "okay"; + }; From 5ac665f56583a850a9a4364b13cbe6fe248789cd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Berkay=20K=C3=B6sebay?= Date: Sat, 21 Nov 2026 12:22:48 +0100 Subject: [PATCH 4/8] examples: ipod-gpu-drm, a test payload for the A8 GPU minimal-boot with a constant USB serial number, the kernel's modules, the GPU firmware and a8-gpu-drm-test, which loads powervr and checks the probe, Mesa's Vulkan path and two DRM-level replays, each step under a hardware watchdog with its kernel lines saved. It builds for every A8 board (n102, j96, j97, n61, n56). tools/extract-fw.py writes the GPU firmware and its border colour table from Apple's public iPod touch 6 iOS 12.4 IPSW, read over HTTP ranges with orchard's own IPSW and kernelcache code, or from another IPSW or kernelcache. Nothing from Apple is in the repository, and everything build.sh stages is git-ignored. Assisted-by: LLM --- examples/ipod-gpu-drm/.gitignore | 5 + examples/ipod-gpu-drm/README.md | 79 ++ examples/ipod-gpu-drm/build.sh | 54 ++ examples/ipod-gpu-drm/init | 42 + examples/ipod-gpu-drm/orchard.toml | 7 + .../rootfs/usr/bin/a8-gpu-drm-test | 150 ++++ .../ipod-gpu-drm/rootfs/usr/bin/a8-gpu-soak | 54 ++ examples/ipod-gpu-drm/src/a8-gpu-drm.c | 835 ++++++++++++++++++ examples/ipod-gpu-drm/tools/extract-fw.py | 151 ++++ examples/ipod-gpu-drm/tools/gen-replay.py | 186 ++++ examples/ipod-gpu-drm/tools/verify-payload.sh | 21 + 11 files changed, 1584 insertions(+) create mode 100644 examples/ipod-gpu-drm/.gitignore create mode 100644 examples/ipod-gpu-drm/README.md create mode 100755 examples/ipod-gpu-drm/build.sh create mode 100755 examples/ipod-gpu-drm/init create mode 100644 examples/ipod-gpu-drm/orchard.toml create mode 100755 examples/ipod-gpu-drm/rootfs/usr/bin/a8-gpu-drm-test create mode 100755 examples/ipod-gpu-drm/rootfs/usr/bin/a8-gpu-soak create mode 100644 examples/ipod-gpu-drm/src/a8-gpu-drm.c create mode 100755 examples/ipod-gpu-drm/tools/extract-fw.py create mode 100755 examples/ipod-gpu-drm/tools/gen-replay.py create mode 100755 examples/ipod-gpu-drm/tools/verify-payload.sh diff --git a/examples/ipod-gpu-drm/.gitignore b/examples/ipod-gpu-drm/.gitignore new file mode 100644 index 00000000..d7a8115f --- /dev/null +++ b/examples/ipod-gpu-drm/.gitignore @@ -0,0 +1,5 @@ +out/ +rootfs/usr/bin/a8-gpu-drm +rootfs/usr/share/a8-gpu-drm/ +rootfs/lib/firmware/ +rootfs/opt/ diff --git a/examples/ipod-gpu-drm/README.md b/examples/ipod-gpu-drm/README.md new file mode 100644 index 00000000..4385d71f --- /dev/null +++ b/examples/ipod-gpu-drm/README.md @@ -0,0 +1,79 @@ +# ipod-gpu-drm + +A RAM payload that tests the A8 (T7000) GPU, a PowerVR GX6450, through powervr's A8 variant +(`kernel/patches/drivers/gpu/0063`–`0073`, `devices/t7000/0011` and `0012`). It is `minimal-boot` with a +constant USB serial number (`a8-gpu-drm`, so macOS does not ask to allow the accessory on every boot), +the kernel's modules, the GPU firmware, a test script and, optionally, a Vulkan bundle built from +OrchardLinux/mesa. + +It builds for every A8 board: the iPod touch 6 (`n102`, where the GPU has been tested), the iPad mini 4 +(`j96`, `j97`) and the iPhone 6 and 6 Plus (`n61`, `n56`). The A8X (iPad Air 2) has a different GPU. + +## Build + +Use the `orchard` launcher of this checkout (`bin/orchard`): the nightly `orchard` on PATH does not carry +the A8 GPU patches. `BOARD` below is one of the boards above. + +1. Kernel: `bin/orchard build examples/minimal-boot --device BOARD`. This builds the apple-4k kernel in + Orchard's shared tree, `~/.cache/orchard/kernel-work/apple-4k-/`. +2. Optional, for the `vk` step, the Vulkan ladder and vkmark: build the bundle on aarch64 Linux (an + Arch Linux ARM container works) with `vk/make-bundle.sh MESA_BUILD OUT`. `MESA_BUILD` is a meson build + of OrchardLinux/mesa's A8 branch with `-Dvulkan-drivers=imagination`; the script's header has the + meson line. It also packs vkmark if the `vkmark` package is installed there. +3. Stage the payload's files: + + ```sh + A8_VK_DIR=/path/to/OUT examples/ipod-gpu-drm/build.sh ~/.cache/orchard/kernel-work/apple-4k- + ``` + + `build.sh` runs `tools/extract-fw.py`, which reads the iPod touch 6's iOS 12.4 (16G77) kernelcache + from Apple's public IPSW over HTTP ranges (about 15 MB, not the whole IPSW) and writes + `gfx-t7000.bin` (the GPU firmware) and `gfx-t7000-border.bin` (its border colour table). Use that + firmware on every A8 board: it is the one the driver was tested with. `tools/extract-fw.py DIR --ipsw + URL BYTES BOARD` takes another IPSW, e.g. the iPad mini 4's, and warns when the firmware is not the + tested one. Nothing from Apple is stored in this repository; everything `build.sh` writes is + git-ignored. +4. `bin/orchard build examples/ipod-gpu-drm --device BOARD` + +## Run + +1. Put the device in DFU mode, then `bin/orchard run examples/ipod-gpu-drm --device BOARD --no-build` + (checkm8, PongoOS, m1n1, Linux; RAM only, nothing is written to the device). +2. Open the USB serial console. powervr is not loaded at boot. +3. `a8-gpu-drm-test probe` loads powervr (`exp_hw_support=1`). The probe powers the GPU, reads its ID and + boots the firmware to its alive event; the render node then reports BVNC 3.10.4.5. Every step prints + one `RESULT` line, runs under a 30 s hardware watchdog (a frozen SoC resets) and saves its kernel lines + to `/run/a8-gpu-drm/STEP.txt`. After a failed step nothing more runs until the next boot. +4. `a8-gpu-drm-test vk`: Vulkan probe, compute and a triangle through Mesa. +5. The Vulkan ladder, every pixel checked on the CPU: + + ```sh + /opt/a8vk/a8-vk-ladder.sh list + for r in $(/opt/a8vk/a8-vk-ladder.sh list | awk 'NR>1 && /^ /{print $1}'); do /opt/a8vk/a8-vk-ladder.sh $r; done + ``` + +6. vkmark: `/opt/a8vk/vkmark.sh --size 256x256` and `--size 750x1334`. +7. A soak: `a8-gpu-soak 1000 0` runs the `vk` step 1000 times. + +The `compute` and `triangle` steps replay two Mesa recordings from the bring-up through the DRM ioctls +(`src/a8-gpu-drm.c`). They need `A8_RECORDINGS` at build time, which this example does not provide; +without it `a8-gpu-drm-test all` skips them. + +## Reporting + +On a board other than the iPod touch 6, report the board, the output of `a8-gpu-drm-test probe` with +its kernel lines (`/run/a8-gpu-drm/probe.txt`, or `dmesg` if the device froze and the serial log is all +there is), and the ladder's `RESULT` lines. If the probe freezes the device, the last kernel lines +streamed over the serial console show how far it got. + +## Files + +| Path | What | +|---|---| +| `init` | PID 1: `minimal-boot` with the constant USB serial number | +| `rootfs/usr/bin/a8-gpu-drm-test` | the test steps: `probe`, `compute`, `triangle`, `vk`, `all` | +| `rootfs/usr/bin/a8-gpu-soak` | repeats the `vk` and `triangle` steps | +| `src/a8-gpu-drm.c` | the DRM replay program: static aarch64, no libc | +| `tools/extract-fw.py` | the GPU firmware and border table from an Apple kernelcache | +| `tools/gen-replay.py` | packs a Mesa recording into a replay file | +| `vk/` | the Vulkan smoke test, the Vulkan ladder and `make-bundle.sh` | diff --git a/examples/ipod-gpu-drm/build.sh b/examples/ipod-gpu-drm/build.sh new file mode 100755 index 00000000..712f2e2c --- /dev/null +++ b/examples/ipod-gpu-drm/build.sh @@ -0,0 +1,54 @@ +#!/usr/bin/env bash +# build.sh KERNEL_WORK: stage what the payload needs beside the kernel. Writes, all git-ignored: +# rootfs/lib/firmware/apple/gfx-t7000.bin the GPU firmware } tools/extract-fw.py, from Apple's +# rootfs/lib/firmware/apple/gfx-t7000-border.bin its border colours } public iPod touch 6 iOS 12.4 IPSW +# rootfs/usr/bin/a8-gpu-drm the DRM test program (src/a8-gpu-drm.c; static aarch64, no libc) +# rootfs/opt/a8vk/ optional: a copy of A8_VK_DIR, the Vulkan bundle (README.md) +# rootfs/usr/share/a8-gpu-drm/{compute,triangle}.rpl optional: replays packed from A8_RECORDINGS +# +# KERNEL_WORK Orchard's apple-4k kernel work directory after `bin/orchard build examples/minimal-boot --device BOARD` +# (~/.cache/orchard/kernel-work/apple-4k-): its source/ and output/ give the UAPI headers. +# A8_KERNELCACHE optional: a decompressed kernelcache to take the firmware from instead of fetching the +# tested one (tools/extract-fw.py). +# A8_VK_DIR optional: a directory laid out as /opt/a8vk (README.md); without it the vk step is skipped. +# A8_RECORDINGS optional: the Mesa recordings for the compute and triangle replays (tools/gen-replay.py). +# Then, from the checkout: bin/orchard build examples/ipod-gpu-drm --device BOARD (n102, j96, j97, n61, n56) +set -euo pipefail +here=$(cd "$(dirname "$0")" && pwd) +work=${1:?usage: build.sh KERNEL_WORK} +work=$(cd "$work" && pwd) +[[ -d $work/source && -d $work/output ]] || { echo "build.sh: $work has no source/ and output/" >&2; exit 1; } + +mkdir -p "$here/rootfs/usr/bin" "$here/rootfs/lib/firmware/apple" +python3 -I "$here/tools/extract-fw.py" "$here/rootfs/lib/firmware/apple" \ + ${A8_KERNELCACHE:+--kernelcache "$A8_KERNELCACHE"} + +rm -rf "$here/rootfs/opt/a8vk" +if [[ -n ${A8_VK_DIR:-} ]]; then + [[ -x $A8_VK_DIR/a8-vk-smoke && -f $A8_VK_DIR/lib/libvulkan_powervr_mesa.so ]] || + { echo "build.sh: $A8_VK_DIR is not a Vulkan bundle (a8-vk-smoke, lib/libvulkan_powervr_mesa.so)" >&2; exit 1; } + mkdir -p "$here/rootfs/opt" + cp -R "$A8_VK_DIR" "$here/rootfs/opt/a8vk" +else + echo "build.sh: no A8_VK_DIR: the payload has no Vulkan bundle; the vk step is skipped" +fi + +rm -rf "$here/rootfs/usr/share/a8-gpu-drm" +if [[ -n ${A8_RECORDINGS:-} ]]; then + mkdir -p "$here/rootfs/usr/share/a8-gpu-drm" + for step in compute triangle; do + python3 -I "$here/tools/gen-replay.py" "$step" "$A8_RECORDINGS" "$here/rootfs/usr/share/a8-gpu-drm/$step.rpl" + done +fi + +podman run --rm --network none \ + -v "$work/source:/src:ro" -v "$work/output:/kout:ro" -v "$here:/work:rw" \ + localhost/orchard-hardware-kernel-toolchain:bookworm \ + clang-19 --target=aarch64-linux-gnu -O2 -Wall -Werror -ffreestanding -fno-builtin -nostdlib -static \ + -fno-pic -fno-pie -fno-stack-protector -fno-asynchronous-unwind-tables -mgeneral-regs-only -D__user= -D__EXPORTED_HEADERS__ \ + --ld-path=/usr/bin/ld.lld-19 -Wl,--build-id=none \ + -I/kout/arch/arm64/include/generated/uapi -I/src/arch/arm64/include/uapi -I/src/include/uapi \ + -o /work/rootfs/usr/bin/a8-gpu-drm /work/src/a8-gpu-drm.c +chmod 755 "$here/rootfs/usr/bin/a8-gpu-drm" +cd "$here/rootfs" +shasum -a 256 usr/bin/a8-gpu-drm usr/bin/a8-gpu-drm-test lib/firmware/apple/*.bin diff --git a/examples/ipod-gpu-drm/init b/examples/ipod-gpu-drm/init new file mode 100755 index 00000000..f10bebd5 --- /dev/null +++ b/examples/ipod-gpu-drm/init @@ -0,0 +1,42 @@ +#!/bin/busybox sh +# ipod-gpu-drm PID 1 (any A8 board): minimal-boot plus a constant USB serial. The GPU is +# not touched here: powervr loads only when a8-gpu-drm-test probe runs. +set -eu +export PATH=/bin:/sbin:/usr/bin:/usr/sbin +/bin/busybox --install -s /bin +mount -t proc -o nosuid,nodev,noexec proc /proc +mount -t sysfs -o nosuid,nodev,noexec sysfs /sys +mount -t devtmpfs -o nosuid devtmpfs /dev +mount -t tmpfs -o nosuid,nodev tmpfs /run +mkdir -p /dev/pts +mount -t devpts devpts /dev/pts +exec /dev/console 2>&1 +# No hotplug module loader; optional staged modules are for manual use only. +echo /bin/false > /proc/sys/kernel/modprobe +mount -t configfs -o nosuid,nodev,noexec configfs /sys/kernel/config +name=$(cat /etc/hostname) +g=/sys/kernel/config/usb_gadget/$name +mkdir "$g" +echo 0x05ac > "$g/idVendor" +# Prototype PID, not an allocation. Distinct from recovery and discovery. +echo 0x4144 > "$g/idProduct" +mkdir -p "$g/strings/0x409" "$g/configs/c.1/strings/0x409" +echo "Orchard $name" > "$g/strings/0x409/product" +# Constant serial: a per-boot serial makes macOS ask to Allow the accessory on every boot. +echo a8-gpu-drm > "$g/strings/0x409/serialnumber" +echo 'Physical USB diagnostic shell' > "$g/configs/c.1/strings/0x409/configuration" +# Descriptor only: not charging capability or PMIC policy. +echo 100 > "$g/configs/c.1/MaxPower" +mkdir "$g/functions/acm.usb0" +ln -s "$g/functions/acm.usb0" "$g/configs/c.1/acm.usb0" +set -- /sys/class/udc/* +[ "$#" -eq 1 ] && [ -e "$1" ] +printf '%s\n' "${1##*/}" > "$g/UDC" +echo "$name: RAM-only. USB root shell is unauthenticated; trusted host only." +mkdir -p /run/a8-gpu-drm +[ ! -f /etc/motd ] || cat /etc/motd +while :; do + setsid sh -c 'echo "ipod-gpu-drm: a8-gpu-drm-test probe|compute|triangle|vk|all"; exec sh -i' \ + /dev/ttyGS0 2>&1 || true + sleep 1 +done diff --git a/examples/ipod-gpu-drm/orchard.toml b/examples/ipod-gpu-drm/orchard.toml new file mode 100644 index 00000000..9052fadf --- /dev/null +++ b/examples/ipod-gpu-drm/orchard.toml @@ -0,0 +1,7 @@ +# orchard.toml: ipod-gpu-drm, minimal-boot with the constant USB serial, the kernel's modules +# (powervr and its dependencies) and the A8 GPU test (rootfs/, staged by ./build.sh). +name = "ipod-gpu-drm" + +[payload] +transport = false +modules = true diff --git a/examples/ipod-gpu-drm/rootfs/usr/bin/a8-gpu-drm-test b/examples/ipod-gpu-drm/rootfs/usr/bin/a8-gpu-drm-test new file mode 100755 index 00000000..80b5392d --- /dev/null +++ b/examples/ipod-gpu-drm/rootfs/usr/bin/a8-gpu-drm-test @@ -0,0 +1,150 @@ +#!/bin/sh +# a8-gpu-drm-test STEP [--again]: the A8 GPU through powervr, one step at a time. +# probe modprobe powervr exp_hw_support=1: the probe powers the GPU, reads its ID and boots +# the firmware to its alive event; then the render node's GPU ID and heaps +# compute Mesa's k_store_lid recording through the DRM ioctls; 32 stores of 0x12345678 +# triangle Mesa's tri-twiddled recording (TA + 3D); the 64x64 target checked pixel by pixel +# vk Mesa live on the real driver: /opt/a8vk/a8-vk-smoke all (Vulkan probe, compute, +# triangle); PASS = its three RESULT lines PASS +# all probe, compute, triangle, vk; stops at the first step that does not pass and skips +# a step whose files this payload does not carry (no recordings, no /opt/a8vk) +# Each step prints one RESULT line and runs once per boot (/run/a8-gpu-drm/STEP.done); --again repeats +# a step that passed. After a step that failed, or with the GPU left alone ("left as it is" in the +# kernel log: Fender stuck, Apple panics there), nothing more runs on this boot. +# Every step runs under a 30 s hardware watchdog, disarmed when the step ends: a frozen SoC resets. +# The kernel's lines of each step are saved to /run/a8-gpu-drm/STEP.txt, and every kernel line is +# streamed here ("kmsg: ...") while the step runs, so the last one survives a frozen SoC. +D=/run/a8-gpu-drm +SHARE=/usr/share/a8-gpu-drm +mkdir -p "$D" +step=${1:-} +[ $# -gt 0 ] && shift +again= +params= +for a in "$@"; do + case $a in + --again) again=--again ;; + *) echo "unknown argument $a"; exit 2 ;; + esac +done + +refuse() { + echo "RESULT $step REFUSED $*" + exit 1 +} + +case $step in +probe|compute|triangle|vk) ;; +all) + for s in probe compute triangle vk; do + if [ -e "$D/$s.done" ]; then echo "RESULT $s PASS (earlier on this boot)"; continue; fi + case $s in + compute|triangle) [ -f "$SHARE/$s.rpl" ] || { echo "RESULT $s SKIPPED (no $SHARE/$s.rpl)"; continue; } ;; + vk) [ -x /opt/a8vk/a8-vk-smoke ] || { echo "RESULT vk SKIPPED (no /opt/a8vk/a8-vk-smoke)"; continue; } ;; + esac + "$0" "$s" || exit 1 + done + exit 0 ;; +*) echo "usage: a8-gpu-drm-test probe|compute|triangle|vk|all [--again]"; exit 2 ;; +esac + +[ -e "$D/FAILED" ] && refuse "a step failed on this boot ($(cat "$D/FAILED")): reboot first" +dmesg | grep -q 'left as it is' && refuse "the kernel left the GPU alone (Fender stuck): reboot (DFU)" +[ -e "$D/$step.done" ] && [ "$again" != --again ] && refuse "already passed on this boot (--again repeats it)" +if [ "$step" != probe ] && ! grep -q '^powervr ' /proc/modules; then + refuse "powervr is not loaded: run a8-gpu-drm-test probe first" +fi +if [ "$step" = vk ]; then + [ -x /opt/a8vk/a8-vk-smoke ] || refuse "no /opt/a8vk/a8-vk-smoke in this initramfs" + [ -c /dev/dri/renderD128 ] || refuse "no /dev/dri/renderD128 (devtmpfs)" + [ -d /sys/class/drm ] || refuse "/sys is not mounted" +fi +case $step in +compute|triangle) [ -f "$SHARE/$step.rpl" ] || refuse "no $SHARE/$step.rpl in this initramfs" ;; +esac + +wd= +for d in /dev/watchdog0 /dev/watchdog; do [ -c "$d" ] && { wd=$d; break; }; done +[ -z "$wd" ] && [ "$NO_WATCHDOG" != 1 ] && refuse "no /dev/watchdog: a hang would leave no reset (NO_WATCHDOG=1 runs anyway)" + +marker="A8-GPU-DRM $step $$ begin" +# live: every kernel line from here on, before anything touches the GPU +(seen=0; while read -r l; do + case $l in *"$marker"*) seen=1 ;; esac + [ $seen = 1 ] && echo "kmsg: ${l#*;}" +done < /dev/kmsg) & +fwd=$! +sleep 1 +echo "$marker" > /dev/kmsg +wdpid= +if [ -n "$wd" ]; then + watchdog -F -T 30 -t 3600 "$wd" /dev/null 2>&1 & + wdpid=$! + sleep 1 + kill -0 $wdpid 2>/dev/null || { wdpid=; echo "watchdog failed to arm"; } +fi + +out=$D/$step.out +case $step in +probe) + if grep -q '^powervr ' /proc/modules; then + echo "powervr already loaded" + else + echo "modprobe powervr exp_hw_support=1$params" + if ! modprobe powervr exp_hw_support=1 $params && ! grep -q '^powervr ' /proc/modules; then + # without a working modprobe: the dependencies modules.dep names, then powervr + m=/lib/modules/$(uname -r) + deps=$(sed -n 's|^\(.*/powervr.ko\):\(.*\)|\2|p' "$m/modules.dep") + for k in $deps $deps; do insmod "$m/$k" 2>/dev/null; done + insmod "$m/kernel/drivers/gpu/drm/imagination/powervr.ko" exp_hw_support=1 $params + fi + echo "powervr loaded: $(grep -c '^powervr ' /proc/modules)" + fi + sleep 1 + a8-gpu-drm info > "$out" 2>&1 ;; +compute) + a8-gpu-drm compute "$SHARE/compute.rpl" > "$out" 2>&1 ;; +triangle) + a8-gpu-drm triangle "$SHARE/triangle.rpl" > "$out" 2>&1 ;; +vk) + /opt/a8vk/a8-vk-smoke all > "$out" 2>&1 ;; +esac +rc=$? +[ -n "$wdpid" ] && { kill -TERM $wdpid; wait $wdpid 2>/dev/null; } +# the runtime suspend's stop comes about 1 s after the probe +[ "$step" = probe ] && sleep 3 +kill $fwd 2>/dev/null + +dmesg | sed -n "/$marker/,\$p" | grep -i -e powervr -e 'A8-GPU-DRM' > "$D/$step.txt" +grep -v '^RESULT' "$out" +echo "---- kernel ($D/$step.txt):" +cat "$D/$step.txt" +result=$(grep '^RESULT' "$out" | tail -1) +[ -z "$result" ] && result="RESULT $step FAIL no result (exit $rc)" +if [ "$step" = vk ]; then + grep '^RESULT' "$out" + if [ $rc = 0 ] && [ "$(grep -c '^RESULT .* PASS' "$out")" -ge 3 ] && ! grep -q '^RESULT .* FAIL' "$out"; then + result="RESULT vk PASS (Mesa probe, compute, triangle)" + else + result="RESULT vk FAIL (exit $rc; see $out)" + rc=1 + fi +fi +if [ "$step" = probe ]; then + if [ $rc = 0 ] && grep -q '^RESULT info PASS' "$out"; then + result="RESULT probe PASS $(grep '^gpu id' "$out")" + else + result="RESULT probe FAIL (no powervr render node; see $D/probe.txt)" + rc=1 + fi +fi +echo "$result" +if [ $rc = 0 ]; then + : > "$D/$step.done" +elif echo "$result" | grep -q 'FAIL submit '; then + # Refused by SUBMIT_JOBS: no job reached the GPU, so the step may run again on this boot + echo "(a submit-time rejection: no GPU work ran; $step can be retried on this boot)" +else + echo "$step" > "$D/FAILED" +fi +exit $rc diff --git a/examples/ipod-gpu-drm/rootfs/usr/bin/a8-gpu-soak b/examples/ipod-gpu-drm/rootfs/usr/bin/a8-gpu-soak new file mode 100755 index 00000000..34cbd685 --- /dev/null +++ b/examples/ipod-gpu-drm/rootfs/usr/bin/a8-gpu-soak @@ -0,0 +1,54 @@ +#!/bin/sh +# a8-gpu-soak [VK [TRI]]: VK cycles of "a8-gpu-drm-test vk --again", then TRI cycles of +# "a8-gpu-drm-test triangle --again" (defaults 100 and 50). Each cycle is a full client: +# open, firmware start, jobs, close. +# Before each step one line "ITER n STEP s T" (T = uptime in seconds, as in dmesg); the +# last such line names the step that hung. At the first failure: the step's output +# tail, the last 40 kernel lines, "SOAK FAIL ..." and exit 1. At the end "SOAK PASS n". +# Each step's output goes to a file and is read after the step has exited (no live pipe: +# an early reader exit would kill the step before it disarms its watchdog); stdin is +# /dev/null, so nothing typed on the serial line reaches a step. Output only on stdout; +# the full log is also in /run/a8-gpu-drm/soak.log. +D=/run/a8-gpu-drm +mkdir -p "$D" +LOG=$D/soak.log +OUT=$D/soak-step.out +vk=${1:-100} +tri=${2:-50} +case $vk$tri in *[!0-9]*|'') echo "usage: a8-gpu-soak [VK_CYCLES [TRIANGLE_CYCLES]]"; exit 2 ;; esac + +say() { + echo "$*" + echo "$*" >> "$LOG" +} + +now() { + read -r up _ < /proc/uptime + echo "$up" +} + +: > "$LOG" +say "SOAK START vk $vk triangle $tri at $(now)" +n=0 +for step in vk triangle; do + if [ "$step" = vk ]; then count=$vk; else count=$tri; fi + i=0 + while [ "$i" -lt "$count" ]; do + i=$((i + 1)) + n=$((n + 1)) + say "ITER $i STEP $step $(now)" + a8-gpu-drm-test "$step" --again > "$OUT" 2>&1 < /dev/null + rc=$? + if [ $rc = 0 ] && grep -q "^RESULT $step PASS" "$OUT"; then + continue + fi + say "---- step output (last 20 lines):" + tail -20 "$OUT" | while IFS= read -r l; do say "$l"; done + say "---- kernel (last 40 lines):" + dmesg | tail -40 | while IFS= read -r l; do say "$l"; done + say "SOAK FAIL at ITER $i STEP $step (exit $rc) after $((n - 1)) passed, $(now)" + exit 1 + done +done +say "SOAK PASS $n ($vk vk, $tri triangle) at $(now)" +exit 0 diff --git a/examples/ipod-gpu-drm/src/a8-gpu-drm.c b/examples/ipod-gpu-drm/src/a8-gpu-drm.c new file mode 100644 index 00000000..a772624d --- /dev/null +++ b/examples/ipod-gpu-drm/src/a8-gpu-drm.c @@ -0,0 +1,835 @@ +// SPDX-License-Identifier: GPL-2.0-only OR MIT +/* + * a8-gpu-drm: replay a Mesa A8 recording through powervr's real + * DRM interface (render node, CREATE_VM_CONTEXT, CREATE_BO, VM_MAP, + * CREATE_CONTEXT, CREATE_FREE_LIST, CREATE_HWRT_DATASET, SUBMIT_JOBS, a + * syncobj wait) and check what the GPU wrote. Static, no libc: raw aarch64 + * system calls, for the iPod's busybox initramfs. + * + * a8-gpu-drm compute FILE.rpl k_store_lid: 32 dwords 0x12345678 in the output BO + * a8-gpu-drm triangle FILE.rpl tri-twiddled: the 64x64 target, checked the way the + * bring-up module did (unflipped, twiddled layout) + * a8-gpu-drm info the device's GPU ID and heaps + * a8-gpu-drm dry compute|triangle FILE.rpl + * walk the recording without a device (every ioctl succeeds, + * BOs are anonymous memory): the check then fails + * + * The .rpl files come from tools/gen-replay.py. One line starts "RESULT". + * Exit status 0 is a pass. + */ +#include +#include +#include + +typedef unsigned long uintptr_t; /* for DRM_PVR_OBJ_ARRAY() */ + +typedef __u8 u8; +typedef __u32 u32; +typedef __s32 s32; +typedef __u64 u64; +typedef long ssize; +typedef unsigned long usize; + +/* ---- system calls (aarch64) ---- */ + +#define SYS_ioctl 29 +#define SYS_openat 56 +#define SYS_close 57 +#define SYS_read 63 +#define SYS_write 64 +#define SYS_exit_group 94 +#define SYS_clock_gettime 113 +#define SYS_munmap 215 +#define SYS_mmap 222 + +#define AT_FDCWD -100 +#define O_RDONLY 0 +#define O_RDWR 2 +#define O_CLOEXEC 02000000 +#define PROT_READ 1 +#define PROT_WRITE 2 +#define MAP_SHARED 1 +#define MAP_PRIVATE 2 +#define MAP_ANONYMOUS 0x20 +#define CLOCK_MONOTONIC 1 + +static long sys6(long n, long a, long b, long c, long d, long e, long f) +{ + register long x8 __asm__("x8") = n; + register long x0 __asm__("x0") = a; + register long x1 __asm__("x1") = b; + register long x2 __asm__("x2") = c; + register long x3 __asm__("x3") = d; + register long x4 __asm__("x4") = e; + register long x5 __asm__("x5") = f; + + __asm__ volatile("svc #0" + : "+r"(x0) + : "r"(x8), "r"(x1), "r"(x2), "r"(x3), "r"(x4), "r"(x5) + : "memory", "cc"); + return x0; +} + +#define sys3(n, a, b, c) sys6(n, (long)(a), (long)(b), (long)(c), 0, 0, 0) + +static __attribute__((noreturn)) void sys_exit(int code) +{ + for (;;) + sys6(SYS_exit_group, code, 0, 0, 0, 0, 0); +} + +static void *sys_mmap(void *addr, usize len, int prot, int flags, int fd, u64 off) +{ + return (void *)sys6(SYS_mmap, (long)addr, (long)len, prot, flags, fd, (long)off); +} + +static int is_err(long v) +{ + return v < 0 && v > -4096; +} + +/* The compiler may emit calls to these */ +void *memset(void *d, int c, usize n) +{ + u8 *p = d; + + while (n--) + *p++ = (u8)c; + return d; +} + +void *memcpy(void *d, const void *s, usize n) +{ + u8 *p = d; + const u8 *q = s; + + while (n--) + *p++ = *q++; + return d; +} + +int memcmp(const void *a, const void *b, usize n) +{ + const u8 *p = a, *q = b; + + for (; n--; p++, q++) + if (*p != *q) + return *p < *q ? -1 : 1; + return 0; +} + +/* ---- output ---- */ + +static char obuf[512]; +static usize olen; + +static void flush(void) +{ + if (olen) + sys3(SYS_write, 1, obuf, olen); + olen = 0; +} + +static void putch(char c) +{ + if (olen == sizeof(obuf)) + flush(); + obuf[olen++] = c; + if (c == '\n') + flush(); +} + +static void puts_(const char *s) +{ + while (*s) + putch(*s++); +} + +static void putu(u64 v) +{ + char b[24]; + int i = 0; + + do { + b[i++] = '0' + v % 10; + v /= 10; + } while (v); + while (i--) + putch(b[i]); +} + +static void puth(u64 v) +{ + static const char hex[] = "0123456789abcdef"; + char b[18]; + int i = 0; + + do { + b[i++] = hex[v & 15]; + v >>= 4; + } while (v); + puts_("0x"); + while (i--) + putch(b[i]); +} + +static void puti(long v) +{ + if (v < 0) { + putch('-'); + v = -v; + } + putu((u64)v); +} + +static int streq(const char *a, const char *b) +{ + while (*a && *a == *b) + a++, b++; + return *a == *b; +} + +/* ---- time ---- */ + +struct ts { + long sec, nsec; +}; + +static u64 now_us(void) +{ + struct ts t; + + sys3(SYS_clock_gettime, CLOCK_MONOTONIC, &t, 0); + return (u64)t.sec * 1000000 + t.nsec / 1000; +} + +/* ---- the replay file (tools/gen-replay.py) ---- */ + +#define RPL_MAGIC "A8RPLAY1" +enum { + RPL_BO = 1, /* u32 index, u32 kind (0 CPU, 1 PM protected), u64 size */ + RPL_MAP, /* u32 index, u32 0, u64 va, u64 size */ + RPL_DATA, /* u32 index, u32 0, u64 offset, bytes */ + RPL_FREELIST, /* u32 index, u32 0, u64 va, u32 initial, max, grow, threshold */ + RPL_HWRT, /* u32 free list 0, u32 free list 1, create_hwrt_dataset args */ + RPL_CONTEXT, /* u32 DRM_PVR_CTX_TYPE_* */ + RPL_JOB, /* u32 type, u32 flags, u32 data index, u32 0, stream */ + RPL_CHECK_STORE, /* u64 va, u32 count, u32 value */ + RPL_CHECK_TRI, /* u64 va */ + RPL_NAME, /* text */ +}; + +#define MAX_BOS 64 +#define MAX_FREELISTS 4 +#define MAX_JOBS 4 +#define KIND_PM 1 + +static struct bo { + u32 handle; + u32 kind; + u64 size; + u8 *map; + u64 va; /* its first mapping, for the checks */ +} bos[MAX_BOS]; +static u32 nbos; +static u32 freelists[MAX_FREELISTS]; +static int fd = -1; +static u32 vm, hwrt, ctx; + +static int dry; /* no device: every ioctl succeeds */ +static u32 dry_handle; + +static int ioc(unsigned long req, void *arg, const char *what) +{ + long r; + + if (dry) { + /* every handle-returning argument struct of ours has its handle where the kernel puts it */ + if (req == DRM_IOCTL_PVR_CREATE_BO) + ((struct drm_pvr_ioctl_create_bo_args *)arg)->handle = ++dry_handle; + else if (req == DRM_IOCTL_PVR_CREATE_VM_CONTEXT) + ((struct drm_pvr_ioctl_create_vm_context_args *)arg)->handle = ++dry_handle; + else if (req == DRM_IOCTL_PVR_CREATE_FREE_LIST) + ((struct drm_pvr_ioctl_create_free_list_args *)arg)->handle = ++dry_handle; + else if (req == DRM_IOCTL_PVR_CREATE_HWRT_DATASET) + ((struct drm_pvr_ioctl_create_hwrt_dataset_args *)arg)->handle = ++dry_handle; + else if (req == DRM_IOCTL_PVR_CREATE_CONTEXT) + ((struct drm_pvr_ioctl_create_context_args *)arg)->handle = ++dry_handle; + else if (req == DRM_IOCTL_SYNCOBJ_CREATE) + ((struct drm_syncobj_create *)arg)->handle = ++dry_handle; + puts_("dry: "); + puts_(what); + putch('\n'); + return 0; + } + r = sys3(SYS_ioctl, fd, req, arg); + + if (r < 0) { + puts_("a8-gpu-drm: "); + puts_(what); + puts_(" failed: "); + puti(r); + putch('\n'); + } + return (int)r; +} + +static const u8 *rpl; +static u64 rpl_size; + +static u32 rd32(const u8 *p) +{ + return p[0] | p[1] << 8 | p[2] << 16 | (u32)p[3] << 24; +} + +static u64 rd64(const u8 *p) +{ + return rd32(p) | (u64)rd32(p + 4) << 32; +} + +static int load(const char *path) +{ + int f = (int)sys6(SYS_openat, AT_FDCWD, (long)path, O_RDONLY | O_CLOEXEC, 0, 0, 0); + u64 cap = 1 << 20, n = 0; + u8 *buf; + + if (f < 0) + return f; + buf = sys_mmap(0, 64 << 20, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); + if (is_err((long)buf)) + return -12; + for (;;) { + long r = sys3(SYS_read, f, buf + n, cap); + + if (r < 0) + return (int)r; + if (!r) + break; + n += r; + if (n + cap > (64 << 20)) + return -27; + } + sys3(SYS_close, f, 0, 0); + rpl = buf; + rpl_size = n; + return n >= 12 && !memcmp(buf, RPL_MAGIC, 8) ? 0 : -22; +} + +/* ---- the triangle check (as the bring-up module checked it) ---- */ + +#define TRI_DIM 64 +#define TRI_CLEAR_PX 0xff000033u +#define TRI_COLOUR_PX 0xffff4000u +enum { TL_TWID_YLO, TL_TWID_XLO, TL_LINEAR, TL_KINDS }; +#define TL_FLIP TL_KINDS +#define TL_COUNT (2 * TL_KINDS) + +static const char *const tl_name[TL_COUNT] = { + "twiddled-y0", "twiddled-x0", "linear", "twiddled-y0-flipped", "twiddled-x0-flipped", + "linear-flipped", +}; + +static u32 tri_index(unsigned int layout, u32 x, u32 y) +{ + u32 i, idx = 0; + + if (layout >= TL_FLIP) + y = TRI_DIM - 1 - y; + switch (layout % TL_KINDS) { + case TL_LINEAR: + return y * TRI_DIM + x; + case TL_TWID_YLO: + for (i = 0; i < 6; i++) + idx |= ((y >> i) & 1) << (2 * i) | ((x >> i) & 1) << (2 * i + 1); + return idx; + default: + for (i = 0; i < 6; i++) + idx |= ((x >> i) & 1) << (2 * i) | ((y >> i) & 1) << (2 * i + 1); + return idx; + } +} + +/* 0: clear, 1: triangle, 2: either (pixel centre within 0.75 px of an edge) */ +static int tri_expect(u32 x, u32 y) +{ + static const int v[3][2] = { { 32, 32 }, { 96, 32 }, { 64, 96 } }; + long long px = 2 * x + 1, py = 2 * y + 1; + int i, inside = 1, near = 0; + + for (i = 0; i < 3; i++) { + long long ax = v[i][0], ay = v[i][1], bx = v[(i + 1) % 3][0], by = v[(i + 1) % 3][1]; + long long e = (bx - ax) * (py - ay) - (by - ay) * (px - ax); + long long len2 = (bx - ax) * (bx - ax) + (by - ay) * (by - ay); + int close = 4 * e * e < 9 * len2; + + if (e <= 0 && !close) + return 0; + if (e <= 0) + inside = 0; + near |= close; + } + return near ? 2 : inside; +} + +static int px_near(u32 a, u32 b) +{ + int i; + + for (i = 0; i < 32; i += 8) { + int d = (int)((a >> i) & 0xff) - (int)((b >> i) & 0xff); + + if (d < -2 || d > 2) + return 0; + } + return 1; +} + +static int px_class(u32 v) +{ + return px_near(v, TRI_CLEAR_PX) ? 0 : px_near(v, TRI_COLOUR_PX) ? 1 : 2; +} + +struct tri_result { + u32 clear, colour, edge, other, bad; +}; + +static int check_triangle(const u8 *img) +{ + struct tri_result r[TL_COUNT]; + unsigned int l, best = 0; + u32 x, y; + int pass; + + for (l = 0; l < TL_COUNT; l++) { + struct tri_result *t = &r[l]; + + memset(t, 0, sizeof(*t)); + for (y = 0; y < TRI_DIM; y++) + for (x = 0; x < TRI_DIM; x++) { + u32 v = rd32(img + 4 * tri_index(l, x, y)); + int c = px_class(v), e = tri_expect(x, y); + + if (c == 2) + t->other++; + else if (e == 2) + t->edge++; + else if (c != e) + t->bad++; + else if (c) + t->colour++; + else + t->clear++; + } + if (t->bad + t->other < r[best].bad + r[best].other) + best = l; + puts_("target as "); + puts_(tl_name[l]); + puts_(": clear "); + putu(t->clear); + puts_(" colour "); + putu(t->colour); + puts_(" edge "); + putu(t->edge); + puts_(" other "); + putu(t->other); + puts_(" bad "); + putu(t->bad); + putch('\n'); + } + for (y = 0; y < 16; y++) { + for (x = 0; x < 16; x++) { + unsigned int n[3] = { 0, 0, 0 }, yy, xx; + + for (yy = 4 * y; yy < 4 * y + 4; yy++) + for (xx = 4 * x; xx < 4 * x + 4; xx++) + n[px_class(rd32(img + 4 * tri_index(best, xx, yy)))]++; + putch(n[2] ? '?' : n[1] == 16 ? '#' : n[0] == 16 ? '.' : 'x'); + } + putch('\n'); + } + pass = best < TL_FLIP && best != TL_LINEAR && !r[best].bad && !r[best].other && r[best].colour; + puts_("RESULT triangle "); + puts_(pass ? "PASS" : "FAIL"); + puts_(" layout "); + puts_(tl_name[best]); + puts_(" colour "); + putu(r[best].colour); + puts_(" bad "); + putu(r[best].bad); + puts_(" other "); + putu(r[best].other); + puts_(" pixel(0,0) "); + puth(rd32(img)); + puts_(" pixel(32,32) "); + puth(rd32(img + 4 * tri_index(best, 32, 32))); + putch('\n'); + return pass ? 0 : 1; +} + +static struct bo *bo_at(u64 va, u64 len) +{ + u32 i; + + for (i = 0; i < nbos; i++) + if (bos[i].va && va >= bos[i].va && va + len <= bos[i].va + bos[i].size) + return &bos[i]; + return 0; +} + +static int open_node(void) +{ + static const char *const nodes[] = { "/dev/dri/renderD128", "/dev/dri/renderD129", + "/dev/dri/renderD130" }; + unsigned int i; + + for (i = 0; i < sizeof(nodes) / sizeof(nodes[0]); i++) { + char name[16] = { 0 }; + struct drm_version v = { .name = name, .name_len = sizeof(name) - 1 }; + int f = (int)sys6(SYS_openat, AT_FDCWD, (long)nodes[i], O_RDWR | O_CLOEXEC, 0, 0, 0); + + if (f < 0) + continue; + if (!sys3(SYS_ioctl, f, DRM_IOCTL_VERSION, &v) && streq(name, "powervr")) + return f; + sys3(SYS_close, f, 0, 0); + } + return -19; +} + +static int info(void) +{ + struct drm_pvr_dev_query_gpu_info gpu = { 0 }; + struct drm_pvr_heap heaps[8]; + struct drm_pvr_dev_query_heap_info hi = { + .heaps = { .stride = sizeof(struct drm_pvr_heap), .count = 8, .array = (u64)heaps }, + }; + struct drm_pvr_ioctl_dev_query_args q = { + .type = DRM_PVR_DEV_QUERY_GPU_INFO_GET, .pointer = (u64)&gpu, .size = sizeof(gpu), + }; + u32 i; + + if (ioc(DRM_IOCTL_PVR_DEV_QUERY, &q, "DEV_QUERY gpu")) + return 1; + puts_("gpu id "); + puth(gpu.gpu_id); + puts_(" (BVNC "); + putu(gpu.gpu_id >> 48); + putch('.'); + putu((gpu.gpu_id >> 32) & 0xffff); + putch('.'); + putu((gpu.gpu_id >> 16) & 0xffff); + putch('.'); + putu(gpu.gpu_id & 0xffff); + puts_(")\n"); + q = (struct drm_pvr_ioctl_dev_query_args) { + .type = DRM_PVR_DEV_QUERY_HEAP_INFO_GET, .pointer = (u64)&hi, .size = sizeof(hi), + }; + if (ioc(DRM_IOCTL_PVR_DEV_QUERY, &q, "DEV_QUERY heaps")) + return 1; + for (i = 0; i < hi.heaps.count && i < 8; i++) { + puts_("heap "); + putu(i); + puts_(" base "); + puth(heaps[i].base); + puts_(" size "); + puth(heaps[i].size); + puts_(" page 2^"); + putu(heaps[i].page_size_log2); + putch('\n'); + } + puts_("RESULT info PASS\n"); + return 0; +} + +static int replay(int triangle) +{ + struct drm_pvr_job jobs[MAX_JOBS]; + static u8 streams[MAX_JOBS][1024]; + struct drm_pvr_sync_op signal; + struct drm_syncobj_create sync = { 0 }; + struct drm_pvr_ioctl_create_vm_context_args cvm = { 0 }; + u64 check_va = 0, start; + u32 check_count = 0, check_value = 0, njobs = 0, nfl = 0, i; + const u8 *p = rpl + 12, *end = rpl + rpl_size; + s32 ctx_type = -1; + int err; + + if (ioc(DRM_IOCTL_PVR_CREATE_VM_CONTEXT, &cvm, "CREATE_VM_CONTEXT")) + return 2; + vm = cvm.handle; + while (p + 8 <= end) { + u32 type = rd32(p), len = rd32(p + 4); + const u8 *d = p + 8; + + if (d + len > end) + return 2; + p = d + len; + switch (type) { + case RPL_NAME: + puts_("recording: "); + for (i = 0; i < len; i++) + putch(d[i]); + putch('\n'); + break; + case RPL_BO: { + u32 idx = rd32(d); + struct bo *b = &bos[idx]; + struct drm_pvr_ioctl_create_bo_args c = { .size = rd64(d + 8) }; + struct drm_pvr_ioctl_get_bo_mmap_offset_args o = { 0 }; + + if (idx >= MAX_BOS) + return 2; + b->kind = rd32(d + 4); + b->size = c.size; + c.flags = b->kind == KIND_PM ? DRM_PVR_BO_PM_FW_PROTECT : + DRM_PVR_BO_BYPASS_DEVICE_CACHE | DRM_PVR_BO_ALLOW_CPU_USERSPACE_ACCESS; + if (ioc(DRM_IOCTL_PVR_CREATE_BO, &c, "CREATE_BO")) + return 2; + b->handle = c.handle; + if (b->kind != KIND_PM) { + o.handle = b->handle; + if (ioc(DRM_IOCTL_PVR_GET_BO_MMAP_OFFSET, &o, "GET_BO_MMAP_OFFSET")) + return 2; + b->map = dry ? sys_mmap(0, b->size, PROT_READ | PROT_WRITE, + MAP_PRIVATE | MAP_ANONYMOUS, -1, 0) : + sys_mmap(0, b->size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, + o.offset); + if (is_err((long)b->map)) { + puts_("a8-gpu-drm: mmap failed\n"); + return 2; + } + } + if (idx >= nbos) + nbos = idx + 1; + break; + } + case RPL_DATA: { + struct bo *b = &bos[rd32(d)]; + u64 off = rd64(d + 8); + + if (!b->map || off + len - 16 > b->size) + return 2; + memcpy(b->map + off, d + 16, len - 16); + break; + } + case RPL_MAP: { + struct bo *b = &bos[rd32(d)]; + struct drm_pvr_ioctl_vm_map_args m = { + .vm_context_handle = vm, .device_addr = rd64(d + 8), .handle = b->handle, + .size = rd64(d + 16), + }; + + if (ioc(DRM_IOCTL_PVR_VM_MAP, &m, "VM_MAP")) + return 2; + if (!b->va) + b->va = m.device_addr; + break; + } + case RPL_FREELIST: { + struct drm_pvr_ioctl_create_free_list_args f = { + .free_list_gpu_addr = rd64(d + 8), .initial_num_pages = rd32(d + 16), + .max_num_pages = rd32(d + 20), .grow_num_pages = rd32(d + 24), + .grow_threshold = rd32(d + 28), .vm_context_handle = vm, + }; + + if (nfl >= MAX_FREELISTS || ioc(DRM_IOCTL_PVR_CREATE_FREE_LIST, &f, "CREATE_FREE_LIST")) + return 2; + freelists[rd32(d)] = f.handle; + nfl++; + break; + } + case RPL_HWRT: { + struct drm_pvr_ioctl_create_hwrt_dataset_args h; + + if (len != 8 + sizeof(h)) + return 2; + memcpy(&h, d + 8, sizeof(h)); + h.free_list_handles[0] = freelists[rd32(d)]; + h.free_list_handles[1] = freelists[rd32(d + 4)]; + h.handle = 0; + if (ioc(DRM_IOCTL_PVR_CREATE_HWRT_DATASET, &h, "CREATE_HWRT_DATASET")) + return 2; + hwrt = h.handle; + break; + } + case RPL_CONTEXT: { + struct drm_pvr_ioctl_create_context_args c = { + .type = rd32(d), .priority = DRM_PVR_CTX_PRIORITY_NORMAL, + .vm_context_handle = vm, + }; + + if (ioc(DRM_IOCTL_PVR_CREATE_CONTEXT, &c, "CREATE_CONTEXT")) + return 2; + ctx = c.handle; + ctx_type = (s32)c.type; + break; + } + case RPL_JOB: + if (njobs >= MAX_JOBS || len - 16 > sizeof(streams[0])) + return 2; + memcpy(streams[njobs], d + 16, len - 16); + /* + * Every job's sync_ops carries the stride, even with no ops: the + * kernel checks the stride before the count (pvr_get_uobj_array), + * so a zeroed array is -EINVAL: the triangle's geometry + * job, the one job without the signal op. + */ + jobs[njobs] = (struct drm_pvr_job) { + .type = rd32(d), .context_handle = ctx, .flags = rd32(d + 4), + .cmd_stream_len = len - 16, .cmd_stream = (u64)streams[njobs], + .sync_ops = DRM_PVR_OBJ_ARRAY(0, &signal), + }; + if (jobs[njobs].type != DRM_PVR_JOB_TYPE_COMPUTE) + jobs[njobs].hwrt = (struct drm_pvr_hwrt_data_ref) { + .set_handle = hwrt, .data_index = rd32(d + 8), + }; + njobs++; + break; + case RPL_CHECK_STORE: + check_va = rd64(d); + check_count = rd32(d + 8); + check_value = rd32(d + 12); + break; + case RPL_CHECK_TRI: + check_va = rd64(d); + break; + default: + return 2; + } + } + if (!njobs || ctx_type < 0 || !check_va || triangle != (check_count == 0)) + return 2; + + if (ioc(DRM_IOCTL_SYNCOBJ_CREATE, &sync, "SYNCOBJ_CREATE")) + return 2; + signal = (struct drm_pvr_sync_op) { + .handle = sync.handle, + .flags = DRM_PVR_SYNC_OP_FLAG_HANDLE_TYPE_SYNCOBJ | DRM_PVR_SYNC_OP_FLAG_SIGNAL, + }; + jobs[njobs - 1].sync_ops = (struct drm_pvr_obj_array) DRM_PVR_OBJ_ARRAY(1, &signal); + { + struct drm_pvr_ioctl_submit_jobs_args s = { + .jobs = DRM_PVR_OBJ_ARRAY(njobs, jobs), + }; + struct drm_syncobj_wait w = { + .handles = (u64)&sync.handle, .count_handles = 1, + }; + struct ts t; + + puts_("submitting "); + putu(njobs); + puts_(triangle ? " render jobs\n" : " compute job\n"); + start = now_us(); + err = ioc(DRM_IOCTL_PVR_SUBMIT_JOBS, &s, "SUBMIT_JOBS"); + if (err) { + puts_(triangle ? "RESULT triangle FAIL submit " : "RESULT compute FAIL submit "); + puti(err); + putch('\n'); + return 1; + } + sys3(SYS_clock_gettime, CLOCK_MONOTONIC, &t, 0); + w.timeout_nsec = (t.sec + 10) * 1000000000ll + t.nsec; + err = ioc(DRM_IOCTL_SYNCOBJ_WAIT, &w, "SYNCOBJ_WAIT"); + puts_("jobs ended after "); + putu(now_us() - start); + puts_(" us\n"); + if (err) { + puts_(triangle ? "RESULT triangle FAIL wait " : "RESULT compute FAIL wait "); + puti(err); + putch('\n'); + return 1; + } + } + + if (triangle) { + struct bo *b = bo_at(check_va, TRI_DIM * TRI_DIM * 4); + + if (!b || !b->map) + return 2; + return check_triangle(b->map + (check_va - b->va)); + } + { + struct bo *b = bo_at(check_va, 4ull * check_count); + u32 good = 0, n; + const u8 *out; + + if (!b || !b->map) + return 2; + out = b->map + (check_va - b->va); + for (n = 0; n < check_count; n++) + good += rd32(out + 4 * n) == check_value; + puts_("out[0..3] "); + for (n = 0; n < 4; n++) { + puth(rd32(out + 4 * n)); + putch(' '); + } + putch('\n'); + puts_(good == check_count ? "RESULT compute PASS " : "RESULT compute FAIL "); + putu(good); + putch('/'); + putu(check_count); + puts_(" dwords "); + puth(check_value); + puts_(" at "); + puth(check_va); + putch('\n'); + return good == check_count ? 0 : 1; + } +} + +static int main_(int argc, char **argv) +{ + int err; + + if (argc == 4 && streq(argv[1], "dry")) { + dry = 1; + argv++; + argc--; + } + if (argc < 2 || (!streq(argv[1], "info") && argc < 3)) { + puts_("usage: a8-gpu-drm compute|triangle FILE.rpl | info | dry compute|triangle FILE.rpl\n"); + return 2; + } + fd = dry ? -2 : open_node(); + if (!dry && fd < 0) { + puts_("RESULT "); + puts_(argv[1]); + puts_(" FAIL no powervr render node\n"); + return 2; + } + if (streq(argv[1], "info")) + return info(); + err = load(argv[2]); + if (err) { + puts_("RESULT "); + puts_(argv[1]); + puts_(" FAIL cannot read the recording: "); + puti(err); + putch('\n'); + return 2; + } + err = replay(streq(argv[1], "triangle")); + if (err == 2) { + puts_("RESULT "); + puts_(argv[1]); + puts_(" FAIL the recording does not replay (see above)\n"); + } + return err; +} + +__attribute__((used)) void start_c(long *sp) +{ + int argc = (int)sp[0]; + char **argv = (char **)(sp + 1); + int r = main_(argc, argv); + + flush(); + sys_exit(r); +} + +__asm__(".text\n" + ".global _start\n" + "_start:\n" + " mov x0, sp\n" + " and sp, x0, #~15\n" + " bl start_c\n"); diff --git a/examples/ipod-gpu-drm/tools/extract-fw.py b/examples/ipod-gpu-drm/tools/extract-fw.py new file mode 100755 index 00000000..17840a1f --- /dev/null +++ b/examples/ipod-gpu-drm/tools/extract-fw.py @@ -0,0 +1,151 @@ +#!/usr/bin/env python3 +"""Make the A8 GPU firmware files from an Apple kernelcache you fetch yourself. + +Usage: + extract-fw.py OUT_DIR fetch the tested kernelcache (iPod touch 6, iOS 12.4 16G77) + from Apple's public IPSW over HTTP ranges (~15 MB, not the + whole 2.8 GB IPSW) + extract-fw.py OUT_DIR --ipsw URL BYTES BOARD the same from another IPSW, e.g. an iPad mini 4's (BOARD j96ap) + extract-fw.py OUT_DIR --kernelcache FILE a decompressed kernelcache you already have + (e.g. `ipsw download ipsw --device iPod7,1 --build 16G77 --kernel`) + +Writes: + OUT_DIR/gfx-t7000.bin the GPU firmware: AGXFirmwareKextG4P's META LDR image, byte for byte + OUT_DIR/gfx-t7000-border.bin BorderColorTable's first 0x800 bytes from AGXG4P's data; powervr's A8 + variant loads them beside the firmware and refuses to probe without them + +The A8 GPU is the same on every T7000 board, and the driver was written and tested against the firmware +of iOS 12.4 (16G77) for iPod7,1: that LDR's SHA-256 is pinned below. Another kernelcache's LDR is written +too, with a warning that it is not the tested one. The border table is found by its pinned SHA-256; if a +kernelcache does not hold that table, use the default (iPod) kernelcache for every A8 board. + +The kernelcache is decompressed by orchard's own code (src/orchard/apple). Nothing from Apple is stored in +this repository. +""" +import hashlib +import os +import struct +import sys + +ROOT = os.path.abspath(os.path.join(os.path.dirname(os.path.abspath(__file__)), '..', '..', '..')) +sys.path.insert(0, os.path.join(ROOT, 'src')) + +TESTED_IPSW = ('https://updates.cdn-apple.com/2019SummerFCS/fullrestores/041-86563/' + '9E4DEC4C-A743-11E9-87E4-CD93DBB619B1/iPodtouch_12.4_16G77_Restore.ipsw', 2813180896, 'n102ap') +LDR_MAGIC = 0x01AA5500 +TESTED_LDR_SHA256 = '0b60def67290391b4c6aa913d9a78a8105d6d9b03738175b8e5732689db1e5ba' +TABLE_LEN = 0x800 +TABLE_WORD = (0x440, 0x02040000) # a word of the table, to find candidates before hashing them +TABLE_SHA256 = '233ffc1d7db27d9f16db17e23a9ce33082b5a7b72061af3da510bca3c94aff3e' + + +def fail(msg): + raise SystemExit('extract-fw: ' + msg) + + +def ldr_length(blob): + """The META LDR image's length: the end of its last L1 record or L2 block.""" + if len(blob) < 16: + raise ValueError('truncated header') + magic, code, data, _, _ = struct.unpack_from(' len(blob): + raise ValueError('bad record list') + seen.add(pos) + cmd, size, nxt = struct.unpack_from(' len(blob): + raise ValueError('bad L1 record') + words = struct.unpack_from('<%dI' % ((size - 12) // 4), blob, pos + 8) + if cmd & 0x10 or cmd in (3, 5): + l2 = words[0] + elif cmd == 0: + l2 = words[1] + elif cmd == 4: + l2 = words[2] + else: + raise ValueError('unknown L1 command %#x' % cmd) + if l2 + 4 > len(blob): + raise ValueError('bad L2 offset') + l2size = struct.unpack_from(' len(blob): + raise ValueError('bad L2 block') + end = max(end, pos + size, l2 + l2size) + pos = nxt + return end + + +def find_ldr(kc): + found = [] + magic = struct.pack('= 0: + try: + found.append(kc[at:at + ldr_length(kc[at:])]) + except ValueError: + pass + at = kc.find(magic, at + 4) + if not found: + fail('no META LDR (GPU firmware) in this kernelcache') + tested = [f for f in found if hashlib.sha256(f).hexdigest() == TESTED_LDR_SHA256] + if tested: + return tested[0], True + if len(found) > 1: + fail('%d META LDR images and none is the tested one; use the default kernelcache' % len(found)) + return found[0], False + + +def find_table(kc): + off, word = TABLE_WORD + needle = struct.pack('= 0: + start = at - off + if start >= 0 and start % 16 == 0: + table = kc[start:start + TABLE_LEN] + if hashlib.sha256(table).hexdigest() == TABLE_SHA256: + return table + at = kc.find(needle, at + 1) + fail('this kernelcache does not hold the known BorderColorTable; run extract-fw.py OUT_DIR without ' + 'options (the iPod touch 6 kernelcache serves every A8 board)') + + +def main(argv): + if len(argv) == 1: + source = TESTED_IPSW + elif len(argv) == 5 and argv[1] == '--ipsw': + source = (argv[2], int(argv[3]), argv[4].lower()) + elif len(argv) == 3 and argv[1] == '--kernelcache': + source = argv[2] + else: + fail('usage: extract-fw.py OUT_DIR [--ipsw URL BYTES BOARD | --kernelcache FILE]') + out = argv[0] + if isinstance(source, str): + kc = open(source, 'rb').read() + if kc[:4] != b'\xcf\xfa\xed\xfe': + fail('%s is not a decompressed 64-bit Mach-O kernelcache' % source) + else: + from orchard.apple.remote_ipsw import Ipsw + url, size, board = source + print('extract-fw: reading the %s kernelcache from %s' % (board, url)) + kc = Ipsw(url, size).kernelcache(board) + + ldr, tested = find_ldr(kc) + table = find_table(kc) + os.makedirs(out, exist_ok=True) + for name, data in (('gfx-t7000.bin', ldr), ('gfx-t7000-border.bin', table)): + path = os.path.join(out, name) + with open(path, 'wb') as f: + f.write(data) + print('extract-fw: %s, %d bytes, sha256 %s' % (path, len(data), hashlib.sha256(data).hexdigest())) + if tested: + print('extract-fw: the GPU firmware is the tested one (iOS 12.4 16G77, iPod touch 6)') + else: + print('extract-fw: WARNING: the GPU firmware is not the tested one (iOS 12.4 16G77, iPod touch 6); ' + 'the driver may not boot it. Without options this tool fetches the tested one.') + + +if __name__ == '__main__': + main(sys.argv[1:]) diff --git a/examples/ipod-gpu-drm/tools/gen-replay.py b/examples/ipod-gpu-drm/tools/gen-replay.py new file mode 100755 index 00000000..4bdfa245 --- /dev/null +++ b/examples/ipod-gpu-drm/tools/gen-replay.py @@ -0,0 +1,186 @@ +#!/usr/bin/env python3 +"""Pack a Mesa A8 recording into a replay file for a8-gpu-drm. + +Usage: gen-replay.py compute|triangle MESA_OUT OUT.rpl + + compute k_store_lid.raw: one compute job; pass = 32 dwords 0x12345678 at the output BO 0x1_00116000 + triangle tri-job/tri-twiddled.raw: a geometry and a fragment job on HWRT set 1; pass = the 64x64 target + at 0x1_00116000, unflipped and twiddled + +MESA_OUT holds recordings of Mesa's jobs (one directory per job: its BOs, mappings and command stream), made +with Mesa (OrchardLinux/mesa, A8 branch) and a DRM call recorder that is not part of this example. The +generator accepts only the two recordings the bring-up runs used, by their sha256 prefixes (MESA_RAW_SHA, +TRI_RAW_SHA). + +File format (little endian): "A8RPLAY1", u32 version 1, then records {u32 type, u32 length, payload}: + 1 BO u32 index, u32 kind (0: CPU-mapped, 1: PM/firmware protected), u64 size + 2 MAP u32 index, u32 0, u64 VA, u64 size (BO offset 0) + 3 DATA u32 index, u32 0, u64 offset, bytes (non-zero 4 KiB pages only) + 4 FREELIST u32 index, u32 0, u64 VA, u32 initial, max, grow, threshold + 5 HWRT u32 free list 0, u32 free list 1, struct drm_pvr_ioctl_create_hwrt_dataset_args (0x88) + 6 CONTEXT u32 DRM_PVR_CTX_TYPE_* + 7 JOB u32 type, u32 flags, u32 HWRT data index, u32 0, the command stream + 8 STORE u64 VA, u32 count, u32 value + 9 TRI u64 VA + 10 NAME text + +BOs that hold a free list or an MList are created PM/firmware protected (powervr refuses a render job +otherwise) and get no data: on the A8 the kernel keeps its own parameter manager and never reads them. +""" +import hashlib +import os +import re +import struct +import sys + +# sha256 prefixes of the recordings the bring-up runs used (Mesa 5348247d857 compute, 67ca6f6897d triangle) +MESA_RAW_SHA = '944a22aad7ba0381' +TRI_RAW_SHA = 'a82dec6e55433ab5' + + +def fail(msg): + raise SystemExit('gen-replay: ' + msg) + + +def read_maps(raw, job): + maps = [] + for line in open(os.path.join(raw, '%s-maps.txt' % job)): + f = line.split() + if not f: + continue + va, size, h, off = int(f[0], 16), int(f[1], 16), f[2], f[3] + if off != 'off0': + fail('mapping at %x is not at BO offset 0' % va) + maps.append((va, size, h)) + return maps + + +class Out: + def __init__(self): + self.recs = [] + + def rec(self, t, payload): + self.recs.append(struct.pack(' %s (%d bytes, sha256 %s)' % (sys.argv[1], sys.argv[3], os.path.getsize(sys.argv[3]), + hashlib.sha256(open(sys.argv[3], 'rb').read()).hexdigest())) diff --git a/examples/ipod-gpu-drm/tools/verify-payload.sh b/examples/ipod-gpu-drm/tools/verify-payload.sh new file mode 100755 index 00000000..e5b8136b --- /dev/null +++ b/examples/ipod-gpu-drm/tools/verify-payload.sh @@ -0,0 +1,21 @@ +#!/usr/bin/env bash +# verify-payload.sh [OUT_DIR]: orchard's own pre-boot check (orchard.ram.verify_ram), without a device. +# It is what `orchard run --no-build` checks before booting: every file's digest and every recorded +# source path. A payload built in a scratch copy records the scratch paths; after the scratch dir is +# deleted the boot is refused ("/rootfs changed; rebuild before booting"). This also refuses +# any source that is not under examples/ipod-gpu-drm. +set -euo pipefail +here=$(cd "$(dirname "$0")/.." && pwd) +orchard=$(cd "$here/../.." && pwd) +out=${1:-$here/out/apple-4k} +cd "$orchard" +PYTHONPATH=src "${PYTHON:-python3}" - "$out" "$here" <<'EOF' +import sys +from orchard.ram import verify_ram +out, here = sys.argv[1], sys.argv[2] +r = verify_ram(out) +bad = [p for p in r.get('sources', {}) if '/examples/' in p and not p.startswith(here + '/')] +if bad: + sys.exit('verify-payload: sources outside %s: %s' % (here, bad[:3])) +print('verify-payload: OK (%d files, %d sources, all under %s)' % (len(r['files']), len(r.get('sources', {})), here)) +EOF From 8cebec98a72eb28f1a91a58ec35386a721501ec9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Berkay=20K=C3=B6sebay?= Date: Sat, 21 Nov 2026 12:22:48 +0100 Subject: [PATCH 5/8] examples: ipod-gpu-drm: the Vulkan smoke test and ladder a8-vk-smoke (Vulkan probe, compute, a triangle) and a8-vk-ladder, a set of Vulkan tests from one clear to vkmark's scenes with every pixel or value checked on the CPU, both MIT. vk/make-bundle.sh builds them with a Mesa build of the A8 branch into the self-contained /opt/a8vk bundle that build.sh's A8_VK_DIR stages, with vkmark when it is installed. Assisted-by: LLM --- examples/ipod-gpu-drm/vk/a8-vk-smoke.c | 401 +++ examples/ipod-gpu-drm/vk/k_store_const.comp | 6 + .../ipod-gpu-drm/vk/ladder/a8-vk-ladder.c | 2670 +++++++++++++++++ examples/ipod-gpu-drm/vk/ladder/l_big.comp | 5 + examples/ipod-gpu-drm/vk/ladder/l_const.frag | 4 + examples/ipod-gpu-drm/vk/ladder/l_lit.vert | 11 + examples/ipod-gpu-drm/vk/ladder/l_pc.frag | 5 + examples/ipod-gpu-drm/vk/ladder/l_rect.vert | 10 + examples/ipod-gpu-drm/vk/ladder/l_tex.frag | 6 + examples/ipod-gpu-drm/vk/ladder/l_tex2.frag | 7 + .../ipod-gpu-drm/vk/ladder/l_texmath.frag | 6 + examples/ipod-gpu-drm/vk/ladder/l_tri.vert | 4 + examples/ipod-gpu-drm/vk/ladder/l_ubo.frag | 5 + examples/ipod-gpu-drm/vk/ladder/l_vcol.frag | 4 + examples/ipod-gpu-drm/vk/ladder/l_vcol.vert | 6 + examples/ipod-gpu-drm/vk/ladder/l_verts.comp | 9 + examples/ipod-gpu-drm/vk/make-bundle.sh | 87 + examples/ipod-gpu-drm/vk/tri.frag | 4 + examples/ipod-gpu-drm/vk/tri.vert | 4 + 19 files changed, 3254 insertions(+) create mode 100644 examples/ipod-gpu-drm/vk/a8-vk-smoke.c create mode 100644 examples/ipod-gpu-drm/vk/k_store_const.comp create mode 100644 examples/ipod-gpu-drm/vk/ladder/a8-vk-ladder.c create mode 100644 examples/ipod-gpu-drm/vk/ladder/l_big.comp create mode 100644 examples/ipod-gpu-drm/vk/ladder/l_const.frag create mode 100644 examples/ipod-gpu-drm/vk/ladder/l_lit.vert create mode 100644 examples/ipod-gpu-drm/vk/ladder/l_pc.frag create mode 100644 examples/ipod-gpu-drm/vk/ladder/l_rect.vert create mode 100644 examples/ipod-gpu-drm/vk/ladder/l_tex.frag create mode 100644 examples/ipod-gpu-drm/vk/ladder/l_tex2.frag create mode 100644 examples/ipod-gpu-drm/vk/ladder/l_texmath.frag create mode 100644 examples/ipod-gpu-drm/vk/ladder/l_tri.vert create mode 100644 examples/ipod-gpu-drm/vk/ladder/l_ubo.frag create mode 100644 examples/ipod-gpu-drm/vk/ladder/l_vcol.frag create mode 100644 examples/ipod-gpu-drm/vk/ladder/l_vcol.vert create mode 100644 examples/ipod-gpu-drm/vk/ladder/l_verts.comp create mode 100755 examples/ipod-gpu-drm/vk/make-bundle.sh create mode 100644 examples/ipod-gpu-drm/vk/tri.frag create mode 100644 examples/ipod-gpu-drm/vk/tri.vert diff --git a/examples/ipod-gpu-drm/vk/a8-vk-smoke.c b/examples/ipod-gpu-drm/vk/a8-vk-smoke.c new file mode 100644 index 00000000..1c857d00 --- /dev/null +++ b/examples/ipod-gpu-drm/vk/a8-vk-smoke.c @@ -0,0 +1,401 @@ +/* SPDX-License-Identifier: MIT */ +/* + * a8-vk-smoke [probe|compute|triangle|all]: Mesa on the A8 through the real powervr driver (T7000 variant). + * One RESULT line per step, as a8-gpu-drm-test prints them: + * probe the Vulkan device (name, BVNC from the driver name), queue + * compute k_store_const (32 threads, out[i] = 0x12345678) into a host-visible buffer; pass = 32/32 + * triangle metal-triangle into a 64x64 B8G8R8A8 target (two mip levels: the twiddled layout the A8 drew first), + * clear (0, 0, 0.2, 1), colour (1, 0.25, 0, 1), copied to a host buffer; pass = every pixel is the + * triangle's colour inside it and the clear colour outside (one channel step of tolerance, edge pixels + * not judged) + * The shaders are built in (glslangValidator --vn headers). A8_SMOKE_NOCHECK=1 skips the result checks (e.g. on a fake DRM device: + * nothing executes). Exit status 0 only when every requested step passed. + */ +#include +#include +#include +#include +#include + +#include "a8_smoke_kstore.h" /* a8_smoke_kstore[] */ +#include "a8_smoke_vs.h" /* a8_smoke_vs[] */ +#include "a8_smoke_fs.h" /* a8_smoke_fs[] */ + +static int failed; + +#define DO(step, x) \ + do { \ + VkResult r_ = (x); \ + if (r_ != VK_SUCCESS) { \ + printf("RESULT %s FAIL %s = %d\n", step, #x, r_); \ + fflush(stdout); \ + return 1; \ + } \ + } while (0) + +struct ctx { + VkInstance inst; + VkPhysicalDevice phy; + VkDevice dev; + VkQueue q; + VkCommandPool pool; +}; + +static uint32_t mem_type(const struct ctx *c, uint32_t bits, VkMemoryPropertyFlags want) +{ + VkPhysicalDeviceMemoryProperties mp; + vkGetPhysicalDeviceMemoryProperties(c->phy, &mp); + for (uint32_t i = 0; i < mp.memoryTypeCount; i++) + if ((bits & (1u << i)) && (mp.memoryTypes[i].propertyFlags & want) == want) + return i; + return 0; +} + +static int host_buffer(const struct ctx *c, const char *step, VkDeviceSize size, VkBufferUsageFlags usage, + VkBuffer *buf, VkDeviceMemory *mem, void **map) +{ + const VkBufferCreateInfo bi = { .sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO, .size = size, .usage = usage }; + DO(step, vkCreateBuffer(c->dev, &bi, NULL, buf)); + VkMemoryRequirements mr; + vkGetBufferMemoryRequirements(c->dev, *buf, &mr); + const VkMemoryAllocateInfo ai = { + .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO, .allocationSize = mr.size, + .memoryTypeIndex = mem_type(c, mr.memoryTypeBits, + VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT) }; + DO(step, vkAllocateMemory(c->dev, &ai, NULL, mem)); + DO(step, vkBindBufferMemory(c->dev, *buf, *mem, 0)); + DO(step, vkMapMemory(c->dev, *mem, 0, VK_WHOLE_SIZE, 0, map)); + return 0; +} + +static int run(const struct ctx *c, const char *step, VkCommandBuffer cmd) +{ + VkFence fence; + const VkFenceCreateInfo fi = { .sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO }; + DO(step, vkCreateFence(c->dev, &fi, NULL, &fence)); + const VkSubmitInfo si = { .sType = VK_STRUCTURE_TYPE_SUBMIT_INFO, .commandBufferCount = 1, + .pCommandBuffers = &cmd }; + DO(step, vkQueueSubmit(c->q, 1, &si, fence)); + DO(step, vkWaitForFences(c->dev, 1, &fence, VK_TRUE, 5000000000ull)); + vkDestroyFence(c->dev, fence, NULL); + return 0; +} + +static int begin(const struct ctx *c, const char *step, VkCommandBuffer *cmd) +{ + const VkCommandBufferAllocateInfo ai = { .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO, + .commandPool = c->pool, .level = VK_COMMAND_BUFFER_LEVEL_PRIMARY, + .commandBufferCount = 1 }; + DO(step, vkAllocateCommandBuffers(c->dev, &ai, cmd)); + const VkCommandBufferBeginInfo bi = { .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO }; + DO(step, vkBeginCommandBuffer(*cmd, &bi)); + return 0; +} + +static int probe(struct ctx *c, int report) +{ + const VkApplicationInfo app = { .sType = VK_STRUCTURE_TYPE_APPLICATION_INFO, .apiVersion = VK_API_VERSION_1_1 }; + const VkInstanceCreateInfo ici = { .sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO, .pApplicationInfo = &app }; + DO("probe", vkCreateInstance(&ici, NULL, &c->inst)); + uint32_t n = 1; + VkResult r = vkEnumeratePhysicalDevices(c->inst, &n, &c->phy); + if ((r != VK_SUCCESS && r != VK_INCOMPLETE) || !n) { + printf("RESULT probe FAIL no Vulkan device (%d)\n", r); + return 1; + } + VkPhysicalDeviceProperties props; + vkGetPhysicalDeviceProperties(c->phy, &props); + const float prio = 1.0f; + const VkDeviceQueueCreateInfo qi = { .sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO, .queueCount = 1, + .pQueuePriorities = &prio }; + const VkDeviceCreateInfo di = { .sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO, .queueCreateInfoCount = 1, + .pQueueCreateInfos = &qi }; + DO("probe", vkCreateDevice(c->phy, &di, NULL, &c->dev)); + vkGetDeviceQueue(c->dev, 0, 0, &c->q); + const VkCommandPoolCreateInfo cpci = { .sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO }; + DO("probe", vkCreateCommandPool(c->dev, &cpci, NULL, &c->pool)); + if (report) + printf("RESULT probe PASS device \"%s\" api %u.%u.%u driver %#x\n", props.deviceName, + VK_API_VERSION_MAJOR(props.apiVersion), VK_API_VERSION_MINOR(props.apiVersion), + VK_API_VERSION_PATCH(props.apiVersion), props.driverVersion); + return 0; +} + +static int compute(const struct ctx *c, int check) +{ + const char *step = "compute"; + VkBuffer buf; + VkDeviceMemory mem; + uint32_t *out; + if (host_buffer(c, step, 4096, VK_BUFFER_USAGE_STORAGE_BUFFER_BIT, &buf, &mem, (void **)&out)) + return 1; + memset(out, 0, 4096); + + const VkDescriptorSetLayoutBinding b = { 0, VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, 1, VK_SHADER_STAGE_COMPUTE_BIT }; + const VkDescriptorSetLayoutCreateInfo dli = { .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO, + .bindingCount = 1, .pBindings = &b }; + VkDescriptorSetLayout dsl; + DO(step, vkCreateDescriptorSetLayout(c->dev, &dli, NULL, &dsl)); + const VkPipelineLayoutCreateInfo pli = { .sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO, + .setLayoutCount = 1, .pSetLayouts = &dsl }; + VkPipelineLayout pl; + DO(step, vkCreatePipelineLayout(c->dev, &pli, NULL, &pl)); + const VkShaderModuleCreateInfo smi = { .sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO, + .codeSize = sizeof(a8_smoke_kstore), .pCode = a8_smoke_kstore }; + VkShaderModule sm; + DO(step, vkCreateShaderModule(c->dev, &smi, NULL, &sm)); + const VkComputePipelineCreateInfo cpi = { + .sType = VK_STRUCTURE_TYPE_COMPUTE_PIPELINE_CREATE_INFO, + .stage = { .sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO, .stage = VK_SHADER_STAGE_COMPUTE_BIT, + .module = sm, .pName = "main" }, + .layout = pl }; + VkPipeline pipe; + DO(step, vkCreateComputePipelines(c->dev, VK_NULL_HANDLE, 1, &cpi, NULL, &pipe)); + const VkDescriptorPoolSize ps = { VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, 1 }; + const VkDescriptorPoolCreateInfo dpi = { .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO, .maxSets = 1, + .poolSizeCount = 1, .pPoolSizes = &ps }; + VkDescriptorPool dp; + DO(step, vkCreateDescriptorPool(c->dev, &dpi, NULL, &dp)); + const VkDescriptorSetAllocateInfo dsai = { .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO, + .descriptorPool = dp, .descriptorSetCount = 1, .pSetLayouts = &dsl }; + VkDescriptorSet ds; + DO(step, vkAllocateDescriptorSets(c->dev, &dsai, &ds)); + const VkDescriptorBufferInfo dbi = { buf, 0, VK_WHOLE_SIZE }; + const VkWriteDescriptorSet w = { .sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET, .dstSet = ds, + .descriptorCount = 1, .descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, + .pBufferInfo = &dbi }; + vkUpdateDescriptorSets(c->dev, 1, &w, 0, NULL); + + VkCommandBuffer cmd; + if (begin(c, step, &cmd)) + return 1; + vkCmdBindPipeline(cmd, VK_PIPELINE_BIND_POINT_COMPUTE, pipe); + vkCmdBindDescriptorSets(cmd, VK_PIPELINE_BIND_POINT_COMPUTE, pl, 0, 1, &ds, 0, NULL); + vkCmdDispatch(cmd, 1, 1, 1); + const VkMemoryBarrier mb = { .sType = VK_STRUCTURE_TYPE_MEMORY_BARRIER, .srcAccessMask = VK_ACCESS_SHADER_WRITE_BIT, + .dstAccessMask = VK_ACCESS_HOST_READ_BIT }; + vkCmdPipelineBarrier(cmd, VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT, VK_PIPELINE_STAGE_HOST_BIT, 0, 1, &mb, 0, NULL, + 0, NULL); + DO(step, vkEndCommandBuffer(cmd)); + if (run(c, step, cmd)) + return 1; + + unsigned good = 0; + for (unsigned i = 0; i < 32; i++) + good += out[i] == 0x12345678u; + const unsigned beyond = out[32]; + if (!check) + printf("RESULT compute DONE (not checked) %u/32 dwords 0x12345678\n", good); + else if (good == 32 && !beyond) + printf("RESULT compute PASS 32/32 dwords 0x12345678\n"); + else { + printf("RESULT compute FAIL %u/32 dwords 0x12345678 (out[0] %#x, out[31] %#x, out[32] %#x)\n", good, out[0], + out[31], beyond); + return 1; + } + return 0; +} + + +/* The signed distance in pixels of pixel centre (x, y) from the triangle (-0.5,-0.5) (0.5,-0.5) (0,0.5) on 64x64, + * positive inside: the smallest of the three edge distances */ +static float inside(int x, int y) +{ + const float px = x + 0.5f, py = y + 0.5f; + const float vx[3] = { 16, 48, 32 }, vy[3] = { 16, 16, 48 }; + float d = 1e9f; + for (int i = 0; i < 3; i++) { + const int j = (i + 1) % 3; + const float ex = vx[j] - vx[i], ey = vy[j] - vy[i]; + const float e = (ex * (py - vy[i]) - ey * (px - vx[i])) / sqrtf(ex * ex + ey * ey); + d = fminf(d, e); + } + return d; +} + +static int near(const uint8_t *p, const uint8_t *want) +{ + for (int i = 0; i < 4; i++) + if (abs((int)p[i] - (int)want[i]) > 1) + return 0; + return 1; +} + +static int triangle(const struct ctx *c, int check) +{ + const char *step = "triangle"; + const VkFormat fmt = VK_FORMAT_B8G8R8A8_UNORM; + const VkImageCreateInfo imi = { .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO, .imageType = VK_IMAGE_TYPE_2D, + .format = fmt, .extent = { 64, 64, 1 }, .mipLevels = 2, .arrayLayers = 1, + .samples = VK_SAMPLE_COUNT_1_BIT, .tiling = VK_IMAGE_TILING_OPTIMAL, + .usage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | VK_IMAGE_USAGE_TRANSFER_SRC_BIT }; + VkImage img; + DO(step, vkCreateImage(c->dev, &imi, NULL, &img)); + VkMemoryRequirements mr; + vkGetImageMemoryRequirements(c->dev, img, &mr); + const VkMemoryAllocateInfo iai = { .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO, .allocationSize = mr.size, + .memoryTypeIndex = mem_type(c, mr.memoryTypeBits, 0) }; + VkDeviceMemory imem; + DO(step, vkAllocateMemory(c->dev, &iai, NULL, &imem)); + DO(step, vkBindImageMemory(c->dev, img, imem, 0)); + const VkImageViewCreateInfo vi = { .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO, .image = img, + .viewType = VK_IMAGE_VIEW_TYPE_2D, .format = fmt, + .subresourceRange = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1 } }; + VkImageView view; + DO(step, vkCreateImageView(c->dev, &vi, NULL, &view)); + + static const float verts[12] = { -0.5f, -0.5f, 0, 1, 0.5f, -0.5f, 0, 1, 0, 0.5f, 0, 1 }; + VkBuffer vb, rb; + VkDeviceMemory vmem, rmem; + void *vmap; + uint8_t *px; + if (host_buffer(c, step, sizeof(verts), VK_BUFFER_USAGE_VERTEX_BUFFER_BIT, &vb, &vmem, &vmap) || + host_buffer(c, step, 64 * 64 * 4, VK_BUFFER_USAGE_TRANSFER_DST_BIT, &rb, &rmem, (void **)&px)) + return 1; + memcpy(vmap, verts, sizeof(verts)); + memset(px, 0x5a, 64 * 64 * 4); + + const VkAttachmentDescription ad = { .format = fmt, .samples = VK_SAMPLE_COUNT_1_BIT, + .loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR, .storeOp = VK_ATTACHMENT_STORE_OP_STORE, + .stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE, + .stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE, + .initialLayout = VK_IMAGE_LAYOUT_UNDEFINED, + .finalLayout = VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL }; + const VkAttachmentReference ar = { 0, VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL }; + const VkSubpassDescription sd = { .pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS, + .colorAttachmentCount = 1, .pColorAttachments = &ar }; + const VkRenderPassCreateInfo rpi = { .sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO, .attachmentCount = 1, + .pAttachments = &ad, .subpassCount = 1, .pSubpasses = &sd }; + VkRenderPass rp; + DO(step, vkCreateRenderPass(c->dev, &rpi, NULL, &rp)); + const VkFramebufferCreateInfo fbi = { .sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO, .renderPass = rp, + .attachmentCount = 1, .pAttachments = &view, .width = 64, .height = 64, + .layers = 1 }; + VkFramebuffer fb; + DO(step, vkCreateFramebuffer(c->dev, &fbi, NULL, &fb)); + + VkShaderModule vmod, fmod; + const VkShaderModuleCreateInfo vmi = { .sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO, + .codeSize = sizeof(a8_smoke_vs), .pCode = a8_smoke_vs }; + const VkShaderModuleCreateInfo fmi = { .sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO, + .codeSize = sizeof(a8_smoke_fs), .pCode = a8_smoke_fs }; + DO(step, vkCreateShaderModule(c->dev, &vmi, NULL, &vmod)); + DO(step, vkCreateShaderModule(c->dev, &fmi, NULL, &fmod)); + const VkPipelineShaderStageCreateInfo stages[2] = { + { .sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO, .stage = VK_SHADER_STAGE_VERTEX_BIT, + .module = vmod, .pName = "main" }, + { .sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO, .stage = VK_SHADER_STAGE_FRAGMENT_BIT, + .module = fmod, .pName = "main" }, + }; + const VkVertexInputBindingDescription vbd = { 0, 16, VK_VERTEX_INPUT_RATE_VERTEX }; + const VkVertexInputAttributeDescription vad = { 0, 0, VK_FORMAT_R32G32B32A32_SFLOAT, 0 }; + const VkPipelineVertexInputStateCreateInfo vis = { + .sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO, .vertexBindingDescriptionCount = 1, + .pVertexBindingDescriptions = &vbd, .vertexAttributeDescriptionCount = 1, .pVertexAttributeDescriptions = &vad }; + const VkPipelineInputAssemblyStateCreateInfo ias = { + .sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO, + .topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST }; + const VkViewport vp = { 0, 0, 64, 64, 0, 1 }; + const VkRect2D sc = { { 0, 0 }, { 64, 64 } }; + const VkPipelineViewportStateCreateInfo vps = { .sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO, + .viewportCount = 1, .pViewports = &vp, .scissorCount = 1, + .pScissors = &sc }; + const VkPipelineRasterizationStateCreateInfo rs = { + .sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO, .polygonMode = VK_POLYGON_MODE_FILL, + .cullMode = VK_CULL_MODE_NONE, .frontFace = VK_FRONT_FACE_COUNTER_CLOCKWISE, .lineWidth = 1.0f }; + const VkPipelineMultisampleStateCreateInfo ms = { .sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO, + .rasterizationSamples = VK_SAMPLE_COUNT_1_BIT }; + const VkPipelineColorBlendAttachmentState cba = { .colorWriteMask = 0xf }; + const VkPipelineColorBlendStateCreateInfo cbs = { .sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO, + .attachmentCount = 1, .pAttachments = &cba }; + const VkPipelineLayoutCreateInfo pli = { .sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO }; + VkPipelineLayout pl; + DO(step, vkCreatePipelineLayout(c->dev, &pli, NULL, &pl)); + const VkGraphicsPipelineCreateInfo gpi = { + .sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO, .stageCount = 2, .pStages = stages, + .pVertexInputState = &vis, .pInputAssemblyState = &ias, .pViewportState = &vps, .pRasterizationState = &rs, + .pMultisampleState = &ms, .pColorBlendState = &cbs, .layout = pl, .renderPass = rp }; + VkPipeline pipe; + DO(step, vkCreateGraphicsPipelines(c->dev, VK_NULL_HANDLE, 1, &gpi, NULL, &pipe)); + + VkCommandBuffer cmd; + if (begin(c, step, &cmd)) + return 1; + const VkClearValue cv = { .color = { .float32 = { 0.0f, 0.0f, 0.2f, 1.0f } } }; + const VkRenderPassBeginInfo rbi = { .sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO, .renderPass = rp, + .framebuffer = fb, .renderArea = sc, .clearValueCount = 1, + .pClearValues = &cv }; + vkCmdBeginRenderPass(cmd, &rbi, VK_SUBPASS_CONTENTS_INLINE); + vkCmdBindPipeline(cmd, VK_PIPELINE_BIND_POINT_GRAPHICS, pipe); + const VkDeviceSize off = 0; + vkCmdBindVertexBuffers(cmd, 0, 1, &vb, &off); + vkCmdDraw(cmd, 3, 1, 0, 0); + vkCmdEndRenderPass(cmd); + const VkBufferImageCopy copy = { .imageSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 }, + .imageExtent = { 64, 64, 1 } }; + vkCmdCopyImageToBuffer(cmd, img, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, rb, 1, ©); + const VkMemoryBarrier mb = { .sType = VK_STRUCTURE_TYPE_MEMORY_BARRIER, + .srcAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT, + .dstAccessMask = VK_ACCESS_HOST_READ_BIT }; + vkCmdPipelineBarrier(cmd, VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_HOST_BIT, 0, 1, &mb, 0, NULL, 0, + NULL); + DO(step, vkEndCommandBuffer(cmd)); + if (run(c, step, cmd)) + return 1; + + /* B8G8R8A8: clear (0, 0, 0.2, 1) = 33 00 00 ff, triangle (1, 0.25, 0, 1) = 00 40 ff ff */ + static const uint8_t clear_px[4] = { 0x33, 0x00, 0x00, 0xff }, tri_px[4] = { 0x00, 0x40, 0xff, 0xff }; + unsigned in_ok = 0, in_n = 0, out_ok = 0, out_n = 0; + int first_bad = -1; + for (int y = 0; y < 64; y++) + for (int x = 0; x < 64; x++) { + const float d = inside(x, y); + const uint8_t *p = px + 4 * (64 * y + x); + if (d > 0.75f) { + in_n++; + in_ok += near(p, tri_px); + if (!near(p, tri_px) && first_bad < 0) + first_bad = 64 * y + x; + } else if (d < -0.75f) { + out_n++; + out_ok += near(p, clear_px); + if (!near(p, clear_px) && first_bad < 0) + first_bad = 64 * y + x; + } + } + const uint8_t *ctr = px + 4 * (64 * 32 + 32), *cor = px; + if (!check) + printf("RESULT triangle DONE (not checked) inside %u/%u outside %u/%u\n", in_ok, in_n, out_ok, out_n); + else if (in_ok == in_n && out_ok == out_n) + printf("RESULT triangle PASS inside %u/%u outside %u/%u centre %02x%02x%02x%02x corner %02x%02x%02x%02x\n", + in_ok, in_n, out_ok, out_n, ctr[0], ctr[1], ctr[2], ctr[3], cor[0], cor[1], cor[2], cor[3]); + else { + const uint8_t *b = px + 4 * first_bad; + printf("RESULT triangle FAIL inside %u/%u outside %u/%u centre %02x%02x%02x%02x corner %02x%02x%02x%02x " + "first bad (%d,%d) %02x%02x%02x%02x\n", + in_ok, in_n, out_ok, out_n, ctr[0], ctr[1], ctr[2], ctr[3], cor[0], cor[1], cor[2], cor[3], + first_bad % 64, first_bad / 64, b[0], b[1], b[2], b[3]); + return 1; + } + return 0; +} + +int main(int argc, char **argv) +{ + const char *what = argc > 1 ? argv[1] : "all"; + const int check = !getenv("A8_SMOKE_NOCHECK"); + const int all = !strcmp(what, "all"); + if (!all && strcmp(what, "probe") && strcmp(what, "compute") && strcmp(what, "triangle")) { + fprintf(stderr, "usage: %s [probe|compute|triangle|all]\n", argv[0]); + return 2; + } + setvbuf(stdout, NULL, _IOLBF, 0); + struct ctx c = { 0 }; + if (probe(&c, all || !strcmp(what, "probe"))) + return 1; + if ((all || !strcmp(what, "compute")) && compute(&c, check)) + failed = 1; + if (!failed && (all || !strcmp(what, "triangle")) && triangle(&c, check)) + failed = 1; + return failed; +} diff --git a/examples/ipod-gpu-drm/vk/k_store_const.comp b/examples/ipod-gpu-drm/vk/k_store_const.comp new file mode 100644 index 00000000..7e0bded6 --- /dev/null +++ b/examples/ipod-gpu-drm/vk/k_store_const.comp @@ -0,0 +1,6 @@ +#version 450 +// The Metal k_store_const of tools/gpu-userspace/shaders/basic.metal (captured on the A8 by a8-shadercap): +// out[tid] = 0x12345678, one workgroup of 32 threads as a8-shadercap dispatched it. +layout(local_size_x = 32) in; +layout(set = 0, binding = 0, std430) buffer Out { uint d[]; } o; +void main() { o.d[gl_GlobalInvocationID.x] = 0x12345678u; } diff --git a/examples/ipod-gpu-drm/vk/ladder/a8-vk-ladder.c b/examples/ipod-gpu-drm/vk/ladder/a8-vk-ladder.c new file mode 100644 index 00000000..18d937dc --- /dev/null +++ b/examples/ipod-gpu-drm/vk/ladder/a8-vk-ladder.c @@ -0,0 +1,2670 @@ +/* SPDX-License-Identifier: MIT */ +/* + * a8-vk-ladder |list: a Vulkan escalation suite for Mesa on the A8 (powervr T7000 variant), one process per + * rung, run with the /opt/a8vk bundle's libraries (see ../README.md). Every pixel or value is checked on the + * CPU. Prints exactly one `RESULT PASS|FAIL|TIMEOUT|SKIP ` line; exit 0 PASS/SKIP, 1 FAIL, 3 TIMEOUT. + * Each submission waits at most A8_LADDER_TIMEOUT_MS (default 5000) on its fence; on expiry the rung prints TIMEOUT + * and leaves with _exit() without tearing anything down (the kernel reclaims the context with the process). + * A8_SMOKE_NOCHECK=1 skips the result checks (e.g. on a fake DRM device, where nothing executes). + * Self-reporting: before every step that enters the kernel driver (instance, device, each submit, each fence wait, + * teardown) it prints `STEP + ` and flushes, so a hang shows where it stopped. + * A8_LADDER_TEARDOWN=1: after the RESULT line, destroy the device and instance explicitly (each with its STEP line) + * instead of leaving everything to the file's release at exit. + * + * Render targets are B8G8R8A8_UNORM with two mip levels (the twiddled layout the A8 drew first); level 0 is drawn and + * copied to a host buffer with vkCmdCopyImageToBuffer. Rectangles are aligned to pixel boundaries, so every pixel centre + * is strictly inside or outside; for triangles the pixels within 0.75 px of an edge are not judged. + */ +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define MAX2(a, b) ((a) > (b) ? (a) : (b)) + +#include "l_big_comp.h" +#include "l_const_frag.h" +#include "l_pc_frag.h" +#include "l_rect_vert.h" +#include "l_tex_frag.h" +#include "l_tex2_frag.h" +#include "l_texmath_frag.h" +#include "l_lit_vert.h" +#include "l_tri_vert.h" +#include "l_ubo_frag.h" +#include "l_vcol_frag.h" +#include "l_vcol_vert.h" +#include "l_verts_comp.h" + +static const char *rung; +static int nocheck; +static uint64_t timeout_ns = 5000000000ull; +static VkInstance inst; +static VkPhysicalDevice phy; +static VkDevice dev; +static VkQueue q; +static VkCommandPool pool; +static VkPhysicalDeviceMemoryProperties memprops; +static double last_ms, sum_ms, max_ms, t_start; +static unsigned njobs; +static int teardown, timed_out; +static int skip_refusal; /* a refusal by Mesa is this rung's SKIP (a feature the A8 driver does not build) */ + +static double now_ms(void); +static void step(const char *fmt, ...) __attribute__((format(printf, 1, 2))); +static void step(const char *fmt, ...) +{ + va_list ap; + va_start(ap, fmt); + printf("STEP %s +%.1f ", rung ? rung : "-", now_ms() - t_start); + vprintf(fmt, ap); + printf("\n"); + va_end(ap); + fflush(stdout); +} + +static void finish(int code) +{ + fflush(stdout); + if (teardown && dev && !timed_out) { + step("vkDeviceWaitIdle"); + vkDeviceWaitIdle(dev); + step("vkDestroyDevice"); + vkDestroyDevice(dev, NULL); + step("vkDestroyInstance"); + vkDestroyInstance(inst, NULL); + step("teardown done"); + } + step("exit %d", code); + _exit(code); +} + +#define CK(x) \ + do { \ + VkResult r_ = (x); \ + if (r_ != VK_SUCCESS) { \ + printf("RESULT %s FAIL %s = %d\n", rung, #x, r_); \ + finish(1); \ + } \ + } while (0) + +static double now_ms(void) +{ + struct timespec t; + clock_gettime(CLOCK_MONOTONIC, &t); + return t.tv_sec * 1e3 + t.tv_nsec / 1e6; +} + +/* ---- objects ---- */ +static uint32_t memtype(uint32_t bits, VkMemoryPropertyFlags want) +{ + for (uint32_t i = 0; i < memprops.memoryTypeCount; i++) + if ((bits & (1u << i)) && (memprops.memoryTypes[i].propertyFlags & want) == want) + return i; + return 0; +} + +struct buf { + VkBuffer b; + VkDeviceMemory m; + void *p; +}; + +static struct buf newbuf(VkDeviceSize size, VkBufferUsageFlags usage) +{ + struct buf r; + const VkBufferCreateInfo bi = { .sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO, .size = size, .usage = usage }; + CK(vkCreateBuffer(dev, &bi, NULL, &r.b)); + VkMemoryRequirements mr; + vkGetBufferMemoryRequirements(dev, r.b, &mr); + const VkMemoryAllocateInfo ai = { + .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO, .allocationSize = mr.size, + .memoryTypeIndex = memtype(mr.memoryTypeBits, + VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT) }; + CK(vkAllocateMemory(dev, &ai, NULL, &r.m)); + CK(vkBindBufferMemory(dev, r.b, r.m, 0)); + CK(vkMapMemory(dev, r.m, 0, VK_WHOLE_SIZE, 0, &r.p)); + memset(r.p, 0, size); + return r; +} + +struct img { + VkImage i; + VkDeviceMemory m; + VkImageView v; + uint32_t w, h; + VkFormat f; +}; + +/* host: a linear-tiling image in host-visible memory, created PREINITIALIZED (texhostlinear) */ +static struct img newimg_ex(uint32_t w, uint32_t h, VkFormat f, VkImageUsageFlags usage, uint32_t mips, + uint32_t view_levels, int host); +static struct img newimg(uint32_t w, uint32_t h, VkFormat f, VkImageUsageFlags usage, uint32_t mips) +{ + return newimg_ex(w, h, f, usage, mips, 1, 0); +} + +static struct img newimg_ex(uint32_t w, uint32_t h, VkFormat f, VkImageUsageFlags usage, uint32_t mips, + uint32_t view_levels, int host) +{ + struct img r = { .w = w, .h = h, .f = f }; + const int depth = f == VK_FORMAT_D32_SFLOAT || f == VK_FORMAT_D16_UNORM; + const VkImageCreateInfo ii = { .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO, .imageType = VK_IMAGE_TYPE_2D, + .format = f, .extent = { w, h, 1 }, .mipLevels = mips, .arrayLayers = 1, + .samples = VK_SAMPLE_COUNT_1_BIT, + .tiling = host ? VK_IMAGE_TILING_LINEAR : VK_IMAGE_TILING_OPTIMAL, + .usage = usage, + .initialLayout = host ? VK_IMAGE_LAYOUT_PREINITIALIZED : VK_IMAGE_LAYOUT_UNDEFINED }; + CK(vkCreateImage(dev, &ii, NULL, &r.i)); + VkMemoryRequirements mr; + vkGetImageMemoryRequirements(dev, r.i, &mr); + const VkMemoryAllocateInfo ai = { .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO, .allocationSize = mr.size, + .memoryTypeIndex = memtype(mr.memoryTypeBits, host ? VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | + VK_MEMORY_PROPERTY_HOST_COHERENT_BIT : 0) }; + CK(vkAllocateMemory(dev, &ai, NULL, &r.m)); + CK(vkBindImageMemory(dev, r.i, r.m, 0)); + const VkImageViewCreateInfo vi = { + .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO, .image = r.i, .viewType = VK_IMAGE_VIEW_TYPE_2D, + .format = f, + .subresourceRange = { depth ? VK_IMAGE_ASPECT_DEPTH_BIT : VK_IMAGE_ASPECT_COLOR_BIT, 0, view_levels, 0, 1 } }; + CK(vkCreateImageView(dev, &vi, NULL, &r.v)); + return r; +} + +/* a target: colour attachment, copied out, optionally sampled */ +static struct img newtarget(uint32_t w, uint32_t h, VkImageUsageFlags extra) +{ + return newimg(w, h, VK_FORMAT_B8G8R8A8_UNORM, + VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | VK_IMAGE_USAGE_TRANSFER_SRC_BIT | extra, 2); +} + +static VkRenderPass newrp_ex(VkFormat depth, VkAttachmentLoadOp load, VkImageLayout initial, VkImageLayout final); +static VkRenderPass newrp(VkFormat depth, VkImageLayout final) +{ + return newrp_ex(depth, VK_ATTACHMENT_LOAD_OP_CLEAR, VK_IMAGE_LAYOUT_UNDEFINED, final); +} + +static VkRenderPass newrp_ex(VkFormat depth, VkAttachmentLoadOp load, VkImageLayout initial, VkImageLayout final) +{ + const VkAttachmentDescription ad[2] = { + { .format = VK_FORMAT_B8G8R8A8_UNORM, .samples = VK_SAMPLE_COUNT_1_BIT, .loadOp = load, + .storeOp = VK_ATTACHMENT_STORE_OP_STORE, .stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE, + .stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE, .initialLayout = initial, + .finalLayout = final }, + { .format = depth, .samples = VK_SAMPLE_COUNT_1_BIT, .loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR, + .storeOp = VK_ATTACHMENT_STORE_OP_DONT_CARE, .stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE, + .stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE, .initialLayout = VK_IMAGE_LAYOUT_UNDEFINED, + .finalLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL }, + }; + const VkAttachmentReference cr = { 0, VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL }; + const VkAttachmentReference dr = { 1, VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL }; + const VkSubpassDescription sd = { .pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS, + .colorAttachmentCount = 1, .pColorAttachments = &cr, + .pDepthStencilAttachment = depth ? &dr : NULL }; + const VkRenderPassCreateInfo rpi = { .sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO, + .attachmentCount = depth ? 2 : 1, .pAttachments = ad, .subpassCount = 1, + .pSubpasses = &sd }; + VkRenderPass rp; + CK(vkCreateRenderPass(dev, &rpi, NULL, &rp)); + return rp; +} + +static VkFramebuffer newfb(VkRenderPass rp, const struct img *c, const struct img *d) +{ + const VkImageView v[2] = { c->v, d ? d->v : VK_NULL_HANDLE }; + const VkFramebufferCreateInfo fi = { .sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO, .renderPass = rp, + .attachmentCount = d ? 2 : 1, .pAttachments = v, .width = c->w, + .height = c->h, .layers = 1 }; + VkFramebuffer fb; + CK(vkCreateFramebuffer(dev, &fi, NULL, &fb)); + return fb; +} + +static VkShaderModule shader(const uint32_t *code, size_t size) +{ + const VkShaderModuleCreateInfo si = { .sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO, .codeSize = size, + .pCode = code }; + VkShaderModule m; + CK(vkCreateShaderModule(dev, &si, NULL, &m)); + return m; +} +#define SHADER(x) shader(x, sizeof(x)) + +/* push constants: rect (NDC x0 y0 x1 y1), colour, z */ +struct pc { + float rect[4], color[4], z[4]; +}; + +static VkPipelineLayout newlayout(VkDescriptorSetLayout dsl) +{ + const VkPushConstantRange pr = { VK_SHADER_STAGE_VERTEX_BIT | VK_SHADER_STAGE_FRAGMENT_BIT, 0, + sizeof(struct pc) }; + const VkPipelineLayoutCreateInfo li = { .sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO, + .setLayoutCount = dsl ? 1 : 0, .pSetLayouts = &dsl, + .pushConstantRangeCount = 1, .pPushConstantRanges = &pr }; + VkPipelineLayout l; + CK(vkCreatePipelineLayout(dev, &li, NULL, &l)); + return l; +} + +static VkDescriptorSetLayout newdsl(VkDescriptorType type, VkShaderStageFlags stages) +{ + const VkDescriptorSetLayoutBinding b = { 0, type, 1, stages }; + const VkDescriptorSetLayoutCreateInfo li = { .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO, + .bindingCount = 1, .pBindings = &b }; + VkDescriptorSetLayout l; + CK(vkCreateDescriptorSetLayout(dev, &li, NULL, &l)); + return l; +} + +static VkDescriptorSet newset(VkDescriptorSetLayout dsl, VkDescriptorType type, const VkDescriptorBufferInfo *bi, + const VkDescriptorImageInfo *ii) +{ + const VkDescriptorPoolSize ps = { type, 1 }; + const VkDescriptorPoolCreateInfo pi = { .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO, .maxSets = 1, + .poolSizeCount = 1, .pPoolSizes = &ps }; + VkDescriptorPool dp; + CK(vkCreateDescriptorPool(dev, &pi, NULL, &dp)); + const VkDescriptorSetAllocateInfo ai = { .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO, + .descriptorPool = dp, .descriptorSetCount = 1, .pSetLayouts = &dsl }; + VkDescriptorSet s; + CK(vkAllocateDescriptorSets(dev, &ai, &s)); + const VkWriteDescriptorSet w = { .sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET, .dstSet = s, + .descriptorCount = 1, .descriptorType = type, .pBufferInfo = bi, + .pImageInfo = ii }; + vkUpdateDescriptorSets(dev, 1, &w, 0, NULL); + return s; +} + +enum vin { VIN_POS4, VIN_CORNER2, VIN_POS2_COL4, VIN_POS3_NRM3, VIN_POS2_TC2, VIN_POS3_NRM3_TC2 }; + +struct pipe_cfg { + VkShaderModule vs, fs; + enum vin vin; + int blend, depth; + VkPipelineLayout layout; + VkRenderPass rp; + uint32_t w, h; +}; + +static VkPipeline newpipe(const struct pipe_cfg *c) +{ + const VkPipelineShaderStageCreateInfo st[2] = { + { .sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO, .stage = VK_SHADER_STAGE_VERTEX_BIT, + .module = c->vs, .pName = "main" }, + { .sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO, .stage = VK_SHADER_STAGE_FRAGMENT_BIT, + .module = c->fs, .pName = "main" }, + }; + VkVertexInputBindingDescription vb = { 0, 16, VK_VERTEX_INPUT_RATE_VERTEX }; + VkVertexInputAttributeDescription va[3] = { { 0, 0, VK_FORMAT_R32G32B32A32_SFLOAT, 0 } }; + uint32_t nva = 1; + if (c->vin == VIN_CORNER2) { + vb.stride = 8; + va[0].format = VK_FORMAT_R32G32_SFLOAT; + } else if (c->vin == VIN_POS3_NRM3) { + vb.stride = 24; + va[0].format = VK_FORMAT_R32G32B32_SFLOAT; + va[1] = (VkVertexInputAttributeDescription){ 1, 0, VK_FORMAT_R32G32B32_SFLOAT, 12 }; + nva = 2; + } else if (c->vin == VIN_POS2_TC2) { + va[0].format = VK_FORMAT_R32G32_SFLOAT; + va[1] = (VkVertexInputAttributeDescription){ 1, 0, VK_FORMAT_R32G32_SFLOAT, 8 }; + nva = 2; + } else if (c->vin == VIN_POS3_NRM3_TC2) { + vb.stride = 32; + va[0].format = VK_FORMAT_R32G32B32_SFLOAT; + va[1] = (VkVertexInputAttributeDescription){ 1, 0, VK_FORMAT_R32G32B32_SFLOAT, 12 }; + va[2] = (VkVertexInputAttributeDescription){ 2, 0, VK_FORMAT_R32G32_SFLOAT, 24 }; + nva = 3; + } else if (c->vin == VIN_POS2_COL4) { + vb.stride = 24; + va[0].format = VK_FORMAT_R32G32_SFLOAT; + va[1] = (VkVertexInputAttributeDescription){ 1, 0, VK_FORMAT_R32G32B32A32_SFLOAT, 8 }; + nva = 2; + } + const VkPipelineVertexInputStateCreateInfo vis = { + .sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO, .vertexBindingDescriptionCount = 1, + .pVertexBindingDescriptions = &vb, .vertexAttributeDescriptionCount = nva, .pVertexAttributeDescriptions = va }; + const VkPipelineInputAssemblyStateCreateInfo ias = { + .sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO, + .topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST }; + const VkViewport vp = { 0, 0, (float)c->w, (float)c->h, 0, 1 }; + const VkRect2D sc = { { 0, 0 }, { c->w, c->h } }; + const VkPipelineViewportStateCreateInfo vps = { .sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO, + .viewportCount = 1, .pViewports = &vp, .scissorCount = 1, + .pScissors = &sc }; + const VkPipelineRasterizationStateCreateInfo rs = { + .sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO, .polygonMode = VK_POLYGON_MODE_FILL, + .cullMode = VK_CULL_MODE_NONE, .frontFace = VK_FRONT_FACE_COUNTER_CLOCKWISE, .lineWidth = 1.0f }; + const VkPipelineMultisampleStateCreateInfo ms = { .sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO, + .rasterizationSamples = VK_SAMPLE_COUNT_1_BIT }; + const VkPipelineDepthStencilStateCreateInfo ds = { + .sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO, .depthTestEnable = VK_TRUE, + .depthWriteEnable = VK_TRUE, .depthCompareOp = VK_COMPARE_OP_LESS }; + const VkPipelineColorBlendAttachmentState cba = { + .blendEnable = c->blend ? VK_TRUE : VK_FALSE, .srcColorBlendFactor = VK_BLEND_FACTOR_SRC_ALPHA, + .dstColorBlendFactor = VK_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA, .colorBlendOp = VK_BLEND_OP_ADD, + .srcAlphaBlendFactor = VK_BLEND_FACTOR_ONE, .dstAlphaBlendFactor = VK_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA, + .alphaBlendOp = VK_BLEND_OP_ADD, .colorWriteMask = 0xf }; + const VkPipelineColorBlendStateCreateInfo cbs = { .sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO, + .attachmentCount = 1, .pAttachments = &cba }; + const VkGraphicsPipelineCreateInfo gi = { + .sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO, .stageCount = 2, .pStages = st, + .pVertexInputState = &vis, .pInputAssemblyState = &ias, .pViewportState = &vps, .pRasterizationState = &rs, + .pMultisampleState = &ms, .pDepthStencilState = c->depth ? &ds : NULL, .pColorBlendState = &cbs, + .layout = c->layout, .renderPass = c->rp }; + VkPipeline p; + step("vkCreateGraphicsPipelines"); + const VkResult pr = vkCreateGraphicsPipelines(dev, VK_NULL_HANDLE, 1, &gi, NULL, &p); + if (pr != VK_SUCCESS) { + printf("RESULT %s %s refused at pipeline creation (vkCreateGraphicsPipelines = %d; the reason is the G5P line " + "above)\n", rung, skip_refusal ? "SKIP" : "FAIL", pr); + finish(skip_refusal ? 0 : 1); + } + return p; +} + +static VkCommandBuffer begin(int reusable) +{ + const VkCommandBufferAllocateInfo ai = { .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO, + .commandPool = pool, .level = VK_COMMAND_BUFFER_LEVEL_PRIMARY, + .commandBufferCount = 1 }; + VkCommandBuffer cmd; + CK(vkAllocateCommandBuffers(dev, &ai, &cmd)); + const VkCommandBufferBeginInfo bi = { .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO, + .flags = reusable ? 0 : VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT }; + CK(vkBeginCommandBuffer(cmd, &bi)); + return cmd; +} + +/* submit and wait at most timeout_ns; TIMEOUT ends the process */ +static void submit(VkCommandBuffer cmd) +{ + static VkFence fence; + if (!fence) { + const VkFenceCreateInfo fi = { .sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO }; + CK(vkCreateFence(dev, &fi, NULL, &fence)); + } else { + CK(vkResetFences(dev, 1, &fence)); + } + const VkSubmitInfo si = { .sType = VK_STRUCTURE_TYPE_SUBMIT_INFO, .commandBufferCount = 1, + .pCommandBuffers = &cmd }; + step("submit %u", njobs + 1); + const double t0 = now_ms(); + CK(vkQueueSubmit(q, 1, &si, fence)); + step("wait %u", njobs + 1); + const VkResult r = vkWaitForFences(dev, 1, &fence, VK_TRUE, timeout_ns); + last_ms = now_ms() - t0; + if (r == VK_TIMEOUT) { + timed_out = 1; + printf("RESULT %s TIMEOUT fence not signalled after %.0f ms (job %u)\n", rung, last_ms, njobs + 1); + finish(3); + } + step("done %u %.2f ms", njobs + 1, last_ms); + if (r != VK_SUCCESS) { + printf("RESULT %s FAIL vkWaitForFences = %d after %.1f ms (job %u)\n", rung, r, last_ms, njobs + 1); + finish(1); + } + njobs++; + sum_ms += last_ms; + if (last_ms > max_ms) + max_ms = last_ms; +} + +/* a failing vkEndCommandBuffer is a G5P refusal (its reason is the "G5P refuses:" line on stderr) */ +static void end_submit(VkCommandBuffer cmd) +{ + const VkResult r = vkEndCommandBuffer(cmd); + if (r != VK_SUCCESS) { + if (skip_refusal) { + printf("RESULT %s SKIP refused by the A8 driver (vkEndCommandBuffer = %d)\n", rung, r); + finish(0); + } + printf("RESULT %s FAIL refused by Mesa (vkEndCommandBuffer = %d; the reason is the G5P line above)\n", rung, r); + finish(1); + } + submit(cmd); +} + +static void barrier(VkCommandBuffer cmd, VkPipelineStageFlags src, VkAccessFlags sa, VkPipelineStageFlags dst, + VkAccessFlags da) +{ + const VkMemoryBarrier mb = { .sType = VK_STRUCTURE_TYPE_MEMORY_BARRIER, .srcAccessMask = sa, .dstAccessMask = da }; + vkCmdPipelineBarrier(cmd, src, dst, 0, 1, &mb, 0, NULL, 0, NULL); +} + +static void image_barrier(VkCommandBuffer cmd, VkImage img, VkImageLayout from, VkImageLayout to, + VkPipelineStageFlags src, VkAccessFlags sa, VkPipelineStageFlags dst, VkAccessFlags da) +{ + const VkImageMemoryBarrier b = { .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER, .srcAccessMask = sa, + .dstAccessMask = da, .oldLayout = from, .newLayout = to, + .srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED, + .dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED, .image = img, + .subresourceRange = { VK_IMAGE_ASPECT_COLOR_BIT, 0, VK_REMAINING_MIP_LEVELS, 0, 1 } }; + vkCmdPipelineBarrier(cmd, src, dst, 0, 0, NULL, 0, NULL, 1, &b); +} + +static void begin_pass(VkCommandBuffer cmd, VkRenderPass rp, VkFramebuffer fb, uint32_t w, uint32_t h, + const float clear[4], int depth) +{ + VkClearValue cv[2]; + memcpy(cv[0].color.float32, clear, 16); + cv[1].depthStencil = (VkClearDepthStencilValue){ 1.0f, 0 }; + const VkRenderPassBeginInfo bi = { .sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO, .renderPass = rp, + .framebuffer = fb, .renderArea = { { 0, 0 }, { w, h } }, + .clearValueCount = depth ? 2 : 1, .pClearValues = cv }; + vkCmdBeginRenderPass(cmd, &bi, VK_SUBPASS_CONTENTS_INLINE); +} + +/* the target's level 0 into a host buffer (the target is in TRANSFER_SRC_OPTIMAL after its pass) */ +static void readback(VkCommandBuffer cmd, const struct img *t, const struct buf *rb) +{ + const VkBufferImageCopy c = { .imageSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 }, + .imageExtent = { t->w, t->h, 1 } }; + vkCmdCopyImageToBuffer(cmd, t->i, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, rb->b, 1, &c); + barrier(cmd, VK_PIPELINE_STAGE_TRANSFER_BIT, VK_ACCESS_TRANSFER_WRITE_BIT, VK_PIPELINE_STAGE_HOST_BIT, + VK_ACCESS_HOST_READ_BIT); +} + +/* ---- expected images ---- */ +static uint8_t u8(float f) +{ + const long v = lroundf(f * 255.0f); + return v < 0 ? 0 : v > 255 ? 255 : (uint8_t)v; +} + +static void bgra(uint8_t *p, const float c[4]) +{ + p[0] = u8(c[2]); + p[1] = u8(c[1]); + p[2] = u8(c[0]); + p[3] = u8(c[3]); +} + +struct expect { + uint32_t w, h; + uint8_t *px; /* BGRA8 */ + uint8_t *skip; /* 1 = not judged */ +}; + +static struct expect newexpect(uint32_t w, uint32_t h, const float clear[4]) +{ + struct expect e = { w, h, malloc((size_t)w * h * 4), calloc((size_t)w * h, 1) }; + for (size_t i = 0; i < (size_t)w * h; i++) + bgra(e.px + 4 * i, clear); + return e; +} + +/* NDC rectangle, as the rasterizer covers it: pixel centres inside [x0, x1) x [y0, y1) */ +static void expect_rect(struct expect *e, const float r[4], const float c[4]) +{ + for (uint32_t y = 0; y < e->h; y++) + for (uint32_t x = 0; x < e->w; x++) { + const float nx = (x + 0.5f) / e->w * 2 - 1, ny = (y + 0.5f) / e->h * 2 - 1; + if (nx > fminf(r[0], r[2]) && nx < fmaxf(r[0], r[2]) && ny > fminf(r[1], r[3]) && ny < fmaxf(r[1], r[3])) + bgra(e->px + 4 * ((size_t)y * e->w + x), c); + } +} + +/* the smoke triangle (-0.5,-0.5) (0.5,-0.5) (0,0.5), edges +-0.75 px not judged */ +static void expect_tri(struct expect *e, const float c[4]) +{ + const float vx[3] = { 0.25f * e->w, 0.75f * e->w, 0.5f * e->w }, vy[3] = { 0.25f * e->h, 0.25f * e->h, 0.75f * e->h }; + for (uint32_t y = 0; y < e->h; y++) + for (uint32_t x = 0; x < e->w; x++) { + const float px = x + 0.5f, py = y + 0.5f; + float d = 1e9f; + for (int i = 0; i < 3; i++) { + const int j = (i + 1) % 3; + const float ex = vx[j] - vx[i], ey = vy[j] - vy[i]; + d = fminf(d, (ex * (py - vy[i]) - ey * (px - vx[i])) / sqrtf(ex * ex + ey * ey)); + } + const size_t k = (size_t)y * e->w + x; + if (fabsf(d) <= 0.75f) + e->skip[k] = 1; + else if (d > 0) + bgra(e->px + 4 * k, c); + } +} + +/* compare; prints the RESULT line (with extra) and ends the process on a failure */ +static void check(const struct expect *e, const uint8_t *got, int tol, const char *extra) +{ + if (nocheck) { + printf("RESULT %s PASS (not checked) %ux%u %s\n", rung, e->w, e->h, extra ? extra : ""); + finish(0); + } + size_t bad = 0, judged = 0, first = (size_t)-1; + for (size_t i = 0; i < (size_t)e->w * e->h; i++) { + if (e->skip[i]) + continue; + judged++; + for (int c = 0; c < 4; c++) + if (abs((int)got[4 * i + c] - (int)e->px[4 * i + c]) > tol) { + if (first == (size_t)-1) + first = i; + bad++; + break; + } + } + if (!bad) { + printf("RESULT %s PASS %ux%u %zu/%zu pixels %s\n", rung, e->w, e->h, judged, judged, extra ? extra : ""); + finish(0); + } + const uint8_t *g = got + 4 * first, *x = e->px + 4 * first; + printf("RESULT %s FAIL %ux%u %zu/%zu pixels wrong, first (%zu,%zu) got %02x%02x%02x%02x want %02x%02x%02x%02x %s\n", + rung, e->w, e->h, bad, judged, first % e->w, first / e->w, g[0], g[1], g[2], g[3], x[0], x[1], x[2], x[3], + extra ? extra : ""); + finish(1); +} + +/* ---- common draws ---- */ +static const float CLEAR[4] = { 0.0f, 0.0f, 0.25f, 1.0f }; +static const float TRI[4] = { 1.0f, 0.25f, 0.0f, 1.0f }; +static const float TRI_V[12] = { -0.5f, -0.5f, 0, 1, 0.5f, -0.5f, 0, 1, 0, 0.5f, 0, 1 }; +static const float CORNERS[12] = { 0, 0, 1, 0, 0, 1, 1, 0, 1, 1, 0, 1 }; + +static void rect_draw_uv(VkCommandBuffer cmd, VkPipelineLayout l, const float r[4], const float c[4], float z, + float su, float sv) +{ + struct pc pc = { { r[0], r[1], r[2], r[3] }, { c[0], c[1], c[2], c[3] }, { z, su, sv, 0 } }; + vkCmdPushConstants(cmd, l, VK_SHADER_STAGE_VERTEX_BIT | VK_SHADER_STAGE_FRAGMENT_BIT, 0, sizeof(pc), &pc); + vkCmdDraw(cmd, 6, 1, 0, 0); +} + +static void rect_draw(VkCommandBuffer cmd, VkPipelineLayout l, const float r[4], const float c[4], float z) +{ + struct pc pc = { { r[0], r[1], r[2], r[3] }, { c[0], c[1], c[2], c[3] }, { z, 1, 1, 0 } }; + vkCmdPushConstants(cmd, l, VK_SHADER_STAGE_VERTEX_BIT | VK_SHADER_STAGE_FRAGMENT_BIT, 0, sizeof(pc), &pc); + vkCmdDraw(cmd, 6, 1, 0, 0); +} + +/* the 4x4 grid of rung draws16: cell i in colour i */ +static void grid_colour(int i, float c[4]) +{ + static const float lv[4] = { 0.0f, 0.25f, 0.75f, 1.0f }; + c[0] = lv[i & 3]; + c[1] = lv[(i >> 2) & 3]; + c[2] = lv[(i + 1) & 3]; + c[3] = 1.0f; +} + +static void grid_rect(int i, float r[4]) +{ + const int cx = i & 3, cy = i >> 2; + r[0] = -1 + cx * 0.5f; + r[1] = -1 + cy * 0.5f; + r[2] = r[0] + 0.5f; + r[3] = r[1] + 0.5f; +} + +/* ---- rungs ---- */ +static void r_clear(uint32_t w, uint32_t h) +{ + struct img t = newtarget(w, h, 0); + VkRenderPass rp = newrp(VK_FORMAT_UNDEFINED, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL); + VkFramebuffer fb = newfb(rp, &t, NULL); + struct buf rb = newbuf((VkDeviceSize)w * h * 4, VK_BUFFER_USAGE_TRANSFER_DST_BIT); + const float clear[4] = { 0.25f, 0.5f, 0.75f, 1.0f }; + VkCommandBuffer cmd = begin(0); + begin_pass(cmd, rp, fb, w, h, clear, 0); + vkCmdEndRenderPass(cmd); + readback(cmd, &t, &rb); + end_submit(cmd); + struct expect e = newexpect(w, h, clear); + char d[64]; + snprintf(d, sizeof(d), "job %.1f ms", last_ms); + check(&e, rb.p, 1, d); +} + +/* the triangle pipeline and its vertex buffer */ +struct tri { + struct img t; + VkRenderPass rp; + VkFramebuffer fb; + VkPipeline p; + struct buf vb, rb; +}; + +static struct tri tri_setup(uint32_t w, uint32_t h) +{ + struct tri s; + s.t = newtarget(w, h, 0); + s.rp = newrp(VK_FORMAT_UNDEFINED, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL); + s.fb = newfb(s.rp, &s.t, NULL); + const struct pipe_cfg pc = { SHADER(l_tri_vert), SHADER(l_const_frag), VIN_POS4, 0, 0, newlayout(0), s.rp, w, h }; + s.p = newpipe(&pc); + s.vb = newbuf(sizeof(TRI_V), VK_BUFFER_USAGE_VERTEX_BUFFER_BIT); + memcpy(s.vb.p, TRI_V, sizeof(TRI_V)); + s.rb = newbuf((VkDeviceSize)w * h * 4, VK_BUFFER_USAGE_TRANSFER_DST_BIT); + return s; +} + +static void tri_record(VkCommandBuffer cmd, struct tri *s, VkBuffer vb) +{ + begin_pass(cmd, s->rp, s->fb, s->t.w, s->t.h, CLEAR, 0); + vkCmdBindPipeline(cmd, VK_PIPELINE_BIND_POINT_GRAPHICS, s->p); + const VkDeviceSize off = 0; + vkCmdBindVertexBuffers(cmd, 0, 1, &vb, &off); + vkCmdDraw(cmd, 3, 1, 0, 0); + vkCmdEndRenderPass(cmd); + readback(cmd, &s->t, &s->rb); +} + +static void r_tri(uint32_t w, uint32_t h) +{ + struct tri s = tri_setup(w, h); + VkCommandBuffer cmd = begin(0); + tri_record(cmd, &s, s.vb.b); + end_submit(cmd); + struct expect e = newexpect(w, h, CLEAR); + expect_tri(&e, TRI); + char d[64]; + snprintf(d, sizeof(d), "job %.1f ms", last_ms); + check(&e, s.rb.p, 1, d); +} + +static void r_frames(void) +{ + struct tri s = tri_setup(64, 64); + VkCommandBuffer cmd = begin(1); + tri_record(cmd, &s, s.vb.b); + CK(vkEndCommandBuffer(cmd)); + struct expect e = newexpect(64, 64, CLEAR); + expect_tri(&e, TRI); + for (int f = 0; f < 100; f++) { + memset(s.rb.p, 0x5a, 64 * 64 * 4); + submit(cmd); + if (!nocheck && f < 99) { + size_t bad = 0; + for (size_t i = 0; i < 64 * 64; i++) + if (!e.skip[i] && memcmp((uint8_t *)s.rb.p + 4 * i, e.px + 4 * i, 4) && + (abs(((uint8_t *)s.rb.p)[4 * i] - e.px[4 * i]) > 1 || + abs(((uint8_t *)s.rb.p)[4 * i + 1] - e.px[4 * i + 1]) > 1 || + abs(((uint8_t *)s.rb.p)[4 * i + 2] - e.px[4 * i + 2]) > 1 || + abs(((uint8_t *)s.rb.p)[4 * i + 3] - e.px[4 * i + 3]) > 1)) + bad++; + if (bad) { + printf("RESULT %s FAIL frame %d: %zu pixels wrong (avg %.2f ms, max %.2f ms over %u jobs)\n", rung, f, + bad, sum_ms / njobs, max_ms, njobs); + finish(1); + } + } + } + char d[96]; + snprintf(d, sizeof(d), "100 frames, fence wait avg %.2f ms max %.2f ms", sum_ms / njobs, max_ms); + check(&e, s.rb.p, 1, d); +} + +/* the rectangle pipeline (push constants), optional blend / depth / descriptor set */ +struct rect { + struct img t, d; + VkRenderPass rp; + VkFramebuffer fb; + VkPipelineLayout l; + VkPipeline p; + struct buf vb, rb; +}; + +static struct rect rect_setup(uint32_t w, uint32_t h, VkShaderModule fs, int blend, VkFormat depth, + VkDescriptorSetLayout dsl, VkImageUsageFlags extra, VkImageLayout final) +{ + struct rect s = { 0 }; + s.t = newtarget(w, h, extra); + if (depth) + s.d = newimg(w, h, depth, VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT, 1); + s.rp = newrp(depth, final); + s.fb = newfb(s.rp, &s.t, depth ? &s.d : NULL); + s.l = newlayout(dsl); + const struct pipe_cfg pc = { SHADER(l_rect_vert), fs, VIN_CORNER2, blend, depth != 0, s.l, s.rp, w, h }; + s.p = newpipe(&pc); + s.vb = newbuf(sizeof(CORNERS), VK_BUFFER_USAGE_VERTEX_BUFFER_BIT); + memcpy(s.vb.p, CORNERS, sizeof(CORNERS)); + s.rb = newbuf((VkDeviceSize)w * h * 4, VK_BUFFER_USAGE_TRANSFER_DST_BIT); + return s; +} + +static void rect_begin(VkCommandBuffer cmd, struct rect *s, const float clear[4]) +{ + begin_pass(cmd, s->rp, s->fb, s->t.w, s->t.h, clear, s->d.i != VK_NULL_HANDLE); + vkCmdBindPipeline(cmd, VK_PIPELINE_BIND_POINT_GRAPHICS, s->p); + const VkDeviceSize off = 0; + vkCmdBindVertexBuffers(cmd, 0, 1, &s->vb.b, &off); +} + +static const float FULL[4] = { -1, -1, 1, 1 }; + +static void r_pc(void) +{ + struct rect s = rect_setup(64, 64, SHADER(l_pc_frag), 0, 0, 0, 0, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL); + const float c[4] = { 0.75f, 0.25f, 1.0f, 1.0f }, r[4] = { -0.5f, -1.0f, 0.75f, 0.5f }; + VkCommandBuffer cmd = begin(0); + rect_begin(cmd, &s, CLEAR); + rect_draw(cmd, s.l, r, c, 0.5f); + vkCmdEndRenderPass(cmd); + readback(cmd, &s.t, &s.rb); + end_submit(cmd); + struct expect e = newexpect(64, 64, CLEAR); + expect_rect(&e, r, c); + check(&e, s.rb.p, 1, "one push-constant rectangle"); +} + +static void grid_draws(VkCommandBuffer cmd, VkPipelineLayout l) +{ + for (int i = 0; i < 16; i++) { + float r[4], c[4]; + grid_rect(i, r); + grid_colour(i, c); + rect_draw(cmd, l, r, c, 0.5f); + } +} + +static void grid_expect(struct expect *e) +{ + for (int i = 0; i < 16; i++) { + float r[4], c[4]; + grid_rect(i, r); + grid_colour(i, c); + expect_rect(e, r, c); + } +} + +static void r_draws16(void) +{ + struct rect s = rect_setup(64, 64, SHADER(l_pc_frag), 0, 0, 0, 0, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL); + VkCommandBuffer cmd = begin(0); + rect_begin(cmd, &s, CLEAR); + grid_draws(cmd, s.l); + vkCmdEndRenderPass(cmd); + readback(cmd, &s.t, &s.rb); + end_submit(cmd); + struct expect e = newexpect(64, 64, CLEAR); + grid_expect(&e); + check(&e, s.rb.p, 1, "16 draws, 16 colours"); +} + +static void r_vbib(void) +{ + struct img t = newtarget(64, 64, 0); + VkRenderPass rp = newrp(VK_FORMAT_UNDEFINED, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL); + VkFramebuffer fb = newfb(rp, &t, NULL); + const struct pipe_cfg pc = { SHADER(l_vcol_vert), SHADER(l_vcol_frag), VIN_POS2_COL4, 0, 0, newlayout(0), rp, + 64, 64 }; + VkPipeline p = newpipe(&pc); + /* two quads (left half 0.75/0/0.25, right half 0/1/0.5), four vertices each, flat colour per quad */ + const float L[4] = { 0.75f, 0.0f, 0.25f, 1.0f }, R[4] = { 0.0f, 1.0f, 0.5f, 1.0f }; + float v[8][6]; + const float qx[2][2] = { { -1.0f, 0.0f }, { 0.0f, 1.0f } }; + for (int qd = 0; qd < 2; qd++) + for (int k = 0; k < 4; k++) { + float *o = v[4 * qd + k]; + o[0] = qx[qd][k & 1]; + o[1] = (k & 2) ? 0.75f : -0.75f; + memcpy(o + 2, qd ? R : L, 16); + } + const uint16_t idx[12] = { 0, 1, 2, 1, 3, 2, 4, 5, 6, 5, 7, 6 }; + struct buf vb = newbuf(sizeof(v), VK_BUFFER_USAGE_VERTEX_BUFFER_BIT); + struct buf ib = newbuf(sizeof(idx), VK_BUFFER_USAGE_INDEX_BUFFER_BIT); + struct buf rb = newbuf(64 * 64 * 4, VK_BUFFER_USAGE_TRANSFER_DST_BIT); + memcpy(vb.p, v, sizeof(v)); + memcpy(ib.p, idx, sizeof(idx)); + VkCommandBuffer cmd = begin(0); + begin_pass(cmd, rp, fb, 64, 64, CLEAR, 0); + vkCmdBindPipeline(cmd, VK_PIPELINE_BIND_POINT_GRAPHICS, p); + const VkDeviceSize off = 0; + vkCmdBindVertexBuffers(cmd, 0, 1, &vb.b, &off); + vkCmdBindIndexBuffer(cmd, ib.b, 0, VK_INDEX_TYPE_UINT16); + vkCmdDrawIndexed(cmd, 12, 1, 0, 0, 0); + vkCmdEndRenderPass(cmd); + readback(cmd, &t, &rb); + end_submit(cmd); + struct expect e = newexpect(64, 64, CLEAR); + const float rl[4] = { -1, -0.75f, 0, 0.75f }, rr[4] = { 0, -0.75f, 1, 0.75f }; + expect_rect(&e, rl, L); + expect_rect(&e, rr, R); + check(&e, rb.p, 1, "indexed draw, vertex colours"); +} + +static void r_ubo(void) +{ + VkDescriptorSetLayout dsl = newdsl(VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, VK_SHADER_STAGE_FRAGMENT_BIT); + struct rect s = rect_setup(64, 64, SHADER(l_ubo_frag), 0, 0, dsl, 0, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL); + struct buf u = newbuf(256, VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT); + const float c[4] = { 0.25f, 0.75f, 0.5f, 1.0f }; + memcpy(u.p, c, 16); + const VkDescriptorBufferInfo bi = { u.b, 0, 16 }; + VkDescriptorSet set = newset(dsl, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, &bi, NULL); + VkCommandBuffer cmd = begin(0); + rect_begin(cmd, &s, CLEAR); + vkCmdBindDescriptorSets(cmd, VK_PIPELINE_BIND_POINT_GRAPHICS, s.l, 0, 1, &set, 0, NULL); + rect_draw(cmd, s.l, FULL, c, 0.5f); + vkCmdEndRenderPass(cmd); + readback(cmd, &s.t, &s.rb); + end_submit(cmd); + struct expect e = newexpect(64, 64, c); + check(&e, s.rb.p, 1, "uniform-buffer colour"); +} + +static void r_blend(void) +{ + struct rect s = rect_setup(64, 64, SHADER(l_pc_frag), 1, 0, 0, 0, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL); + const float dst[4] = { 0.0f, 0.0f, 1.0f, 1.0f }, src[4] = { 1.0f, 0.5f, 0.0f, 0.5f }; + const float r[4] = { -1.0f, -1.0f, 0.0f, 1.0f }; + VkCommandBuffer cmd = begin(0); + rect_begin(cmd, &s, dst); + rect_draw(cmd, s.l, r, src, 0.5f); + vkCmdEndRenderPass(cmd); + readback(cmd, &s.t, &s.rb); + end_submit(cmd); + struct expect e = newexpect(64, 64, dst); + const float out[4] = { 0.5f, 0.25f, 0.5f, 1.0f }; /* src*a + dst*(1-a); alpha a + 1*(1-a) */ + expect_rect(&e, r, out); + check(&e, s.rb.p, 2, "SRC_ALPHA / ONE_MINUS_SRC_ALPHA over the left half"); +} + +static void r_depth(VkFormat f) +{ + /* G5P on the A8 builds D32F and D32F_S8 at one sample (the captured ZLS forms); D16 is refused today */ + skip_refusal = f == VK_FORMAT_D16_UNORM; + struct rect s = rect_setup(64, 64, SHADER(l_pc_frag), 0, f, 0, 0, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL); + const float c1[4] = { 1, 0, 0, 1 }, c2[4] = { 0, 1, 0, 1 }, c3[4] = { 0, 0.25f, 0.75f, 1 }; + const float left[4] = { -1, -1, 0, 1 }; + VkCommandBuffer cmd = begin(0); + rect_begin(cmd, &s, CLEAR); + rect_draw(cmd, s.l, FULL, c1, 0.75f); /* everywhere at 0.75 */ + rect_draw(cmd, s.l, left, c2, 0.25f); /* the left half in front */ + rect_draw(cmd, s.l, FULL, c3, 0.5f); /* in front of c1, behind c2 */ + vkCmdEndRenderPass(cmd); + readback(cmd, &s.t, &s.rb); + end_submit(cmd); + struct expect e = newexpect(64, 64, c3); + expect_rect(&e, left, c2); + check(&e, s.rb.p, 1, f == VK_FORMAT_D32_SFLOAT ? "D32 LESS, 3 layers" : "D16 LESS, 3 layers"); +} + +/* a 64x64 BGRA8 texture: B = 4x, G = 4y, R = 2(x + y), A = 255 */ +static uint8_t tex_val(int x, int y, int c) +{ + return c == 0 ? 4 * x : c == 1 ? 4 * y : c == 2 ? 2 * (x + y) : 255; +} + +/* texmap's texels: B = 4x, G = 4y, R = 255 - 2(x + y) (a check), A = 255 */ +static uint8_t map_val(int x, int y, int c) +{ + return c == 0 ? 4 * x : c == 1 ? 4 * y : c == 2 ? 255 - 2 * (x + y) : 255; +} + +static struct img upload_texture_fn(uint32_t levels, uint8_t (*val)(int, int, int)); +static struct img upload_texture(void) +{ + return upload_texture_fn(2, tex_val); +} + +static struct img upload_texture_fn(uint32_t levels, uint8_t (*val)(int, int, int)) +{ + /* two levels: the twiddled layout every Apple A8 texture capture has (a one-level image would be linear, a + * Rogue stride state Apple's captures never show); level 0 is filled and sampled (sampler maxLod 0) */ + struct img t = newimg(64, 64, VK_FORMAT_B8G8R8A8_UNORM, VK_IMAGE_USAGE_SAMPLED_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT, + levels); + struct buf st = newbuf(64 * 64 * 4, VK_BUFFER_USAGE_TRANSFER_SRC_BIT); + uint8_t *p = st.p; + for (int y = 0; y < 64; y++) + for (int x = 0; x < 64; x++) + for (int c = 0; c < 4; c++) + p[4 * (64 * y + x) + c] = val(x, y, c); + VkCommandBuffer cmd = begin(0); + image_barrier(cmd, t.i, VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, + VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT, 0, VK_PIPELINE_STAGE_TRANSFER_BIT, VK_ACCESS_TRANSFER_WRITE_BIT); + const VkBufferImageCopy c = { .imageSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 }, + .imageExtent = { 64, 64, 1 } }; + vkCmdCopyBufferToImage(cmd, st.b, t.i, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, &c); + image_barrier(cmd, t.i, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL, + VK_PIPELINE_STAGE_TRANSFER_BIT, VK_ACCESS_TRANSFER_WRITE_BIT, VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT, + VK_ACCESS_SHADER_READ_BIT); + end_submit(cmd); + return t; +} + +static VkSampler newsampler(VkFilter f) +{ + const VkSamplerCreateInfo si = { .sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO, .magFilter = f, .minFilter = f, + .mipmapMode = VK_SAMPLER_MIPMAP_MODE_NEAREST, + .addressModeU = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE, + .addressModeV = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE, + .addressModeW = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE, .maxLod = 0.0f }; + VkSampler s; + CK(vkCreateSampler(dev, &si, NULL, &s)); + return s; +} + +/* draw the texture over the whole target (uv 0..1) and return the readback */ +static uint8_t *sample_pass(const struct img *tex, VkFilter filter, uint32_t w, uint32_t h) +{ + VkDescriptorSetLayout dsl = newdsl(VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, VK_SHADER_STAGE_FRAGMENT_BIT); + struct rect s = rect_setup(w, h, SHADER(l_tex_frag), 0, 0, dsl, 0, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL); + const VkDescriptorImageInfo ii = { newsampler(filter), tex->v, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL }; + VkDescriptorSet set = newset(dsl, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, NULL, &ii); + const float black[4] = { 0, 0, 0, 0 }; + VkCommandBuffer cmd = begin(0); + rect_begin(cmd, &s, black); + vkCmdBindDescriptorSets(cmd, VK_PIPELINE_BIND_POINT_GRAPHICS, s.l, 0, 1, &set, 0, NULL); + rect_draw(cmd, s.l, FULL, black, 0.5f); + vkCmdEndRenderPass(cmd); + readback(cmd, &s.t, &s.rb); + end_submit(cmd); + return s.rb.p; +} + +static void r_tex(int linear) +{ + struct img tex = upload_texture(); + const uint32_t n = linear ? 128 : 64; + uint8_t *got = sample_pass(&tex, linear ? VK_FILTER_LINEAR : VK_FILTER_NEAREST, n, n); + const float z[4] = { 0, 0, 0, 0 }; + struct expect e = newexpect(n, n, z); + for (uint32_t y = 0; y < n; y++) + for (uint32_t x = 0; x < n; x++) { + uint8_t *o = e.px + 4 * ((size_t)y * n + x); + if (!linear) { + for (int c = 0; c < 4; c++) + o[c] = tex_val(x, y, c); + continue; + } + /* bilinear at texel coordinate (x + 0.5) / 2 - 0.5, clamped to the edge */ + const float u = (x + 0.5f) * 0.5f - 0.5f, v = (y + 0.5f) * 0.5f - 0.5f; + const int x0 = (int)floorf(u), y0 = (int)floorf(v); + const float fu = u - x0, fv = v - y0; + for (int c = 0; c < 4; c++) { + float acc = 0; + for (int j = 0; j < 2; j++) + for (int i = 0; i < 2; i++) { + int tx = x0 + i, ty = y0 + j; + tx = tx < 0 ? 0 : tx > 63 ? 63 : tx; + ty = ty < 0 ? 0 : ty > 63 ? 63 : ty; + acc += tex_val(tx, ty, c) * (i ? fu : 1 - fu) * (j ? fv : 1 - fv); + } + o[c] = (uint8_t)lroundf(acc); + } + } + check(&e, got, linear ? 3 : 1, linear ? "64x64 texture, LINEAR to 128x128" : "64x64 texture, NEAREST"); +} + +/* least squares of v ~ a x + b y + c over n points (sums given), printed */ +static void fit_print(const char *what, const double s[10], double n) +{ + /* s: sx sy sxx syy sxy sv svx svy (v for one axis), solve the 3x3 normal equations */ + const double m[3][4] = { { s[2], s[4], s[0], s[6] }, { s[4], s[3], s[1], s[7] }, { s[0], s[1], n, s[5] } }; + double a[3][4]; + memcpy(a, m, sizeof(a)); + for (int i = 0; i < 3; i++) { + int piv = i; + for (int r = i + 1; r < 3; r++) + if (fabs(a[r][i]) > fabs(a[piv][i])) + piv = r; + for (int c = 0; c < 4; c++) { + const double t = a[i][c]; + a[i][c] = a[piv][c]; + a[piv][c] = t; + } + if (fabs(a[i][i]) < 1e-9) { + printf("TEXMAP %s fit: singular\n", what); + return; + } + for (int r = 0; r < 3; r++) + if (r != i) { + const double f = a[r][i] / a[i][i]; + for (int c = i; c < 4; c++) + a[r][c] -= f * a[i][c]; + } + } + printf("TEXMAP %s = %.3f*px %+.3f*py %+.2f\n", what, a[0][3] / a[0][0], a[1][3] / a[1][1], a[2][3] / a[2][2]); +} + +/* texmap: every pixel's source texel. The texture holds (x, y) in its colour (map_val), sampled NEAREST 1:1 over the + * whole 64x64 target (two triangles, uv from the corners). Prints: counts of correct / wrong-but-a-texel (inside the + * texture) / not a texel (outside: stale or foreign memory); how many wrong pixels fit a y-flip, an x-flip, a transpose + * (also a swapped Morton order) or a 180-degree turn; a least-squares affine fit of the source texel per triangle; the + * first 16 mismatches (px,py)->(tx,ty) or (px,py)->raw. */ +static void r_texmap(uint32_t levels) +{ + struct img tex = upload_texture_fn(levels, map_val); + const uint8_t *got = sample_pass(&tex, VK_FILTER_NEAREST, 64, 64); + if (nocheck) { + printf("RESULT %s PASS (not checked) %u level(s)\n", rung, levels); + finish(0); + } + unsigned ok = 0, inside = 0, outside = 0, yflip = 0, xflip = 0, transp = 0, rot = 0, shown = 0; + unsigned tile_bad[2][2] = { { 0 } }, tri_bad[2] = { 0 }, row_bad[64] = { 0 }; + double fs[2][2][10] = { { { 0 } } }, fn[2] = { 0 }; /* [triangle][axis][sum] */ + char list[16][40]; + for (int py = 0; py < 64; py++) + for (int px = 0; px < 64; px++) { + const uint8_t *g = got + 4 * (64 * py + px); + const int tx = g[0] / 4, ty = g[1] / 4; + const int valid = !(g[0] & 3) && !(g[1] & 3) && g[2] == 255 - 2 * (tx + ty) && g[3] == 255; + if (valid && tx == px && ty == py) { + ok++; + } else if (valid) { + inside++; + yflip += tx == px && ty == 63 - py; + xflip += tx == 63 - px && ty == py; + transp += tx == py && ty == px; + rot += tx == 63 - px && ty == 63 - py; + } else { + outside++; + } + if (valid) { + const int tri = (px + 0.5) + (py + 0.5) > 64; /* the rectangle's second triangle */ + for (int axis = 0; axis < 2; axis++) { + double *f = fs[tri][axis]; + const double v = axis ? ty : tx; + f[0] += px, f[1] += py, f[2] += px * px, f[3] += py * py, f[4] += px * py; + f[5] += v, f[6] += v * px, f[7] += v * py; + } + fn[tri]++; + } + if (!(valid && tx == px && ty == py)) { + tile_bad[py / 32][px / 32]++; + tri_bad[(px + 0.5) + (py + 0.5) > 64]++; + row_bad[py]++; + } + if (!(valid && tx == px && ty == py) && shown < 16) { + if (valid) + snprintf(list[shown++], sizeof(list[0]), "(%d,%d)->(%d,%d)", px, py, tx, ty); + else + snprintf(list[shown++], sizeof(list[0]), "(%d,%d)->raw %02x%02x%02x%02x", px, py, g[0], g[1], g[2], + g[3]); + } + } + printf("TEXMAP %u level(s): correct %u, wrong-but-texel (inside) %u, not-a-texel (outside/stale) %u\n", levels, ok, + inside, outside); + printf("TEXMAP wrong per 32x32 tile: [%u %u / %u %u], per triangle: first %u second %u\n", tile_bad[0][0], + tile_bad[0][1], tile_bad[1][0], tile_bad[1][1], tri_bad[0], tri_bad[1]); + printf("TEXMAP wrong per row:"); + for (int r = 0; r < 64; r++) + printf(" %u", row_bad[r]); + printf("\n"); + printf("TEXMAP wrong texels fitting: y-flip %u, x-flip %u, transpose/swapped-Morton %u, 180 %u\n", yflip, xflip, + transp, rot); + for (int tri = 0; tri < 2; tri++) + if (fn[tri] > 3) { + char w[32]; + snprintf(w, sizeof(w), "triangle %d tx", tri); + fit_print(w, fs[tri][0], fn[tri]); + snprintf(w, sizeof(w), "triangle %d ty", tri); + fit_print(w, fs[tri][1], fn[tri]); + } + if (shown) { + printf("TEXMAP first mismatches:"); + for (unsigned i = 0; i < shown; i++) + printf(" %s", list[i]); + printf("\n"); + } + if (!inside && !outside) { + printf("RESULT %s PASS 4096/4096 pixels took their own texel (%u level(s))\n", rung, levels); + finish(0); + } + printf("RESULT %s FAIL %u/4096 wrong: %u wrong texel, %u not a texel (%u level(s))\n", rung, inside + outside, + inside, outside, levels); + finish(1); +} + +/* varyings: a 64x64 rectangle of two triangles from four vertices of four colours (indexed), interpolated per pixel, + * against the CPU's barycentric interpolation (3 steps per channel of tolerance) */ +static void r_varyings(void) +{ + struct img t = newtarget(64, 64, 0); + VkRenderPass rp = newrp(VK_FORMAT_UNDEFINED, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL); + VkFramebuffer fb = newfb(rp, &t, NULL); + const struct pipe_cfg pc = { SHADER(l_vcol_vert), SHADER(l_vcol_frag), VIN_POS2_COL4, 0, 0, newlayout(0), rp, + 64, 64 }; + VkPipeline p = newpipe(&pc); + static const float v[4][6] = { + { -1, -1, 1.0f, 0.0f, 0.0f, 1.0f }, /* top left: red */ + { 1, -1, 0.0f, 1.0f, 0.0f, 1.0f }, /* top right: green */ + { -1, 1, 0.0f, 0.0f, 1.0f, 1.0f }, /* bottom left: blue */ + { 1, 1, 1.0f, 1.0f, 1.0f, 1.0f }, /* bottom right: white */ + }; + const uint16_t idx[6] = { 0, 1, 2, 1, 3, 2 }; + struct buf vb = newbuf(sizeof(v), VK_BUFFER_USAGE_VERTEX_BUFFER_BIT); + struct buf ib = newbuf(sizeof(idx), VK_BUFFER_USAGE_INDEX_BUFFER_BIT); + struct buf rb = newbuf(64 * 64 * 4, VK_BUFFER_USAGE_TRANSFER_DST_BIT); + memcpy(vb.p, v, sizeof(v)); + memcpy(ib.p, idx, sizeof(idx)); + VkCommandBuffer cmd = begin(0); + begin_pass(cmd, rp, fb, 64, 64, CLEAR, 0); + vkCmdBindPipeline(cmd, VK_PIPELINE_BIND_POINT_GRAPHICS, p); + const VkDeviceSize off = 0; + vkCmdBindVertexBuffers(cmd, 0, 1, &vb.b, &off); + vkCmdBindIndexBuffer(cmd, ib.b, 0, VK_INDEX_TYPE_UINT16); + vkCmdDrawIndexed(cmd, 6, 1, 0, 0, 0); + vkCmdEndRenderPass(cmd); + readback(cmd, &t, &rb); + end_submit(cmd); + struct expect e = newexpect(64, 64, CLEAR); + for (int py = 0; py < 64; py++) + for (int px = 0; px < 64; px++) { + /* (u, v) in [0,1]: triangle 0 (v0 v1 v2) where u + v < 1, else triangle 1 (v1 v3 v2) */ + const float u = (px + 0.5f) / 64, w = (py + 0.5f) / 64; + float c[4]; + for (int k = 0; k < 4; k++) + c[k] = u + w < 1 ? v[0][2 + k] * (1 - u - w) + v[1][2 + k] * u + v[2][2 + k] * w + : v[1][2 + k] * (1 - w) + v[3][2 + k] * (u + w - 1) + v[2][2 + k] * (1 - u); + bgra(e.px + 4 * (64 * py + px), c); + } + check(&e, rb.p, 3, "four vertex colours interpolated over two triangles"); +} + +/* ---- round 2: more texture coverage, LOAD ---- */ + +/* a w x h texture of `levels` levels (viewed as view_levels), format f; val(x, y, byte, level) gives its bytes in memory + * order. Filled through one staging buffer and vkCmdCopyBufferToImage per level, as applications upload. */ +static struct img upload_tex(uint32_t w, uint32_t h, VkFormat f, uint32_t levels, uint32_t view_levels, + uint8_t (*val)(int, int, int, int)) +{ + struct img t = newimg_ex(w, h, f, VK_IMAGE_USAGE_SAMPLED_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT, levels, + view_levels, 0); + VkDeviceSize total = 0; + for (uint32_t l = 0; l < levels; l++) + total += (VkDeviceSize)MAX2(w >> l, 1u) * MAX2(h >> l, 1u) * 4; + struct buf st = newbuf(total, VK_BUFFER_USAGE_TRANSFER_SRC_BIT); + VkBufferImageCopy reg[16]; + VkDeviceSize off = 0; + for (uint32_t l = 0; l < levels; l++) { + const uint32_t lw = MAX2(w >> l, 1u), lh = MAX2(h >> l, 1u); + uint8_t *p = (uint8_t *)st.p + off; + for (uint32_t y = 0; y < lh; y++) + for (uint32_t x = 0; x < lw; x++) + for (int c = 0; c < 4; c++) + p[4 * (lw * y + x) + c] = val(x, y, c, l); + reg[l] = (VkBufferImageCopy){ .bufferOffset = off, .imageSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, l, 0, 1 }, + .imageExtent = { lw, lh, 1 } }; + off += (VkDeviceSize)lw * lh * 4; + } + VkCommandBuffer cmd = begin(0); + image_barrier(cmd, t.i, VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, + VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT, 0, VK_PIPELINE_STAGE_TRANSFER_BIT, VK_ACCESS_TRANSFER_WRITE_BIT); + vkCmdCopyBufferToImage(cmd, st.b, t.i, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, levels, reg); + image_barrier(cmd, t.i, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL, + VK_PIPELINE_STAGE_TRANSFER_BIT, VK_ACCESS_TRANSFER_WRITE_BIT, VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT, + VK_ACCESS_SHADER_READ_BIT); + end_submit(cmd); + return t; +} + +static VkSampler newsampler_ex(VkFilter f, VkSamplerMipmapMode mip, VkSamplerAddressMode am, float max_lod) +{ + const VkSamplerCreateInfo si = { .sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO, .magFilter = f, .minFilter = f, + .mipmapMode = mip, .addressModeU = am, .addressModeV = am, .addressModeW = am, + .maxLod = max_lod }; + VkSampler s; + CK(vkCreateSampler(dev, &si, NULL, &s)); + return s; +} + +/* the texture over the whole w x h target with uv 0..(su, sv); the readback */ +static uint8_t *sample_pass_ex(const struct img *tex, VkSampler smp, uint32_t w, uint32_t h, float su, float sv) +{ + VkDescriptorSetLayout dsl = newdsl(VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, VK_SHADER_STAGE_FRAGMENT_BIT); + struct rect s = rect_setup(w, h, SHADER(l_tex_frag), 0, 0, dsl, 0, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL); + const VkDescriptorImageInfo ii = { smp, tex->v, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL }; + VkDescriptorSet set = newset(dsl, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, NULL, &ii); + const float black[4] = { 0, 0, 0, 0 }; + VkCommandBuffer cmd = begin(0); + rect_begin(cmd, &s, black); + vkCmdBindDescriptorSets(cmd, VK_PIPELINE_BIND_POINT_GRAPHICS, s.l, 0, 1, &set, 0, NULL); + rect_draw_uv(cmd, s.l, FULL, black, 0.5f, su, sv); + vkCmdEndRenderPass(cmd); + readback(cmd, &s.t, &s.rb); + end_submit(cmd); + return s.rb.p; +} + +/* texmap's encoding in memory order B,G,R,A (a BGRA8 texture) */ +static uint8_t map4(int x, int y, int c, int l) +{ + (void)l; + return map_val(x, y, c); +} + +/* the same texels in memory order R,G,B,A (an RGBA8 texture): r = 4x, g = 4y, b = check, a */ +static uint8_t map4_rgba(int x, int y, int c, int l) +{ + (void)l; + return c == 0 ? 4 * x : c == 1 ? 4 * y : c == 2 ? 255 - 2 * (x + y) : 255; +} + +/* a non-power-of-two texture's texels (x < 128, y < 64): B = 2x, G = 4y, R = 255 - (x + y), A */ +static uint8_t map_npot(int x, int y, int c, int l) +{ + (void)l; + return c == 0 ? 2 * x : c == 1 ? 4 * y : c == 2 ? 255 - (x + y) : 255; +} + +/* level l of texmip's texture: one colour per level */ +static void mip_colour(int l, uint8_t out[4]) +{ + out[0] = (uint8_t)(36 * l); + out[1] = (uint8_t)(255 - 36 * l); + out[2] = (uint8_t)(l & 1 ? 0x30 : 0xc0); + out[3] = 255; +} + +static uint8_t map_mip(int x, int y, int c, int l) +{ + (void)x, (void)y; + uint8_t col[4]; + mip_colour(l, col); + return col[c]; +} + +static void expect_bytes(struct expect *e, int px, int py, const uint8_t b[4]) +{ + memcpy(e->px + 4 * ((size_t)py * e->w + px), b, 4); +} + +static void r_texrgba(void) +{ + struct img tex = upload_tex(64, 64, VK_FORMAT_R8G8B8A8_UNORM, 2, 1, map4_rgba); + const uint8_t *got = sample_pass_ex(&tex, newsampler_ex(VK_FILTER_NEAREST, VK_SAMPLER_MIPMAP_MODE_NEAREST, + VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE, 0), 64, 64, 1, 1); + const float z[4] = { 0 }; + struct expect e = newexpect(64, 64, z); + for (int y = 0; y < 64; y++) + for (int x = 0; x < 64; x++) { + /* the BGRA8 target stores (b, g, r, a) of the sampled (r, g, b, a) */ + const uint8_t b[4] = { map4_rgba(x, y, 2, 0), map4_rgba(x, y, 1, 0), map4_rgba(x, y, 0, 0), 255 }; + expect_bytes(&e, x, y, b); + } + check(&e, got, 1, "an RGBA8 texture (R = 4x, G = 4y) into a BGRA8 target, NEAREST 1:1"); +} + +static void r_texnpot(uint32_t levels) +{ + struct img tex = upload_tex(100, 60, VK_FORMAT_B8G8R8A8_UNORM, levels, 1, map_npot); + const uint8_t *got = sample_pass_ex(&tex, newsampler_ex(VK_FILTER_NEAREST, VK_SAMPLER_MIPMAP_MODE_NEAREST, + VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE, 0), 100, 60, 1, 1); + const float z[4] = { 0 }; + struct expect e = newexpect(100, 60, z); + for (int y = 0; y < 60; y++) + for (int x = 0; x < 100; x++) { + const uint8_t b[4] = { map_npot(x, y, 0, 0), map_npot(x, y, 1, 0), map_npot(x, y, 2, 0), 255 }; + expect_bytes(&e, x, y, b); + } + check(&e, got, 1, levels > 1 ? "100x60 texture (twiddled), NEAREST 1:1" : "100x60 texture (one level, linear)"); +} + +/* REPEAT / MIRRORED_REPEAT: uv 0..1.5 over 64 pixels; pixel p samples texel floor((p + 0.5) * 1.5) wrapped */ +static int wrap_texel(int p, int mirror) +{ + const int c = (int)floorf((p + 0.5f) * 1.5f); + if (!mirror) + return c % 64; + const int m = c % 128; + return m < 64 ? m : 127 - m; +} + +static void r_texwrap(int mirror) +{ + struct img tex = upload_tex(64, 64, VK_FORMAT_B8G8R8A8_UNORM, 2, 1, map4); + const uint8_t *got = sample_pass_ex( + &tex, newsampler_ex(VK_FILTER_NEAREST, VK_SAMPLER_MIPMAP_MODE_NEAREST, + mirror ? VK_SAMPLER_ADDRESS_MODE_MIRRORED_REPEAT : VK_SAMPLER_ADDRESS_MODE_REPEAT, 0), + 64, 64, 1.5f, 1.5f); + const float z[4] = { 0 }; + struct expect e = newexpect(64, 64, z); + for (int y = 0; y < 64; y++) + for (int x = 0; x < 64; x++) { + const int tx = wrap_texel(x, mirror), ty = wrap_texel(y, mirror); + const uint8_t b[4] = { map_val(tx, ty, 0), map_val(tx, ty, 1), map_val(tx, ty, 2), 255 }; + expect_bytes(&e, x, y, b); + } + check(&e, got, 1, mirror ? "MIRRORED_REPEAT, uv 0..1.5" : "REPEAT, uv 0..1.5"); +} + +/* texmip: a 64x64 texture of 7 levels, one colour each, NEAREST/NEAREST-mip into 64, 32, 16, 8 and 4 pixel targets: + * LOD 0..4. (G5P lays mips out as the A9 does, twiddle grids padded to at least 32x32; Rogue's TPU computes level + * offsets from the unpadded sizes, which agree through level 2 and diverge from level 3.) */ +static void r_texmip(void) +{ + struct img tex = upload_tex(64, 64, VK_FORMAT_B8G8R8A8_UNORM, 7, 7, map_mip); + VkSampler smp = newsampler_ex(VK_FILTER_NEAREST, VK_SAMPLER_MIPMAP_MODE_NEAREST, + VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE, 16.0f); + static const uint32_t size[5] = { 64, 32, 16, 8, 4 }; + unsigned bad[5] = { 0 }, got_lvl[5]; + uint8_t first_px[5][4] = { { 0 } }; + for (int i = 0; i < 5; i++) { + const uint8_t *got = sample_pass_ex(&tex, smp, size[i], size[i], 1, 1); + uint8_t want[4]; + mip_colour(i, want); + got_lvl[i] = 99; + for (uint32_t k = 0; k < size[i] * size[i]; k++) { + int off = 0; + for (int c = 0; c < 4; c++) + off |= abs(got[4 * k + c] - want[c]) > 1; + if (off && !bad[i]++) { + memcpy(first_px[i], got + 4 * k, 4); + for (int l = 0; l < 7; l++) { + uint8_t lc[4]; + mip_colour(l, lc); + if (!memcmp(lc, got + 4 * k, 4)) + got_lvl[i] = l; + } + } + } + } + if (nocheck) { + printf("RESULT %s PASS (not checked)\n", rung); + finish(0); + } + char d[512]; + int n = 0; + for (int i = 0; i < 5; i++) + n += snprintf(d + n, sizeof(d) - n, " LOD%d %u/%u%s", i, bad[i], size[i] * size[i], bad[i] ? "" : " ok"); + if (!bad[0] && !bad[1] && !bad[2] && !bad[3] && !bad[4]) { + printf("RESULT %s PASS LOD 0..4 each took its level (64..4 pixel targets, every pixel)\n", rung); + finish(0); + } + printf("RESULT %s FAIL wrong pixels:%s; first wrong per LOD:", rung, d); + for (int i = 0; i < 5; i++) + if (bad[i]) + printf(" LOD%d %02x%02x%02x%02x (level %u)", i, first_px[i][0], first_px[i][1], first_px[i][2], + first_px[i][3], got_lvl[i]); + printf(" (level 99 = no level's colour)\n"); + finish(1); +} + +/* texhostlinear: a VK_IMAGE_TILING_LINEAR image in host-visible memory, written by the CPU at its row pitch, sampled */ +static void r_texhostlinear(void) +{ + struct img tex = newimg_ex(64, 64, VK_FORMAT_B8G8R8A8_UNORM, VK_IMAGE_USAGE_SAMPLED_BIT, 1, 1, 1); + const VkImageSubresource sub = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0 }; + VkSubresourceLayout lay; + vkGetImageSubresourceLayout(dev, tex.i, &sub, &lay); + uint8_t *map; + CK(vkMapMemory(dev, tex.m, 0, VK_WHOLE_SIZE, 0, (void **)&map)); + for (int y = 0; y < 64; y++) + for (int x = 0; x < 64; x++) + for (int c = 0; c < 4; c++) + map[lay.offset + y * lay.rowPitch + 4 * x + c] = map_val(x, y, c); + VkCommandBuffer cmd = begin(0); + image_barrier(cmd, tex.i, VK_IMAGE_LAYOUT_PREINITIALIZED, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL, + VK_PIPELINE_STAGE_HOST_BIT, VK_ACCESS_HOST_WRITE_BIT, VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT, + VK_ACCESS_SHADER_READ_BIT); + end_submit(cmd); + const uint8_t *got = sample_pass_ex(&tex, newsampler_ex(VK_FILTER_NEAREST, VK_SAMPLER_MIPMAP_MODE_NEAREST, + VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE, 0), 64, 64, 1, 1); + const float z[4] = { 0 }; + struct expect e = newexpect(64, 64, z); + for (int y = 0; y < 64; y++) + for (int x = 0; x < 64; x++) { + const uint8_t b[4] = { map_val(x, y, 0), map_val(x, y, 1), map_val(x, y, 2), 255 }; + expect_bytes(&e, x, y, b); + } + char d[96]; + snprintf(d, sizeof(d), "linear-tiling host image, row pitch %u, offset %u", (unsigned)lay.rowPitch, + (unsigned)lay.offset); + check(&e, got, 1, d); +} + +/* tex2: two textures in one fragment program (left half t0, right half t1) */ +static uint8_t solid_a(int x, int y, int c, int l) +{ + (void)x, (void)y, (void)l; + static const uint8_t v[4] = { 0x40, 0xc0, 0x20, 0xff }; + return v[c]; +} + +static uint8_t solid_b(int x, int y, int c, int l) +{ + (void)x, (void)y, (void)l; + static const uint8_t v[4] = { 0xe0, 0x10, 0x90, 0xff }; + return v[c]; +} + +static void r_tex2(void) +{ + /* the A8 sample names one image slot (common dword 0) in every Apple capture: a second texture is refused */ + skip_refusal = 1; + struct img a = upload_tex(64, 64, VK_FORMAT_B8G8R8A8_UNORM, 2, 1, solid_a); + struct img b = upload_tex(64, 64, VK_FORMAT_B8G8R8A8_UNORM, 2, 1, solid_b); + const VkDescriptorSetLayoutBinding bd[2] = { + { 0, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 1, VK_SHADER_STAGE_FRAGMENT_BIT }, + { 1, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 1, VK_SHADER_STAGE_FRAGMENT_BIT } }; + const VkDescriptorSetLayoutCreateInfo li = { .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO, + .bindingCount = 2, .pBindings = bd }; + VkDescriptorSetLayout dsl; + CK(vkCreateDescriptorSetLayout(dev, &li, NULL, &dsl)); + struct rect s = rect_setup(64, 64, SHADER(l_tex2_frag), 0, 0, dsl, 0, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL); + const VkDescriptorPoolSize ps = { VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 2 }; + const VkDescriptorPoolCreateInfo pi = { .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO, .maxSets = 1, + .poolSizeCount = 1, .pPoolSizes = &ps }; + VkDescriptorPool dp; + CK(vkCreateDescriptorPool(dev, &pi, NULL, &dp)); + const VkDescriptorSetAllocateInfo ai = { .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO, + .descriptorPool = dp, .descriptorSetCount = 1, .pSetLayouts = &dsl }; + VkDescriptorSet set; + CK(vkAllocateDescriptorSets(dev, &ai, &set)); + VkSampler smp = newsampler_ex(VK_FILTER_NEAREST, VK_SAMPLER_MIPMAP_MODE_NEAREST, + VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE, 0); + const VkDescriptorImageInfo ii[2] = { { smp, a.v, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL }, + { smp, b.v, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL } }; + const VkWriteDescriptorSet w[2] = { + { .sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET, .dstSet = set, .dstBinding = 0, .descriptorCount = 1, + .descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, .pImageInfo = &ii[0] }, + { .sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET, .dstSet = set, .dstBinding = 1, .descriptorCount = 1, + .descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, .pImageInfo = &ii[1] } }; + vkUpdateDescriptorSets(dev, 2, w, 0, NULL); + const float black[4] = { 0 }; + VkCommandBuffer cmd = begin(0); + rect_begin(cmd, &s, black); + vkCmdBindDescriptorSets(cmd, VK_PIPELINE_BIND_POINT_GRAPHICS, s.l, 0, 1, &set, 0, NULL); + rect_draw(cmd, s.l, FULL, black, 0.5f); + vkCmdEndRenderPass(cmd); + readback(cmd, &s.t, &s.rb); + end_submit(cmd); + skip_refusal = 0; + struct expect e = newexpect(64, 64, black); + for (int y = 0; y < 64; y++) + for (int x = 0; x < 64; x++) { + const uint8_t v[4] = { x < 32 ? 0x40 : 0xe0, x < 32 ? 0xc0 : 0x10, x < 32 ? 0x20 : 0x90, 0xff }; + expect_bytes(&e, x, y, v); + } + check(&e, s.rb.p, 1, "two textures in one fragment program"); +} + +/* LOAD: pass 1 clears and draws the left half; pass 2 (loadOp LOAD) draws the top half; pass 3 (LOAD) a centre box. + * One command buffer, one render pass per pass; the result is every pass composed in order. */ +static void r_load(uint32_t w, uint32_t h, int passes) +{ + struct img t = newtarget(w, h, 0); + VkRenderPass rp1 = newrp_ex(0, VK_ATTACHMENT_LOAD_OP_CLEAR, VK_IMAGE_LAYOUT_UNDEFINED, + passes > 1 ? VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL : VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL); + VkRenderPass rpm = newrp_ex(0, VK_ATTACHMENT_LOAD_OP_LOAD, VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL, + VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL); + VkRenderPass rpl = newrp_ex(0, VK_ATTACHMENT_LOAD_OP_LOAD, VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL, + VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL); + VkFramebuffer fb = newfb(rp1, &t, NULL); + VkPipelineLayout l = newlayout(0); + const struct pipe_cfg pc = { SHADER(l_rect_vert), SHADER(l_pc_frag), VIN_CORNER2, 0, 0, l, rp1, w, h }; + VkPipeline p = newpipe(&pc); + struct buf vb = newbuf(sizeof(CORNERS), VK_BUFFER_USAGE_VERTEX_BUFFER_BIT); + memcpy(vb.p, CORNERS, sizeof(CORNERS)); + struct buf rb = newbuf((VkDeviceSize)w * h * 4, VK_BUFFER_USAGE_TRANSFER_DST_BIT); + const float c0[4] = { 0.0f, 0.0f, 0.25f, 1.0f }, c1[4] = { 1.0f, 0.25f, 0.0f, 1.0f }, + c2[4] = { 0.0f, 0.75f, 0.25f, 1.0f }, c3[4] = { 0.75f, 0.75f, 1.0f, 1.0f }; + const float ra[4] = { -1, -1, 0, 1 }, rbx[4] = { -1, -1, 1, 0 }, rc[4] = { -0.4f, -0.4f, 0.4f, 0.4f }; + const float *rects[3] = { ra, rbx, rc }, *cols[3] = { c1, c2, c3 }; + VkCommandBuffer cmd = begin(0); + for (int i = 0; i < passes; i++) { + VkRenderPass rp = !i ? rp1 : i == passes - 1 ? rpl : rpm; + begin_pass(cmd, rp, fb, w, h, c0, 0); + vkCmdBindPipeline(cmd, VK_PIPELINE_BIND_POINT_GRAPHICS, p); + const VkDeviceSize off = 0; + vkCmdBindVertexBuffers(cmd, 0, 1, &vb.b, &off); + rect_draw(cmd, l, rects[i], cols[i], 0.5f); + vkCmdEndRenderPass(cmd); + if (i < passes - 1) + barrier(cmd, VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT, VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT, + VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT | VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT, + VK_ACCESS_COLOR_ATTACHMENT_READ_BIT | VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT | + VK_ACCESS_INPUT_ATTACHMENT_READ_BIT); + } + readback(cmd, &t, &rb); + end_submit(cmd); + struct expect e = newexpect(w, h, c0); + for (int i = 0; i < passes; i++) + expect_rect(&e, rects[i], cols[i]); + char d[96]; + snprintf(d, sizeof(d), "%d passes (clear, then LOAD), job %.1f ms", passes, last_ms); + check(&e, rb.p, 1, d); +} + +/* mesh / meshvary: one draw of N triangles at 256x256, N = 1k, 2k, 4k, 8k, 16k, 32k, 64k, each its own submission. + * The triangles tile the target exactly (32 columns x N/64 rows of cells, two triangles each), so every pixel ends + * the triangle colour. meshvary adds a vec4 varying per vertex (more parameter memory per vertex, as vkmark's lit + * meshes have). Prints one MESH line per step; the step that times out is the size curve's knee. */ +static void r_mesh(int vary) +{ + const uint32_t w = 256, h = 256; + struct img t = newtarget(w, h, 0); + VkRenderPass rp = newrp(VK_FORMAT_UNDEFINED, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL); + VkFramebuffer fb = newfb(rp, &t, NULL); + const struct pipe_cfg pc = { vary ? SHADER(l_vcol_vert) : SHADER(l_tri_vert), + vary ? SHADER(l_vcol_frag) : SHADER(l_const_frag), + vary ? VIN_POS2_COL4 : VIN_POS4, 0, 0, newlayout(0), rp, w, h }; + VkPipeline p = newpipe(&pc); + const unsigned maxn = 65536, stride = vary ? 24 : 16; + struct buf vb = newbuf((VkDeviceSize)maxn * 3 * stride, VK_BUFFER_USAGE_VERTEX_BUFFER_BIT); + struct buf rb = newbuf(w * h * 4, VK_BUFFER_USAGE_TRANSFER_DST_BIT); + struct expect e = newexpect(w, h, TRI); + char curve[256] = ""; + int cl = 0; + for (unsigned n = 1024; n <= maxn; n *= 2) { + const unsigned cols = 32, rows = n / 64; + float *v = vb.p; + for (unsigned r = 0; r < rows; r++) + for (unsigned col = 0; col < cols; col++) { + const float x0 = -1 + 2.0f * col / cols, x1 = -1 + 2.0f * (col + 1) / cols; + const float y0 = -1 + 2.0f * r / rows, y1 = -1 + 2.0f * (r + 1) / rows; + const float q[6][2] = { { x0, y0 }, { x1, y0 }, { x0, y1 }, { x1, y0 }, { x1, y1 }, { x0, y1 } }; + for (int k = 0; k < 6; k++) { + *v++ = q[k][0]; + *v++ = q[k][1]; + if (vary) { + memcpy(v, TRI, 16); + v += 4; + } else { + *v++ = 0; + *v++ = 1; + } + } + } + step("mesh %u triangles", n); + memset(rb.p, 0, w * h * 4); + VkCommandBuffer cmd = begin(0); + begin_pass(cmd, rp, fb, w, h, CLEAR, 0); + vkCmdBindPipeline(cmd, VK_PIPELINE_BIND_POINT_GRAPHICS, p); + const VkDeviceSize off = 0; + vkCmdBindVertexBuffers(cmd, 0, 1, &vb.b, &off); + vkCmdDraw(cmd, 3 * n, 1, 0, 0); + vkCmdEndRenderPass(cmd); + readback(cmd, &t, &rb); + end_submit(cmd); + size_t bad = 0; + for (size_t i = 0; !nocheck && i < w * h; i++) + for (int c = 0; c < 4; c++) + if (abs(((uint8_t *)rb.p)[4 * i + c] - e.px[4 * i + c]) > 1) { + bad++; + break; + } + printf("MESH %s %u triangles: %s %.1f ms\n", vary ? "vec4-varying" : "plain", n, + bad ? "WRONG" : nocheck ? "done" : "ok", last_ms); + cl += snprintf(curve + cl, sizeof(curve) - cl, " %uk:%.0fms%s", n / 1024, last_ms, bad ? "(wrong)" : ""); + if (bad) { + printf("RESULT %s FAIL at %u triangles: %zu/%u pixels wrong; curve%s\n", rung, n, bad, w * h, curve); + finish(1); + } + } + printf("RESULT %s PASS 1k..64k triangles at 256x256, every pixel; curve%s\n", rung, curve); + finish(0); +} + +/* texmath: two samples whose 32-bit results feed arithmetic (the sample form vkmark's texture, effect2d and desktop + * scenes need): each pixel the average of its texel and the right neighbour's, texmap's 64x64 texture, NEAREST */ +static void r_texmath(void) +{ + struct img tex = upload_tex(64, 64, VK_FORMAT_B8G8R8A8_UNORM, 2, 1, map4); + VkDescriptorSetLayout dsl = newdsl(VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, VK_SHADER_STAGE_FRAGMENT_BIT); + struct rect s = rect_setup(64, 64, SHADER(l_texmath_frag), 0, 0, dsl, 0, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL); + const VkDescriptorImageInfo ii = { newsampler_ex(VK_FILTER_NEAREST, VK_SAMPLER_MIPMAP_MODE_NEAREST, + VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE, 0), + tex.v, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL }; + VkDescriptorSet set = newset(dsl, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, NULL, &ii); + const float black[4] = { 0 }; + VkCommandBuffer cmd = begin(0); + rect_begin(cmd, &s, black); + vkCmdBindDescriptorSets(cmd, VK_PIPELINE_BIND_POINT_GRAPHICS, s.l, 0, 1, &set, 0, NULL); + rect_draw(cmd, s.l, FULL, black, 0.5f); + vkCmdEndRenderPass(cmd); + readback(cmd, &s.t, &s.rb); + end_submit(cmd); + struct expect e = newexpect(64, 64, black); + for (int y = 0; y < 64; y++) + for (int x = 0; x < 64; x++) { + const int x1 = x < 63 ? x + 1 : 63; + uint8_t b[4]; + for (int c = 0; c < 4; c++) + b[c] = (uint8_t)((map_val(x, y, c) + map_val(x1, y, c) + 1) / 2); + expect_bytes(&e, x, y, b); + } + check(&e, s.rb.p, 2, "0.5 * texel + 0.5 * right neighbour (32-bit sample results into arithmetic)"); +} + +/* litmesh: a lat/long sphere with normals and per-vertex diffuse lighting, depth-tested (D32), at 256x256, drawn + * twice: both frames must be identical, inside the silhouette lit (never the clear colour), outside the clear + * colour (a 2-pixel band at the outline is not judged). vkmark vertex/shading without texturing; the pool-grow + * regression test (litmesh about 3k triangles, below the plain mesh's 8k knee; litmeshbig about 24k). */ +static void r_litmesh(unsigned su, unsigned sv) +{ + const uint32_t w = 256, h = 256; + const unsigned ntri = 2 * su * sv; + struct img t = newtarget(w, h, 0); + struct img d = newimg(w, h, VK_FORMAT_D32_SFLOAT, VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT, 1); + VkRenderPass rp = newrp(VK_FORMAT_D32_SFLOAT, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL); + VkFramebuffer fb = newfb(rp, &t, &d); + const struct pipe_cfg pc = { SHADER(l_lit_vert), SHADER(l_vcol_frag), VIN_POS3_NRM3, 0, 1, newlayout(0), rp, w, h }; + VkPipeline p = newpipe(&pc); + struct buf vb = newbuf((VkDeviceSize)ntri * 3 * 24, VK_BUFFER_USAGE_VERTEX_BUFFER_BIT); + struct buf rb[2] = { newbuf(w * h * 4, VK_BUFFER_USAGE_TRANSFER_DST_BIT), newbuf(w * h * 4, VK_BUFFER_USAGE_TRANSFER_DST_BIT) }; + float *v = vb.p; + for (unsigned i = 0; i < su; i++) + for (unsigned j = 0; j < sv; j++) { + float q[4][3]; + for (int k = 0; k < 4; k++) { + const unsigned ii = i + (k & 1), jj = j + (k >> 1); + const float lon = 2.0f * (float)M_PI * ii / su, lat = (float)M_PI * jj / sv - (float)M_PI / 2; + q[k][0] = cosf(lat) * sinf(lon); + q[k][1] = sinf(lat); + q[k][2] = cosf(lat) * cosf(lon); + } + static const int o[6] = { 0, 1, 2, 1, 3, 2 }; + for (int k = 0; k < 6; k++) { + memcpy(v, q[o[k]], 12); /* position */ + memcpy(v + 3, q[o[k]], 12); /* normal (a unit sphere) */ + v += 6; + } + } + for (int f = 0; f < 2; f++) { + step("litmesh frame %d (%u triangles)", f + 1, ntri); + VkCommandBuffer cmd = begin(0); + begin_pass(cmd, rp, fb, w, h, CLEAR, 1); + vkCmdBindPipeline(cmd, VK_PIPELINE_BIND_POINT_GRAPHICS, p); + const VkDeviceSize off = 0; + vkCmdBindVertexBuffers(cmd, 0, 1, &vb.b, &off); + vkCmdDraw(cmd, 3 * ntri, 1, 0, 0); + vkCmdEndRenderPass(cmd); + readback(cmd, &t, &rb[f]); + end_submit(cmd); + } + if (nocheck) { + printf("RESULT %s PASS (not checked) %u triangles, %.1f ms\n", rung, ntri, last_ms); + finish(0); + } + const uint8_t *a = rb[0].p, *b = rb[1].p; + uint8_t clear[4]; + bgra(clear, CLEAR); + size_t diff = 0, in_bad = 0, out_bad = 0, in_n = 0, out_n = 0; + const float R = 0.8f * 128.0f; + for (uint32_t y = 0; y < h; y++) + for (uint32_t x = 0; x < w; x++) { + const size_t k = 4 * ((size_t)y * w + x); + diff += memcmp(a + k, b + k, 4) != 0; + const float r = hypotf(x + 0.5f - 128.0f, y + 0.5f - 128.0f); + const int is_clear = !memcmp(a + k, clear, 4); + if (r < R - 2) { + in_n++; + in_bad += is_clear || a[k + 3] != 255 || a[k + 2] < 36 || a[k + 2] > 245; + } else if (r > R + 2) { + out_n++; + out_bad += !is_clear; + } + } + if (!diff && !in_bad && !out_bad) { + printf("RESULT %s PASS %u triangles, lit %zu/%zu inside, clear %zu/%zu outside, frames identical, %.1f ms\n", + rung, ntri, in_n, in_n, out_n, out_n, last_ms); + finish(0); + } + printf("RESULT %s FAIL %u triangles: inside wrong %zu/%zu, outside wrong %zu/%zu, frames differ in %zu pixels\n", + rung, ntri, in_bad, in_n, out_bad, out_n, diff); + finish(1); +} + +static void r_rtt(void) +{ + /* pass 1: the draws16 grid into a sampled 64x64 target */ + struct rect s = rect_setup(64, 64, SHADER(l_pc_frag), 0, 0, 0, VK_IMAGE_USAGE_SAMPLED_BIT, + VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL); + VkCommandBuffer cmd = begin(0); + rect_begin(cmd, &s, CLEAR); + grid_draws(cmd, s.l); + vkCmdEndRenderPass(cmd); + end_submit(cmd); + const double first = last_ms; + /* pass 2: sample it, nearest, into another 64x64 target */ + uint8_t *got = sample_pass(&s.t, VK_FILTER_NEAREST, 64, 64); + struct expect e = newexpect(64, 64, CLEAR); + grid_expect(&e); + char d[96]; + snprintf(d, sizeof(d), "grid rendered (%.1f ms) then sampled (%.1f ms)", first, last_ms); + check(&e, got, 1, d); +} + +static void compute_setup(const uint32_t *spv, size_t size, VkDescriptorSet *set, VkPipelineLayout *pl, + VkPipeline *pipe, const struct buf *b, VkDeviceSize range) +{ + VkDescriptorSetLayout dsl = newdsl(VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, VK_SHADER_STAGE_COMPUTE_BIT); + const VkPipelineLayoutCreateInfo li = { .sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO, + .setLayoutCount = 1, .pSetLayouts = &dsl }; + CK(vkCreatePipelineLayout(dev, &li, NULL, pl)); + const VkComputePipelineCreateInfo ci = { + .sType = VK_STRUCTURE_TYPE_COMPUTE_PIPELINE_CREATE_INFO, + .stage = { .sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO, .stage = VK_SHADER_STAGE_COMPUTE_BIT, + .module = shader(spv, size), .pName = "main" }, + .layout = *pl }; + step("vkCreateComputePipelines"); + CK(vkCreateComputePipelines(dev, VK_NULL_HANDLE, 1, &ci, NULL, pipe)); + const VkDescriptorBufferInfo bi = { b->b, 0, range }; + *set = newset(dsl, VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, &bi, NULL); +} + +static void r_compute64k(void) +{ + const uint32_t n = 65536; + struct buf b = newbuf(n * 4 + 4096, VK_BUFFER_USAGE_STORAGE_BUFFER_BIT); + memset(b.p, 0xee, n * 4 + 4096); + VkDescriptorSet set; + VkPipelineLayout pl; + VkPipeline p; + compute_setup(l_big_comp, sizeof(l_big_comp), &set, &pl, &p, &b, n * 4); + VkCommandBuffer cmd = begin(0); + vkCmdBindPipeline(cmd, VK_PIPELINE_BIND_POINT_COMPUTE, p); + vkCmdBindDescriptorSets(cmd, VK_PIPELINE_BIND_POINT_COMPUTE, pl, 0, 1, &set, 0, NULL); + vkCmdDispatch(cmd, n / 32, 1, 1); + barrier(cmd, VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT, VK_ACCESS_SHADER_WRITE_BIT, VK_PIPELINE_STAGE_HOST_BIT, + VK_ACCESS_HOST_READ_BIT); + end_submit(cmd); + if (nocheck) { + printf("RESULT %s PASS (not checked) job %.1f ms\n", rung, last_ms); + finish(0); + } + const uint32_t *o = b.p; + uint32_t bad = 0, first = 0; + for (uint32_t i = 0; i < n; i++) + if (o[i] != i && !bad++) + first = i; + const uint32_t guard = o[n]; + if (!bad && guard == 0xeeeeeeeeu) { + printf("RESULT %s PASS 65536/65536 values (2048 workgroups of 32) job %.1f ms\n", rung, last_ms); + finish(0); + } + printf("RESULT %s FAIL %u/65536 wrong, first out[%u] = %#x, guard %#x, job %.1f ms\n", rung, bad, first, + bad ? o[first] : 0, guard, last_ms); + finish(1); +} + +static void r_c2g(void) +{ + struct tri s = tri_setup(64, 64); + struct buf vb = newbuf(256, VK_BUFFER_USAGE_STORAGE_BUFFER_BIT | VK_BUFFER_USAGE_VERTEX_BUFFER_BIT); + VkDescriptorSet set; + VkPipelineLayout pl; + VkPipeline p; + compute_setup(l_verts_comp, sizeof(l_verts_comp), &set, &pl, &p, &vb, 48); + VkCommandBuffer cmd = begin(0); + vkCmdBindPipeline(cmd, VK_PIPELINE_BIND_POINT_COMPUTE, p); + vkCmdBindDescriptorSets(cmd, VK_PIPELINE_BIND_POINT_COMPUTE, pl, 0, 1, &set, 0, NULL); + vkCmdDispatch(cmd, 1, 1, 1); + barrier(cmd, VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT, VK_ACCESS_SHADER_WRITE_BIT, VK_PIPELINE_STAGE_VERTEX_INPUT_BIT, + VK_ACCESS_VERTEX_ATTRIBUTE_READ_BIT); + tri_record(cmd, &s, vb.b); + end_submit(cmd); + struct expect e = newexpect(64, 64, CLEAR); + expect_tri(&e, TRI); + check(&e, s.rb.p, 1, "vertices written by a compute dispatch in the same command buffer"); +} + +static void r_timing(void) +{ + uint32_t nq = 1; + VkQueueFamilyProperties qf; + vkGetPhysicalDeviceQueueFamilyProperties(phy, &nq, &qf); + VkPhysicalDeviceProperties props; + vkGetPhysicalDeviceProperties(phy, &props); + /* wall-clock per job: 10 clears, 10 triangles, 10 small dispatches */ + double c_ms[3] = { 0 }, c_max[3] = { 0 }; + { + struct img t = newtarget(64, 64, 0); + VkRenderPass rp = newrp(VK_FORMAT_UNDEFINED, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL); + VkFramebuffer fb = newfb(rp, &t, NULL); + VkCommandBuffer cmd = begin(1); + begin_pass(cmd, rp, fb, 64, 64, CLEAR, 0); + vkCmdEndRenderPass(cmd); + CK(vkEndCommandBuffer(cmd)); + for (int i = 0; i < 10; i++) { + submit(cmd); + c_ms[0] += last_ms / 10; + c_max[0] = fmax(c_max[0], last_ms); + } + } + { + struct tri s = tri_setup(64, 64); + VkCommandBuffer cmd = begin(1); + tri_record(cmd, &s, s.vb.b); + CK(vkEndCommandBuffer(cmd)); + for (int i = 0; i < 10; i++) { + submit(cmd); + c_ms[1] += last_ms / 10; + c_max[1] = fmax(c_max[1], last_ms); + } + } + { + struct buf b = newbuf(4096, VK_BUFFER_USAGE_STORAGE_BUFFER_BIT); + VkDescriptorSet set; + VkPipelineLayout pl; + VkPipeline p; + compute_setup(l_big_comp, sizeof(l_big_comp), &set, &pl, &p, &b, 4096); + VkCommandBuffer cmd = begin(1); + vkCmdBindPipeline(cmd, VK_PIPELINE_BIND_POINT_COMPUTE, p); + vkCmdBindDescriptorSets(cmd, VK_PIPELINE_BIND_POINT_COMPUTE, pl, 0, 1, &set, 0, NULL); + vkCmdDispatch(cmd, 1, 1, 1); + CK(vkEndCommandBuffer(cmd)); + for (int i = 0; i < 10; i++) { + submit(cmd); + c_ms[2] += last_ms / 10; + c_max[2] = fmax(c_max[2], last_ms); + } + } + printf("RESULT %s PASS timestampValidBits %u timestampPeriod %.1f ns (queries not used: wall clock per job) " + "clear64 avg %.2f max %.2f ms, tri64 avg %.2f max %.2f ms, compute32 avg %.2f max %.2f ms\n", + rung, qf.timestampValidBits, props.limits.timestampPeriod, c_ms[0], c_max[0], c_ms[1], c_max[1], c_ms[2], + c_max[2]); + finish(0); +} + +/* ---- round 3: vkCmdClearColorImage on the GPU, vkmark's own shaders against a CPU reference ---- */ + +/* level `level` of t (TRANSFER_SRC_OPTIMAL) into rb */ +static void readback_level(VkCommandBuffer cmd, const struct img *t, uint32_t level, const struct buf *rb) +{ + const VkBufferImageCopy c = { .imageSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, level, 0, 1 }, + .imageExtent = { MAX2(t->w >> level, 1u), MAX2(t->h >> level, 1u), 1 } }; + vkCmdCopyImageToBuffer(cmd, t->i, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, rb->b, 1, &c); + barrier(cmd, VK_PIPELINE_STAGE_TRANSFER_BIT, VK_ACCESS_TRANSFER_WRITE_BIT, VK_PIPELINE_STAGE_HOST_BIT, + VK_ACCESS_HOST_READ_BIT); +} + +/* clearimg: vkCmdClearColorImage (vkmark's clear scene) to colour A, then again to colour B, in one command buffer; + * every pixel checked. mode 0: level 0 of the two-level twiddled target (the target shape the A8 has drawn); + * mode 1: the first clear covers levels 0 and 1 (a render into mip level 1, untested on the A8), level 1 must be A; + * mode 2: a LINEAR-tiling target (Apple's passcap `linear` encoding; optimal one-level images are twiddled on the + * A8, as Metal's). The job times are the + * fence waits (on the A8 a render per clear and level). */ +static void r_clearimg(uint32_t w, uint32_t h, int mode) +{ + struct img t = mode == 2 ? newimg_ex(w, h, VK_FORMAT_B8G8R8A8_UNORM, VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | + VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT, 1, 1, 1) + : newtarget(w, h, VK_IMAGE_USAGE_TRANSFER_DST_BIT); + struct buf rb0 = newbuf((VkDeviceSize)w * h * 4, VK_BUFFER_USAGE_TRANSFER_DST_BIT); + struct buf rb1 = newbuf((VkDeviceSize)(w / 2) * (h / 2) * 4, VK_BUFFER_USAGE_TRANSFER_DST_BIT); + const float ca[4] = { 0.75f, 0.25f, 0.5f, 1.0f }, cb[4] = { 0.25f, 0.5f, 0.75f, 0.5f }; + VkClearColorValue va, vb; + memcpy(va.float32, ca, 16); + memcpy(vb.float32, cb, 16); + const VkImageSubresourceRange both = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 2, 0, 1 }; + const VkImageSubresourceRange l0 = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1 }; + VkCommandBuffer cmd = begin(0); + image_barrier(cmd, t.i, VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, + VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT, 0, VK_PIPELINE_STAGE_TRANSFER_BIT, VK_ACCESS_TRANSFER_WRITE_BIT); + vkCmdClearColorImage(cmd, t.i, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, &va, 1, mode == 1 ? &both : &l0); + barrier(cmd, VK_PIPELINE_STAGE_TRANSFER_BIT, VK_ACCESS_TRANSFER_WRITE_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT, + VK_ACCESS_TRANSFER_WRITE_BIT); + vkCmdClearColorImage(cmd, t.i, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, &vb, 1, &l0); + image_barrier(cmd, t.i, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, + VK_PIPELINE_STAGE_TRANSFER_BIT, VK_ACCESS_TRANSFER_WRITE_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT, + VK_ACCESS_TRANSFER_READ_BIT); + readback_level(cmd, &t, 0, &rb0); + if (mode == 1) + readback_level(cmd, &t, 1, &rb1); + end_submit(cmd); + const double clear_ms = last_ms; + /* the same command buffer shape once more: the second run is the steady-state time (no first-use setup) */ + VkCommandBuffer cmd2 = begin(0); + image_barrier(cmd2, t.i, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, + VK_PIPELINE_STAGE_TRANSFER_BIT, 0, VK_PIPELINE_STAGE_TRANSFER_BIT, VK_ACCESS_TRANSFER_WRITE_BIT); + vkCmdClearColorImage(cmd2, t.i, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, &vb, 1, &l0); + image_barrier(cmd2, t.i, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, + VK_PIPELINE_STAGE_TRANSFER_BIT, VK_ACCESS_TRANSFER_WRITE_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT, + VK_ACCESS_TRANSFER_READ_BIT); + end_submit(cmd2); + char d[128]; + snprintf(d, sizeof(d), "%s; first job %.2f ms (2%s clears + copy), one clear %.2f ms", + mode == 1 ? "level 0 = B, level 1 = A" : mode == 2 ? "one-level target = B" : "level 0 = B", clear_ms, + mode == 1 ? "+1" : "", last_ms); + if (mode == 1 && !nocheck) { + struct expect e1 = newexpect(w / 2, h / 2, ca); + size_t bad = 0; + for (size_t i = 0; i < (size_t)(w / 2) * (h / 2); i++) + for (int c = 0; c < 4; c++) + bad += abs((int)((uint8_t *)rb1.p)[4 * i + c] - (int)e1.px[4 * i + c]) > 1; + if (bad) { + const uint8_t *g = rb1.p; + printf("RESULT %s FAIL level 1 (%ux%u) not colour A: %zu bytes off, first texel %02x%02x%02x%02x want " + "%02x%02x%02x%02x\n", rung, w / 2, h / 2, bad, g[0], g[1], g[2], g[3], e1.px[0], e1.px[1], e1.px[2], + e1.px[3]); + finish(1); + } + } + struct expect e0 = newexpect(w, h, cb); + check(&e0, rb0.p, 1, d); +} + +/* vkmark's SPIR-V, read from the vkmark side bundle (A8_VKMARK_SHADERS, default /../vkmark/data/shaders): + * exactly the shaders vkmark runs, not copies. A missing bundle is this rung's SKIP. */ +static VkShaderModule vkm_shader(const char *name) +{ + char path[512], exe[400]; + const char *dir = getenv("A8_VKMARK_SHADERS"); + if (dir) { + snprintf(path, sizeof(path), "%s/%s.spv", dir, name); + } else { + const ssize_t n = readlink("/proc/self/exe", exe, sizeof(exe) - 1); + exe[n > 0 ? n : 0] = 0; + char *slash = strrchr(exe, '/'); + if (slash) + *slash = 0; + snprintf(path, sizeof(path), "%s/../vkmark/data/shaders/%s.spv", exe, name); + } + FILE *f = fopen(path, "rb"); + if (!f) { + printf("RESULT %s SKIP vkmark's shader %s not found (install the vkmark side bundle next to the ladder, or set " + "A8_VKMARK_SHADERS)\n", rung, path); + finish(0); + } + fseek(f, 0, SEEK_END); + const long size = ftell(f); + fseek(f, 0, SEEK_SET); + uint32_t *code = malloc(size); + if (fread(code, 1, size, f) != (size_t)size) { + printf("RESULT %s FAIL short read of %s\n", rung, path); + finish(1); + } + fclose(f); + step("vkmark shader %s (%ld bytes)", path, size); + return shader(code, size); +} + +/* a 256x256 RGBA8 texture of hashed texels (memory order R,G,B,A): any wrong texel, tap or weight shows */ +static uint8_t vkm_hash(int x, int y, int c) +{ + uint32_t v = (uint32_t)x * 73856093u ^ (uint32_t)y * 19349663u ^ (uint32_t)(c + 1) * 83492791u; + v ^= v >> 13; + v *= 0x5bd1e995u; + v ^= v >> 15; + return (uint8_t)v; +} + +static uint8_t vkm_tex_val(int x, int y, int c, int l) +{ + (void)l; + return vkm_hash(x, y, c); +} + +/* set 0: binding 0 the uniform block, binding 1 the combined image sampler (vkmark's layout for all three scenes) */ +static VkDescriptorSetLayout vkm_dsl(void) +{ + const VkDescriptorSetLayoutBinding b[2] = { + { 0, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 1, VK_SHADER_STAGE_VERTEX_BIT | VK_SHADER_STAGE_FRAGMENT_BIT }, + { 1, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 1, VK_SHADER_STAGE_FRAGMENT_BIT }, + }; + const VkDescriptorSetLayoutCreateInfo li = { .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO, + .bindingCount = 2, .pBindings = b }; + VkDescriptorSetLayout l; + CK(vkCreateDescriptorSetLayout(dev, &li, NULL, &l)); + return l; +} + +static VkDescriptorSet vkm_set(VkDescriptorSetLayout dsl, const struct buf *ubo, VkDeviceSize ubo_size, + const struct img *tex, VkSampler smp) +{ + const VkDescriptorPoolSize ps[2] = { { VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 1 }, + { VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 1 } }; + const VkDescriptorPoolCreateInfo pi = { .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO, .maxSets = 1, + .poolSizeCount = 2, .pPoolSizes = ps }; + VkDescriptorPool dp; + CK(vkCreateDescriptorPool(dev, &pi, NULL, &dp)); + const VkDescriptorSetAllocateInfo ai = { .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO, + .descriptorPool = dp, .descriptorSetCount = 1, .pSetLayouts = &dsl }; + VkDescriptorSet s; + CK(vkAllocateDescriptorSets(dev, &ai, &s)); + const VkDescriptorBufferInfo bi = { ubo->b, 0, ubo_size }; + const VkDescriptorImageInfo ii = { smp, tex->v, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL }; + const VkWriteDescriptorSet w[2] = { + { .sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET, .dstSet = s, .dstBinding = 0, .descriptorCount = 1, + .descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, .pBufferInfo = &bi }, + { .sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET, .dstSet = s, .dstBinding = 1, .descriptorCount = 1, + .descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, .pImageInfo = &ii }, + }; + vkUpdateDescriptorSets(dev, 2, w, 0, NULL); + return s; +} + +/* column-major mat4 (std140): 0.5^k scale s, rotation by 90 degrees (x,y) -> (-y,x) when rot, translation (tx,ty) */ +static void vkm_mat(float *m, float s, int rot, float tx, float ty) +{ + memset(m, 0, 64); + if (rot) { + m[1] = s; /* column 0 = (0, s) */ + m[4] = -s; /* column 1 = (-s, 0) */ + } else { + m[0] = s; + m[5] = s; + } + m[10] = 1; + m[12] = tx; + m[13] = ty; + m[15] = 1; +} + +/* "vkmark texture/effect2d/desktop shaders at 256x256": which scene; every pixel against the CPU */ +enum vkm_scene { VKM_TEXTURE, VKM_BLUR, VKM_DESKTOP }; + +static void r_vkmark(enum vkm_scene scene) +{ + const uint32_t W = 256, H = 256; + struct img tex = upload_tex(256, 256, VK_FORMAT_R8G8B8A8_UNORM, 2, 1, vkm_tex_val); + VkSampler smp = newsampler_ex(VK_FILTER_NEAREST, VK_SAMPLER_MIPMAP_MODE_NEAREST, + VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE, 0); + VkDescriptorSetLayout dsl = vkm_dsl(); + /* vkmark's headless image: one level, optimal (twiddled on the A8) */ + struct img t = newimg(W, H, VK_FORMAT_B8G8R8A8_UNORM, VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | + VK_IMAGE_USAGE_TRANSFER_SRC_BIT, 1); + VkRenderPass rp = newrp(VK_FORMAT_UNDEFINED, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL); + VkFramebuffer fb = newfb(rp, &t, NULL); + struct buf rb = newbuf((VkDeviceSize)W * H * 4, VK_BUFFER_USAGE_TRANSFER_DST_BIT); + VkPipelineLayout pl = newlayout(dsl); + const char *vsn = scene == VKM_TEXTURE ? "light-basic-tex.vert" : scene == VKM_BLUR ? "effect2d.vert" : "desktop.vert"; + const char *fsn = scene == VKM_TEXTURE ? "light-basic-tex.frag" : scene == VKM_BLUR ? "effect2d-blur.frag" : "desktop.frag"; + const struct pipe_cfg pc = { vkm_shader(vsn), vkm_shader(fsn), scene == VKM_TEXTURE ? VIN_POS3_NRM3_TC2 : VIN_POS2_TC2, + 0, 0, pl, rp, W, H }; + VkPipeline p = newpipe(&pc); + + /* the full-screen quad (two triangles), and for desktop a window quad with texcoords 0..0.5 */ + static const float qp[6][2] = { { -1, -1 }, { 1, -1 }, { -1, 1 }, { -1, 1 }, { 1, -1 }, { 1, 1 } }; + struct buf vb = newbuf(2 * 6 * 32, VK_BUFFER_USAGE_VERTEX_BUFFER_BIT); + float *v = vb.p; + /* texture: normal (1,-1,2) (the NormalMatrix turns it to (1,1,2): diffuse 0.272 instead of 0.816) */ + for (int i = 0; i < 6; i++) { + if (scene == VKM_TEXTURE) { + const float d[8] = { qp[i][0], qp[i][1], 0, 1, -1, 2, (qp[i][0] + 1) / 2, (qp[i][1] + 1) / 2 }; + memcpy(v + 8 * i, d, 32); + } else { + const float d[4] = { qp[i][0], qp[i][1], (qp[i][0] + 1) / 2, (qp[i][1] + 1) / 2 }; + const float w[4] = { qp[i][0], qp[i][1], (qp[i][0] + 1) / 4, (qp[i][1] + 1) / 4 }; + memcpy(v + 4 * i, d, 16); + memcpy(v + 24 + 4 * i, w, 16); + } + } + /* uniform blocks: texture {MVP = rotate 90, NormalMatrix = rotate 90, MaterialDiffuse}; blur {1/256, 1/256}; + * desktop {identity} for the background and {0.5 * rotate 90 + (0.25, -0.25)} for the window */ + const float diffuse[4] = { 1.0f, 0.8f, 0.6f, 0.9f }; + struct buf u0 = newbuf(256, VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT), u1 = newbuf(256, VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT); + float *uf = u0.p; + if (scene == VKM_TEXTURE) { + vkm_mat(uf, 1, 1, 0, 0); + vkm_mat(uf + 16, 1, 1, 0, 0); + memcpy(uf + 32, diffuse, 16); + } else if (scene == VKM_BLUR) { + uf[0] = 1.0f / W; + uf[1] = 1.0f / H; + } else { + vkm_mat(uf, 1, 0, 0, 0); + vkm_mat(u1.p, 0.5f, 1, 0.25f, -0.25f); + } + VkDescriptorSet s0 = vkm_set(dsl, &u0, scene == VKM_TEXTURE ? 144 : scene == VKM_BLUR ? 16 : 64, &tex, smp); + VkDescriptorSet s1 = scene == VKM_DESKTOP ? vkm_set(dsl, &u1, 64, &tex, smp) : VK_NULL_HANDLE; + + const float clear[4] = { 0.0f, 0.0f, 0.0f, 0.0f }; + VkCommandBuffer cmd = begin(0); + begin_pass(cmd, rp, fb, W, H, clear, 0); + vkCmdBindPipeline(cmd, VK_PIPELINE_BIND_POINT_GRAPHICS, p); + const VkDeviceSize off = 0, off2 = 6 * 16; + vkCmdBindVertexBuffers(cmd, 0, 1, &vb.b, &off); + vkCmdBindDescriptorSets(cmd, VK_PIPELINE_BIND_POINT_GRAPHICS, pl, 0, 1, &s0, 0, NULL); + vkCmdDraw(cmd, 6, 1, 0, 0); + if (scene == VKM_DESKTOP) { + vkCmdBindVertexBuffers(cmd, 0, 1, &vb.b, &off2); + vkCmdBindDescriptorSets(cmd, VK_PIPELINE_BIND_POINT_GRAPHICS, pl, 0, 1, &s1, 0, NULL); + vkCmdDraw(cmd, 6, 1, 0, 0); + } + vkCmdEndRenderPass(cmd); + readback(cmd, &t, &rb); + end_submit(cmd); + + /* the CPU reference (target bytes B,G,R,A; texture bytes R,G,B,A) */ + struct expect e = newexpect(W, H, clear); + const double ln = sqrt(20.0 * 20 + 20 * 20 + 10 * 10), nl = sqrt(6.0); + const double dif = fmax((1 * 20.0 / ln + 1 * -20.0 / ln + 2 * 10.0 / ln) / nl, 0); /* N = (1,1,2)/sqrt 6 */ + size_t window = 0; + for (uint32_t y = 0; y < H; y++) + for (uint32_t x = 0; x < W; x++) { + const double nx = (x + 0.5) / W * 2 - 1, ny = (y + 0.5) / H * 2 - 1; + uint8_t out[4]; /* R,G,B,A */ + if (scene == VKM_TEXTURE) { + /* MVP rotates by 90: pixel (nx, ny) shows the quad point (ny, -nx) */ + const int u = (int)floor((ny + 1) / 2 * 256), w = (int)floor((-nx + 1) / 2 * 256); + for (int c = 0; c < 4; c++) { + const double col = c < 3 ? dif * diffuse[c] : diffuse[3]; + out[c] = (uint8_t)lround(vkm_hash(u, w, c) * col); + } + } else if (scene == VKM_BLUR) { + for (int c = 0; c < 3; c++) { + double acc = 0; + for (int j = -1; j <= 1; j++) + for (int i = -2; i <= 2; i++) { + const int tx = (int)x + i < 0 ? 0 : (int)x + i > 255 ? 255 : (int)x + i; + const int ty = (int)y + j < 0 ? 0 : (int)y + j > 255 ? 255 : (int)y + j; + acc += vkm_hash(tx, ty, c) * 0.066667; + } + out[c] = (uint8_t)fmin(lround(acc), 255); + } + out[3] = 255; + } else { + /* the window: quad point (x', y') = ((ny + 0.25) * 2, -(nx - 0.25) * 2), texcoord (p + 1) / 4 */ + const double px = (ny + 0.25) * 2, py = -(nx - 0.25) * 2; + const double edge = 2.0 / 128; /* one window pixel */ + int u = (int)x, w = (int)y; + if (fabs(px) < 1 && fabs(py) < 1) { + if (fabs(px) > 1 - edge || fabs(py) > 1 - edge) { + e.skip[(size_t)y * W + x] = 1; + continue; + } + u = (int)floor((px + 1) / 4 * 256); + w = (int)floor((py + 1) / 4 * 256); + window++; + } + for (int c = 0; c < 4; c++) + out[c] = vkm_hash(u, w, c); + } + const uint8_t b[4] = { out[2], out[1], out[0], out[3] }; + expect_bytes(&e, x, y, b); + } + char d[160]; + snprintf(d, sizeof(d), "vkmark's %s + %s, %s, job %.2f ms", vsn, fsn, + scene == VKM_TEXTURE ? "texel * lit colour (MVP and NormalMatrix rotate 90)" + : scene == VKM_BLUR ? "15-tap blur of a hashed texture" + : "background + rotated half-size window", last_ms); + if (scene == VKM_DESKTOP && !nocheck && window < 120 * 120) { + printf("RESULT %s FAIL reference bug: window covers %zu pixels\n", rung, window); + finish(1); + } + check(&e, rb.p, 2, d); +} + +/* texminperf: what one-level texture layouts cost the GPU when sampled minified (vkmark's effect2d: its 800x600 + * background, one level, drawn into 256x256 with a 15-tap blur). vkmark's own effect2d SPIR-V, LINEAR, the quad drawn + * 8 times a pass, the pass submitted 10 times; per layout the average job time, and the first pass checked against a + * CPU bilinear reference, every pixel. Layouts: an optimal 800x600 one-level image (twiddled on the A8 since update + * 29, padded to 1024x1024), the same texels in a LINEAR-tiling image (linear, Apple's buffer-backed form), and an + * optimal 1024x1024 one-level image (twiddled, no padding), sampled over the same 800x600 texel window. */ +static uint8_t tmp_val(int x, int y, int c) +{ + return vkm_hash(x, y, c); +} + +static double bilerp(const uint8_t *tx, int tw, int th, int pitch, double u, double v, int c) +{ + /* u, v in texels; texel centres at +0.5; clamp to edge */ + const double fx = u - 0.5, fy = v - 0.5; + const int x0 = (int)floor(fx), y0 = (int)floor(fy); + const double ax = fx - x0, ay = fy - y0; + double acc = 0; + for (int j = 0; j < 2; j++) + for (int i = 0; i < 2; i++) { + int x = x0 + i, y = y0 + j; + x = x < 0 ? 0 : x > tw - 1 ? tw - 1 : x; + y = y < 0 ? 0 : y > th - 1 ? th - 1 : y; + acc += tx[y * pitch + 4 * x + c] * (i ? ax : 1 - ax) * (j ? ay : 1 - ay); + } + return acc; +} + +static struct img texminperf_first; + +static void r_texminperf(void) +{ + const uint32_t W = 256, H = 256, TW = 800, TH = 600; + /* two samplers: maxLod 0 (no mip selection possible), and a trilinear one with maxLod VK_LOD_CLAMP_NONE (an + * application that does not know the texture has one level: the TPU must still read one level) */ + VkSampler smps[2] = { newsampler_ex(VK_FILTER_LINEAR, VK_SAMPLER_MIPMAP_MODE_NEAREST, + VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE, 0), + newsampler_ex(VK_FILTER_LINEAR, VK_SAMPLER_MIPMAP_MODE_LINEAR, + VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE, VK_LOD_CLAMP_NONE) }; + VkDescriptorSetLayout dsl = vkm_dsl(); + struct img t = newimg_ex(W, H, VK_FORMAT_B8G8R8A8_UNORM, VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | + VK_IMAGE_USAGE_TRANSFER_SRC_BIT, 1, 1, 0); + VkRenderPass rp = newrp(VK_FORMAT_UNDEFINED, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL); + VkFramebuffer fb = newfb(rp, &t, NULL); + struct buf rb = newbuf((VkDeviceSize)W * H * 4, VK_BUFFER_USAGE_TRANSFER_DST_BIT); + VkPipelineLayout pl = newlayout(dsl); + const struct pipe_cfg pc = { vkm_shader("effect2d.vert"), vkm_shader("effect2d-blur.frag"), VIN_POS2_TC2, 0, 0, pl, rp, + W, H }; + VkPipeline p = newpipe(&pc); + static const float qp[6][2] = { { -1, -1 }, { 1, -1 }, { -1, 1 }, { -1, 1 }, { 1, -1 }, { 1, 1 } }; + const char *names[3] = { "optimal 800x600 (twiddled, padded)", "LINEAR-tiling 800x600 (linear)", + "optimal 1024x1024 (twiddled), 800x600 window" }; + double avg[3][3] = { { 0 } }; + uint8_t *src = malloc(1024 * 1024 * 4); + for (int y = 0; y < 1024; y++) + for (int x = 0; x < 1024; x++) + for (int c = 0; c < 4; c++) + src[4 * (y * 1024 + x) + c] = tmp_val(x, y, c); + /* sm 2: vkmark's own texture format, R8G8B8A8_SRGB (its effect2d/desktop/texture images), trilinear sampler; + * timed only (no sRGB reference) */ + for (int vs = 0; vs < 9; vs++) { + const int v = vs / 3, sm = vs % 3; + VkSampler smp = smps[sm ? 1 : 0]; + const VkFormat tf = sm == 2 ? VK_FORMAT_R8G8B8A8_SRGB : VK_FORMAT_R8G8B8A8_UNORM; + const uint32_t iw = v == 2 ? 1024 : TW, ih = v == 2 ? 1024 : TH; + static struct img texs[3]; + struct img tex; + if (sm == 1) { + tex = texs[v]; + } else if (v == 1) { + tex = newimg_ex(iw, ih, tf, VK_IMAGE_USAGE_SAMPLED_BIT, 1, 1, 1); + const VkImageSubresource sub = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0 }; + VkSubresourceLayout lay; + vkGetImageSubresourceLayout(dev, tex.i, &sub, &lay); + uint8_t *map; + CK(vkMapMemory(dev, tex.m, 0, VK_WHOLE_SIZE, 0, (void **)&map)); + for (uint32_t y = 0; y < ih; y++) + memcpy(map + lay.offset + y * lay.rowPitch, src + 4 * y * 1024, 4 * iw); + VkCommandBuffer cmd = begin(0); + image_barrier(cmd, tex.i, VK_IMAGE_LAYOUT_PREINITIALIZED, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL, + VK_PIPELINE_STAGE_HOST_BIT, VK_ACCESS_HOST_WRITE_BIT, VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT, + VK_ACCESS_SHADER_READ_BIT); + end_submit(cmd); + } else { + tex = upload_tex(iw, ih, tf, 1, 1, vkm_tex_val); + } + if (sm == 0) { + texs[v] = tex; + if (v == 0) + texminperf_first = tex; + } + /* uv over the 800x600 texel window, steps of one texel */ + const float su = (float)TW / iw, sv = (float)TH / ih; + struct buf vb = newbuf(6 * 16, VK_BUFFER_USAGE_VERTEX_BUFFER_BIT); + for (int i = 0; i < 6; i++) { + const float d[4] = { qp[i][0], qp[i][1], (qp[i][0] + 1) / 2 * su, (qp[i][1] + 1) / 2 * sv }; + memcpy((float *)vb.p + 4 * i, d, 16); + } + struct buf u = newbuf(256, VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT); + ((float *)u.p)[0] = 1.0f / iw; + ((float *)u.p)[1] = 1.0f / ih; + VkDescriptorSet set = vkm_set(dsl, &u, 16, &tex, smp); + const float clear[4] = { 0, 0, 0, 0 }; + /* the timed command buffer has no readback (a CPU copy on the A8); a separate one checks the pixels */ + VkCommandBuffer cmds[2]; + for (int c = 0; c < 2; c++) { + VkCommandBuffer cmd = cmds[c] = begin(1); + begin_pass(cmd, rp, fb, W, H, clear, 0); + vkCmdBindPipeline(cmd, VK_PIPELINE_BIND_POINT_GRAPHICS, p); + const VkDeviceSize off = 0; + vkCmdBindVertexBuffers(cmd, 0, 1, &vb.b, &off); + vkCmdBindDescriptorSets(cmd, VK_PIPELINE_BIND_POINT_GRAPHICS, pl, 0, 1, &set, 0, NULL); + for (int k = 0; k < 8; k++) + vkCmdDraw(cmd, 6, 1, 0, 0); + vkCmdEndRenderPass(cmd); + if (c) + readback(cmd, &t, &rb); + CK(vkEndCommandBuffer(cmd)); + } + double sum = 0; + for (int f = 0; f < 11; f++) { + submit(cmds[f == 0]); + if (f > 1) + sum += last_ms; + if (!f && !nocheck && sm < 2) { + /* the reference: 15 bilinear taps of one-texel steps at the pixel's uv */ + size_t bad = 0, first = (size_t)-1; + const uint8_t *g = rb.p; + for (uint32_t y = 0; y < H; y++) + for (uint32_t x = 0; x < W; x++) { + const double uu = (x + 0.5) / W * TW, vv = (y + 0.5) / H * TH; + const int bc[3] = { 2, 1, 0 }; /* target bytes B,G,R from texture channels R,G,B */ + for (int c = 0; c < 3; c++) { + double acc = 0; + for (int j = -1; j <= 1; j++) + for (int i = -2; i <= 2; i++) + acc += bilerp(src, iw, ih, 1024 * 4, uu + i, vv + j, c) * 0.066667; + if (fabs(g[4 * (y * W + x) + bc[c]] - acc) > 4) { + if (first == (size_t)-1) + first = (size_t)y * W + x; + bad++; + break; + } + } + } + if (bad) { + printf("RESULT %s FAIL %s, sampler %s: %zu/%u pixels wrong, first (%zu,%zu)\n", rung, names[v], + sm ? "trilinear maxLod none" : "maxLod 0", bad, W * H, first % W, first / W); + finish(1); + } + } + } + avg[v][sm] = sum / 9; /* submissions 2..10 (1 warms the timed buffer) */ + printf("TEXMIN %s, %s: %.2f ms a job (8 x 15-tap blur over 256x256, minified 800x600)\n", names[v], + sm == 2 ? "SRGB, trilinear maxLod none" : sm ? "trilinear maxLod none" : "maxLod 0", avg[v][sm]); + } + /* does a job cost more with more memory bound (the padded texture's 4 MB against 1.9 MB)? 32 MB more, unused but + * uploaded once, then the first layout's job again */ + double more = 0; + { + struct img big = upload_tex(2048, 2048, VK_FORMAT_R8G8B8A8_UNORM, 1, 1, vkm_tex_val); + struct img big2 = upload_tex(2048, 2048, VK_FORMAT_R8G8B8A8_UNORM, 1, 1, vkm_tex_val); + (void)big; + (void)big2; + struct buf vb = newbuf(6 * 16, VK_BUFFER_USAGE_VERTEX_BUFFER_BIT); + for (int i = 0; i < 6; i++) { + const float d[4] = { qp[i][0], qp[i][1], (qp[i][0] + 1) / 2, (qp[i][1] + 1) / 2 }; + memcpy((float *)vb.p + 4 * i, d, 16); + } + struct buf u = newbuf(256, VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT); + ((float *)u.p)[0] = 1.0f / TW; + ((float *)u.p)[1] = 1.0f / TH; + VkDescriptorSet set = vkm_set(dsl, &u, 16, &texminperf_first, smps[0]); + const float clear[4] = { 0, 0, 0, 0 }; + VkCommandBuffer cmd = begin(1); + begin_pass(cmd, rp, fb, W, H, clear, 0); + vkCmdBindPipeline(cmd, VK_PIPELINE_BIND_POINT_GRAPHICS, p); + const VkDeviceSize off = 0; + vkCmdBindVertexBuffers(cmd, 0, 1, &vb.b, &off); + vkCmdBindDescriptorSets(cmd, VK_PIPELINE_BIND_POINT_GRAPHICS, pl, 0, 1, &set, 0, NULL); + for (int k = 0; k < 8; k++) + vkCmdDraw(cmd, 6, 1, 0, 0); + vkCmdEndRenderPass(cmd); + CK(vkEndCommandBuffer(cmd)); + double sum = 0; + for (int f = 0; f < 10; f++) { + submit(cmd); + if (f) + sum += last_ms; + } + more = sum / 9; + printf("TEXMIN optimal 800x600 (twiddled, padded), maxLod 0, with 32 MB more images bound: %.2f ms a job\n", more); + } + printf("RESULT %s PASS%s ms a job (maxLod 0 / trilinear / SRGB trilinear): twiddled-padded %.2f/%.2f/%.2f, " + "linear %.2f/%.2f/%.2f, twiddled-pot %.2f/%.2f/%.2f; twiddled-padded with 32 MB more bound %.2f\n", rung, + nocheck ? " (not checked)" : "", avg[0][0], avg[0][1], avg[0][2], avg[1][0], avg[1][1], avg[1][2], avg[2][0], + avg[2][1], avg[2][2], more); + finish(0); +} + +/* texvkmark: vkmark's effect2d draw exactly (as recorded: sampler words 08000fff/0 = NEAREST min/mag, REPEAT, + * maxLod ~0; TextureStep 1/256 = one window pixel; the full-screen quad, uv 0..1), so the 15 taps of a pixel are + * 3.1 x 2.3 texels apart in its 800x600 one-level texture: sparse point sampling, minified, no mips. Per layout and + * mode the average job time (8 x overdraw, 9 timed jobs); the UNORM NEAREST pass of each layout is checked against + * the CPU (nearest texel, wrapped), every pixel. Modes: NEAREST UNORM, NEAREST SRGB (vkmark's format, timed), LINEAR + * UNORM (2x2 footprints, timed). Layouts: optimal 800x600 one-level (twiddled, padded), the same texels LINEAR-tiled, + * optimal 1024x1024 one-level (twiddled, unpadded) sampled over the same 800x600 window. */ +static void r_texvkmark(void) +{ + const uint32_t W = 256, H = 256, TW = 800, TH = 600; + VkSampler nearest = newsampler_ex(VK_FILTER_NEAREST, VK_SAMPLER_MIPMAP_MODE_NEAREST, VK_SAMPLER_ADDRESS_MODE_REPEAT, 0); + VkSampler linear = newsampler_ex(VK_FILTER_LINEAR, VK_SAMPLER_MIPMAP_MODE_NEAREST, VK_SAMPLER_ADDRESS_MODE_REPEAT, 0); + VkDescriptorSetLayout dsl = vkm_dsl(); + struct img t = newimg_ex(W, H, VK_FORMAT_B8G8R8A8_UNORM, VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | + VK_IMAGE_USAGE_TRANSFER_SRC_BIT, 1, 1, 0); + VkRenderPass rp = newrp(VK_FORMAT_UNDEFINED, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL); + VkFramebuffer fb = newfb(rp, &t, NULL); + struct buf rb = newbuf((VkDeviceSize)W * H * 4, VK_BUFFER_USAGE_TRANSFER_DST_BIT); + VkPipelineLayout pl = newlayout(dsl); + const struct pipe_cfg pc = { vkm_shader("effect2d.vert"), vkm_shader("effect2d-blur.frag"), VIN_POS2_TC2, 0, 0, pl, rp, + W, H }; + VkPipeline p = newpipe(&pc); + static const float qp[6][2] = { { -1, -1 }, { 1, -1 }, { -1, 1 }, { -1, 1 }, { 1, -1 }, { 1, 1 } }; + const char *lnames[3] = { "twiddled-padded", "linear", "twiddled-pot" }; + const char *mnames[3] = { "NEAREST UNORM", "NEAREST SRGB", "LINEAR UNORM" }; + double avg[3][3] = { { 0 } }; + uint8_t *src = malloc(1024 * 1024 * 4); + for (int y = 0; y < 1024; y++) + for (int x = 0; x < 1024; x++) + for (int c = 0; c < 4; c++) + src[4 * (y * 1024 + x) + c] = tmp_val(x, y, c); + struct buf u = newbuf(256, VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT); + ((float *)u.p)[0] = 1.0f / W; /* vkmark: one window pixel */ + ((float *)u.p)[1] = 1.0f / H; + for (int lm = 0; lm < 9; lm++) { + const int l = lm / 3, m = lm % 3; + const uint32_t iw = l == 2 ? 1024 : TW, ih = l == 2 ? 1024 : TH; + const VkFormat tf = m == 1 ? VK_FORMAT_R8G8B8A8_SRGB : VK_FORMAT_R8G8B8A8_UNORM; + struct img tex; + if (l == 1) { + tex = newimg_ex(iw, ih, tf, VK_IMAGE_USAGE_SAMPLED_BIT, 1, 1, 1); + const VkImageSubresource sub = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0 }; + VkSubresourceLayout lay; + vkGetImageSubresourceLayout(dev, tex.i, &sub, &lay); + uint8_t *map; + CK(vkMapMemory(dev, tex.m, 0, VK_WHOLE_SIZE, 0, (void **)&map)); + for (uint32_t y = 0; y < ih; y++) + memcpy(map + lay.offset + y * lay.rowPitch, src + 4 * y * 1024, 4 * iw); + VkCommandBuffer cmd = begin(0); + image_barrier(cmd, tex.i, VK_IMAGE_LAYOUT_PREINITIALIZED, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL, + VK_PIPELINE_STAGE_HOST_BIT, VK_ACCESS_HOST_WRITE_BIT, VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT, + VK_ACCESS_SHADER_READ_BIT); + end_submit(cmd); + } else { + tex = upload_tex(iw, ih, tf, 1, 1, vkm_tex_val); + } + const float su = (float)TW / iw, sv = (float)TH / ih; + struct buf vb = newbuf(6 * 16, VK_BUFFER_USAGE_VERTEX_BUFFER_BIT); + for (int i = 0; i < 6; i++) { + const float d[4] = { qp[i][0], qp[i][1], (qp[i][0] + 1) / 2 * su, (qp[i][1] + 1) / 2 * sv }; + memcpy((float *)vb.p + 4 * i, d, 16); + } + VkDescriptorSet set = vkm_set(dsl, &u, 16, &tex, m == 2 ? linear : nearest); + const float clear[4] = { 0, 0, 0, 0 }; + VkCommandBuffer cmds[2]; + for (int c = 0; c < 2; c++) { + VkCommandBuffer cmd = cmds[c] = begin(1); + begin_pass(cmd, rp, fb, W, H, clear, 0); + vkCmdBindPipeline(cmd, VK_PIPELINE_BIND_POINT_GRAPHICS, p); + const VkDeviceSize off = 0; + vkCmdBindVertexBuffers(cmd, 0, 1, &vb.b, &off); + vkCmdBindDescriptorSets(cmd, VK_PIPELINE_BIND_POINT_GRAPHICS, pl, 0, 1, &set, 0, NULL); + vkCmdDraw(cmd, 6, 1, 0, 0); /* one draw a job, as vkmark (overdraw keeps a tile's texels in the cache) */ + vkCmdEndRenderPass(cmd); + if (c) + readback(cmd, &t, &rb); + CK(vkEndCommandBuffer(cmd)); + } + double sum = 0; + for (int f = 0; f < 22; f++) { + submit(cmds[f == 0]); + if (f > 1) + sum += last_ms; + if (!f && !nocheck && m == 0) { + /* nearest texel of each tap, wrapped (REPEAT) over the image, taps 1/256 apart in uv */ + size_t bad = 0, first = (size_t)-1; + const uint8_t *g = rb.p; + for (uint32_t y = 0; y < H; y++) + for (uint32_t x = 0; x < W; x++) { + const int bc[3] = { 2, 1, 0 }; + for (int c = 0; c < 3; c++) { + double acc = 0; + for (int j = -1; j <= 1; j++) + for (int i = -2; i <= 2; i++) { + const double tu = ((x + 0.5) / W) * su + i / (double)W, tv = ((y + 0.5) / H) * sv + j / (double)H; + int tx = (int)floor(tu * iw), ty = (int)floor(tv * ih); + tx = ((tx % (int)iw) + iw) % iw; + ty = ((ty % (int)ih) + ih) % ih; + acc += src[4 * (ty * 1024 + tx) + c] * 0.066667; + } + if (fabs(g[4 * (y * W + x) + bc[c]] - acc) > 3) { + if (first == (size_t)-1) + first = (size_t)y * W + x; + bad++; + break; + } + } + } + if (bad) { + printf("RESULT %s FAIL %s %s: %zu/%u pixels wrong, first (%zu,%zu)\n", rung, lnames[l], mnames[m], bad, + W * H, first % W, first / W); + finish(1); + } + } + } + avg[l][m] = sum / 20; + printf("TEXVKMARK %s %s: %.2f ms a job (vkmark's effect2d blur once, 800x600 texels)\n", lnames[l], mnames[m], + avg[l][m]); + } + printf("RESULT %s PASS%s ms a job (NEAREST UNORM / NEAREST SRGB / LINEAR UNORM): twiddled-padded %.2f/%.2f/%.2f, " + "linear %.2f/%.2f/%.2f, twiddled-pot %.2f/%.2f/%.2f\n", rung, nocheck ? " (not checked)" : "", avg[0][0], + avg[0][1], avg[0][2], avg[1][0], avg[1][1], avg[1][2], avg[2][0], avg[2][1], avg[2][2]); + finish(0); +} + +/* ---- table: least to most hang risk ---- */ +struct rung_desc { + const char *name, *risk, *what; +}; +static const struct rung_desc rungs[] = { + { "clear64", "low", "clear-only pass 64x64" }, + { "tri64", "low", "the smoke triangle 64x64 (tested)" }, + { "clear256", "low", "clear-only 256x256" }, + { "tri256", "low", "triangle 256x256" }, + { "frames100", "low", "the 64x64 triangle submitted 100 times (one command buffer), fence wait avg/max" }, + { "pc", "low-med", "one rectangle, push-constant colour (VS+FS push constants, 6-vertex corner buffer)" }, + { "draws16", "low-med", "16 rectangles, 16 push-constant colours, one pass" }, + { "vbib", "low-med", "indexed draw (uint16), vec2 position + vec4 colour attributes, varyings" }, + { "ubo", "med", "uniform-buffer colour in the FS (descriptor set)" }, + { "clear1024", "med", "clear-only 1024x1024 (region ring 0x20000, 1024 tiles)" }, + { "clear750x1334", "med", "clear-only at screen size" }, + { "compute64k", "med", "65536 invocations (2048 workgroups), every value checked" }, + { "c2g", "med", "compute writes the triangle's vertices, then the draw reads them" }, + { "timing", "med", "wall-clock ms per job: 10 clears, 10 triangles, 10 dispatches; timestamp bits reported" }, + { "tri1024", "high", "triangle 1024x1024 (the kernel's fixed 64-page PM pool, no grow: running out would need a " + "partial render, which the A8 path cannot do yet)" }, + { "tri750x1334", "high", "triangle at screen size (same PM-pool risk)" }, + { "blend", "high", "SRC_ALPHA blend over the left half (tile read in the FS: unproven on the A8)" }, + { "depth32", "high", "D32 attachment, LESS, 3 layered rectangles (A8 ZLS words unproven)" }, + { "depth16", "high", "D16 attachment (SKIP: the A8 driver builds D32F/D32F_S8 only, refused before submit)" }, + { "texnearest", "high", "upload a 64x64 texture, sample NEAREST (A8 texture state unproven on hardware)" }, + { "texlinear", "high", "the same texture, LINEAR into 128x128" }, + { "varyings", "med", "four vertex colours interpolated over two triangles, every pixel against CPU barycentrics" }, + { "texmap", "high", "texel (x,y) encoded in its colour, NEAREST 1:1 (twiddled, 2 levels): which texel each pixel got" }, + { "texmaplin", "high", "texmap with a one-level texture (twiddled on the A8; texhostlinear is linear)" }, + { "texmath", "med", "two samples whose 32-bit results feed arithmetic (average of a texel and its neighbour)" }, + { "texrgba", "med", "an RGBA8 texture (texmap encoding) into the BGRA8 target, NEAREST 1:1" }, + { "texnpot", "med", "a 100x60 texture, twiddled (2 levels), NEAREST 1:1 into 100x60" }, + { "texnpotlin", "med", "the 100x60 texture with one level (twiddled on the A8)" }, + { "texrepeat", "med", "REPEAT, uv 0..1.5 over 64 pixels, every texel index checked" }, + { "texmirror", "med", "MIRRORED_REPEAT, uv 0..1.5" }, + { "texmip", "med-high", "7 levels of one colour each, NEAREST mip: LOD 0..4 into 64..4 pixel targets" }, + { "texhostlinear", "med-high", "a LINEAR-tiling host-visible image written by the CPU at its row pitch, sampled" }, + { "tex2", "med", "two textures in one fragment program (SKIP while the A8 path refuses a second image slot)" }, + { "load2", "high", "clear + left half, then a LOAD pass drawing the top half (Apple's load background, untested)" }, + { "load3", "high", "three passes: clear, LOAD, LOAD (a centre box)" }, + { "load750x1334", "high", "load2 at screen size" }, + { "texdiag", "high", "texnearest with PVR_G5P_TEXDIAG=1: Mesa prints each texture/sampler upload's words and texaddr" }, + { "rtt", "high", "render the 16-colour grid into a texture, sample it in a second pass" }, + { "clearimg", "med", "vkCmdClearColorImage of level 0 to A, then B (a GPU render with a clear background)" }, + { "clearimg750x1334", "med", "clearimg at screen size (twiddled): one clear's job time" }, + { "clearimglin", "med-high", "clearimg into a LINEAR-tiling target (Apple's linear PBE form)" }, + { "clearimglin750x1334", "med-high", "clearimglin at screen size: one clear's job time" }, + { "clearimgmip", "med-high", "clearimg whose first clear covers levels 0 and 1 (a render into mip level 1, untested)" }, + { "vkmtexture", "high", "vkmark's light-basic-tex shaders (its texture scene), 256x256 linear target, every pixel vs CPU" }, + { "vkmeffect2d", "high", "vkmark's effect2d + effect2d-blur shaders (15 taps, UBO in the FS), 256x256 vs CPU" }, + { "vkmdesktop", "high", "vkmark's desktop shaders: background + a rotated half-size window, 256x256 vs CPU" }, + { "texminperf", "high", "minified one-level 800x600 sampling (vkmark effect2d): twiddled-padded vs linear vs twiddled-POT, UNORM and vkmark's SRGB, job ms" }, + { "texvkmark", "high", "vkmark's effect2d draw exactly (NEAREST, REPEAT, taps 1/256 apart): twiddled-padded vs linear vs twiddled-POT job ms" }, + { "mesh", "highest", "one draw of 1k..64k triangles at 256x256 (5 s fence each): which size first hangs (PM pool)" }, + { "meshvary", "highest", "mesh with a vec4 varying per vertex (more parameter memory per vertex)" }, + { "litmesh", "highest", "a lit, depth-tested sphere of 3072 triangles at 256x256 (vkmark vertex/shading without textures)" }, + { "litmeshbig", "highest", "the lit sphere with 24576 triangles (past the 8k knee: the pool-grow regression test)" }, +}; + +int main(int argc, char **argv) +{ + setvbuf(stdout, NULL, _IOLBF, 0); + if (argc != 2 || !strcmp(argv[1], "list")) { + printf("rungs, least to most hang risk:\n"); + for (unsigned i = 0; i < sizeof(rungs) / sizeof(rungs[0]); i++) + printf(" %-14s %-8s %s\n", rungs[i].name, rungs[i].risk, rungs[i].what); + printf("not built on the A8 (refused by the driver today): MSAA 4x, loadOp LOAD of colour, indexed UBO/SSBO in " + "graphics shaders, partial render\n"); + return argc == 2 ? 0 : 2; + } + rung = argv[1]; + unsigned found = 0; + for (unsigned i = 0; i < sizeof(rungs) / sizeof(rungs[0]); i++) + found |= !strcmp(rungs[i].name, rung); + if (!found) { + fprintf(stderr, "unknown rung %s (a8-vk-ladder list)\n", rung); + return 2; + } + nocheck = getenv("A8_SMOKE_NOCHECK") != NULL; + if (!strcmp(rung, "texdiag") || !strcmp(rung, "texmap") || !strcmp(rung, "texmaplin")) + setenv("PVR_G5P_TEXDIAG", "1", 1); /* before the device: Mesa reads it per draw */ + teardown = getenv("A8_LADDER_TEARDOWN") != NULL; + t_start = now_ms(); + step("vkCreateInstance"); + if (getenv("A8_LADDER_TIMEOUT_MS")) + timeout_ns = strtoull(getenv("A8_LADDER_TIMEOUT_MS"), NULL, 0) * 1000000ull; + + const VkApplicationInfo app = { .sType = VK_STRUCTURE_TYPE_APPLICATION_INFO, .apiVersion = VK_API_VERSION_1_1 }; + const VkInstanceCreateInfo ici = { .sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO, .pApplicationInfo = &app }; + CK(vkCreateInstance(&ici, NULL, &inst)); + step("vkEnumeratePhysicalDevices"); + uint32_t n = 1; + VkResult r = vkEnumeratePhysicalDevices(inst, &n, &phy); + if ((r != VK_SUCCESS && r != VK_INCOMPLETE) || !n) { + printf("RESULT %s FAIL no Vulkan device (%d)\n", rung, r); + finish(1); + } + vkGetPhysicalDeviceMemoryProperties(phy, &memprops); + const float prio = 1.0f; + const VkDeviceQueueCreateInfo qi = { .sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO, .queueCount = 1, + .pQueuePriorities = &prio }; + const VkDeviceCreateInfo di = { .sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO, .queueCreateInfoCount = 1, + .pQueueCreateInfos = &qi }; + step("vkCreateDevice"); + CK(vkCreateDevice(phy, &di, NULL, &dev)); + step("device up, rung starts"); + vkGetDeviceQueue(dev, 0, 0, &q); + const VkCommandPoolCreateInfo cpi = { .sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO }; + CK(vkCreateCommandPool(dev, &cpi, NULL, &pool)); + +#define IS(x) !strcmp(rung, x) + if (IS("clear64")) r_clear(64, 64); + if (IS("clear256")) r_clear(256, 256); + if (IS("clear1024")) r_clear(1024, 1024); + if (IS("clear750x1334")) r_clear(750, 1334); + if (IS("tri64")) r_tri(64, 64); + if (IS("tri256")) r_tri(256, 256); + if (IS("tri1024")) r_tri(1024, 1024); + if (IS("tri750x1334")) r_tri(750, 1334); + if (IS("frames100")) r_frames(); + if (IS("pc")) r_pc(); + if (IS("draws16")) r_draws16(); + if (IS("vbib")) r_vbib(); + if (IS("ubo")) r_ubo(); + if (IS("blend")) r_blend(); + if (IS("depth32")) r_depth(VK_FORMAT_D32_SFLOAT); + if (IS("depth16")) r_depth(VK_FORMAT_D16_UNORM); + if (IS("texnearest")) r_tex(0); + if (IS("texlinear")) r_tex(1); + if (IS("texdiag")) r_tex(0); + if (IS("texmap")) r_texmap(2); + if (IS("texmaplin")) r_texmap(1); + if (IS("varyings")) r_varyings(); + if (IS("texrgba")) r_texrgba(); + if (IS("texnpot")) r_texnpot(2); + if (IS("texnpotlin")) r_texnpot(1); + if (IS("texrepeat")) r_texwrap(0); + if (IS("texmirror")) r_texwrap(1); + if (IS("texmip")) r_texmip(); + if (IS("texhostlinear")) r_texhostlinear(); + if (IS("tex2")) r_tex2(); + if (IS("load2")) r_load(64, 64, 2); + if (IS("mesh")) r_mesh(0); + if (IS("litmesh")) r_litmesh(48, 32); + if (IS("litmeshbig")) r_litmesh(128, 96); + if (IS("texmath")) r_texmath(); + if (IS("meshvary")) r_mesh(1); + if (IS("load3")) r_load(64, 64, 3); + if (IS("load750x1334")) r_load(750, 1334, 2); + if (IS("rtt")) r_rtt(); + if (IS("compute64k")) r_compute64k(); + if (IS("c2g")) r_c2g(); + if (IS("timing")) r_timing(); + if (IS("clearimg")) r_clearimg(256, 256, 0); + if (IS("clearimg750x1334")) r_clearimg(750, 1334, 0); + if (IS("clearimglin")) r_clearimg(256, 256, 2); + if (IS("clearimglin750x1334")) r_clearimg(750, 1334, 2); + if (IS("clearimgmip")) r_clearimg(256, 256, 1); + if (IS("vkmtexture")) r_vkmark(VKM_TEXTURE); + if (IS("texminperf")) r_texminperf(); + if (IS("texvkmark")) r_texvkmark(); + if (IS("vkmeffect2d")) r_vkmark(VKM_BLUR); + if (IS("vkmdesktop")) r_vkmark(VKM_DESKTOP); + printf("RESULT %s FAIL rung not dispatched\n", rung); + finish(1); +} diff --git a/examples/ipod-gpu-drm/vk/ladder/l_big.comp b/examples/ipod-gpu-drm/vk/ladder/l_big.comp new file mode 100644 index 00000000..d8eb6c65 --- /dev/null +++ b/examples/ipod-gpu-drm/vk/ladder/l_big.comp @@ -0,0 +1,5 @@ +#version 450 +// out[gid] = gid, 2048 workgroups of 32 (65536 invocations) +layout(local_size_x = 32) in; +layout(set = 0, binding = 0, std430) buffer Out { uint d[]; } o; +void main() { o.d[gl_GlobalInvocationID.x] = gl_GlobalInvocationID.x; } diff --git a/examples/ipod-gpu-drm/vk/ladder/l_const.frag b/examples/ipod-gpu-drm/vk/ladder/l_const.frag new file mode 100644 index 00000000..7b1f753e --- /dev/null +++ b/examples/ipod-gpu-drm/vk/ladder/l_const.frag @@ -0,0 +1,4 @@ +#version 450 +// a constant colour (1, 0.25, 0, 1) +layout(location = 0) out vec4 c; +void main() { c = vec4(1.0, 0.25, 0.0, 1.0); } diff --git a/examples/ipod-gpu-drm/vk/ladder/l_lit.vert b/examples/ipod-gpu-drm/vk/ladder/l_lit.vert new file mode 100644 index 00000000..4649be05 --- /dev/null +++ b/examples/ipod-gpu-drm/vk/ladder/l_lit.vert @@ -0,0 +1,11 @@ +#version 450 +// a lit mesh as vkmark's vertex/shading scenes have it: position + normal in, per-vertex diffuse lighting out +layout(location = 0) in vec3 pos; +layout(location = 1) in vec3 nrm; +layout(location = 0) out vec4 col; +void main() { + const vec3 L = normalize(vec3(0.3, -0.5, 0.8)); + float d = max(dot(normalize(nrm), L), 0.0); + col = vec4(vec3(0.15) + d * vec3(0.8, 0.6, 0.3), 1.0); + gl_Position = vec4(pos.xy * 0.8, 0.5 - 0.4 * pos.z, 1.0); +} diff --git a/examples/ipod-gpu-drm/vk/ladder/l_pc.frag b/examples/ipod-gpu-drm/vk/ladder/l_pc.frag new file mode 100644 index 00000000..02e78b7f --- /dev/null +++ b/examples/ipod-gpu-drm/vk/ladder/l_pc.frag @@ -0,0 +1,5 @@ +#version 450 +// the push-constant colour +layout(push_constant) uniform PC { vec4 rect; vec4 color; vec4 z; } pc; +layout(location = 0) out vec4 c; +void main() { c = pc.color; } diff --git a/examples/ipod-gpu-drm/vk/ladder/l_rect.vert b/examples/ipod-gpu-drm/vk/ladder/l_rect.vert new file mode 100644 index 00000000..d7585dcb --- /dev/null +++ b/examples/ipod-gpu-drm/vk/ladder/l_rect.vert @@ -0,0 +1,10 @@ +#version 450 +// an axis-aligned rectangle (NDC x0, y0, x1, y1) at depth z.x from the push constants; the vertex buffer holds the +// six corners in [0, 1] (two triangles), passed on as uv scaled by z.yz +layout(push_constant) uniform PC { vec4 rect; vec4 color; vec4 z; } pc; +layout(location = 0) in vec2 t; +layout(location = 0) out vec2 uv; +void main() { + uv = t * pc.z.yz; + gl_Position = vec4(mix(pc.rect.xy, pc.rect.zw, t), pc.z.x, 1.0); +} diff --git a/examples/ipod-gpu-drm/vk/ladder/l_tex.frag b/examples/ipod-gpu-drm/vk/ladder/l_tex.frag new file mode 100644 index 00000000..8cf60c7e --- /dev/null +++ b/examples/ipod-gpu-drm/vk/ladder/l_tex.frag @@ -0,0 +1,6 @@ +#version 450 +// the bound texture at the rectangle's uv +layout(set = 0, binding = 0) uniform sampler2D tex; +layout(location = 0) in vec2 uv; +layout(location = 0) out vec4 c; +void main() { c = texture(tex, uv); } diff --git a/examples/ipod-gpu-drm/vk/ladder/l_tex2.frag b/examples/ipod-gpu-drm/vk/ladder/l_tex2.frag new file mode 100644 index 00000000..243678b1 --- /dev/null +++ b/examples/ipod-gpu-drm/vk/ladder/l_tex2.frag @@ -0,0 +1,7 @@ +#version 450 +// two textures in one fragment program: the left half from t0, the right from t1 +layout(set = 0, binding = 0) uniform sampler2D t0; +layout(set = 0, binding = 1) uniform sampler2D t1; +layout(location = 0) in vec2 uv; +layout(location = 0) out vec4 c; +void main() { c = uv.x < 0.5 ? texture(t0, uv) : texture(t1, uv); } diff --git a/examples/ipod-gpu-drm/vk/ladder/l_texmath.frag b/examples/ipod-gpu-drm/vk/ladder/l_texmath.frag new file mode 100644 index 00000000..5d9790a7 --- /dev/null +++ b/examples/ipod-gpu-drm/vk/ladder/l_texmath.frag @@ -0,0 +1,6 @@ +#version 450 +// two samples whose results feed arithmetic: the average of the texel and its right neighbour (a 1/64 step) +layout(set = 0, binding = 0) uniform sampler2D tex; +layout(location = 0) in vec2 uv; +layout(location = 0) out vec4 c; +void main() { c = 0.5 * texture(tex, uv) + 0.5 * texture(tex, uv + vec2(1.0 / 64.0, 0.0)); } diff --git a/examples/ipod-gpu-drm/vk/ladder/l_tri.vert b/examples/ipod-gpu-drm/vk/ladder/l_tri.vert new file mode 100644 index 00000000..927a52f2 --- /dev/null +++ b/examples/ipod-gpu-drm/vk/ladder/l_tri.vert @@ -0,0 +1,4 @@ +#version 450 +// the smoke triangle's vertex shader: the position passed through +layout(location = 0) in vec4 pos; +void main() { gl_Position = pos; } diff --git a/examples/ipod-gpu-drm/vk/ladder/l_ubo.frag b/examples/ipod-gpu-drm/vk/ladder/l_ubo.frag new file mode 100644 index 00000000..42a61eeb --- /dev/null +++ b/examples/ipod-gpu-drm/vk/ladder/l_ubo.frag @@ -0,0 +1,5 @@ +#version 450 +// a uniform-buffer colour +layout(set = 0, binding = 0) uniform U { vec4 color; } u; +layout(location = 0) out vec4 c; +void main() { c = u.color; } diff --git a/examples/ipod-gpu-drm/vk/ladder/l_vcol.frag b/examples/ipod-gpu-drm/vk/ladder/l_vcol.frag new file mode 100644 index 00000000..f749b9c4 --- /dev/null +++ b/examples/ipod-gpu-drm/vk/ladder/l_vcol.frag @@ -0,0 +1,4 @@ +#version 450 +layout(location = 0) in vec4 vcol; +layout(location = 0) out vec4 c; +void main() { c = vcol; } diff --git a/examples/ipod-gpu-drm/vk/ladder/l_vcol.vert b/examples/ipod-gpu-drm/vk/ladder/l_vcol.vert new file mode 100644 index 00000000..bedbfa88 --- /dev/null +++ b/examples/ipod-gpu-drm/vk/ladder/l_vcol.vert @@ -0,0 +1,6 @@ +#version 450 +// vertex-buffer position and colour +layout(location = 0) in vec2 pos; +layout(location = 1) in vec4 col; +layout(location = 0) out vec4 vcol; +void main() { vcol = col; gl_Position = vec4(pos, 0.0, 1.0); } diff --git a/examples/ipod-gpu-drm/vk/ladder/l_verts.comp b/examples/ipod-gpu-drm/vk/ladder/l_verts.comp new file mode 100644 index 00000000..55fae1fa --- /dev/null +++ b/examples/ipod-gpu-drm/vk/ladder/l_verts.comp @@ -0,0 +1,9 @@ +#version 450 +// one invocation writes the smoke triangle's three vec4 vertices for a following draw (compute -> vertex input) +layout(local_size_x = 1) in; +layout(set = 0, binding = 0, std430) buffer Out { vec4 v[3]; } o; +void main() { + o.v[0] = vec4(-0.5, -0.5, 0.0, 1.0); + o.v[1] = vec4(0.5, -0.5, 0.0, 1.0); + o.v[2] = vec4(0.0, 0.5, 0.0, 1.0); +} diff --git a/examples/ipod-gpu-drm/vk/make-bundle.sh b/examples/ipod-gpu-drm/vk/make-bundle.sh new file mode 100755 index 00000000..b816cb9e --- /dev/null +++ b/examples/ipod-gpu-drm/vk/make-bundle.sh @@ -0,0 +1,87 @@ +#!/bin/bash +# SPDX-License-Identifier: MIT +# make-bundle.sh MESA_BUILD OUT: the /opt/a8vk Vulkan bundle for build.sh's A8_VK_DIR. +# It follows how the tested bundle was built, but has not been run from scratch yet; the tested Mesa +# build also had -Dplatforms=x11,wayland. +# +# Run it on aarch64 Linux (an Arch Linux ARM container works) with a C compiler, glslangValidator, the Vulkan +# loader (libvulkan.so.1) and, for vkmark, the vkmark package installed. MESA_BUILD is a meson build directory of +# OrchardLinux/mesa's A8 branch with the imagination Vulkan driver, e.g. +# meson setup MESA_BUILD MESA_SOURCE --buildtype=release -Dvulkan-drivers=imagination -Dgallium-drivers= \ +# -Dplatforms= -Dglx=disabled -Degl=disabled -Dgbm=disabled -Dllvm=disabled && ninja -C MESA_BUILD +# OUT gets a self-contained directory: nothing is installed on the iPod, every program starts through the +# bundled glibc loader (lib/ld-linux-aarch64.so.1 --library-path lib), so the initramfs only needs +# /dev/dri/renderD128, /sys and a writable /tmp. +# a8-vk-smoke, bin/a8-vk-smoke Vulkan probe, compute and triangle (a8-gpu-drm-test vk runs it) +# a8-vk-ladder.sh, bin/a8-vk-ladder the Vulkan ladder: ./a8-vk-ladder.sh list | RUNG +# vkmark.sh, vkmark/ vkmark, headless (only when vkmark is installed) +# lib/ libvulkan_powervr_mesa.so, its ICD manifest and every library they need +set -euo pipefail +here=$(cd "$(dirname "$0")" && pwd) +B=$(cd "${1:?usage: make-bundle.sh MESA_BUILD OUT}" && pwd) +OUT=${2:?usage: make-bundle.sh MESA_BUILD OUT} +[[ $(uname -m) == aarch64 ]] || { echo "make-bundle.sh: run it on aarch64 Linux" >&2; exit 1; } +ICD=$B/src/imagination/vulkan/libvulkan_powervr_mesa.so +[[ -f $ICD ]] || { echo "make-bundle.sh: no $ICD (build Mesa with -Dvulkan-drivers=imagination)" >&2; exit 1; } +T=$(mktemp -d) +trap 'rm -rf "$T"' EXIT +rm -rf "$OUT" +mkdir -p "$OUT/bin" "$OUT/lib" + +glslangValidator -V --vn a8_smoke_kstore "$here/k_store_const.comp" -o "$T/a8_smoke_kstore.h" >/dev/null +glslangValidator -V --vn a8_smoke_vs "$here/tri.vert" -o "$T/a8_smoke_vs.h" >/dev/null +glslangValidator -V --vn a8_smoke_fs "$here/tri.frag" -o "$T/a8_smoke_fs.h" >/dev/null +for s in "$here"/ladder/*.vert "$here"/ladder/*.frag "$here"/ladder/*.comp; do + b=$(basename "$s"); n=${b%.*}_${b##*.} + glslangValidator -V --vn "$n" "$s" -o "$T/$n.h" >/dev/null +done +cc -O2 -Wall -I"$T" "$here/a8-vk-smoke.c" -o "$OUT/bin/a8-vk-smoke" -lvulkan -lm +cc -O2 -Wall -I"$T" "$here/ladder/a8-vk-ladder.c" -o "$OUT/bin/a8-vk-ladder" -lvulkan -lm +strip "$OUT/bin/a8-vk-smoke" "$OUT/bin/a8-vk-ladder" + +strip --strip-unneeded -o "$OUT/lib/libvulkan_powervr_mesa.so" "$ICD" +cp -L "$(ldconfig -p | awk '/libvulkan.so.1 /{print $NF; exit}')" "$OUT/lib/libvulkan.so.1" +cat > "$OUT/lib/powervr_icd.json" <<'JSON' +{ "file_format_version": "1.0.1", "ICD": { "library_path": "./libvulkan_powervr_mesa.so", "api_version": "1.4.0" } } +JSON + +vkm= +if command -v vkmark >/dev/null; then + vkm=$(command -v vkmark) + mkdir -p "$OUT/vkmark/bin" "$OUT/vkmark/winsys" + cp "$vkm" "$OUT/vkmark/bin/" + strip "$OUT/vkmark/bin/vkmark" + winsys=$(dirname "$(find /usr/lib /usr/lib64 -name headless.so -path '*vkmark*' 2>/dev/null | head -1)") + cp "$winsys/headless.so" "$OUT/vkmark/winsys/" + cp -r /usr/share/vkmark "$OUT/vkmark/data" +fi + +# every shared library the programs, the loader and the driver need, and the dynamic loader itself +for x in "$OUT"/bin/* "$OUT"/lib/libvulkan.so.1 "$OUT"/lib/libvulkan_powervr_mesa.so ${vkm:+"$OUT"/vkmark/bin/vkmark "$OUT"/vkmark/winsys/headless.so}; do + ldd "$x" | awk '/=> \//{print $3}' +done | sort -u | while read -r l; do cp -nL "$l" "$OUT/lib/"; done +cp -nL /lib/ld-linux-aarch64.so.1 "$OUT/lib/" + +wrapper() { # wrapper NAME PROGRAM [EXTRA LIBRARY PATH] [ARGUMENTS...] + local name=$1 prog=$2 extra=$3 + shift 3 + cat > "$OUT/$name" < SHA256SUMS) +du -sh "$OUT" diff --git a/examples/ipod-gpu-drm/vk/tri.frag b/examples/ipod-gpu-drm/vk/tri.frag new file mode 100644 index 00000000..6a791e56 --- /dev/null +++ b/examples/ipod-gpu-drm/vk/tri.frag @@ -0,0 +1,4 @@ +#version 450 +// metal-triangle's fragment shader: a constant colour (1, 0.25, 0, 1) +layout(location = 0) out vec4 c; +void main() { c = vec4(1.0, 0.25, 0.0, 1.0); } diff --git a/examples/ipod-gpu-drm/vk/tri.vert b/examples/ipod-gpu-drm/vk/tri.vert new file mode 100644 index 00000000..1ce6684a --- /dev/null +++ b/examples/ipod-gpu-drm/vk/tri.vert @@ -0,0 +1,4 @@ +#version 450 +// metal-triangle's vertex shader: the position passed through +layout(location = 0) in vec4 pos; +void main() { gl_Position = pos; } From c54a2b219e6891181a9c52373e21765ea2065686 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Berkay=20K=C3=B6sebay?= Date: Sat, 21 Nov 2026 23:07:38 +0100 Subject: [PATCH 6/8] kernel: ANS1 internal storage on the iPod touch 6 (n102) The iPod touch 6 keeps its flash behind ANS1 like the iPad Air 2, but its ANS firmware (iOS 12.4) speaks RTKit 10.2, where the storage endpoint is 6 instead of 0x20. - soc-apple/0028: RTKit 10 firmware sends its minor version in the HELLO's minimum-version field and wants it back; keep it, answer with it and let drivers read the protocol version. RTKit 11 and later are unchanged. - block/0003: asp takes its storage endpoint from the protocol version (6 on 10.2, 0x20 on 11 and later); 10.0 is refused by name. - devices/t7000/0013: the ANS nodes, as Nick Chan wrote them. - devices/t7000/0014: enabled on the N102 only, ps_ans kept on as the boot loader leaves it. The iPad Air 2 (J81, RTKit 12) runs the same rtkit and asp code; its path is meant to be unchanged and was not retested. Assisted-by: LLM --- ...-arm64-dts-apple-t7000-Add-ASP-Nodes.patch | 80 ++++++++++++ ...00-Enable-internal-storage-on-the-iP.patch | 79 +++++++++++ ...d-the-storage-endpoint-on-RTKit-10.2.patch | 60 +++++++++ ...Answer-protocol-version-10-with-its-.patch | 123 ++++++++++++++++++ 4 files changed, 342 insertions(+) create mode 100644 kernel/patches/devices/t7000/0013-arm64-dts-apple-t7000-Add-ASP-Nodes.patch create mode 100644 kernel/patches/devices/t7000/0014-arm64-dts-apple-t7000-Enable-internal-storage-on-the-iP.patch create mode 100644 kernel/patches/drivers/block/0003-block-asp-Find-the-storage-endpoint-on-RTKit-10.2.patch create mode 100644 kernel/patches/drivers/soc-apple/0028-soc-apple-rtkit-Answer-protocol-version-10-with-its-.patch diff --git a/kernel/patches/devices/t7000/0013-arm64-dts-apple-t7000-Add-ASP-Nodes.patch b/kernel/patches/devices/t7000/0013-arm64-dts-apple-t7000-Add-ASP-Nodes.patch new file mode 100644 index 00000000..0604ecdb --- /dev/null +++ b/kernel/patches/devices/t7000/0013-arm64-dts-apple-t7000-Add-ASP-Nodes.patch @@ -0,0 +1,80 @@ +From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 +From: Nick Chan +Date: Tue, 1 Sep 2026 11:22:31 +0800 +Subject: [PATCH] arm64: dts: apple: t7000: Add ASP Nodes + +XXX TODO + +[Orchard: from HoolockLinux/linux ans1 ed8528f (c13623f9ac9b), unchanged. +It describes the A8 storage coprocessor (ANS1) and its mailbox, with the +firmware region m1n1 fills in through the asp alias.] +Signed-off-by: Nick Chan +--- + arch/arm64/boot/dts/apple/t7000-common.dtsi | 12 +++++++++++ + arch/arm64/boot/dts/apple/t7000.dtsi | 23 +++++++++++++++++++++ + 2 files changed, 35 insertions(+) + +diff --git a/arch/arm64/boot/dts/apple/t7000-common.dtsi b/arch/arm64/boot/dts/apple/t7000-common.dtsi +index 0d40a5753..11f251162 100644 +--- a/arch/arm64/boot/dts/apple/t7000-common.dtsi ++++ b/arch/arm64/boot/dts/apple/t7000-common.dtsi +@@ -12,6 +12,7 @@ + / { + aliases { + ausbphy0 = &ausbphy0; /* Used by loader */ ++ asp = &ans; + serial0 = &serial0; + serial6 = &serial6; + }; +@@ -32,6 +33,17 @@ + #size-cells = <2>; + ranges; + ++ ans_firmware: ans-firmware { ++ /* Filled in by bootloder */ ++ reg = <0 0 0 0>; ++ no-map; ++ }; + /* To be filled by loader */ + }; + }; ++ ++&ans { ++ memory-region = <&ans_firmware>; ++ memory-region-names = "firmware"; ++ status = "okay"; ++}; +diff --git a/arch/arm64/boot/dts/apple/t7000.dtsi b/arch/arm64/boot/dts/apple/t7000.dtsi +index 1a165a7b8..3a93dbe2e 100644 +--- a/arch/arm64/boot/dts/apple/t7000.dtsi ++++ b/arch/arm64/boot/dts/apple/t7000.dtsi +@@ -152,6 +152,29 @@ + #performance-domain-cells = <0>; + }; + ++ ans: block@208040000 { ++ compatible = "apple,t7000-ans", "apple,s5l8960x-ans"; ++ reg = <0x2 0x08040000 0x0 0x1000>; ++ mboxes = <&ans_mbox>; ++ power-domains = <&ps_ans>; ++ resets = <&ps_ans>; ++ status = "disabled"; ++ }; ++ ++ ans_mbox: mbox@208041000 { ++ compatible = "apple,t7000-akf-mailbox-v1", "apple,akf-mailbox-v1"; ++ reg = <0x2 0x08041000 0x0 0x1000>; ++ interrupt-parent = <&aic>; ++ interrupts = , ++ , ++ , ++ ; ++ interrupt-names = "send-empty", "send-not-empty", ++ "recv-empty", "recv-not-empty"; ++ #mbox-cells = <0>; ++ power-domains = <&ps_ans>; ++ }; ++ + serial0: serial@20a0c0000 { + compatible = "apple,s5l-uart"; + reg = <0x2 0x0a0c0000 0x0 0x4000>; diff --git a/kernel/patches/devices/t7000/0014-arm64-dts-apple-t7000-Enable-internal-storage-on-the-iP.patch b/kernel/patches/devices/t7000/0014-arm64-dts-apple-t7000-Enable-internal-storage-on-the-iP.patch new file mode 100644 index 00000000..52960869 --- /dev/null +++ b/kernel/patches/devices/t7000/0014-arm64-dts-apple-t7000-Enable-internal-storage-on-the-iP.patch @@ -0,0 +1,79 @@ +From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 +From: =?UTF-8?q?Berkay=20K=C3=B6sebay?= +Date: Sat, 21 Nov 2026 11:51:14 +0100 +Subject: [PATCH] arm64: dts: apple: t7000: Enable internal storage on the iPod + touch 6 only + +The T7000 ANS nodes enable the ANS1 storage processor on every A8 board +from t7000-common.dtsi, and its mailbox from t7000.dtsi. Only the iPod +touch 6 (N102) has run it: leave both disabled in the SoC files and +enable them in t7000-n102.dts. The firmware region stays described for +every board; m1n1 fills it in from the ADT. + +Also keep ps_ans on, as the s5l8960x HACK does for the A7. The boot +loader started the ANS firmware and the ADT marks it no-shutdown: iOS +never powers it down. Without this the domain could go off before the +driver (a module here) binds, once the mailbox suspends or unused +domains are turned off. + +The ANS node matches the N102 ADT: ans at 0x208040000 (0x2000 of +registers), interrupts 36-39, and a pre-loaded firmware nub with +segment-ranges for m1n1 to read. + +Assisted-by: LLM +--- + arch/arm64/boot/dts/apple/t7000-common.dtsi | 1 - + arch/arm64/boot/dts/apple/t7000-n102.dts | 17 +++++++++++++++++ + arch/arm64/boot/dts/apple/t7000.dtsi | 1 + + 3 files changed, 18 insertions(+), 1 deletion(-) + +diff --git a/arch/arm64/boot/dts/apple/t7000-common.dtsi b/arch/arm64/boot/dts/apple/t7000-common.dtsi +index 11f251162..ca6507d2d 100644 +--- a/arch/arm64/boot/dts/apple/t7000-common.dtsi ++++ b/arch/arm64/boot/dts/apple/t7000-common.dtsi +@@ -45,5 +45,4 @@ + &ans { + memory-region = <&ans_firmware>; + memory-region-names = "firmware"; +- status = "okay"; + }; +diff --git a/arch/arm64/boot/dts/apple/t7000-n102.dts b/arch/arm64/boot/dts/apple/t7000-n102.dts +index 2c189bd5d..96192afa3 100644 +--- a/arch/arm64/boot/dts/apple/t7000-n102.dts ++++ b/arch/arm64/boot/dts/apple/t7000-n102.dts +@@ -47,6 +47,23 @@ + }; + }; + ++/* ++ * Internal NAND through the ANS1 storage processor. The ++ * boot loader started its firmware and iOS never shuts it down ++ * (no-shutdown in the ADT), so keep its power domain on, as on the A7. ++ */ ++&ans { ++ status = "okay"; ++}; ++ ++&ans_mbox { ++ status = "okay"; ++}; ++ ++&ps_ans { ++ apple,always-on; ++}; ++ + &framebuffer0 { + power-domains = <&ps_disp0 &ps_mipi_dsi>; + }; +diff --git a/arch/arm64/boot/dts/apple/t7000.dtsi b/arch/arm64/boot/dts/apple/t7000.dtsi +index 3a93dbe2e..22e529016 100644 +--- a/arch/arm64/boot/dts/apple/t7000.dtsi ++++ b/arch/arm64/boot/dts/apple/t7000.dtsi +@@ -173,6 +173,7 @@ + "recv-empty", "recv-not-empty"; + #mbox-cells = <0>; + power-domains = <&ps_ans>; ++ status = "disabled"; + }; + + serial0: serial@20a0c0000 { diff --git a/kernel/patches/drivers/block/0003-block-asp-Find-the-storage-endpoint-on-RTKit-10.2.patch b/kernel/patches/drivers/block/0003-block-asp-Find-the-storage-endpoint-on-RTKit-10.2.patch new file mode 100644 index 00000000..fe59c7a3 --- /dev/null +++ b/kernel/patches/drivers/block/0003-block-asp-Find-the-storage-endpoint-on-RTKit-10.2.patch @@ -0,0 +1,60 @@ +From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 +From: =?UTF-8?q?Berkay=20K=C3=B6sebay?= +Date: Sat, 21 Nov 2026 19:00:01 +0100 +Subject: [PATCH] block: asp: Find the storage endpoint on RTKit 10.2 + +The storage endpoint is the first app endpoint, which RTKit 11 and later +put at 0x20 and RTKit 10.2 at 6. The iPod touch 6 (A8, N102) runs the ANS +firmware of iOS 12.4, RTKit 10.2: take the endpoint from the protocol +version instead of always 0x20. RTKit 10.0 (endpoint 5) stays unsupported +and now says so. + +The endpoint numbers are those of Nick Chan's ans1 driver +(HoolockLinux/linux ans1 ed8528f, 3b237c39df79). + +Assisted-by: LLM +--- + drivers/block/asp.c | 18 +++++++++++++++--- + 1 file changed, 15 insertions(+), 3 deletions(-) + +diff --git a/drivers/block/asp.c b/drivers/block/asp.c +index ae1cc80..d4f7a0a 100644 +--- a/drivers/block/asp.c ++++ b/drivers/block/asp.c +@@ -30,6 +30,7 @@ + + #define DEVICE_NAME "asp" + #define ASP_RTKIT_ENDPOINT 0x20 ++#define ASP_RTKIT_ENDPOINT_V10_2 0x6 + #define DISK_NAME "asp%dn%d" + + #define AKF_V1_REMAP_AP_LO 0x8 +@@ -1174,14 +1175,25 @@ static void apple_asp_rtkit_recv(void *cookie, u8 endpoint, u64 message) + static void apple_asp_rtkit_epmap_done(void *cookie) + { + struct apple_asp *asp = cookie; ++ u16 major_ver, minor_ver; + int ret; + + /* + * The storage endpoint is the first app endpoint: 0x20 on RTKit 11 and +- * later (the iPad Air 2's ANS speaks 12.0). Older ANS firmware with +- * RTKit 10 is not supported; starting the endpoint fails there. ++ * later (the iPad Air 2's ANS speaks 12.0), 6 on RTKit 10.2 (iOS 12's ++ * ANS firmware on the iPod touch 6). RTKit 10.0 numbers it 5, which ++ * RTKit here does not start app endpoints at: not supported. + */ +- asp->ep = ASP_RTKIT_ENDPOINT; ++ apple_rtkit_protocol_version(asp->rtk, &major_ver, &minor_ver); ++ if (major_ver > 10) ++ asp->ep = ASP_RTKIT_ENDPOINT; ++ else if (major_ver == 10 && minor_ver >= 2) ++ asp->ep = ASP_RTKIT_ENDPOINT_V10_2; ++ else { ++ dev_err(asp->dev, "RTKit %u.%u is not supported", major_ver, ++ minor_ver); ++ return; ++ } + + dev_dbg(asp->dev, "ASP endpoint is %d", asp->ep); + diff --git a/kernel/patches/drivers/soc-apple/0028-soc-apple-rtkit-Answer-protocol-version-10-with-its-.patch b/kernel/patches/drivers/soc-apple/0028-soc-apple-rtkit-Answer-protocol-version-10-with-its-.patch new file mode 100644 index 00000000..e231f7d3 --- /dev/null +++ b/kernel/patches/drivers/soc-apple/0028-soc-apple-rtkit-Answer-protocol-version-10-with-its-.patch @@ -0,0 +1,123 @@ +From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 +From: Nick Chan +Date: Mon, 22 Dec 2025 21:25:12 +0800 +Subject: [PATCH] soc: apple: rtkit: Answer protocol version 10 with its minor + version + +RTKit protocol version 10 firmware (iOS 11 to 13) sends its minor version +in the HELLO field that later versions use for the minimum version, and +takes it back in the reply. Keep it, reply with it, and let drivers read +the version: the endpoints of some coprocessors move between 10.0 and +10.2. + +[Orchard: from HoolockLinux/linux ans1 ed8528f: the minor-version handshake +of 4b179996821a ("soc: apple: rtkit: Add protcol version 10 support") and +apple_rtkit_protocol_version() of ac12267a258a ("soc: apple: rtkit: Prepare +support for older protocol versions"), with 4b179996821a's author date. +Reduced to what the iPod touch 6's ANS (RTKit 10.2, iOS 12.4) needs, on top +of Hoolock's RTKit 10 support (hoolock/070-smc 0008-0009) instead of +replacing it: the app endpoints keep starting at 6 for every 10.x and the +10.0 power state handling is not carried. The message is new; the original +ones describe the parts not carried.] +Signed-off-by: Nick Chan +Assisted-by: LLM +--- + drivers/soc/apple/rtkit-internal.h | 1 + + drivers/soc/apple/rtkit.c | 28 +++++++++++++++++++++++++--- + include/linux/soc/apple/rtkit.h | 11 +++++++++++ + 3 files changed, 37 insertions(+), 3 deletions(-) + +diff --git a/drivers/soc/apple/rtkit-internal.h b/drivers/soc/apple/rtkit-internal.h +index 5f91dea..820588e 100644 +--- a/drivers/soc/apple/rtkit-internal.h ++++ b/drivers/soc/apple/rtkit-internal.h +@@ -36,6 +36,7 @@ struct apple_rtkit { + + int boot_result; + int version; ++ u16 minor_ver; + u8 app_ep_start; + + unsigned int iop_power_state; +diff --git a/drivers/soc/apple/rtkit.c b/drivers/soc/apple/rtkit.c +index 569206c..76e0d0b 100644 +--- a/drivers/soc/apple/rtkit.c ++++ b/drivers/soc/apple/rtkit.c +@@ -122,6 +122,16 @@ static void apple_rtkit_management_rx_hello(struct apple_rtkit *rtk, u64 msg) + int max_ver = FIELD_GET(APPLE_RTKIT_MGMT_HELLO_MAXVER, msg); + int want_ver = min(APPLE_RTKIT_MAX_SUPPORTED_VERSION, max_ver); + ++ /* ++ * Protocol version 10 firmware puts its minor version where later ++ * versions put the minimum version, and expects it back in the reply. ++ */ ++ rtk->minor_ver = 0; ++ if (max_ver == 10) { ++ rtk->minor_ver = min_ver; ++ min_ver = max_ver; ++ } ++ + dev_dbg(rtk->dev, "RTKit: Min ver %d, max ver %d\n", min_ver, max_ver); + + if (min_ver > APPLE_RTKIT_MAX_SUPPORTED_VERSION) { +@@ -136,8 +146,8 @@ static void apple_rtkit_management_rx_hello(struct apple_rtkit *rtk, u64 msg) + goto abort_boot; + } + +- dev_info(rtk->dev, "RTKit: Initializing (protocol version %d)\n", +- want_ver); ++ dev_info(rtk->dev, "RTKit: Initializing (protocol version %d.%d)\n", ++ want_ver, rtk->minor_ver); + rtk->version = want_ver; + + if (rtk->version < 11) +@@ -145,7 +155,10 @@ static void apple_rtkit_management_rx_hello(struct apple_rtkit *rtk, u64 msg) + else + rtk->app_ep_start = APPLE_RTKIT_APP_ENDPOINT_START_V11; + +- reply = FIELD_PREP(APPLE_RTKIT_MGMT_HELLO_MINVER, want_ver); ++ if (rtk->version == 10) ++ reply = FIELD_PREP(APPLE_RTKIT_MGMT_HELLO_MINVER, rtk->minor_ver); ++ else ++ reply = FIELD_PREP(APPLE_RTKIT_MGMT_HELLO_MINVER, want_ver); + reply |= FIELD_PREP(APPLE_RTKIT_MGMT_HELLO_MAXVER, want_ver); + apple_rtkit_management_send(rtk, APPLE_RTKIT_MGMT_HELLO_REPLY, reply); + +@@ -952,6 +965,15 @@ u8 apple_rtkit_app_ep_to_ep(struct apple_rtkit *rtk, u8 app_ep) + } + EXPORT_SYMBOL_GPL(apple_rtkit_app_ep_to_ep); + ++void apple_rtkit_protocol_version(struct apple_rtkit *rtk, u16 *major_ver, ++ u16 *minor_ver) ++{ ++ *major_ver = rtk->version; ++ if (minor_ver) ++ *minor_ver = rtk->minor_ver; ++} ++EXPORT_SYMBOL_GPL(apple_rtkit_protocol_version); ++ + int apple_rtkit_idle(struct apple_rtkit *rtk) + { + int ret; +diff --git a/include/linux/soc/apple/rtkit.h b/include/linux/soc/apple/rtkit.h +index ef33268..c415eb3 100644 +--- a/include/linux/soc/apple/rtkit.h ++++ b/include/linux/soc/apple/rtkit.h +@@ -201,6 +201,17 @@ int apple_rtkit_poll(struct apple_rtkit *rtk); + */ + u8 apple_rtkit_app_ep_to_ep(struct apple_rtkit *rtk, u8 app_ep); + ++/* ++ * Get the RTKit protocol version the firmware was booted with. The minor ++ * version is only reported by protocol version 10 firmware, 0 otherwise. ++ * ++ * @rtk: RTKit reference ++ * @major_ver: Pointer to store the major version ++ * @minor_ver: Optional, pointer to store the minor version ++ */ ++void apple_rtkit_protocol_version(struct apple_rtkit *rtk, u16 *major_ver, ++ u16 *minor_ver); ++ + /* Terminal only: bounded requests, no callback join or buffer release. */ + int apple_rtkit_shutdown_terminal(struct apple_rtkit *rtk); + From 8862c99a01fff52db5c163dcf250338ccb8efe86 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Berkay=20K=C3=B6sebay?= Date: Sat, 21 Nov 2026 23:07:38 +0100 Subject: [PATCH 7/8] kernel: asp: send the controller only the commands the driver needs ANS1's write unlock opens the whole controller, so only the driver keeps writes off the auxiliary namespaces (LLB, firmware, NVRAM, SysCfg with the factory calibration, ...). Check each command against the ones the driver uses (identify, the user area's read, write and flush, the write unlock and the auxiliary reads) before it is queued; anything else, auxiliary writes and format commands included, is refused. Shared with the iPad Air 2 (J81), which uses only those commands; not retested there. Assisted-by: LLM --- ...e-controller-only-the-user-area-s-wr.patch | 99 +++++++++++++++++++ 1 file changed, 99 insertions(+) create mode 100644 kernel/patches/drivers/block/0004-block-asp-Send-the-controller-only-the-user-area-s-wr.patch diff --git a/kernel/patches/drivers/block/0004-block-asp-Send-the-controller-only-the-user-area-s-wr.patch b/kernel/patches/drivers/block/0004-block-asp-Send-the-controller-only-the-user-area-s-wr.patch new file mode 100644 index 00000000..22dc6ec9 --- /dev/null +++ b/kernel/patches/drivers/block/0004-block-asp-Send-the-controller-only-the-user-area-s-wr.patch @@ -0,0 +1,99 @@ +From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 +From: =?UTF-8?q?Berkay=20K=C3=B6sebay?= +Date: Sat, 21 Nov 2026 19:00:02 +0100 +Subject: [PATCH] block: asp: Send the controller only the user area's writes + +The write unlock (0x19) opens the whole controller, not one namespace: +after it, only the driver keeps writes away from the auxiliary +namespaces (LLB, firmware, defect maps, control bits, effaceable storage, +NVRAM, SysCfg, panic log). SysCfg holds the factory calibration, which no +restore brings back. Today one check in asp_setup_cmd() does that, and +asp_setup_rw() still builds the auxiliary write opcodes 0x40-0x48 behind +it. + +Check every command against the commands the driver needs before it is +queued: identify, the user area's read, write and flush, the write +unlock and the auxiliary reads. Anything else, the auxiliary writes and +the format commands among them, is refused before it reaches the command +buffer or the mailbox. + +Assisted-by: LLM +--- + drivers/block/asp.c | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ + 1 file changed, 50 insertions(+) + +diff --git a/drivers/block/asp.c b/drivers/block/asp.c +index d4f7a0a..d1de321 100644 +--- a/drivers/block/asp.c ++++ b/drivers/block/asp.c +@@ -338,6 +338,39 @@ static bool asp_is_write(struct asp_cmd_hdr *cmd) + }; + } + ++/* ++ * The only commands that may reach the controller: identify, the user ++ * area's read, write and flush, the write unlock, and reads of the ++ * auxiliary namespaces. The write unlock opens the whole controller, not ++ * one namespace, so this is what keeps writes to the auxiliary namespaces ++ * (0x40-0x48: LLB, firmware, defect maps, control bits, effaceable ++ * storage, NVRAM, SysCfg, panic log) and the format commands from ever ++ * being sent, whatever the request path above builds. SysCfg holds the ++ * factory calibration, which no restore brings back. ++ */ ++static bool asp_cmd_allowed(const struct asp_cmd_hdr *cmd) ++{ ++ switch (cmd->op) { ++ case ASP_CMD_IDENTIFY: ++ case ASP_CMD_READ_USERAREA: ++ case ASP_CMD_WRITE_USERAREA: ++ case ASP_CMD_FLUSH: ++ case ASP_CMD_WRITE_UNLOCK: ++ case ASP_CMD_OP_READ_LLB: ++ case ASP_CMD_OP_READ_FW: ++ case ASP_CMD_OP_READ_UTILDM: ++ case ASP_CMD_OP_READ_DM: ++ case ASP_CMD_OP_READ_CTRLBITS: ++ case ASP_CMD_OP_READ_EFFACE: ++ case ASP_CMD_OP_READ_NVRAM: ++ case ASP_CMD_OP_READ_SYSCFG: ++ case ASP_CMD_OP_READ_PANICLOG: ++ return true; ++ default: ++ return false; ++ } ++} ++ + static enum req_op asp_req_op(struct asp_cmd_hdr *cmd) + { + return asp_is_write(cmd) ? REQ_OP_DRV_OUT : REQ_OP_DRV_IN; +@@ -516,6 +549,10 @@ static blk_status_t apple_asp_submit_cmd(struct apple_asp *asp, + { + int ret; + ++ /* every command comes through apple_asp_queue_rq(); checked again */ ++ if (WARN_ON_ONCE(!asp_cmd_allowed(cmd))) ++ return BLK_STS_NOTSUPP; ++ + memcpy(asp_q_cmd_hdr(asp, cmd), cmd, ASP_CMD_HDR_SIZE); + + dma_wmb(); +@@ -609,6 +646,19 @@ static blk_status_t apple_asp_queue_rq(struct blk_mq_hw_ctx *hctx, + if (ret) + return ret; + ++ if (!asp_cmd_allowed(&iod->cmd)) { ++ dev_err_ratelimited(asp->dev, "refusing command 0x%02x\n", ++ iod->cmd.op); ++ if (req_op(req) == REQ_OP_READ || req_op(req) == REQ_OP_WRITE) { ++ /* asp_setup_rw() mapped it; nothing was sent */ ++ dma_unmap_sg(asp->dev, iod->sg, iod->nents, ++ rq_dma_dir(req)); ++ mempool_free(iod->sg, asp->iod_mempool); ++ iod->sg = NULL; ++ } ++ return BLK_STS_NOTSUPP; ++ } ++ + if (iod->sg) + asp_setup_hw_sgl(asp, iod); + From 16df1670e936004afeae24f3db3770a6d38f7746 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Berkay=20K=C3=B6sebay?= Date: Sat, 21 Nov 2026 23:09:44 +0100 Subject: [PATCH 8/8] devices: n102: internal storage works; installable With ANS1 on the iPod touch 6, Orchard installs to its user area (asp0n1) next to a shrunk iOS: storage_read and storage_write are working. The board now meets every install requirement. The catalog gets the board's drivers (it had none yet). The ANS1 driver is not one the catalog looks for, so the matrix lists storage as working with no driver binding, as for the iPad Air 2. Assisted-by: LLM --- devices/n102/README.md | 14 +++++++++++++- devices/n102/device.toml | 8 ++++---- docs/compatibility.md | 7 +++++-- tests/test_catalog.py | 2 +- tools/catalog/drivers.json | 6 ++++++ 5 files changed, 29 insertions(+), 8 deletions(-) diff --git a/devices/n102/README.md b/devices/n102/README.md index 0ff5908b..def30bb1 100644 --- a/devices/n102/README.md +++ b/devices/n102/README.md @@ -14,12 +14,24 @@ no faults over a 514k-job soak. Experimental: dEQP has not run, so the chip has no `[gpu]` entry yet. Test with [`examples/ipod-gpu-drm`](../../examples/ipod-gpu-drm/README.md). +## Internal storage + +The flash sits behind ANS1, as on the iPad Air 2, with iOS 12.4's ANS +firmware (RTKit 10.2). `asp` shows the user area as `asp0n1` and the nine +auxiliary namespaces (LLB, firmware, NVRAM, SysCfg, ...) read-only. The +write unlock opens the whole controller, so the driver refuses every +command it does not need: only the user area is written. + +To keep iOS, shrink its APFS container first (resize-apfs) and install +Orchard into the free space of `asp0n1`. A DFU restore brings iOS back; it +keeps SysCfg, which Linux never writes. + ## Open work - GPU: per-job firmware overhead, parameter-manager pool growth inside a scene, and submitting a fragment job behind its geometry job (`a8_joint`) by default. -- Storage, touch, Wi-Fi and the battery gauge are not described yet. +- Touch, Wi-Fi and the battery gauge are not described yet. ## Unmerged code diff --git a/devices/n102/device.toml b/devices/n102/device.toml index eb8e23b7..dc7e0129 100644 --- a/devices/n102/device.toml +++ b/devices/n102/device.toml @@ -1,5 +1,5 @@ # iPod touch 6 (N102): identity from public sources (tools/catalog/generate's read_firmware); -# A8/T7000 like the iPhone 6. RAM-boots to a USB serial shell; GPU notes in README.md. +# A8/T7000 like the iPhone 6. Installs to its internal storage next to iOS; notes in README.md. schema = 1 kind = "hardware.device" @@ -14,7 +14,7 @@ chip = 0x7000 board = 0x10 [boot] -methods = ["checkm8-pongo"] # checkra1n then PongoOS and m1n1, RAM only. +methods = ["checkm8-pongo"] # checkra1n then PongoOS and m1n1, tethered. entries = ["checkra1n"] [kernel] @@ -39,8 +39,8 @@ accelerometer = "missing" gyroscope = "missing" light_sensor = "missing" touch_id = "missing" -storage_read = "missing" -storage_write = "missing" +storage_read = "working" # ANS1 (asp) on RTKit 10.2: the user area asp0n1 and nine read-only auxiliary namespaces. +storage_write = "working" # The user area only; asp refuses every other namespace's writes. An install next to iOS wrote asp0n1 only. wifi = "missing" bluetooth_input = "missing" bluetooth_audio = "missing" diff --git a/docs/compatibility.md b/docs/compatibility.md index 467bb4b3..c60c0862 100644 --- a/docs/compatibility.md +++ b/docs/compatibility.md @@ -65,7 +65,7 @@ Generated by `tools/catalog/generate matrix` from `devices/`, `chips/` and `meth | A7 | `n53` | iPhone 5s (Global) | iPhone6,2 | experimental | – | checkm8-pongo | yes | yes | 16H88 | | A8 | `j96` | iPad mini 4 (Wi-Fi) | iPad5,1 | experimental | – | checkm8-pongo | yes | yes | 19H422 | | A8 | `j97` | iPad mini 4 (Cellular) | iPad5,2 | experimental | – | checkm8-pongo | yes | yes | 19H422 | -| A8 | `n102` | iPod touch 6 | iPod7,1 | experimental | – | checkm8-pongo | yes | yes | 16H88 | +| A8 | `n102` | iPod touch 6 | iPod7,1 | experimental | yes | checkm8-pongo | yes | yes | 16H88 | | A8 | `n56` | iPhone 6+ | iPhone7,1 | experimental | – | checkm8-pongo | yes | yes | 16H88 | | A8 | `n61` | iPhone 6 | iPhone7,2 | experimental | – | checkm8-pongo | yes | yes | 16H22 | | A8X | `j81` | iPad Air 2 (Wi-Fi) | iPad5,3 | experimental | yes | checkm8-pongo | yes | yes | 19H422 | @@ -153,7 +153,7 @@ Boards Orchard builds for. States: working, untested, missing, none (no such har | `n53` | untested | missing | untested | missing | untested | missing | missing | missing | missing | missing | missing | missing | missing | missing | missing | missing | missing | missing | untested | untested | untested | untested | missing | missing | missing | missing | missing | | `j96` | working | missing | working | untested | working | missing | missing | missing | missing | missing | missing | working | missing | missing | none | working | missing | missing | untested | untested | working | working | missing | missing | missing | missing | missing | | `j97` | untested | missing | untested | untested | untested | missing | missing | missing | missing | missing | missing | untested | missing | missing | missing | untested | missing | missing | untested | untested | untested | untested | missing | missing | missing | missing | missing | -| `n102` | untested | missing | untested | missing | untested | missing | missing | missing | missing | missing | missing | missing | missing | missing | missing | missing | missing | missing | untested | untested | working | untested | missing | missing | missing | missing | missing | +| `n102` | untested | missing | untested | missing | untested | missing | missing | missing | missing | working | working | missing | missing | missing | missing | missing | missing | missing | untested | untested | working | untested | missing | missing | missing | missing | missing | | `n56` | untested | missing | untested | missing | untested | missing | missing | missing | missing | missing | missing | missing | missing | missing | missing | untested | missing | missing | untested | untested | untested | untested | missing | missing | missing | missing | missing | | `n61` | working | untested | untested | working | untested | missing | missing | missing | missing | missing | missing | missing | missing | missing | missing | untested | missing | missing | untested | untested | working | untested | missing | missing | missing | missing | missing | | `j81` | working | untested | working | working | working | missing | missing | missing | missing | working | working | working | untested | untested | none | working | missing | missing | untested | untested | working | working | missing | missing | missing | missing | missing | @@ -209,6 +209,7 @@ A yes means an enabled device tree node binds a driver in `7.3.0-rc1-orchard-app | `n53` | experimental | yes | – | yes | – | – | – | – | – | – | – | – | – | – | yes | yes | – | – | – | – | | `j96` | experimental | yes | yes | yes | – | – | – | – | – | yes | – | – | yes | – | yes | yes | – | – | – | – | | `j97` | experimental | yes | yes | yes | – | – | – | – | – | yes | – | – | yes | – | yes | yes | – | – | – | – | +| `n102` | experimental | yes | – | yes | – | – | – | – | – | – | – | – | – | – | yes | yes | – | – | – | – | | `n56` | experimental | yes | – | yes | – | – | – | – | – | – | – | – | yes | – | yes | yes | – | – | – | – | | `n61` | experimental | yes | yes | yes | – | – | – | – | – | – | – | – | yes | – | yes | yes | – | – | – | – | | `j81` | experimental | yes | yes | yes | – | – | – | – | – | yes | yes | yes | yes | – | yes | yes | – | – | – | – | @@ -250,6 +251,8 @@ A yes means an enabled device tree node binds a driver in `7.3.0-rc1-orchard-app | Board | Feature | | | --- | --- | --- | +| `n102` | Internal storage read | working, but no driver binds | +| `n102` | Internal storage write | working, but no driver binds | | `j81` | Internal storage read | working, but no driver binds | | `j81` | Internal storage write | working, but no driver binds | | `j71s` | Microphone | untested, but no driver binds | diff --git a/tests/test_catalog.py b/tests/test_catalog.py index 0150773e..8e9d7a49 100644 --- a/tests/test_catalog.py +++ b/tests/test_catalog.py @@ -246,7 +246,7 @@ def test_the_boards_with_every_requirement_can_install(self): from orchard import hardware boards = {path.parent.name for path in DESCRIPTORS if not hardware.install_blockers(path.parent.name)} self.assertEqual(boards, {'d10', 'd101', 'd11', 'd111', 'd20', 'd201', 'd21', 'd211', 'd22', 'd221', - 'j171', 'j172', 'j71b', 'j71t', 'j72b', 'j72t', 'j81'}) + 'j171', 'j172', 'j71b', 'j71t', 'j72b', 'j72t', 'j81', 'n102'}) def test_data_can_name_a_method_this_code_does_not_implement(self): from orchard import hardware diff --git a/tools/catalog/drivers.json b/tools/catalog/drivers.json index cc3d6380..268940e8 100644 --- a/tools/catalog/drivers.json +++ b/tools/catalog/drivers.json @@ -401,6 +401,12 @@ "usb_network", "usb_serial" ], + "n102": [ + "backlight", + "buttons", + "usb_network", + "usb_serial" + ], "n51": [ "backlight", "buttons",