diff --git a/devices/j71t/firmware.json b/devices/j71t/firmware.json index c03fb880..afa2a6d4 100644 --- a/devices/j71t/firmware.json +++ b/devices/j71t/firmware.json @@ -313,6 +313,16 @@ "coprocessor": "AOP_ext" } ] + }, + { + "destination": "lib/firmware/apple/gfx-s8003.bin", + "sha256": "33ae20cbbe2498e4f119ba4e9a55467e4e0192d4c86d5bf1903adce63ffa5dca", + "steps": [ + { + "step": "agx-g5p-ldr-from-kernelcache@1", + "input": "kernelcache" + } + ] } ] } diff --git a/docs/firmware-recipes.md b/docs/firmware-recipes.md index 6eb10f97..9a6180d1 100644 --- a/docs/firmware-recipes.md +++ b/docs/firmware-recipes.md @@ -144,6 +144,7 @@ The CLI used to do that; it isn't needed for recipes any more. | `smartio-from-kernelcache@1` | `input` (kernelcache) | SmartIO coprocessor firmware | | `vxd-firmware-from-kernelcache@1` | `input` (kernelcache) | the VXD (DEVA) video decoder firmware from AppleD5500 | | `agx-g9p-firmware-from-kernelcache@1` | `input` (kernelcache) | the A10 GPU firmware from AGXFirmwareKextG9PRTBuddy | +| `agx-g5p-ldr-from-kernelcache@1` | `input` (kernelcache) | the A9 GPU firmware (a META LDR) from AGXFirmwareKextG5P; size and digest are pinned | | `im4p-payload@1` | `input` | the payload of an IM4P container | | `aop-image@1` | `coprocessor` | one image (`AOP`, `AOP_ext`) of an AOP firmware container, the OSSerializeBinary payload of `Firmware/AOP/aopfw-*.im4p` | | `z2fw-from-mtprops@1` | `input`, `profile`, `packet`, `load_address`, `hbpp_version` | Z2 touch firmware with calibration records | @@ -238,3 +239,18 @@ The decompressed N61 kernel's SHA-256 is These fields are explicit recipe parameters, including zero-valued overrides. Physical qualification of firmware loading and input events remains separate from converting this shared firmware file. + + +## S8003 G5P META firmware + +`agx-g5p-ldr-from-kernelcache@1` reads the unique `__TEXT,__const` section +of `com.apple.AGXFirmwareKextG5P` in the user's own legacy prelinked +kernelcache. It rejects any size other than 119112 bytes or any digest other +than `33ae20cbbe2498e4f119ba4e9a55467e4e0192d4c86d5bf1903adce63ffa5dca`. +The existing J71t kernelcache input names the active device kernelcache and +the pinned restore-image fallback. The normal recipe installer writes the +verified output to `/lib/firmware/apple/gfx-s8003.bin`, named like the A10's +`gfx-t8010.bin`: the GPU driver asks for `apple/gfx-.bin`. The file is +Apple's LDR, byte for byte. No payload patch, calibration, firmware bytes or +captured job is stored in the source tree. Other C0 revisions and other +boards require separate digest qualification. diff --git a/kernel/patches/devices/s8000/0013-arm64-dts-apple-s8003-Describe-the-GPU.patch b/kernel/patches/devices/s8000/0013-arm64-dts-apple-s8003-Describe-the-GPU.patch new file mode 100644 index 00000000..c256be12 --- /dev/null +++ b/kernel/patches/devices/s8000/0013-arm64-dts-apple-s8003-Describe-the-GPU.patch @@ -0,0 +1,54 @@ +From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 +From: =?UTF-8?q?Berkay=20K=C3=B6sebay?= +Date: Tue, 10 Nov 2026 15:07:45 +0000 +Subject: [PATCH] arm64: dts: apple: s8003: Describe the GPU +MIME-Version: 1.0 +Content-Type: text/plain; charset=UTF-8 +Content-Transfer-Encoding: 8bit + +The A9's GPU: the Rogue core's registers, Fender, the gfx power domain +and the five interrupts, and the firmware the driver loads (the image +the firmware recipe extracts from the device's kernelcache). The node is +disabled: no driver takes it yet. The powervr driver's Apple firmware +path will, and the boards enable it then. + +The registers and interrupts are those of Berkay Kösebay's G5P driver +node (orchard #74, 557ef01b), which runs the GPU on the iPad 5 (J71t). +Reworked for the powervr driver: the gpu label, the firmware name, and +no board enables it before that driver binds. + +Assisted-by: LLM +--- + arch/arm64/boot/dts/apple/s8003.dtsi | 22 ++++++++++++++++++++++ + 1 file changed, 22 insertions(+) + +diff --git a/arch/arm64/boot/dts/apple/s8003.dtsi b/arch/arm64/boot/dts/apple/s8003.dtsi +index b515889..1ed521d 100644 +--- a/arch/arm64/boot/dts/apple/s8003.dtsi ++++ b/arch/arm64/boot/dts/apple/s8003.dtsi +@@ -336,3 +336,25 @@ + * the S8003 (APL1022) on 16nm. There are some minor differences + * such as timing in cpufreq state transistions. + */ ++ ++/* ++ * The GPU: a PowerVR Rogue core (GT7600) behind Apple's Fender block, with ++ * Apple's firmware on the core's own processor. Off until a driver takes it. ++ */ ++&{/soc} { ++ gpu: gpu@201000000 { ++ compatible = "apple,s8003-gpu"; ++ reg = <0x2 0x01000000 0x0 0x100000>, ++ <0x2 0x01e00000 0x0 0x100000>; ++ reg-names = "rgx", "fender"; ++ power-domains = <&ps_gfx>; ++ interrupt-parent = <&aic>; ++ interrupts = , ++ , ++ , ++ , ++ ; ++ firmware-name = "apple/gfx-s8003.bin"; ++ status = "disabled"; ++ }; ++}; diff --git a/kernel/patches/drivers/gpu/0015-drm-imagination-apple-Split-chip-specifics-from-the-.patch b/kernel/patches/drivers/gpu/0015-drm-imagination-apple-Split-chip-specifics-from-the-.patch new file mode 100644 index 00000000..baf20b0e --- /dev/null +++ b/kernel/patches/drivers/gpu/0015-drm-imagination-apple-Split-chip-specifics-from-the-.patch @@ -0,0 +1,1875 @@ +From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 +From: Henrik Klee +Date: Sat, 14 Nov 2026 21:35:03 +0800 +Subject: [PATCH] drm/imagination: apple: Split chip specifics from the Apple + firmware path + +The A9's GPU runs the same family of Apple firmware as the A10's: work +rings and completion stamps per engine, an event ring, Fender's power +state. What differs is the processor the firmware runs on (the A10's is +an RTKit coprocessor), how it is booted and rung, and the layout of the +memory it shares with the host, commands included. + +Put that behind struct pvr_apple_chip, chosen by the compatible, and +move the A10's side to pvr_apple_t8010.c: RTKit, the firmware window, +the init handshake, the doorbell, the job writers and the translation of +the geometry and fragment commands into them. pvr_apple.c keeps what the +chips share: submission and its ordering, fences and completion, events, +Fender power and performance states, runtime PM, sleep and recovery. It +reaches the firmware's memory through offsets the chip sets +(struct pvr_apple_layout) and takes the command stream definitions from +the chip. + +No change for the A10: the same register writes and firmware messages in +the same order. Two things move within host-only code: at removal and +on a failed probe the completion pollers are cancelled after the run bit +is cleared instead of just before it (as runtime and system suspend +always did), and a firmware start resets the sequence numbers before +clearing the firmware's memory instead of after. + +Assisted-by: LLM +--- + drivers/gpu/drm/imagination/Makefile | 3 +- + drivers/gpu/drm/imagination/pvr_apple.c | 658 ++++-------------- + drivers/gpu/drm/imagination/pvr_apple.h | 12 +- + drivers/gpu/drm/imagination/pvr_apple_chip.h | 230 ++++++ + .../gpu/drm/imagination/pvr_apple_layout.c | 9 - + .../gpu/drm/imagination/pvr_apple_layout.h | 1 - + drivers/gpu/drm/imagination/pvr_apple_t8010.c | 477 +++++++++++++ + drivers/gpu/drm/imagination/pvr_device.h | 5 + + drivers/gpu/drm/imagination/pvr_drv.c | 1 + + drivers/gpu/drm/imagination/pvr_job.c | 10 +- + 10 files changed, 879 insertions(+), 527 deletions(-) + create mode 100644 drivers/gpu/drm/imagination/pvr_apple_chip.h + create mode 100644 drivers/gpu/drm/imagination/pvr_apple_t8010.c + +diff --git a/drivers/gpu/drm/imagination/Makefile b/drivers/gpu/drm/imagination/Makefile +index de8d1b1..e991b67 100644 +--- a/drivers/gpu/drm/imagination/Makefile ++++ b/drivers/gpu/drm/imagination/Makefile +@@ -32,7 +32,8 @@ powervr-y := \ + powervr-$(CONFIG_DRM_POWERVR_APPLE) += \ + pvr_apple.o \ + pvr_apple_layout.o \ +- pvr_apple_queue.o ++ pvr_apple_queue.o \ ++ pvr_apple_t8010.o + + powervr-$(CONFIG_DEBUG_FS) += \ + pvr_debugfs.o +diff --git a/drivers/gpu/drm/imagination/pvr_apple.c b/drivers/gpu/drm/imagination/pvr_apple.c +index 6b980f7..2039928 100644 +--- a/drivers/gpu/drm/imagination/pvr_apple.c ++++ b/drivers/gpu/drm/imagination/pvr_apple.c +@@ -1,22 +1,18 @@ + // SPDX-License-Identifier: GPL-2.0-only OR MIT + /* +- * Apple A10 (T8010) support: Rogue fixed-function hardware driven by Apple's +- * GFX firmware instead of Imagination's. ++ * Apple's GPU firmware: Rogue fixed-function hardware driven by Apple's GFX ++ * firmware instead of Imagination's. + * +- * The firmware runs on "gfx-kf", an AppleA7IOP V2 coprocessor, and speaks RTKit +- * through the A7IOP mailbox. It powers the Rogue core through the "Fender" +- * block; the AP may only touch RGX registers once Fender reports a power state +- * above 0. +- * The firmware maps host memory itself: a 1 MiB window at IOP address 0 holds +- * the image and the init page, and a heap is mapped through memory descriptors +- * (see pvr_apple_layout.c). +- * +- * Messages, AGX endpoints: +- * 0x20 init: 0x81 << 48 | physical address of the init page +- * 0x21 doorbell: 0x83 << 48 makes the firmware scan its rings ++ * This file is what the chips share: job submission through the firmware's ++ * work rings, completion from its stamps and events, the order of geometry ++ * and fragment jobs, the GPU's power and performance state (Fender), runtime ++ * PM, sleep and recovery. The processor the firmware runs on, its memory ++ * layout and its commands belong to the chip (struct pvr_apple_chip, ++ * pvr_apple_chip.h): pvr_apple_t8010.c for the A10. + */ + + #include "pvr_apple.h" ++#include "pvr_apple_chip.h" + #include "pvr_apple_layout.h" + #include "pvr_context.h" + #include "pvr_device.h" +@@ -48,134 +44,12 @@ + #include + #include + #include +-#include +- +-#define APPLE_EP_AGX_INIT 0x20 +-#define APPLE_EP_AGX_DOORBELL 0x21 +-#define APPLE_MSG_INIT (0x81ull << 48) +-#define APPLE_MSG_KICK (0x83ull << 48) +- +-/* +- * Fender: power-state request/acknowledge, the firmware window, the run bit. +- * The power state is also the GPU performance state: 0 is off, 1 to N are +- * the GPU's clock and voltage steps, lowest to highest (the firmware's own +- * DVFS writes the same register, fw 0x16d40; ours leaves it alone). The +- * steps come from the bootloader as the GPU's OPP table, the state number +- * as opp-level; without one the GPU runs in FENDER_PERF_STATE_MAX, T8010's +- * fastest. +- */ +-#define FENDER_POWER_STATE 0x1000 +-#define FENDER_POWER_STATE_MASK 0x1f +-#define FENDER_POWER_REQUEST 0x10 /* | target state */ +-#define FENDER_PERF_STATE_MAX 6 +-#define FENDER_WINDOW 0x200c +-#define FENDER_RUN 0x2048 +-#define FENDER_RUN_ENABLE BIT(0) +- +-/* A7IOP: the IOP's copy of the firmware window */ +-#define IOP_WINDOW 0x0008 +-#define IOP_WINDOW_ENABLE 0x0038 +- +-/* Heap: U+0x14 becomes 1 once the firmware accepted the init data */ +-#define HEAP_U_READY (0x5000 + 0x14) +- +-struct pvr_apple { +- struct pvr_device *pvr_dev; +- void __iomem *fender; +- void __iomem *iop; +- struct apple_rtkit *rtk; +- +- void *win; +- dma_addr_t win_dma; +- size_t win_next; /* RTKit buffers are carved after the image */ +- const void *fw_image; /* the firmware image, for a restart after a reset */ +- size_t fw_size; +- void *heap; +- dma_addr_t heap_dma; +- bool booted; +- bool fw_ready; /* init data handed over once: runtime PM restarts it */ +- bool rtk_idle; /* RTKit's mailbox stopped while the GPU is off */ +- +- struct mutex submit_lock; /* job slots, rings, producer index, GPU VM */ +- u32 last_seq; /* last submitted compute job */ +- u32 ta_seq; /* last submitted geometry job */ +- u32 frag_seq; /* last submitted fragment job */ +- u32 frag_events; /* fragment-done events the firmware posted */ +- /* +- * A geometry job reached the firmware since the last fragment job (or +- * its start): only then does the next Start3D find a free list +- */ +- bool geom_pending; +- spinlock_t event_lock; /* event ring consumer, frag_events */ +- dma_addr_t vm_root; /* page-table root GPU context 0 runs on */ +- +- /* +- * Completion: fences of submitted jobs, one timeline per engine, +- * signalled from the engine's stamp. +- */ +- u64 fence_context; /* first of APPLE_ENGINES */ +- spinlock_t fence_lock; +- struct list_head pending[3]; /* struct pvr_apple_fence, by seq */ +- struct delayed_work poll_work; +- struct hrtimer poll_timer; /* rearms poll_work while jobs are pending */ +- /* +- * The last fragment job pushed: the next geometry job waits for it, as +- * the firmware keeps one parameter-manager scene at a time. +- */ +- struct dma_fence *last_frag; +- /* The last geometry job pushed: the next fragment job consumes its scene */ +- struct dma_fence *last_geom; +- +- /* +- * Performance state (Fender power state) and its scaling: perf_lock +- * serializes requests; busy_since (0 while idle) and busy_ns, under +- * fence_lock, give devfreq the time jobs were in flight. +- */ +- struct mutex perf_lock; +- u32 perf_state; +- u32 perf_state_max; +- struct devfreq *devfreq; +- ktime_t busy_since; +- u64 busy_ns; +- ktime_t stat_since; +- +- /* Backs GPU VA 0 in userspace VMs: the end-of-render writes there */ +- struct pvr_gem_object *zero_obj; +- +- /* Parameter-manager pages every free list hands out (pvr_apple_pm_pages()) */ +- struct mutex pm_lock; +- struct pvr_gem_object *pm_pages; +- +- /* +- * GPU pages of the kernel VM context: data (stream, terminate PDS, +- * context state, flush scratch; also mapped into every userspace VM), +- * PDS and USC code at the code bases. +- */ +- struct pvr_apple_gpu_page { +- struct pvr_gem_object *obj; +- u8 *map; +- u64 device_addr; +- size_t size; +- } data, pds, usc; +-}; +- +-enum pvr_apple_engine { +- APPLE_ENGINE_COMPUTE, +- APPLE_ENGINE_GEOM, +- APPLE_ENGINE_FRAG, +- APPLE_ENGINES +-}; + + struct pvr_apple_fence { + struct dma_fence base; + struct list_head node; + }; + +-/* Driver-owned GPU addresses: code at powervr's PDS and USC heap bases */ +-#define APPLE_GPU_PDS PVR_APPLE_PDSCODEDATA_HEAP_BASE +-#define APPLE_GPU_USC PVR_APPLE_USCCODE_HEAP_BASE +-#define APPLE_JOB_SLOTS 16 /* matches the layout's per-job slots */ +- + /* + * How often the stamps are checked while jobs are pending. A jiffy-based + * delay (1-2 ms per check) cost a render about 4 ms: a geometry and a +@@ -188,108 +62,18 @@ MODULE_PARM_DESC(apple_poll_us, "Apple A10: microseconds between job completion + /* Idle time before runtime PM powers the GPU off; tunable in sysfs. */ + #define PVR_APPLE_AUTOSUSPEND_MS 1000 + +-/* +- * The firmware's debug flags: 1 SKU trace events, 2 cancel-path events, into +- * a debug ring of 511 records that nothing drains. Once it is full the +- * firmware stops the GPU's work and asks the host to drain it (messages of +- * type 2 on the init endpoint, iOS 10's receivedMessageFromAKF()): only for +- * short experiments. Read at each firmware start, so a change applies from +- * the next reset; a hang report then ends with the ring's last records. +- */ +-static unsigned int pvr_apple_fw_debug; +-module_param_named(apple_fw_debug, pvr_apple_fw_debug, uint, 0644); +-MODULE_PARM_DESC(apple_fw_debug, "Apple A10: GFX firmware debug flags (1 trace events, 2 cancel events; default 0)"); +- + static unsigned int pvr_apple_perf_state; + module_param_named(apple_perf_state, pvr_apple_perf_state, uint, 0444); + MODULE_PARM_DESC(apple_perf_state, + "Apple A10: fixed GPU performance state, 1 (slowest) up; 0 (default) scales it with the load (devfreq), or runs the fastest without an OPP table"); + +-/* The heap is coherent memory shared with the firmware, not MMIO. */ +-static u32 heap_read32(struct pvr_apple *apple, u32 offset) +-{ +- return le32_to_cpu(READ_ONCE(*(__le32 *)(apple->heap + offset))); +-} +- + static struct pvr_apple *to_apple(struct pvr_device *pvr_dev) + { + return pvr_dev->apple; + } + +-static struct device *apple_dev(struct pvr_apple *apple) +-{ +- return from_pvr_device(apple->pvr_dev)->dev; +-} +- +-static int pvr_apple_shmem_setup(void *cookie, struct apple_rtkit_shmem *bfr) +-{ +- struct pvr_apple *apple = cookie; +- size_t size = ALIGN(bfr->size, SZ_4K); +- +- if (bfr->iova) { +- /* Firmware-chosen address: must lie inside the window */ +- if (bfr->iova + bfr->size > PVR_APPLE_WINDOW_SIZE) +- return -EINVAL; +- bfr->buffer = apple->win + bfr->iova; +- bfr->is_mapped = true; +- return 0; +- } +- if (apple->win_next + size > pvr_apple_layout_init_offset()) +- return -ENOMEM; +- bfr->iova = apple->win_next; +- bfr->buffer = apple->win + apple->win_next; +- apple->win_next += size; +- return 0; +-} +- +-static void pvr_apple_shmem_destroy(void *cookie, struct apple_rtkit_shmem *bfr) +-{ +- /* Carved from the window, which lives as long as the device */ +-} +- +-static void pvr_apple_recv_message(void *cookie, u8 endpoint, u64 message) +-{ +- struct pvr_apple *apple = cookie; +- +- dev_dbg(apple_dev(apple), "firmware ep %#x: %#016llx\n", endpoint, message); +- /* none is known to mean completion, but a check costs little */ +- mod_delayed_work(system_highpri_wq, &apple->poll_work, 0); +-} +- +-static void pvr_apple_crashed(void *cookie, const void *crashlog, size_t size) +-{ +- struct pvr_apple *apple = cookie; +- +- dev_err(apple_dev(apple), "GFX firmware crashed (%zu byte crashlog)\n", size); +- if (crashlog) +- print_hex_dump(KERN_ERR, "crashlog: ", DUMP_PREFIX_OFFSET, 16, 4, crashlog, size, +- false); +-} +- +-static const struct apple_rtkit_ops pvr_apple_rtkit_ops = { +- .crashed = pvr_apple_crashed, +- .recv_message = pvr_apple_recv_message, +- .shmem_setup = pvr_apple_shmem_setup, +- .shmem_destroy = pvr_apple_shmem_destroy, +-}; +- +-/* The window is programmed in two copies: Fender (4-byte stride) and IOP (8). */ +-static void pvr_apple_set_window(struct pvr_apple *apple) +-{ +- const u32 fields[] = { +- lower_32_bits(apple->win_dma), upper_32_bits(apple->win_dma), +- 0, 0, PVR_APPLE_WINDOW_SIZE, 0, +- }; +- unsigned int i; +- +- for (i = 0; i < ARRAY_SIZE(fields); i++) { +- writel(fields[i], apple->fender + FENDER_WINDOW + 4 * i); +- writel(fields[i], apple->iop + IOP_WINDOW + 8 * i); +- } +- writel(1, apple->iop + IOP_WINDOW_ENABLE); +-} +- +-static int pvr_apple_request_power_state(struct pvr_apple *apple, u32 state) ++/* Ask Fender for power state @state and wait until it reports it */ ++int pvr_apple_request_power_state(struct pvr_apple *apple, u32 state) + { + u32 val; + int err; +@@ -304,6 +88,39 @@ static int pvr_apple_request_power_state(struct pvr_apple *apple, u32 state) + return err; + } + ++/** ++ * pvr_apple_fender_power_on() - Power the Rogue core up ++ * @apple: Apple firmware state. ++ * ++ * For a chip's boot(), once its firmware runs far enough to serve Fender. ++ * ++ * Return: 0 or a negative error. ++ */ ++int pvr_apple_fender_power_on(struct pvr_apple *apple) ++{ ++ u32 state; ++ int err; ++ ++ /* A core left on by a previous boot is reset through state 0 first. */ ++ state = readl(apple->fender + FENDER_POWER_STATE) & FENDER_POWER_STATE_MASK; ++ if (state != 0) { ++ err = pvr_apple_request_power_state(apple, 0); ++ if (err) ++ return err; ++ } ++ /* ++ * Powering up and changing the clock are the same request: start in ++ * the fixed state if one is set, else the fastest; devfreq takes it ++ * from there. ++ */ ++ mutex_lock(&apple->perf_lock); ++ /* never state 0 here: that is off, and the firmware is about to start */ ++ err = pvr_apple_request_power_state(apple, clamp(pvr_apple_perf_state ?: apple->perf_state_max, ++ 1u, apple->perf_state_max ?: 1u)); ++ mutex_unlock(&apple->perf_lock); ++ return err; ++} ++ + static void pvr_apple_poll_work(struct work_struct *work); + static enum hrtimer_restart pvr_apple_poll_timer(struct hrtimer *timer); + static u32 pvr_apple_frag_retired(struct pvr_apple *apple); +@@ -383,7 +200,7 @@ static void pvr_apple_opp_init(struct pvr_apple *apple) + unsigned long freq = ULONG_MAX; + struct dev_pm_opp *opp; + +- apple->perf_state_max = FENDER_PERF_STATE_MAX; ++ apple->perf_state_max = apple->chip->perf_state_max; + if (devm_pm_opp_of_add_table(dev) || dev_pm_opp_get_opp_count(dev) <= 0) + return; + opp = dev_pm_opp_find_freq_floor(dev, &freq); +@@ -431,21 +248,50 @@ static void pvr_apple_stop_firmware(void *data) + + if (!apple->booted) + return; +- pvr_apple_request_power_state(apple, 0); +- apple_rtkit_shutdown(apple->rtk); ++ apple->chip->stop(apple, false); + cancel_delayed_work_sync(&apple->poll_work); + hrtimer_cancel(&apple->poll_timer); +- writel(readl(apple->fender + FENDER_RUN) & ~FENDER_RUN_ENABLE, apple->fender + FENDER_RUN); + apple->booted = false; + } + ++/** ++ * pvr_apple_fw_load() - Load the chip's firmware file ++ * @apple: Apple firmware state. ++ * @max_size: Largest image the chip takes. ++ * ++ * For a chip's init(). The image stays in &pvr_apple.fw_image for as long as ++ * the device, for every later start of the firmware. ++ * ++ * Return: 0 or a negative error. ++ */ ++int pvr_apple_fw_load(struct pvr_apple *apple, size_t max_size) ++{ ++ struct device *dev = apple_dev(apple); ++ const char *name = apple->chip->fw_name; ++ const struct firmware *fw; ++ int err; ++ ++ of_property_read_string(dev->of_node, "firmware-name", &name); ++ err = request_firmware(&fw, name, dev); ++ if (err) ++ return dev_err_probe(dev, err, "cannot load %s\n", name); ++ if (fw->size > max_size) { ++ release_firmware(fw); ++ return dev_err_probe(dev, -EINVAL, "%s too large\n", name); ++ } ++ apple->fw_image = devm_kmemdup(dev, fw->data, fw->size, GFP_KERNEL); ++ apple->fw_size = fw->size; ++ release_firmware(fw); ++ if (!apple->fw_image) ++ return -ENOMEM; ++ return 0; ++} ++ + static int pvr_apple_power_init(struct pvr_device *pvr_dev) + { + struct device *dev = from_pvr_device(pvr_dev)->dev; + struct platform_device *pdev = to_platform_device(dev); +- const struct firmware *fw; + struct pvr_apple *apple; +- const char *name = "apple/gfx-t8010.bin"; + unsigned int i; + int err; + +@@ -453,6 +299,7 @@ static int pvr_apple_power_init(struct pvr_device *pvr_dev) + if (!apple) + return -ENOMEM; + apple->pvr_dev = pvr_dev; ++ apple->chip = pvr_dev->device_data->apple_chip; + pvr_dev->apple = apple; + err = devm_mutex_init(dev, &apple->submit_lock); + if (!err) +@@ -472,42 +319,11 @@ static int pvr_apple_power_init(struct pvr_device *pvr_dev) + apple->fender = devm_platform_ioremap_resource_byname(pdev, "fender"); + if (IS_ERR(apple->fender)) + return PTR_ERR(apple->fender); +- apple->iop = devm_platform_ioremap_resource_byname(pdev, "iop"); +- if (IS_ERR(apple->iop)) +- return PTR_ERR(apple->iop); + +- /* The firmware addresses host memory physically through its own MMU. */ +- err = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(40)); ++ err = apple->chip->init(apple); + if (err) + return err; +- apple->win = dmam_alloc_coherent(dev, PVR_APPLE_WINDOW_SIZE, &apple->win_dma, GFP_KERNEL); +- apple->heap = dmam_alloc_coherent(dev, PVR_APPLE_HEAP_SIZE, &apple->heap_dma, GFP_KERNEL); +- if (!apple->win || !apple->heap) +- return -ENOMEM; +- if (!IS_ALIGNED(apple->win_dma, SZ_16K)) +- return dev_err_probe(dev, -EINVAL, "firmware window %pad not 16 KiB aligned\n", +- &apple->win_dma); +- apple->win_next = PVR_APPLE_FW_RUNTIME; + +- of_property_read_string(dev->of_node, "firmware-name", &name); +- err = request_firmware(&fw, name, dev); +- if (err) +- return dev_err_probe(dev, err, "cannot load %s\n", name); +- if (fw->size > PVR_APPLE_FW_RUNTIME) { +- release_firmware(fw); +- return dev_err_probe(dev, -EINVAL, "%s too large\n", name); +- } +- memcpy(apple->win, fw->data, fw->size); +- /* The firmware writes into its image while it runs: keep it for a reset */ +- apple->fw_image = devm_kmemdup(dev, fw->data, fw->size, GFP_KERNEL); +- apple->fw_size = fw->size; +- release_firmware(fw); +- if (!apple->fw_image) +- return -ENOMEM; +- +- apple->rtk = devm_apple_rtkit_init(dev, apple, NULL, 0, &pvr_apple_rtkit_ops); +- if (IS_ERR(apple->rtk)) +- return dev_err_probe(dev, PTR_ERR(apple->rtk), "RTKit init failed\n"); + /* + * Reading the GPU ID powers the GPU on, which boots the firmware: a probe + * failing after that (the support check without exp_hw_support) left it +@@ -530,64 +346,15 @@ static int pvr_apple_power_init(struct pvr_device *pvr_dev) + return 0; + } + +-static int pvr_apple_boot(struct pvr_device *pvr_dev) ++static int pvr_apple_boot(struct pvr_apple *apple) + { +- struct pvr_apple *apple = to_apple(pvr_dev); +- struct device *dev = apple_dev(apple); +- u32 state; + int err; + + if (apple->booted) + return 0; +- +- /* The mailbox first: the firmware talks as soon as it runs */ +- if (apple->rtk_idle) { +- err = apple_rtkit_reinit(apple->rtk); +- if (err) +- return err; +- apple->rtk_idle = false; +- } +- pvr_apple_set_window(apple); +- writel(readl(apple->fender + FENDER_RUN) | FENDER_RUN_ENABLE, apple->fender + FENDER_RUN); +- +- err = apple_rtkit_boot(apple->rtk); +- if (err) { +- dev_err(dev, "GFX firmware boot failed: %d\n", err); +- goto err_stop; +- } +- err = apple_rtkit_start_ep(apple->rtk, APPLE_EP_AGX_INIT); ++ err = apple->chip->boot(apple); + if (!err) +- err = apple_rtkit_start_ep(apple->rtk, APPLE_EP_AGX_DOORBELL); +- if (err) +- goto err_shutdown; +- +- /* A core left on by a previous boot is reset through state 0 first. */ +- state = readl(apple->fender + FENDER_POWER_STATE) & FENDER_POWER_STATE_MASK; +- if (state != 0) { +- err = pvr_apple_request_power_state(apple, 0); +- if (err) +- goto err_shutdown; +- } +- /* +- * Powering up and changing the clock are the same request: start in +- * the fixed state if one is set, else the fastest; devfreq takes it +- * from there. +- */ +- mutex_lock(&apple->perf_lock); +- /* never state 0 here: that is off, and the firmware is about to start */ +- err = pvr_apple_request_power_state(apple, clamp(pvr_apple_perf_state ?: apple->perf_state_max, +- 1u, apple->perf_state_max ?: 1u)); +- mutex_unlock(&apple->perf_lock); +- if (err) +- goto err_shutdown; +- +- apple->booted = true; +- return 0; +- +-err_shutdown: +- apple_rtkit_shutdown(apple->rtk); +-err_stop: +- writel(readl(apple->fender + FENDER_RUN) & ~FENDER_RUN_ENABLE, apple->fender + FENDER_RUN); ++ apple->booted = true; + return err; + } + +@@ -609,7 +376,7 @@ static int pvr_apple_power_on(struct pvr_device *pvr_dev) + int err; + + if (!apple->fw_ready) +- return pvr_apple_boot(pvr_dev); ++ return pvr_apple_boot(apple); + + mutex_lock(&apple->submit_lock); + err = apple->booted ? 0 : pvr_apple_fw_restart(apple); +@@ -636,8 +403,7 @@ static int pvr_apple_power_off(struct pvr_device *pvr_dev) + err = pvr_apple_wait_idle(apple, 1000000); + if (!err) { + pvr_apple_fw_stop(apple); +- /* its mailbox would keep the GPU's power domain on */ +- apple->rtk_idle = !apple_rtkit_mbox_stop(apple->rtk); ++ apple->chip->idle(apple); + } + mutex_unlock(&apple->submit_lock); + if (err) { +@@ -769,10 +535,10 @@ static void pvr_apple_switch_vm(struct pvr_apple *apple, struct pvr_vm_context * + lockdep_assert_held(&apple->submit_lock); + if (root == apple->vm_root) + return; +- *(__le64 *)(apple->heap + pvr_apple_layout_mmu_root_offset(0)) = cpu_to_le64(root); ++ *(__le64 *)(apple->heap + apple->layout.mmu_root) = cpu_to_le64(root); + pvr_cr_write32(pvr_dev, ROGUE_CR_MMU_CBASE_MAPPING_CONTEXT, 0); + pvr_cr_write32(pvr_dev, ROGUE_CR_MMU_CBASE_MAPPING, (u32)(root >> 12)); +- gen = (__le32 *)(apple->heap + pvr_apple_layout_mmu_gen_offset(0)); ++ gen = (__le32 *)(apple->heap + apple->layout.mmu_gen); + *gen = cpu_to_le32(le32_to_cpu(*gen) + 0x400000); /* full invalidation */ + wmb(); + apple->vm_root = root; +@@ -780,7 +546,7 @@ static void pvr_apple_switch_vm(struct pvr_apple *apple, struct pvr_vm_context * + + static u32 pvr_apple_completed_seq(struct pvr_apple *apple) + { +- return heap_read32(apple, pvr_apple_layout_stamp_offset()); ++ return heap_read32(apple, apple->layout.stamp[APPLE_ENGINE_COMPUTE]); + } + + static int pvr_apple_wait(struct pvr_apple *apple, u32 seq, unsigned int timeout_us) +@@ -793,14 +559,7 @@ static int pvr_apple_wait(struct pvr_apple *apple, u32 seq, unsigned int timeout + + static u32 pvr_apple_engine_done(struct pvr_apple *apple, enum pvr_apple_engine engine) + { +- switch (engine) { +- case APPLE_ENGINE_GEOM: +- return heap_read32(apple, pvr_apple_layout_ta_stamp_offset()); +- case APPLE_ENGINE_FRAG: +- return heap_read32(apple, pvr_apple_layout_3d_stamp_offset()); +- default: +- return pvr_apple_completed_seq(apple); +- } ++ return heap_read32(apple, apple->layout.stamp[engine]); + } + + static u32 pvr_apple_engine_last(struct pvr_apple *apple, enum pvr_apple_engine engine) +@@ -855,7 +614,7 @@ static int pvr_apple_submit_compute(struct pvr_apple *apple, struct pvr_vm_conte + int err; + + mutex_lock(&apple->submit_lock); +- err = pvr_apple_wait(apple, apple->last_seq - (APPLE_JOB_SLOTS - 1), 5000000); ++ err = pvr_apple_wait(apple, apple->last_seq - (apple->chip->compute_slots - 1), 5000000); + if (!err && pvr_vm_get_page_table_root_addr(vm_ctx) != apple->vm_root) + err = pvr_apple_wait_idle(apple, 5000000); + if (err) { +@@ -865,13 +624,13 @@ static int pvr_apple_submit_compute(struct pvr_apple *apple, struct pvr_vm_conte + } + pvr_apple_switch_vm(apple, vm_ctx); + seq = ++apple->last_seq; +- pvr_apple_layout_write_compute_job(apple->heap, seq, stream, ctx_state); ++ apple->chip->write_compute(apple, seq, stream, ctx_state); + wmb(); +- WRITE_ONCE(*(__le32 *)(apple->heap + pvr_apple_layout_compute_producer()), +- cpu_to_le32(pvr_apple_layout_ring_index(seq))); ++ /* ring indices wrap at the ring size, sequence numbers do not */ ++ WRITE_ONCE(*(__le32 *)(apple->heap + apple->layout.producer[APPLE_ENGINE_COMPUTE]), ++ cpu_to_le32(seq % APPLE_RING_ENTRIES)); + wmb(); +- err = apple_rtkit_send_message(apple->rtk, APPLE_EP_AGX_DOORBELL, APPLE_MSG_KICK, NULL, +- false); ++ err = apple->chip->kick(apple); + if (err) + dev_err(apple_dev(apple), "doorbell for job %u failed: %d\n", seq, err); + else +@@ -906,10 +665,11 @@ static void pvr_apple_ack_events(struct pvr_apple *apple) + u32 cons, prod; + + spin_lock_irqsave(&apple->event_lock, flags); +- cons = heap_read32(apple, pvr_apple_layout_event_consumer()); +- prod = heap_read32(apple, pvr_apple_layout_event_producer()); +- for (; cons != prod; cons = (cons + 1) % 256) { +- const u32 *e = apple->heap + pvr_apple_layout_event_entry(cons); ++ cons = heap_read32(apple, apple->layout.event_consumer); ++ prod = heap_read32(apple, apple->layout.event_producer); ++ for (; cons != prod; cons = (cons + 1) % APPLE_RING_ENTRIES) { ++ const u32 *e = apple->heap + apple->layout.events + ++ apple->layout.event_size * (cons % APPLE_RING_ENTRIES); + + /* type 1, 4: a fragment job finished (1, 2: a geometry job) */ + if (e[0] == 1 && e[1] == 4) +@@ -925,7 +685,7 @@ static void pvr_apple_ack_events(struct pvr_apple *apple) + "firmware event %u: %08x %08x %08x %08x %08x %08x %08x %08x %08x\n", + cons, e[0], e[1], e[2], e[3], e[4], e[5], e[6], e[7], e[8]); + } +- WRITE_ONCE(*(__le32 *)(apple->heap + pvr_apple_layout_event_consumer()), ++ WRITE_ONCE(*(__le32 *)(apple->heap + apple->layout.event_consumer), + cpu_to_le32(prod)); + spin_unlock_irqrestore(&apple->event_lock, flags); + } +@@ -942,7 +702,7 @@ static u32 pvr_apple_frag_retired_seq(struct pvr_apple *apple) + u32 seq = READ_ONCE(apple->frag_seq), mark; + + rmb(); /* after the events that announced it */ +- mark = heap_read32(apple, pvr_apple_layout_3d_retired_offset()); ++ mark = heap_read32(apple, apple->layout.frag_retired); + return seq - (u8)(seq - mark); + } + +@@ -1048,19 +808,11 @@ void pvr_apple_report_hang(struct pvr_device *pvr_dev, struct dma_fence *fence) + pvr_apple_engine_done(apple, APPLE_ENGINE_GEOM), + pvr_apple_engine_done(apple, APPLE_ENGINE_FRAG), pvr_apple_completed_seq(apple), + apple->ta_seq, apple->frag_seq, apple->last_seq, pvr_apple_frag_retired(apple), +- heap_read32(apple, pvr_apple_layout_ta_producer()), +- heap_read32(apple, pvr_apple_layout_3d_producer()), +- heap_read32(apple, pvr_apple_layout_event_producer()), +- heap_read32(apple, pvr_apple_layout_event_consumer())); +- if (heap_read32(apple, pvr_apple_layout_debug_flags_offset())) { +- u32 next = heap_read32(apple, pvr_apple_layout_sku_index()); +- unsigned int i; +- +- dev_err(apple_dev(apple), "hang: trace ring index %u\n", next); +- for (i = next >= 24 ? next - 24 : 0; i < next; i++) +- dev_err(apple_dev(apple), "hang: trace %u: %*ph\n", i, 36, +- apple->heap + pvr_apple_layout_sku_record(i)); +- } ++ heap_read32(apple, apple->layout.producer[APPLE_ENGINE_GEOM]), ++ heap_read32(apple, apple->layout.producer[APPLE_ENGINE_FRAG]), ++ heap_read32(apple, apple->layout.event_producer), ++ heap_read32(apple, apple->layout.event_consumer)); ++ apple->chip->report_hang(apple); + } + + static unsigned int pvr_apple_cancel_fences(struct pvr_apple *apple, int error) +@@ -1197,7 +949,7 @@ void pvr_apple_quiesce(struct pvr_device *pvr_dev) + void pvr_apple_mmu_flush(struct pvr_device *pvr_dev) + { + struct pvr_apple *apple = to_apple(pvr_dev); +- __le32 *gen = (__le32 *)(apple->heap + pvr_apple_layout_mmu_gen_offset(0)); ++ __le32 *gen = (__le32 *)(apple->heap + apple->layout.mmu_gen); + + /* + * May run in atomic context, so no lock: only the host writes the +@@ -1252,14 +1004,14 @@ void pvr_apple_vm_fini(struct pvr_device *pvr_dev, struct pvr_vm_context *vm_ctx + /* Fragment jobs the firmware has retired: work ring 1's first index word */ + static u32 pvr_apple_frag_retired(struct pvr_apple *apple) + { +- return heap_read32(apple, pvr_apple_layout_ring_indices_offset(1)); ++ return heap_read32(apple, apple->layout.frag_ring); + } + + static int pvr_apple_queue_render(struct pvr_apple *apple, struct pvr_vm_context *vm_ctx, + enum pvr_apple_engine engine, const void *job, + struct pvr_free_list *fl) + { +- u32 seq, producer; ++ u32 seq; + int err; + + mutex_lock(&apple->submit_lock); +@@ -1327,19 +1079,16 @@ static int pvr_apple_queue_render(struct pvr_apple *apple, struct pvr_vm_context + apple->geom_pending = engine == APPLE_ENGINE_GEOM; + if (engine == APPLE_ENGINE_GEOM) { + seq = ++apple->ta_seq; +- pvr_apple_layout_write_ta_job(apple->heap, seq, job); +- producer = pvr_apple_layout_ta_producer(); ++ apple->chip->write_geom(apple, seq, job); + } else { + seq = ++apple->frag_seq; +- pvr_apple_layout_write_3d_job(apple->heap, seq, job); +- producer = pvr_apple_layout_3d_producer(); ++ apple->chip->write_frag(apple, seq, job); + } + wmb(); +- WRITE_ONCE(*(__le32 *)(apple->heap + producer), +- cpu_to_le32(pvr_apple_layout_ring_index(seq))); ++ WRITE_ONCE(*(__le32 *)(apple->heap + apple->layout.producer[engine]), ++ cpu_to_le32(seq % APPLE_RING_ENTRIES)); + wmb(); +- err = apple_rtkit_send_message(apple->rtk, APPLE_EP_AGX_DOORBELL, APPLE_MSG_KICK, NULL, +- false); ++ err = apple->chip->kick(apple); + if (!err) + err = seq; + out_unlock: +@@ -1347,91 +1096,6 @@ out_unlock: + return err; + } + +-static void pvr_apple_geom_job_init(struct pvr_apple_ta_job *ta, const struct pvr_apple_cmd_geom *cmd, +- struct pvr_hwrt_data *hwrt) +-{ +- const struct pvr_hwrt_dataset *set = hwrt->hwrt_dataset; +- const struct rogue_fwif_hwrtdata_common *common = &set->common; +- const struct pvr_apple_geom_regs *apple = &cmd->apple; +- struct pvr_free_list *fl = set->free_lists[0]; +- u64 offset; +- +- memset(ta, 0, sizeof(*ta)); +- ta->regs32[0] = apple->r_c78; +- ta->regs32[1] = cmd->cmd.regs.ppp_ctrl; +- ta->regs32[2] = common->screen_pixel_max; /* 0xc98 PPP_SCREEN */ +- ta->regs32[3] = common->te_screen; /* 0xc18 */ +- ta->regs32[4] = common->te_mtile1; /* 0xc08 */ +- ta->regs32[5] = common->te_mtile2; /* 0xc10 */ +- ta->regs32[6] = common->mtile_stride; /* 0xc20 */ +- ta->regs32[7] = cmd->cmd.regs.te_psg; /* 0xc28 */ +- ta->regs64[0] = apple->pds_ctrl; /* 0xc90 */ +- ta->regs64[1] = hwrt->data.rgn_header_dev_addr; /* 0xc38 */ +- ta->regs64[2] = hwrt->data.tail_ptrs_dev_addr; /* 0xc40 */ +- ta->regs64[3] = common->teaa; /* 0xc00 */ +- ta->regs64[4] = common->multi_sample_ctl; /* 0xc80 */ +- ta->regs64[5] = apple->ctx_state_base[0]; /* 0x428 */ +- ta->regs64[6] = apple->ctx_state_base[1]; /* 0xcc0 */ +- memcpy(&ta->regs64[7], apple->vdm_ctx_store_task, sizeof(apple->vdm_ctx_store_task)); +- memcpy(&ta->regs64[10], apple->vdm_ctx_resume_task, sizeof(apple->vdm_ctx_resume_task)); +- ta->regs64[13] = apple->vdm_ctx_state; /* 0x470 */ +- ta->regs64[14] = apple->vdm_call_stack; /* 0x418 */ +- ta->regs64[15] = cmd->cmd.regs.vdm_ctrl_stream_base; /* 0x408 */ +- memcpy(&ta->regs64[16], apple->pds_state, sizeof(apple->pds_state)); +- memcpy(&ta->regs64[22], apple->cluster, sizeof(apple->cluster)); +- +- mutex_lock(&fl->lock); +- offset = (u64)(fl->max_pages - fl->current_pages) * sizeof(u32); +- ta->free_list = fl->free_list_gpu_addr + (offset & ~15ull); +- ta->free_list_pages = fl->current_pages + (offset & 15) / sizeof(u32); +- mutex_unlock(&fl->lock); +- ta->mlist = hwrt->data.pm_mlist_dev_addr; +- ta->mtile_array = hwrt->data.macrotile_array_dev_addr; +- ta->vheap_table = hwrt->data.vheap_table_dev_addr; +-} +- +-/* Start3D's register blocks from the Rogue fragment command and the Apple extension */ +-static void pvr_apple_frag_job_init(struct pvr_apple_3d_job *frag, +- const struct pvr_apple_cmd_frag *cmd, struct pvr_hwrt_data *hwrt) +-{ +- const struct rogue_fwif_frag_regs *regs = &cmd->cmd.regs; +- const struct pvr_apple_frag_regs *apple = &cmd->apple; +- +- memset(frag, 0, sizeof(*frag)); +- frag->regs32[0] = hwrt->hwrt_dataset->common.isp_mtile_size; /* 0xf18 */ +- frag->regs32[1] = regs->isp_bgobjdepth; /* 0xf80 */ +- frag->regs32[2] = regs->isp_bgobjvals; /* 0xf88 */ +- frag->regs32[4] = apple->r_6d0; +- frag->regs32[5] = apple->event_pixel_pds_code; /* 0x618 */ +- frag->regs32[6] = regs->event_pixel_pds_data; /* 0x620 */ +- frag->regs32[7] = regs->event_pixel_pds_info; /* 0x628 */ +- frag->regs32[8] = regs->isp_ctl; /* 0xf38 */ +- frag->regs32[9] = apple->r_3e00; +- frag->regs32[10] = apple->r_fd8; +- frag->regs32[11] = apple->r_4070; +- memcpy(&frag->regs32[12], apple->line, sizeof(apple->line)); +- frag->regs64[0] = apple->isp_sample_pos; /* 0xfa8 */ +- frag->regs64[1] = regs->isp_scissor_base; /* 0xfb0 */ +- frag->regs64[2] = regs->isp_dbias_base; /* 0xfb8 */ +- frag->regs64[3] = regs->isp_oclqry_base; /* 0xfc0, pipe 0 */ +- frag->regs64[4] = regs->isp_oclqry_base; /* 0xfc0, pipe 1 */ +- /* +- * Depth load and store bases, then stencil's (measured: the depth +- * store reaches memory only with the base in 0xf58) +- */ +- frag->regs64[5] = regs->isp_zlsctl; /* 0xf48 */ +- frag->regs64[6] = regs->isp_zload_store_base; /* 0xf50 Z load */ +- frag->regs64[7] = regs->isp_zload_store_base; /* 0xf58 Z store */ +- frag->regs64[8] = regs->isp_stencil_load_store_base; /* 0xf60 S load */ +- frag->regs64[9] = regs->isp_stencil_load_store_base; /* 0xf68 S store */ +- frag->regs64[10] = apple->isp_rgn; /* 0xf28 */ +- memcpy(&frag->regs64[11], apple->pds_bgnd, sizeof(apple->pds_bgnd)); +- frag->regs64[15] = regs->isp_aa; /* 0xf30 */ +- memcpy(&frag->regs64[16], apple->cluster0, sizeof(apple->cluster0)); +- memcpy(&frag->regs64[22], apple->cluster1, sizeof(apple->cluster1)); +- frag->regs64[28] = apple->r_fd0; +-} +- + /** + * pvr_apple_submit_render_job() - Queue a geometry or fragment job + * @job: The job, its command decoded with pvr_apple_cmd_{geom,frag}_stream. +@@ -1443,10 +1107,7 @@ struct dma_fence *pvr_apple_submit_render_job(struct pvr_job *job) + struct pvr_apple *apple = to_apple(job->pvr_dev); + struct pvr_apple_fence *fence; + enum pvr_apple_engine engine; +- union { +- struct pvr_apple_ta_job ta; +- struct pvr_apple_3d_job frag; +- } *hw; ++ void *hw; + int seq; + + if (job->type == DRM_PVR_JOB_TYPE_FRAGMENT && +@@ -1454,7 +1115,7 @@ struct dma_fence *pvr_apple_submit_render_job(struct pvr_job *job) + return ERR_PTR(-EOPNOTSUPP); /* partial renders: not yet */ + + fence = kzalloc(sizeof(*fence), GFP_KERNEL); +- hw = kmalloc(sizeof(*hw), GFP_KERNEL); ++ hw = kmalloc(apple->chip->render_job_size, GFP_KERNEL); + if (!fence || !hw) { + kfree(fence); + kfree(hw); +@@ -1462,10 +1123,10 @@ struct dma_fence *pvr_apple_submit_render_job(struct pvr_job *job) + } + if (job->type == DRM_PVR_JOB_TYPE_GEOMETRY) { + engine = APPLE_ENGINE_GEOM; +- pvr_apple_geom_job_init(&hw->ta, job->cmd, job->hwrt); ++ apple->chip->prepare_geom(hw, job); + } else { + engine = APPLE_ENGINE_FRAG; +- pvr_apple_frag_job_init(&hw->frag, job->cmd, job->hwrt); ++ apple->chip->prepare_frag(hw, job); + } + seq = pvr_apple_queue_render(apple, job->ctx->vm_ctx, engine, hw, + engine == APPLE_ENGINE_GEOM ? +@@ -1548,66 +1209,31 @@ int pvr_apple_vm_init(struct pvr_device *pvr_dev, struct pvr_vm_context *vm_ctx) + } + + /* +- * Hand the firmware fresh init data: the window and heap layouts with empty +- * rings, GPU context 0 on the kernel VM. Every stamp starts at 0 again, so +- * do the sequence numbers. The firmware has just booted. ++ * Hand the firmware fresh init data: empty rings, GPU context 0 on the kernel ++ * VM. Every stamp starts at 0 again, so do the sequence numbers. The firmware ++ * has just booted. + */ + static int pvr_apple_fw_start(struct pvr_apple *apple) + { +- struct pvr_device *pvr_dev = apple->pvr_dev; +- struct device *dev = apple_dev(apple); +- u32 init = pvr_apple_layout_init_offset(); +- u32 ready; +- int err; +- +- memset(apple->win + init, 0, PVR_APPLE_WINDOW_SIZE - init); +- memset(apple->heap, 0, PVR_APPLE_HEAP_SIZE); +- apple->vm_root = pvr_vm_get_page_table_root_addr(pvr_dev->kernel_vm_ctx); ++ apple->vm_root = pvr_vm_get_page_table_root_addr(apple->pvr_dev->kernel_vm_ctx); + apple->last_seq = 0; + apple->ta_seq = 0; + apple->frag_seq = 0; + apple->frag_events = 0; + apple->geom_pending = false; +- pvr_apple_layout_build(apple->win, apple->win_dma, apple->heap, apple->heap_dma, +- apple->vm_root, APPLE_GPU_PDS, APPLE_GPU_USC); +- /* Geometry and fragment queues (work rings 0 and 1) */ +- pvr_apple_layout_build_ta(apple->heap); +- pvr_apple_layout_build_3d(apple->heap); +- *(__le32 *)(apple->heap + pvr_apple_layout_debug_flags_offset()) = +- cpu_to_le32(pvr_apple_fw_debug); +- wmb(); +- +- err = apple_rtkit_send_message(apple->rtk, APPLE_EP_AGX_INIT, +- APPLE_MSG_INIT | (apple->win_dma + init), NULL, false); +- if (err) +- return err; +- err = read_poll_timeout(heap_read32, ready, ready == 1, 1000, 3000000, false, apple, +- HEAP_U_READY); +- if (err) { +- dev_err(dev, "GFX firmware did not accept init data\n"); +- return err; +- } +- dev_info(dev, "GFX firmware initialized (window %pad, heap %pad)\n", &apple->win_dma, +- &apple->heap_dma); +- return 0; ++ return apple->chip->start(apple); + } + + /* + * Shut the firmware down and power the GPU off through state 0. A wedged or +- * crashed firmware may not answer: go on regardless. apple_rtkit_shutdown() +- * resets RTKit's state only once the firmware acknowledged; after a crash it +- * stays "crashed" and refuses to boot, so reset it here. ++ * crashed firmware may not answer: go on regardless. + */ + static void pvr_apple_fw_stop(struct pvr_apple *apple) + { + lockdep_assert_held(&apple->submit_lock); + if (!apple->booted) + return; +- pvr_apple_request_power_state(apple, 0); +- if (apple_rtkit_shutdown(apple->rtk)) +- apple_rtkit_reinit(apple->rtk); +- writel(readl(apple->fender + FENDER_RUN) & ~FENDER_RUN_ENABLE, +- apple->fender + FENDER_RUN); ++ apple->chip->stop(apple, true); + apple->booted = false; + } + +@@ -1617,12 +1243,8 @@ static int pvr_apple_fw_restart(struct pvr_apple *apple) + int err; + + lockdep_assert_held(&apple->submit_lock); +- /* The window as at probe: the image, zeroes after it, RTKit buffers anew */ +- memset(apple->win, 0, pvr_apple_layout_init_offset()); +- memcpy(apple->win, apple->fw_image, apple->fw_size); +- apple->win_next = PVR_APPLE_FW_RUNTIME; +- wmb(); +- err = pvr_apple_boot(apple->pvr_dev); ++ apple->chip->restore_image(apple); ++ err = pvr_apple_boot(apple); + if (!err) + err = pvr_apple_fw_start(apple); + return err; +@@ -1835,12 +1457,10 @@ void pvr_apple_fw_fini(struct pvr_device *pvr_dev) + } + if (!apple->booted) + return; +- pvr_apple_request_power_state(apple, 0); +- apple_rtkit_shutdown(apple->rtk); +- /* messages until the shutdown queued completion checks */ ++ apple->chip->stop(apple, false); ++ /* the firmware's messages until its stop queued completion checks */ + cancel_delayed_work_sync(&apple->poll_work); + hrtimer_cancel(&apple->poll_timer); +- writel(readl(apple->fender + FENDER_RUN) & ~FENDER_RUN_ENABLE, apple->fender + FENDER_RUN); + apple->booted = false; + } + +@@ -1867,3 +1487,19 @@ void pvr_apple_device_info_init(struct pvr_device *pvr_dev) + pvr_dev->features.has_isp_samples_per_pixel = true; + pvr_dev->features.isp_samples_per_pixel = 2; + } ++ ++/** ++ * pvr_apple_cmd_stream() - How a render job's command stream is decoded ++ * @pvr_dev: Target device. ++ * @type: %DRM_PVR_JOB_TYPE_GEOMETRY or %DRM_PVR_JOB_TYPE_FRAGMENT. ++ * ++ * Return: the chip's stream definition: the Rogue command plus the registers ++ * its firmware takes beyond it. ++ */ ++const struct pvr_stream_cmd_defs *pvr_apple_cmd_stream(struct pvr_device *pvr_dev, ++ enum drm_pvr_job_type type) ++{ ++ const struct pvr_apple_chip *chip = pvr_dev->device_data->apple_chip; ++ ++ return type == DRM_PVR_JOB_TYPE_GEOMETRY ? chip->geom_stream : chip->frag_stream; ++} +diff --git a/drivers/gpu/drm/imagination/pvr_apple.h b/drivers/gpu/drm/imagination/pvr_apple.h +index 390bd22..90aeaae 100644 +--- a/drivers/gpu/drm/imagination/pvr_apple.h ++++ b/drivers/gpu/drm/imagination/pvr_apple.h +@@ -1,5 +1,5 @@ + /* SPDX-License-Identifier: GPL-2.0-only OR MIT */ +-/* Apple A10 (T8010): Rogue hardware behind Apple's GFX firmware. */ ++/* Apple chips: Rogue hardware behind Apple's GFX firmware. */ + + #ifndef PVR_APPLE_H + #define PVR_APPLE_H +@@ -12,14 +12,17 @@ + #include "pvr_rogue_fwif_client.h" + + struct dma_fence; ++struct pvr_apple_chip; + struct pvr_context; + struct pvr_device; + struct pvr_job; + struct pvr_queue; + struct pvr_power_sequence_ops; ++struct pvr_stream_cmd_defs; + 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_t8010; + + /* + * The A10's GPU resolves 38-bit data addresses (a triangle renders with its +@@ -102,6 +105,8 @@ int pvr_apple_vm_init(struct pvr_device *pvr_dev, struct pvr_vm_context *vm_ctx) + void pvr_apple_mmu_flush(struct pvr_device *pvr_dev); + void pvr_apple_vm_fini(struct pvr_device *pvr_dev, struct pvr_vm_context *vm_ctx); + void pvr_apple_quiesce(struct pvr_device *pvr_dev); ++const struct pvr_stream_cmd_defs *pvr_apple_cmd_stream(struct pvr_device *pvr_dev, ++ enum drm_pvr_job_type type); + + struct pvr_queue *pvr_apple_queue_create(struct pvr_context *ctx, enum drm_pvr_job_type type); + void pvr_apple_queue_destroy(struct pvr_queue *queue, bool cleanup_queue_entity); +@@ -141,6 +146,11 @@ static inline void pvr_apple_wait_file_jobs(struct pvr_device *pvr_dev) {} + static inline void pvr_apple_report_hang(struct pvr_device *pvr_dev, struct dma_fence *fence) {} + static inline void pvr_apple_vm_fini(struct pvr_device *pvr_dev, + struct pvr_vm_context *vm_ctx) {} ++static inline const struct pvr_stream_cmd_defs * ++pvr_apple_cmd_stream(struct pvr_device *pvr_dev, enum drm_pvr_job_type type) ++{ ++ return NULL; ++} + #endif + + #endif /* PVR_APPLE_H */ +diff --git a/drivers/gpu/drm/imagination/pvr_apple_chip.h b/drivers/gpu/drm/imagination/pvr_apple_chip.h +new file mode 100644 +index 0000000..9f399ea +--- /dev/null ++++ b/drivers/gpu/drm/imagination/pvr_apple_chip.h +@@ -0,0 +1,230 @@ ++/* SPDX-License-Identifier: GPL-2.0-only OR MIT */ ++/* ++ * Apple's GPU firmware, between pvr_apple.c and a chip: the state every chip ++ * shares, and what a chip provides (struct pvr_apple_chip). ++ * ++ * The chips run one family of firmware: jobs go into 256-entry work rings, ++ * one per engine, and are done when the engine's stamp reaches their sequence ++ * number; the firmware posts events to a ring the host drains; the Rogue core ++ * is powered through Fender's power-state register. A chip differs in the ++ * processor the firmware runs on and how it is booted and rung, and in the ++ * layout of the memory it shares with the host, commands included. ++ */ ++ ++#ifndef PVR_APPLE_CHIP_H ++#define PVR_APPLE_CHIP_H ++ ++#include "pvr_apple.h" ++#include "pvr_device.h" ++ ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++ ++struct devfreq; ++struct pvr_gem_object; ++struct pvr_stream_cmd_defs; ++ ++/* ++ * Fender's power-state request and acknowledge. The power state is also the ++ * GPU performance state: 0 is off, 1 to N are the GPU's clock and voltage ++ * steps, lowest to highest (the firmware's own DVFS writes the same register, ++ * fw 0x16d40; ours leaves it alone). The steps come from the bootloader as ++ * the GPU's OPP table, the state number as opp-level; without one the GPU ++ * runs in the chip's perf_state_max. ++ */ ++#define FENDER_POWER_STATE 0x1000 ++#define FENDER_POWER_STATE_MASK 0x1f ++#define FENDER_POWER_REQUEST 0x10 /* | target state */ ++ ++/* Driver-owned GPU addresses: code at powervr's PDS and USC heap bases */ ++#define APPLE_GPU_PDS PVR_APPLE_PDSCODEDATA_HEAP_BASE ++#define APPLE_GPU_USC PVR_APPLE_USCCODE_HEAP_BASE ++ ++/* Entries of a work ring and of the event ring */ ++#define APPLE_RING_ENTRIES 256 ++ ++enum pvr_apple_engine { ++ APPLE_ENGINE_COMPUTE, ++ APPLE_ENGINE_GEOM, ++ APPLE_ENGINE_FRAG, ++ APPLE_ENGINES ++}; ++ ++/* ++ * Where pvr_apple.c reads and writes the memory shared with the firmware: ++ * offsets into pvr_apple.heap, set by the chip's init(). ++ */ ++struct pvr_apple_layout { ++ u32 stamp[APPLE_ENGINES]; /* sequence number of the engine's last finished job */ ++ u32 producer[APPLE_ENGINES]; /* producer index of the engine's work ring */ ++ u32 frag_ring; /* fragment work ring's first index: jobs retired */ ++ u32 frag_retired; /* fragment retirement stamp */ ++ u32 mmu_root; /* GPU context 0: page-table root, 64 bits */ ++ u32 mmu_gen; /* GPU context 0: TLB generation */ ++ u32 event_consumer; /* event ring indices */ ++ u32 event_producer; ++ u32 events; /* event ring entries, event_size bytes each */ ++ u32 event_size; ++}; ++ ++struct pvr_apple { ++ struct pvr_device *pvr_dev; ++ const struct pvr_apple_chip *chip; ++ void *chip_data; /* the chip's own state, set by its init() */ ++ void __iomem *fender; ++ ++ const void *fw_image; /* the firmware as loaded (pvr_apple_fw_load()) */ ++ size_t fw_size; ++ void *heap; /* memory shared with the firmware: rings, stamps, commands */ ++ struct pvr_apple_layout layout; ++ bool booted; ++ bool fw_ready; /* init data handed over once: runtime PM restarts it */ ++ ++ struct mutex submit_lock; /* job slots, rings, producer index, GPU VM */ ++ u32 last_seq; /* last submitted compute job */ ++ u32 ta_seq; /* last submitted geometry job */ ++ u32 frag_seq; /* last submitted fragment job */ ++ u32 frag_events; /* fragment-done events the firmware posted */ ++ /* ++ * A geometry job reached the firmware since the last fragment job (or ++ * its start): only then does the next Start3D find a free list ++ */ ++ bool geom_pending; ++ spinlock_t event_lock; /* event ring consumer, frag_events */ ++ dma_addr_t vm_root; /* page-table root GPU context 0 runs on */ ++ ++ /* ++ * Completion: fences of submitted jobs, one timeline per engine, ++ * signalled from the engine's stamp. ++ */ ++ u64 fence_context; /* first of APPLE_ENGINES */ ++ spinlock_t fence_lock; ++ struct list_head pending[3]; /* struct pvr_apple_fence, by seq */ ++ struct delayed_work poll_work; ++ struct hrtimer poll_timer; /* rearms poll_work while jobs are pending */ ++ /* ++ * The last fragment job pushed: the next geometry job waits for it, as ++ * the firmware keeps one parameter-manager scene at a time. ++ */ ++ struct dma_fence *last_frag; ++ /* The last geometry job pushed: the next fragment job consumes its scene */ ++ struct dma_fence *last_geom; ++ ++ /* ++ * Performance state (Fender power state) and its scaling: perf_lock ++ * serializes requests; busy_since (0 while idle) and busy_ns, under ++ * fence_lock, give devfreq the time jobs were in flight. ++ */ ++ struct mutex perf_lock; ++ u32 perf_state; ++ u32 perf_state_max; ++ struct devfreq *devfreq; ++ ktime_t busy_since; ++ u64 busy_ns; ++ ktime_t stat_since; ++ ++ /* Backs GPU VA 0 in userspace VMs: the end-of-render writes there */ ++ struct pvr_gem_object *zero_obj; ++ ++ /* Parameter-manager pages every free list hands out (pvr_apple_pm_pages()) */ ++ struct mutex pm_lock; ++ struct pvr_gem_object *pm_pages; ++ ++ /* ++ * GPU pages of the kernel VM context: data (stream, terminate PDS, ++ * context state, flush scratch; also mapped into every userspace VM), ++ * PDS and USC code at the code bases. ++ */ ++ struct pvr_apple_gpu_page { ++ struct pvr_gem_object *obj; ++ u8 *map; ++ u64 device_addr; ++ size_t size; ++ } data, pds, usc; ++}; ++ ++/** ++ * struct pvr_apple_chip - One chip's firmware processor, memory layout and jobs ++ * @fw_name: Firmware file, unless the device tree names one (firmware-name). ++ * @perf_state_max: Fastest Fender power state, used without an OPP table. ++ * @compute_slots: Compute jobs the firmware's memory holds at a time. ++ * @render_job_size: Bytes @prepare_geom and @prepare_frag fill. ++ * @geom_stream: How a geometry job's command stream from userspace is decoded. ++ * @frag_stream: The same for a fragment job. ++ * @init: At probe, with Fender mapped: map and allocate what the chip needs, ++ * load the firmware (pvr_apple_fw_load()), set &pvr_apple.heap, ++ * &pvr_apple.layout and &pvr_apple.chip_data. Device-managed. ++ * @boot: Run the firmware's processor and power the core ++ * (pvr_apple_fender_power_on()); the RGX registers work afterwards. Leaves ++ * everything stopped on an error. ++ * @start: Hand the booted firmware fresh init data and wait until it takes ++ * jobs. Every stamp and ring starts empty; GPU context 0 runs on the ++ * kernel VM (&pvr_apple.vm_root). ++ * @stop: Power the core off (Fender state 0) and stop the firmware's ++ * processor. With @recover the firmware may have crashed or hung: leave ++ * the processor ready for the next @boot regardless. ++ * @idle: After @stop for runtime suspend: give up what would keep the GPU's ++ * power domain on. ++ * @restore_image: Before @boot, when the firmware ran before: put its memory ++ * back as @init left it. ++ * @kick: Make the firmware look at its work rings. ++ * @write_compute: Write compute job @seq (CDM control stream and context ++ * state at GPU addresses) and its work-ring entry. ++ * @prepare_geom: Translate a geometry job (its decoded command, HWRT data and ++ * free list) into what @write_geom takes. May sleep; no lock held. ++ * @prepare_frag: The same for a fragment job and @write_frag. ++ * @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. ++ * ++ * pvr_apple.c calls @boot, @start, @stop, @restore_image, @kick and the ++ * write functions with &pvr_apple.submit_lock held, except @boot and @start ++ * at probe and @stop when the device goes. It publishes the ring's producer index ++ * after a write function and before @kick. ++ */ ++struct pvr_apple_chip { ++ const char *fw_name; ++ u32 perf_state_max; ++ u32 compute_slots; ++ size_t render_job_size; ++ const struct pvr_stream_cmd_defs *geom_stream; ++ const struct pvr_stream_cmd_defs *frag_stream; ++ ++ int (*init)(struct pvr_apple *apple); ++ int (*boot)(struct pvr_apple *apple); ++ int (*start)(struct pvr_apple *apple); ++ void (*stop)(struct pvr_apple *apple, bool recover); ++ void (*idle)(struct pvr_apple *apple); ++ void (*restore_image)(struct pvr_apple *apple); ++ int (*kick)(struct pvr_apple *apple); ++ void (*write_compute)(struct pvr_apple *apple, u32 seq, u64 stream, u64 ctx_state); ++ void (*prepare_geom)(void *hw, struct pvr_job *job); ++ void (*prepare_frag)(void *hw, struct pvr_job *job); ++ 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); ++}; ++ ++/* The heap is coherent memory shared with the firmware, not MMIO. */ ++static inline u32 heap_read32(struct pvr_apple *apple, u32 offset) ++{ ++ return le32_to_cpu(READ_ONCE(*(__le32 *)(apple->heap + offset))); ++} ++ ++static inline struct device *apple_dev(struct pvr_apple *apple) ++{ ++ return from_pvr_device(apple->pvr_dev)->dev; ++} ++ ++int pvr_apple_fw_load(struct pvr_apple *apple, size_t max_size); ++int pvr_apple_request_power_state(struct pvr_apple *apple, u32 state); ++int pvr_apple_fender_power_on(struct pvr_apple *apple); ++ ++#endif /* PVR_APPLE_CHIP_H */ +diff --git a/drivers/gpu/drm/imagination/pvr_apple_layout.c b/drivers/gpu/drm/imagination/pvr_apple_layout.c +index 34c9c49..d04cfc0 100644 +--- a/drivers/gpu/drm/imagination/pvr_apple_layout.c ++++ b/drivers/gpu/drm/imagination/pvr_apple_layout.c +@@ -718,15 +718,6 @@ void pvr_apple_layout_write_3d_job(u8 *heap, u32 seq, const struct pvr_apple_3d_ + heap[work + 0x12] = F_SLOT; + } + +-/* +- * The producer index of a work ring after job @seq: ring indices wrap at the +- * ring size (the firmware's own indices do), sequence numbers do not. +- */ +-u32 pvr_apple_layout_ring_index(u32 seq) +-{ +- return seq % RING_ENTRIES; +-} +- + /* Work ring @ring's index words: two the firmware advances, then the producer */ + u32 pvr_apple_layout_ring_indices_offset(unsigned int ring) + { +diff --git a/drivers/gpu/drm/imagination/pvr_apple_layout.h b/drivers/gpu/drm/imagination/pvr_apple_layout.h +index ff2205d..1796162 100644 +--- a/drivers/gpu/drm/imagination/pvr_apple_layout.h ++++ b/drivers/gpu/drm/imagination/pvr_apple_layout.h +@@ -54,7 +54,6 @@ u32 pvr_apple_layout_mmu_root_offset(unsigned int ctx); + u32 pvr_apple_layout_mmu_gen_offset(unsigned int ctx); + u32 pvr_apple_layout_init_offset(void); + u32 pvr_apple_layout_compute_producer(void); +-u32 pvr_apple_layout_ring_index(u32 seq); + u32 pvr_apple_layout_stamp_offset(void); + u32 pvr_apple_layout_debug_flags_offset(void); + u32 pvr_apple_layout_sku_index(void); +diff --git a/drivers/gpu/drm/imagination/pvr_apple_t8010.c b/drivers/gpu/drm/imagination/pvr_apple_t8010.c +new file mode 100644 +index 0000000..f3ee833 +--- /dev/null ++++ b/drivers/gpu/drm/imagination/pvr_apple_t8010.c +@@ -0,0 +1,477 @@ ++// SPDX-License-Identifier: GPL-2.0-only OR MIT ++/* ++ * Apple A10 (T8010): the GFX firmware's coprocessor, memory and commands. ++ * ++ * The firmware runs on "gfx-kf", an AppleA7IOP V2 coprocessor, and speaks RTKit ++ * through the A7IOP mailbox. It powers the Rogue core through the "Fender" ++ * block; the AP may only touch RGX registers once Fender reports a power state ++ * above 0. ++ * The firmware maps host memory itself: a 1 MiB window at IOP address 0 holds ++ * the image and the init page, and a heap is mapped through memory descriptors ++ * (see pvr_apple_layout.c). ++ * ++ * Messages, AGX endpoints: ++ * 0x20 init: 0x81 << 48 | physical address of the init page ++ * 0x21 doorbell: 0x83 << 48 makes the firmware scan its rings ++ */ ++ ++#include "pvr_apple.h" ++#include "pvr_apple_chip.h" ++#include "pvr_apple_layout.h" ++#include "pvr_device.h" ++#include "pvr_free_list.h" ++#include "pvr_hwrt.h" ++#include "pvr_job.h" ++#include "pvr_stream_defs.h" ++ ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++ ++#define APPLE_EP_AGX_INIT 0x20 ++#define APPLE_EP_AGX_DOORBELL 0x21 ++#define APPLE_MSG_INIT (0x81ull << 48) ++#define APPLE_MSG_KICK (0x83ull << 48) ++ ++/* Fender beyond the power state: the firmware window, the run bit */ ++#define FENDER_PERF_STATE_MAX 6 /* T8010's fastest */ ++#define FENDER_WINDOW 0x200c ++#define FENDER_RUN 0x2048 ++#define FENDER_RUN_ENABLE BIT(0) ++ ++/* A7IOP: the IOP's copy of the firmware window */ ++#define IOP_WINDOW 0x0008 ++#define IOP_WINDOW_ENABLE 0x0038 ++ ++#define APPLE_JOB_SLOTS 16 /* matches the layout's per-job slots */ ++ ++/* Heap: U+0x14 becomes 1 once the firmware accepted the init data */ ++#define HEAP_U_READY (0x5000 + 0x14) ++ ++struct pvr_apple_t8010 { ++ void __iomem *iop; ++ struct apple_rtkit *rtk; ++ ++ void *win; ++ dma_addr_t win_dma; ++ size_t win_next; /* RTKit buffers are carved after the image */ ++ dma_addr_t heap_dma; ++ bool rtk_idle; /* RTKit's mailbox stopped while the GPU is off */ ++}; ++ ++union pvr_apple_t8010_render_job { ++ struct pvr_apple_ta_job ta; ++ struct pvr_apple_3d_job frag; ++}; ++ ++/* ++ * The firmware's debug flags: 1 SKU trace events, 2 cancel-path events, into ++ * a debug ring of 511 records that nothing drains. Once it is full the ++ * firmware stops the GPU's work and asks the host to drain it (messages of ++ * type 2 on the init endpoint, iOS 10's receivedMessageFromAKF()): only for ++ * short experiments. Read at each firmware start, so a change applies from ++ * the next reset; a hang report then ends with the ring's last records. ++ */ ++static unsigned int pvr_apple_fw_debug; ++module_param_named(apple_fw_debug, pvr_apple_fw_debug, uint, 0644); ++MODULE_PARM_DESC(apple_fw_debug, "Apple A10: GFX firmware debug flags (1 trace events, 2 cancel events; default 0)"); ++ ++static int pvr_apple_shmem_setup(void *cookie, struct apple_rtkit_shmem *bfr) ++{ ++ struct pvr_apple *apple = cookie; ++ struct pvr_apple_t8010 *t = apple->chip_data; ++ size_t size = ALIGN(bfr->size, SZ_4K); ++ ++ if (bfr->iova) { ++ /* Firmware-chosen address: must lie inside the window */ ++ if (bfr->iova + bfr->size > PVR_APPLE_WINDOW_SIZE) ++ return -EINVAL; ++ bfr->buffer = t->win + bfr->iova; ++ bfr->is_mapped = true; ++ return 0; ++ } ++ if (t->win_next + size > pvr_apple_layout_init_offset()) ++ return -ENOMEM; ++ bfr->iova = t->win_next; ++ bfr->buffer = t->win + t->win_next; ++ t->win_next += size; ++ return 0; ++} ++ ++static void pvr_apple_shmem_destroy(void *cookie, struct apple_rtkit_shmem *bfr) ++{ ++ /* Carved from the window, which lives as long as the device */ ++} ++ ++static void pvr_apple_recv_message(void *cookie, u8 endpoint, u64 message) ++{ ++ struct pvr_apple *apple = cookie; ++ ++ dev_dbg(apple_dev(apple), "firmware ep %#x: %#016llx\n", endpoint, message); ++ /* none is known to mean completion, but a check costs little */ ++ mod_delayed_work(system_highpri_wq, &apple->poll_work, 0); ++} ++ ++static void pvr_apple_crashed(void *cookie, const void *crashlog, size_t size) ++{ ++ struct pvr_apple *apple = cookie; ++ ++ dev_err(apple_dev(apple), "GFX firmware crashed (%zu byte crashlog)\n", size); ++ if (crashlog) ++ print_hex_dump(KERN_ERR, "crashlog: ", DUMP_PREFIX_OFFSET, 16, 4, crashlog, size, ++ false); ++} ++ ++static const struct apple_rtkit_ops pvr_apple_rtkit_ops = { ++ .crashed = pvr_apple_crashed, ++ .recv_message = pvr_apple_recv_message, ++ .shmem_setup = pvr_apple_shmem_setup, ++ .shmem_destroy = pvr_apple_shmem_destroy, ++}; ++ ++/* The window is programmed in two copies: Fender (4-byte stride) and IOP (8). */ ++static void pvr_apple_set_window(struct pvr_apple *apple) ++{ ++ struct pvr_apple_t8010 *t = apple->chip_data; ++ const u32 fields[] = { ++ lower_32_bits(t->win_dma), upper_32_bits(t->win_dma), ++ 0, 0, PVR_APPLE_WINDOW_SIZE, 0, ++ }; ++ unsigned int i; ++ ++ for (i = 0; i < ARRAY_SIZE(fields); i++) { ++ writel(fields[i], apple->fender + FENDER_WINDOW + 4 * i); ++ writel(fields[i], t->iop + IOP_WINDOW + 8 * i); ++ } ++ writel(1, t->iop + IOP_WINDOW_ENABLE); ++} ++ ++static int pvr_apple_t8010_init(struct pvr_apple *apple) ++{ ++ struct device *dev = apple_dev(apple); ++ struct platform_device *pdev = to_platform_device(dev); ++ struct pvr_apple_layout *layout = &apple->layout; ++ struct pvr_apple_t8010 *t; ++ int err; ++ ++ t = devm_kzalloc(dev, sizeof(*t), GFP_KERNEL); ++ if (!t) ++ return -ENOMEM; ++ apple->chip_data = t; ++ ++ t->iop = devm_platform_ioremap_resource_byname(pdev, "iop"); ++ if (IS_ERR(t->iop)) ++ return PTR_ERR(t->iop); ++ ++ /* The firmware addresses host memory physically through its own MMU. */ ++ err = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(40)); ++ if (err) ++ return err; ++ t->win = dmam_alloc_coherent(dev, PVR_APPLE_WINDOW_SIZE, &t->win_dma, GFP_KERNEL); ++ apple->heap = dmam_alloc_coherent(dev, PVR_APPLE_HEAP_SIZE, &t->heap_dma, GFP_KERNEL); ++ if (!t->win || !apple->heap) ++ return -ENOMEM; ++ if (!IS_ALIGNED(t->win_dma, SZ_16K)) ++ return dev_err_probe(dev, -EINVAL, "firmware window %pad not 16 KiB aligned\n", ++ &t->win_dma); ++ t->win_next = PVR_APPLE_FW_RUNTIME; ++ ++ /* The firmware writes into its image while it runs: the copy is for a reset */ ++ err = pvr_apple_fw_load(apple, PVR_APPLE_FW_RUNTIME); ++ if (err) ++ return err; ++ memcpy(t->win, apple->fw_image, apple->fw_size); ++ ++ t->rtk = devm_apple_rtkit_init(dev, apple, NULL, 0, &pvr_apple_rtkit_ops); ++ if (IS_ERR(t->rtk)) ++ return dev_err_probe(dev, PTR_ERR(t->rtk), "RTKit init failed\n"); ++ ++ layout->stamp[APPLE_ENGINE_COMPUTE] = pvr_apple_layout_stamp_offset(); ++ layout->stamp[APPLE_ENGINE_GEOM] = pvr_apple_layout_ta_stamp_offset(); ++ layout->stamp[APPLE_ENGINE_FRAG] = pvr_apple_layout_3d_stamp_offset(); ++ layout->producer[APPLE_ENGINE_COMPUTE] = pvr_apple_layout_compute_producer(); ++ layout->producer[APPLE_ENGINE_GEOM] = pvr_apple_layout_ta_producer(); ++ layout->producer[APPLE_ENGINE_FRAG] = pvr_apple_layout_3d_producer(); ++ layout->frag_ring = pvr_apple_layout_ring_indices_offset(1); ++ layout->frag_retired = pvr_apple_layout_3d_retired_offset(); ++ layout->mmu_root = pvr_apple_layout_mmu_root_offset(0); ++ layout->mmu_gen = pvr_apple_layout_mmu_gen_offset(0); ++ layout->event_consumer = pvr_apple_layout_event_consumer(); ++ layout->event_producer = pvr_apple_layout_event_producer(); ++ layout->events = pvr_apple_layout_event_entry(0); ++ layout->event_size = pvr_apple_layout_event_entry(1) - layout->events; ++ return 0; ++} ++ ++static int pvr_apple_t8010_boot(struct pvr_apple *apple) ++{ ++ struct pvr_apple_t8010 *t = apple->chip_data; ++ struct device *dev = apple_dev(apple); ++ int err; ++ ++ /* The mailbox first: the firmware talks as soon as it runs */ ++ if (t->rtk_idle) { ++ err = apple_rtkit_reinit(t->rtk); ++ if (err) ++ return err; ++ t->rtk_idle = false; ++ } ++ pvr_apple_set_window(apple); ++ writel(readl(apple->fender + FENDER_RUN) | FENDER_RUN_ENABLE, apple->fender + FENDER_RUN); ++ ++ err = apple_rtkit_boot(t->rtk); ++ if (err) { ++ dev_err(dev, "GFX firmware boot failed: %d\n", err); ++ goto err_stop; ++ } ++ err = apple_rtkit_start_ep(t->rtk, APPLE_EP_AGX_INIT); ++ if (!err) ++ err = apple_rtkit_start_ep(t->rtk, APPLE_EP_AGX_DOORBELL); ++ if (err) ++ goto err_shutdown; ++ ++ err = pvr_apple_fender_power_on(apple); ++ if (err) ++ goto err_shutdown; ++ return 0; ++ ++err_shutdown: ++ apple_rtkit_shutdown(t->rtk); ++err_stop: ++ writel(readl(apple->fender + FENDER_RUN) & ~FENDER_RUN_ENABLE, apple->fender + FENDER_RUN); ++ return err; ++} ++ ++/* The window and heap layouts with empty rings: see pvr_apple_layout.c */ ++static int pvr_apple_t8010_start(struct pvr_apple *apple) ++{ ++ struct pvr_apple_t8010 *t = apple->chip_data; ++ struct device *dev = apple_dev(apple); ++ u32 init = pvr_apple_layout_init_offset(); ++ u32 ready; ++ int err; ++ ++ memset(t->win + init, 0, PVR_APPLE_WINDOW_SIZE - init); ++ memset(apple->heap, 0, PVR_APPLE_HEAP_SIZE); ++ pvr_apple_layout_build(t->win, t->win_dma, apple->heap, t->heap_dma, ++ apple->vm_root, APPLE_GPU_PDS, APPLE_GPU_USC); ++ /* Geometry and fragment queues (work rings 0 and 1) */ ++ pvr_apple_layout_build_ta(apple->heap); ++ pvr_apple_layout_build_3d(apple->heap); ++ *(__le32 *)(apple->heap + pvr_apple_layout_debug_flags_offset()) = ++ cpu_to_le32(pvr_apple_fw_debug); ++ wmb(); ++ ++ err = apple_rtkit_send_message(t->rtk, APPLE_EP_AGX_INIT, ++ APPLE_MSG_INIT | (t->win_dma + init), NULL, false); ++ if (err) ++ return err; ++ err = read_poll_timeout(heap_read32, ready, ready == 1, 1000, 3000000, false, apple, ++ HEAP_U_READY); ++ if (err) { ++ dev_err(dev, "GFX firmware did not accept init data\n"); ++ return err; ++ } ++ dev_info(dev, "GFX firmware initialized (window %pad, heap %pad)\n", &t->win_dma, ++ &t->heap_dma); ++ return 0; ++} ++ ++/* ++ * Shut the firmware down and power the GPU off through state 0. A wedged or ++ * crashed firmware may not answer: go on regardless. apple_rtkit_shutdown() ++ * resets RTKit's state only once the firmware acknowledged; after a crash it ++ * stays "crashed" and refuses to boot, so @recover resets it here. ++ */ ++static void pvr_apple_t8010_stop(struct pvr_apple *apple, bool recover) ++{ ++ struct pvr_apple_t8010 *t = apple->chip_data; ++ ++ pvr_apple_request_power_state(apple, 0); ++ if (apple_rtkit_shutdown(t->rtk) && recover) ++ apple_rtkit_reinit(t->rtk); ++ writel(readl(apple->fender + FENDER_RUN) & ~FENDER_RUN_ENABLE, ++ apple->fender + FENDER_RUN); ++} ++ ++static void pvr_apple_t8010_idle(struct pvr_apple *apple) ++{ ++ struct pvr_apple_t8010 *t = apple->chip_data; ++ ++ /* its mailbox would keep the GPU's power domain on */ ++ t->rtk_idle = !apple_rtkit_mbox_stop(t->rtk); ++} ++ ++static void pvr_apple_t8010_restore_image(struct pvr_apple *apple) ++{ ++ struct pvr_apple_t8010 *t = apple->chip_data; ++ ++ /* The window as at probe: the image, zeroes after it, RTKit buffers anew */ ++ memset(t->win, 0, pvr_apple_layout_init_offset()); ++ memcpy(t->win, apple->fw_image, apple->fw_size); ++ t->win_next = PVR_APPLE_FW_RUNTIME; ++ wmb(); ++} ++ ++static int pvr_apple_t8010_kick(struct pvr_apple *apple) ++{ ++ struct pvr_apple_t8010 *t = apple->chip_data; ++ ++ return apple_rtkit_send_message(t->rtk, APPLE_EP_AGX_DOORBELL, APPLE_MSG_KICK, NULL, ++ false); ++} ++ ++static void pvr_apple_t8010_write_compute(struct pvr_apple *apple, u32 seq, u64 stream, ++ u64 ctx_state) ++{ ++ pvr_apple_layout_write_compute_job(apple->heap, seq, stream, ctx_state); ++} ++ ++static void pvr_apple_geom_job_init(struct pvr_apple_ta_job *ta, const struct pvr_apple_cmd_geom *cmd, ++ struct pvr_hwrt_data *hwrt) ++{ ++ const struct pvr_hwrt_dataset *set = hwrt->hwrt_dataset; ++ const struct rogue_fwif_hwrtdata_common *common = &set->common; ++ const struct pvr_apple_geom_regs *apple = &cmd->apple; ++ struct pvr_free_list *fl = set->free_lists[0]; ++ u64 offset; ++ ++ memset(ta, 0, sizeof(*ta)); ++ ta->regs32[0] = apple->r_c78; ++ ta->regs32[1] = cmd->cmd.regs.ppp_ctrl; ++ ta->regs32[2] = common->screen_pixel_max; /* 0xc98 PPP_SCREEN */ ++ ta->regs32[3] = common->te_screen; /* 0xc18 */ ++ ta->regs32[4] = common->te_mtile1; /* 0xc08 */ ++ ta->regs32[5] = common->te_mtile2; /* 0xc10 */ ++ ta->regs32[6] = common->mtile_stride; /* 0xc20 */ ++ ta->regs32[7] = cmd->cmd.regs.te_psg; /* 0xc28 */ ++ ta->regs64[0] = apple->pds_ctrl; /* 0xc90 */ ++ ta->regs64[1] = hwrt->data.rgn_header_dev_addr; /* 0xc38 */ ++ ta->regs64[2] = hwrt->data.tail_ptrs_dev_addr; /* 0xc40 */ ++ ta->regs64[3] = common->teaa; /* 0xc00 */ ++ ta->regs64[4] = common->multi_sample_ctl; /* 0xc80 */ ++ ta->regs64[5] = apple->ctx_state_base[0]; /* 0x428 */ ++ ta->regs64[6] = apple->ctx_state_base[1]; /* 0xcc0 */ ++ memcpy(&ta->regs64[7], apple->vdm_ctx_store_task, sizeof(apple->vdm_ctx_store_task)); ++ memcpy(&ta->regs64[10], apple->vdm_ctx_resume_task, sizeof(apple->vdm_ctx_resume_task)); ++ ta->regs64[13] = apple->vdm_ctx_state; /* 0x470 */ ++ ta->regs64[14] = apple->vdm_call_stack; /* 0x418 */ ++ ta->regs64[15] = cmd->cmd.regs.vdm_ctrl_stream_base; /* 0x408 */ ++ memcpy(&ta->regs64[16], apple->pds_state, sizeof(apple->pds_state)); ++ memcpy(&ta->regs64[22], apple->cluster, sizeof(apple->cluster)); ++ ++ mutex_lock(&fl->lock); ++ offset = (u64)(fl->max_pages - fl->current_pages) * sizeof(u32); ++ ta->free_list = fl->free_list_gpu_addr + (offset & ~15ull); ++ ta->free_list_pages = fl->current_pages + (offset & 15) / sizeof(u32); ++ mutex_unlock(&fl->lock); ++ ta->mlist = hwrt->data.pm_mlist_dev_addr; ++ ta->mtile_array = hwrt->data.macrotile_array_dev_addr; ++ ta->vheap_table = hwrt->data.vheap_table_dev_addr; ++} ++ ++/* Start3D's register blocks from the Rogue fragment command and the Apple extension */ ++static void pvr_apple_frag_job_init(struct pvr_apple_3d_job *frag, ++ const struct pvr_apple_cmd_frag *cmd, struct pvr_hwrt_data *hwrt) ++{ ++ const struct rogue_fwif_frag_regs *regs = &cmd->cmd.regs; ++ const struct pvr_apple_frag_regs *apple = &cmd->apple; ++ ++ memset(frag, 0, sizeof(*frag)); ++ frag->regs32[0] = hwrt->hwrt_dataset->common.isp_mtile_size; /* 0xf18 */ ++ frag->regs32[1] = regs->isp_bgobjdepth; /* 0xf80 */ ++ frag->regs32[2] = regs->isp_bgobjvals; /* 0xf88 */ ++ frag->regs32[4] = apple->r_6d0; ++ frag->regs32[5] = apple->event_pixel_pds_code; /* 0x618 */ ++ frag->regs32[6] = regs->event_pixel_pds_data; /* 0x620 */ ++ frag->regs32[7] = regs->event_pixel_pds_info; /* 0x628 */ ++ frag->regs32[8] = regs->isp_ctl; /* 0xf38 */ ++ frag->regs32[9] = apple->r_3e00; ++ frag->regs32[10] = apple->r_fd8; ++ frag->regs32[11] = apple->r_4070; ++ memcpy(&frag->regs32[12], apple->line, sizeof(apple->line)); ++ frag->regs64[0] = apple->isp_sample_pos; /* 0xfa8 */ ++ frag->regs64[1] = regs->isp_scissor_base; /* 0xfb0 */ ++ frag->regs64[2] = regs->isp_dbias_base; /* 0xfb8 */ ++ frag->regs64[3] = regs->isp_oclqry_base; /* 0xfc0, pipe 0 */ ++ frag->regs64[4] = regs->isp_oclqry_base; /* 0xfc0, pipe 1 */ ++ /* ++ * Depth load and store bases, then stencil's (measured: the depth ++ * store reaches memory only with the base in 0xf58) ++ */ ++ frag->regs64[5] = regs->isp_zlsctl; /* 0xf48 */ ++ frag->regs64[6] = regs->isp_zload_store_base; /* 0xf50 Z load */ ++ frag->regs64[7] = regs->isp_zload_store_base; /* 0xf58 Z store */ ++ frag->regs64[8] = regs->isp_stencil_load_store_base; /* 0xf60 S load */ ++ frag->regs64[9] = regs->isp_stencil_load_store_base; /* 0xf68 S store */ ++ frag->regs64[10] = apple->isp_rgn; /* 0xf28 */ ++ memcpy(&frag->regs64[11], apple->pds_bgnd, sizeof(apple->pds_bgnd)); ++ frag->regs64[15] = regs->isp_aa; /* 0xf30 */ ++ memcpy(&frag->regs64[16], apple->cluster0, sizeof(apple->cluster0)); ++ memcpy(&frag->regs64[22], apple->cluster1, sizeof(apple->cluster1)); ++ frag->regs64[28] = apple->r_fd0; ++} ++ ++static void pvr_apple_t8010_prepare_geom(void *hw, struct pvr_job *job) ++{ ++ union pvr_apple_t8010_render_job *render = hw; ++ ++ pvr_apple_geom_job_init(&render->ta, job->cmd, job->hwrt); ++} ++ ++static void pvr_apple_t8010_prepare_frag(void *hw, struct pvr_job *job) ++{ ++ union pvr_apple_t8010_render_job *render = hw; ++ ++ pvr_apple_frag_job_init(&render->frag, job->cmd, job->hwrt); ++} ++ ++static void pvr_apple_t8010_write_geom(struct pvr_apple *apple, u32 seq, const void *hw) ++{ ++ pvr_apple_layout_write_ta_job(apple->heap, seq, hw); ++} ++ ++static void pvr_apple_t8010_write_frag(struct pvr_apple *apple, u32 seq, const void *hw) ++{ ++ pvr_apple_layout_write_3d_job(apple->heap, seq, hw); ++} ++ ++/* With debug flags set (apple_fw_debug): the trace ring's last records */ ++static void pvr_apple_t8010_report_hang(struct pvr_apple *apple) ++{ ++ if (heap_read32(apple, pvr_apple_layout_debug_flags_offset())) { ++ u32 next = heap_read32(apple, pvr_apple_layout_sku_index()); ++ unsigned int i; ++ ++ dev_err(apple_dev(apple), "hang: trace ring index %u\n", next); ++ for (i = next >= 24 ? next - 24 : 0; i < next; i++) ++ dev_err(apple_dev(apple), "hang: trace %u: %*ph\n", i, 36, ++ apple->heap + pvr_apple_layout_sku_record(i)); ++ } ++} ++ ++const struct pvr_apple_chip pvr_apple_chip_t8010 = { ++ .fw_name = "apple/gfx-t8010.bin", ++ .perf_state_max = FENDER_PERF_STATE_MAX, ++ .compute_slots = APPLE_JOB_SLOTS, ++ .render_job_size = sizeof(union pvr_apple_t8010_render_job), ++ .geom_stream = &pvr_apple_cmd_geom_stream, ++ .frag_stream = &pvr_apple_cmd_frag_stream, ++ .init = pvr_apple_t8010_init, ++ .boot = pvr_apple_t8010_boot, ++ .start = pvr_apple_t8010_start, ++ .stop = pvr_apple_t8010_stop, ++ .idle = pvr_apple_t8010_idle, ++ .restore_image = pvr_apple_t8010_restore_image, ++ .kick = pvr_apple_t8010_kick, ++ .write_compute = pvr_apple_t8010_write_compute, ++ .prepare_geom = pvr_apple_t8010_prepare_geom, ++ .prepare_frag = pvr_apple_t8010_prepare_frag, ++ .write_geom = pvr_apple_t8010_write_geom, ++ .write_frag = pvr_apple_t8010_write_frag, ++ .report_hang = pvr_apple_t8010_report_hang, ++}; +diff --git a/drivers/gpu/drm/imagination/pvr_device.h b/drivers/gpu/drm/imagination/pvr_device.h +index 41272c0..26b589e 100644 +--- a/drivers/gpu/drm/imagination/pvr_device.h ++++ b/drivers/gpu/drm/imagination/pvr_device.h +@@ -62,6 +62,8 @@ struct pvr_fw_version { + u16 major, minor; + }; + ++struct pvr_apple_chip; ++ + /** + * struct pvr_device_data - Platform specific data associated with a compatible string. + * @pwr_ops: Pointer to a structure with platform-specific power functions. +@@ -71,6 +73,9 @@ struct pvr_device_data { + + /** @apple_fw: GPU driven by Apple's GFX firmware (see pvr_apple.c). */ + bool apple_fw; ++ ++ /** @apple_chip: With @apple_fw, the chip's side of it (pvr_apple_chip.h). */ ++ const struct pvr_apple_chip *apple_chip; + }; + + struct pvr_apple; +diff --git a/drivers/gpu/drm/imagination/pvr_drv.c b/drivers/gpu/drm/imagination/pvr_drv.c +index 9eeb820..acc4979 100644 +--- a/drivers/gpu/drm/imagination/pvr_drv.c ++++ b/drivers/gpu/drm/imagination/pvr_drv.c +@@ -1512,6 +1512,7 @@ static const struct pvr_device_data pvr_device_data_pwrseq = { + static const struct pvr_device_data pvr_device_data_apple = { + .pwr_ops = &pvr_power_sequence_ops_apple, + .apple_fw = true, ++ .apple_chip = &pvr_apple_chip_t8010, + }; + #endif + +diff --git a/drivers/gpu/drm/imagination/pvr_job.c b/drivers/gpu/drm/imagination/pvr_job.c +index c8ac3c5..0336d25 100644 +--- a/drivers/gpu/drm/imagination/pvr_job.c ++++ b/drivers/gpu/drm/imagination/pvr_job.c +@@ -158,8 +158,9 @@ pvr_geom_job_fw_cmd_init(struct pvr_job *job, + + job->fw_ccb_cmd_type = ROGUE_FWIF_CCB_CMD_TYPE_GEOM; + err = pvr_fw_cmd_init(job->pvr_dev, job, +- pvr_device_is_apple(job->pvr_dev) ? &pvr_apple_cmd_geom_stream : +- &pvr_cmd_geom_stream, ++ pvr_device_is_apple(job->pvr_dev) ? ++ pvr_apple_cmd_stream(job->pvr_dev, DRM_PVR_JOB_TYPE_GEOMETRY) : ++ &pvr_cmd_geom_stream, + args->cmd_stream, args->cmd_stream_len); + if (err) + return err; +@@ -192,8 +193,9 @@ pvr_frag_job_fw_cmd_init(struct pvr_job *job, + ROGUE_FWIF_CCB_CMD_TYPE_FRAG_PR : + ROGUE_FWIF_CCB_CMD_TYPE_FRAG; + err = pvr_fw_cmd_init(job->pvr_dev, job, +- pvr_device_is_apple(job->pvr_dev) ? &pvr_apple_cmd_frag_stream : +- &pvr_cmd_frag_stream, ++ pvr_device_is_apple(job->pvr_dev) ? ++ pvr_apple_cmd_stream(job->pvr_dev, DRM_PVR_JOB_TYPE_FRAGMENT) : ++ &pvr_cmd_frag_stream, + args->cmd_stream, args->cmd_stream_len); + if (err) + return err; diff --git a/kernel/patches/drivers/gpu/0016-dt-bindings-gpu-Describe-the-Apple-S8003-A9-GPU.patch b/kernel/patches/drivers/gpu/0016-dt-bindings-gpu-Describe-the-Apple-S8003-A9-GPU.patch new file mode 100644 index 00000000..1c7eced5 --- /dev/null +++ b/kernel/patches/drivers/gpu/0016-dt-bindings-gpu-Describe-the-Apple-S8003-A9-GPU.patch @@ -0,0 +1,110 @@ +From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 +From: =?UTF-8?q?Berkay=20K=C3=B6sebay?= +Date: Tue, 10 Nov 2026 15:07:45 +0000 +Subject: [PATCH] dt-bindings: gpu: Describe the Apple S8003 (A9) GPU +MIME-Version: 1.0 +Content-Type: text/plain; charset=UTF-8 +Content-Transfer-Encoding: 8bit + +The A9's GPU: a PowerVR Rogue core behind Apple's Fender block, with +Apple's firmware on the core's own META processor. The compatible and +the register names follow the A10's node (apple,t8010-gpu: "rgx", +"fender"), so the Apple firmware path of the powervr driver can take +both; the A9 has interrupts where the A10 has a coprocessor and its +mailbox. + +From Berkay Kösebay's G5P driver (orchard #74, 557ef01b). Reworked for +the powervr driver: firmware-name and operating-points-v2 as on the A10, +the file named after the compatible. + +Assisted-by: LLM +--- + .../bindings/gpu/apple,s8003-gpu.yaml | 79 +++++++++++++++++++ + 1 file changed, 79 insertions(+) + create mode 100644 Documentation/devicetree/bindings/gpu/apple,s8003-gpu.yaml + +diff --git a/Documentation/devicetree/bindings/gpu/apple,s8003-gpu.yaml b/Documentation/devicetree/bindings/gpu/apple,s8003-gpu.yaml +new file mode 100644 +index 0000000..9539108 +--- /dev/null ++++ b/Documentation/devicetree/bindings/gpu/apple,s8003-gpu.yaml +@@ -0,0 +1,79 @@ ++# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) ++%YAML 1.2 ++--- ++$id: http://devicetree.org/schemas/gpu/apple,s8003-gpu.yaml# ++$schema: http://devicetree.org/meta-schemas/core.yaml# ++ ++title: Apple S8003 (A9) GPU ++ ++maintainers: ++ - Berkay Kösebay ++ ++description: | ++ The A9's GPU is a PowerVR Rogue core (GT7600, Apple's G5P) behind Apple's ++ "Fender" power and control block. Apple's firmware runs on the core's own ++ 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. ++ ++properties: ++ compatible: ++ const: apple,s8003-gpu ++ ++ reg: ++ items: ++ - description: The Rogue core's registers ++ - description: Fender ++ ++ reg-names: ++ items: ++ - const: rgx ++ - const: fender ++ ++ interrupts: ++ minItems: 5 ++ maxItems: 5 ++ ++ power-domains: ++ maxItems: 1 ++ ++ operating-points-v2: true ++ ++ firmware-name: ++ maxItems: 1 ++ description: ++ The firmware file, Apple's META LDR image from the device's own ++ kernelcache. ++ ++required: ++ - compatible ++ - reg ++ - reg-names ++ - interrupts ++ - power-domains ++ ++additionalProperties: false ++ ++examples: ++ - | ++ #include ++ #include ++ ++ soc { ++ #address-cells = <2>; ++ #size-cells = <2>; ++ ++ gpu@201000000 { ++ compatible = "apple,s8003-gpu"; ++ reg = <0x2 0x01000000 0x0 0x100000>, ++ <0x2 0x01e00000 0x0 0x100000>; ++ reg-names = "rgx", "fender"; ++ power-domains = <&ps_gfx>; ++ interrupt-parent = <&aic>; ++ interrupts = , ++ , ++ , ++ , ++ ; ++ firmware-name = "apple/gfx-s8003.bin"; ++ }; ++ }; diff --git a/src/orchard/apple/agx.py b/src/orchard/apple/agx.py index 3043b94a..4f5f79ac 100644 --- a/src/orchard/apple/agx.py +++ b/src/orchard/apple/agx.py @@ -1,12 +1,18 @@ -"""Extract the A10 GPU firmware from a legacy prelinked kernelcache. +"""Extract Apple GPU firmware from a legacy prelinked kernelcache. -AGXFirmwareKextG9PRTBuddy keeps the image in __TEXT.__const: its build +A10 (G9P): AGXFirmwareKextG9PRTBuddy keeps the image in __TEXT.__const: its build variant as a C string ("Release"), then the image to the end of the section, an ARM32 binary loaded at address 0 whose first word is the reset vector's branch. The kext's code reaches it with PC-relative addresses, so there is no pointer record to follow; the section layout is checked instead, and the caller pins the image's SHA-256. + +A9 (G5P): AGXFirmwareKextG5P's __TEXT.__const is the whole firmware, a META +LDR command stream the driver loads into the GPU's own processor. The LDR has +no header to check here, and the driver's firmware layout belongs to one +image, so its size and SHA-256 are pinned: the C0 image of iPadOS 16.7.10. """ +import hashlib import re from orchard.apple.prelink import Kernelcache, named @@ -15,6 +21,10 @@ CONST = ('__TEXT', '__const') VARIANT = re.compile(rb'\0*[\x20-\x7e]+\0') +G5P = 'com.apple.AGXFirmwareKextG5P' +G5P_SIZE = 119112 +G5P_SHA256 = '33ae20cbbe2498e4f119ba4e9a55467e4e0192d4c86d5bf1903adce63ffa5dca' + def extract(blob: bytes) -> bytes: kernel = Kernelcache(blob) @@ -28,3 +38,14 @@ def extract(blob: bytes) -> bytes: if len(image) < 4096 or len(image) % 4 or image[3] != 0xea: raise ValueError('The GPU firmware does not start with a reset vector') return image + + +def extract_g5p(blob: bytes) -> bytes: + kernel = Kernelcache(blob) + section = named(kernel.kext_sections(G5P, 'G5P firmware'), CONST)[CONST] + if section['size'] != G5P_SIZE: + raise ValueError('Unsupported G5P C0 firmware size') + data = kernel.read(section['addr'], section['size']) + if hashlib.sha256(data).hexdigest() != G5P_SHA256: + raise ValueError('Unsupported G5P C0 firmware digest') + return data diff --git a/src/orchard/recipe.py b/src/orchard/recipe.py index 74b73102..009639a0 100644 --- a/src/orchard/recipe.py +++ b/src/orchard/recipe.py @@ -47,6 +47,10 @@ def _agx(step, data, expected): return agx.extract(data) +def _agx_g5p(step, data, expected): + return agx.extract_g5p(data) + + def _im4p_payload(step, data, expected): return im4p_payload(data) @@ -112,6 +116,7 @@ def _z2fw_mtfw(step, data, expected): 'vxd-firmware-from-kernelcache@1': (_vxd, {}), # A10 GPU firmware from AGXFirmwareKextG9PRTBuddy. 'agx-g9p-firmware-from-kernelcache@1': (_agx, {}), + 'agx-g5p-ldr-from-kernelcache@1': (_agx_g5p, {}), 'im4p-payload@1': (_im4p_payload, {}), # One image of an AOP firmware container (IPSW Firmware/AOP/aopfw-*.im4p payload). 'aop-image@1': (_aop_image, {'coprocessor': str}), diff --git a/tests/test_agx_g5p.py b/tests/test_agx_g5p.py new file mode 100644 index 00000000..5605e92e --- /dev/null +++ b/tests/test_agx_g5p.py @@ -0,0 +1,46 @@ +import hashlib +import json +from pathlib import Path +import unittest +from unittest.mock import Mock, patch + +from orchard.apple import agx + +ROOT = Path(__file__).resolve().parents[1] + + +class G5PFirmware(unittest.TestCase): + def test_original_bytes_and_bundle(self): + data = b'synthetic firmware, not Apple data' + kernel = Mock() + kernel.kext_sections.return_value = [dict(segment='__TEXT', name='__const', addr=4096, size=len(data))] + kernel.read.return_value = data + with patch.object(agx, 'Kernelcache', return_value=kernel), patch.object(agx, 'G5P_SIZE', len(data)), \ + patch.object(agx, 'G5P_SHA256', hashlib.sha256(data).hexdigest()): + self.assertIs(agx.extract_g5p(b'input'), data) + kernel.kext_sections.assert_called_once_with(agx.G5P, 'G5P firmware') + kernel.read.assert_called_once_with(4096, len(data)) + + def test_wrong_size_is_rejected_before_read(self): + kernel = Mock() + kernel.kext_sections.return_value = [dict(segment='__TEXT', name='__const', addr=4096, size=1)] + with patch.object(agx, 'Kernelcache', return_value=kernel), self.assertRaisesRegex(ValueError, 'size'): + agx.extract_g5p(b'input') + kernel.read.assert_not_called() + + def test_wrong_digest_is_rejected(self): + kernel = Mock() + kernel.kext_sections.return_value = [dict(segment='__TEXT', name='__const', addr=4096, size=agx.G5P_SIZE)] + kernel.read.return_value = bytes(agx.G5P_SIZE) + with patch.object(agx, 'Kernelcache', return_value=kernel), self.assertRaisesRegex(ValueError, 'digest'): + agx.extract_g5p(b'input') + + def test_j71t_recipe_writes_the_pinned_image(self): + recipe = json.loads((ROOT / 'devices/j71t/firmware.json').read_text()) + output, = [o for o in recipe['outputs'] if o['destination'] == 'lib/firmware/apple/gfx-s8003.bin'] + self.assertEqual(output['sha256'], agx.G5P_SHA256) + self.assertEqual(output['steps'], [{'step': 'agx-g5p-ldr-from-kernelcache@1', 'input': 'kernelcache'}]) + + +if __name__ == '__main__': + unittest.main() diff --git a/tests/test_recipe.py b/tests/test_recipe.py index 756a3f5e..f8b6dff8 100644 --- a/tests/test_recipe.py +++ b/tests/test_recipe.py @@ -79,6 +79,9 @@ def test_vxd_firmware_step_is_known(self): def test_a10_gpu_firmware_step_is_known(self): recipe.check(sample([{'step': 'agx-g9p-firmware-from-kernelcache@1', 'input': 'blob'}])) + def test_a9_gpu_firmware_step_is_known(self): + recipe.check(sample([{'step': 'agx-g5p-ldr-from-kernelcache@1', 'input': 'blob'}])) + def test_inputs_and_outputs_are_hash_checked(self): document = recipe.check(sample([{'step': 'copy@1', 'input': 'blob'}])) self.assertEqual(recipe.run(document, lambda name: b'firmware'),