diff --git a/Documentation/devicetree/bindings/gpu/apple,t8020-agx.yaml b/Documentation/devicetree/bindings/gpu/apple,t8020-agx.yaml index fd5dfacf968291..f0bad579ba041a 100644 --- a/Documentation/devicetree/bindings/gpu/apple,t8020-agx.yaml +++ b/Documentation/devicetree/bindings/gpu/apple,t8020-agx.yaml @@ -46,6 +46,14 @@ properties: The firmware's segments as Apple's device tree lists them (32 bytes each: physical address, address in the firmware's space, size). + apple,firmware-build: + description: + The GPU firmware build the board's boot loader is given, for when the + driver cannot recognise the loaded firmware itself. + enum: + - 20g81 + - 21b91 + apple,g11-core-perf-states: $ref: /schemas/types.yaml#/definitions/uint32-array description: The GPU's performance states, frequency and voltage pairs. diff --git a/arch/arm64/boot/dts/apple/t8020-ipad8.dtsi b/arch/arm64/boot/dts/apple/t8020-ipad8.dtsi index 5933360243ed5e..1c78f7a1dd18f2 100644 --- a/arch/arm64/boot/dts/apple/t8020-ipad8.dtsi +++ b/arch/arm64/boot/dts/apple/t8020-ipad8.dtsi @@ -263,3 +263,12 @@ dmas = <&sio 0x2c>; dma-names = "tx1a"; }; + +/* + * The GPU firmware the iPad 8 is given is 17.1.1's (21B91), the build + * dr1408's backend fits; the driver recognises it from the firmware, and + * falls back to this. + */ +&gfx_asc { + apple,firmware-build = "21b91"; +}; diff --git a/drivers/gpu/drm/agx/Kconfig b/drivers/gpu/drm/agx/Kconfig index 51f73f219004ab..3a75b930b58530 100644 --- a/drivers/gpu/drm/agx/Kconfig +++ b/drivers/gpu/drm/agx/Kconfig @@ -4,6 +4,7 @@ config DRM_AGX depends on DRM && ARCH_APPLE && APPLE_RTKIT select DRM_GEM_SHMEM_HELPER select DRM_SCHED + select CRYPTO_LIB_SHA256 help Compute-only DRM driver for the GPU in the Apple A12 (T8020). It boots the GPU firmware and runs compute jobs through the Asahi uAPI. diff --git a/drivers/gpu/drm/agx/agx.h b/drivers/gpu/drm/agx/agx.h index 535b6790e02076..0e27ef58826531 100644 --- a/drivers/gpu/drm/agx/agx.h +++ b/drivers/gpu/drm/agx/agx.h @@ -70,8 +70,10 @@ struct agx_device { struct { u32 cmd, evc, useq, scratch; } slot[4]; /* Render: one job's commands and the buffer manager, see agx_fw_render(). */ struct { - u32 bmd, bms, init, ta, frag, scratch, tilemap, tpc, heapmeta, pre; - bool frag_pending; + u32 bmd, bms, init, barrier, ta, frag, scratch, tilemap, tpc, heapmeta, + pre; + /* last stamp values, see agx_fw_render() */ + u32 ta_stamp, frag_stamp; } rn; bool wedged; @@ -80,7 +82,7 @@ struct agx_device { spinlock_t fence_lock; u64 fence_ctx, fence_seqno; struct dma_fence *active; - u32 active_stamp; + u32 active_stamp, active_value; struct mutex ctx_lock; unsigned long ctx_used[BITS_TO_LONGS(AGX_NR_CTX)]; diff --git a/drivers/gpu/drm/agx/agx_drv.c b/drivers/gpu/drm/agx/agx_drv.c index d3df6018fce23f..43bfbce64be549 100644 --- a/drivers/gpu/drm/agx/agx_drv.c +++ b/drivers/gpu/drm/agx/agx_drv.c @@ -23,10 +23,12 @@ #include #include #include +#include #include #include #include #include +#include #include #include #include @@ -759,9 +761,81 @@ struct agx_device *agx21_asahi_register(struct platform_device *pdev, return agx; } -static char *agx_firmware = "21b91"; +static char *agx_firmware = ""; module_param_named(firmware, agx_firmware, charp, 0400); -MODULE_PARM_DESC(firmware, "GPU firmware build iBoot loads: 21b91 (17.1.1) or 20g81 (16.6.1)"); +MODULE_PARM_DESC(firmware, "GPU firmware build iBoot loaded: 20g81 (16.6.1) or 21b91 (17.1.1); default: recognised from the firmware"); + +/* + * The GPU firmware builds this driver speaks, by the SHA-256 of their + * __TEXT segment (0x44000 bytes) as iBoot loads it: armfw_g11p's Mach-O, + * after its rkosftab header. Each is the same file on every A12 board that + * has the build (20G81: the iPhone XS, XS Max and XR; 21B91: the XR and + * iPad 8). + */ +#define AGX_FW_TEXT_SIZE 0x44000 +static const struct { + const char *build; + u8 text[SHA256_DIGEST_SIZE]; +} agx_known_firmware[] = { + { "20g81", { 0x80, 0x47, 0x72, 0x3b, 0xc6, 0xdb, 0x37, 0x48, + 0x9d, 0xc7, 0xf7, 0x50, 0xc5, 0xdc, 0x44, 0xf3, + 0xf5, 0xef, 0xa8, 0x26, 0xb3, 0xbb, 0xd7, 0xe9, + 0x38, 0x1b, 0x77, 0x35, 0x96, 0xaa, 0x83, 0xa0 } }, + { "21b91", { 0xc6, 0x3d, 0x55, 0xb5, 0xa3, 0x11, 0x33, 0xfd, + 0x9f, 0x18, 0x19, 0x47, 0x1d, 0x97, 0x50, 0x41, + 0x9c, 0x68, 0x4c, 0x57, 0x6c, 0x9b, 0xb0, 0x93, + 0xa9, 0x1f, 0x7f, 0xa3, 0x72, 0xbe, 0xd9, 0x3d } }, +}; + +/* One entry of apple,segment-ranges: {phys, iova, remap, size, flags}. */ +struct agx_segment_range { + __le64 phys; + __le64 iova; + __le64 remap; + __le32 size; + __le32 flags; +} __packed; + +/* + * The build of the firmware iBoot loaded: from the module parameter, else + * recognised by its __TEXT segment, else the board's apple,firmware-build, + * else 20g81, the build badapple runs on the XS Max. + */ +static const char *agx_firmware_build(struct device *dev) +{ + const struct agx_segment_range *seg; + const char *build; + u8 digest[SHA256_DIGEST_SIZE]; + int len, i, j; + + if (agx_firmware[0]) + return agx_firmware; + seg = of_get_property(dev->of_node, "apple,segment-ranges", &len); + for (i = 0; seg && i < len / (int)sizeof(*seg); i++) { + void *text; + + if (le32_to_cpu(seg[i].size) != AGX_FW_TEXT_SIZE || + le64_to_cpu(seg[i].iova)) + continue; + /* iBoot loaded it and m1n1 reserved it; nothing runs it yet */ + text = memremap(le64_to_cpu(seg[i].phys), AGX_FW_TEXT_SIZE, MEMREMAP_WB); + if (!text) + continue; + sha256(text, AGX_FW_TEXT_SIZE, digest); + memunmap(text); + for (j = 0; j < ARRAY_SIZE(agx_known_firmware); j++) { + if (!memcmp(digest, agx_known_firmware[j].text, sizeof(digest))) { + dev_info(dev, "GPU firmware %s\n", agx_known_firmware[j].build); + return agx_known_firmware[j].build; + } + } + dev_warn(dev, "GPU firmware not one this driver knows (__TEXT %*phN)\n", + 8, digest); + } + if (!of_property_read_string(dev->of_node, "apple,firmware-build", &build)) + return build; + return "20g81"; +} static int agx_probe(struct platform_device *pdev) { @@ -769,19 +843,18 @@ static int agx_probe(struct platform_device *pdev) struct agx_device *agx; struct resource *res; void __iomem *asc; + const char *build; int len, ret; + build = agx_firmware_build(dev); /* - * Which firmware build iBoot was given decides the ABI. Orchard hands - * every A12 board the 17.1.1 (21B91) GPU firmware, the build the 21B91 - * backend fits; the XS Max's 16.6.1 (20G81) layout below is kept for - * that firmware and taken with agx.firmware=20g81. + * The firmware build iBoot loaded decides the ABI: badapple's 16.6.1 + * (20G81) layout below, or dr1408's 17.1.1 (21B91) backend. */ - if (!strcmp(agx_firmware, "21b91")) + if (!strcmp(build, "21b91")) return agx21_transport_probe(pdev); - if (strcmp(agx_firmware, "20g81")) - return dev_err_probe(dev, -EINVAL, "unknown firmware build %s\n", - agx_firmware); + if (strcmp(build, "20g81")) + return dev_err_probe(dev, -EINVAL, "unknown firmware build %s\n", build); agx = devm_drm_dev_alloc(dev, &agx_drm_driver, struct agx_device, drm); if (IS_ERR(agx)) diff --git a/drivers/gpu/drm/agx/agx_fw.c b/drivers/gpu/drm/agx/agx_fw.c index d203582df88442..dcd65eba1b9302 100644 --- a/drivers/gpu/drm/agx/agx_fw.c +++ b/drivers/gpu/drm/agx/agx_fw.c @@ -10,13 +10,11 @@ * Copyright (c) 2026 kiki */ -#include #include #include #include #include #include -#include #include #include #include @@ -26,6 +24,15 @@ #include "agx-21b91-layout.h" #include "agx_21b91_memory.h" +/* + * Nothing throttles the GPU for heat: the firmware's thermal and PPM limits + * are pinned below. The top pstate, 1098 MHz, gets the phone hot under a + * long load, so the default stops at 740 MHz, about half the power. + */ +static unsigned int max_pstate = 4; +module_param(max_pstate, uint, 0444); +MODULE_PARM_DESC(max_pstate, "highest GPU pstate, 1 (316 MHz) to 6 (1098 MHz)"); + #define EP_MSGS 0x20 #define EP_INTS 0x21 #define MSG_INIT (0x81ULL << 48) @@ -51,6 +58,13 @@ #define EV_3D 7 #define RGX_MMU_FAULT_STATUS 0x17040 #define DONE_STAMP 0xcafe0003 +/* + * Render stamps count up by 0x100 per job, as Apple's do: bits 14:8 are the + * job's slot in the firmware's table for its event, which must come in + * order (FUN_0001d8d8), and a barrier waits while target - stamp is + * positive as a signed int. + */ +#define STAMP_STEP 0x100 /* One entry of the ADT's segment-ranges. */ struct asc_segment { @@ -132,7 +146,7 @@ static void agx_build(struct agx_device *agx, void *init, void *table, dma_addr_ { 8, 10, 10, 21, 0x800, 1, 0xfffffffc00, 0x3fe00000 }, { 8, 12, 12, 14, 0x200, 1, 0xfffffff000, 0x1fc000 }, }; - u32 B, C, st, hb, ha, ttbr; + u32 B, C, st, hb, ha, ttbr, top; int i, j; B = agx->B = agx_alloc(agx, 0x8000, 0x4000); /* RegionB */ @@ -187,11 +201,11 @@ static void agx_build(struct agx_device *agx, void *init, void *table, dma_addr_ * 100 the firmware duty-cycles the GPU and at 0 parks every job. * +0x770 and +0x820 pin the thermal and PPM limits. */ + top = clamp(max_pstate, 1U, (u32)ARRAY_SIZE(mhz) - 1); for (i = 0; i < ARRAY_SIZE(limits); i++) - put32(agx, (limits[i] > 0x2000 ? C : ha) + limits[i], - (ARRAY_SIZE(mhz) - 1) * 100); + put32(agx, (limits[i] > 0x2000 ? C : ha) + limits[i], top * 100); /* Highest pstate; the DVFS loop in FUN_00020b44 walks down from it. */ - put32(agx, hb + 0xa6c, ARRAY_SIZE(mhz) - 1); + put32(agx, hb + 0xa6c, top); /* VA bases; power-up loads +0x18 into RGX 0x10000. */ put64(agx, hb, 0x1400000000); put64(agx, hb + 8, 0xffc00000); @@ -250,6 +264,11 @@ static void agx_build(struct agx_device *agx, void *init, void *table, dma_addr_ put32(agx, C, 0x1); /* The first power-up installs the job handlers only when set. */ put32(agx, C + 0xb1cc, 1); + /* + * While a barrier waits, the scheduler only looks again when a channel + * has a new message, unless this is set (FUN_000075fc, FUN_0000e048). + */ + put32(agx, C + 0x8970, 1); /* Mem-desc table: va, pa >> 12, 0, 16K pages, flags */ put_unaligned_le64(AGX_CHUNK_VA, table); @@ -475,6 +494,7 @@ struct dma_fence *agx_fw_run(struct agx_device *agx, u32 ctx, u64 args, u64 stre spin_lock_irq(&agx->fence_lock); agx->active = dma_fence_get(fence); agx->active_stamp = scratch + 0x10; + agx->active_value = DONE_STAMP; spin_unlock_irq(&agx->fence_lock); agx->nr_cmds++; @@ -491,9 +511,12 @@ struct dma_fence *agx_fw_run(struct agx_device *agx, u32 ctx, u64 args, u64 stre * AGX_CHUNK_GVA. Layouts: kext FUN_fffffff0084d22c8 (TA registers), * FUN_fffffff0084cd07c (3D registers), firmware FUN_00017018 (microsequence). */ -#define RN_MAX 16384 -/* The tiling buffers fit 16384x4096 worth of macrotiles, in any shape */ -#define RN_BUDGET_W 16384 +#define RN_MAX 8192 +/* + * The tiling buffers fit 8192x4096 worth of macrotiles, in any shape. Twice + * that (16384x4096, 8192x8192) wedges the firmware, so such jobs fail here. + */ +#define RN_BUDGET_W 8192 #define RN_BUDGET_H 4096 #define RN_BLOCKS 64 /* parameter heap, 128K blocks */ #define RN_MAX_BLOCKS 128 @@ -538,6 +561,7 @@ static int agx_render_init(struct agx_device *agx) agx->rn.frag = agx_alloc(agx, 0xd00, 0x100); /* cmd, evc +0xa00, useq +0xb00 */ agx->rn.scratch = agx_alloc(agx, 0x100, 0x100); agx->rn.init = agx_alloc(agx, 0x40, 0x40); + agx->rn.barrier = agx_alloc(agx, 0x40, 0x40); agx->rn.tilemap = agx_alloc(agx, t.rgn_pages * SZ_4K, AGX_PAGE); agx->rn.tpc = agx_alloc(agx, t.tpc_pages * SZ_4K, AGX_PAGE); agx->rn.heapmeta = agx_alloc(agx, AGX_PAGE, AGX_PAGE); @@ -739,7 +763,8 @@ static void rn_ta(struct agx_device *agx, u32 ctx, const struct drm_asahi_cmd_re /* stamp record {target, ?, value, event}, retired by FUN_0000d704 */ put64(agx, cmd + 0x560, fw_va(sc)); put64(agx, cmd + 0x568, fw_va(sc)); - put32(agx, cmd + 0x570, DONE_STAMP); + /* the 21B91 path patches its completion itself, agx21_render_rewrite() */ + put32(agx, cmd + 0x570, agx->ipad_memory ? DONE_STAMP : agx->rn.ta_stamp); *(u8 *)(agx->buf + cmd + 0x574) = EV_TA; put32(agx, cmd + 0x580, EV_TA); @@ -834,7 +859,7 @@ static void rn_frag(struct agx_device *agx, u32 ctx, const struct drm_asahi_cmd_ put64(agx, evc, fw_va(sc + 0x48)); put64(agx, cmd + 0x8c4, fw_va(sc + 0x40)); put64(agx, cmd + 0x8cc, fw_va(sc + 0x40)); - put32(agx, cmd + 0x8d4, DONE_STAMP); + put32(agx, cmd + 0x8d4, agx->ipad_memory ? DONE_STAMP : agx->rn.frag_stamp); *(u8 *)(agx->buf + cmd + 0x8d8) = EV_3D; put32(agx, cmd + 0x8e4, EV_3D); @@ -1020,15 +1045,15 @@ static int agx21_render_rewrite(struct agx_device *agx, u32 ctx, u32 stamp) } /* - * Queue the TA job behind an InitBuffer. The 3D job waits in its ring until - * agx_complete() sees the TA stamp. + * Queue the TA job behind an InitBuffer, and the 3D job behind a barrier on + * the TA's stamp, which the firmware resolves itself. */ struct dma_fence *agx_fw_render(struct agx_device *agx, u32 ctx, const struct drm_asahi_cmd_render *r) { struct dma_fence *fence; struct rn_tiling t; - u32 sc = agx->rn.scratch, init = agx->rn.init; + u32 sc = agx->rn.scratch, init = agx->rn.init, bar = agx->rn.barrier; u32 render_stamp = DONE_STAMP; u64 utile = (r->utile_width_px / 16) << 12 | (r->utile_height_px / 16) << 14 | ilog2(r->samples); @@ -1058,7 +1083,16 @@ struct dma_fence *agx_fw_render(struct agx_device *agx, u32 ctx, kfree(fence); return ERR_PTR(-EOPNOTSUPP); } - memset(agx->buf + sc, 0, 0x100); + if (agx->ipad_memory) { + /* The 21B91 path rewrites the stamps itself, agx21_render_rewrite() */ + memset(agx->buf + sc, 0, 0x100); + } else { + agx->rn.ta_stamp += STAMP_STEP; + agx->rn.frag_stamp += STAMP_STEP; + /* everything but the stamps at +0 and +0x40, which only count up */ + memset(agx->buf + sc + 8, 0, 0x38); + memset(agx->buf + sc + 0x48, 0, 0xb8); + } put64(agx, sc + 0x20, 1); put64(agx, sc + 0x60, 1); if (agx->ipad_memory) @@ -1091,7 +1125,7 @@ struct dma_fence *agx_fw_render(struct agx_device *agx, u32 ctx, spin_lock_irq(&agx->fence_lock); agx->active = dma_fence_get(fence); agx->active_stamp = sc + 0x40; - agx->rn.frag_pending = true; + agx->active_value = agx->rn.frag_stamp; spin_unlock_irq(&agx->fence_lock); /* @@ -1102,39 +1136,47 @@ struct dma_fence *agx_fw_render(struct agx_device *agx, u32 ctx, put32(agx, init, 6); put32(agx, init + 4, ctx); put64(agx, init + 0x14, fw_va(agx->rn.bmd)); - put32(agx, init + 0x1c, DONE_STAMP); + put32(agx, init + 0x1c, agx->rn.ta_stamp); agx_push(agx, &agx->taq, init); agx_push(agx, &agx->taq, agx->rn.ta); + + /* + * Barrier {4, u64 stamp, target, event slot waited on, this queue's next + * stamp, uuid, type}: the stamp's bits 14:8 pick the job slot on each + * side (FUN_0001fb60). Type 0 links the jobs in the firmware's DAG, + * which never released the 3D job. Other types have the scheduler poll + * the stamp before starting the job (FUN_0001def8), see RegionC +0x8970. + */ + memset(agx->buf + bar, 0, 0x40); + put32(agx, bar, 4); + put64(agx, bar + 0x4, fw_va(sc)); + put32(agx, bar + 0xc, agx->rn.ta_stamp); + put32(agx, bar + 0x10, EV_TA); + put32(agx, bar + 0x14, agx->rn.frag_stamp); + put32(agx, bar + 0x1c, 1); + agx_push(agx, &agx->fragq, bar); agx_push(agx, &agx->fragq, agx->rn.frag); + agx_kick(agx, &agx->taq, CH_TA, EV_TA); + agx_kick(agx, &agx->fragq, CH_3D, EV_3D); return fence; } -/* - * Signal the running job's fence if its stamp has landed, or kick a - * render's 3D job once its TA job is done. - */ +/* Signal the running job's fence if its stamp has landed. */ static bool agx_complete(struct agx_device *agx) { struct dma_fence *fence = NULL; unsigned long flags; - bool kick = false; spin_lock_irqsave(&agx->fence_lock, flags); - if (agx->rn.frag_pending && - READ_ONCE(*(u32 *)(agx->buf + agx->rn.scratch)) == DONE_STAMP) { - agx->rn.frag_pending = false; - kick = true; - } else if (agx->active && - READ_ONCE(*(u32 *)(agx->buf + agx->active_stamp)) == DONE_STAMP) { + if (agx->active && + READ_ONCE(*(u32 *)(agx->buf + agx->active_stamp)) == agx->active_value) { dma_rmb(); fence = agx->active; agx->active = NULL; dma_fence_signal_locked(fence); } spin_unlock_irqrestore(&agx->fence_lock, flags); - if (kick) - agx_kick(agx, &agx->fragq, CH_3D, EV_3D); if (fence) dma_fence_put(fence); return fence; @@ -1194,12 +1236,15 @@ void agx_fw_timeout(struct agx_device *agx) "job timed out, GPU wedged; MMU fault %#llx: VA %#llx ctx %llu requester %llu %s type %llu\n", st, (st >> 24) & ~0x3fULL, (st >> 16) & 0x3f, (st >> 8) & 0xff, st & BIT(4) ? "read" : "write", (st >> 1) & 7); - if (agx->rn.frag_pending) + if (agx->active_stamp == agx->rn.scratch + 0x40 && + READ_ONCE(*(u32 *)(agx->buf + agx->rn.scratch)) != agx->rn.ta_stamp) dev_err(agx->dev, "render stuck in its TA job\n"); + dev_err(agx->dev, "stamps: ta %#x of %#x, 3d %#x of %#x\n", + get32(agx, agx->rn.scratch), agx->rn.ta_stamp, + get32(agx, agx->rn.scratch + 0x40), agx->rn.frag_stamp); agx_dump_trace(agx); agx->wedged = true; spin_lock_irq(&agx->fence_lock); - agx->rn.frag_pending = false; fence = agx->active; agx->active = NULL; if (fence) { @@ -1274,30 +1319,6 @@ const struct apple_rtkit_ops agx_rtk_ops = { .shmem_destroy = agx_shmem_destroy, }; -static int agx_dbg_show(struct seq_file *m, void *v) -{ - struct agx_device *agx = m->private; - int i; - - seq_printf(m, "pa %pad va %#llx gva %#llx size %#x free %#x\n", &agx->pa, - AGX_CHUNK_VA, AGX_CHUNK_GVA, AGX_CHUNK_SIZE, agx->off); - seq_printf(m, "B %#x C %#x info %#x\n", agx->B, agx->C, agx->cpq.info); - for (i = 0; i < ARRAY_SIZE(agx->chan_state); i++) - seq_printf(m, "chan %d state %#x ring %#x\n", i, agx->chan_state[i], - agx->chan_ring[i]); - seq_printf(m, "ctx %*pb\n", AGX_NR_CTX, agx->ctx_used); - seq_printf(m, "rn ta %#x frag %#x\n", agx->rn.ta, agx->rn.frag); - return 0; -} -DEFINE_SHOW_ATTRIBUTE(agx_dbg); - -static void agx_debugfs(struct agx_device *agx) -{ - struct dentry *d = debugfs_create_dir("agx", NULL); - - debugfs_create_file("info", 0400, d, agx, &agx_dbg_fops); -} - /* * Boot the firmware, hand it the init block and bring the GPU up. The * firmware cannot be restarted, so this runs once per boot. @@ -1374,7 +1395,6 @@ int agx_fw_init(struct agx_device *agx, void __iomem *asc) ret = agx_render_init(agx); if (ret) return ret; - agx_debugfs(agx); dev_info(dev, "firmware up, chunk at %pad\n", &agx->pa); return 0; }