From 4cce5434d0fcbb08e08cd764f9d2fc3c64fde820 Mon Sep 17 00:00:00 2001 From: Thomas Glanzmann Date: Fri, 13 Nov 2026 09:54:10 +0100 Subject: [PATCH 01/29] soc: apple: rtkit: let clients register adopted endpoints A co-processor adopted from the bootloader has already sent its endpoint map, to the bootloader, so the application endpoints a client starts on it by hand are not in rtk->endpoints. Every message on them is then logged as one to an undiscovered endpoint. The T8152 DCP driver would get one per SET_SURFACE, about 60 lines a second during video. Add apple_rtkit_adopt_ep() for a client to register an application endpoint of such a session. Signed-off-by: Thomas Glanzmann --- drivers/soc/apple/rtkit.c | 12 ++++++++++++ include/linux/soc/apple/rtkit.h | 10 ++++++++++ 2 files changed, 22 insertions(+) diff --git a/drivers/soc/apple/rtkit.c b/drivers/soc/apple/rtkit.c index e529b4d4dd4c6..e516f9d6cb6a9 100644 --- a/drivers/soc/apple/rtkit.c +++ b/drivers/soc/apple/rtkit.c @@ -2125,6 +2125,18 @@ bool apple_rtkit_v27_rx_drained(struct apple_rtkit *rtk) } EXPORT_SYMBOL_GPL(apple_rtkit_v27_rx_drained); +/* + * A co-processor adopted from the bootloader has already sent its endpoint + * map. A client that starts one of its application endpoints by hand + * registers it here, so its messages are not reported as undiscovered. + */ +void apple_rtkit_adopt_ep(struct apple_rtkit *rtk, u8 ep) +{ + if (ep >= APPLE_RTKIT_APP_ENDPOINT_START) + set_bit(ep, rtk->endpoints); +} +EXPORT_SYMBOL_GPL(apple_rtkit_adopt_ep); + int apple_rtkit_start_ep(struct apple_rtkit *rtk, u8 endpoint) { u64 msg; diff --git a/include/linux/soc/apple/rtkit.h b/include/linux/soc/apple/rtkit.h index 4db916bb89f09..c76e50bd67d59 100644 --- a/include/linux/soc/apple/rtkit.h +++ b/include/linux/soc/apple/rtkit.h @@ -512,4 +512,14 @@ struct apple_rtkit *devm_apple_rtkit_init_m5_saved(struct device *dev, void *coo const char *mbox_name, int mbox_idx, const struct apple_rtkit_ops *ops); +/* + * Registers an application endpoint the client started itself on a + * co-processor whose endpoint map the bootloader consumed, so that its + * messages are not reported as undiscovered + * + * @rtk: RTKit reference + * @ep: endpoint to register + */ +void apple_rtkit_adopt_ep(struct apple_rtkit *rtk, u8 ep); + #endif /* _LINUX_APPLE_RTKIT_H_ */ From 36b6071305bb7a9c5a62d52afa0617dfd9d46b9b Mon Sep 17 00:00:00 2001 From: Thomas Glanzmann Date: Fri, 13 Nov 2026 09:55:49 +0100 Subject: [PATCH 02/29] dt-bindings: iommu: apple,dart: add the T8152 Gen3 display DARTs The DARTs in front of the T8152 display coprocessors (DCP, DCPEXT) and their display pipes (ADT "dart,gen3", version 0x300) use the T8110 register layout, but they belong to the SPTM, and the bootloader hands them over locked. Their TLB command register at 0x80 answers translation queries instead of flushing. A stream is invalidated the way iBoot does it: a payload naming the DART and the stream is written to the SoC's TLB invalidation aperture, which these DARTs share, and a broadcast TLBI follows. Add apple,t8152-dart-gen3 for them, without the apple,t8110-dart fallback, since a T8110 driver would flush through the wrong register, and a "tlbi" register entry for the DART's window in the aperture. Signed-off-by: Thomas Glanzmann --- .../devicetree/bindings/iommu/apple,dart.yaml | 37 ++++++++++++++++++- 1 file changed, 35 insertions(+), 2 deletions(-) diff --git a/Documentation/devicetree/bindings/iommu/apple,dart.yaml b/Documentation/devicetree/bindings/iommu/apple,dart.yaml index 034e70bdc6558..d7e8e332951ad 100644 --- a/Documentation/devicetree/bindings/iommu/apple,dart.yaml +++ b/Documentation/devicetree/bindings/iommu/apple,dart.yaml @@ -32,6 +32,7 @@ properties: - apple,t8103-usb4-dart - apple,t8110-dart - apple,t6000-dart + - apple,t8152-dart-gen3 - items: - const: apple,t8140-ane-dart - const: apple,t8140-dart @@ -87,13 +88,16 @@ properties: fabric filter (GAPF) that protects the power state of the coprocessor CPU behind this DART, cleared to power that CPU down, or the shared PS write-disable register that holds this DART's - bit for the same purpose (T8142) + bit for the same purpose (T8142), or on a T8152 Gen3 DART its + window in the SoC's TLB invalidation aperture (ADT + dart-tlbi-aperture plus tlbi-aperture-offset), which the Gen3 + DARTs share reg-names: minItems: 1 items: - const: dart - - enum: [dapf, gapf, ps-wr-dis, auxiliary] + - enum: [dapf, gapf, ps-wr-dis, auxiliary, tlbi] interrupts: maxItems: 1 @@ -361,6 +365,35 @@ allOf: then: required: - reg-names + - if: + properties: + compatible: + contains: + const: apple,t8152-dart-gen3 + then: + description: + The T8152 Gen3 DARTs in front of the display coprocessors and their + display pipes belong to the SPTM. The bootloader hands them over + locked, and the operating system only adds page table entries in + root slots that are empty at handoff. Their TLB command register + answers translation queries; a stream is invalidated through the + TLB invalidation aperture instead. + properties: + reg: + minItems: 2 + reg-names: + items: + - const: dart + - const: tlbi + required: + - reg-names + else: + properties: + reg-names: + minItems: 1 + items: + - const: dart + - enum: [dapf, gapf, ps-wr-dis, auxiliary] additionalProperties: false From 2ac0620b46399f918c15e8a1ed90adcb8587e12e Mon Sep 17 00:00:00 2001 From: Thomas Glanzmann Date: Fri, 13 Nov 2026 09:57:36 +0100 Subject: [PATCH 03/29] iommu: apple-dart: support the locked Gen3 DARTs of T8152 The T8152 display DARTs (ADT "dart,gen3", version 0x300) use the T8110 register layout but belong to the SPTM: their TLB command register at 0x80 answers translation queries instead of flushing. A stream is invalidated the way iBoot does it, by writing a 128-bit payload (valid, route, SID) to the SoC's shared TLBI aperture and broadcasting a TLBI, which the DART snoops. The route is the DART's 0x4604 register. Admit a Gen3 DART only when the bootloader hands it over locked (its streams cannot be set up cold yet), map the aperture from a second "tlbi" reg entry without claiming it, since every Gen3 DART shares it, flush the whole stream where a range flush would go through 0x80, and do not claim the fault interrupt or drain stale exceptions, whose handling acknowledges through that register as well. The range flush test fixture gains the new hw flag. Signed-off-by: Thomas Glanzmann --- drivers/iommu/apple-dart.c | 113 +++++++++++++++++- tools/asahi/apple-dart-range-tests.py | 2 +- .../drivers/apple/bcm4377-radio-sids.py | 1 + 3 files changed, 112 insertions(+), 4 deletions(-) diff --git a/drivers/iommu/apple-dart.c b/drivers/iommu/apple-dart.c index 9e009107f5a9d..a6c45cf639964 100644 --- a/drivers/iommu/apple-dart.c +++ b/drivers/iommu/apple-dart.c @@ -277,6 +277,8 @@ struct apple_dart; struct apple_dart_hw { enum dart_type type; + /* T8110 register layout, SPTM-owned TLB invalidation (T8152) */ + bool gen3; irqreturn_t (*irq_handler)(int irq, void *dev); int (*invalidate_tlb)(struct apple_dart_stream_map *stream_map); void (*enable_remap)(struct apple_dart *dart, int sid, int remap_to); @@ -540,6 +542,10 @@ struct apple_dart { unsigned int sleep_writers; bool sleep_closed; + /* Gen3: shared TLBI aperture and this DART's route in it */ + void __iomem *tlbi; + u8 tlbi_route; + DECLARE_BITMAP(remapped, DART_MAX_STREAMS); u8 remap_target[DART_MAX_STREAMS]; @@ -1998,6 +2004,43 @@ static int apple_dart_fw_invalidate(struct apple_dart_stream_map *stream_map, return 0; } +#define DART_GEN3_TLBI_ROUTE 0x4604 +#define DART_GEN3_TLBI_VALID BIT_ULL(60) +#define DART_GEN3_TLBI_ROUTE_FIELD GENMASK_ULL(59, 56) +#define DART_GEN3_TLBI_SID GENMASK_ULL(55, 48) + +/* + * Gen3 DARTs (T8152) are owned by the SPTM, and their TLB command register at + * 0x80 answers translation queries instead of flushing. A stream is + * invalidated the way iBoot does it: write a 128-bit payload (valid, route, + * SID) to the shared TLBI aperture, then broadcast a TLBI, which the DART + * snoops and applies to that stream. + */ +static int +apple_dart_gen3_hw_invalidate_tlb(struct apple_dart_stream_map *stream_map) +{ + struct apple_dart *dart = stream_map->dart; + unsigned long flags; + int sid; + + spin_lock_irqsave(&dart->lock, flags); + for_each_set_bit(sid, stream_map->sidmap, dart->num_streams) { + u64 lo = DART_GEN3_TLBI_VALID | + FIELD_PREP(DART_GEN3_TLBI_ROUTE_FIELD, dart->tlbi_route) | + FIELD_PREP(DART_GEN3_TLBI_SID, sid); + + dma_wmb(); + asm volatile("stp %0, xzr, [%1]\n" + "dsb ish\n" + ".inst 0xd50881bf\n" /* tlbi vale1os, xzr */ + "dsb osh\n" + : : "r"(lo), "r"(dart->tlbi) : "memory"); + } + spin_unlock_irqrestore(&dart->lock, flags); + + return 0; +} + static int apple_dart_t8020_hw_invalidate_tlb(struct apple_dart_stream_map *stream_map) { @@ -2736,8 +2779,11 @@ static int apple_dart_domain_flush_tlb_range(struct apple_dart_domain *domain, /* The range registers take the DVA the device issues. */ start = first + domain->dma_offset; end = last + domain->dma_offset; - if (range && (stream.dart->fw_mirror || stream.dart->fw_handoff) && - stream.dart->version >= 0x0202) + /* Gen3 has no range command; invalidate the complete stream. */ + if (stream.dart->hw->gen3) + ret = stream.dart->hw->invalidate_tlb(&stream); + else if (range && (stream.dart->fw_mirror || stream.dart->fw_handoff) && + stream.dart->version >= 0x0202) ret = apple_dart_fw_invalidate(&stream, start, end); else if (range && stream.dart->locked && stream.dart->version >= 0x0202) ret = apple_dart_t8110_hw_tlb_command_range(&stream, op, true, @@ -5632,6 +5678,13 @@ static int apple_dart_probe(struct platform_device *pdev) * not admitted: never enable that level IRQ for one block alone. */ dart->irq = -1; + } else if (dart->hw->gen3) { + /* + * The T8110 fault handler acknowledges through the TLB command + * register, which a Gen3 DART uses for translation queries. + * Leave its faults to the firmware for now. + */ + dart->irq = -1; } else if (dart->preserve_display || j713_radio) { dart->irq = platform_get_irq_optional(pdev, 0); if (dart->irq == -ENXIO) @@ -6021,8 +6074,34 @@ static int apple_dart_probe(struct platform_device *pdev) goto err_clk_disable; } + if (dart->hw->gen3) { + struct resource *tlbi; + + /* Only an iBoot-provisioned (locked) Gen3 stream is supported. */ + if (!dart->locked) { + dev_err(dev, "Gen3 DART is not locked; cold setup is not supported\n"); + ret = -ENODEV; + goto err_clk_disable; + } + /* The aperture is shared by every Gen3 DART: map, do not claim. */ + tlbi = platform_get_resource_byname(pdev, IORESOURCE_MEM, "tlbi"); + if (!tlbi || resource_size(tlbi) < 16) { + dev_err(dev, "Gen3 DART needs its TLBI aperture\n"); + ret = -EINVAL; + goto err_clk_disable; + } + dart->tlbi = devm_ioremap(dev, tlbi->start, resource_size(tlbi)); + if (!dart->tlbi) { + ret = -ENOMEM; + goto err_clk_disable; + } + dart->tlbi_route = apple_dart_readl(dart, DART_GEN3_TLBI_ROUTE) & 0xf; + dev_info(dev, "Gen3 DART, version %#x, TLBI route %u\n", + dart->version, dart->tlbi_route); + } + /* Borrowed display state: never acknowledge someone else's fault. */ - if (dart->locked && !dart->preserve_display) + if (dart->locked && !dart->preserve_display && !dart->hw->gen3) apple_dart_drain_stale_exceptions(dart); /* Refuse an already pending inherited fault before joining the shared IRQ. */ @@ -6377,6 +6456,33 @@ static const struct apple_dart_hw apple_dart_hw_t6000 = { .ttbr_count = 4, }; +static const struct apple_dart_hw apple_dart_hw_gen3 = { + .type = DART_T8110, + .gen3 = true, + .irq_handler = apple_dart_t8110_irq, + .invalidate_tlb = apple_dart_gen3_hw_invalidate_tlb, + .fmt = APPLE_DART2, + .max_sid_count = 256, + + .enable_streams = DART_T8110_ENABLE_STREAMS, + .lock = DART_T8110_PROTECT, + .lock_bit = DART_T8110_PROTECT_TTBR_TCR, + + .error = DART_T8110_ERROR, + + .tcr = DART_T8110_TCR, + .tcr_enabled = DART_T8110_TCR_TRANSLATE_ENABLE, + .tcr_disabled = 0, + .tcr_bypass = DART_T8110_TCR_BYPASS_DAPF | DART_T8110_TCR_BYPASS_DART, + .tcr_4level = DART_T8110_TCR_FOUR_LEVEL, + + .ttbr = DART_T8110_TTBR, + .ttbr_valid = DART_T8110_TTBR_VALID, + .ttbr_addr_field_shift = DART_T8110_TTBR_ADDR_FIELD_SHIFT, + .ttbr_shift = DART_T8110_TTBR_SHIFT, + .ttbr_count = 1, +}; + static const struct apple_dart_hw apple_dart_hw_t8110 = { .type = DART_T8110, .irq_handler = apple_dart_t8110_irq, @@ -7254,6 +7360,7 @@ static const struct dev_pm_ops apple_dart_pm_ops = { }; static const struct of_device_id apple_dart_of_match[] = { + { .compatible = "apple,t8152-dart-gen3", .data = &apple_dart_hw_gen3 }, { .compatible = "apple,t8103-dart", .data = &apple_dart_hw_t8103 }, { .compatible = "apple,t8103-usb4-dart", .data = &apple_dart_hw_t8103_usb4 }, { .compatible = "apple,t8110-dart", .data = &apple_dart_hw_t8110 }, diff --git a/tools/asahi/apple-dart-range-tests.py b/tools/asahi/apple-dart-range-tests.py index edebda6eb6a9d..8939de0a74e85 100644 --- a/tools/asahi/apple-dart-range-tests.py +++ b/tools/asahi/apple-dart-range-tests.py @@ -44,7 +44,7 @@ def main(): #define io_pgtable_ops_to_pgtable(x) ((struct io_pgtable *)(x)) struct apple_dart; struct apple_dart_stream_map { struct apple_dart *dart; unsigned long sidmap[1]; }; -struct apple_dart_hw { int (*invalidate_tlb)(struct apple_dart_stream_map *); }; +struct apple_dart_hw { int (*invalidate_tlb)(struct apple_dart_stream_map *); bool gen3; }; struct apple_dart { int num_streams, locked, version; bool fw_mirror, fw_handoff, managed_sids, display_dark, display_tlb_stale; void *dev; struct apple_dart_hw *hw; }; struct apple_dart_atomic_stream_map { struct apple_dart *dart; unsigned long sidmap[1]; }; struct apple_dart_domain { diff --git a/tools/testing/selftests/drivers/apple/bcm4377-radio-sids.py b/tools/testing/selftests/drivers/apple/bcm4377-radio-sids.py index 3d1026008cbd9..e13025a6157c5 100755 --- a/tools/testing/selftests/drivers/apple/bcm4377-radio-sids.py +++ b/tools/testing/selftests/drivers/apple/bcm4377-radio-sids.py @@ -97,6 +97,7 @@ def main(): u32 tcr_enabled, tcr_4level, ttbr_shift, ttbr_addr_field_shift; void (*enable_remap)(struct apple_dart *, int, int); int (*invalidate_tlb)(struct apple_dart_stream_map *); + bool gen3; }; struct apple_dart { const struct apple_dart_hw *hw; From 762549f5a30a008f90a583f521253324dd1fe885 Mon Sep 17 00:00:00 2001 From: Thomas Glanzmann Date: Fri, 13 Nov 2026 09:58:00 +0100 Subject: [PATCH 04/29] iommu: apple-dart: issue the Gen3 TLBI in the EL1&0 regime on VHE The Gen3 DART snoops the broadcast TLBI that follows the aperture write. iBoot and the aura bootloader issue TLBI VALE1OS at EL2 without VHE, which targets the EL1&0 regime. On a VHE kernel the same instruction targets EL2&0 while HCR_EL2.TGE is set, so clear TGE around it, as KVM does to reach the EL1&0 regime. The J873g display works with this in place; the effect of the change on its own has not been isolated on hardware. Signed-off-by: Thomas Glanzmann --- drivers/iommu/apple-dart.c | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/drivers/iommu/apple-dart.c b/drivers/iommu/apple-dart.c index a6c45cf639964..52a87ddcd878b 100644 --- a/drivers/iommu/apple-dart.c +++ b/drivers/iommu/apple-dart.c @@ -57,6 +57,9 @@ #include #include +#include +#include + #include "dma-iommu.h" #if IS_ENABLED(CONFIG_APPLE_DART_KUNIT_TEST) @@ -2021,9 +2024,21 @@ apple_dart_gen3_hw_invalidate_tlb(struct apple_dart_stream_map *stream_map) { struct apple_dart *dart = stream_map->dart; unsigned long flags; + u64 hcr = 0; int sid; spin_lock_irqsave(&dart->lock, flags); + /* + * The DART snoops an EL1&0 TLBI, as iBoot and aura (EL2 without VHE) + * issue it. A VHE kernel's TLBI VALE1OS targets EL2&0 while + * HCR_EL2.TGE is set, so clear TGE around it, as KVM does to reach + * the EL1&0 regime. + */ + if (is_kernel_in_hyp_mode()) { + hcr = read_sysreg(hcr_el2); + write_sysreg(hcr & ~HCR_TGE, hcr_el2); + isb(); + } for_each_set_bit(sid, stream_map->sidmap, dart->num_streams) { u64 lo = DART_GEN3_TLBI_VALID | FIELD_PREP(DART_GEN3_TLBI_ROUTE_FIELD, dart->tlbi_route) | @@ -2036,6 +2051,10 @@ apple_dart_gen3_hw_invalidate_tlb(struct apple_dart_stream_map *stream_map) "dsb osh\n" : : "r"(lo), "r"(dart->tlbi) : "memory"); } + if (is_kernel_in_hyp_mode()) { + write_sysreg(hcr, hcr_el2); + isb(); + } spin_unlock_irqrestore(&dart->lock, flags); return 0; From 47451b59770f69e10fef4a47abf955d90b6f9224 Mon Sep 17 00:00:00 2001 From: Thomas Glanzmann Date: Fri, 13 Nov 2026 09:58:41 +0100 Subject: [PATCH 05/29] dt-bindings: display: add the Apple T8152 DCP on the iBoot interface The display coprocessor (DCP) of the Apple T8152 runs RTKit firmware that offers a small display service, "disp0-service", next to the full IOMobileFramebuffer protocol; iBoot drives the displays through it. The DCP fetches through its own DART, and the display pipe scans out through another one that is locked with a root of its own, so the scanout stream is a child node. The DCP's DPTX port drives either the ATC PHY of a USB-C port, whose PD controller reports the DisplayPort alt mode and the plug orientation, or, on the Mac mini (J873g), the dedicated DisplayPort PHY that feeds the HDMI port through a DisplayPort-to-HDMI converter. The sink's EDID is read through the AP's DisplayPort AUX controller. Signed-off-by: Thomas Glanzmann --- .../display/apple,t8152-dcp-iboot.yaml | 235 ++++++++++++++++++ 1 file changed, 235 insertions(+) create mode 100644 Documentation/devicetree/bindings/display/apple,t8152-dcp-iboot.yaml diff --git a/Documentation/devicetree/bindings/display/apple,t8152-dcp-iboot.yaml b/Documentation/devicetree/bindings/display/apple,t8152-dcp-iboot.yaml new file mode 100644 index 0000000000000..37b14961bbe77 --- /dev/null +++ b/Documentation/devicetree/bindings/display/apple,t8152-dcp-iboot.yaml @@ -0,0 +1,235 @@ +# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) +%YAML 1.2 +--- +$id: http://devicetree.org/schemas/display/apple,t8152-dcp-iboot.yaml# +$schema: http://devicetree.org/meta-schemas/core.yaml# + +title: Apple T8152 DCP on the iBoot display interface + +maintainers: + - Thomas Glanzmann + +description: | + The Display Coprocessor (DCP) of the Apple T8152 SoC runs RTKit firmware + that offers a small iBoot display interface (disp0-service) next to the + full IOMobileFramebuffer protocol. This binding describes the DCP driven + through that interface, together with the DisplayPort PHY its DPTX port + is routed to. The DCP fetches through its own DART, the display pipe + scans out through another one, which sees each scanout buffer at the same + address. + +properties: + compatible: + const: apple,t8152-dcp-iboot + + reg: + minItems: 2 + maxItems: 7 + + reg-names: + oneOf: + - items: + - const: coproc + - const: atc + - const: dp-clock + - const: atc-ps + - const: atc-psd + - const: aux + - const: aux-ps + minItems: 5 + description: + DCP coprocessor, ATC DisplayPort PHY (USB-C), the PMGR clock + control of its DisplayPort link, the PMGR power states the PHY + depends on, and optionally the AP DisplayPort AUX controller that + reads the sink's EDID with its PMGR power state + - items: + - const: coproc + - const: phy-core + - const: phy-apb + - const: aux + - const: aux-ps + minItems: 3 + description: + DCP coprocessor, the HDMI dptx-phy blocks, and optionally the AP + DisplayPort AUX controller that reads the sink's EDID with its + PMGR power state + - items: + - const: coproc + - const: dp-clock + - const: aux + - const: aux-ps + minItems: 2 + description: + DCP coprocessor, the PMGR clock control of the ATC DisplayPort + link (shared by the ports), and optionally the AP DisplayPort AUX + controller with its PMGR power state; the USB-C ports follow as + usb-c child nodes + + apple,tunable-pre: + $ref: /schemas/types.yaml#/definitions/uint32-matrix + items: + items: + - description: register offset in the ATC PHY block + - description: mask + - description: value + description: + ATC PHY tunables (ADT ACIOPHY_CMN and CIOPLL_CORE defaults), applied + before the lane crossbar setup when the PHY starts from cold. + + apple,tunable-post: + $ref: /schemas/types.yaml#/definitions/uint32-matrix + items: + items: + - description: register offset in the ATC PHY block + - description: mask + - description: value + description: + ATC PHY tunables (ADT ATC_FABRIC, DPPLL and AUX defaults), applied + after the lane crossbar setup. + + apple,hdmi: + type: boolean + description: + The DCP drives the HDMI port through the dedicated dptx-phy and an + on-board DisplayPort-to-HDMI converter, instead of an ATC USB-C PHY. + + apple,usb-pd: + $ref: /schemas/types.yaml#/definitions/phandle + description: + The USB-C port's PD controller (SPMI), polled for the DisplayPort alt + mode connection and the plug orientation. + + hpd-gpios: + maxItems: 1 + description: + Hot plug detect line of the connector, if the AP can read it (J873g + HDMI); without it the sink is assumed to stay connected. + + mboxes: + maxItems: 1 + + iommus: + maxItems: 1 + description: DCP DART stream + + scanout: + type: object + additionalProperties: false + description: + The display pipe's scanout stream. Its DART is locked with its own + root, so it is a separate device that sees each scanout buffer at the + DCP's address. + properties: + iommus: + maxItems: 1 + dma-coherent: true + required: + - iommus + + dma-coherent: true + + apple,die: + $ref: /schemas/types.yaml#/definitions/uint32 + description: Die of the DPTX connection target, 0 if absent. + + "#address-cells": + const: 2 + + "#size-cells": + const: 2 + + ranges: true + +patternProperties: + "^usb-c@[0-9a-f]+$": + type: object + additionalProperties: false + description: + A USB-C port whose ATC PHY the DCP can drive. The DCP drives one port + at a time, the one a DisplayPort sink is plugged into. + properties: + reg: + maxItems: 3 + reg-names: + items: + - const: atc + - const: atc-ps + - const: atc-psd + description: + ATC DisplayPort PHY, the PMGR power states it depends on (DP_0, + DP_1 and USB_AON) and its common power domain (PSD) + apple,dfp-endpoint: + $ref: /schemas/types.yaml#/definitions/uint32 + description: The ATC's display-crossbar0 DFP endpoint (ATC instance). + apple,usb-pd: + $ref: /schemas/types.yaml#/definitions/phandle + description: The port's PD controller (SPMI). + apple,tunable-pre: + $ref: /schemas/types.yaml#/definitions/uint32-matrix + description: As the node's apple,tunable-pre, for this port's PHY. + apple,tunable-post: + $ref: /schemas/types.yaml#/definitions/uint32-matrix + description: As the node's apple,tunable-post, for this port's PHY. + required: + - reg + - reg-names + - apple,dfp-endpoint + - apple,usb-pd + +required: + - compatible + - reg + - reg-names + - mboxes + - iommus + - scanout + +additionalProperties: false + +examples: + - | + soc { + #address-cells = <2>; + #size-cells = <2>; + + display@286e00000 { + compatible = "apple,t8152-dcp-iboot"; + reg = <0x2 0x86e00000 0x0 0x74000>, + <0x4 0x13000000 0x0 0x84000>, + <0x3 0x0002005c 0x0 0x4>, + <0x3 0x082900c0 0x0 0x104>, + <0x3 0x03a13a00 0x0 0xc0>; + reg-names = "coproc", "atc", "dp-clock", "atc-ps", "atc-psd"; + mboxes = <&dcp_mbox>; + iommus = <&dcp_dart 23>; + + scanout { + iommus = <&disp0_dart 0>; + }; + }; + + display@296e00000 { + compatible = "apple,t8152-dcp-iboot"; + reg = <0x2 0x96e00000 0x0 0x74000>, + <0x3 0x0002005c 0x0 0x4>; + reg-names = "coproc", "dp-clock"; + #address-cells = <2>; + #size-cells = <2>; + ranges; + mboxes = <&dcp_mbox>; + iommus = <&dcp_dart 23>; + + scanout { + iommus = <&disp0_dart 0>; + }; + + usb-c@413000000 { + reg = <0x4 0x13000000 0x0 0x84000>, + <0x3 0x082900c0 0x0 0x104>, + <0x3 0x03a13a00 0x0 0xc0>; + reg-names = "atc", "atc-ps", "atc-psd"; + apple,dfp-endpoint = <3>; + apple,usb-pd = <&hpm3>; + }; + }; + }; From c443b6bdbd843132892bd3ba69c439af5244e10f Mon Sep 17 00:00:00 2001 From: Acelogic Date: Fri, 6 Nov 2026 05:00:57 -0500 Subject: [PATCH 06/29] dt-bindings: display: apple,t8152-dcp-iboot: add the HDMI power states A USB-C-only J873 boot leaves DPTX_PHY off, and reading the HDMI PHY then raises an SError. Describe the read-only PMGR power states of the HDMI PHY (DPTX_PHY) and of the DCPEXT CPU (DISPEXT0_CPU) with the HDMI PHY's register layout, so that the driver can check them before it touches the PHY. Signed-off-by: Acelogic [Thomas: split out of the driver change] Signed-off-by: Thomas Glanzmann --- .../bindings/display/apple,t8152-dcp-iboot.yaml | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/Documentation/devicetree/bindings/display/apple,t8152-dcp-iboot.yaml b/Documentation/devicetree/bindings/display/apple,t8152-dcp-iboot.yaml index 37b14961bbe77..20b5b31e6b0a4 100644 --- a/Documentation/devicetree/bindings/display/apple,t8152-dcp-iboot.yaml +++ b/Documentation/devicetree/bindings/display/apple,t8152-dcp-iboot.yaml @@ -48,11 +48,14 @@ properties: - const: phy-apb - const: aux - const: aux-ps - minItems: 3 + - const: phy-ps + - const: coproc-ps description: - DCP coprocessor, the HDMI dptx-phy blocks, and optionally the AP + DCP coprocessor, the HDMI dptx-phy blocks, the AP DisplayPort AUX controller that reads the sink's EDID with its - PMGR power state + PMGR power state, and the read-only PMGR power states for the + HDMI PHY and DCPEXT CPU. The driver only adopts the bootloader's + powered link and must check these states before accessing the PHY. - items: - const: coproc - const: dp-clock From ce320b2a9239ef5d79ecbf7eb69a25844e3869f4 Mon Sep 17 00:00:00 2001 From: Acelogic Date: Fri, 6 Nov 2026 05:08:51 -0500 Subject: [PATCH 07/29] dt-bindings: display: apple,t8152-dcp-iboot: add apple,afk-active-endpoints The bootloader may hand the DCP over with AFK endpoints still running, and an AFK INIT on a live endpoint asserts in the firmware. Add a property the bootloader fills in at boot with the endpoints it left running, so that the operating system can stop them before it starts a session of its own. Signed-off-by: Acelogic [Thomas: split out of the driver change, added the description] Signed-off-by: Thomas Glanzmann --- .../bindings/display/apple,t8152-dcp-iboot.yaml | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/Documentation/devicetree/bindings/display/apple,t8152-dcp-iboot.yaml b/Documentation/devicetree/bindings/display/apple,t8152-dcp-iboot.yaml index 20b5b31e6b0a4..f1396f44990db 100644 --- a/Documentation/devicetree/bindings/display/apple,t8152-dcp-iboot.yaml +++ b/Documentation/devicetree/bindings/display/apple,t8152-dcp-iboot.yaml @@ -22,6 +22,17 @@ properties: compatible: const: apple,t8152-dcp-iboot + apple,afk-active-endpoints: + $ref: /schemas/types.yaml#/definitions/uint32 + maximum: 65535 + description: + Runtime handoff from the bootloader. Bit N records that AFK endpoint + 0x20 + N is running and must acknowledge shutdown before a new session + starts. A zero bit means stopped. The bootloader inserts this only + for controllers whose complete endpoint state it owns, and reserves + the old session's buffers. The driver refuses takeover if absent. + This property must not be hard-coded in a board device tree. + reg: minItems: 2 maxItems: 7 From a5c2e9c45dca6cc7c1cc50e3d015036fcaaf8a94 Mon Sep 17 00:00:00 2001 From: Thomas Glanzmann Date: Tue, 17 Nov 2026 22:53:55 +0100 Subject: [PATCH 08/29] dt-bindings: display: apple,t8152-dcp-iboot: describe DCPEXT1 and shared USB-C ports All three T8152 display controllers can drive the rear USB-C ports through the one display crossbar. Describe what a controller needs for that: - apple,dp-source: its crossbar UFP endpoint (0 DCP, 1 DCPEXT0, 2 DCPEXT1); - apple,usb-c-ports: the node holding the shared usb-c port children, the DP clock dividers and the AUX controller; - coproc-psd: the read-only PMGR2 PSD admission state of a DCPEXT, checked before its coprocessor is touched (a read while it is gated hangs the machine), and the coproc + coproc-psd layout of an extra controller without PHY resources of its own; - apple,rtkit-wake: set by the loader for an unused DCPEXT whose firmware iBoot loaded and powered and which waits for an RTKit wake. The dp-clock resource now spans the PMGR ATC DP clock dividers of all ATCs (one every eight bytes from ATC0) instead of ATC3's alone. Signed-off-by: Thomas Glanzmann --- .../display/apple,t8152-dcp-iboot.yaml | 45 ++++++++++++++++--- 1 file changed, 38 insertions(+), 7 deletions(-) diff --git a/Documentation/devicetree/bindings/display/apple,t8152-dcp-iboot.yaml b/Documentation/devicetree/bindings/display/apple,t8152-dcp-iboot.yaml index f1396f44990db..d4f007202ae65 100644 --- a/Documentation/devicetree/bindings/display/apple,t8152-dcp-iboot.yaml +++ b/Documentation/devicetree/bindings/display/apple,t8152-dcp-iboot.yaml @@ -33,9 +33,31 @@ properties: the old session's buffers. The driver refuses takeover if absent. This property must not be hard-coded in a board device tree. + apple,dp-source: + $ref: /schemas/types.yaml#/definitions/uint32 + maximum: 2 + description: + Display-crossbar UFP endpoint, 0 for DCP, 1 for DCPEXT0, 2 for DCPEXT1. + The driver uses DPTX port zero, whose ATC mux selector is twice this. + + apple,usb-c-ports: + $ref: /schemas/types.yaml#/definitions/phandle + description: + Controller node containing the shared USB-C port children, dp-clock, + aux and aux-ps resources. Each connected port has one controller owner. + + apple,rtkit-wake: + type: boolean + description: + Runtime handoff for an unused DCPEXT whose firmware is loaded, powered, + and awaiting RTKit wake. The loader must reserve its firmware mappings + and buffers and supply apple,afk-active-endpoints. This property must + not be hard-coded in a board tree. An adopted HDMI session is already + running and takes precedence over this unused-controller indication. + reg: minItems: 2 - maxItems: 7 + maxItems: 8 reg-names: oneOf: @@ -61,12 +83,20 @@ properties: - const: aux-ps - const: phy-ps - const: coproc-ps + - const: coproc-psd + minItems: 7 description: DCP coprocessor, the HDMI dptx-phy blocks, the AP DisplayPort AUX controller that reads the sink's EDID with its PMGR power state, and the read-only PMGR power states for the HDMI PHY and DCPEXT CPU. The driver only adopts the bootloader's powered link and must check these states before accessing the PHY. + - items: + - const: coproc + - const: coproc-psd + description: + Additional display controller and its PMGR2 PSD admission state; + PHY resources are shared through apple,usb-c-ports. - items: - const: coproc - const: dp-clock @@ -75,9 +105,9 @@ properties: minItems: 2 description: DCP coprocessor, the PMGR clock control of the ATC DisplayPort - link (shared by the ports), and optionally the AP DisplayPort AUX - controller with its PMGR power state; the USB-C ports follow as - usb-c child nodes + links (one divider every eight bytes, starting with ATC0), and + optionally the AP DisplayPort AUX controller with its PMGR power + state; the USB-C ports follow as usb-c child nodes apple,tunable-pre: $ref: /schemas/types.yaml#/definitions/uint32-matrix @@ -105,7 +135,8 @@ properties: type: boolean description: The DCP drives the HDMI port through the dedicated dptx-phy and an - on-board DisplayPort-to-HDMI converter, instead of an ATC USB-C PHY. + on-board DisplayPort-to-HDMI converter. With apple,usb-c-ports it can + instead claim a USB-C port when the bootloader has not assigned HDMI. apple,usb-pd: $ref: /schemas/types.yaml#/definitions/phandle @@ -210,7 +241,7 @@ examples: compatible = "apple,t8152-dcp-iboot"; reg = <0x2 0x86e00000 0x0 0x74000>, <0x4 0x13000000 0x0 0x84000>, - <0x3 0x0002005c 0x0 0x4>, + <0x3 0x00020044 0x0 0x1c>, <0x3 0x082900c0 0x0 0x104>, <0x3 0x03a13a00 0x0 0xc0>; reg-names = "coproc", "atc", "dp-clock", "atc-ps", "atc-psd"; @@ -225,7 +256,7 @@ examples: display@296e00000 { compatible = "apple,t8152-dcp-iboot"; reg = <0x2 0x96e00000 0x0 0x74000>, - <0x3 0x0002005c 0x0 0x4>; + <0x3 0x00020044 0x0 0x1c>; reg-names = "coproc", "dp-clock"; #address-cells = <2>; #size-cells = <2>; From 4fc411f256c83859073a7b31b90b7b76f9ed29ad Mon Sep 17 00:00:00 2001 From: Thomas Glanzmann Date: Wed, 18 Nov 2026 11:03:18 +0100 Subject: [PATCH 09/29] dt-bindings: display: apple,t8152-dcp-iboot: describe the HDMI cold start The J873g HDMI port works only if the bootloader brought it up. For the driver to start it itself, the HDMI node needs the PMGR2 PSD service of the dptx-phy (phy-psd), the phy tunables from the ADT (apple,tunable-phy), and the SMC writes that power the PS190 DisplayPort-to-HDMI converter (apple,smc, apple,pcon-power). The phy's PMGR power state is no longer only read. Signed-off-by: Thomas Glanzmann --- .../display/apple,t8152-dcp-iboot.yaml | 39 +++++++++++++++++-- 1 file changed, 35 insertions(+), 4 deletions(-) diff --git a/Documentation/devicetree/bindings/display/apple,t8152-dcp-iboot.yaml b/Documentation/devicetree/bindings/display/apple,t8152-dcp-iboot.yaml index d4f007202ae65..7c4e2b1db5ab9 100644 --- a/Documentation/devicetree/bindings/display/apple,t8152-dcp-iboot.yaml +++ b/Documentation/devicetree/bindings/display/apple,t8152-dcp-iboot.yaml @@ -57,7 +57,7 @@ properties: reg: minItems: 2 - maxItems: 8 + maxItems: 9 reg-names: oneOf: @@ -84,13 +84,15 @@ properties: - const: phy-ps - const: coproc-ps - const: coproc-psd + - const: phy-psd minItems: 7 description: DCP coprocessor, the HDMI dptx-phy blocks, the AP DisplayPort AUX controller that reads the sink's EDID with its - PMGR power state, and the read-only PMGR power states for the - HDMI PHY and DCPEXT CPU. The driver only adopts the bootloader's - powered link and must check these states before accessing the PHY. + PMGR power state, the PMGR power state of the HDMI PHY, the read + only power state of the DCPEXT CPU, and the PMGR2 PSD service of + the HDMI PHY. The PHY is accessed only once its power states read + powered; with phy-psd it is powered up for a cold HDMI start. - items: - const: coproc - const: coproc-psd @@ -138,6 +140,35 @@ properties: on-board DisplayPort-to-HDMI converter. With apple,usb-c-ports it can instead claim a USB-C port when the bootloader has not assigned HDMI. + apple,tunable-phy: + $ref: /schemas/types.yaml#/definitions/uint32-matrix + items: + items: + - description: register offset in the HDMI dptx-phy core block + - description: mask + - description: value + description: + HDMI dptx-phy tunables (ADT dptx-phy asdc and lane-shm-regs groups), + applied when the PHY starts from cold. + + apple,smc: + $ref: /schemas/types.yaml#/definitions/phandle + description: + The SMC that switches the power of the DisplayPort-to-HDMI converter. + + apple,pcon-power: + $ref: /schemas/types.yaml#/definitions/uint32-matrix + items: + items: + - description: SMC key + - description: value, bits 63:32 + - description: value, bits 31:0 + description: + SMC writes, in order, that power the DisplayPort-to-HDMI converter + (ADT dp2hdmi-gpio0 function-dp2hdmi_pwr_en and function-hdmi_pwr_en). + The converter drives the HDMI hot-plug detect line only while it is + powered. + apple,usb-pd: $ref: /schemas/types.yaml#/definitions/phandle description: From ddaa639dfe53771a3a18be480e3503de544b2def Mon Sep 17 00:00:00 2001 From: Thomas Glanzmann Date: Thu, 19 Nov 2026 17:49:53 +0100 Subject: [PATCH 10/29] dt-bindings: chosen: add apple,dcp-rtkit-running The loader reports whether the internal DCP's RTKit was running at the hand-off (1) or whether it quiesced the coprocessor after a failed display setup (0). apple,dcp-rtkit-quiesced carries the same values under a name that reads the other way round, so the T8152 loader and DCP driver use this name instead. Signed-off-by: Thomas Glanzmann --- .../chosen/apple,dcp-rtkit-running.yaml | 37 +++++++++++++++++++ MAINTAINERS | 1 + 2 files changed, 38 insertions(+) create mode 100644 Documentation/devicetree/bindings/chosen/apple,dcp-rtkit-running.yaml diff --git a/Documentation/devicetree/bindings/chosen/apple,dcp-rtkit-running.yaml b/Documentation/devicetree/bindings/chosen/apple,dcp-rtkit-running.yaml new file mode 100644 index 0000000000000..38a4dcb4044a5 --- /dev/null +++ b/Documentation/devicetree/bindings/chosen/apple,dcp-rtkit-running.yaml @@ -0,0 +1,37 @@ +# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) +%YAML 1.2 +--- +$id: http://devicetree.org/schemas/chosen/apple,dcp-rtkit-running.yaml# +$schema: http://devicetree.org/meta-schemas/core.yaml# + +title: Apple DCP RTKit state at the loader hand-off + +maintainers: + - Thomas Glanzmann + +description: + The loader records whether the internal display coprocessor's RTKit was + running when it handed over to the kernel, or whether the loader quiesced + it after a failed display setup. The loader omits this property when it + never started the coprocessor. + +select: + properties: + $nodename: + const: chosen + required: + - $nodename + +properties: + $nodename: + const: chosen + + apple,dcp-rtkit-running: + $ref: /schemas/types.yaml#/definitions/uint32 + enum: [0, 1] + description: + One if the internal DCP's RTKit was running at the hand-off, zero if + the loader quiesced it. + +additionalProperties: true +... diff --git a/MAINTAINERS b/MAINTAINERS index 5c8fc89e51d00..cb48635b6e368 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -2093,6 +2093,7 @@ F: drivers/soc/apple/pmp.rs F: include/linux/mt7932.h F: include/linux/soc/apple/pmp-report.h F: Documentation/devicetree/bindings/chosen/apple,dcp-rtkit-quiesced.yaml +F: Documentation/devicetree/bindings/chosen/apple,dcp-rtkit-running.yaml F: Documentation/devicetree/bindings/chosen/aurora,ctxr-exec-window.yaml F: Documentation/devicetree/bindings/display/apple,dcp.yaml F: Documentation/devicetree/bindings/display/apple,display-subsystem.yaml From 01d47a3ae576931ee5c524efe0298085bcf247df Mon Sep 17 00:00:00 2001 From: Thomas Glanzmann Date: Thu, 19 Nov 2026 21:22:30 +0100 Subject: [PATCH 11/29] iommu: apple-dart: report T8152 Gen3 DART faults A T8152 Gen3 DART reports a fault as a v2 DART does: a bitmap of the streams with a pending exception at 0x4000, a record per stream at 0x8000 (status, address), acknowledged by writing the bitmap back. Request its interrupt and let apple_dart_t8110_v2_irq() report and acknowledge its faults instead of leaving them to the firmware. Only the unlock after a lock-on-error exception differs: a Gen3 DART's TLB command register serves translation queries, so it is not used to unlock. A Gen3 DART with lock-on-error set says that its stream stays stopped. The J873g's Gen3 DARTs have it clear. Tested on the J873g by clearing the AVD DART's SID 0 TTBR during an H.264 decode: one interrupt, "translation fault: SID:0 write TTBR invalid at 0x10fff000000 (status 0x00000100)", the bitmap acknowledged with no repeat, and the decoder retired by its recovery path. After the TTBR was restored and the decoder reloaded, the same file decoded bit-exact again. None of the other eight Gen3 DART fault lines fired, including those of the locked display DARTs with three displays running. Signed-off-by: Thomas Glanzmann --- drivers/iommu/apple-dart.c | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/drivers/iommu/apple-dart.c b/drivers/iommu/apple-dart.c index 52a87ddcd878b..938e6f519efa6 100644 --- a/drivers/iommu/apple-dart.c +++ b/drivers/iommu/apple-dart.c @@ -2230,6 +2230,15 @@ static int apple_dart_t8110_v2_unlock(struct apple_dart *dart) return -EPERM; if (!(readl(dart->regs + DART_T8110_DIAG_LOCK) & DART_T8110_DIAG_LOCK_ON_ERR)) return 0; + /* + * A Gen3 DART's TLB command register serves translation queries and + * cannot unlock it. Its streams stay stopped until the DART is reset. + */ + if (dart->hw->gen3) { + dev_err_ratelimited(dart->dev, + "locked on error; a Gen3 DART is not unlocked\n"); + return -EOPNOTSUPP; + } /* Unlock after acknowledging a lock-on-error exception. */ __set_bit(0, stream_map.sidmap); @@ -5699,11 +5708,16 @@ static int apple_dart_probe(struct platform_device *pdev) dart->irq = -1; } else if (dart->hw->gen3) { /* - * The T8110 fault handler acknowledges through the TLB command - * register, which a Gen3 DART uses for translation queries. - * Leave its faults to the firmware for now. + * Gen3 reports a fault like a v2 DART: the per-SID exception + * bitmap and records, acknowledged by writing the bitmap back + * (apple_dart_t8110_v2_irq()). Only the unlock after a + * lock-on-error exception differs: see apple_dart_t8110_v2_unlock(). */ - dart->irq = -1; + dart->irq = platform_get_irq_optional(pdev, 0); + if (dart->irq == -ENXIO) + dart->irq = -1; + else if (dart->irq < 0) + return dart->irq; } else if (dart->preserve_display || j713_radio) { dart->irq = platform_get_irq_optional(pdev, 0); if (dart->irq == -ENXIO) From d2429a90a778fd93f8ad48a7bb951ec93c396434 Mon Sep 17 00:00:00 2001 From: Thomas Glanzmann Date: Thu, 19 Nov 2026 22:21:46 +0100 Subject: [PATCH 12/29] drm/apple: let the match data select the AFKv2 wire afk_init() picks the AFKv2 wire for firmware 27 and for the T8122 external processor on 26.x, both recognised by their IOMFB interface. The T8152 DCP firmware speaks AFKv2 on every endpoint of its iBoot interface (the display service, the DPAV controller services and the DPTX port services), and drm/apple drives no IOMFB interface of it that a version could key on. Add hw.afk_v2, which selects AFKv2 for all endpoints of a processor. No match entry sets it yet. Signed-off-by: Thomas Glanzmann --- drivers/gpu/drm/apple/afk.c | 3 ++- drivers/gpu/drm/apple/dcp-internal.h | 6 ++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/drivers/gpu/drm/apple/afk.c b/drivers/gpu/drm/apple/afk.c index 3c5eaf725d02a..27fc612d460bb 100644 --- a/drivers/gpu/drm/apple/afk.c +++ b/drivers/gpu/drm/apple/afk.c @@ -115,7 +115,8 @@ struct apple_dcp_afkep *afk_init(struct apple_dcp *dcp, u32 endpoint, afkep->ops = ops; afkep->dcp = dcp; - afkep->wire_v2 = dcp->fw_compat == DCP_FIRMWARE_V_27_0 || dcp_is_ext26(dcp); + afkep->wire_v2 = dcp->fw_compat == DCP_FIRMWARE_V_27_0 || dcp_is_ext26(dcp) || + dcp->hw.afk_v2; afkep->endpoint = endpoint; afkep->wq = alloc_ordered_workqueue("apple-dcp-afkep%02x", WQ_MEM_RECLAIM, endpoint); diff --git a/drivers/gpu/drm/apple/dcp-internal.h b/drivers/gpu/drm/apple/dcp-internal.h index 515e6bf065f09..943019c89f8af 100644 --- a/drivers/gpu/drm/apple/dcp-internal.h +++ b/drivers/gpu/drm/apple/dcp-internal.h @@ -345,6 +345,12 @@ struct apple_dcp_hw_data { * firmware 27.0 and 27.2.1), so there is no secure port to idle before the IOP. */ bool s2r_no_secure_port; + /* + * Every AFK endpoint speaks AFKv2 (see afk_init()), whatever the + * firmware interface: for a firmware that does so on all of them + * without an IOMFB interface version that implies it. + */ + bool afk_v2; }; enum dcp_test_point { From 0eedf7ceda42919935768a54740fb24c2b9d8b6e Mon Sep 17 00:00:00 2001 From: Thomas Glanzmann Date: Thu, 19 Nov 2026 22:23:44 +0100 Subject: [PATCH 13/29] drm/apple: start AFK endpoints of an RTKit session taken without its map The T8152 DCP does not acknowledge AP QUIESCED, and an IOP INIT while its AP power is still on panics the firmware ("Invalid AP power: 32"). A session the bootloader leaves running can only be taken over as it runs, with apple_rtkit_init_adopted(), without the HELLO/EPMAP handshake: the endpoint map went to the bootloader, and apple_rtkit_start_ep() refuses an endpoint RTKit does not know. Add hw.afk_adopt_endpoints. With it afk_start() registers the endpoint with apple_rtkit_adopt_ep() before it sends anything to it, so its START_EP goes out and its replies are not taken for an unknown endpoint's. No match entry sets it yet. Signed-off-by: Thomas Glanzmann --- drivers/gpu/drm/apple/afk.c | 7 +++++++ drivers/gpu/drm/apple/dcp-internal.h | 6 ++++++ 2 files changed, 13 insertions(+) diff --git a/drivers/gpu/drm/apple/afk.c b/drivers/gpu/drm/apple/afk.c index 27fc612d460bb..ff105620dfc09 100644 --- a/drivers/gpu/drm/apple/afk.c +++ b/drivers/gpu/drm/apple/afk.c @@ -249,6 +249,13 @@ int afk_start(struct apple_dcp_afkep *ep) } reinit_completion(&ep->started); + /* + * An adopted session's endpoint map went to the bootloader, and RTKit + * starts only endpoints it knows. Registered before anything is sent, + * the endpoint's replies are not reported as an unknown endpoint's. + */ + if (ep->dcp->hw.afk_adopt_endpoints) + apple_rtkit_adopt_ep(ep->dcp->rtk, ep->endpoint); ret = apple_rtkit_start_ep(ep->dcp->rtk, ep->endpoint); if (ret) return ret; diff --git a/drivers/gpu/drm/apple/dcp-internal.h b/drivers/gpu/drm/apple/dcp-internal.h index 943019c89f8af..ef548bb14ae56 100644 --- a/drivers/gpu/drm/apple/dcp-internal.h +++ b/drivers/gpu/drm/apple/dcp-internal.h @@ -351,6 +351,12 @@ struct apple_dcp_hw_data { * without an IOMFB interface version that implies it. */ bool afk_v2; + /* + * The RTKit session is taken over without its endpoint map (see + * apple_rtkit_init_adopted()): afk_start() registers each endpoint + * with RTKit before starting it. + */ + bool afk_adopt_endpoints; }; enum dcp_test_point { From 2a1bfe4154a9ac33bccf4b61bb26f9191e44f33e Mon Sep 17 00:00:00 2001 From: Thomas Glanzmann Date: Thu, 19 Nov 2026 22:25:35 +0100 Subject: [PATCH 14/29] drm/apple: stop the AFK endpoints a bootloader left running A bootloader that drives the display through the DCP firmware can leave AFK endpoints running with its scanout when it hands over, as the T8152 loader does with the iBoot interface's endpoints. An AFK INIT on a live endpoint asserts in the firmware. Add dcp->afk_live_endpoints, bit N for endpoint 0x20 + N, which a processor's probe fills from such a hand-off (the T8152 loader's apple,afk-active-endpoints). afk_start() sends an endpoint whose bit is set a SHUTDOWN and waits for its acknowledgment before START_EP and INIT, after registering it with an adopted RTKit session so that the acknowledgment is not taken for an unknown endpoint's. The old session's ring stays the bootloader's, which keeps it reserved. The mask is zero on every processor that sets nothing, so their start is unchanged. The stop of the live endpoints is based on a change by Acelogic for the T8152 hand-off. Signed-off-by: Thomas Glanzmann --- drivers/gpu/drm/apple/afk.c | 32 ++++++++++++++++++++++++++++ drivers/gpu/drm/apple/dcp-internal.h | 6 ++++++ 2 files changed, 38 insertions(+) diff --git a/drivers/gpu/drm/apple/afk.c b/drivers/gpu/drm/apple/afk.c index ff105620dfc09..d0f6f84d5704a 100644 --- a/drivers/gpu/drm/apple/afk.c +++ b/drivers/gpu/drm/apple/afk.c @@ -236,6 +236,35 @@ static int afk_start_polled(struct apple_dcp_afkep *ep) return -ETIMEDOUT; } +/* + * An endpoint the bootloader left running (afk_live_endpoints): an INIT + * on a live endpoint asserts in the firmware. Stop it and wait for the + * acknowledgment first. Its old ring stays with the bootloader, which + * keeps it reserved. + */ +static int afk_stop_live(struct apple_dcp_afkep *ep) +{ + u32 bit; + + if (ep->endpoint < 0x20 || ep->endpoint >= 0x40) + return 0; + bit = BIT(ep->endpoint - 0x20); + if (!(ep->dcp->afk_live_endpoints & bit)) + return 0; + reinit_completion(&ep->stopped); + afk_send(ep, FIELD_PREP(RBEP_TYPE, RBEP_SHUTDOWN)); + if (!wait_for_completion_timeout(&ep->stopped, msecs_to_jiffies(1000))) { + dev_err(ep->dcp->dev, "AFK[ep:%02x]: inherited endpoint did not stop\n", + ep->endpoint); + return -ETIMEDOUT; + } + reinit_completion(&ep->stopped); + ep->dcp->afk_live_endpoints &= ~bit; + dev_info(ep->dcp->dev, "AFK[ep:%02x]: inherited endpoint stopped\n", + ep->endpoint); + return 0; +} + int afk_start(struct apple_dcp_afkep *ep) { int ret; @@ -256,6 +285,9 @@ int afk_start(struct apple_dcp_afkep *ep) */ if (ep->dcp->hw.afk_adopt_endpoints) apple_rtkit_adopt_ep(ep->dcp->rtk, ep->endpoint); + ret = afk_stop_live(ep); + if (ret) + return ret; ret = apple_rtkit_start_ep(ep->dcp->rtk, ep->endpoint); if (ret) return ret; diff --git a/drivers/gpu/drm/apple/dcp-internal.h b/drivers/gpu/drm/apple/dcp-internal.h index ef548bb14ae56..bc5449a12f97d 100644 --- a/drivers/gpu/drm/apple/dcp-internal.h +++ b/drivers/gpu/drm/apple/dcp-internal.h @@ -575,6 +575,12 @@ struct apple_dcp { enum dcp_firmware_version fw_compat; /* Bumped by every iomfb poweron; see afk_compact_slot(). */ u32 afk_power_gen; + /* + * AFK endpoints the bootloader left running, bit N for endpoint + * 0x20 + N, from its hand-off: afk_start() stops each before its + * INIT. Zero for a processor without such a hand-off. + */ + u32 afk_live_endpoints; /* Firmware 27.x: the method IDs of this build (iomfb_v27_methods()). */ const struct dcp_method_entry *v27_methods; bool external; From 4a1f227206dba93e2e75d6de8d947f9defa6569f Mon Sep 17 00:00:00 2001 From: Thomas Glanzmann Date: Thu, 19 Nov 2026 22:27:27 +0100 Subject: [PATCH 15/29] drm/apple: send AFK commands without output without a reply buffer afk_send_command() allocates a reply buffer of the output length and names it in the command, even for a length of zero. The AFKv2 setters of the T8152 DCP firmware's iBoot interface (SET_POWER, SET_MODE, SET_SURFACE) answer with a transport status alone. Given a reply buffer, the firmware copies an empty reply into it and asserts. Give a command without output no reply buffer: its descriptor carries address and length zero, which the AFKv2 codec already accepts and checks the reply against. Every current caller passes an output length, so none of them changes. Signed-off-by: Thomas Glanzmann --- drivers/gpu/drm/apple/afk.c | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/drivers/gpu/drm/apple/afk.c b/drivers/gpu/drm/apple/afk.c index d0f6f84d5704a..2a52c9fd63c18 100644 --- a/drivers/gpu/drm/apple/afk.c +++ b/drivers/gpu/drm/apple/afk.c @@ -3308,11 +3308,20 @@ static int afk_send_command_common(struct apple_epic_service *service, u8 type, goto err_put_service; } - rxbuf = dma_alloc_coherent(ep->dcp->dev, output_len, &rxbuf_dma, - GFP_KERNEL); - if (!rxbuf) { - ret = -ENOMEM; - goto err_put_service; + /* + * A command without output carries no reply buffer: firmware that + * answers some commands with a status alone asserts when it is given + * one, since it then copies an empty reply. + */ + rxbuf = NULL; + rxbuf_dma = 0; + if (output_len) { + rxbuf = dma_alloc_coherent(ep->dcp->dev, output_len, &rxbuf_dma, + GFP_KERNEL); + if (!rxbuf) { + ret = -ENOMEM; + goto err_put_service; + } } txbuf = dma_alloc_coherent(ep->dcp->dev, payload_len, &txbuf_dma, GFP_KERNEL); @@ -3470,7 +3479,7 @@ static int afk_send_command_common(struct apple_epic_service *service, u8 type, if (!dcp_is_ext26(ep->dcp)) dma_free_coherent(ep->dcp->dev, payload_len, txbuf, txbuf_dma); err_free_rxbuf: - if (!dcp_is_ext26(ep->dcp)) + if (rxbuf && !dcp_is_ext26(ep->dcp)) dma_free_coherent(ep->dcp->dev, output_len, rxbuf, rxbuf_dma); err_put_service: afk_service_put(service); From b1bc3a3b4c4569820f6e2f865fdae02c50ef6262 Mon Sep 17 00:00:00 2001 From: Thomas Glanzmann Date: Thu, 19 Nov 2026 22:29:25 +0100 Subject: [PATCH 16/29] drm/apple: match AFK services by their announced name afk_recv_handle_init() parses the properties that follow a service's announced name whenever there are any, and matches the service by the EPIC provider class found there. Which keys it accepts depends on the firmware interface version. The T8152 DCP firmware's iBoot service (disp0-service, dispext-service) is announced by name alone, and the properties of its DPTX and DPAV services are not needed to drive it: a property set the parser rejects would leave such a service unopened. Add hw.afk_announced_names. With it the properties are not parsed, the service is matched by its announced name, and that name is what its handler gets as the EPIC name. No match entry sets it yet. The startup host test's and the apple-dcp service rebind test's processors gain the flag, and the latter kstrdup(). The provider admission test looks for the announcement's OPEN in the first 7000 bytes of afk_recv_handle_init() instead of 6500, which no longer reach it. Signed-off-by: Thomas Glanzmann --- drivers/gpu/drm/apple/afk.c | 15 ++++++++++++--- drivers/gpu/drm/apple/dcp-internal.h | 5 +++++ tools/asahi/dcpext26-startup-tests.py | 3 ++- .../selftests/drivers/gpu/apple-dcp/edid.py | 6 ++++-- 4 files changed, 23 insertions(+), 6 deletions(-) diff --git a/drivers/gpu/drm/apple/afk.c b/drivers/gpu/drm/apple/afk.c index 2a52c9fd63c18..630acfdc5af17 100644 --- a/drivers/gpu/drm/apple/afk.c +++ b/drivers/gpu/drm/apple/afk.c @@ -1779,7 +1779,7 @@ static void afk_recv_handle_init(struct apple_dcp_afkep *ep, u32 channel, * EPICProviderClass was used. If the init call has props parse them and * use EPICProviderClass to match the service. */ - if (props_size > 36) { + if (props_size > 36 && !ep->dcp->hw.afk_announced_names) { int ret = parse(props, props_size, &ctx); if (ret) { dev_err(ep->dcp->dev, @@ -1824,8 +1824,17 @@ static void afk_recv_handle_init(struct apple_dcp_afkep *ep, u32 channel, service_name = epic_class; } } else { - service_name = name; - } + service_name = name; + /* + * hw.afk_announced_names: the announced name is the service's + * identity, and its handler gets it as the EPIC name. + */ + if (ep->dcp->hw.afk_announced_names) { + epic_name = kstrdup(name, GFP_KERNEL); + if (!epic_name) + return; + } + } if (ep->match_epic_name) ops = afk_match_service(ep, epic_name); diff --git a/drivers/gpu/drm/apple/dcp-internal.h b/drivers/gpu/drm/apple/dcp-internal.h index bc5449a12f97d..1621c62f1a4a0 100644 --- a/drivers/gpu/drm/apple/dcp-internal.h +++ b/drivers/gpu/drm/apple/dcp-internal.h @@ -357,6 +357,11 @@ struct apple_dcp_hw_data { * with RTKit before starting it. */ bool afk_adopt_endpoints; + /* + * AFK services are matched by the name in their announcement; the + * properties after it are not parsed (afk_recv_handle_init()). + */ + bool afk_announced_names; }; enum dcp_test_point { diff --git a/tools/asahi/dcpext26-startup-tests.py b/tools/asahi/dcpext26-startup-tests.py index 0811ada7ce526..c71308f06d258 100644 --- a/tools/asahi/dcpext26-startup-tests.py +++ b/tools/asahi/dcpext26-startup-tests.py @@ -59,6 +59,7 @@ def run(name,code,reject=False): #define kmalloc(n,f) malloc(n) #define kzalloc_obj(v) calloc(1,sizeof(v)) #define kfree(p) free((void *)(p)) +#define kstrdup(s,f) strdup(s) #define max(a,b) ((a)>(b)?(a):(b)) #define dev_info(...) ((void)0) #define dev_err(...) ((void)0) @@ -93,7 +94,7 @@ def run(name,code,reject=False): struct apple_epic_service_ops {const char *name;bool reusable,optional,open_later;int (*prepare)(struct apple_epic_service *,const char *,const char *,s64);int (*revalidate)(struct apple_epic_service *,const char *,const char *,s64);void (*init)(struct apple_epic_service *,const char *,const char *,s64);void (*teardown)(struct apple_epic_service *);void (*init_props)(struct apple_epic_service *,s64,const void *,size_t);}; struct apple_epic_service {struct apple_dcp_afkep *ep;const struct apple_epic_service_ops *ops;bool enabled,torndown,pending_open,defer_start;unsigned users;int lock;u32 channel;void *cookie;const char *pending_name,*pending_class;s64 pending_unit;int cmd_map[1];}; struct dptx_port {struct apple_epic_service *service;s64 unit;bool enabled,connected;int enable_completion;}; -struct apple_dcp {const void *push_out_prefill;bool native,ext26_services_open,v27_j714s,v27_sleep_transport,dcpext;int fw_compat,index,dptx_controller_ready;struct {unsigned num_dptx_ports;int iomfb_method_profile,firmware_compat;} hw;struct dptx_port dptxport[2];void *dev;struct apple_dcp_afkep *systemep,*ibootep,*dptx_controller_ep,*dcpavservep,*dptxep,*ext26_aux[3];}; +struct apple_dcp {const void *push_out_prefill;bool native,ext26_services_open,v27_j714s,v27_sleep_transport,dcpext;int fw_compat,index,dptx_controller_ready;struct {unsigned num_dptx_ports;int iomfb_method_profile,firmware_compat;bool afk_announced_names;} hw;struct dptx_port dptxport[2];void *dev;struct apple_dcp_afkep *systemep,*ibootep,*dptx_controller_ep,*dcpavservep,*dptxep,*ext26_aux[3];}; struct apple_dcp_afkep {struct apple_dcp *dcp;u32 endpoint,num_channels;bool wire_v2,stopping,match_epic_name,compact;int lock;void *wq,*service_work;const struct apple_epic_service_ops *ops;struct apple_epic_service services[AFK_MAX_CHANNEL];}; static bool dcp_is_ext26(struct apple_dcp *d){return d->native;} static bool dcp_fail_stop(struct apple_dcp *d){return d->native;} diff --git a/tools/testing/selftests/drivers/gpu/apple-dcp/edid.py b/tools/testing/selftests/drivers/gpu/apple-dcp/edid.py index fe2e56e1f46ea..bc5eb6f352967 100644 --- a/tools/testing/selftests/drivers/gpu/apple-dcp/edid.py +++ b/tools/testing/selftests/drivers/gpu/apple-dcp/edid.py @@ -100,7 +100,7 @@ def function(file,signature): struct apple_dcp { void *dev;int fw_compat;struct apple_dcp_afkep *dcpavservep;struct apple_connector *connector; bool external_physically_absent,hpd_mutex,quiescing,typec_cable_connected;u64 typec_generation;unsigned int nr_modes; - struct { int iomfb_method_profile,firmware_compat; bool fw_14_7_endpoints; unsigned int num_dptx_ports; } hw; bool ext_dead,external_native; + struct { int iomfb_method_profile,firmware_compat; bool fw_14_7_endpoints,afk_announced_names; unsigned int num_dptx_ports; } hw; bool ext_dead,external_native; struct { bool lock,enabled;u64 epoch;int enable_completion;struct apple_epic_service *service,*opened; u64 opened_epoch,close_gen,terminated_epoch,close_epoch;bool opening;struct work_struct close_wq; } dcpavserv; /* A J714s firmware 27 processor's: this one is not. */ @@ -146,6 +146,8 @@ def function(file,signature): struct dcp_parse_ctx { struct item *items;unsigned int count,index;bool key; }; struct iterator { struct dcp_parse_ctx *handle;unsigned int idx,len; }; #define kfree(p) free((void *)(p)) +#define GFP_KERNEL 0 +#define kstrdup(s,f) strdup(s) #define PTR_ERR(p) ((int)(intptr_t)(p)) static struct item announcement_items[3]; static int parse(u8 *p,size_t n,struct dcp_parse_ctx *ctx) @@ -574,7 +576,7 @@ def function(file, signature): av = (base / 'epic/dpavservep.c').read_text() af = (base / 'afk.c').read_text() - announce = af[af.index('static void afk_recv_handle_init('):af.index('static void afk_recv_handle_init(') + 6500] + announce = af[af.index('static void afk_recv_handle_init('):af.index('static void afk_recv_handle_init(') + 7000] assert announce.index('ops->prepare(service,') < announce.index('EPIC_CAT_REPORT, AFKV2_OPEN') assert '.prepare = dcpavserv_prepare,' in av and '.prepare = dcpdpserv_prepare,' in av prefix = r''' From ea4edea96760296fd793ef689fe98f87cc69c088 Mon Sep 17 00:00:00 2001 From: Thomas Glanzmann Date: Thu, 19 Nov 2026 22:31:15 +0100 Subject: [PATCH 17/29] drm/apple: open AFK services that have no handler An announced service that no entry of its endpoint's ops table names is left unopened, apart from two firmware-specific exceptions that bind it to the endpoint's first entry. The T8152 DCP firmware's DPAV controller endpoint announces four services and expects all of them to be opened, among them dcpav-video-interface, whose opening starts the firmware's idle power off timer for the display. None of them takes commands from the host. Add ep->unmatched_ops, bound to every announced service that no entry of the endpoint's ops table matches, which is then opened and initialised like a matched one. It is NULL on every existing endpoint, whose unmatched services stay unopened as before. The startup host test's and the apple-dcp service rebind test's endpoints gain the field. Signed-off-by: Thomas Glanzmann --- drivers/gpu/drm/apple/afk.c | 2 ++ drivers/gpu/drm/apple/afk.h | 5 +++++ tools/asahi/dcpext26-startup-tests.py | 2 +- tools/testing/selftests/drivers/gpu/apple-dcp/edid.py | 2 +- 4 files changed, 9 insertions(+), 2 deletions(-) diff --git a/drivers/gpu/drm/apple/afk.c b/drivers/gpu/drm/apple/afk.c index 630acfdc5af17..7b0e00d052b47 100644 --- a/drivers/gpu/drm/apple/afk.c +++ b/drivers/gpu/drm/apple/afk.c @@ -1857,6 +1857,8 @@ static void afk_recv_handle_init(struct apple_dcp_afkep *ep, u32 channel, if (!ops && dcp_is_ext26(ep->dcp) && ep->ops && !strcmp(ep->ops->name, "native26-observer")) ops = ep->ops; + if (!ops) + ops = ep->unmatched_ops; if (!ops) { dev_err(ep->dcp->dev, "AFK[ep:%02x]: unable to match service %s on channel %d\n", diff --git a/drivers/gpu/drm/apple/afk.h b/drivers/gpu/drm/apple/afk.h index b2c7c5deddb29..da51a5fa0daee 100644 --- a/drivers/gpu/drm/apple/afk.h +++ b/drivers/gpu/drm/apple/afk.h @@ -580,6 +580,11 @@ struct apple_dcp_afkep { void *compact_rx_data; const struct apple_epic_service_ops *ops; + /* + * Bound to an announced service no entry of @ops names, which is then + * opened like a matched one. NULL leaves such a service unopened. + */ + const struct apple_epic_service_ops *unmatched_ops; struct apple_epic_service services[AFK_MAX_CHANNEL]; u32 num_channels; diff --git a/tools/asahi/dcpext26-startup-tests.py b/tools/asahi/dcpext26-startup-tests.py index c71308f06d258..bc8a0aa89a992 100644 --- a/tools/asahi/dcpext26-startup-tests.py +++ b/tools/asahi/dcpext26-startup-tests.py @@ -95,7 +95,7 @@ def run(name,code,reject=False): struct apple_epic_service {struct apple_dcp_afkep *ep;const struct apple_epic_service_ops *ops;bool enabled,torndown,pending_open,defer_start;unsigned users;int lock;u32 channel;void *cookie;const char *pending_name,*pending_class;s64 pending_unit;int cmd_map[1];}; struct dptx_port {struct apple_epic_service *service;s64 unit;bool enabled,connected;int enable_completion;}; struct apple_dcp {const void *push_out_prefill;bool native,ext26_services_open,v27_j714s,v27_sleep_transport,dcpext;int fw_compat,index,dptx_controller_ready;struct {unsigned num_dptx_ports;int iomfb_method_profile,firmware_compat;bool afk_announced_names;} hw;struct dptx_port dptxport[2];void *dev;struct apple_dcp_afkep *systemep,*ibootep,*dptx_controller_ep,*dcpavservep,*dptxep,*ext26_aux[3];}; -struct apple_dcp_afkep {struct apple_dcp *dcp;u32 endpoint,num_channels;bool wire_v2,stopping,match_epic_name,compact;int lock;void *wq,*service_work;const struct apple_epic_service_ops *ops;struct apple_epic_service services[AFK_MAX_CHANNEL];}; +struct apple_dcp_afkep {struct apple_dcp *dcp;u32 endpoint,num_channels;bool wire_v2,stopping,match_epic_name,compact;int lock;void *wq,*service_work;const struct apple_epic_service_ops *ops,*unmatched_ops;struct apple_epic_service services[AFK_MAX_CHANNEL];}; static bool dcp_is_ext26(struct apple_dcp *d){return d->native;} static bool dcp_fail_stop(struct apple_dcp *d){return d->native;} static bool dcp_is_v14_7_external(struct apple_dcp *d){return false;} diff --git a/tools/testing/selftests/drivers/gpu/apple-dcp/edid.py b/tools/testing/selftests/drivers/gpu/apple-dcp/edid.py index bc5eb6f352967..b4ea075b85f27 100644 --- a/tools/testing/selftests/drivers/gpu/apple-dcp/edid.py +++ b/tools/testing/selftests/drivers/gpu/apple-dcp/edid.py @@ -95,7 +95,7 @@ def function(file,signature): bool pending_open;const char *pending_name,*pending_class;s64 pending_unit; }; /* v27_closed_teardown: set only by a J714s firmware 27 sleep. */ -struct apple_dcp_afkep { u8 *command_tags;bool lock,stopping;void *wq;struct work_struct service_work;struct apple_dcp *dcp;const struct apple_epic_service_ops *ops;bool wire_v2,inline_v2,match_epic_name,v27_closed_teardown;u32 endpoint;unsigned int num_channels;struct apple_epic_service services[AFK_MAX_CHANNEL]; }; +struct apple_dcp_afkep { u8 *command_tags;bool lock,stopping;void *wq;struct work_struct service_work;struct apple_dcp *dcp;const struct apple_epic_service_ops *ops,*unmatched_ops;bool wire_v2,inline_v2,match_epic_name,v27_closed_teardown;u32 endpoint;unsigned int num_channels;struct apple_epic_service services[AFK_MAX_CHANNEL]; }; struct apple_connector { void *dcp;bool connected,edid_invalidation_pending;struct { int registration_state; } base;unsigned int hotplug_wq; }; struct apple_dcp { void *dev;int fw_compat;struct apple_dcp_afkep *dcpavservep;struct apple_connector *connector; From 10c6cd1cc6c24339f17255eef0b48c3ed1905224 Mon Sep 17 00:00:00 2001 From: Thomas Glanzmann Date: Thu, 19 Nov 2026 22:33:32 +0100 Subject: [PATCH 18/29] drm/apple: let the match data set the AFK timeout AFK waits one second for an endpoint to start or stop and for a command or service call to be answered. The T8152 DCP firmware's iBoot display commands answer only once the DisplayPort work they start is done: SET_POWER and SET_MODE wait for the link training, during which the firmware calls into the AP for the PHY. The T8152 displays have been run on the hardware with three seconds for each of these. Add hw.afk_timeout_ms, the time afk_start(), afk_shutdown(), the inherited endpoint stop, afk_send_command(), afk_service_call() and afk_service_call_len() wait. Zero keeps one second, so every processor that sets nothing waits as before. Callers that pass their own timeout, and the legacy reply-length calls, are unchanged. Signed-off-by: Thomas Glanzmann --- drivers/gpu/drm/apple/afk.c | 27 +++++++++++++++++++++------ drivers/gpu/drm/apple/dcp-internal.h | 6 ++++++ 2 files changed, 27 insertions(+), 6 deletions(-) diff --git a/drivers/gpu/drm/apple/afk.c b/drivers/gpu/drm/apple/afk.c index 7b0e00d052b47..6b65f13e05209 100644 --- a/drivers/gpu/drm/apple/afk.c +++ b/drivers/gpu/drm/apple/afk.c @@ -83,6 +83,18 @@ void afk_service_retry(struct apple_dcp_afkep *ep) } EXPORT_SYMBOL_IF_KUNIT(afk_service_retry); +/* How long the host waits for the firmware: the processor's own, or one second. */ +static unsigned int afk_timeout_ms(struct apple_dcp_afkep *ep) +{ + return ep->dcp->hw.afk_timeout_ms ?: MSEC_PER_SEC; +} + +/* The same for a service the caller may not have; the call fails without one. */ +static unsigned int afk_service_timeout_ms(struct apple_epic_service *service) +{ + return service ? afk_timeout_ms(service->ep) : MSEC_PER_SEC; +} + static void afk_send(struct apple_dcp_afkep *ep, u64 message) { dcp_send_message(ep->dcp, ep->endpoint, message); @@ -185,7 +197,8 @@ void afk_shutdown(struct apple_dcp_afkep *afkep) afk_send(afkep, FIELD_PREP(RBEP_TYPE, RBEP_SHUTDOWN)); int ret; - ret = wait_for_completion_timeout(&afkep->stopped, msecs_to_jiffies(1000)); + ret = wait_for_completion_timeout(&afkep->stopped, + msecs_to_jiffies(afk_timeout_ms(afkep))); if (ret <= 0) { dev_err(afkep->dcp->dev, "Timed out shutting down AFK endpoint %02x", afkep->endpoint); } @@ -253,7 +266,8 @@ static int afk_stop_live(struct apple_dcp_afkep *ep) return 0; reinit_completion(&ep->stopped); afk_send(ep, FIELD_PREP(RBEP_TYPE, RBEP_SHUTDOWN)); - if (!wait_for_completion_timeout(&ep->stopped, msecs_to_jiffies(1000))) { + if (!wait_for_completion_timeout(&ep->stopped, + msecs_to_jiffies(afk_timeout_ms(ep)))) { dev_err(ep->dcp->dev, "AFK[ep:%02x]: inherited endpoint did not stop\n", ep->endpoint); return -ETIMEDOUT; @@ -296,7 +310,8 @@ int afk_start(struct apple_dcp_afkep *ep) return afk_start_polled(ep); afk_send(ep, FIELD_PREP(RBEP_TYPE, RBEP_INIT)); - ret = wait_for_completion_timeout(&ep->started, msecs_to_jiffies(1000)); + ret = wait_for_completion_timeout(&ep->started, + msecs_to_jiffies(afk_timeout_ms(ep))); if (ret <= 0 && ep->dcp->hw.iomfb_method_profile == DCP_IOMFB_METHODS_H17P_EXTERNAL) dev_warn(ep->dcp->dev, @@ -3187,7 +3202,7 @@ int afk_send_command(struct apple_epic_service *service, u8 type, { return afk_send_command_timeout(service, type, payload, payload_len, output, output_len, retcode, - MSEC_PER_SEC); + afk_service_timeout_ms(service)); } static u16 afk_command_tag(struct apple_epic_service *service, u8 slot) @@ -3503,7 +3518,7 @@ int afk_service_call(struct apple_epic_service *service, u16 group, u32 command, { return afk_service_call_timeout(service, group, command, data, data_len, data_pad, output, output_len, - output_pad, MSEC_PER_SEC); + output_pad, afk_service_timeout_ms(service)); } int afk_service_call_len(struct apple_epic_service *service, u16 group, @@ -3513,7 +3528,7 @@ int afk_service_call_len(struct apple_epic_service *service, u16 group, { return afk_service_call_timeout_len(service, group, command, data, data_len, data_pad, output, output_len, output_pad, - MSEC_PER_SEC, received); + afk_service_timeout_ms(service), received); } int afk_service_call_timeout(struct apple_epic_service *service, u16 group, diff --git a/drivers/gpu/drm/apple/dcp-internal.h b/drivers/gpu/drm/apple/dcp-internal.h index 1621c62f1a4a0..f3a8c15ec3d74 100644 --- a/drivers/gpu/drm/apple/dcp-internal.h +++ b/drivers/gpu/drm/apple/dcp-internal.h @@ -362,6 +362,12 @@ struct apple_dcp_hw_data { * properties after it are not parsed (afk_recv_handle_init()). */ bool afk_announced_names; + /* + * How long AFK waits for the firmware to start or stop an endpoint + * and to answer a command or service call (afk_timeout_ms()); zero + * for one second. + */ + unsigned int afk_timeout_ms; }; enum dcp_test_point { From 842c9c424757b9e78eb5d110cadf3f36d902e8fb Mon Sep 17 00:00:00 2001 From: Thomas Glanzmann Date: Thu, 19 Nov 2026 22:37:26 +0100 Subject: [PATCH 19/29] drm/apple: add iBoot SET_SURFACE and display commands for other users ibootep.c sends the iBoot interface's display commands only for the T6030 external role's first-pixel diagnostic: the queries and setters are reachable from its sysfs file and pattern path alone, gated on dcp->external, and the surface is set through the swap operations 15/16/18/19. The T8152 DCP firmware is driven through this interface alone and sets its surface with operation 1, SET_SURFACE. - Add DCP_FIRMWARE_IBOOT, a DCP driven through the iBoot interface alone, and dcp_is_iboot(). No match entry selects it yet. - Build SET_SURFACE (operation 1, one 164-byte layer) and the three queries in iboot_build_pattern_request(). - Export ibootep_set_power(), ibootep_get_hpd(), ibootep_get_timing_modes(), ibootep_get_color_modes(), ibootep_set_mode() and ibootep_set_surface(), with the mode records in ibootep.h. On AFKv2 the setters go out without a reply buffer, as the firmware asserts on one, and a reply is checked against the length the transport reports, through the new afk_send_command_len(). SET_SURFACE scans out one linear BGRA plane in colour space 2, as the bootloader sets it on T8152. - Open the service for a DCP_FIRMWARE_IBOOT processor under either of its names (disp0-service on the main DCP, dispext-service on the external ones), without the manual query file: its display driver sends every command. The T6030 external role keeps its gate, its service name, its sysfs file and its pattern and frame commands as they were. Signed-off-by: Thomas Glanzmann --- drivers/gpu/drm/apple/afk.c | 9 ++ drivers/gpu/drm/apple/afk.h | 7 + drivers/gpu/drm/apple/dcp-internal.h | 6 + drivers/gpu/drm/apple/ibootep.c | 205 +++++++++++++++++++++++++- drivers/gpu/drm/apple/ibootep.h | 38 +++++ drivers/gpu/drm/apple/version_utils.h | 6 + 6 files changed, 266 insertions(+), 5 deletions(-) diff --git a/drivers/gpu/drm/apple/afk.c b/drivers/gpu/drm/apple/afk.c index 6b65f13e05209..ad684ae5e954b 100644 --- a/drivers/gpu/drm/apple/afk.c +++ b/drivers/gpu/drm/apple/afk.c @@ -3247,6 +3247,15 @@ int afk_send_command_timeout(struct apple_epic_service *service, u8 type, output, output_len, retcode, timeout_ms, NULL); } +int afk_send_command_len(struct apple_epic_service *service, u8 type, + const void *payload, size_t payload_len, void *output, + size_t output_len, u32 *retcode, size_t *received) +{ + return afk_send_command_timeout_len(service, type, payload, payload_len, + output, output_len, retcode, + afk_service_timeout_ms(service), received); +} + /* * @reply_len (legacy EPIC only): return just the bytes the firmware reported * in its reply instead of the whole allocated output. diff --git a/drivers/gpu/drm/apple/afk.h b/drivers/gpu/drm/apple/afk.h index da51a5fa0daee..c01022b6b0f87 100644 --- a/drivers/gpu/drm/apple/afk.h +++ b/drivers/gpu/drm/apple/afk.h @@ -646,6 +646,13 @@ int afk_send_command_timeout(struct apple_epic_service *service, u8 type, const void *payload, size_t payload_len, void *output, size_t output_len, u32 *retcode, unsigned int timeout_ms); +/* + * As afk_send_command(), and @received is set to the number of bytes of + * @output the reply filled: an AFKv2 reply may be shorter than @output_len. + */ +int afk_send_command_len(struct apple_epic_service *service, u8 type, + const void *payload, size_t payload_len, void *output, + size_t output_len, u32 *retcode, size_t *received); int afk_service_call(struct apple_epic_service *service, u16 group, u32 command, const void *data, size_t data_len, size_t data_pad, void *output, size_t output_len, size_t output_pad); diff --git a/drivers/gpu/drm/apple/dcp-internal.h b/drivers/gpu/drm/apple/dcp-internal.h index f3a8c15ec3d74..9618011b29c56 100644 --- a/drivers/gpu/drm/apple/dcp-internal.h +++ b/drivers/gpu/drm/apple/dcp-internal.h @@ -1211,6 +1211,12 @@ struct apple_dcp { struct dcp_cec *cec; }; +/* A DCP driven through the firmware's iBoot interface alone. */ +static inline bool dcp_is_iboot(const struct apple_dcp *dcp) +{ + return dcp->fw_compat == DCP_FIRMWARE_IBOOT; +} + /* T8132 firmware uses its own wire codec, with the common DRM/KMS owner. */ static inline bool iomfb_is_m4(const struct apple_dcp *dcp) { diff --git a/drivers/gpu/drm/apple/ibootep.c b/drivers/gpu/drm/apple/ibootep.c index 7f90f68bf26f6..1a12fb947d2cc 100644 --- a/drivers/gpu/drm/apple/ibootep.c +++ b/drivers/gpu/drm/apple/ibootep.c @@ -22,9 +22,12 @@ #define IBOOT_PATTERN_STRIDE DCPEXT_STRIDE enum iboot_query_op { + IBOOT_SET_SURFACE = 1, + IBOOT_SET_POWER = 2, IBOOT_GET_HPD = 3, IBOOT_GET_TIMING_MODES = 4, IBOOT_GET_COLOR_MODES = 5, + IBOOT_SET_MODE = 6, }; /* Packed layouts from m1n1 src/dcp_iboot.h and swap_set_layer_cmd_v13_3. */ @@ -148,15 +151,25 @@ static int iboot_parse_swap_begin(const u8 *reply, size_t received, u32 *id) return 0; } -static int iboot_parse_modes(const u8 *payload, size_t size, u32 *count) +/* A mode list: a count, then as many 24-byte records. */ +static int iboot_parse_mode_count(const u8 *payload, size_t size, u32 *count) { - u32 i; - if (size < 4) return -EPROTO; *count = get_unaligned_le32(payload); if (*count > (size - 4) / IBOOT_MODE_SIZE) return -EPROTO; + return 0; +} + +static int iboot_parse_modes(const u8 *payload, size_t size, u32 *count) +{ + u32 i; + int ret; + + ret = iboot_parse_mode_count(payload, size, count); + if (ret) + return ret; for (i = 0; i < *count; i++) if (get_unaligned_le32(payload + 4 + i * IBOOT_MODE_SIZE) > 1) return -EPROTO; @@ -234,7 +247,13 @@ static int iboot_build_pattern_request(u32 op, const void *data, size_t data_siz size_t expected; switch (op) { + case 1: /* SetSurface */ + expected = sizeof(struct iboot_layer); + break; case 2: expected = 1; break; /* SetPower */ + case 3 ... 5: /* GetHPD, GetTimingModes, GetColorModes */ + expected = 0; + break; case 6: expected = 2 * IBOOT_MODE_SIZE; break; /* SetMode */ case 15: expected = 0; break; /* SwapBegin */ case 16: expected = sizeof(struct iboot_swap_layer_v13_3); break; @@ -401,7 +420,7 @@ static struct iboot_query *iboot_find_query(struct apple_dcp *dcp) struct apple_dcp_afkep *ep = READ_ONCE(dcp->ibootep); u32 i; - if (!dcp->external || !ep) + if ((!dcp->external && !dcp_is_iboot(dcp)) || !ep) return ERR_PTR(-ENODEV); for (i = 0; i < AFK_MAX_CHANNEL; i++) { struct iboot_query *query = smp_load_acquire(&ep->services[i].cookie); @@ -668,6 +687,157 @@ int ibootep_present_pattern(struct apple_dcp *dcp, u64 iova, size_t size, u32 st return ret; } +/* + * One display command of the iBoot interface, with its reply payload copied + * to @out (at least @min_size bytes of it; returns the bytes copied), or a + * setter if @out is NULL. On AFKv2 the setters answer with a transport + * status alone: given a reply buffer, the firmware copies an empty reply + * into it and asserts, so they go out without one. Only the reply's own + * length is checked, not that its first word repeats @op. + */ +static int iboot_command(struct apple_dcp *dcp, u32 op, const void *data, + size_t size, void *out, size_t out_size, size_t min_size) +{ + struct iboot_query *query = iboot_find_query(dcp); + u8 request[16 + sizeof(struct iboot_swap_layer_v13_3)]; + size_t len, received = 0; + u32 retcode = 0, reply_len; + u8 *reply = NULL; + int ret; + + if (IS_ERR(query)) + return PTR_ERR(query); + ret = iboot_build_pattern_request(op, data, size, request, + sizeof(request), &len); + if (ret) + return ret; + if (out || !query->service->ep->wire_v2) { + reply = kzalloc(IBOOT_QUERY_RX_SIZE, GFP_KERNEL); + if (!reply) + return -ENOMEM; + } + ret = afk_send_command_len(query->service, EPIC_SUBTYPE_STD_SERVICE, + request, len, reply, + reply ? IBOOT_QUERY_RX_SIZE : 0, &retcode, + &received); + if (!ret && retcode) { + dev_dbg(dcp->dev, "iBoot command %u returned %#x\n", op, retcode); + ret = -EIO; + } + if (ret || !out) + goto out; + reply_len = received >= 8 ? get_unaligned_le32(reply + 4) : 0; + if (reply_len < 8 || reply_len > received || reply_len - 8 < min_size) { + dev_err(dcp->dev, "iBoot: bad response length for command %u\n", op); + ret = -EPROTO; + goto out; + } + ret = min_t(size_t, reply_len - 8, out_size); + memcpy(out, reply + 8, ret); +out: + kfree(reply); + return ret; +} + +int ibootep_set_power(struct apple_dcp *dcp, bool on) +{ + u8 power = on; + + return iboot_command(dcp, IBOOT_SET_POWER, &power, sizeof(power), NULL, + 0, 0); +} + +int ibootep_get_hpd(struct apple_dcp *dcp, u32 *timings, u32 *colors) +{ + u8 payload[12]; + u32 timing_count, color_count; + bool hpd; + int ret; + + ret = iboot_command(dcp, IBOOT_GET_HPD, NULL, 0, payload, sizeof(payload), + sizeof(payload)); + if (ret >= 0) + ret = iboot_parse_hpd(payload, ret, &hpd, &timing_count, + &color_count); + if (ret) + return ret; + if (timings) + *timings = timing_count; + if (colors) + *colors = color_count; + return hpd; +} + +static int iboot_get_modes(struct apple_dcp *dcp, u32 op, void **modes, + unsigned int *count) +{ + u32 n; + u8 *buf; + int ret; + + buf = kzalloc(IBOOT_QUERY_RX_SIZE, GFP_KERNEL); + if (!buf) + return -ENOMEM; + ret = iboot_command(dcp, op, NULL, 0, buf, IBOOT_QUERY_RX_SIZE, 4); + if (ret >= 0) + ret = iboot_parse_mode_count(buf, ret, &n); + if (!ret) { + *modes = kmemdup(buf + 4, n * IBOOT_MODE_SIZE, GFP_KERNEL); + if (*modes) + *count = n; + else + ret = -ENOMEM; + } + kfree(buf); + return ret; +} + +int ibootep_get_timing_modes(struct apple_dcp *dcp, + struct iboot_timing_mode **modes, + unsigned int *count) +{ + return iboot_get_modes(dcp, IBOOT_GET_TIMING_MODES, (void **)modes, count); +} + +int ibootep_get_color_modes(struct apple_dcp *dcp, + struct iboot_color_mode **modes, unsigned int *count) +{ + return iboot_get_modes(dcp, IBOOT_GET_COLOR_MODES, (void **)modes, count); +} + +int ibootep_set_mode(struct apple_dcp *dcp, const struct iboot_timing_mode *timing, + const struct iboot_color_mode *color) +{ + struct { + struct iboot_timing_mode timing; + struct iboot_color_mode color; + } __packed mode = { *timing, *color }; + + return iboot_command(dcp, IBOOT_SET_MODE, &mode, sizeof(mode), NULL, 0, 0); +} + +int ibootep_set_surface(struct apple_dcp *dcp, u64 iova, u32 width, u32 height, + u32 stride) +{ + /* One linear BGRA plane, SDR, in colour space 2 as the bootloader sets. */ + struct iboot_layer layer = { + .planes = { { + .addr = cpu_to_le64(iova), + .stride = cpu_to_le32(stride), + .addr_format = cpu_to_le32(1), + } }, + .plane_count = cpu_to_le32(1), + .width = cpu_to_le32(width), + .height = cpu_to_le32(height), + .surface_format = cpu_to_le32(1), + .colorspace = cpu_to_le32(2), + .eotf = cpu_to_le32(1), + }; + + return iboot_command(dcp, IBOOT_SET_SURFACE, &layer, sizeof(layer), NULL, + 0, 0); +} + static ssize_t dcpext_modes_query_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count) @@ -699,7 +869,7 @@ static void disp_service_init(struct apple_epic_service *service, const char *na struct iboot_query *query; int ret; - if (!dcp->external) + if (!dcp->external && !dcp_is_iboot(dcp)) return; query = devm_kzalloc(dcp->dev, sizeof(*query), GFP_KERNEL); if (!query) @@ -710,6 +880,16 @@ static void disp_service_init(struct apple_epic_service *service, const char *na ret = devm_add_action_or_reset(dcp->dev, iboot_query_cancel, query); if (ret) return; + /* + * A DCP driven through this interface alone has a display driver that + * sends every command (the exported ibootep_* calls); it takes no + * manual queries next to them. + */ + if (dcp_is_iboot(dcp)) { + /* Published for iboot_find_query()'s acquire. */ + smp_store_release(&service->cookie, query); + return; + } /* Remove the sysfs entry before cancelling work during device cleanup. */ ret = devm_device_add_group(dcp->dev, &iboot_query_group); if (ret) { @@ -741,6 +921,19 @@ static const struct apple_epic_service_ops external_ibootep_ops[] = { {} }; +/* DCP_FIRMWARE_IBOOT: the main DCP's service, or an external one's. */ +static const struct apple_epic_service_ops iboot_variant_ops[] = { + { + .name = "disp0-service", + .init = disp_service_init, + }, + { + .name = "dispext-service", + .init = disp_service_init, + }, + {} +}; + int ibootep_init(struct apple_dcp *dcp) { const struct apple_epic_service_ops *ops = ibootep_ops; @@ -751,6 +944,8 @@ int ibootep_init(struct apple_dcp *dcp) return -EBUSY; if (dcp->hw.iomfb_method_profile == DCP_IOMFB_METHODS_H17P_EXTERNAL) ops = external_ibootep_ops; + if (dcp_is_iboot(dcp)) + ops = iboot_variant_ops; dcp->ibootep = afk_init(dcp, DISP0_ENDPOINT, ops); if (IS_ERR(dcp->ibootep)) { ret = PTR_ERR(dcp->ibootep); diff --git a/drivers/gpu/drm/apple/ibootep.h b/drivers/gpu/drm/apple/ibootep.h index aa44c2f8754e4..aa6ad17a61be1 100644 --- a/drivers/gpu/drm/apple/ibootep.h +++ b/drivers/gpu/drm/apple/ibootep.h @@ -27,4 +27,42 @@ int ibootep_rearm_pattern(struct apple_dcp *dcp); */ int ibootep_present_frame(struct apple_dcp *dcp, u64 iova, size_t size, u32 stride); +/* + * The iBoot interface's display commands, for a DCP driven through it + * (DCP_FIRMWARE_IBOOT) and for the T6030 external role. Sleepable callers + * outside the AFK receive queue; the caller serializes them. + */ +struct iboot_timing_mode { + __le32 valid; + __le32 width; + __le32 height; + __le32 fps; /* 16.16 */ + /* byte 0 bit 6: a standard mode the firmware adds on its own */ + u8 flags[8]; +} __packed; + +struct iboot_color_mode { + __le32 valid; + __le32 colorimetry; + __le32 eotf; + __le32 encoding; + __le32 bpp; + u8 pad[4]; +} __packed; + +int ibootep_set_power(struct apple_dcp *dcp, bool on); +/* 1 with a sink, 0 without; the counts of its timing and colour modes. */ +int ibootep_get_hpd(struct apple_dcp *dcp, u32 *timings, u32 *colors); +/* The sink's mode lists, newly allocated; the caller frees them. */ +int ibootep_get_timing_modes(struct apple_dcp *dcp, + struct iboot_timing_mode **modes, + unsigned int *count); +int ibootep_get_color_modes(struct apple_dcp *dcp, + struct iboot_color_mode **modes, unsigned int *count); +int ibootep_set_mode(struct apple_dcp *dcp, const struct iboot_timing_mode *timing, + const struct iboot_color_mode *color); +/* SET_SURFACE: scan out one linear BGRA plane at @iova. */ +int ibootep_set_surface(struct apple_dcp *dcp, u64 iova, u32 width, u32 height, + u32 stride); + #endif diff --git a/drivers/gpu/drm/apple/version_utils.h b/drivers/gpu/drm/apple/version_utils.h index 8cf36e39d3f24..fe19968ee5f1e 100644 --- a/drivers/gpu/drm/apple/version_utils.h +++ b/drivers/gpu/drm/apple/version_utils.h @@ -62,6 +62,12 @@ enum dcp_firmware_version { */ DCP_FIRMWARE_V_26_6_DESKTOP, DCP_FIRMWARE_V_27_0, + /* + * A DCP driven through its firmware's iBoot interface alone + * (disp0-service, dispext-service; see ibootep.c), not through + * IOMFB: the T8152 one. + */ + DCP_FIRMWARE_IBOOT, }; #endif /*__APPLE_VERSION_UTILS_H__*/ From 9ab600a3f46e2a6cb3c673816e47f9bd3b359114 Mon Sep 17 00:00:00 2001 From: Thomas Glanzmann Date: Thu, 19 Nov 2026 22:48:46 +0100 Subject: [PATCH 20/29] drm/apple: add the interim T8152 DisplayPort hardware The T8152 (Mac mini J873g) DCPs drive their displays through hardware no subsystem driver covers yet: the rear USB-C ports' ATC PHYs have no DisplayPort mode in drivers/phy/apple/atc.c for T8152, the HDMI port's DisplayPort PHY has no T8152 type in drivers/phy/apple/dptx.c (its lanes are 0x2800 apart from 0x4500), the PS190 DisplayPort-to-HDMI converter behind it is powered through the SMC, the ports' ACE3 PD controllers do not report to the Type-C route layer, and the DCP firmware's iBoot interface does not hand out the sink's EDID. Add the T8152 code for them as separate files, each marked interim until atc.c's DisplayPort mode, tipd and the Type-C route layer take over. The bootloader trains the displays before Linux starts; the PHYs keep its setup and reprogram what a link training changes, as the DCP firmware calls into the AP for them. t8152_atc.c, the USB-C ports: - The three DCPs share the three rear ports through the one display crossbar. A DCP claims a free ATC under a mutex and gives it up on unplug; a monitor stays with its DCP while userspace blanks it. The crossbar's upstream reset, clock and enable fields are indexed by DCP endpoint * 2 + DPTX port, independently of the downstream DFP0 mux fields, which select that endpoint. - Each ATC has its own PMGR DP clock divider, at 0x300020044 + 8 * n. - The ATC's DP clock switch (PHY CFG block + 0x14 select, + 0x18 enable, + 0x1c status) chooses where each DP pixel clock comes from. PCLK1 is switched to PMGR DP clock 0 (select field 1, enable and status bit 1) after the divider for the link rate is written, and switched off the same way. With source 0 selected the stream clock never followed the divider: modes above about 165 MHz pixel clock, and 640x480, stayed black while the link trained and the firmware set the mode. - A monitor that also carries USB3 negotiates pin assignment D (data status bit 5): two DisplayPort lanes, and only the PMA that carries them is set up. GET_MAX_LANE_COUNT reports the route's lanes. - A cold start powers the ATC's dependencies (USB AON, the common PSD, both DP power states), the common block and AUX, and starts the lanes at HBR at once: from cold, AUX only works with the DP PLL and PMAs running. A power down undoes what it powered up. - The EDID is read through the AP AUX controller aux0, which the three DCPs share and take in turn; its output select for an ATC port is the ATC instance * 3 + its local DFP. t8152_hdmi.c, the HDMI port: - The bootloader wires the HDMI PHY (source select, APB 0x10) to the main DCP (0) when no USB-C display is attached at boot, and then never starts DCPEXT0; next to a USB-C display it wires DCPEXT0 (1). The source select is read only while the PHY's PMGR power state is requested and active: a USB-C-only boot leaves the PHY off, and a read of its registers then raises an SError. - When the bootloader left HDMI off (no monitor at boot leaves its PLL unlocked), the HDMI port becomes one of the shared ports. A DCP that drives no USB-C display takes it when the HPD line goes high, starts the PHY from cold (PMGR and PSD power, the PS190, source select, PLL, AUX and PHY power, the tunables, PLL and PHY activation, four lanes, impedance calibration), keeps the link up across display off, and gives the port back once the sink is gone. The source select takes every DCP's crossbar endpoint, DCPEXT1's 2 included, so DCPEXT1 takes the port while the other two drive USB-C monitors. The HPD line is requested once for all of them. - The PS190 is powered through the SMC when the port is set up, before anything reads HPD or an EDID: it drives the HPD line only while it is powered, and while it is off every AUX request on the USB-C ports ends with channel status 2 as well. Without the SMC bound yet, the setup is deferred. t8152_aux.c: the AUX controllers' request framing. Bits 21:8 of the retry register are a 14-bit reply timeout, set to 3200; read as a three-bit delay, the EDID through a USB-C to HDMI adapter timed out. A sink left in D3 is woken before DDC. t8152_pd.c: the ACE3 plug, orientation, DP alt mode and HPD state over SPMI, read through the logical register window under the SPMI device's lock, which the USB4 port monitor takes as well. A controller in power state 7 enters alt mode only after SSPS 0. A sink that stopped answering AUX after a monitor's deep sleep comes back after a reset (Gaid) and SSPS 0. t8152_dptx.c: the DPTX port service's calls into the AP apply link rate, lanes and drive settings to these PHYs and read the drive settings back as they were set; an unsupported GET_DRIVE_SETTINGS was followed by a firmware crash. The port's release is group 8 command 7, and its HPD call (group 8 command 8) carries a status in its reply's first word. dptxep.c serves the same calls for PHY framework PHYs, the crossbar mux and Thunderbolt tunnels; its connect and display request, its call numbers and its port records are used here. The HDMI PHY's power state check is based on a change by Acelogic. The files are built with the new DRM_APPLE_DCP_T8152, enabled in asahi.config, and nothing uses them yet. A MAINTAINERS entry for the T8152 display covers them and the binding. Signed-off-by: Thomas Glanzmann --- MAINTAINERS | 7 + arch/arm64/configs/asahi.config | 1 + drivers/gpu/drm/apple/Kconfig | 16 + drivers/gpu/drm/apple/Makefile | 2 + drivers/gpu/drm/apple/dcp-internal.h | 3 + drivers/gpu/drm/apple/t8152_atc.c | 924 ++++++++++++++++++++++++++ drivers/gpu/drm/apple/t8152_aux.c | 200 ++++++ drivers/gpu/drm/apple/t8152_display.h | 140 ++++ drivers/gpu/drm/apple/t8152_dptx.c | 315 +++++++++ drivers/gpu/drm/apple/t8152_hdmi.c | 669 +++++++++++++++++++ drivers/gpu/drm/apple/t8152_pd.c | 211 ++++++ 11 files changed, 2488 insertions(+) create mode 100644 drivers/gpu/drm/apple/t8152_atc.c create mode 100644 drivers/gpu/drm/apple/t8152_aux.c create mode 100644 drivers/gpu/drm/apple/t8152_display.h create mode 100644 drivers/gpu/drm/apple/t8152_dptx.c create mode 100644 drivers/gpu/drm/apple/t8152_hdmi.c create mode 100644 drivers/gpu/drm/apple/t8152_pd.c diff --git a/MAINTAINERS b/MAINTAINERS index cb48635b6e368..1afb033e7ffcd 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -2151,6 +2151,13 @@ F: tools/testing/kunit/configs/mt7932.config F: tools/testing/selftests/drivers/apple/aop-las.py F: tools/testing/selftests/drivers/gpu/apple-dcp/ +APPLE T8152 DCP DISPLAY DRIVER (IBOOT INTERFACE) +M: Thomas Glanzmann +L: dri-devel@lists.freedesktop.org +S: Maintained +F: Documentation/devicetree/bindings/display/apple,t8152-dcp-iboot.yaml +F: drivers/gpu/drm/apple/t8152_* + APPLETALK NETWORK LAYER L: netdev@vger.kernel.org S: Obsolete diff --git a/arch/arm64/configs/asahi.config b/arch/arm64/configs/asahi.config index 3173021d4f19d..ff74ba4c8dad6 100644 --- a/arch/arm64/configs/asahi.config +++ b/arch/arm64/configs/asahi.config @@ -88,6 +88,7 @@ CONFIG_DRM_ASAHI_M3_T8122=y CONFIG_DRM_ADP=m CONFIG_DRM_APPLE=m CONFIG_DRM_APPLE_DCP_T8132=y +CONFIG_DRM_APPLE_DCP_T8152=y CONFIG_DRM_APPLE_CEC=y CONFIG_DRM_APPLE_AUDIO=y CONFIG_DRM_APPLE_DCP_T6030_AUDIO=y diff --git a/drivers/gpu/drm/apple/Kconfig b/drivers/gpu/drm/apple/Kconfig index 99632ffe1535c..d27d975da6f41 100644 --- a/drivers/gpu/drm/apple/Kconfig +++ b/drivers/gpu/drm/apple/Kconfig @@ -75,6 +75,22 @@ config DRM_APPLE_DCP_T8132 Load appledrm explicitly before the GPU, with its alias autoload disabled, after guarded admission of the inherited display DARTs. +config DRM_APPLE_DCP_T8152 + bool "Mac mini M6 (J873g) displays" + depends on DRM_APPLE + depends on SPMI=y || SPMI=DRM_APPLE + depends on MFD_MACSMC=y || MFD_MACSMC=DRM_APPLE + help + Lets the common Apple display driver drive the three display + coprocessors of the T8152 Mac mini (J873g) through their firmware's + iBoot interface: the HDMI port, whose DisplayPort-to-HDMI converter + is powered through the SMC, and displays on the rear USB-C ports, + whose PD controllers are polled over SPMI. + + The boot loader hands each coprocessor over, running or quiesced, + with the AFK endpoints it left running. The adopted sessions cannot + be stopped; their DMA stays retained until reboot. + config DRM_APPLE_DEBUG bool "Enable additional driver debugging" depends on DRM_APPLE diff --git a/drivers/gpu/drm/apple/Makefile b/drivers/gpu/drm/apple/Makefile index a6687d7cccd65..3a0953cf6628a 100644 --- a/drivers/gpu/drm/apple/Makefile +++ b/drivers/gpu/drm/apple/Makefile @@ -18,6 +18,8 @@ appledrm-y += iomfb_v14_7_tpl.o appledrm-y += iomfb_v26_6_desktop.o appledrm-y += iomfb_v14_7.o iomfb_v14_7_link.o appledrm-$(CONFIG_DRM_APPLE_DCP_T8132) += iomfb_m4.o t8132_ext_drm.o +appledrm-$(CONFIG_DRM_APPLE_DCP_T8152) += t8152_atc.o t8152_aux.o t8152_dptx.o +appledrm-$(CONFIG_DRM_APPLE_DCP_T8152) += t8152_hdmi.o t8152_pd.o appledrm-y += iomfb_h17p.o appledrm-$(CONFIG_DRM_APPLE_KUNIT_TEST) += iomfb_h17p_test.o parser_test.o appledrm-$(CONFIG_DRM_APPLE_KUNIT_TEST) += iomfb_external_hdr_hints_test.o diff --git a/drivers/gpu/drm/apple/dcp-internal.h b/drivers/gpu/drm/apple/dcp-internal.h index 9618011b29c56..857ece5849156 100644 --- a/drivers/gpu/drm/apple/dcp-internal.h +++ b/drivers/gpu/drm/apple/dcp-internal.h @@ -462,6 +462,7 @@ struct dcp_desktop_state { /* TODO: move IOMFB members to its own struct */ struct dcpext_scanout; +struct t8152_display; struct dcp_hdmi; struct dcp_cec; @@ -584,6 +585,8 @@ struct apple_dcp { /* firmware version and compatible firmware version */ enum dcp_firmware_version fw_compat; + /* T8152: the DisplayPort hardware behind the DPTX port (t8152_display.h) */ + struct t8152_display *t8152; /* Bumped by every iomfb poweron; see afk_compact_slot(). */ u32 afk_power_gen; /* diff --git a/drivers/gpu/drm/apple/t8152_atc.c b/drivers/gpu/drm/apple/t8152_atc.c new file mode 100644 index 0000000000000..ce4b6a84602de --- /dev/null +++ b/drivers/gpu/drm/apple/t8152_atc.c @@ -0,0 +1,924 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * T8152 (J873g) ATC PHYs as DisplayPort outputs of the DCPs' DPTX ports. + * + * Interim, until drivers/phy/apple/atc.c has a DisplayPort mode for T8152 + * and the display crossbar driver and the Type-C route layer of + * dcp-fabric.c take over the route and the choice of port: this programs + * the PHY's DisplayPort lanes, its pixel clock and the crossbar route + * itself. + * + * The bootloader trains a USB-C display before Linux starts; this takes + * that link over and then follows the firmware: link rate changes (stop and + * set up the lanes again), drive settings, the DCP -> ATCn/DFP0 crossbar + * route, and with the display off or unplugged a full power down and a cold + * start. A DCP drives one USB-C port at a time: the one it claims from the + * shared ports while polling their PD controllers. + */ + +#include +#include +#include +#include +#include +#include +#include +#include + +#include "dptxep.h" +#include "t8152_display.h" + +#define ATC_AON 0x1e00 +#define ATC_CFG 0x60000 +#define ATC_XBAR 0x74000 +/* + * The DP clock switch at CFG + 0x14 (select), + 0x18 (enable) and + 0x1c + * (status). DP pixel clock n (1..4) runs from switch n: select field n is n + * for PMGR DP clock n - 1, 7 for none, and enable/status bit 1 + 7 * (n - 1) + * gates that PMGR clock. PCLK1 is used here, from PMGR DP0, whose divider + * atc_clock_write() sets for the link rate. + */ +#define ATC_CLKSW_SELECT (ATC_CFG + 0x14) +#define ATC_CLKSW_ENABLE (ATC_CFG + 0x18) +#define ATC_CLKSW_STATUS (ATC_CFG + 0x1c) +#define ATC_CLKSW_PCLK1_SEL GENMASK(2, 0) +#define ATC_CLKSW_PCLK1_MODE BIT(12) +#define ATC_CLKSW_PCLK1_OFF 7 +#define ATC_CLKSW_PCLK1_DP0 1 +#define ATC_CLKSW_PCLK1_EN BIT(1) +#define ATC_TIMEOUT_US 10000 + +struct t8152_atc_port { + void __iomem *base; + unsigned int index; /* ATC instance */ + /* display crossbar DFP endpoint of this ATC */ + u32 dfp; + struct spmi_device *pd; + /* AP DisplayPort AUX controller aux0 (shared), for the EDID */ + struct t8152_aux *aux; + /* PMGR power states of the four ATC dependencies */ + void __iomem *ps; + void __iomem *psd; + /* the dependencies this driver powered up, and may power down */ + u8 owned; + /* {offset, mask, value} tunables, before and after the lane setup */ + const __be32 *tunable[2]; + int tunable_len[2]; + /* This ATC's PMGR DP clock divider selects the link rate. */ + void __iomem *dp_clock; + bool swap; + bool active; + bool route_started; + u32 link_rate; + u32 lanes, next_lanes; +}; + +struct t8152_atc { + struct t8152_aux aux; + struct t8152_atc_port port[4]; + unsigned int nports; + /* the port the DCP drives, and the one a sink was found on */ + struct t8152_atc_port *cur, *next; +}; + +/* J873 has one display crossbar. Claims outlive firmware PHY power cycles. */ +static DEFINE_MUTEX(atc_owner_lock); +static struct device *atc_owner[4]; +/* All three DCPs borrow the same AP AUX controller for EDID reads. */ +static DEFINE_MUTEX(atc_aux_lock); + +static struct t8152_atc_port *atc_cur(struct t8152_display *disp) +{ + return ((struct t8152_atc *)disp->phy_priv)->cur; +} + +static const u32 atc_rates[] = { + LINK_RATE_RBR, LINK_RATE_HBR, LINK_RATE_HBR2, LINK_RATE_HBR3, +}; + +static bool atc_dp_pma(struct t8152_atc_port *atc, u32 pma) +{ + return atc->lanes == 4 || pma == !atc->swap; +} + +static bool atc_dp_register(struct t8152_atc_port *atc, u32 offset) +{ + if (offset >= 0x3000 && offset < 0xc000) + return atc_dp_pma(atc, 0); + if (offset >= 0xc000 && offset < 0x15000) + return atc_dp_pma(atc, 1); + return true; +} + +static void atc_update(struct t8152_atc_port *atc, u32 offset, u32 mask, u32 value) +{ + u32 reg = readl(atc->base + offset); + + writel((reg & ~mask) | value, atc->base + offset); +} + +static int atc_poll(struct t8152_atc_port *atc, u32 offset, u32 mask, u32 value) +{ + u32 reg; + + return readl_poll_timeout(atc->base + offset, reg, + (reg & mask) == value, 1, ATC_TIMEOUT_US); +} + +static int atc_route_stop(struct t8152_display *disp, struct t8152_atc_port *atc) +{ + u32 ufp = disp->dp_source * 2; + u32 mask = BIT(ufp); + + if (!atc->route_started) + return 0; + atc_update(atc, ATC_XBAR + 0x3c, mask, 0); + atc_update(atc, ATC_XBAR, mask, 0); + atc_update(atc, ATC_XBAR + 0xc, mask, 0); + atc_update(atc, ATC_XBAR + 0x24, 0x100, 0); + udelay(1); + /* + * With the pipe powered off (the bootloader hands the display over + * that way) nothing acknowledges the stop; the route is idle then, so + * finish it anyway. + */ + if (atc_poll(atc, ATC_XBAR + 0x800, mask, 0) || + atc_poll(atc, ATC_XBAR + 0x808, mask, 0) || + atc_poll(atc, ATC_XBAR + 0x81c, 0x100, 0)) + dev_info(disp->dev, "ATC%u: no crossbar stop acknowledgment\n", atc->index); + atc_update(atc, ATC_XBAR + 8, mask, 0); + atc_update(atc, ATC_XBAR + 0x20, 7 << (3 * ufp), 0); + atc_update(atc, ATC_XBAR + 0x38, 0x700, 0); + atc_update(atc, ATC_XBAR + 4, mask, mask); + atc_update(atc, ATC_XBAR + 0x1c, mask, mask); + atc_update(atc, ATC_XBAR + 0x34, 0x100, 0x100); + atc->route_started = false; + dev_dbg(disp->dev, "ATC%u: display route disabled\n", atc->index); + return 0; +} + +/* + * A source -> ATCn/DFP0 route, PCLK1: both DFP0 mux fields (0x44 bits 23:20 + * and 11:8) select the source. The upstream reset, clock and enable fields + * are indexed by DCP endpoint * 2 + DPTX port, independently of the + * downstream DFP0 fields. AUX borrowing keeps that DCP's clock path. + */ +#define ATC_SEL_AUX0 9 /* UFP endpoint 5 (aux0) + 4 */ + +static int atc_route_start_sel(struct t8152_display *disp, + struct t8152_atc_port *atc, u32 sel) +{ + u32 ufp = disp->dp_source * 2; + u32 mask = BIT(ufp); + + if (atc->route_started) + return 0; + atc_update(atc, ATC_XBAR + 0x44, 0x00f00000, sel << 20); + atc_update(atc, ATC_XBAR + 0x44, 0x00000f00, sel << 8); + atc_update(atc, ATC_XBAR + 4, mask, 0); + atc_update(atc, ATC_XBAR + 0x1c, mask, 0); + atc_update(atc, ATC_XBAR + 0x34, 0x100, 0); + udelay(1); + if (atc_poll(atc, ATC_XBAR + 0x804, mask, 0) || + atc_poll(atc, ATC_XBAR + 0x818, mask, 0) || + atc_poll(atc, ATC_XBAR + 0x82c, 0x100, 0)) { + dev_err(disp->dev, "ATC%u: crossbar reset acknowledgment timeout\n", + atc->index); + return -ETIMEDOUT; + } + atc_update(atc, ATC_XBAR + 8, mask, mask); + atc_update(atc, ATC_XBAR + 0x20, 7 << (3 * ufp), 1 << (3 * ufp)); + atc_update(atc, ATC_XBAR + 0x38, 0x700, 0x100); + atc_update(atc, ATC_XBAR + 0x48, 1, 0); + atc_update(atc, ATC_XBAR, mask, mask); + atc_update(atc, ATC_XBAR + 0xc, mask, mask); + atc_update(atc, ATC_XBAR + 0x24, 0x100, 0x100); + atc_update(atc, ATC_XBAR + 0x4c, 0x100, 0x100); + atc_update(atc, ATC_XBAR + 0x3c, mask, mask); + atc->route_started = true; + dev_dbg(disp->dev, "ATC%u: source %u to local DFP0 route configured\n", + atc->index, sel); + return 0; +} + +static int atc_route_start(struct t8152_display *disp, struct t8152_atc_port *atc) +{ + return atc_route_start_sel(disp, atc, disp->dp_source * 2); +} + +/* Common four-lane setup; the rate-dependent fields follow in atc_dp_start. */ +static const struct { + u32 offset, mask, value; +} atc_dp_setup[] = { + { 0x2044, 0xffff, 0x4b0 }, + { 0x4c, 0x3c0000, 0x3c0000 }, + { 0x4c80, 0x40, 0x40 }, + { 0xdc80, 0x40, 0x40 }, + { 0x4fd4, 0x2000000, 0x2000000 }, + { 0x4fd4, 0x1800000, 0x800000 }, + { 0xdfd4, 0x2000000, 0x2000000 }, + { 0xdfd4, 0x1800000, 0x800000 }, + { 0x4d84, 0x1c0000, 0 }, + { 0x4fd4, 1, 0 }, + { 0x4fd4, 8, 0 }, + { 0x4fd4, 2, 2 }, + { 0x4fd4, 0x10, 0x10 }, + { 0x8904, 0x1c0000, 0 }, + { 0x8800, 0x10000000, 0 }, + { 0x8800, 0x80000000, 0 }, + { 0x8800, 0x20000000, 0x20000000 }, + { 0x8804, 1, 1 }, + { 0xdd84, 0x1c0000, 0 }, + { 0xdfd4, 1, 0 }, + { 0xdfd4, 8, 0 }, + { 0xdfd4, 2, 2 }, + { 0xdfd4, 0x10, 0x10 }, + { 0x11904, 0x1c0000, 0 }, + { 0x11800, 0x10000000, 0 }, + { 0x11800, 0x80000000, 0 }, + { 0x11800, 0x20000000, 0x20000000 }, + { 0x11804, 1, 1 }, + { 0x3c04, 2, 2 }, + { 0x3c04, 1, 1 }, + { 0xcc04, 2, 2 }, + { 0xcc04, 1, 1 }, +}; + +/* Program the PMGR DP clock divider and wait for the change to settle. */ +static int atc_clock_write(struct t8152_atc_port *atc, u32 divider) +{ + u32 reg = readl(atc->dp_clock); + + writel((reg & ~0x0f000000) | (divider << 24), atc->dp_clock); + return readl_poll_timeout(atc->dp_clock, reg, !(reg & BIT(30)), 1, + ATC_TIMEOUT_US); +} + +/* Stop the DP lanes: route, pixel clock, then both PMAs. */ +static int atc_dp_stop(struct t8152_display *disp, struct t8152_atc_port *atc) +{ + u32 reg; + + atc_route_stop(disp, atc); + atc_update(atc, ATC_CLKSW_SELECT, ATC_CLKSW_PCLK1_SEL | ATC_CLKSW_PCLK1_MODE, + ATC_CLKSW_PCLK1_OFF); + atc_update(atc, ATC_CLKSW_ENABLE, ATC_CLKSW_PCLK1_EN, 0); + if (readl_poll_timeout(atc->base + ATC_CLKSW_STATUS, reg, + !(reg & ATC_CLKSW_PCLK1_EN), 1, ATC_TIMEOUT_US)) { + dev_err(disp->dev, "ATC%u: DPTX clock stop failed\n", atc->index); + return -ETIMEDOUT; + } + atc_update(atc, 0x2040, BIT(16), 0); + udelay(5); + atc_update(atc, 0x2000, BIT(15), 0); + atc_update(atc, 0x2000, BIT(13), 0); + atc_update(atc, 0x2000, BIT(14), 0); + atc_update(atc, 0x2000, BIT(2), 0); + atc_update(atc, 0x2000, BIT(3), BIT(3)); + atc_update(atc, 0xe10, BIT(2), BIT(2)); + atc_update(atc, 0xe08, BIT(29), BIT(29)); + /* Remove both PMA clock requests, then clamp and power down both lanes. */ + for (u32 offset = 0x1e18; offset <= 0x1e1c; offset += 4) { + u32 pma = (offset - 0x1e18) / 4; + + if (!atc_dp_pma(atc, pma)) + continue; + atc_update(atc, ATC_AON + 0x24, + BIT(12 + 2 * pma) | BIT(16 + 2 * pma), 0); + udelay(1); + atc_update(atc, offset, BIT(5), 0); + udelay(1); + atc_update(atc, offset, BIT(0), BIT(0)); + udelay(1); + atc_update(atc, offset, BIT(2), 0); + udelay(1); + atc_update(atc, offset, BIT(1), 0); + udelay(1); + } + atc->link_rate = 0; + return 0; +} + +/* DP lane setup at rate index r. */ +static int atc_dp_start(struct t8152_display *disp, struct t8152_atc_port *atc, u32 r) +{ + static const u32 tx_ctrl[] = { 0x4eb0, 0x8b10, 0xdeb0, 0x11b10 }; + static const u32 wake_count[] = { 0xbb5, 0xbb7, 0xbb8, 0xbb9 }; + u32 reg; + int ret; + + if (readl(atc->dp_clock) & BIT(30)) + return -EBUSY; + if (!r) { + atc_update(atc, 0xe10, BIT(2), BIT(2)); + atc_update(atc, 0xe08, BIT(29), BIT(29)); + } + for (u32 i = 0; i < 2; i++) { + u32 offset = 0x1e18 + 4 * i; + + if (!atc_dp_pma(atc, i)) + continue; + atc_update(atc, ATC_AON + 0x24, BIT(12 + 2 * i), BIT(12 + 2 * i)); + udelay(1); + atc_update(atc, ATC_AON + 0x24, BIT(16 + 2 * i), BIT(16 + 2 * i)); + udelay(1); + atc_update(atc, offset, BIT(1), BIT(1)); + udelay(1); + atc_update(atc, offset, BIT(2), BIT(2)); + udelay(1); + atc_update(atc, offset, BIT(0), 0); + udelay(1); + atc_update(atc, offset, BIT(5), BIT(5)); + udelay(2); + } + atc_update(atc, 0x2000, 0x380000, r << 19); + for (unsigned int i = 0; i < ARRAY_SIZE(atc_dp_setup); i++) + if (atc_dp_register(atc, atc_dp_setup[i].offset)) + atc_update(atc, atc_dp_setup[i].offset, atc_dp_setup[i].mask, + atc_dp_setup[i].value); + for (u32 i = 0; i < 2; i++) { + if (!atc_dp_pma(atc, i)) + continue; + atc_update(atc, 0x8684 + i * 0x9000, 3, r ? 0 : 1); + atc_update(atc, 0x4c80 + i * 0x9000, 0x30, r ? 0 : 0x10); + } + for (unsigned int i = 0; i < ARRAY_SIZE(tx_ctrl); i++) { + if (!atc_dp_register(atc, tx_ctrl[i])) + continue; + atc_update(atc, tx_ctrl[i], 0x7e07ff, 1); + atc_update(atc, tx_ctrl[i] - 0x14, 0x7f, r < 2 ? 0x65 : 0x40); + } + atc_update(atc, 0x2000, 0x70, 0x10); + atc_update(atc, 0x2000, BIT(13), BIT(13)); + atc_update(atc, 0x2044, 0xffff0000, wake_count[r] << 16); + atc_update(atc, atc->swap ? 0x89f0 : 0x119f0, BIT(14), BIT(14)); + atc_update(atc, 0x2000, BIT(2), BIT(2)); + atc_update(atc, 0x2000, BIT(3), BIT(3)); + atc_update(atc, 0x2040, BIT(16), BIT(16)); + + ret = atc_clock_write(atc, 10 - r); + if (ret) { + dev_err(disp->dev, "ATC%u: DP clock change timeout\n", atc->index); + return ret; + } + /* + * Clock the stream from PMGR DP0 at the link-rate divider just set. + * With source 0 selected instead, the pixel clock does not follow the + * divider, and every mode above about 165 MHz stays black. + */ + atc_update(atc, ATC_CLKSW_SELECT, ATC_CLKSW_PCLK1_SEL | ATC_CLKSW_PCLK1_MODE, + ATC_CLKSW_PCLK1_DP0); + atc_update(atc, ATC_CLKSW_ENABLE, ATC_CLKSW_PCLK1_EN, ATC_CLKSW_PCLK1_EN); + if (readl_poll_timeout(atc->base + ATC_CLKSW_STATUS, reg, + reg & ATC_CLKSW_PCLK1_EN, 1, ATC_TIMEOUT_US)) { + dev_err(disp->dev, "ATC%u: DP pixel clock does not start\n", atc->index); + return -ETIMEDOUT; + } + ret = atc_route_start(disp, atc); + if (ret) + return ret; + atc->link_rate = atc_rates[r]; + dev_info(disp->dev, "ATC%u: %u-lane DPTX setup at rate %#x\n", atc->index, + atc->lanes, atc->link_rate); + return 0; +} + +/* + * The power dependencies, in order: the ATC's USB AON (PS +0x100), its common + * PSD, then its two DP PS registers (+0, +0x20), with the values they idle at + * (ATC3: PS 0x1c0, PSD 58, PS 0xc0, 0xe0). + */ +#define ATC_PSD_TARGET 0x90 +#define ATC_PSD_STATE 0x80 +static const u32 atc_ps_offset[] = { 0x100, 0, 0x0, 0x20 }; +static const u32 atc_ps_idle[] = { 0x300, 6, 0x244, 0x244 }; + +static int atc_psd_wait(struct t8152_atc_port *atc, u32 state) +{ + u32 reg; + + /* + * Only the state: the acknowledge bit stays clear when Linux switches + * the PSD, in either direction. + */ + return readl_poll_timeout(atc->psd + ATC_PSD_STATE, reg, + (reg & 7) == state, 1, ATC_TIMEOUT_US); +} + +static int atc_gate(struct t8152_atc_port *atc, unsigned int i, bool on) +{ + u32 value = on ? (atc_ps_idle[i] & 0xfffffcf0) | 15 : atc_ps_idle[i]; + u32 reg; + + /* Already up (the bootloader's or an earlier activation's). */ + if (on && (i == 1 ? !(readl(atc->psd + ATC_PSD_STATE) & 7) : + (readl(atc->ps + atc_ps_offset[i]) & 0xf0) == 0xf0)) + return 0; + if (on) + atc->owned |= BIT(i); + else + atc->owned &= ~BIT(i); + if (i == 1) { + writel(on ? 0 : atc_ps_idle[i], atc->psd + ATC_PSD_TARGET); + return atc_psd_wait(atc, on ? 0 : atc_ps_idle[i]); + } + writel(value, atc->ps + atc_ps_offset[i]); + return readl_poll_timeout(atc->ps + atc_ps_offset[i], reg, + (reg & 0xf0) == ((value & 15) << 4), 1, + ATC_TIMEOUT_US); +} + +static void atc_apply_tunables(struct t8152_atc_port *atc, unsigned int which) +{ + const __be32 *t = atc->tunable[which]; + + for (int i = 0; i + 3 <= atc->tunable_len[which]; i += 3) + atc_update(atc, be32_to_cpu(t[i]), be32_to_cpu(t[i + 1]), + be32_to_cpu(t[i + 2])); +} + +/* Cold start: the power dependencies, the common block and AUX, then the lanes at HBR. */ +static int atc_activate(struct t8152_display *disp) +{ + struct t8152_atc_port *atc = atc_cur(disp); + void __iomem *cfg = atc->base + ATC_CFG; + unsigned int gates = 0; + u32 reg; + + if (READ_ONCE(atc_owner[atc->index]) != disp->dev) + return -EBUSY; + if (atc->active) + return 0; + if (!atc->tunable[0] || !atc->tunable[1]) + return -EOPNOTSUPP; + for (; gates < ARRAY_SIZE(atc_ps_idle); gates++) + if (atc_gate(atc, gates, true)) + goto fail; + if (readl(cfg) != 4 || (readl(cfg + 4) & 3) || readl(cfg + 0x30)) + goto fail; + writel(readl(cfg + 0x30) | BIT(0), cfg + 0x30); + udelay(1); + writel(readl(cfg) | BIT(0), cfg); + if (readl_poll_timeout(cfg + 4, reg, reg & BIT(0), 1, ATC_TIMEOUT_US)) + goto fail; + writel(readl(cfg) | BIT(1), cfg); + if (readl_poll_timeout(cfg + 4, reg, reg & BIT(1), 1, ATC_TIMEOUT_US)) + goto fail; + writel(readl(cfg) & ~BIT(2), cfg); + udelay(1); + writel(readl(cfg) | BIT(3), cfg); + + atc_apply_tunables(atc, 0); + /* Pin D/F keeps the other PMA out of the DP path. */ + atc_update(atc, 0x4c, 0x1f, + atc->lanes == 2 ? (atc->swap ? 0x11 : 0x10) : 0x14); + atc_update(atc, 0x4c, 0x3ffe0, + atc->lanes == 2 || atc->swap ? 0x100 : 0x22000); + atc_apply_tunables(atc, 1); + atc_update(atc, ATC_CFG + 8, BIT(1), atc->swap ? BIT(1) : 0); + writel(readl(cfg) | BIT(4), cfg); + if ((readl(cfg) & 0x1f) != 0x1b) + goto fail; + /* AUX: release small and big sleep, then the AUX clamp. */ + atc_update(atc, ATC_AON + 0x24, BIT(8), BIT(8)); + udelay(1); + atc_update(atc, ATC_AON + 0x24, BIT(6), BIT(6)); + udelay(1); + atc_update(atc, ATC_AON + 0x24, BIT(10), 0); + udelay(1); + atc->active = true; + dev_info(disp->dev, "ATC%u: common PHY and AUX up from cold\n", atc->index); + /* + * The lanes start at HBR right away. From cold, AUX only works with + * the DP PLL and PMAs running (without them the firmware reports + * DEVICE_NOT_RESPONDING); the firmware switches to its rate afterwards. + */ + return atc_dp_start(disp, atc, 1); +fail: + dev_err(disp->dev, "ATC%u: cold start failed (gates %u, cfg %#x/%#x)\n", + atc->index, gates, + gates == ARRAY_SIZE(atc_ps_idle) ? readl(cfg) : 0, + gates == ARRAY_SIZE(atc_ps_idle) ? readl(cfg + 4) : 0); + return -EIO; +} + +/* Power the PHY down, dependencies included. */ +static int atc_deactivate(struct t8152_display *disp) +{ + struct t8152_atc_port *atc = atc_cur(disp); + void __iomem *cfg = atc->base + ATC_CFG; + int ret = 0; + u32 reg; + + if (!atc->active) + return 0; + if (atc->link_rate && atc_dp_stop(disp, atc)) { + dev_err(disp->dev, "ATC%u: DP stop failed\n", atc->index); + return -EIO; + } + atc_route_stop(disp, atc); + atc_update(atc, ATC_AON + 0x24, BIT(10), BIT(10)); + udelay(1); + atc_update(atc, ATC_AON + 0x24, BIT(6), 0); + udelay(1); + atc_update(atc, ATC_AON + 0x24, BIT(8), 0); + udelay(1); + writel(readl(cfg) & ~BIT(4), cfg); + udelay(1); + writel(readl(cfg) & ~BIT(3), cfg); + writel(readl(cfg) | BIT(2), cfg); + writel(readl(cfg) & ~BIT(1), cfg); + if (readl_poll_timeout(cfg + 4, reg, !(reg & BIT(1)), 1, ATC_TIMEOUT_US)) { + dev_err(disp->dev, "ATC%u: big-sleep entry timeout\n", atc->index); + ret = -ETIMEDOUT; + } + writel(readl(cfg) & ~BIT(0), cfg); + if (readl_poll_timeout(cfg + 4, reg, !(reg & BIT(0)), 1, ATC_TIMEOUT_US)) { + dev_err(disp->dev, "ATC%u: small-sleep entry timeout\n", atc->index); + ret = -ETIMEDOUT; + } + writel(readl(cfg + 0x30) & ~BIT(0), cfg + 0x30); + atc->active = false; + /* + * Then the power dependencies it powered up: the next activation starts + * it cold. The USB controller of a port keeps the ones it uses. + */ + for (int i = ARRAY_SIZE(atc_ps_idle) - 1; i >= 0; i--) + if ((atc->owned & BIT(i)) && atc_gate(atc, i, false)) + ret = -ETIMEDOUT; + dev_info(disp->dev, "ATC%u: powered down: %d\n", atc->index, ret); + return ret; +} + +static int atc_set_link_rate(struct t8152_display *disp, u32 rate) +{ + struct t8152_atc_port *atc = atc_cur(disp); + u32 r; + int ret; + + /* The firmware drops the link (rate 0) before it retrains. */ + if (!rate || rate == atc->link_rate) + return 0; + for (r = 0; r < ARRAY_SIZE(atc_rates); r++) + if (atc_rates[r] == rate) + break; + if (r == ARRAY_SIZE(atc_rates) || !atc->active) + return -EINVAL; + dev_info(disp->dev, "ATC%u: link rate %#x, was %#x\n", atc->index, rate, + atc->link_rate); + if (atc->link_rate) { + ret = atc_dp_stop(disp, atc); + if (ret) + return ret; + } + return atc_dp_start(disp, atc, r); +} + +static int atc_set_route(struct t8152_display *disp, bool enable) +{ + struct t8152_atc_port *atc = atc_cur(disp); + + if (!enable) + return atc_route_stop(disp, atc); + if (!atc->active) + return -ENODEV; + if (atc_route_start(disp, atc)) + return -EIO; + mdelay(5); + return 0; +} + +/* + * Lane drive presets, indexed by voltage * 4 + pre-emphasis; they serve + * every link rate. + */ +static const u32 atc_drive_presets[16] = { + 0x025, 0x420, 0x717, 0x90f, 0x019, 0x397, 0x78f, 0x025, + 0x011, 0x38f, 0x025, 0x025, 0x000, 0x025, 0x025, 0x025, +}; + +static int atc_set_drive(struct t8152_display *disp, + const struct t8152_drive_settings *settings, u32 count) +{ + struct t8152_atc_port *atc = atc_cur(disp); + u32 presets[4]; + + if (!count) + return 0; + if (count > atc->lanes || !atc->active || !atc->link_rate) + return -EINVAL; + /* Validate the entire request before changing any lane. */ + for (u32 i = 0; i < count; i++) { + u32 v = le32_to_cpu(settings[i].voltage); + u32 e = le32_to_cpu(settings[i].pre_emphasis); + + if (settings[i].format || v > 3 || e > 3 || v + e > 3) + return -EINVAL; + presets[i] = atc_drive_presets[v * 4 + e]; + } + for (u32 i = 0; i < count; i++) { + u32 pma = (i / 2) ^ !atc->swap; + /* Even DP lanes use the PMA RX pair, odd lanes its TX pair. */ + u32 offset = ((i & 1) ? 0x4eb0 : 0x8b10) + pma * 0x9000; + u32 preset = presets[i]; + u32 control = 1 | (((preset >> 13) & 0x3f) << 17) | + (((preset >> 7) & 0x3f) << 5); + + atc_update(atc, offset, 0x7e07ff, control); + atc_update(atc, offset - 0x14, 0x7f, (preset & 0x3f) | 0x40); + } + return 0; +} + +static void atc_set_orientation(struct t8152_display *disp, bool flipped) +{ + struct t8152_atc_port *atc = atc_cur(disp); + + /* Applied by the next cold start; a running PHY keeps its own. */ + if (atc->active && atc->swap != flipped) + dev_warn(disp->dev, "ATC%u: orientation changed while active\n", + atc->index); + atc->swap = flipped; + atc->lanes = atc->next_lanes; +} + +static u32 atc_max_lane_count(struct t8152_display *disp) +{ + return atc_cur(disp)->lanes; +} + +/* + * The sink's EDID through the AP AUX controller aux0, which reaches the ATC + * through crossbar UFP aux0 like the DCP: the DCP's route comes down for + * the read, so this runs while the firmware does no AUX itself (display + * off, HPD not asserted yet). + */ +static int atc_read_edid(struct t8152_display *disp, u8 *buf, size_t size) +{ + struct t8152_atc_port *atc = atc_cur(disp); + bool was_started = atc->route_started; + int ret; + + if (!atc->active || !atc->aux->regs) + return -ENODEV; + mutex_lock(&atc_aux_lock); + /* The AUX output select of an ATC port: ATC instance * 3 + local DFP. */ + atc->aux->sel = atc->index * 3; + ret = t8152_aux_init(disp, atc->aux); + if (ret) + goto out; + atc_route_stop(disp, atc); + ret = atc_route_start_sel(disp, atc, ATC_SEL_AUX0); + if (!ret) + ret = t8152_aux_read_edid(disp, atc->aux, buf, size); + atc_route_stop(disp, atc); + if (was_started) + atc_route_start(disp, atc); +out: + mutex_unlock(&atc_aux_lock); + return ret; +} + +/* + * 1 with a sink on one of the ports: the current one first, then the + * others; select() switches to a port found that way. + */ +static int atc_detect(struct t8152_display *disp, bool *flipped) +{ + struct t8152_atc *atc = disp->phy_priv; + int ret = 0; + + mutex_lock(&atc_owner_lock); + atc->next = atc->cur; + /* Keep a monitor on its controller, including while userspace blanks it. */ + if (atc_owner[atc->cur->index] == disp->dev) { + ret = t8152_pd_sink(atc->cur->pd, flipped, &atc->cur->next_lanes); + if (ret > 0 && (atc->cur->swap != *flipped || + atc->cur->lanes != atc->cur->next_lanes)) + disp->hpd = 0; + if (ret) + goto out; + /* Unplug powers down in KMS; release on the next poll. */ + if (!atc->cur->active) + atc_owner[atc->cur->index] = NULL; + } + for (unsigned int i = 0; i < atc->nports; i++) { + struct t8152_atc_port *port = &atc->port[i]; + + if (atc_owner[port->index] || + t8152_pd_sink(port->pd, flipped, &port->next_lanes) <= 0) + continue; + atc_owner[port->index] = disp->dev; + atc->next = port; + disp->hpd = 0; + ret = 1; + break; + } +out: + mutex_unlock(&atc_owner_lock); + return ret; +} + +/* + * Drive the port a sink was found on: power the old one down and connect + * the DPTX port to the new one's display crossbar endpoint. + */ +static void atc_select(struct t8152_display *disp) +{ + struct t8152_atc *atc = disp->phy_priv; + struct t8152_atc_port *port = atc->next; + + if (!port || port == atc->cur) + return; + if (atc->cur->active && atc_deactivate(disp)) + return; + mutex_lock(&atc_owner_lock); + if (atc_owner[atc->cur->index] == disp->dev) + atc_owner[atc->cur->index] = NULL; + mutex_unlock(&atc_owner_lock); + dev_info(disp->dev, "USB-C display on ATC%u now, was ATC%u\n", + port->index, atc->cur->index); + atc->cur = port; + disp->pd = port->pd; + disp->dcp_output = port->dfp; +} + +/* + * Before the DCP drives another PHY (the HDMI port): give up the claim on a + * port that is powered down. False while the current port is still up. + */ +bool t8152_atc_release(struct t8152_display *disp) +{ + struct t8152_atc_port *atc = atc_cur(disp); + + if (atc->active) + return false; + guard(mutex)(&atc_owner_lock); + if (atc_owner[atc->index] == disp->dev) + atc_owner[atc->index] = NULL; + return true; +} + +static const struct t8152_phy_ops atc_ops = { + .activate = atc_activate, + .deactivate = atc_deactivate, + .set_link_rate = atc_set_link_rate, + .max_lane_count = atc_max_lane_count, + .set_drive = atc_set_drive, + .set_route = atc_set_route, + .set_orientation = atc_set_orientation, + .read_edid = atc_read_edid, + .detect = atc_detect, + .select = atc_select, + .max_link_rate = LINK_RATE_HBR2, +}; + +/* PMGR registers, inside blocks their power domain driver has claimed. */ +static void __iomem *atc_map(struct t8152_display *disp, struct device_node *np, + const char *name) +{ + int i = of_property_match_string(np, "reg-names", name); + struct resource res; + + if (i < 0 || of_address_to_resource(np, i, &res)) + return NULL; + return devm_ioremap(disp->dev, res.start, resource_size(&res)); +} + +static int atc_port_init(struct t8152_display *disp, struct t8152_atc *atc, + struct device_node *np, void __iomem *dp_clock) +{ + struct t8152_atc_port *port = &atc->port[atc->nports]; + u32 cfg, r; + + port->base = atc_map(disp, np, "atc"); + port->ps = atc_map(disp, np, "atc-ps"); + port->psd = atc_map(disp, np, "atc-psd"); + if (!port->base || !port->ps || !port->psd) + return -ENOMEM; + port->pd = t8152_pd_get(np); + if (IS_ERR_OR_NULL(port->pd)) + return port->pd ? PTR_ERR(port->pd) : -ENODEV; + /* The node without ports is ATC3's (J873g back-right). */ + port->dfp = 3; + of_property_read_u32(np, "apple,dfp-endpoint", &port->dfp); + port->index = port->dfp; + port->lanes = 4; + port->next_lanes = 4; + if (port->index >= 4) + return -EINVAL; + port->dp_clock = dp_clock + port->index * 8; + port->aux = &atc->aux; + port->tunable[0] = of_get_property(np, "apple,tunable-pre", + &port->tunable_len[0]); + port->tunable[1] = of_get_property(np, "apple,tunable-post", + &port->tunable_len[1]); + port->tunable_len[0] /= sizeof(__be32); + port->tunable_len[1] /= sizeof(__be32); + atc->nports++; + + /* + * The PHY block hangs the bus while it is power-gated: look at it only + * with its DP dependencies and the common PSD up. + */ + if ((readl(port->ps) & 0xf0) != 0xf0 || + (readl(port->psd + ATC_PSD_STATE) & 7)) { + dev_info(disp->dev, "ATC%u: powered down\n", port->index); + return 0; + } + /* A different DCP may already be driving this port. */ + if (atc_owner[port->index] || + ((readl(port->base + ATC_XBAR + 0x44) >> 20) & 0xf) != + disp->dp_source * 2) + return 0; + /* Common block out of reset and AUX up, as the bootloader leaves it. */ + cfg = readl(port->base + ATC_CFG); + port->active = (cfg & 0x1f) == 0x1b; + if ((readl(port->base + 0x4c) & 0x1f) == 0x10 || + (readl(port->base + 0x4c) & 0x1f) == 0x11) { + port->lanes = 2; + port->next_lanes = 2; + } + port->swap = !!(readl(port->base + ATC_CFG + 8) & BIT(1)); + port->route_started = + (readl(port->base + ATC_XBAR) & BIT(disp->dp_source * 2)) && + (readl(port->base + ATC_XBAR + 0x3c) & BIT(disp->dp_source * 2)); + r = (readl(port->base + 0x2000) >> 19) & 7; + if (port->active && (readl(port->base + 0x2040) & BIT(16)) && + r < ARRAY_SIZE(atc_rates)) + port->link_rate = atc_rates[r]; + /* The bootloader's display: its dependencies are the display's. */ + if (port->active) + port->owned = GENMASK(ARRAY_SIZE(atc_ps_idle) - 1, 0); + dev_info(disp->dev, + "ATC%u: inherited state: active %d, rate %#x, route %d, reversed %d (cfg %#x)\n", + port->index, port->active, port->link_rate, port->route_started, + port->swap, cfg); + return 0; +} + +int t8152_atc_init(struct t8152_display *disp) +{ + struct device_node *ports __free(device_node) = + of_parse_phandle(disp->dev->of_node, "apple,usb-c-ports", 0); + struct device_node *np = ports ?: disp->dev->of_node; + struct resource clock_res; + void __iomem *dp_clock; + struct t8152_atc *atc; + int ret; + + atc = devm_kzalloc(disp->dev, sizeof(*atc), GFP_KERNEL); + if (!atc) + return -ENOMEM; + ret = of_property_match_string(np, "reg-names", "dp-clock"); + if (ret < 0 || of_address_to_resource(np, ret, &clock_res)) + return -EINVAL; + /* Older J873 DTs describe ATC3's divider alone; each ATC has its own. */ + if (resource_size(&clock_res) == 4 && clock_res.start == 0x30002005cULL) + clock_res.start -= 3 * 8; + if (resource_size(&clock_res) < 28) + return -EINVAL; + dp_clock = devm_ioremap(disp->dev, clock_res.start, resource_size(&clock_res)); + if (!dp_clock) + return -ENOMEM; + /* Shared AUX0 is serialized by atc_aux_lock. */ + atc->aux.regs = atc_map(disp, np, "aux"); + atc->aux.ps = atc_map(disp, np, "aux-ps"); + if (!atc->aux.ps) + atc->aux.regs = NULL; + + guard(mutex)(&atc_owner_lock); + /* One child per USB-C port, or the node itself for ATC3 alone. */ + for_each_child_of_node_scoped(np, child) { + if (!of_property_present(child, "apple,dfp-endpoint")) + continue; + if (atc->nports == ARRAY_SIZE(atc->port)) + break; + ret = atc_port_init(disp, atc, child, dp_clock); + if (ret) + return ret; + } + if (!atc->nports) { + ret = atc_port_init(disp, atc, np, dp_clock); + if (ret) + return ret; + } + + /* Only adopt a bootloader route belonging to this DCP. */ + atc->cur = &atc->port[0]; + for (unsigned int i = 0; i < atc->nports; i++) { + if (atc->port[i].active) { + atc->cur = &atc->port[i]; + atc_owner[atc->cur->index] = disp->dev; + break; + } + } + atc->next = atc->cur; + disp->phy = &atc_ops; + disp->phy_priv = atc; + disp->pd = atc->cur->pd; + disp->dcp_output = atc->cur->dfp; + dev_info(disp->dev, "USB-C display on ATC%u (%u ports)\n", atc->cur->index, + atc->nports); + return 0; +} diff --git a/drivers/gpu/drm/apple/t8152_aux.c b/drivers/gpu/drm/apple/t8152_aux.c new file mode 100644 index 0000000000000..8e2dbd2d2bd0c --- /dev/null +++ b/drivers/gpu/drm/apple/t8152_aux.c @@ -0,0 +1,200 @@ +// SPDX-License-Identifier: GPL-2.0-only OR MIT +/* + * The T8152 AP's DisplayPort AUX controllers aux0 and aux1: Analogix-derived + * DPTX cores with their own request framing. They attach to the display + * crossbar as UFP endpoints 5 and 6; the PHY code routes one to its port + * for the time of a read. Used for the sink's EDID, which the DCP firmware + * does not hand out on its iBoot interface. + * + * Interim, until the DCP's DPAV service or a DRM DP AUX channel provides the + * EDID on T8152. + */ + +#include +#include +#include +#include +#include +#include +#include + +#include + +#include "t8152_display.h" + +#define AUX_TIMEOUT_US 10000 +#define AUX_SEL 0x000 /* output: 9 crossbar UFP aux0, 0x10 lpdptx PHY */ +#define AUX_FUNC_EN_2 0x01c +#define AUX_FUNC_EN_N BIT(2) +#define AUX_HW_RETRY 0x390 +#define AUX_INT_STA 0x3dc +#define AUX_REPLY BIT(1) +#define AUX_ERROR BIT(0) +#define AUX_CH_STA 0x780 +#define AUX_DEFER_CTL 0x788 +#define AUX_RX_COMM 0x78c +#define AUX_BUF_CTL 0x790 +#define AUX_BUF_CLR BIT(9) +#define AUX_BUF_COUNT GENMASK(8, 0) +#define AUX_LEN 0x794 +#define AUX_ADDR_7_0 0x798 +#define AUX_ADDR_15_8 0x79c +#define AUX_CMD_ADDR 0x7a0 /* request in [7:4], address [19:16] */ +#define AUX_CTL 0x7a4 +#define AUX_GO BIT(0) +#define AUX_ADDR_ONLY BIT(1) +#define AUX_DATA 0xc00 /* one byte per word */ +#define AUX_MAX 16 + +#define AUX_NATIVE_WRITE 0x80 +#define AUX_I2C_WRITE 0x00 +#define AUX_I2C_READ 0x10 +#define AUX_I2C_MOT 0x40 +#define DDC_ADDR 0x50 +#define DPCD_SET_POWER 0x600 +#define DP_SET_POWER_D0 1 + +static void aux_update(void __iomem *reg, u32 mask, u32 value) +{ + writel((readl(reg) & ~mask) | value, reg); +} + +int t8152_aux_init(struct t8152_display *disp, struct t8152_aux *aux) +{ + u32 reg; + + /* PMGR power state of the controller */ + aux_update(aux->ps, 0xf, 0xf); + if (readl_poll_timeout(aux->ps, reg, ((reg >> 4) & 0xf) == 0xf, 1, + AUX_TIMEOUT_US)) { + dev_err(disp->dev, "AUX: controller does not power up\n"); + return -ETIMEDOUT; + } + writel(AUX_REPLY | AUX_ERROR, aux->regs + AUX_INT_STA); + aux_update(aux->regs + AUX_FUNC_EN_2, AUX_FUNC_EN_N, AUX_FUNC_EN_N); + usleep_range(10, 20); + aux_update(aux->regs + AUX_FUNC_EN_2, AUX_FUNC_EN_N, 0); + /* + * Bits 21:8 of AUX_HW_RETRY are a 14-bit reply timeout, not Analogix's + * three-bit expected-delay field: the value 3 let native reads pass + * but timed out the EDID through a USB-C to HDMI adapter. 3200, with + * a retry interval of 3. + */ + writel((3200 << 8) | (3 << 3), aux->regs + AUX_HW_RETRY); + writel(BIT(7) | 1, aux->regs + AUX_DEFER_CTL); + aux_update(aux->regs + AUX_SEL, 0x3f, aux->sel); + return 0; +} + +/* One AUX request of up to 16 bytes; returns the bytes transferred. */ +static int aux_xfer(struct t8152_aux *aux, u32 cmd, u32 addr, u8 *buf, + u32 len, bool write) +{ + u32 sta, n; + int i; + + for (int tries = 0; tries < 7; tries++) { + writel(AUX_BUF_CLR, aux->regs + AUX_BUF_CTL); + writel(len ? len - 1 : 0, aux->regs + AUX_LEN); + writel(addr & 0xff, aux->regs + AUX_ADDR_7_0); + writel((addr >> 8) & 0xff, aux->regs + AUX_ADDR_15_8); + writel((cmd & 0xf0) | ((addr >> 16) & 0xf), aux->regs + AUX_CMD_ADDR); + aux_update(aux->regs + AUX_CTL, AUX_ADDR_ONLY, len ? 0 : AUX_ADDR_ONLY); + for (i = 0; write && i < len; i++) + writel(buf[i], aux->regs + AUX_DATA + 4 * i); + writel(AUX_REPLY | AUX_ERROR, aux->regs + AUX_INT_STA); + aux_update(aux->regs + AUX_CTL, AUX_GO, AUX_GO); + if (readl_poll_timeout(aux->regs + AUX_INT_STA, sta, + sta & (AUX_REPLY | AUX_ERROR), 10, 20000)) + return -ETIMEDOUT; + writel(AUX_REPLY | AUX_ERROR, aux->regs + AUX_INT_STA); + if ((sta & AUX_ERROR) || (readl(aux->regs + AUX_CH_STA) & 0xf)) + return -EIO; + /* [3:2] I2C and [1:0] AUX reply: 0 ACK, 1 NACK, 2 DEFER */ + sta = readl(aux->regs + AUX_RX_COMM); + if (sta & 0x5) + return -ENXIO; + if (sta & 0xa) { + usleep_range(500, 1000); + continue; + } + if (write) + return len; + n = min(readl(aux->regs + AUX_BUF_CTL) & AUX_BUF_COUNT, len); + for (i = 0; i < n; i++) + buf[i] = readl(aux->regs + AUX_DATA + 4 * i); + return n; + } + return -EBUSY; +} + +/* DDC over I2C-over-AUX: offset 0, then 16-byte reads up to @size. */ +static int aux_ddc_read(struct t8152_aux *aux, u8 *buf, size_t size) +{ + u8 offset = 0; + size_t done = 0; + int ret; + + ret = aux_xfer(aux, AUX_I2C_WRITE | AUX_I2C_MOT, DDC_ADDR, &offset, 1, true); + while (ret >= 0 && done < size) { + ret = aux_xfer(aux, AUX_I2C_READ | AUX_I2C_MOT, DDC_ADDR, + buf + done, min_t(size_t, size - done, AUX_MAX), + false); + if (ret > 0) + done += ret; + else if (!ret) + ret = -EIO; + } + /* End the I2C transaction with an address-only read without MOT. */ + aux_xfer(aux, AUX_I2C_READ, DDC_ADDR, NULL, 0, false); + return ret < 0 ? ret : done; +} + +/* + * The EDID over DDC, the base block and its extensions; the sink answers + * AUX only some time after the path is up, and a sink just plugged in a + * little later still. + */ +int t8152_aux_read_edid(struct t8152_display *disp, struct t8152_aux *aux, + u8 *buf, size_t size) +{ + u8 d0 = DP_SET_POWER_D0; + int ret = -EIO, ext; + + for (int i = 0; i < 30 && (ret == -EIO || ret == -ETIMEDOUT); i++) { + msleep(i ? 100 : 20); + /* + * A sink left in D3 (after an unplug) NACKs DDC: wake it + * first, as the DP spec asks before AUX use. + */ + ret = aux_xfer(aux, AUX_NATIVE_WRITE, DPCD_SET_POWER, &d0, 1, true); + if (ret < 0) + continue; + usleep_range(1000, 2000); + ret = aux_ddc_read(aux, buf, min_t(size_t, size, EDID_LENGTH)); + } + if (ret != EDID_LENGTH) + return ret < 0 ? ret : -EIO; + ext = min_t(size_t, buf[0x7e], size / EDID_LENGTH - 1); + return ext ? aux_ddc_read(aux, buf, (1 + ext) * EDID_LENGTH) : ret; +} + +/* + * The controller named by np's "aux" and "aux-ps" regs (np need not be the + * DCP's own node); the PMGR register is mapped without claiming it. + */ +void t8152_aux_probe(struct t8152_display *disp, struct t8152_aux *aux, + u32 sel, struct device_node *np) +{ + int ps = of_property_match_string(np, "reg-names", "aux-ps"); + int regs = of_property_match_string(np, "reg-names", "aux"); + struct resource res; + + aux->sel = sel; + if (ps < 0 || regs < 0 || of_address_to_resource(np, ps, &res)) + return; + aux->ps = devm_ioremap(disp->dev, res.start, resource_size(&res)); + aux->regs = devm_of_iomap(disp->dev, np, regs, NULL); + if (IS_ERR(aux->regs) || !aux->ps) + aux->regs = NULL; +} diff --git a/drivers/gpu/drm/apple/t8152_display.h b/drivers/gpu/drm/apple/t8152_display.h new file mode 100644 index 0000000000000..b26baf6b04924 --- /dev/null +++ b/drivers/gpu/drm/apple/t8152_display.h @@ -0,0 +1,140 @@ +/* SPDX-License-Identifier: GPL-2.0-only OR MIT */ +/* + * The T8152 (Mac mini J873g) DisplayPort hardware behind a DCP's DPTX port + * that no subsystem driver covers yet: the ATC PHYs' DisplayPort lanes and + * their display crossbar route (t8152_atc.c), the HDMI port's dedicated + * DisplayPort PHY and PS190 converter (t8152_hdmi.c), the AP's DisplayPort + * AUX controllers that read the sink's EDID (t8152_aux.c), the rear USB-C + * ports' ACE3 PD controllers (t8152_pd.c), and the DPTX port service the + * DCP firmware calls into for the PHY (t8152_dptx.c). + * + * Interim: the ATC DisplayPort mode moves to drivers/phy/apple/atc.c, the + * ACE3 controllers to tipd, and the choice of port with DP alt mode to the + * Type-C route layer of dcp-fabric.c, each in its own change. Until then + * these files program the registers themselves, for the iBoot interface + * DCPs of iomfb_iboot.c alone. + */ +#ifndef __APPLE_T8152_DISPLAY_H__ +#define __APPLE_T8152_DISPLAY_H__ + +#include + +struct apple_dcp; +struct apple_epic_service_ops; +struct device; +struct device_node; +struct gpio_desc; +struct spmi_device; +struct t8152_display; +struct t8152_hdmi; + +/* SET_DRIVE_SETTINGS and GET_DRIVE_SETTINGS records, one per lane */ +struct t8152_drive_settings { + __le32 format; + __le32 voltage; + __le32 pre_emphasis; +} __packed; + +/* The PHY behind the DCP's DPTX port; link rates in DPCD units. */ +struct t8152_phy_ops { + int (*activate)(struct t8152_display *disp); + int (*deactivate)(struct t8152_display *disp); + int (*set_link_rate)(struct t8152_display *disp, u32 rate); + /* optional */ + int (*set_lane_count)(struct t8152_display *disp, u32 lanes); + u32 (*max_lane_count)(struct t8152_display *disp); + int (*set_drive)(struct t8152_display *disp, + const struct t8152_drive_settings *settings, u32 count); + int (*set_route)(struct t8152_display *disp, bool enable); + /* optional: plug orientation for the next activation */ + void (*set_orientation)(struct t8152_display *disp, bool flipped); + /* optional: the sink's EDID, returns its length */ + int (*read_edid)(struct t8152_display *disp, u8 *buf, size_t size); + /* + * optional: 1 with a sink on one of the PHY's ports, 0 without; the + * port it found is taken by select() in the plug path + */ + int (*detect)(struct t8152_display *disp, bool *flipped); + void (*select)(struct t8152_display *disp); + u32 max_link_rate; +}; + +/* The DPTX port state the firmware's calls set and read back. */ +struct t8152_port { + u32 link_rate; + u32 lanes; + /* the drive settings the firmware set last, for GET_DRIVE_SETTINGS */ + struct t8152_drive_settings drive[4]; + u32 drive_count; +}; + +/* An AP DisplayPort AUX controller, and its output select */ +struct t8152_aux { + void __iomem *regs; + void __iomem *ps; + u32 sel; +}; + +/* The DisplayPort side of one DCP */ +struct t8152_display { + struct apple_dcp *dcp; + struct device *dev; + /* display crossbar UFP endpoint of the DCP; DPTX port 0 */ + u32 dp_source; + /* display crossbar DFP endpoint the DCP drives */ + u32 dcp_output; + u32 die; + + const struct t8152_phy_ops *phy; + void *phy_priv; + /* optional sink HPD line (HDMI), polled; the last level seen */ + struct gpio_desc *hpd_gpio; + int hpd; + /* USB-C: the PD controller of the port in use */ + struct spmi_device *pd; + /* HDMI PHY: DCPEXT0's, or the main DCP's on an HDMI-only boot */ + bool hdmi; + /* + * The HDMI port of the shared ports when the bootloader brought HDMI + * up on no DCP: taken while this DCP drives no USB-C display, with + * the USB-C PHY kept to switch back. + */ + struct t8152_hdmi *hdmi_port; + bool on_hdmi; + const struct t8152_phy_ops *usbc_phy; + void *usbc_phy_priv; + u32 usbc_output; + + struct t8152_port port[2]; +}; + +/* t8152_atc.c */ +int t8152_atc_init(struct t8152_display *disp); +bool t8152_atc_release(struct t8152_display *disp); + +/* t8152_hdmi.c */ +struct device_node *t8152_hdmi_node(void); +int t8152_hdmi_source(struct device_node *np); +int t8152_hdmi_init(struct t8152_display *disp, struct device_node *np); +int t8152_hdmi_port_init(struct t8152_display *disp, struct device_node *np); +void t8152_hdmi_port_poll(struct t8152_display *disp); + +/* t8152_aux.c */ +void t8152_aux_probe(struct t8152_display *disp, struct t8152_aux *aux, + u32 sel, struct device_node *np); +int t8152_aux_init(struct t8152_display *disp, struct t8152_aux *aux); +int t8152_aux_read_edid(struct t8152_display *disp, struct t8152_aux *aux, + u8 *buf, size_t size); + +/* t8152_pd.c */ +struct spmi_device *t8152_pd_get(struct device_node *np); +int t8152_pd_sink(struct spmi_device *pd, bool *flipped, u32 *lanes); +int t8152_pd_reconnect(struct t8152_display *disp); + +/* t8152_dptx.c */ +extern const struct apple_epic_service_ops t8152_dptx_port_ops; +int t8152_dptx_connect(struct t8152_display *disp, unsigned int port); +int t8152_dptx_disconnect(struct t8152_display *disp, unsigned int port); +int t8152_dptx_set_hpd(struct t8152_display *disp, unsigned int port, bool hpd); + +#endif diff --git a/drivers/gpu/drm/apple/t8152_dptx.c b/drivers/gpu/drm/apple/t8152_dptx.c new file mode 100644 index 0000000000000..14da33f3c7c3e --- /dev/null +++ b/drivers/gpu/drm/apple/t8152_dptx.c @@ -0,0 +1,315 @@ +// SPDX-License-Identifier: GPL-2.0-only OR MIT +/* + * The DPTX port service of a T8152 iBoot interface DCP: the calls the + * firmware makes into the AP for the PHY behind its DPTX port, and the + * port's release and HPD calls. + * + * dptxep.c serves the same calls for the PHYs of the PHY framework, the + * display crossbar mux and Thunderbolt tunnels. The T8152 PHYs are + * programmed by t8152_atc.c and t8152_hdmi.c instead: their link rate, + * lanes and drive settings are applied here, and the drive settings are + * read back as they were set. The port's connect and display request are + * dptxep.c's. Interim, until the T8152 PHYs are PHY framework providers + * and dptxep.c can host this service. + */ + +#include +#include +#include + +#include "afk.h" +#include "dcp-internal.h" +#include "dptx-service.h" +#include "dptxep.h" +#include "t8152_display.h" + +/* IOReturn values the firmware takes as call status */ +#define KIO_RETURN_ERROR 0xe00002bcU +#define KIO_RETURN_BAD_ARG 0xe00002c2U +#define KIO_RETURN_UNSUPPORTED 0xe00002c7U +#define KIO_RETURN_NOT_READY 0xe00002d8U + +static bool dptx_needs_port(u32 command) +{ + switch (command) { + case DPTX_APCALL_ACTIVATE: + case DPTX_APCALL_DEACTIVATE: + case DPTX_APCALL_WILL_CHANGE_LINKG_CONFIG: + case DPTX_APCALL_DID_CHANGE_LINK_CONFIG: + case DPTX_APCALL_SET_LINK_RATE: + case DPTX_APCALL_SET_DOWN_SPREAD: + case DPTX_APCALL_SET_ACTIVE_LANE_COUNT: + case DPTX_APCALL_SET_DRIVE_SETTINGS: + case DPTX_APCALL_GET_LINK_RATE: + case DPTX_APCALL_GET_ACTIVE_LANE_COUNT: + case DPTX_APCALL_GET_DRIVE_SETTINGS: + return true; + default: + return false; + } +} + +/* + * One call; the IOReturn status. Requests carry their argument, replies + * their value, at offset 16. @reply holds a copy of the request. + */ +static u32 dptx_apcall(struct t8152_display *disp, struct t8152_port *port, + u16 group, u32 command, const void *data, size_t data_size, + void *reply, size_t reply_size) +{ + const struct t8152_phy_ops *phy = disp->phy; + u64 value; + u32 arg; + + if (group && !(group == 1 && (command == DPTX_APCALL_SET_DRIVE_SETTINGS || + command == DPTX_APCALL_GET_DRIVE_SETTINGS))) + return KIO_RETURN_UNSUPPORTED; + + if ((!port || !phy) && dptx_needs_port(command)) + return KIO_RETURN_NOT_READY; + + switch (command) { + case DPTX_APCALL_ACTIVATE: + if (phy->activate(disp)) + return KIO_RETURN_ERROR; + port->link_rate = LINK_RATE_HBR; + return 0; + case DPTX_APCALL_DEACTIVATE: + if (phy->deactivate(disp)) + return KIO_RETURN_ERROR; + port->link_rate = 0; + return 0; + case DPTX_APCALL_WILL_CHANGE_LINKG_CONFIG: + case DPTX_APCALL_DID_CHANGE_LINK_CONFIG: + return phy->set_route(disp, command == + DPTX_APCALL_DID_CHANGE_LINK_CONFIG) ? + KIO_RETURN_ERROR : 0; + case DPTX_APCALL_GET_MAX_LINK_RATE: + value = phy ? phy->max_link_rate : LINK_RATE_HBR; + break; + case DPTX_APCALL_GET_LINK_RATE: + value = port->link_rate; + break; + case DPTX_APCALL_SET_LINK_RATE: + if (data_size < 17) + return KIO_RETURN_BAD_ARG; + arg = ((const u8 *)data)[16]; + if (arg > phy->max_link_rate) + return KIO_RETURN_UNSUPPORTED; + if (phy->set_link_rate(disp, arg)) + return KIO_RETURN_ERROR; + port->link_rate = arg; + return 0; + case DPTX_APCALL_GET_MAX_LANE_COUNT: + value = phy && phy->max_lane_count ? phy->max_lane_count(disp) : 4; + break; + case DPTX_APCALL_GET_ACTIVE_LANE_COUNT: + value = port->lanes; + break; + case DPTX_APCALL_SET_ACTIVE_LANE_COUNT: + if (data_size < 20) + return KIO_RETURN_BAD_ARG; + arg = get_unaligned_le32(data + 16); + if (arg == 3 || arg > 4) + return KIO_RETURN_BAD_ARG; + dev_info(disp->dev, "DPTX: %u active lanes\n", arg); + if (phy->set_lane_count && phy->set_lane_count(disp, arg)) + return KIO_RETURN_ERROR; + port->lanes = arg; + return 0; + case DPTX_APCALL_GET_MAX_DRIVE_SETTINGS: { + const struct t8152_drive_settings maximum = { + 0, cpu_to_le32(3), cpu_to_le32(3) + }; + + if (reply_size < 16 + sizeof(maximum)) + return KIO_RETURN_BAD_ARG; + memcpy(reply + 16, &maximum, sizeof(maximum)); + return 0; + } + case DPTX_APCALL_SET_DRIVE_SETTINGS: { + struct t8152_drive_settings settings[4]; + + if (group != 1) + return KIO_RETURN_UNSUPPORTED; + if (data_size < 32) + return KIO_RETURN_BAD_ARG; + arg = get_unaligned_le32(data + 16); + if (arg > 4 || data_size < 32 + arg * sizeof(settings[0])) + return KIO_RETURN_BAD_ARG; + memcpy(settings, data + 32, arg * sizeof(settings[0])); + if (phy->set_drive(disp, settings, arg)) + return KIO_RETURN_ERROR; + memcpy(port->drive, settings, arg * sizeof(settings[0])); + port->drive_count = arg; + return 0; + } + case DPTX_APCALL_GET_DRIVE_SETTINGS: + /* + * The firmware reads back what it set during link training; an + * unsupported reply here was followed by a firmware crash. + * Same layout as SET: count at 16, settings from 32. + */ + if (group != 1) + return KIO_RETURN_UNSUPPORTED; + if (reply_size < 32 + port->drive_count * sizeof(port->drive[0])) + return KIO_RETURN_BAD_ARG; + put_unaligned_le32(port->drive_count, reply + 16); + memcpy(reply + 32, port->drive, + port->drive_count * sizeof(port->drive[0])); + return 0; + case DPTX_APCALL_SET_DOWN_SPREAD: + if (data_size < 17) + return KIO_RETURN_BAD_ARG; + /* The link is set up with downspread disabled. */ + return ((const u8 *)data)[16] ? KIO_RETURN_UNSUPPORTED : 0; + case DPTX_APCALL_GET_SUPPORTS_DOWN_SPREAD: + case DPTX_APCALL_GET_DOWN_SPREAD: + case DPTX_APCALL_GET_SUPPORTS_LANE_MAPPING: + case DPTX_APCALL_GET_VIRTUAL_DEVICE_MODE: + value = 0; + break; + case DPTX_APCALL_GET_SUPPORTS_HPD: + value = 1; + break; + case DPTX_APCALL_INACTIVE_SINK_DETECTED: + case DPTX_APCALL_DEVICE_NOT_RESPONDING: + case DPTX_APCALL_DEVICE_BUSY_TIMEOUT: + case DPTX_APCALL_DEVICE_NOT_STARTED: + /* Link fault reports: acknowledged with a zero status. */ + dev_info(disp->dev, "DPTX: firmware reports link fault %u\n", + command); + return 0; + case DPTX_APCALL_SET_TILED_DISPLAY_HINTS: + /* Acknowledged, the hints unused. */ + return 0; + default: + dev_info(disp->dev, "DPTX: unsupported call %u\n", command); + return KIO_RETURN_UNSUPPORTED; + } + + if (reply_size < 16 + sizeof(value)) + return KIO_RETURN_BAD_ARG; + put_unaligned_le64(value, reply + 16); + return 0; +} + +/* + * The reply starts as a copy of the request and carries the call's status + * in its first word; the transport status is always success. + */ +static int t8152_dptx_call(struct apple_epic_service *service, u16 group, + u32 command, const void *data, size_t size, + void *reply, size_t room) +{ + struct apple_dcp *dcp = service->ep->dcp; + struct dptx_port *dptx = service->cookie; + struct t8152_display *disp = READ_ONCE(dcp->t8152); + u32 status; + + dev_dbg(dcp->dev, "DPTX: port %d group %u command %u size %zu\n", + dptx ? (int)dptx->unit : -1, group, command, size); + memcpy(reply, data, min(room, size)); + if (disp) + status = dptx_apcall(disp, dptx ? &disp->port[dptx->unit] : NULL, + group, command, data, size, reply, room); + else + status = KIO_RETURN_NOT_READY; + if (status) + dev_dbg(dcp->dev, "DPTX: group %u command %u -> %#x\n", group, + command, status); + put_unaligned_le32(status, reply); + return 0; +} + +/* + * The ports in the order the firmware announces them. A further service is + * opened as well, without a port. + */ +static int t8152_dptx_prepare(struct apple_epic_service *service, + const char *name, const char *class, s64 unit) +{ + struct apple_dcp *dcp = service->ep->dcp; + + for (unsigned int i = 0; i < ARRAY_SIZE(dcp->dptxport); i++) + if (!dcp->dptxport[i].service) + return dptx_service_bind(service, &dcp->dptxport[i], i); + return 0; +} + +static void t8152_dptx_init(struct apple_epic_service *service, + const char *name, const char *class, s64 unit) +{ + if (service->cookie) + dptx_service_publish(service); +} + +/* Bound to every service of the DPTX endpoint (afk unmatched_ops). */ +const struct apple_epic_service_ops t8152_dptx_port_ops = { + .name = "dcpdptx-port", + .prepare = t8152_dptx_prepare, + .init = t8152_dptx_init, + .call_v2 = t8152_dptx_call, +}; + +static struct apple_epic_service *t8152_dptx_service(struct t8152_display *disp, + unsigned int port) +{ + struct dptx_port *dptx = &disp->dcp->dptxport[port]; + + return dptx_service_available(dptx) ? dptx->service : NULL; +} + +/* Connect the port to the PHY the DCP drives and request the display. */ +int t8152_dptx_connect(struct t8152_display *disp, unsigned int port) +{ + struct apple_epic_service *service = t8152_dptx_service(disp, port); + int ret; + + if (!service) + return -ENODEV; + ret = dptxport_connect(service, 0, disp->dcp_output, disp->die, true); + if (!ret) + ret = dptxport_request_display(service); + return ret; +} + +/* + * HPD as the port reports it to the firmware (group 8, command 8): the + * reply's first word is the call's status. + */ +int t8152_dptx_set_hpd(struct t8152_display *disp, unsigned int port, bool hpd) +{ + struct apple_epic_service *service = t8152_dptx_service(disp, port); + struct { + u8 pad[16]; + __le32 hpd; + } __packed cmd = { .hpd = cpu_to_le32(hpd) }, resp = {}; + size_t received; + int ret; + + if (!service) + return -ENODEV; + ret = afk_service_call_len(service, 8, 8, &cmd, sizeof(cmd), 12, &resp, + sizeof(resp), 12, &received); + if (ret) + return ret; + return get_unaligned_le32(resp.pad) ? -EIO : 0; +} + +/* Release the display (group 8, command 7), then HPD low. */ +int t8152_dptx_disconnect(struct t8152_display *disp, unsigned int port) +{ + struct apple_epic_service *service = t8152_dptx_service(disp, port); + __le32 status = 0; + size_t received; + int ret; + + if (!service) + return -ENODEV; + ret = afk_service_call_len(service, 8, 7, NULL, 0, 16, &status, + sizeof(status), 12, &received); + if (ret || status) + return ret ?: -EIO; + return t8152_dptx_set_hpd(disp, port, false); +} diff --git a/drivers/gpu/drm/apple/t8152_hdmi.c b/drivers/gpu/drm/apple/t8152_hdmi.c new file mode 100644 index 0000000000000..5b11879b1724b --- /dev/null +++ b/drivers/gpu/drm/apple/t8152_hdmi.c @@ -0,0 +1,669 @@ +// SPDX-License-Identifier: GPL-2.0-only OR MIT +/* + * The J873g HDMI port: the T8152's dedicated DisplayPort PHY in display + * crossbar 0's lpdptx slot, converted by a PS190. + * + * Interim, until drivers/phy/apple/dptx.c has a T8152 type (its lanes are + * 0x2800 apart from 0x4500) and the PS190's power through the SMC is + * described as a supply: this programs the PHY and powers the converter + * itself. + * + * When the bootloader brought HDMI up, this keeps its setup and reprograms + * what a link training changes: the PLL for a new link rate and the active + * lanes. Drive settings are only recorded, and there is no per-link route. + * When it did not (no HDMI monitor at boot, or a USB-C display on the main + * DCP), the HDMI port joins the shared USB-C ports: a DCP without a USB-C + * display takes it on a plug and starts the PHY and the PS190 from cold. + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "dptxep.h" +#include "t8152_display.h" + +#define HDMI_LANES 4 +#define HDMI_LANE0 0x4500 +#define HDMI_LANE_STRIDE 0x2800 + +struct t8152_hdmi { + void __iomem *core; + void __iomem *apb; + /* AP DisplayPort AUX controller aux1, for the EDID */ + struct t8152_aux aux; + /* The cold start of the HDMI port taken from the shared ports: */ + void __iomem *ps; /* PMGR power state of the PHY */ + void __iomem *psd; /* PMGR PSD service of the PHY */ + struct gpio_desc *hpd; + struct device_node *smc_np; + const __be32 *pcon, *tunable; + int pcon_len, tunable_len; + bool active; +}; + +/* + * The DCP driving the HDMI port taken from the shared ports, if any, and its + * HPD line, requested once for all the DCPs that may take the port. + */ +static DEFINE_MUTEX(hdmi_port_lock); +static struct device *hdmi_port_owner; +static struct gpio_desc *hdmi_port_hpd; +static unsigned int hdmi_port_users; + +#define HDMI_PS_TARGET GENMASK(3, 0) +#define HDMI_PS_ACTUAL GENMASK(7, 4) +#define HDMI_PS_FLAGS GENMASK(9, 8) /* write-one-to-clear */ +#define HDMI_PSD_STATE 0x80 +#define HDMI_PSD_TARGET 0x90 +#define HDMI_POWER_TIMEOUT_US 100000 + +/* APB 0x10 bits 3:0: the crossbar UFP feeding the PHY */ +#define HDMI_SOURCE_SELECT 0x10 +#define HDMI_SOURCE_AUX1 6 /* UFP endpoint of the aux1 controller */ + +static void hdmi_set_bits(struct t8152_hdmi *hdmi, u32 offset, u32 shift, + u32 mask, u32 value) +{ + u32 reg = readl(hdmi->core + offset); + + writel((reg & ~mask) | (value << shift), hdmi->core + offset); +} + +static void hdmi_apb_update(struct t8152_hdmi *hdmi, u32 offset, u32 mask, + u32 value) +{ + u32 reg = readl(hdmi->apb + offset); + + writel((reg & ~mask) | value, hdmi->apb + offset); +} + +/* The PLL configuration words for each link rate (DPCD units). */ +static const struct { + u32 rate; + u32 w[22]; +} hdmi_pll_configs[] = { + { LINK_RATE_RBR, + { 1, 0x21c, 0, 0, 1, 0x13, 1, 0, 0, 0x1a, 1, 0x3c, 2, 1, 0, 0, 1, 0, + 0, 0, 1, 3 } }, + { LINK_RATE_HBR, + { 1, 0x1c2, 0x1fff, 0x3ffe, 1, 0x13, 1, 0, 0, 0x1a, 1, 0x32, 2, 1, 0, + 0, 1, 0, 0, 0, 1, 3 } }, + { LINK_RATE_HBR2, + { 0, 0x1c2, 0x1fff, 0x3ffe, 1, 9, 1, 0, 0, 0x1a, 1, 0x32, 1, 0, 0, 1, + 0, 0, 0, 1, 1, 3 } }, + { LINK_RATE_HBR3, + { 0, 0x2a3, 0x2ffd, 0x3ffc, 1, 9, 1, 0, 0, 0x1a, 1, 0x5a, 1, 0, 0, 1, + 0, 0, 0, 1, 1, 3 } }, +}; + +/* A PLL command: command in 0x408[2:0], request bit 3, acknowledge bit 19. */ +static int hdmi_pll_command(struct t8152_display *disp, struct t8152_hdmi *hdmi, + u32 command) +{ + int i; + + hdmi_set_bits(hdmi, 0x408, 6, 0x40, 1); + hdmi_set_bits(hdmi, 0x408, 0, 0x7, command); + hdmi_set_bits(hdmi, 0x408, 3, 0x8, 1); + for (i = 0; i < 10; i++) { + usleep_range(16, 32); + if (readl(hdmi->core + 0x408) & BIT(19)) + break; + } + hdmi_set_bits(hdmi, 0x408, 3, 0x8, 0); + hdmi_set_bits(hdmi, 0x408, 6, 0x40, 0); + if (i == 10) { + dev_err(disp->dev, "HDMI: no ACK to PLL command %u\n", command); + return -ETIMEDOUT; + } + return 0; +} + +/* The lane-disable bits of each lane; 0x2420 holds the mask of unused lanes. */ +static void hdmi_set_lanes(struct t8152_hdmi *hdmi, u32 lanes) +{ + static const u32 offsets[] = { 0x64, 0x24, 0x24, 0x48, 0x48, 0x5c, 0x5c, 0x48 }; + static const u32 shifts[] = { 4, 4, 1, 0, 1, 2, 3, 4 }; + static const u32 masks[] = { 0x10, 0x10, 0x2, 0x1, 0x2, 0x4, 0x8, 0x10 }; + u32 lane, i; + + hdmi_set_bits(hdmi, 0x2400, 0, 0x1, 0); + for (lane = 0; lane < HDMI_LANES; lane++) { + u32 base = HDMI_LANE0 + lane * HDMI_LANE_STRIDE; + + for (i = 0; i < ARRAY_SIZE(offsets); i++) + hdmi_set_bits(hdmi, base + offsets[i], shifts[i], + masks[i], lane >= lanes); + } + hdmi_set_bits(hdmi, 0x2420, 0, 0xf, 0xf & ~((1 << lanes) - 1)); + hdmi_set_bits(hdmi, 0x2400, 0, 0x1, 1); +} + +static int hdmi_activate(struct t8152_display *disp) +{ + return 0; +} + +static int hdmi_deactivate(struct t8152_display *disp) +{ + dev_info(disp->dev, "HDMI: keeping the link up on deactivate\n"); + return 0; +} + +/* + * A new link rate: lanes off, PLL down, the PLL frequency, half link rate, + * PLL up again. + */ +static int hdmi_set_link_rate(struct t8152_display *disp, u32 rate) +{ + struct t8152_hdmi *hdmi = disp->phy_priv; + const u32 *w = NULL; + bool half; + size_t i; + int ret; + + for (i = 0; i < ARRAY_SIZE(hdmi_pll_configs); i++) + if (hdmi_pll_configs[i].rate == rate) + w = hdmi_pll_configs[i].w; + if (!w) + return -EINVAL; + + dev_info(disp->dev, "HDMI: link rate %#x\n", rate); + hdmi_set_bits(hdmi, 0x2400, 0, 0x1, 0); + ret = hdmi_pll_command(disp, hdmi, 0); + if (ret) + return ret; + + /* The PLL frequency, downspread off */ + hdmi_set_bits(hdmi, 0x6c0, 0, 0x7ff, w[1]); + hdmi_set_bits(hdmi, 0x6c0, 11, 0x800, w[6]); + hdmi_set_bits(hdmi, 0x6c0, 13, 0x2000, w[10]); + hdmi_set_bits(hdmi, 0x6c0, 18, 0x3c0000, w[21]); + hdmi_set_bits(hdmi, 0x6c4, 14, 0xfffc000, w[2]); + hdmi_set_bits(hdmi, 0x6c4, 0, 0x3fff, + (!(w[7] & 1) && w[2] < 2) ? 0 : w[3]); + hdmi_set_bits(hdmi, 0x6c8, 14, 0xc000, w[0]); + hdmi_set_bits(hdmi, 0x6c8, 8, 0x100, 0); + hdmi_set_bits(hdmi, 0x6c8, 0, 0xff, w[8]); + hdmi_set_bits(hdmi, 0x6c8, 9, 0x3e00, w[5]); + hdmi_set_bits(hdmi, 0x6c8, 16, 0x10000, w[17]); + hdmi_set_bits(hdmi, 0x6c8, 26, 0xfc000000, w[9]); + hdmi_set_bits(hdmi, 0x6cc, 0, 0x7f, w[11]); + hdmi_set_bits(hdmi, 0x6cc, 9, 0x600, w[13]); + hdmi_set_bits(hdmi, 0x6cc, 11, 0x7800, w[14]); + hdmi_set_bits(hdmi, 0x6cc, 15, 0x38000, w[15]); + hdmi_set_bits(hdmi, 0x6cc, 20, 0x100000, w[16]); + hdmi_set_bits(hdmi, 0x6cc, 21, 0x200000, w[18]); + hdmi_set_bits(hdmi, 0x6cc, 22, 0xc00000, w[19]); + hdmi_set_bits(hdmi, 0x6cc, 24, 0x3000000, w[4]); + hdmi_set_bits(hdmi, 0x6cc, 26, 0xc000000, w[20]); + + /* Latch the link rate dependent shadow registers (APB 0xa0). */ + hdmi_apb_update(hdmi, 0xa0, BIT(3), BIT(3)); + hdmi_apb_update(hdmi, 0xa0, BIT(2), BIT(2)); + hdmi_apb_update(hdmi, 0xa0, BIT(18), BIT(18)); + hdmi_apb_update(hdmi, 0xa0, BIT(18), 0); + hdmi_apb_update(hdmi, 0xa0, BIT(19), BIT(19)); + hdmi_apb_update(hdmi, 0xa0, BIT(19), 0); + hdmi_apb_update(hdmi, 0xa0, BIT(2), 0); + hdmi_apb_update(hdmi, 0xa0, BIT(3), 0); + + /* No UHBR rates; half link rate from HBR up. */ + half = rate >= LINK_RATE_HBR; + hdmi_set_bits(hdmi, 0x2400, 2, 0x4, 0); + hdmi_set_bits(hdmi, 0x2400, 3, 0x8, !half); + hdmi_set_bits(hdmi, 0x0000, 17, 0x20000, !half); + + ret = hdmi_pll_command(disp, hdmi, 7); + if (ret) + return ret; + if (!(readl(hdmi->core + 0x438) & BIT(30))) { + dev_err(disp->dev, "HDMI: PLL failed to lock at rate %#x\n", rate); + return -EIO; + } + return 0; +} + +static int hdmi_set_lane_count(struct t8152_display *disp, u32 lanes) +{ + /* The PHY takes no zero lane count. */ + if (lanes) + hdmi_set_lanes(disp->phy_priv, lanes); + return 0; +} + +static int hdmi_set_drive(struct t8152_display *disp, + const struct t8152_drive_settings *settings, u32 count) +{ + return 0; +} + +static int hdmi_set_route(struct t8152_display *disp, bool enable) +{ + return 0; +} + +/* + * The sink's EDID through the AP AUX controller aux1: the PHY takes its + * source from aux1 for the read (its AUX reaches the PS190, which reads the + * monitor's DDC), then from the DCP again. The display is off meanwhile. + */ +static int hdmi_read_edid(struct t8152_display *disp, u8 *buf, size_t size) +{ + struct t8152_hdmi *hdmi = disp->phy_priv; + u32 src; + int ret; + + if (!hdmi->aux.regs) + return -ENODEV; + ret = t8152_aux_init(disp, &hdmi->aux); + if (ret) + return ret; + src = readl(hdmi->apb + HDMI_SOURCE_SELECT); + writel((src & ~0xf) | HDMI_SOURCE_AUX1, hdmi->apb + HDMI_SOURCE_SELECT); + ret = t8152_aux_read_edid(disp, &hdmi->aux, buf, size); + writel(src, hdmi->apb + HDMI_SOURCE_SELECT); + return ret; +} + +static const struct t8152_phy_ops hdmi_ops = { + .activate = hdmi_activate, + .deactivate = hdmi_deactivate, + .set_link_rate = hdmi_set_link_rate, + .set_lane_count = hdmi_set_lane_count, + .set_drive = hdmi_set_drive, + .set_route = hdmi_set_route, + .read_edid = hdmi_read_edid, + .max_link_rate = LINK_RATE_HBR3, +}; + +/* + * The PS190's power through the SMC: the {key, value} writes of + * apple,pcon-power. The PS190 drives the HDMI HPD line only while it is + * powered, and the AP AUX controller that reads the USB-C monitors' EDID + * gets no reply from any USB-C port while it is off. Nothing here waits + * for it to come up: a DCP takes the port only once it sees HPD + * (t8152_hdmi_port_poll()), and an EDID read retries until the AUX + * replies (t8152_aux_read_edid()). On a port taken over, these writes find + * the PS190 powered since probe. + */ +static int hdmi_pcon_power(struct t8152_display *disp, struct t8152_hdmi *hdmi) +{ + struct platform_device *smc_pdev = of_find_device_by_node(hdmi->smc_np); + struct apple_smc *smc = smc_pdev ? platform_get_drvdata(smc_pdev) : NULL; + int ret = 0; + + if (!smc) { + ret = -EPROBE_DEFER; + goto out; + } + for (int i = 0; i + 3 <= hdmi->pcon_len; i += 3) { + u64 value = (u64)be32_to_cpu(hdmi->pcon[i + 1]) << 32 | + be32_to_cpu(hdmi->pcon[i + 2]); + + ret = apple_smc_write(smc, be32_to_cpu(hdmi->pcon[i]), &value, + sizeof(value)); + if (ret) { + dev_err(disp->dev, "HDMI: PS190 power failed: %d\n", ret); + goto out; + } + } +out: + if (smc_pdev) + put_device(&smc_pdev->dev); + return ret; +} + +/* The PHY's PMGR and PSD power, then the PS190's. */ +static int hdmi_power_up(struct t8152_display *disp, struct t8152_hdmi *hdmi) +{ + int ret; + u32 val; + + if ((readl(hdmi->ps) & HDMI_PS_ACTUAL) != HDMI_PS_ACTUAL) { + val = readl(hdmi->ps) & ~(HDMI_PS_TARGET | HDMI_PS_FLAGS); + writel(val | HDMI_PS_TARGET, hdmi->ps); + ret = readl_poll_timeout(hdmi->ps, val, + (val & HDMI_PS_ACTUAL) == HDMI_PS_ACTUAL, + 10, HDMI_POWER_TIMEOUT_US); + if (ret) { + dev_err(disp->dev, "HDMI: PHY power state stays %#x\n", val); + return ret; + } + } + if (readl(hdmi->psd + HDMI_PSD_STATE) & 7) { + writel(0, hdmi->psd + HDMI_PSD_TARGET); + ret = readl_poll_timeout(hdmi->psd + HDMI_PSD_STATE, val, !(val & 7), + 10, HDMI_POWER_TIMEOUT_US); + if (ret) { + dev_err(disp->dev, "HDMI: PHY PSD state stays %#x\n", val); + return ret; + } + } + return hdmi_pcon_power(disp, hdmi); +} + +/* The impedance calibration: wait for 0x1c04 bit 0, take 0x1c24 bits 12:6. */ +static void hdmi_impedance_calibration(struct t8152_display *disp, + struct t8152_hdmi *hdmi) +{ + u32 val; + + if (readl_poll_timeout(hdmi->core + 0x1c04, val, val & 1, 10, 1000000)) + dev_warn(disp->dev, "HDMI: impedance calibration timed out\n"); + hdmi_set_bits(hdmi, 0x1c00, 4, 0x3f0, 0x3f); + val = readl(hdmi->core + 0x1c24); + hdmi_set_bits(hdmi, 0x1c18, 15, 0x3f8000, (val >> 6) & 0x7f); + hdmi_set_bits(hdmi, 0x1c18, 14, 0x4000, 1); +} + +/* + * The cold start, as the bootloader does it: power, source select, PLL, AUX + * and PHY power, the tunables, the PLL and PHY activation, four lanes and + * the impedance calibration. Once up, the link is kept on deactivate, as + * for the bootloader's HDMI. + */ +static int hdmi_port_activate(struct t8152_display *disp) +{ + struct t8152_hdmi *hdmi = disp->phy_priv; + int ret; + + if (hdmi->active) + return 0; + ret = hdmi_power_up(disp, hdmi); + if (ret) + return ret; + /* The source select: only the low nibble */ + hdmi_apb_update(hdmi, HDMI_SOURCE_SELECT, 0xf, disp->dp_source); + /* PLL, AUX and PHY power enables */ + hdmi_apb_update(hdmi, 0x48, 0x010, 0x010); + usleep_range(1000, 2000); + hdmi_apb_update(hdmi, 0x48, 0x020, 0x020); + hdmi_apb_update(hdmi, 0x48, 0x040, 0); + hdmi_apb_update(hdmi, 0x48, 0x100, 0x100); + usleep_range(1000, 2000); + hdmi_apb_update(hdmi, 0x48, 0x200, 0x200); + hdmi_apb_update(hdmi, 0x48, 0x400, 0); + hdmi_apb_update(hdmi, 0x48, 0x001, 0x001); + usleep_range(1000, 2000); + hdmi_apb_update(hdmi, 0x48, 0x002, 0x002); + hdmi_apb_update(hdmi, 0x48, 0x004, 0); + + for (int i = 0; i + 3 <= hdmi->tunable_len; i += 3) { + void __iomem *reg = hdmi->core + be32_to_cpu(hdmi->tunable[i]); + + writel((readl(reg) & ~be32_to_cpu(hdmi->tunable[i + 1])) | + be32_to_cpu(hdmi->tunable[i + 2]), reg); + } + + hdmi_set_bits(hdmi, 0x08d0, 4, 0x10, 1); /* single-ended PLL clock */ + hdmi_set_bits(hdmi, 0x0000, 1, 0x02, 1); /* PLL on */ + hdmi_set_bits(hdmi, 0x1400, 0, 0x01, 0); /* AUX common control */ + hdmi_set_bits(hdmi, 0x1c00, 4, 0x3f0, 0x3c); /* PHY on */ + hdmi_set_bits(hdmi, 0x0000, 0, 0x01, 1); + usleep_range(1000, 2000); + hdmi_set_lanes(hdmi, HDMI_LANES); + hdmi_impedance_calibration(disp, hdmi); + hdmi->active = true; + dev_info(disp->dev, "HDMI: PHY up from cold, source %u\n", disp->dp_source); + return 0; +} + +static const struct t8152_phy_ops hdmi_port_ops = { + .activate = hdmi_port_activate, + .deactivate = hdmi_deactivate, + .set_link_rate = hdmi_set_link_rate, + .set_lane_count = hdmi_set_lane_count, + .set_drive = hdmi_set_drive, + .set_route = hdmi_set_route, + .read_edid = hdmi_read_edid, + .max_link_rate = LINK_RATE_HBR3, +}; + +/* Registers shared with the other DCPs that may take the HDMI port. */ +static void __iomem *hdmi_ioremap(struct t8152_display *disp, + struct device_node *np, const char *name) +{ + int index = of_property_match_string(np, "reg-names", name); + struct resource res; + + if (index < 0 || of_address_to_resource(np, index, &res)) + return NULL; + return devm_ioremap(disp->dev, res.start, resource_size(&res)); +} + +static void hdmi_put_smc(void *data) +{ + of_node_put(data); +} + +static void hdmi_port_put(void *data) +{ + guard(mutex)(&hdmi_port_lock); + if (!--hdmi_port_users) { + gpiod_put(hdmi_port_hpd); + hdmi_port_hpd = NULL; + } +} + +static struct gpio_desc *hdmi_port_get(struct t8152_display *disp, + struct device_node *np) +{ + struct gpio_desc *hpd; + int ret; + + scoped_guard(mutex, &hdmi_port_lock) { + if (!hdmi_port_users) { + hpd = fwnode_gpiod_get_index(of_fwnode_handle(np), "hpd", 0, + GPIOD_ASIS, "hdmi-hpd"); + if (IS_ERR(hpd)) + return hpd; + hdmi_port_hpd = hpd; + } + hdmi_port_users++; + hpd = hdmi_port_hpd; + } + ret = devm_add_action_or_reset(disp->dev, hdmi_port_put, NULL); + return ret ? ERR_PTR(ret) : hpd; +} + +/* + * The HDMI port of the shared ports, for every DCP: the PHY's source select + * takes each one's crossbar endpoint. Without the cold start description in + * np, HDMI stays the bootloader's. + */ +int t8152_hdmi_port_init(struct t8152_display *disp, struct device_node *np) +{ + struct t8152_hdmi *hdmi; + int ret; + + if (!of_property_present(np, "apple,pcon-power")) + return 0; + hdmi = devm_kzalloc(disp->dev, sizeof(*hdmi), GFP_KERNEL); + if (!hdmi) + return -ENOMEM; + hdmi->core = hdmi_ioremap(disp, np, "phy-core"); + hdmi->apb = hdmi_ioremap(disp, np, "phy-apb"); + hdmi->ps = hdmi_ioremap(disp, np, "phy-ps"); + hdmi->psd = hdmi_ioremap(disp, np, "phy-psd"); + hdmi->aux.regs = hdmi_ioremap(disp, np, "aux"); + hdmi->aux.ps = hdmi_ioremap(disp, np, "aux-ps"); + /* AUX output select 0x10: the lpdptx PHY */ + hdmi->aux.sel = 0x10; + if (!hdmi->core || !hdmi->apb || !hdmi->ps || !hdmi->psd) + return -ENOMEM; + if (!hdmi->aux.ps) + hdmi->aux.regs = NULL; + hdmi->pcon = of_get_property(np, "apple,pcon-power", &hdmi->pcon_len); + hdmi->pcon_len /= sizeof(__be32); + hdmi->tunable = of_get_property(np, "apple,tunable-phy", + &hdmi->tunable_len); + hdmi->tunable_len /= sizeof(__be32); + hdmi->smc_np = of_parse_phandle(np, "apple,smc", 0); + if (!hdmi->smc_np) + return -EINVAL; + ret = devm_add_action_or_reset(disp->dev, hdmi_put_smc, hdmi->smc_np); + if (ret) + return ret; + /* Without the bootloader's HDMI nothing else powers the PS190. */ + ret = hdmi_pcon_power(disp, hdmi); + if (ret) + return ret; + /* Every DCP that may take the port watches the same line. */ + hdmi->hpd = hdmi_port_get(disp, np); + if (IS_ERR(hdmi->hpd)) + return PTR_ERR(hdmi->hpd); + disp->hdmi_port = hdmi; + dev_info(disp->dev, "HDMI: port shared, taken without a USB-C display\n"); + return 0; +} + +/* + * Take the HDMI port when a sink is plugged in and this DCP drives no USB-C + * display; give it back once the sink is gone and the plug path has + * released the link. Called from the connector's detect with the display + * lock held. + */ +void t8152_hdmi_port_poll(struct t8152_display *disp) +{ + struct t8152_hdmi *hdmi = disp->hdmi_port; + int hpd; + + if (!hdmi) + return; + hpd = gpiod_get_value_cansleep(hdmi->hpd); + if (hpd < 0) + return; + guard(mutex)(&hdmi_port_lock); + if (disp->on_hdmi) { + if (hpd || disp->hpd) + return; + disp->phy = disp->usbc_phy; + disp->phy_priv = disp->usbc_phy_priv; + disp->dcp_output = disp->usbc_output; + disp->hpd_gpio = NULL; + disp->on_hdmi = false; + hdmi_port_owner = NULL; + dev_info(disp->dev, "HDMI: port given back\n"); + return; + } + if (!hpd || disp->hpd || hdmi_port_owner || !t8152_atc_release(disp)) + return; + hdmi_port_owner = disp->dev; + disp->usbc_phy = disp->phy; + disp->usbc_phy_priv = disp->phy_priv; + disp->usbc_output = disp->dcp_output; + disp->phy = &hdmi_port_ops; + disp->phy_priv = hdmi; + /* display crossbar DFP endpoint lpdptx */ + disp->dcp_output = 4; + disp->hpd_gpio = hdmi->hpd; + disp->on_hdmi = true; + dev_info(disp->dev, "HDMI: sink plugged, taking the HDMI port\n"); +} + +/* The DCP node that describes the HDMI PHY (DCPEXT0's). */ +struct device_node *t8152_hdmi_node(void) +{ + struct device_node *np; + + for_each_compatible_node(np, NULL, "apple,t8152-dcp-iboot") + if (of_property_read_bool(np, "apple,hdmi")) + return np; + return NULL; +} + +static void __iomem *hdmi_map(struct device *dev, struct device_node *np, + const char *name) +{ + int index = of_property_match_string(np, "reg-names", name); + + if (index < 0) + return IOMEM_ERR_PTR(index); + return devm_of_iomap(dev, np, index, NULL); +} + +static bool hdmi_power_active(struct device_node *np, const char *name) +{ + int index = of_property_match_string(np, "reg-names", name); + void __iomem *regs; + u32 state; + + if (index < 0) + return false; + regs = of_iomap(np, index); + if (!regs) + return false; + state = readl(regs); + iounmap(regs); + /* Both the requested and actual PMGR states must be active. */ + return (state & GENMASK(7, 0)) == GENMASK(7, 0); +} + +/* + * The DCP the bootloader wired to the PHY (its source select: 0 for the main + * DCP on the HDMI-only route, 1 for DCPEXT0 next to a USB-C display), or + * -ENODEV if it left the PHY off or the PLL unlocked. + */ +int t8152_hdmi_source(struct device_node *np) +{ + int core = of_property_match_string(np, "reg-names", "phy-core"); + int apb = of_property_match_string(np, "reg-names", "phy-apb"); + void __iomem *core_regs, *apb_regs; + int ret = -ENODEV; + + /* The PHY aperture aborts while gated, including on a USB-C-only boot. */ + if (core < 0 || apb < 0 || !hdmi_power_active(np, "phy-ps")) + return -ENODEV; + core_regs = of_iomap(np, core); + apb_regs = of_iomap(np, apb); + if (core_regs && apb_regs && readl(core_regs + 0x438) & BIT(30)) + ret = readl(apb_regs + HDMI_SOURCE_SELECT) & 0xf; + if (apb_regs) + iounmap(apb_regs); + if (core_regs) + iounmap(core_regs); + if (ret == 1 && !hdmi_power_active(np, "coproc-ps")) + return -ENODEV; + if (ret != 0 && ret != 1) + return -ENODEV; + return ret; +} + +/* The PHY, its AUX controller and its HPD line come from np. */ +int t8152_hdmi_init(struct t8152_display *disp, struct device_node *np) +{ + struct t8152_hdmi *hdmi; + + hdmi = devm_kzalloc(disp->dev, sizeof(*hdmi), GFP_KERNEL); + if (!hdmi) + return -ENOMEM; + hdmi->core = hdmi_map(disp->dev, np, "phy-core"); + if (IS_ERR(hdmi->core)) + return PTR_ERR(hdmi->core); + hdmi->apb = hdmi_map(disp->dev, np, "phy-apb"); + if (IS_ERR(hdmi->apb)) + return PTR_ERR(hdmi->apb); + + /* AUX output select 0x10: the lpdptx PHY */ + t8152_aux_probe(disp, &hdmi->aux, 0x10, np); + + dev_info(disp->dev, "HDMI: inherited source select %u, PLL %slocked\n", + readl(hdmi->apb + HDMI_SOURCE_SELECT) & 0xf, + readl(hdmi->core + 0x438) & BIT(30) ? "" : "not "); + + disp->phy = &hdmi_ops; + disp->phy_priv = hdmi; + /* display crossbar DFP endpoint lpdptx */ + disp->dcp_output = 4; + return 0; +} diff --git a/drivers/gpu/drm/apple/t8152_pd.c b/drivers/gpu/drm/apple/t8152_pd.c new file mode 100644 index 0000000000000..8c514d17d69d2 --- /dev/null +++ b/drivers/gpu/drm/apple/t8152_pd.c @@ -0,0 +1,211 @@ +// SPDX-License-Identifier: GPL-2.0-only OR MIT +/* + * USB-C plug state from the rear ports' ACE3 PD controllers over SPMI, for + * the USB-C displays' hotplug: the DP alt mode connection, its pin + * assignment and the plug orientation. Logical registers are read through a + * window: select with a register-zero write, wait for the busy bit to + * clear, read the size at 0x1f and the data from 0x20. + * + * Interim, until tipd owns these controllers and reports DP alt mode to the + * Type-C route layer of dcp-fabric.c: tipd and this share the SPMI devices, + * serialized by the device lock. + */ + +#include +#include +#include +#include +#include +#include +#include +#include + +#include "t8152_display.h" + +#define ACE3_SELECT 0x00 +#define ACE3_SELECT_BUSY BIT(7) +#define ACE3_SIZE 0x1f +#define ACE3_DATA 0x20 +#define ACE3_BURST 16 + +#define ACE3_REG_CMD1 0x08 +#define ACE3_REG_DATA1 0x09 +#define ACE3_REG_STATUS 0x1a +#define ACE3_REG_POWER_STATE 0x20 +#define ACE3_REG_DATA_STATUS 0x5f + +#define STATUS_PLUG_PRESENT BIT(0) +#define STATUS_UPSIDE_DOWN BIT(4) +#define DATA_DP_CONNECTION BIT(8) +#define DATA_HPD_LEVEL BIT(15) /* CD321x, as tipd */ + +static int ace3_read(struct spmi_device *sdev, u8 reg, u8 *buf, size_t len) +{ + u8 sel, size; + int ret, err = 0; + + ret = spmi_register_zero_write(sdev, reg); + if (ret) + return ret; + ret = read_poll_timeout(spmi_ext_register_read, err, + err || !(sel & ACE3_SELECT_BUSY), 50, 20000, + false, sdev, ACE3_SELECT, &sel, 1); + if (ret || err) + return ret ?: err; + ret = spmi_ext_register_read(sdev, ACE3_SIZE, &size, 1); + if (ret) + return ret; + if (size < len) + return -EIO; + for (size_t done = 0; done < len; done += ACE3_BURST) { + ret = spmi_ext_register_read(sdev, ACE3_DATA + done, buf + done, + min_t(size_t, len - done, ACE3_BURST)); + if (ret) + return ret; + } + return 0; +} + +static int ace3_select(struct spmi_device *sdev, u8 reg) +{ + u8 sel; + int ret, err = 0; + + ret = spmi_register_zero_write(sdev, reg); + if (ret) + return ret; + ret = read_poll_timeout(spmi_ext_register_read, err, + err || !(sel & ACE3_SELECT_BUSY), 50, 20000, + false, sdev, ACE3_SELECT, &sel, 1); + return ret ?: err; +} + +static int ace3_write(struct spmi_device *sdev, u8 reg, const u8 *buf, + size_t len) +{ + int ret = ace3_select(sdev, reg); + + return ret ?: spmi_ext_register_write(sdev, ACE3_DATA, (u8 *)buf, len); +} + +/* A 4CC command with an optional data byte; 0 once the controller took it. */ +static int ace3_cmd(struct spmi_device *sdev, const char *cmd, int data) +{ + u8 val[4]; + int ret; + + ret = spmi_command_wakeup(sdev); + if (ret) + return ret; + if (data >= 0) { + u8 d = data; + + ret = ace3_write(sdev, ACE3_REG_DATA1, &d, 1); + if (ret) + return ret; + } + ret = ace3_write(sdev, ACE3_REG_CMD1, cmd, 4); + if (ret) + return ret; + for (int i = 0; i < 1000; i++) { + ret = ace3_read(sdev, ACE3_REG_CMD1, val, sizeof(val)); + if (ret) + return ret; + if (!memcmp(val, "!CMD", 4)) + return -EIO; + if (!memcmp(val, "\0\0\0\0", 4)) + return 0; + usleep_range(1000, 2000); + } + return -ETIMEDOUT; +} + +/* + * Leave and enter DP alt mode again, as unplugging and plugging the cable + * does: reset the controller (Gaid), then power the port up again once it + * takes commands (SSPS 0). A sink that stopped answering AUX after a + * monitor's deep sleep comes back this way. + */ +int t8152_pd_reconnect(struct t8152_display *disp) +{ + struct spmi_device *pd = disp->pd; + int ret; + + device_lock(&pd->dev); + /* Gaid resets the controller before it can report completion. */ + ret = spmi_command_wakeup(pd); + if (!ret) + ret = ace3_write(pd, ACE3_REG_CMD1, "Gaid", 4); + if (ret) + goto out; + /* + * The controller restarts: on the J873g ports its mode register read + * "BOOT" about 22 ms after Gaid, it acknowledged no SPMI command from + * about 215 ms on, and it read "APP " and took SSPS again after about + * 290 ms. Polling it through that gap only gets NACKs, which the SPMI + * controller logs as errors, and nothing signals the restart's end. + * So SSPS is first sent 500 ms after Gaid, and every 500 ms after that + * for a controller that takes longer. + */ + for (int i = 0; i < 60; i++) { + msleep(500); + ret = ace3_cmd(pd, "SSPS", 0); + if (!ret) + break; + } +out: + device_unlock(&pd->dev); + return ret; +} + +/* 1 with a DisplayPort sink in DP alt mode and HPD high, 0 without. */ +int t8152_pd_sink(struct spmi_device *pd, bool *flipped, u32 *lanes) +{ + u8 status[4], data[4], power; + int ret; + + /* Serialize the logical register window with the USB4 port monitor. */ + device_lock(&pd->dev); + /* Without a plug the controller sleeps and ignores selects. */ + ret = spmi_command_wakeup(pd); + if (ret) + goto out; + ret = ace3_read(pd, ACE3_REG_POWER_STATE, &power, sizeof(power)); + if (ret) + goto out; + /* Register access alone does not enable the alt-mode policy engine. */ + if (power) { + ret = ace3_cmd(pd, "SSPS", 0); + if (ret) + goto out; + } + ret = ace3_read(pd, ACE3_REG_STATUS, status, sizeof(status)); + if (!ret) + ret = ace3_read(pd, ACE3_REG_DATA_STATUS, data, sizeof(data)); + if (ret) + goto out; + *flipped = status[0] & STATUS_UPSIDE_DOWN; + /* Pin D/F shares the cable with USB3; C/E uses all four DP lanes. */ + *lanes = get_unaligned_le32(data) & BIT(5) ? 2 : 4; + /* The sink answers AUX once it raised HPD, not at alt mode entry. */ + ret = (status[0] & STATUS_PLUG_PRESENT) && + (get_unaligned_le32(data) & DATA_DP_CONNECTION) && + (get_unaligned_le32(data) & DATA_HPD_LEVEL); +out: + device_unlock(&pd->dev); + return ret; +} + +/* The PD controller @np's apple,usb-pd points at, NULL without one. */ +struct spmi_device *t8152_pd_get(struct device_node *np) +{ + struct device_node *pd_np = of_parse_phandle(np, "apple,usb-pd", 0); + struct spmi_device *pd; + + if (!pd_np) + return NULL; + /* Holds a reference for the lifetime of the (retained) device. */ + pd = spmi_find_device_by_of_node(pd_np); + of_node_put(pd_np); + return pd ?: ERR_PTR(-EPROBE_DEFER); +} From 8d197aae70a930ad882ff8657e8086076a793691 Mon Sep 17 00:00:00 2001 From: Thomas Glanzmann Date: Fri, 20 Nov 2026 07:21:03 +0100 Subject: [PATCH 21/29] drm/apple: wake AFK waiters when a service opens A caller that needs an endpoint's services, or the state one of their init callbacks sets up, can only poll for them. Give each endpoint a wait queue that is woken once a service has opened and its init callback has run, on each path that opens one: compact, AFKv2 and a deferred AFKv2 open. The host-test fixtures that build those paths get the wait queue too. Signed-off-by: Thomas Glanzmann --- drivers/gpu/drm/apple/afk.c | 3 +++ drivers/gpu/drm/apple/afk.h | 3 +++ tools/asahi/dcpext26-parity-tests.py | 3 ++- tools/asahi/dcpext26-startup-tests.py | 3 ++- tools/testing/selftests/drivers/gpu/apple-dcp/edid.py | 3 ++- 5 files changed, 12 insertions(+), 3 deletions(-) diff --git a/drivers/gpu/drm/apple/afk.c b/drivers/gpu/drm/apple/afk.c index ad684ae5e954b..75152de0a85dd 100644 --- a/drivers/gpu/drm/apple/afk.c +++ b/drivers/gpu/drm/apple/afk.c @@ -1024,6 +1024,7 @@ static void afk_recv_handle_compact_init(struct apple_dcp_afkep *ep, if (ops->init) ops->init(service, service_name, service_name, unit); + wake_up_all(&ep->service_wq); dev_info(ep->dcp->dev, "AFK[ep:%02x]: new compact service %s on interface %u%s\n", @@ -1713,6 +1714,7 @@ static void afk_retry_service_init(struct apple_dcp_afkep *ep) if (ops->init) ops->init(service, service->pending_name, service->pending_class, service->pending_unit); + wake_up_all(&ep->service_wq); if (service->enabled) afk_populate_service_debugfs(service); kfree(service->pending_name); @@ -1951,6 +1953,7 @@ static void afk_recv_handle_init(struct apple_dcp_afkep *ep, u32 channel, ops->init_props(service, epic_unit, props, props_size); if (ops->init) ops->init(service, epic_name, epic_class, epic_unit); + wake_up_all(&ep->service_wq); dev_info(ep->dcp->dev, "AFK[ep:%02x]: new service %s on channel %d\n", ep->endpoint, service_name, channel); diff --git a/drivers/gpu/drm/apple/afk.h b/drivers/gpu/drm/apple/afk.h index c01022b6b0f87..cf214e1eb1b68 100644 --- a/drivers/gpu/drm/apple/afk.h +++ b/drivers/gpu/drm/apple/afk.h @@ -587,6 +587,8 @@ struct apple_dcp_afkep { const struct apple_epic_service_ops *unmatched_ops; struct apple_epic_service services[AFK_MAX_CHANNEL]; u32 num_channels; + /* Woken once a service has opened and its init callback has run. */ + wait_queue_head_t service_wq; struct dentry *debugfs_entry; @@ -606,6 +608,7 @@ static inline void afk_ep_init_state(struct apple_dcp_afkep *ep) spin_lock_init(&ep->lock); INIT_LIST_HEAD(&ep->compact_cmds); init_waitqueue_head(&ep->compact_orphan_wq); + init_waitqueue_head(&ep->service_wq); mutex_init(&ep->compact_tx_lock); init_completion(&ep->started); init_completion(&ep->stopped); diff --git a/tools/asahi/dcpext26-parity-tests.py b/tools/asahi/dcpext26-parity-tests.py index 08e8fddb71e0f..bbdff169ae2e9 100644 --- a/tools/asahi/dcpext26-parity-tests.py +++ b/tools/asahi/dcpext26-parity-tests.py @@ -158,7 +158,8 @@ def run(name,code): struct apple_epic_service {bool pending_open,enabled;const struct apple_epic_service_ops *ops;const char *pending_name,*pending_class;int pending_unit;u32 channel;}; struct apple_dcp {bool ext26_services_open;struct apple_dcp_afkep *dptxep;}; static bool dptx_native_controllers_ready(struct apple_dcp *d){return true;} -struct apple_dcp_afkep {struct apple_dcp *dcp;unsigned num_channels;struct apple_epic_service services[2];}; +struct apple_dcp_afkep {struct apple_dcp *dcp;unsigned num_channels;struct apple_epic_service services[2];int service_wq;}; +#define wake_up_all(q) ((void)(q)) static bool dcp_is_ext26(struct apple_dcp *d){return true;} static unsigned sent,published,discarded,initialized;static int fault; static void afk_discard_pending_open(struct apple_epic_service *s){discarded++;s->pending_open=false;} diff --git a/tools/asahi/dcpext26-startup-tests.py b/tools/asahi/dcpext26-startup-tests.py index bc8a0aa89a992..98b984e597554 100644 --- a/tools/asahi/dcpext26-startup-tests.py +++ b/tools/asahi/dcpext26-startup-tests.py @@ -95,7 +95,8 @@ def run(name,code,reject=False): struct apple_epic_service {struct apple_dcp_afkep *ep;const struct apple_epic_service_ops *ops;bool enabled,torndown,pending_open,defer_start;unsigned users;int lock;u32 channel;void *cookie;const char *pending_name,*pending_class;s64 pending_unit;int cmd_map[1];}; struct dptx_port {struct apple_epic_service *service;s64 unit;bool enabled,connected;int enable_completion;}; struct apple_dcp {const void *push_out_prefill;bool native,ext26_services_open,v27_j714s,v27_sleep_transport,dcpext;int fw_compat,index,dptx_controller_ready;struct {unsigned num_dptx_ports;int iomfb_method_profile,firmware_compat;bool afk_announced_names;} hw;struct dptx_port dptxport[2];void *dev;struct apple_dcp_afkep *systemep,*ibootep,*dptx_controller_ep,*dcpavservep,*dptxep,*ext26_aux[3];}; -struct apple_dcp_afkep {struct apple_dcp *dcp;u32 endpoint,num_channels;bool wire_v2,stopping,match_epic_name,compact;int lock;void *wq,*service_work;const struct apple_epic_service_ops *ops,*unmatched_ops;struct apple_epic_service services[AFK_MAX_CHANNEL];}; +struct apple_dcp_afkep {struct apple_dcp *dcp;u32 endpoint,num_channels;bool wire_v2,stopping,match_epic_name,compact;int lock;void *wq,*service_work;const struct apple_epic_service_ops *ops,*unmatched_ops;struct apple_epic_service services[AFK_MAX_CHANNEL];int service_wq;}; +#define wake_up_all(q) ((void)(q)) static bool dcp_is_ext26(struct apple_dcp *d){return d->native;} static bool dcp_fail_stop(struct apple_dcp *d){return d->native;} static bool dcp_is_v14_7_external(struct apple_dcp *d){return false;} diff --git a/tools/testing/selftests/drivers/gpu/apple-dcp/edid.py b/tools/testing/selftests/drivers/gpu/apple-dcp/edid.py index b4ea075b85f27..6898b7b368801 100644 --- a/tools/testing/selftests/drivers/gpu/apple-dcp/edid.py +++ b/tools/testing/selftests/drivers/gpu/apple-dcp/edid.py @@ -95,7 +95,8 @@ def function(file,signature): bool pending_open;const char *pending_name,*pending_class;s64 pending_unit; }; /* v27_closed_teardown: set only by a J714s firmware 27 sleep. */ -struct apple_dcp_afkep { u8 *command_tags;bool lock,stopping;void *wq;struct work_struct service_work;struct apple_dcp *dcp;const struct apple_epic_service_ops *ops,*unmatched_ops;bool wire_v2,inline_v2,match_epic_name,v27_closed_teardown;u32 endpoint;unsigned int num_channels;struct apple_epic_service services[AFK_MAX_CHANNEL]; }; +struct apple_dcp_afkep { u8 *command_tags;bool lock,stopping;void *wq;struct work_struct service_work;struct apple_dcp *dcp;const struct apple_epic_service_ops *ops,*unmatched_ops;bool wire_v2,inline_v2,match_epic_name,v27_closed_teardown;u32 endpoint;unsigned int num_channels;int service_wq;struct apple_epic_service services[AFK_MAX_CHANNEL]; }; +#define wake_up_all(q) ((void)(q)) struct apple_connector { void *dcp;bool connected,edid_invalidation_pending;struct { int registration_state; } base;unsigned int hotplug_wq; }; struct apple_dcp { void *dev;int fw_compat;struct apple_dcp_afkep *dcpavservep;struct apple_connector *connector; From 54f310756b0828ef422df6cc1ed1794f294b4a47 Mon Sep 17 00:00:00 2001 From: Thomas Glanzmann Date: Thu, 19 Nov 2026 22:59:43 +0100 Subject: [PATCH 22/29] drm/apple: drive the T8152 DCPs through the iBoot interface The T8152 (Mac mini J873g) DCP firmware offers a small display service, disp0-service on the main DCP and dispext-service on the DCPEXTs, next to its IOMFB interface. The bootloader drives the J873g displays through it, with the DPTX port service that the firmware calls into the AP for the PHY and the DPAV controller services it expects to be opened, all over AFKv2. Add these DCPs to drm/apple as the DCP_FIRMWARE_IBOOT variant, matched by apple,t8152-dcp-iboot, with the shared AFK code and the iBoot commands of ibootep.c. The match data selects AFKv2 on every endpoint, endpoints registered with an RTKit session taken without its map, services matched by their announced name and a three-second AFK timeout. dcp_platform_probe() hands such a DCP to iomfb_iboot.c. Taking the session over: - A running session is adopted without the RTKit handshake (apple_rtkit_init_adopted(), apple_rtkit_adopt_running()): the DCP does not acknowledge AP QUIESCED, and an IOP INIT while its AP power is on panics it. A session the loader quiesced after a failed display setup (/chosen apple,dcp-rtkit-running = 0; the older J873 loader build v0.1.0-259-ga30a589a predates the property and is recognised by its 640x1136 placeholder framebuffer) is woken instead, and so is an unused DCPEXT the loader marks apple,rtkit-wake, once its PSD reads powered (a read of the coprocessor while it is gated hangs the machine) and its CPU runs. - The loader's AFK endpoint hand-off (apple,afk-active-endpoints) is required and fills dcp->afk_live_endpoints, so afk_start() stops each endpoint the loader left running: an AFK INIT on a live endpoint asserts in the firmware. Without the property there is no telling whether an endpoint is live, and the DCP is not taken over. - A woken DCPEXT's oslog endpoint offers a 0x5ff8-byte ring at 0x100033cc000, the log segment of its own firmware addressed with the coprocessor's DRAM tag in bit 40, which only the firmware's retained DART tables map. With that buffer request failed, RTKit rejected the firmware's write counters as invalid instead of acknowledging them, which the firmware waits for before AP power transitions. The ring is taken as the firmware's, without a CPU mapping; the other buffers go through drm/apple's RTKit shared memory hooks. The surface log lines go to the debug level. - Once the session is taken it is retained until reboot like the other live sessions (dcp_retain_live_session(), split out of dcp_pin_live_session()). A failed start stops the endpoint work, detaches RTKit with its buffers kept and leaves the device bound, so nothing retries over a partly started session. Starting the display: - A new iBoot session has no surface, and a pipe that still runs then fetches from a null base (IOMFB axi_rd_err, firmware NMI): the display is powered off at once and powered on only after a SET_SURFACE. - The DPAV controller endpoint's four services are opened without a handler. Opening dcpav-video-interface starts the firmware's idle power off timer: a powered display without a following SET_MODE counts as idle, and the firmware takes the link down 30 s later, as it did with HDMI 30 s after boot while the compositor still had the output on. SET_MODE is sent again a second after that interface opens, from a work outside the AFK receive queues. - The DPTX port services are bound in the order they are announced. On USB-C the inherited DPTX link is released before the first connection. - The DCP fetches through dart-dcp and the display pipe scans out through dart-disp0; both are locked, with live roots of their own. One domain over both would publish dart-dcp's root into dart-disp0 and drop the bootloader's scanout mappings, so the scanout stream is a child device with a domain of its own. Scanout buffers are mapped there at their DCP address, cacheable, since the pipe faults on entries with the DART's no-cache bit, and below any range the firmware already maps there (a buffer mapped over one flickered). They are written through a write-combined mapping: the pipe does not snoop the CPU caches. - The bootloader's simple-framebuffer is taken over. KMS (iboot_drm.c), one DRM device per DCP: - Changing the surface between Linux buffers made later SET_MODEs crash the firmware. Three scanout buffers of at least 3840x2160 are allocated at probe; a frame's damage is copied by the CPU into one that is not on screen, which SET_SURFACE then shows, at most once per refresh by an hrtimer: there is no vblank interrupt, and several swaps within one refresh let an older frame stay on screen. The pitch is 64-byte aligned, as the firmware refuses an unaligned one. - The firmware takes a mode as width, height and a 16.16 refresh rate and picks the timing itself. The modes are the sink's EDID modes the firmware also lists, or the firmware's list with CVT timings. A DRM mode is matched to the firmware mode nearest by the exact refresh rate, within 1 Hz: next to an EIZO EV2780's 2560x1440@59.951 (241.5 MHz) the firmware lists a 60.000 Hz standard mode of its own (byte 0 bit 6), which it drives at 312.25 MHz and the monitor does not take. - The EDID is read through the AP's DisplayPort AUX controllers while the firmware does no AUX itself, since the iBoot interface does not hand it out. - A mode must fit the lanes the route may train, as GET_MAX_LANE_COUNT reports them, not those of the last training: a monitor that drops and reasserts HPD while the display is off is trained without a stream on a single lane, and 2560x1440@60 then failed the atomic check when the outputs came back on. - Right after power on the firmware still trains the link and fails SET_MODE; it takes the mode once the link is up, so SET_MODE is retried. The colour mode is the deepest valid one up to 32 bpp, as SET_MODE refuses the deeper ones the HDMI converter offers. - USB-C hotplug is polled from the PD controllers every 250 ms, HDMI from the converter's HPD line. An unplug releases DPTX and powers the PHY down, a sink that no longer answers after a monitor's deep sleep gets a fresh DP alt mode entry at most once a minute, and a quick reconnect restores a CRTC that was enabled. - drm/apple's per-DCP DRM device of dcpext_drm.c and dcpext_scanout.c does not fit: it is the T6030 external role's diagnostic, with one fixed 3840x2160 mode and the swap operations instead of SET_SURFACE. The DRM driver is named "apple-dcpib" (DCP iBoot interface), apart from the display subsystem's "apple"; appledrm.iboot_buffered=0 copies frames into the buffer on screen instead. Removal, shutdown and system sleep leave the firmware session alone. An unbind unplugs the DRM device, releases its committed state without touching the firmware, and detaches RTKit; the session stays retained, and a new bind is refused until reboot. The live endpoint hand-off and the retained failed start are based on changes by Acelogic. Tested on the J873g with HDMI and two rear USB-C monitors: all three come up, HDMI-A-1 at 2560x1440@59.951, DP-1 at 2560x1440@59.951 and DP-2 at 1920x1200@59.950. DPMS off and on works on each output, and so does HDMI at 1920x1080 and back. mpv runs fullscreen at 60 fps with no dropped frame after the first five. Unbinding DCPEXT1 removes its DRM device without a warning, and a new bind is refused with -EBUSY. Earlier runs of the same display code on the J873g: booted without HDMI, a monitor plugged in later is taken by a free DCP (DCPEXT0, and in another run DCPEXT1 with source select 2), which starts the PHY from cold and runs 2560x1440@60 at HBR3, DCPEXT0 within 0.6 s; with the bootloader's DCP start failing on purpose, HDMI and both USB-C monitors come up at their EDID modes; woken DCPEXTs log no buffer error. Signed-off-by: Thomas Glanzmann --- MAINTAINERS | 2 + drivers/gpu/drm/apple/Kconfig | 1 + drivers/gpu/drm/apple/Makefile | 1 + drivers/gpu/drm/apple/dcp-internal.h | 10 + drivers/gpu/drm/apple/dcp.c | 103 ++- drivers/gpu/drm/apple/iboot_drm.c | 1081 ++++++++++++++++++++++++++ drivers/gpu/drm/apple/iomfb_iboot.c | 840 ++++++++++++++++++++ drivers/gpu/drm/apple/iomfb_iboot.h | 139 ++++ 8 files changed, 2169 insertions(+), 8 deletions(-) create mode 100644 drivers/gpu/drm/apple/iboot_drm.c create mode 100644 drivers/gpu/drm/apple/iomfb_iboot.c create mode 100644 drivers/gpu/drm/apple/iomfb_iboot.h diff --git a/MAINTAINERS b/MAINTAINERS index 1afb033e7ffcd..8bd740eea9930 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -2156,6 +2156,8 @@ M: Thomas Glanzmann L: dri-devel@lists.freedesktop.org S: Maintained F: Documentation/devicetree/bindings/display/apple,t8152-dcp-iboot.yaml +F: drivers/gpu/drm/apple/iboot_drm.c +F: drivers/gpu/drm/apple/iomfb_iboot.* F: drivers/gpu/drm/apple/t8152_* APPLETALK NETWORK LAYER diff --git a/drivers/gpu/drm/apple/Kconfig b/drivers/gpu/drm/apple/Kconfig index d27d975da6f41..876ebbd58605e 100644 --- a/drivers/gpu/drm/apple/Kconfig +++ b/drivers/gpu/drm/apple/Kconfig @@ -80,6 +80,7 @@ config DRM_APPLE_DCP_T8152 depends on DRM_APPLE depends on SPMI=y || SPMI=DRM_APPLE depends on MFD_MACSMC=y || MFD_MACSMC=DRM_APPLE + select APERTURE_HELPERS help Lets the common Apple display driver drive the three display coprocessors of the T8152 Mac mini (J873g) through their firmware's diff --git a/drivers/gpu/drm/apple/Makefile b/drivers/gpu/drm/apple/Makefile index 3a0953cf6628a..e6752d0f28516 100644 --- a/drivers/gpu/drm/apple/Makefile +++ b/drivers/gpu/drm/apple/Makefile @@ -20,6 +20,7 @@ appledrm-y += iomfb_v14_7.o iomfb_v14_7_link.o appledrm-$(CONFIG_DRM_APPLE_DCP_T8132) += iomfb_m4.o t8132_ext_drm.o appledrm-$(CONFIG_DRM_APPLE_DCP_T8152) += t8152_atc.o t8152_aux.o t8152_dptx.o appledrm-$(CONFIG_DRM_APPLE_DCP_T8152) += t8152_hdmi.o t8152_pd.o +appledrm-$(CONFIG_DRM_APPLE_DCP_T8152) += iomfb_iboot.o iboot_drm.o appledrm-y += iomfb_h17p.o appledrm-$(CONFIG_DRM_APPLE_KUNIT_TEST) += iomfb_h17p_test.o parser_test.o appledrm-$(CONFIG_DRM_APPLE_KUNIT_TEST) += iomfb_external_hdr_hints_test.o diff --git a/drivers/gpu/drm/apple/dcp-internal.h b/drivers/gpu/drm/apple/dcp-internal.h index 857ece5849156..f55c05d9f1793 100644 --- a/drivers/gpu/drm/apple/dcp-internal.h +++ b/drivers/gpu/drm/apple/dcp-internal.h @@ -463,6 +463,7 @@ struct dcp_desktop_state { /* TODO: move IOMFB members to its own struct */ struct dcpext_scanout; struct t8152_display; +struct dcp_iboot; struct dcp_hdmi; struct dcp_cec; @@ -587,6 +588,8 @@ struct apple_dcp { enum dcp_firmware_version fw_compat; /* T8152: the DisplayPort hardware behind the DPTX port (t8152_display.h) */ struct t8152_display *t8152; + /* DCP_FIRMWARE_IBOOT: the display driven through it (iomfb_iboot.h) */ + struct dcp_iboot *iboot; /* Bumped by every iomfb poweron; see afk_compact_slot(). */ u32 afk_power_gen; /* @@ -1220,6 +1223,13 @@ static inline bool dcp_is_iboot(const struct apple_dcp *dcp) return dcp->fw_compat == DCP_FIRMWARE_IBOOT; } +struct apple_rtkit_shmem; +/* The common RTKit shared memory hooks, for a processor with its own RTKit ops. */ +int dcp_rtk_shmem_setup(void *cookie, struct apple_rtkit_shmem *bfr); +void dcp_rtk_shmem_destroy(void *cookie, struct apple_rtkit_shmem *bfr); +/* Keep a session that cannot be stopped, its DMA and this driver until reboot. */ +int dcp_retain_live_session(struct apple_dcp *dcp); + /* T8132 firmware uses its own wire codec, with the common DRM/KMS owner. */ static inline bool iomfb_is_m4(const struct apple_dcp *dcp) { diff --git a/drivers/gpu/drm/apple/dcp.c b/drivers/gpu/drm/apple/dcp.c index b16995083020d..396b62c988ecb 100644 --- a/drivers/gpu/drm/apple/dcp.c +++ b/drivers/gpu/drm/apple/dcp.c @@ -77,6 +77,7 @@ #include "dptx-service.h" #include "iomfb.h" #include "iomfb_internal.h" +#include "iomfb_iboot.h" #include "iomfb_m4.h" #include "ibootep.h" #include "iomfb_internal.h" @@ -114,15 +115,15 @@ static bool dcp_session_retained(struct device *dev) return retained; } -static int dcp_pin_live_session(struct apple_dcp *dcp) +/* + * Keep a live session's DMA device, domain use and this driver until reboot, + * and refuse a new probe of its device after an unbind. + */ +int dcp_retain_live_session(struct apple_dcp *dcp) { struct dcp_session_ref *session; - if ((!dcp->hw.adopt_live_session && - dcp->hw.iomfb_method_profile != - DCP_IOMFB_METHODS_H17P_EXTERNAL) || - !dcp_fail_stop(dcp) || - dcp->hw.iomfb_method_profile == DCP_IOMFB_METHODS_H17G || dcp->retain_dma) + if (dcp->retain_dma) return 0; session = kzalloc_obj(*session); if (!session) @@ -143,6 +144,17 @@ static int dcp_pin_live_session(struct apple_dcp *dcp) return 0; } +static int dcp_pin_live_session(struct apple_dcp *dcp) +{ + if ((!dcp->hw.adopt_live_session && + dcp->hw.iomfb_method_profile != + DCP_IOMFB_METHODS_H17P_EXTERNAL) || + !dcp_fail_stop(dcp) || + dcp->hw.iomfb_method_profile == DCP_IOMFB_METHODS_H17G || dcp->retain_dma) + return 0; + return dcp_retain_live_session(dcp); +} + static void dcp_release_dma_domain(void *data) { struct apple_dcp *dcp = data; @@ -1843,7 +1855,7 @@ static int dcp_check_iova_contiguous(struct iommu_domain *domain, return 0; } -static int dcp_rtk_shmem_setup(void *cookie, struct apple_rtkit_shmem *bfr) +int dcp_rtk_shmem_setup(void *cookie, struct apple_rtkit_shmem *bfr) { struct apple_dcp *dcp = cookie; int ret; @@ -1904,7 +1916,7 @@ static int dcp_rtk_shmem_setup(void *cookie, struct apple_rtkit_shmem *bfr) return 0; } -static void dcp_rtk_shmem_destroy(void *cookie, struct apple_rtkit_shmem *bfr) +void dcp_rtk_shmem_destroy(void *cookie, struct apple_rtkit_shmem *bfr) { struct apple_dcp *dcp = cookie; @@ -7884,13 +7896,51 @@ static int dcp_platform_probe_t8132_ext(struct platform_device *pdev) * bind's, may still appear (bounded by the deferred probe timeout); on any * other failure join as an unusable pipe. */ +/* + * A DCP driven through the iBoot interface alone (DCP_FIRMWARE_IBOOT, + * iomfb_iboot.c): no component of a display subsystem, with a DRM device of + * its own. Its adopted session cannot be stopped: once the firmware is + * touched, its DMA and this driver stay until reboot, and a failed start + * leaves the device bound without a display. + */ +static int dcp_platform_probe_iboot(struct platform_device *pdev) +{ + struct device *dev = &pdev->dev; + struct apple_dcp *dcp; + int ret; + + if (dcp_session_retained(dev)) + return dev_err_probe(dev, -EBUSY, "Previous live DCP session requires a reboot\n"); + dcp = kzalloc_obj(*dcp); + if (!dcp) + return -ENOMEM; + ret = devm_add_action_or_reset(dev, dcp_release_context, dcp); + if (ret) + return ret; + dcp->dev = dev; + dcp->hw = *(struct apple_dcp_hw_data *)of_device_get_match_data(dev); + dcp->fw_compat = dcp->hw.firmware_compat; + ret = iommu_device_use_default_domain(dev); + if (ret) + return ret; + ret = devm_add_action_or_reset(dev, dcp_release_dma_domain, dcp); + if (ret) + return ret; + platform_set_drvdata(pdev, dcp); + return dcp_iboot_probe(dcp); +} + static int dcp_platform_probe(struct platform_device *pdev) { + const struct apple_dcp_hw_data *hw = of_device_get_match_data(&pdev->dev); struct device *dev = &pdev->dev; struct apple_dcp *dcp; void *group; int ret; + if (hw->firmware_compat == DCP_FIRMWARE_IBOOT) + return dcp_platform_probe_iboot(pdev); + /* t6041_dcpext=0: stay off the external processors' hardware. */ if (of_device_is_compatible(dev->of_node, "apple,t6041-dcpext") && !t6041_dcpext) { @@ -7949,6 +7999,11 @@ static void dcp_platform_remove(struct platform_device *pdev) { struct apple_dcp *dcp = platform_get_drvdata(pdev); + /* No component and none of the works below; see dcp_iboot_remove(). */ + if (dcp && dcp_is_iboot(dcp)) { + dcp_iboot_remove(dcp); + return; + } if (dcp) { cancel_work_sync(&dcp->s2r_prewarm_work); cancel_delayed_work_sync(&dcp->s2r_prewarm_off_wq); @@ -8008,6 +8063,9 @@ VISIBLE_IF_KUNIT void dcp_platform_shutdown(struct platform_device *pdev) if (dcp_takeover_was_aborted(dcp)) return; + /* The iBoot interface's display stays as it is: no component to remove. */ + if (dcp && dcp_is_iboot(dcp)) + return; if (dcp && iomfb_is_m4(dcp)) { dcp_comp_unbind(&pdev->dev, NULL, NULL); @@ -8239,6 +8297,9 @@ static void dcp_platform_complete(struct device *dev) { struct apple_dcp *dcp = dev_get_drvdata(dev); + /* The iBoot interface's display stays powered across system sleep. */ + if (dcp_is_iboot(dcp)) + return; if (dcp_is_ext26(dcp)) { WRITE_ONCE(dcp->ext26_pm_preparing, false); if (dcp->ext26_pm_work_off && !READ_ONCE(dcp->ext26_blocked) && @@ -9724,6 +9785,9 @@ static int dcp_platform_suspend(struct device *dev) /* Its power sequences may crash the DCP and blank the panel. */ if (dcp_takeover_was_aborted(dcp)) return -EBUSY; + /* The iBoot interface's display stays powered across system sleep. */ + if (dcp_is_iboot(dcp)) + return 0; /* * An external DCP given up at probe may never have set up the works @@ -9877,6 +9941,8 @@ static int dcp_platform_resume(struct device *dev) if (dcp_takeover_was_aborted(dcp)) return 0; + if (dcp_is_iboot(dcp)) + return 0; if (dcp_v27_sleep_selected(dcp) && READ_ONCE(dcp->sleep_v27_closed)) return dcp_v27_sleep_resume(dcp); @@ -10113,8 +10179,29 @@ static const struct apple_dcp_hw_data apple_dcp_hw_t8142_dcpext = { .s2r_layout = &dcp_s2r_t8142_ext[0], }; +#if IS_ENABLED(CONFIG_DRM_APPLE_DCP_T8152) +/* + * T8152 (J873g): the DCP and both DCPEXTs, driven through the firmware's + * iBoot interface (disp0-service, dispext-service) over AFKv2, with the + * DPAV controller and DPTX port endpoints opened as the firmware expects + * them. The bootloader leaves each session running or quiesced; a running + * one is taken without the RTKit handshake. + */ +static const struct apple_dcp_hw_data apple_dcp_hw_t8152_iboot = { + .num_dptx_ports = 2, + .firmware_compat = DCP_FIRMWARE_IBOOT, + .afk_v2 = true, + .afk_adopt_endpoints = true, + .afk_announced_names = true, + .afk_timeout_ms = 3000, +}; +#endif + static const struct of_device_id of_match[] = { { .compatible = "apple,t8142-dcpext", .data = &apple_dcp_hw_t8142_dcpext }, +#if IS_ENABLED(CONFIG_DRM_APPLE_DCP_T8152) + { .compatible = "apple,t8152-dcp-iboot", .data = &apple_dcp_hw_t8152_iboot }, +#endif { .compatible = "apple,t6041-dcp", .data = &apple_dcp_hw_t6041, }, { .compatible = "apple,t6041-dcpext", .data = &apple_dcp_hw_t6041_dcpext, }, { .compatible = "apple,t6050-dcp", .data = &apple_dcp_hw_t6050 }, diff --git a/drivers/gpu/drm/apple/iboot_drm.c b/drivers/gpu/drm/apple/iboot_drm.c new file mode 100644 index 0000000000000..c0b3bd2311962 --- /dev/null +++ b/drivers/gpu/drm/apple/iboot_drm.c @@ -0,0 +1,1081 @@ +// SPDX-License-Identifier: GPL-2.0-only OR MIT +/* + * KMS of a DCP driven through the iBoot interface (DCP_FIRMWARE_IBOOT): a DRM + * device of its own per DCP with one primary plane, CRTC, encoder and + * connector. The firmware only takes width, height and refresh rate for a + * mode and picks the timing itself; the modes come from the sink's EDID, or + * from the firmware's list built with CVT. + * + * Framebuffers are shadow buffers copied by the CPU into the scanout + * buffers, and a modeset only changes their geometry, as the bootloader + * does. Changing the surface address between Linux buffers made later + * modesets crash the firmware (IOMFB axi_rd_err), so the scanout buffers + * are the only surfaces the firmware gets. There is no vblank interrupt. + */ + +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "dcp-internal.h" +#include "ibootep.h" +#include "iomfb_iboot.h" +#include "t8152_display.h" + +/* + * Swap between the scanout buffers per frame. Off: frames are copied into + * the buffer on screen, which tears during motion. + */ +static bool buffered = true; +module_param_named(iboot_buffered, buffered, bool, 0444); +MODULE_PARM_DESC(iboot_buffered, "iBoot interface displays: copy each frame into a scanout buffer that is not on screen (default: on)"); + +DEFINE_DRM_GEM_DMA_FOPS(dcp_iboot_fops); + +/* + * Not the display subsystem's "apple": these DCPs take no direct scanout, + * every frame is copied, so userspace must not treat them as that one. + */ +static const struct drm_driver dcp_iboot_drm_driver = { + /* + * Frames are copied by the CPU, imported ones too: a compositor + * driving both displays imports one device's buffers into the other. + */ + DRM_GEM_DMA_DRIVER_OPS_VMAP, + DRM_FBDEV_DMA_DRIVER_OPS, + .name = "apple-dcpib", + .desc = "Apple DCP (iBoot interface)", + .major = 1, + .minor = 0, + .driver_features = DRIVER_GEM | DRIVER_MODESET | DRIVER_ATOMIC, + .fops = &dcp_iboot_fops, +}; + +struct dcp_iboot *dcp_iboot_alloc(struct device *dev) +{ + return devm_drm_dev_alloc(dev, &dcp_iboot_drm_driver, struct dcp_iboot, drm); +} + +const char *dcp_iboot_driver_name(void) +{ + return dcp_iboot_drm_driver.name; +} + +static const u32 dcp_iboot_formats[] = { + DRM_FORMAT_XRGB8888, + DRM_FORMAT_ARGB8888, +}; + +static int iboot_refresh(const struct iboot_timing_mode *t) +{ + return (le32_to_cpu(t->fps) + 0x8000) >> 16; +} + +/* + * Whether @t works: the link at the PHY's highest rate must carry it (24 bpp + * plus about 12% blanking against rate x lanes x 0.8 for 8b/10b); a mode + * beyond that only blanks the display. + */ +static bool iboot_mode_fits(struct dcp_iboot *ib, const struct iboot_timing_mode *t) +{ + const struct t8152_phy_ops *phy = ib->disp.phy; + /* + * The lanes the firmware may train, as GET_MAX_LANE_COUNT reports them: + * the USB-C route's. Not the last training's: a sink that replugs while + * the display is off is trained with a single lane. + */ + u32 lanes = (phy->max_lane_count ? phy->max_lane_count(&ib->disp) : 4) ?: 2; + u64 link = (u64)phy->max_link_rate * 270000000ULL * lanes * 8 / 10; + u64 pixels = (u64)le32_to_cpu(t->width) * le32_to_cpu(t->height) * + iboot_refresh(t); + + /* Hotplug may return modes larger than the buffers allocated at probe. */ + if ((u64)ALIGN(le32_to_cpu(t->width) * 4, 64) * le32_to_cpu(t->height) > + ib->scan[0].size) + return false; + return pixels * 24 * 112 / 100 <= link; +} + +/* @mode's refresh rate in 16.16, as drm_mode_vrefresh() computes it. */ +static u32 iboot_mode_fps(const struct drm_display_mode *mode) +{ + u64 num = mul_u32_u32(mode->clock, 1000) << 16; + u64 den = mul_u32_u32(mode->htotal, mode->vtotal); + + if (mode->flags & DRM_MODE_FLAG_INTERLACE) + num *= 2; + if (mode->flags & DRM_MODE_FLAG_DBLSCAN) + den *= 2; + if (mode->vscan > 1) + den *= mode->vscan; + return den ? DIV64_U64_ROUND_CLOSEST(num, den) : 0; +} + +/* + * The firmware mode for @mode: same size, refresh rate within 1 Hz (EDID's + * 59.94 against the firmware's 60.00), the nearest one by the exact rate. + * The firmware picks the timing from the rate, and next to the sink's + * 2560x1440@59.951 (241.5 MHz) it lists a 60.000 standard mode that it + * drives with CVT at 312.25 MHz, which the J873g's USB-C EV2780 rejects. + */ +static const struct iboot_timing_mode * +iboot_find_mode(struct dcp_iboot *ib, const struct drm_display_mode *mode) +{ + const struct iboot_timing_mode *best = NULL; + u32 fps = iboot_mode_fps(mode); + + for (unsigned int i = 0; i < ib->nr_tmodes; i++) { + const struct iboot_timing_mode *t = &ib->tmodes[i]; + + if (!t->valid || le32_to_cpu(t->width) != mode->hdisplay || + le32_to_cpu(t->height) != mode->vdisplay || + abs_diff(le32_to_cpu(t->fps), fps) > 0x10000 || + !iboot_mode_fits(ib, t)) + continue; + if (!best || abs_diff(le32_to_cpu(t->fps), fps) < + abs_diff(le32_to_cpu(best->fps), fps)) + best = t; + } + return best; +} + +/* + * Valid, at most 32 bpp, the highest depth, then the lowest colorimetry, + * encoding and EOTF, as the bootloader chooses. Deeper modes the HDMI + * converter offers are refused by SET_MODE. + */ +static bool iboot_color_better(const struct iboot_color_mode *a, + const struct iboot_color_mode *b) +{ + u32 abpp = le32_to_cpu(a->bpp), bbpp = le32_to_cpu(b->bpp); + bool afits = abpp <= 32, bfits = bbpp <= 32; + + if (!!a->valid != !!b->valid) + return a->valid; + if (afits != bfits) + return afits; + if (abpp != bbpp) + return abpp > bbpp; + if (a->colorimetry != b->colorimetry) + return le32_to_cpu(a->colorimetry) < le32_to_cpu(b->colorimetry); + if (a->encoding != b->encoding) + return le32_to_cpu(a->encoding) < le32_to_cpu(b->encoding); + return le32_to_cpu(a->eotf) < le32_to_cpu(b->eotf); +} + +static const struct iboot_color_mode *iboot_find_color(struct dcp_iboot *ib) +{ + const struct iboot_color_mode *best = NULL; + + for (unsigned int i = 0; i < ib->nr_cmodes; i++) + if (!best || iboot_color_better(&ib->cmodes[i], best)) + best = &ib->cmodes[i]; + return best && best->valid ? best : NULL; +} + +/* + * A conservative console mode from 50 to 60 Hz, up to 1920x1080 on + * USB-C. Userspace can select the larger modes, including 2560x1440@60, + * after the sink's EDID is available. + */ +static const struct iboot_timing_mode *iboot_preferred(struct dcp_iboot *ib) +{ + const struct iboot_timing_mode *best = NULL; + + for (unsigned int i = 0; i < ib->nr_tmodes; i++) { + const struct iboot_timing_mode *t = &ib->tmodes[i]; + + /* + * Byte 0 bit 6 marks standard modes a converter adds on its own + * (the J873g PS190 lists 3840x2160 for a 2560x1440 monitor); + * the bootloader's largest mode skips them too. + */ + if (!t->valid || iboot_refresh(t) > 60 || iboot_refresh(t) < 50 || + (t->flags[0] & 0x40) || + (!ib->disp.hdmi && (le32_to_cpu(t->width) > 1920 || + le32_to_cpu(t->height) > 1080)) || + !iboot_mode_fits(ib, t)) + continue; + if (!best || (u64)le32_to_cpu(t->width) * le32_to_cpu(t->height) > + (u64)le32_to_cpu(best->width) * le32_to_cpu(best->height) || + (t->width == best->width && t->height == best->height && + le32_to_cpu(t->fps) > le32_to_cpu(best->fps))) + best = t; + } + return best; +} + +/* + * The sink's EDID modes the firmware also has (mode_valid drops the rest), + * the largest of them preferred: firmware modes the sink does not list, + * such as 1400x1050 on the J873g EV2780, stay black. + */ +static int iboot_edid_get_modes(struct drm_connector *connector) +{ + struct dcp_iboot *ib = to_dcp_iboot(connector->dev); + struct drm_display_mode *mode, *best = NULL; + int count = drm_edid_connector_add_modes(connector); + + mutex_lock(&ib->lock); + list_for_each_entry(mode, &connector->probed_modes, head) { + mode->type &= ~DRM_MODE_TYPE_PREFERRED; + if (!iboot_find_mode(ib, mode)) + continue; + if (!best || mode->hdisplay * mode->vdisplay > + best->hdisplay * best->vdisplay || + (mode->hdisplay * mode->vdisplay == + best->hdisplay * best->vdisplay && + drm_mode_vrefresh(mode) > drm_mode_vrefresh(best))) + best = mode; + } + if (best) + best->type |= DRM_MODE_TYPE_PREFERRED; + mutex_unlock(&ib->lock); + return count; +} + +static int iboot_connector_get_modes(struct drm_connector *connector) +{ + struct dcp_iboot *ib = to_dcp_iboot(connector->dev); + const struct iboot_timing_mode *preferred; + int count = 0; + + drm_edid_connector_update(connector, ib->edid); + if (ib->edid) + return iboot_edid_get_modes(connector); + + /* Without an EDID, the firmware's list. */ + mutex_lock(&ib->lock); + preferred = iboot_preferred(ib); + for (unsigned int i = 0; i < ib->nr_tmodes; i++) { + const struct iboot_timing_mode *t = &ib->tmodes[i]; + struct drm_display_mode *mode; + + if (!t->valid || !iboot_mode_fits(ib, t)) + continue; + mode = drm_cvt_mode(connector->dev, le32_to_cpu(t->width), + le32_to_cpu(t->height), iboot_refresh(t), + false, false, false); + if (!mode) + continue; + if (t == preferred) + mode->type |= DRM_MODE_TYPE_PREFERRED; + drm_mode_probed_add(connector, mode); + count++; + } + mutex_unlock(&ib->lock); + return count; +} + +static enum drm_mode_status +iboot_connector_mode_valid(struct drm_connector *connector, + const struct drm_display_mode *mode) +{ + struct dcp_iboot *ib = to_dcp_iboot(connector->dev); + + return iboot_find_mode(ib, mode) ? MODE_OK : MODE_NOMODE; +} + +static const struct drm_connector_helper_funcs iboot_connector_helper = { + .get_modes = iboot_connector_get_modes, + .mode_valid = iboot_connector_mode_valid, +}; + +static int iboot_set_mode_locked(struct dcp_iboot *ib, + const struct iboot_timing_mode *t, + const struct iboot_color_mode *c); + +/* With ib->lock held; queued work will observe the cleared pending slot. */ +static void iboot_cancel_swap_locked(struct dcp_iboot *ib) +{ + hrtimer_cancel(&ib->swap_timer); + ib->pending = -1; +} + +/* Read the sink's EDID through the PHY, if it can, and keep it. */ +void dcp_iboot_update_edid(struct dcp_iboot *ib) +{ + const struct drm_edid *edid = NULL; + u8 *buf; + int len; + + if (!ib->disp.phy->read_edid) + return; + buf = kzalloc(4 * EDID_LENGTH, GFP_KERNEL); + if (!buf) + return; + len = ib->disp.phy->read_edid(&ib->disp, buf, 4 * EDID_LENGTH); + if (len >= EDID_LENGTH) { + edid = drm_edid_alloc(buf, len); + if (edid && !drm_edid_valid(edid)) { + drm_edid_free(edid); + edid = NULL; + } + } + if (!edid) + dev_warn(ib->dcp->dev, "no EDID: %d\n", len); + else + dev_info(ib->dcp->dev, "EDID: %d bytes\n", len); + drm_edid_free(ib->edid); + ib->edid = edid; + kfree(buf); +} + +static enum drm_connector_status +iboot_connector_detect(struct drm_connector *connector, bool force) +{ + struct dcp_iboot *ib = to_dcp_iboot(connector->dev); + struct t8152_display *disp = &ib->disp; + struct apple_dcp *dcp = ib->dcp; + enum drm_connector_status status; + bool flipped = false; + int hpd; + + mutex_lock(&ib->lock); + t8152_hdmi_port_poll(disp); + if (disp->hpd_gpio) { + hpd = gpiod_get_value_cansleep(disp->hpd_gpio); + } else if (disp->phy->detect) { + hpd = disp->phy->detect(disp, &flipped); + } else { + status = ib->nr_tmodes ? connector_status_connected : + connector_status_disconnected; + goto out; + } + if (hpd < 0) { + status = connector_status_unknown; + goto out; + } + if (hpd != disp->hpd) { + dev_info(dcp->dev, "sink %s\n", hpd ? "plugged" : "unplugged"); + disp->hpd = hpd; + /* + * USB-C: the DPTX port's HPD follows the PD controller, and a + * new plug may come the other way round. + */ + if (disp->pd) { + if (!hpd || ib->powered) { + iboot_cancel_swap_locked(ib); + if (t8152_dptx_disconnect(disp, 0)) + dev_err(dcp->dev, "DPTX release failed\n"); + if (!ibootep_set_power(dcp, false)) { + ib->powered = false; + ib->mode_set = false; + disp->phy->deactivate(disp); + } + } + /* + * A plug while the display stayed on (unplug and plug + * between two polls): start the PHY cold again, the + * new plug may come the other way round. + */ + if (hpd) + disp->phy->deactivate(disp); + /* The sink may be on another port now. */ + if (hpd && disp->phy->select) + disp->phy->select(disp); + if (hpd && disp->phy->set_orientation) + disp->phy->set_orientation(disp, flipped); + /* The firmware talks AUX as soon as it sees HPD. */ + if (hpd && disp->phy->activate(disp)) + dev_err(dcp->dev, "PHY activation failed\n"); + if (hpd && t8152_dptx_connect(disp, 0)) + dev_err(dcp->dev, "DPTX connect failed\n"); + if (hpd && t8152_dptx_set_hpd(disp, 0, true)) + dev_err(dcp->dev, "DPTX HPD update failed\n"); + } + /* A new sink: the firmware reads its EDID, take the new lists. */ + if (hpd && (dcp_iboot_wait_hpd(ib) || dcp_iboot_get_modes(ib))) { + dev_err(dcp->dev, "no modes from the new sink\n"); + ib->nr_tmodes = 0; + ib->nr_cmodes = 0; + /* A sleeping EIZO may need a fresh DP alt mode entry. */ + if (disp->pd && !disp->on_hdmi && + time_after(jiffies, ib->pd_reset_time + 60 * HZ)) { + ib->pd_reset_time = jiffies; + schedule_work(&ib->pd_reset_work); + } + } + /* + * The EDID once the firmware has read it, so the sink's DDC + * answers, and before SET_MODE: reading it borrows the AUX path + * while the firmware does no AUX. + */ + if (hpd && ib->nr_tmodes) + dcp_iboot_update_edid(ib); + /* + * A fast reconnect can finish before userspace disables the CRTC. + * Restore its requested state even though unplug powered it off. + */ + if (hpd && ib->enabled && ib->cur_mode.valid) { + const struct iboot_timing_mode *t = NULL; + const struct iboot_color_mode *c = iboot_find_color(ib); + int ret = 0; + + for (unsigned int i = 0; i < ib->nr_tmodes; i++) { + const struct iboot_timing_mode *candidate = &ib->tmodes[i]; + + if (candidate->valid && candidate->width == ib->cur_mode.width && + candidate->height == ib->cur_mode.height && + candidate->fps == ib->cur_mode.fps) { + t = candidate; + break; + } + } + if (t && c && !ib->powered) { + ret = ibootep_set_surface(dcp, ib->scan_dva[ib->front], + le32_to_cpu(t->width), + le32_to_cpu(t->height), + ALIGN(le32_to_cpu(t->width) * 4, 64)); + if (!ret) + ret = ibootep_set_power(dcp, true); + ib->powered = !ret; + } + if (t && c && !ret) + iboot_set_mode_locked(ib, t, c); + } + } + status = hpd && ib->nr_tmodes ? connector_status_connected : + connector_status_disconnected; +out: + mutex_unlock(&ib->lock); + return status; +} + +static const struct drm_connector_funcs iboot_connector_funcs = { + .detect = iboot_connector_detect, + .fill_modes = drm_helper_probe_single_connector_modes, + .destroy = drm_connector_cleanup, + .reset = drm_atomic_helper_connector_reset, + .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state, + .atomic_destroy_state = drm_atomic_helper_connector_destroy_state, +}; + +static int iboot_plane_atomic_check(struct drm_plane *plane, + struct drm_atomic_commit *state) +{ + struct drm_plane_state *new = drm_atomic_get_new_plane_state(state, plane); + struct dcp_iboot *ib = to_dcp_iboot(plane->dev); + struct drm_crtc_state *crtc_state = NULL; + u32 width, height; + int ret; + + if (new->crtc) + crtc_state = drm_atomic_get_new_crtc_state(state, new->crtc); + /* The surface fills the whole mode; no scaling or positioning. */ + ret = drm_atomic_helper_check_plane_state(new, crtc_state, + DRM_PLANE_NO_SCALING, + DRM_PLANE_NO_SCALING, + false, false); + if (ret || !new->visible) + return ret; + /* The helper clips src/dst; raw src_w/src_h may exceed the CRTC. */ + if ((new->src.x1 | new->src.y1 | new->src.x2 | new->src.y2) & 0xffff) + return -EINVAL; + width = drm_rect_width(&new->src) >> 16; + height = drm_rect_height(&new->src) >> 16; + return (u64)ALIGN(width * 4, 64) * height <= ib->scan[0].size ? 0 : -EINVAL; +} + +static void iboot_rect_grow(struct drm_rect *r, const struct drm_rect *add) +{ + if (!drm_rect_visible(r)) { + *r = *add; + return; + } + r->x1 = min(r->x1, add->x1); + r->y1 = min(r->y1, add->y1); + r->x2 = max(r->x2, add->x2); + r->y2 = max(r->y2, add->y2); +} + +/* The current mode's refresh period, from its exact 16.16 rate. */ +static s64 iboot_period_ns(struct dcp_iboot *ib) +{ + u32 fps = le32_to_cpu(ib->cur_mode.fps); + + if (fps < (24 << 16)) + fps = 60 << 16; + return div64_u64((u64)NSEC_PER_SEC << 16, fps); +} + +/* Whether a frame may be shown: the display is on, set up and alive. */ +static bool iboot_scanning_locked(struct dcp_iboot *ib) +{ + struct apple_rtkit *rtk = READ_ONCE(ib->dcp->rtk); + + return ib->powered && ib->mode_set && ib->disp.hpd && rtk && + !apple_rtkit_is_crashed(rtk); +} + +/* Show scan[buf]. Called with ib->lock held. */ +static void iboot_swap_locked(struct dcp_iboot *ib, unsigned int buf, u32 w, + u32 h, u32 pitch) +{ + int ret; + + if (!iboot_scanning_locked(ib)) + return; + + /* The pipe must see the whole frame before it is swapped in. */ + wmb(); + ret = ibootep_set_surface(ib->dcp, ib->scan_dva[buf], w, h, pitch); + if (ret) { + /* Still on the old buffer, which misses this frame. */ + dev_err_ratelimited(ib->dcp->dev, "swap failed: %d\n", ret); + ib->stale[ib->front] = DRM_RECT_INIT(0, 0, INT_MAX / 2, INT_MAX / 2); + return; + } + ib->last_swap = ktime_get(); + ib->retired[ib->front] = ib->last_swap; + ib->front = buf; +} + +static void iboot_swap_work(struct work_struct *work) +{ + struct dcp_iboot *ib = container_of(work, struct dcp_iboot, swap_work); + + mutex_lock(&ib->lock); + if (ib->pending >= 0) { + iboot_swap_locked(ib, ib->pending, ib->pending_w, + ib->pending_h, ib->pending_pitch); + ib->pending = -1; + } + mutex_unlock(&ib->lock); +} + +static enum hrtimer_restart iboot_swap_timer(struct hrtimer *t) +{ + struct dcp_iboot *ib = container_of(t, struct dcp_iboot, swap_timer); + + queue_work(system_highpri_wq, &ib->swap_work); + return HRTIMER_NORESTART; +} + +/* + * Copy the frame into a scanout buffer that is not on screen and show it. + * That buffer already holds an older frame: copy this frame's damage and + * what changed since that buffer was last written. + * + * SET_SURFACE returns before the pipe takes the buffer at its next frame + * start, and there is no vblank interrupt. Several swaps within one refresh + * let an older frame stay on screen (a cursor that jumps back), so swaps are + * spaced one refresh apart: a frame that comes sooner waits in a pending + * buffer, which a newer frame overwrites, and a timer swaps it in. The + * commit itself never waits, so a client's next frame is not held up. + */ +static void iboot_plane_atomic_update(struct drm_plane *plane, + struct drm_atomic_commit *state) +{ + struct drm_plane_state *old = drm_atomic_get_old_plane_state(state, plane); + struct drm_plane_state *new = drm_atomic_get_new_plane_state(state, plane); + struct drm_shadow_plane_state *shadow = to_drm_shadow_plane_state(new); + struct dcp_iboot *ib = to_dcp_iboot(plane->dev); + struct drm_framebuffer *fb = new->fb; + struct drm_atomic_helper_damage_iter iter; + struct drm_rect clip, source_clip, src, full, damage = { 0, 0, 0, 0 }; + unsigned int pitch, back; + struct iosys_map dst; + s64 period, since; + + if (!fb || !new->visible) + return; + src = DRM_RECT_INIT(new->src.x1 >> 16, new->src.y1 >> 16, + drm_rect_width(&new->src) >> 16, + drm_rect_height(&new->src) >> 16); + full = DRM_RECT_INIT(0, 0, drm_rect_width(&src), drm_rect_height(&src)); + pitch = ALIGN(drm_rect_width(&full) * 4, 64); + drm_atomic_helper_damage_iter_init(&iter, old, new); + drm_atomic_for_each_plane_damage(&iter, &clip) { + if (!drm_rect_intersect(&clip, &src)) + continue; + drm_rect_translate(&clip, -src.x1, -src.y1); + iboot_rect_grow(&damage, &clip); + } + if (!drm_rect_visible(&damage)) + return; + + mutex_lock(&ib->lock); + if (!iboot_scanning_locked(ib)) + goto out; + period = iboot_period_ns(ib); + if (!READ_ONCE(buffered)) { + /* Into the buffer on screen (tears during motion). */ + back = ib->front; + } else if (ib->pending >= 0) { + back = ib->pending; + } else { + /* The other buffer that left the screen first. */ + unsigned int a = (ib->front + 1) % DCP_IBOOT_SCANOUT_BUFS; + unsigned int b = (ib->front + 2) % DCP_IBOOT_SCANOUT_BUFS; + s64 wait; + + back = ktime_before(ib->retired[a], ib->retired[b]) ? a : b; + /* Off screen for a full refresh: only after a mode change. */ + wait = period - ktime_to_ns(ktime_sub(ktime_get(), ib->retired[back])); + if (ib->mode_set && wait > 0) + usleep_range(div_s64(wait, NSEC_PER_USEC), + div_s64(wait, NSEC_PER_USEC) + 100); + } + clip = ib->stale[back]; + iboot_rect_grow(&clip, &damage); + if (drm_rect_intersect(&clip, &full)) { + iosys_map_set_vaddr(&dst, ib->scan_wc[back]); + iosys_map_incr(&dst, drm_fb_clip_offset(pitch, fb->format, &clip)); + source_clip = clip; + drm_rect_translate(&source_clip, src.x1, src.y1); + drm_fb_memcpy(&dst, &pitch, shadow->data, fb, &source_clip); + } + ib->stale[back] = (struct drm_rect){ 0, 0, 0, 0 }; + for (unsigned int i = 0; i < DCP_IBOOT_SCANOUT_BUFS; i++) + if (i != back) + iboot_rect_grow(&ib->stale[i], &damage); + if (back == ib->front) + goto out; + + since = ktime_to_ns(ktime_sub(ktime_get(), ib->last_swap)); + if (ib->pending < 0 && (!ib->mode_set || since >= period)) { + iboot_swap_locked(ib, back, drm_rect_width(&full), + drm_rect_height(&full), pitch); + goto out; + } + ib->pending_w = drm_rect_width(&full); + ib->pending_h = drm_rect_height(&full); + ib->pending_pitch = pitch; + if (ib->pending < 0) { + ib->pending = back; + hrtimer_start(&ib->swap_timer, ktime_add_ns(ib->last_swap, period), + HRTIMER_MODE_ABS); + } +out: + mutex_unlock(&ib->lock); +} + +static const struct drm_plane_helper_funcs iboot_plane_helper = { + DRM_GEM_SHADOW_PLANE_HELPER_FUNCS, + .atomic_check = iboot_plane_atomic_check, + .atomic_update = iboot_plane_atomic_update, +}; + +static const struct drm_plane_funcs iboot_plane_funcs = { + .update_plane = drm_atomic_helper_update_plane, + .disable_plane = drm_atomic_helper_disable_plane, + .destroy = drm_plane_cleanup, + DRM_GEM_SHADOW_PLANE_FUNCS, +}; + +static int iboot_crtc_atomic_check(struct drm_crtc *crtc, + struct drm_atomic_commit *state) +{ + struct drm_crtc_state *new = drm_atomic_get_new_crtc_state(state, crtc); + struct dcp_iboot *ib = to_dcp_iboot(crtc->dev); + + /* No vblank interrupt: the helpers complete events after the flush. */ + new->no_vblank = true; + if (new->enable && !iboot_find_mode(ib, &new->mode)) + return -EINVAL; + return 0; +} + +/* SET_MODE, then the scanout buffer with the new mode's geometry. */ +static int iboot_set_mode_locked(struct dcp_iboot *ib, + const struct iboot_timing_mode *t, + const struct iboot_color_mode *c) +{ + int ret; + + /* + * Right after power on the firmware may still train the link, and it + * fails SET_MODE with an error status until the link is up. It sends + * no message once it is: the DPTX calls it makes while training (link + * configuration, rate, lane count) have no last one, and a sink can + * be trained twice in a row. So SET_MODE is retried. In the traced + * J873g modesets the link was up 76 to 229 ms after power on; one + * failed SET_MODE takes about 200 ms itself, so 200 ms between tries + * cover a second training, and five tries give up after about two + * seconds. On USB-C the DP alt mode reentry below takes over then. + */ + for (int i = 0; i < 5; i++) { + ret = ibootep_set_mode(ib->dcp, t, c); + if (ret != -EIO) + break; + msleep(200); + } + if (ret) { + dev_err(ib->dcp->dev, "set_mode failed: %d\n", ret); + /* + * USB-C: a sink that no longer answers AUX (seen after the + * monitor's deep sleep) comes back with a fresh DP alt mode + * entry; the plug that follows sets the mode again. At most + * once a minute, so a dead sink does not loop. + */ + if (ib->disp.pd && !ib->disp.on_hdmi && + time_after(jiffies, ib->pd_reset_time + 60 * HZ)) { + ib->cur_mode = *t; + ib->mode_failed = true; + ib->pd_reset_time = jiffies; + schedule_work(&ib->pd_reset_work); + } + return ret; + } + ib->cur_mode = *t; + ib->mode_set = true; + ib->mode_failed = false; + /* The buffers hold the old geometry: the next frame is copied whole. */ + for (unsigned int i = 0; i < DCP_IBOOT_SCANOUT_BUFS; i++) + ib->stale[i] = DRM_RECT_INIT(0, 0, INT_MAX / 2, INT_MAX / 2); + /* A pending frame has the old geometry. */ + iboot_cancel_swap_locked(ib); + /* As the bootloader swaps after a modeset. */ + ret = ibootep_set_surface(ib->dcp, ib->scan_dva[ib->front], + le32_to_cpu(t->width), le32_to_cpu(t->height), + ALIGN(le32_to_cpu(t->width) * 4, 64)); + if (ret) + dev_err(ib->dcp->dev, "set_surface failed: %d\n", ret); + return ret; +} + +/* + * The boot mode again: a display powered on without SET_MODE counts as idle + * to the firmware's AV stack, which powers the link off after 30 s. + */ +int dcp_iboot_set_boot_mode(struct dcp_iboot *ib) +{ + const struct iboot_timing_mode *best = NULL; + const struct iboot_color_mode *c; + int ret = -ENOENT; + + mutex_lock(&ib->lock); + for (unsigned int i = 0; i < ib->nr_tmodes; i++) { + const struct iboot_timing_mode *t = &ib->tmodes[i]; + + if (!t->valid || le32_to_cpu(t->width) != ib->boot_w || + le32_to_cpu(t->height) != ib->boot_h || + iboot_refresh(t) > 60) + continue; + if (!best || le32_to_cpu(t->fps) > le32_to_cpu(best->fps)) + best = t; + } + c = iboot_find_color(ib); + if (best && c) + ret = iboot_set_mode_locked(ib, best, c); + mutex_unlock(&ib->lock); + return ret; +} + +/* + * The DCPAV service starts the firmware's idle power off timer when its video + * interface comes up: until a SET_MODE follows, it counts the powered-on + * display as idle and takes the link down 30 s later. The probe's SET_MODE + * comes before the interface starts, so one follows it here. + */ +void dcp_iboot_reset_mode(struct dcp_iboot *ib) +{ + const struct iboot_color_mode *c; + + mutex_lock(&ib->lock); + c = iboot_find_color(ib); + if (c && ib->powered && ib->mode_set) + iboot_set_mode_locked(ib, &ib->cur_mode, c); + mutex_unlock(&ib->lock); +} + +/* + * From the AFK receive work of the DPAV endpoint: let the firmware finish + * opening the interface before the SET_MODE, which waits for a reply that + * another endpoint's receive work completes. + */ +void dcp_iboot_av_started(struct dcp_iboot *ib) +{ + queue_delayed_work(system_percpu_wq, &ib->av_work, msecs_to_jiffies(1000)); +} + +static void iboot_crtc_atomic_enable(struct drm_crtc *crtc, + struct drm_atomic_commit *state) +{ + struct drm_crtc_state *new = drm_atomic_get_new_crtc_state(state, crtc); + struct dcp_iboot *ib = to_dcp_iboot(crtc->dev); + struct apple_dcp *dcp = ib->dcp; + const struct iboot_timing_mode *t; + const struct iboot_color_mode *c; + int ret; + + mutex_lock(&ib->lock); + t = iboot_find_mode(ib, &new->mode); + c = iboot_find_color(ib); + if (!t || !c) + goto out; + ib->enabled = true; + if (!ib->powered) { + /* The PHY starts cold first; the firmware then trains. */ + ret = ib->disp.phy->activate(&ib->disp); + if (ret) { + dev_err(dcp->dev, "PHY activation failed: %d\n", ret); + goto out; + } + /* A pipe powered on without a surface fetches from a null base. */ + ret = ibootep_set_surface(dcp, ib->scan_dva[ib->front], + le32_to_cpu(t->width), le32_to_cpu(t->height), + ALIGN(le32_to_cpu(t->width) * 4, 64)); + if (ret) { + dev_err(dcp->dev, "set_surface failed: %d\n", ret); + goto out; + } + ret = ibootep_set_power(dcp, true); + if (ret) { + dev_err(dcp->dev, "set_power failed: %d\n", ret); + goto out; + } + ib->powered = true; + } + if (ib->mode_set && !memcmp(t, &ib->cur_mode, sizeof(*t))) + goto out; + dev_info(dcp->dev, "modeset %ux%u@%d\n", le32_to_cpu(t->width), + le32_to_cpu(t->height), iboot_refresh(t)); + /* + * As the bootloader configures a display: query the sink and both mode + * lists again before SET_MODE, and look the mode up in the fresh list. + * The DPTX port stays connected; reconnecting it here drops the link + * and the HDMI firmware then fails SET_MODE. + */ + ret = ibootep_set_power(dcp, true); + if (!ret) + ret = ibootep_get_hpd(dcp, NULL, NULL) < 0; + if (!ret) + ret = dcp_iboot_get_modes(ib); + if (ret) + dev_err(dcp->dev, "refreshing the modes failed: %d\n", ret); + t = iboot_find_mode(ib, &new->mode); + c = iboot_find_color(ib); + if (!t || !c) + goto out; + iboot_set_mode_locked(ib, t, c); +out: + mutex_unlock(&ib->lock); +} + +static void iboot_crtc_atomic_disable(struct drm_crtc *crtc, + struct drm_atomic_commit *state) +{ + struct drm_crtc_state *new = drm_atomic_get_new_crtc_state(state, crtc); + struct dcp_iboot *ib = to_dcp_iboot(crtc->dev); + int ret, idx; + + /* A mode change: SET_MODE alone switches, as before. */ + if (new->active) + return; + /* + * After an unbind the firmware session is retained as it is, and the + * shutdown commit only releases the state. + */ + if (!drm_dev_enter(crtc->dev, &idx)) + return; + /* + * Display off (DPMS, blanking): the firmware stops the pipe and the + * monitor goes to sleep. The next enable sets the mode again. + */ + mutex_lock(&ib->lock); + ib->enabled = false; + iboot_cancel_swap_locked(ib); + if (ib->powered) { + ret = ibootep_set_power(ib->dcp, false); + if (ret) { + dev_err(ib->dcp->dev, "set_power off failed: %d\n", ret); + } else { + dev_info(ib->dcp->dev, "display off\n"); + /* Nothing scans out: power the PHY down as well. */ + if (!ib->disp.phy->deactivate(&ib->disp)) + ib->disp.port[0].link_rate = 0; + } + ib->powered = !!ret; + ib->mode_set = false; + } + mutex_unlock(&ib->lock); + drm_dev_exit(idx); +} + +static const struct drm_crtc_helper_funcs iboot_crtc_helper = { + .atomic_check = iboot_crtc_atomic_check, + .atomic_enable = iboot_crtc_atomic_enable, + .atomic_disable = iboot_crtc_atomic_disable, +}; + +static const struct drm_crtc_funcs iboot_crtc_funcs = { + .reset = drm_atomic_helper_crtc_reset, + .destroy = drm_crtc_cleanup, + .set_config = drm_atomic_helper_set_config, + .page_flip = drm_atomic_helper_page_flip, + .atomic_duplicate_state = drm_atomic_helper_crtc_duplicate_state, + .atomic_destroy_state = drm_atomic_helper_crtc_destroy_state, +}; + +static const struct drm_encoder_funcs iboot_encoder_funcs = { + .destroy = drm_encoder_cleanup, +}; + +/* Modeset before the plane update, which copies at the new geometry. */ +static const struct drm_mode_config_helper_funcs iboot_mode_config_helpers = { + .atomic_commit_tail = drm_atomic_helper_commit_tail_rpm, +}; + +static const struct drm_mode_config_funcs iboot_mode_config_funcs = { + /* fbdev writes reach the scanout buffer through the dirty damage */ + .fb_create = drm_gem_fb_create_with_dirty, + .atomic_check = drm_atomic_helper_check, + .atomic_commit = drm_atomic_helper_commit, +}; + +static void iboot_pd_reset_work(struct work_struct *work) +{ + struct dcp_iboot *ib = container_of(work, struct dcp_iboot, pd_reset_work); + struct t8152_display *disp = &ib->disp; + int ret; + + dev_info(ib->dcp->dev, "USB-C sink does not answer, entering DP alt mode again\n"); + /* The reset may finish between polls. Release the old link explicitly. */ + mutex_lock(&ib->lock); + iboot_cancel_swap_locked(ib); + ret = t8152_dptx_disconnect(disp, 0); + if (!ret) + ret = ibootep_set_power(ib->dcp, false); + if (!ret) { + ib->powered = false; + ib->mode_set = false; + ret = disp->phy->deactivate(disp); + } + if (ret) { + mutex_unlock(&ib->lock); + dev_err(ib->dcp->dev, "cannot release old DP link: %d\n", ret); + return; + } + disp->hpd = 0; + ret = t8152_pd_reconnect(disp); + mutex_unlock(&ib->lock); + if (ret) { + dev_err(ib->dcp->dev, "DP alt mode entry failed: %d\n", ret); + return; + } + drm_kms_helper_hotplug_event(&ib->drm); +} + +static void iboot_hpd_work(struct work_struct *work) +{ + struct dcp_iboot *ib = container_of(to_delayed_work(work), struct dcp_iboot, + hpd_work); + + if (!READ_ONCE(ib->ready)) + return; + drm_helper_hpd_irq_event(&ib->drm); + if (READ_ONCE(ib->ready)) + queue_delayed_work(system_freezable_wq, &ib->hpd_work, + msecs_to_jiffies(250)); +} + +void dcp_iboot_kms_stop(struct dcp_iboot *ib) +{ + if (!ib->kms_work_ready) + return; + + cancel_delayed_work_sync(&ib->hpd_work); + hrtimer_cancel(&ib->swap_timer); + cancel_work_sync(&ib->swap_work); + cancel_work_sync(&ib->pd_reset_work); +} + +static void iboot_swap_stop(void *data) +{ + dcp_iboot_kms_stop(data); +} + +int dcp_iboot_kms_init(struct dcp_iboot *ib) +{ + struct drm_device *drm = &ib->drm; + u32 max_w = 3840, max_h = 2160; + int ret; + + ib->pending = -1; + INIT_WORK(&ib->swap_work, iboot_swap_work); + INIT_WORK(&ib->pd_reset_work, iboot_pd_reset_work); + INIT_DELAYED_WORK(&ib->hpd_work, iboot_hpd_work); + hrtimer_setup(&ib->swap_timer, iboot_swap_timer, CLOCK_MONOTONIC, + HRTIMER_MODE_ABS); + ib->kms_work_ready = true; + ret = devm_add_action_or_reset(ib->dcp->dev, iboot_swap_stop, ib); + if (ret) + return ret; + + for (unsigned int i = 0; i < ib->nr_tmodes; i++) { + max_w = max(max_w, le32_to_cpu(ib->tmodes[i].width)); + max_h = max(max_h, le32_to_cpu(ib->tmodes[i].height)); + } + + ret = drmm_mode_config_init(drm); + if (ret) + return ret; + ib->pd_reset_time = jiffies - 61 * HZ; + + drm->mode_config.min_width = 320; + drm->mode_config.min_height = 240; + drm->mode_config.max_width = max_w; + drm->mode_config.max_height = max_h; + drm->mode_config.preferred_depth = 24; + drm->mode_config.funcs = &iboot_mode_config_funcs; + drm->mode_config.helper_private = &iboot_mode_config_helpers; + + ret = drm_universal_plane_init(drm, &ib->plane, 0, &iboot_plane_funcs, + dcp_iboot_formats, ARRAY_SIZE(dcp_iboot_formats), + NULL, DRM_PLANE_TYPE_PRIMARY, NULL); + if (ret) + return ret; + drm_plane_helper_add(&ib->plane, &iboot_plane_helper); + drm_plane_enable_fb_damage_clips(&ib->plane); + + ret = drm_crtc_init_with_planes(drm, &ib->crtc, &ib->plane, NULL, + &iboot_crtc_funcs, NULL); + if (ret) + return ret; + drm_crtc_helper_add(&ib->crtc, &iboot_crtc_helper); + + ret = drm_encoder_init(drm, &ib->encoder, &iboot_encoder_funcs, + DRM_MODE_ENCODER_TMDS, NULL); + if (ret) + return ret; + ib->encoder.possible_crtcs = drm_crtc_mask(&ib->crtc); + + ret = drm_connector_init(drm, &ib->connector, &iboot_connector_funcs, + ib->disp.hdmi ? DRM_MODE_CONNECTOR_HDMIA : + DRM_MODE_CONNECTOR_DisplayPort); + if (ret) + return ret; + drm_connector_helper_add(&ib->connector, &iboot_connector_helper); + if (ib->disp.hpd_gpio || ib->disp.pd) + ib->connector.polled = DRM_CONNECTOR_POLL_CONNECT | + DRM_CONNECTOR_POLL_DISCONNECT; + if (ib->disp.pd) + ib->connector.polled = DRM_CONNECTOR_POLL_HPD; + ret = drm_connector_attach_encoder(&ib->connector, &ib->encoder); + if (ret) + return ret; + + drm_mode_config_reset(drm); + return 0; +} diff --git a/drivers/gpu/drm/apple/iomfb_iboot.c b/drivers/gpu/drm/apple/iomfb_iboot.c new file mode 100644 index 0000000000000..757b122dc8e42 --- /dev/null +++ b/drivers/gpu/drm/apple/iomfb_iboot.c @@ -0,0 +1,840 @@ +// SPDX-License-Identifier: GPL-2.0-only OR MIT +/* + * DCP_FIRMWARE_IBOOT: the T8152 DCPs, driven through their firmware's + * iBoot interface alone. + * + * The bootloader leaves a successful display's RTKit session running, or + * quiesces it after a failed setup, and leaves an unused DCPEXT's firmware + * loaded and waiting for an RTKit wake (apple,rtkit-wake). This adopts or + * wakes that session, stops the AFK endpoints the bootloader left running + * and starts the iBoot (disp0-service or dispext-service), DPAV and DPTX + * port endpoints, reconnects the DPTX port and asserts HPD, then registers + * a DRM device whose flips are iBoot SET_SURFACE calls (iboot_drm.c), in + * the order the bootloader brings the display up. + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include + +#include "afk.h" +#include "dcp.h" +#include "dcp-internal.h" +#include "ibootep.h" +#include "iomfb_iboot.h" +#include "t8152_display.h" + +#define DCP_IBOOT_HPD_TIMEOUT_MS 10000 +/* RTKit's oslog system endpoint */ +#define DCP_IBOOT_RTKIT_EP_OSLOG 8 +/* The coprocessor's CPU control (bit 4: run) and status registers */ +#define DCP_IBOOT_CPU_CONTROL 0x44 +#define DCP_IBOOT_CPU_CONTROL_RUN BIT(4) +#define DCP_IBOOT_CPU_STATUS 0x48 + +static void dcp_iboot_rtk_crashed(void *cookie, const void *crashlog, size_t size) +{ + struct apple_dcp *dcp = cookie; + + WRITE_ONCE(dcp->crashed, true); + dev_err(dcp->dev, "DCP firmware crashed\n"); +} + +static void dcp_iboot_rtk_recv(void *cookie, u8 endpoint, u64 message) +{ + struct apple_dcp *dcp = cookie; + struct apple_dcp_afkep *ep = NULL; + + if (READ_ONCE(dcp->quiescing)) + return; + switch (endpoint) { + case DISP0_ENDPOINT: + ep = READ_ONCE(dcp->ibootep); + break; + case DPTX_CONTROLLER_ENDPOINT: + ep = READ_ONCE(dcp->dptx_controller_ep); + break; + case DPTX_ENDPOINT: + ep = READ_ONCE(dcp->dptxep); + break; + } + if (IS_ERR_OR_NULL(ep)) { + dev_dbg(dcp->dev, "message %#llx for idle endpoint %#x\n", + message, endpoint); + return; + } + afk_receive_message(ep, message); +} + +/* + * Buffers the firmware placed in its own DART mappings, or new ones, as for + * every DCP; and a woken DCPEXT's oslog ring. + */ +static int dcp_iboot_rtk_shmem_setup(void *cookie, u8 endpoint, + struct apple_rtkit_shmem *bfr) +{ + struct apple_dcp *dcp = cookie; + + /* + * A woken DCPEXT offers the oslog ring in its own firmware segment, at + * an address only the firmware's retained DART tables map. Linux never + * reads that ring and only acknowledges its write counters: take it as + * the firmware's, without a CPU mapping. The request needs no reply; + * the bootloader sends none either. + */ + if (bfr->iova && endpoint == DCP_IBOOT_RTKIT_EP_OSLOG) { + struct iommu_domain *domain = iommu_get_domain_for_dev(dcp->dev); + + if (domain && !iommu_iova_to_phys(domain, bfr->iova)) { + bfr->is_mapped = true; + return 0; + } + } + return dcp_rtk_shmem_setup(cookie, bfr); +} + +static void dcp_iboot_rtk_shmem_destroy(void *cookie, struct apple_rtkit_shmem *bfr) +{ + /* The firmware's oslog ring, taken without a mapping */ + if (bfr->is_mapped && !bfr->buffer) + return; + dcp_rtk_shmem_destroy(cookie, bfr); +} + +static bool dcp_iboot_rtk_syslog_debug(void *cookie, const char *context, + const char *message) +{ + /* Surface logging through fbcon would itself submit another surface. */ + return strstr(message, "setSurfaceRequest") || + strstr(message, "swap_submit_dcp") || + strstr(message, "validateRequest: inSize"); +} + +static const struct apple_rtkit_ops dcp_iboot_rtkit_ops = { + .crashed = dcp_iboot_rtk_crashed, + .recv_message = dcp_iboot_rtk_recv, + .shmem_setup_ep = dcp_iboot_rtk_shmem_setup, + .shmem_destroy = dcp_iboot_rtk_shmem_destroy, + .syslog_is_debug = dcp_iboot_rtk_syslog_debug, +}; + +static bool dcp_iboot_handoff_quiesced(struct dcp_iboot *ib) +{ + struct device_node *chosen __free(device_node) = of_find_node_by_path("/chosen"); + const char *version; + u32 running; + + if (ib->disp.hdmi || !chosen) + return false; + if (!of_property_read_u32(chosen, "apple,dcp-rtkit-running", &running)) + return running == 0; + /* + * This older J873 loader predates the state property. Its failed + * display setup quiesces DCP and keeps iBoot's placeholder framebuffer; + * a successful setup hands over ON. Restrict this fallback to the + * verified loader build, rather than guessing for other handoffs. + */ + if (of_property_read_string(chosen, "asahi,m1n1-stage2-version", &version) || + strcmp(version, "v0.1.0-259-ga30a589a")) + return false; + for_each_child_of_node_scoped(chosen, np) { + u32 width, height; + + if (of_device_is_compatible(np, "simple-framebuffer") && + !of_property_read_u32(np, "width", &width) && + !of_property_read_u32(np, "height", &height)) + return width == 640 && height == 1136; + } + return false; +} + +/* The unused DCPEXT firmware is loaded and powered by iBoot. */ +static int dcp_iboot_check_wake(struct apple_dcp *dcp) +{ + struct platform_device *pdev = to_platform_device(dcp->dev); + struct resource *res; + void __iomem *psd, *coproc; + + res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "coproc-psd"); + if (!res) + return -EINVAL; + psd = devm_ioremap(dcp->dev, res->start, resource_size(res)); + if (!psd) + return -ENOMEM; + /* A read from the coprocessor while gated hangs the machine. */ + if (readl(psd + 0x80) & 7) + return -ENODEV; + res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "coproc"); + if (!res) + return -EINVAL; + coproc = devm_ioremap(dcp->dev, res->start, resource_size(res)); + if (!coproc) + return -ENOMEM; + dev_info(dcp->dev, "firmware handoff: CPU control %#x status %#x\n", + readl(coproc + DCP_IBOOT_CPU_CONTROL), + readl(coproc + DCP_IBOOT_CPU_STATUS)); + return (readl(coproc + DCP_IBOOT_CPU_CONTROL) & + DCP_IBOOT_CPU_CONTROL_RUN) ? 0 : -ENODEV; +} + +/* + * Wake only a quiesced session: IOP INIT on a running DCP can panic it. A + * running one is taken as it runs, without the HELLO/EPMAP handshake; + * its AFK endpoints are then registered as they start (hw.afk_adopt_endpoints). + */ +static int dcp_iboot_boot(struct dcp_iboot *ib) +{ + struct apple_dcp *dcp = ib->dcp; + bool wake = of_property_read_bool(dcp->dev->of_node, "apple,rtkit-wake"); + struct apple_rtkit *rtk; + int ret; + + if ((!ib->disp.hdmi && wake) || dcp_iboot_handoff_quiesced(ib)) { + if (wake) { + ret = dcp_iboot_check_wake(dcp); + if (ret) + return ret; + } + rtk = apple_rtkit_init(dcp->dev, dcp, NULL, 0, &dcp_iboot_rtkit_ops); + if (IS_ERR(rtk)) + return PTR_ERR(rtk); + dcp->rtk = rtk; + ret = apple_rtkit_wake(rtk); + dev_info(dcp->dev, "quiesced DCP wake: %d\n", ret); + return ret; + } + rtk = apple_rtkit_init_adopted(dcp->dev, dcp, NULL, 0, &dcp_iboot_rtkit_ops); + if (IS_ERR(rtk)) + return PTR_ERR(rtk); + dcp->rtk = rtk; + return apple_rtkit_adopt_running(rtk); +} + +/* + * The display pipe scans out through dart-disp0, the DCP fetches through + * dart-dcp. Both are locked with their own live root, so each needs its own + * device and domain: one domain spanning both would publish dart-dcp's root + * into dart-disp0 and drop the scanout mappings the bootloader left there + * (the pipe then faults, IOMFB axi_rd_err). Scanout buffers are allocated on + * the DCP device and mapped into the scanout domain at the same address. + */ +static int dcp_iboot_scanout_init(struct dcp_iboot *ib) +{ + struct device *dev = ib->dcp->dev; + struct device_node *np __free(device_node) = + of_get_child_by_name(dev->of_node, "scanout"); + int ret; + + if (!np) + return -ENODEV; + ib->scanout = of_platform_device_create(np, NULL, dev); + if (!ib->scanout) + ib->scanout = of_find_device_by_node(np); + if (!ib->scanout) + return -ENODEV; + ret = dma_set_mask_and_coherent(&ib->scanout->dev, DMA_BIT_MASK(64)); + if (ret) + return ret; + ret = of_dma_configure(&ib->scanout->dev, np, true); + if (ret) + return ret; + ib->scanout_dom = iommu_get_domain_for_dev(&ib->scanout->dev); + return ib->scanout_dom ? 0 : -ENODEV; +} + +/* Firmware-visible memory: never freed, the firmware may still use it. */ +static int dcp_iboot_dma_alloc(struct dcp_iboot *ib, struct dcp_iboot_dma *dma, + size_t size) +{ + dma->size = PAGE_ALIGN(size); + dma->cpu = dma_alloc_coherent(ib->dcp->dev, dma->size, &dma->iova, + GFP_KERNEL); + return dma->cpu ? 0 : -ENOMEM; +} + +static void *dcp_iboot_vmap_wc(struct sg_table *sgt) +{ + unsigned int n = 0, count = 0; + struct sg_page_iter iter; + struct page **pages; + void *vaddr; + + for_each_sgtable_page(sgt, &iter, 0) + count++; + pages = kvmalloc_array(count, sizeof(*pages), GFP_KERNEL); + if (!pages) + return NULL; + for_each_sgtable_page(sgt, &iter, 0) + pages[n++] = sg_page_iter_page(&iter); + vaddr = vmap(pages, count, VM_MAP, pgprot_writecombine(PAGE_KERNEL)); + kvfree(pages); + return vaddr; +} + +/* + * The display pipe's DART is locked and comes with the firmware's own + * mappings. The address the coprocessor's allocator handed out for a scanout + * buffer can be one of them, and mapping over it corrupts what the pipe + * fetches every frame (the picture flickers). Start at @dva and go down until + * a range with no mapping is found. + */ +static dma_addr_t dcp_iboot_scan_free_dva(struct dcp_iboot *ib, dma_addr_t dva, + size_t size) +{ + const size_t step = SZ_16K; + + size = ALIGN(size, step); + for (int tries = 0; tries < 64 && dva >= size; tries++) { + size_t off; + + for (off = 0; off < size; off += step) + if (iommu_iova_to_phys(ib->scanout_dom, dva + off)) + break; + if (off == size) + return dva; + dev_info(ib->dcp->dev, "scanout DART: %pad+%#zx is in use, moving down\n", + &dva, off); + dva -= size; + } + return 0; +} + +/* One scanout buffer: allocated, cleared, mapped for the CPU and the pipe. */ +static int dcp_iboot_scan_alloc(struct dcp_iboot *ib, int i, size_t size) +{ + struct dcp_iboot_dma *fb = &ib->scan[i]; + struct sg_table sgt; + ssize_t mapped; + dma_addr_t dva; + int ret; + + ret = dcp_iboot_dma_alloc(ib, fb, size); + if (ret) + return ret; + ret = dma_get_sgtable(ib->dcp->dev, &sgt, fb->cpu, fb->iova, fb->size); + if (ret) + return ret; + /* + * The DCP is coherent, the display pipe is not: it reads memory past + * the CPU caches, and frames written through the cacheable mapping + * show only once evicted. Write through a write-combined mapping of + * the same pages, with the zeroing of the allocation cleaned out first. + */ + arch_wb_cache_pmem(fb->cpu, fb->size); + ib->scan_wc[i] = dcp_iboot_vmap_wc(&sgt); + if (!ib->scan_wc[i]) { + sg_free_table(&sgt); + return -ENOMEM; + } + memset(ib->scan_wc[i], 0, fb->size); + /* + * Cacheable, as the bootloader maps scanout buffers: the pipe faults on + * entries with the DART's no-cache bit (IOMFB axi_rd_err). + */ + dva = dcp_iboot_scan_free_dva(ib, fb->iova, fb->size); + if (!dva) { + sg_free_table(&sgt); + return -ENOSPC; + } + mapped = iommu_map_sgtable(ib->scanout_dom, dva, &sgt, + IOMMU_READ | IOMMU_WRITE | IOMMU_CACHE); + sg_free_table(&sgt); + if (mapped < (ssize_t)fb->size) + return mapped < 0 ? mapped : -ENOMEM; + ib->scan_dva[i] = dva; + dev_info(ib->dcp->dev, "scanout buffer %d: %zu bytes at %pad\n", i, + fb->size, &dva); + return 0; +} + +/* + * A fresh iBoot session has no surface. Whenever the pipe runs without one + * (the link retrains when the DPTX services restart, SET_MODE turns it on), + * it fetches from a null base and the firmware takes an NMI (IOMFB + * axi_rd_err). Hand it a black buffer of the boot mode at once, sized from + * the bootloader's simple-framebuffer. + */ +static int dcp_iboot_boot_surface(struct dcp_iboot *ib) +{ + /* + * The boot display's framebuffer is /chosen's; the bootloader adds the + * second display's (HDMI) at the root. + */ + struct device_node *chosen __free(device_node) = + of_find_node_by_path("/chosen"); + struct device_node *parent __free(device_node) = + ib->disp.dp_source ? + of_find_node_by_path("/") : of_node_get(chosen); + u32 width = 1280, height = 720, stride = 1280 * 4; + size_t size; + int ret; + + for_each_child_of_node_scoped(parent, np) { + struct resource res; + + if (!of_device_is_compatible(np, "simple-framebuffer")) + continue; + of_property_read_u32(np, "width", &width); + of_property_read_u32(np, "height", &height); + of_property_read_u32(np, "stride", &stride); + /* + * Take the display over from simpledrm on the bootloader's + * framebuffer, so it does not shadow this driver's output + * (fbcon on a framebuffer nothing scans out). + */ + if (!of_address_to_resource(np, 0, &res)) { + ret = aperture_remove_conflicting_devices(res.start, + resource_size(&res), + dcp_iboot_driver_name()); + if (ret) + dev_warn(ib->dcp->dev, "simpledrm stays: %d\n", ret); + } + break; + } + /* + * Large enough for the largest mode: while a SET_MODE switches, the + * pipe briefly scans the current surface with the new mode's size. + * A monitor may first appear after probe, with a larger mode list. + */ + size = max_t(size_t, (size_t)stride * height, 3840 * 2160 * 4); + for (unsigned int i = 0; i < ib->nr_tmodes; i++) + size = max_t(size_t, size, (size_t)le32_to_cpu(ib->tmodes[i].width) * + le32_to_cpu(ib->tmodes[i].height) * 4); + ib->boot_w = width; + ib->boot_h = height; + ib->boot_stride = stride; + for (int i = 0; i < ARRAY_SIZE(ib->scan); i++) { + ret = dcp_iboot_scan_alloc(ib, i, size); + if (ret) + return ret; + } + ib->front = 0; + ret = ibootep_set_surface(ib->dcp, ib->scan_dva[0], width, height, stride); + dev_info(ib->dcp->dev, "boot surface %ux%u at %pad: %d\n", width, height, + &ib->scan_dva[0], ret); + return ret; +} + +/* Both mode lists of the current sink; a list is kept when its query fails. */ +int dcp_iboot_get_modes(struct dcp_iboot *ib) +{ + struct iboot_timing_mode *tmodes; + struct iboot_color_mode *cmodes; + unsigned int count; + int ret; + + ret = ibootep_get_timing_modes(ib->dcp, &tmodes, &count); + if (ret) + return ret; + kfree(ib->tmodes); + ib->tmodes = tmodes; + ib->nr_tmodes = count; + ret = ibootep_get_color_modes(ib->dcp, &cmodes, &count); + if (ret) + return ret; + kfree(ib->cmodes); + ib->cmodes = cmodes; + ib->nr_cmodes = count; + return 0; +} + +/* + * Wait until the firmware reports a sink with modes, as the bootloader does. + * Nothing on the iBoot interface announces the sink, so GET_HPD is asked + * every 100 ms; the J873g ports took 268 to 328 ms after power on. + */ +int dcp_iboot_wait_hpd(struct dcp_iboot *ib) +{ + unsigned long timeout = jiffies + msecs_to_jiffies(DCP_IBOOT_HPD_TIMEOUT_MS); + u32 timings = 0, colors = 0; + int hpd; + + do { + hpd = ibootep_get_hpd(ib->dcp, &timings, &colors); + if (hpd > 0 && timings && colors) { + dev_info(ib->dcp->dev, "sink: %u timing, %u color modes\n", + timings, colors); + return 0; + } + msleep(100); + } while (time_before(jiffies, timeout)); + + dev_err(ib->dcp->dev, "no sink (hpd %d, %u timing, %u color modes)\n", hpd, + timings, colors); + return hpd < 0 ? hpd : -ENODEV; +} + +static void dcp_iboot_av_work(struct work_struct *work) +{ + struct dcp_iboot *ib = container_of(to_delayed_work(work), struct dcp_iboot, + av_work); + + if (READ_ONCE(ib->ready)) + dcp_iboot_reset_mode(ib); +} + +/* + * The DPAV controller endpoint's services take no host commands, but the + * firmware expects all of them opened: its video interface powers the + * display on and starts its idle power off timer, so SET_MODE follows once + * that one is up. + */ +static void dcp_iboot_dpav_init(struct apple_epic_service *service, + const char *name, const char *class, s64 unit) +{ + struct dcp_iboot *ib = READ_ONCE(service->ep->dcp->iboot); + + if (ib && name && strstr(name, "dcpav-video-interface")) + dcp_iboot_av_started(ib); +} + +static const struct apple_epic_service_ops dcp_iboot_dpav_ops = { + .name = "dcpav", + .reusable = true, + .init = dcp_iboot_dpav_init, +}; + +static unsigned int dcp_iboot_ep_services(struct apple_dcp_afkep *ep) +{ + unsigned int count = 0; + + for (unsigned int i = 0; i < AFK_MAX_CHANNEL; i++) + count += READ_ONCE(ep->services[i].enabled); + return count; +} + +/* + * The endpoint's services as it announces them, up to @expected within the + * AFK timeout; none is an error, fewer a warning. + */ +static int dcp_iboot_wait_services(struct apple_dcp *dcp, + struct apple_dcp_afkep *ep, unsigned int expected) +{ + unsigned int count; + + wait_event_timeout(ep->service_wq, dcp_iboot_ep_services(ep) >= expected, + msecs_to_jiffies(dcp->hw.afk_timeout_ms)); + count = dcp_iboot_ep_services(ep); + if (!count) { + dev_err(dcp->dev, "endpoint %#x announced no service\n", ep->endpoint); + return -ETIMEDOUT; + } + if (count < expected) + dev_warn(dcp->dev, "endpoint %#x: %u of %u services\n", ep->endpoint, + count, expected); + return 0; +} + +static int dcp_iboot_start_ep(struct apple_dcp *dcp, u32 endpoint, + struct apple_dcp_afkep **slot, + const struct apple_epic_service_ops *ops, + unsigned int expected) +{ + struct apple_dcp_afkep *ep = afk_init(dcp, endpoint, NULL); + int ret; + + if (IS_ERR(ep)) + return PTR_ERR(ep); + ep->unmatched_ops = ops; + WRITE_ONCE(*slot, ep); + ret = afk_start(ep); + if (ret) { + dev_err(dcp->dev, "endpoint %#x did not start: %d\n", endpoint, ret); + return ret; + } + return dcp_iboot_wait_services(dcp, ep, expected); +} + +/* The iBoot service is ready once its init callback has run. */ +static int dcp_iboot_start_iboot(struct apple_dcp *dcp) +{ + int ret; + + ret = ibootep_init(dcp); + if (ret) + return ret; + wait_event_timeout(dcp->ibootep->service_wq, ibootep_is_ready(dcp), + msecs_to_jiffies(dcp->hw.afk_timeout_ms)); + return ibootep_is_ready(dcp) ? 0 : -ENODEV; +} + +/* + * Once adopted, the DCP may still DMA into our buffers. Stop the host side: + * no more endpoint work or messages, no KMS work, and RTKit detached with + * its buffers retained. Firmware-visible storage stays allocated until + * reboot. + */ +static void dcp_iboot_detach(struct dcp_iboot *ib) +{ + struct apple_dcp *dcp = ib->dcp; + + WRITE_ONCE(ib->ready, false); + WRITE_ONCE(dcp->quiescing, true); + cancel_delayed_work_sync(&ib->av_work); + dcp_iboot_kms_stop(ib); + afk_quiesce(dcp->ibootep); + afk_quiesce(dcp->dptx_controller_ep); + afk_quiesce(dcp->dptxep); + if (!IS_ERR_OR_NULL(dcp->rtk)) + apple_rtkit_free_retaining_buffers(dcp->rtk); + WRITE_ONCE(dcp->rtk, NULL); +} + +static int dcp_iboot_start(struct dcp_iboot *ib) +{ + struct t8152_display *disp = &ib->disp; + struct apple_dcp *dcp = ib->dcp; + struct device *dev = dcp->dev; + bool sink = true; + int ret; + + ret = dcp_iboot_start_iboot(dcp); + if (ret) + return dev_err_probe(dev, ret, "no disp0-service\n"); + /* + * The new iBoot session has dropped the surface while the pipe still + * runs: it fetches from a null base (IOMFB axi_rd_err, firmware NMI) + * unless it is powered off at once. It comes back on below, after a + * surface is set. + */ + ret = ibootep_set_power(dcp, false); + if (ret) + return dev_err_probe(dev, ret, "display power off failed\n"); + ret = dcp_iboot_start_ep(dcp, DPTX_CONTROLLER_ENDPOINT, + &dcp->dptx_controller_ep, &dcp_iboot_dpav_ops, 4); + if (ret) + return dev_err_probe(dev, ret, "no DPAV controllers\n"); + ret = dcp_iboot_start_ep(dcp, DPTX_ENDPOINT, &dcp->dptxep, + &t8152_dptx_port_ops, 2); + if (ret) + return dev_err_probe(dev, ret, "no DPTX port service\n"); + if (disp->pd) { + ret = t8152_dptx_disconnect(disp, 0); + if (ret) + return dev_err_probe(dev, ret, "cannot release inherited DP link\n"); + } + + /* + * USB-C negotiation can outlive probe or fail while a monitor sleeps. + * Register the connector first; the hotplug path owns all USB-C links. + */ + if (disp->pd) + sink = false; + if (sink) { + ret = t8152_dptx_connect(disp, 0); + if (ret) + return dev_err_probe(dev, ret, "DPTX connect failed\n"); + ret = t8152_dptx_set_hpd(disp, 0, true); + if (ret) + return dev_err_probe(dev, ret, "DPTX hotplug failed\n"); + ret = dcp_iboot_wait_hpd(ib); + if (ret) + return ret; + ret = dcp_iboot_get_modes(ib); + if (ret) + return dev_err_probe(dev, ret, "no modes\n"); + } + ret = dcp_iboot_boot_surface(ib); + if (ret) + return dev_err_probe(dev, ret, "no boot surface\n"); + /* Keep an idle connector registered while the PD controller negotiates. */ + if (!sink) { + dev_info(dev, "waiting for a USB-C display\n"); + goto register_kms; + } + disp->hpd = 1; + /* + * Without the bootloader's link (no display on the port at boot) the + * PHY starts cold, the right way round for the plug. + */ + if (disp->phy->detect && disp->phy->set_orientation) { + bool flipped; + + if (disp->phy->detect(disp, &flipped) > 0) { + if (disp->phy->select) + disp->phy->select(disp); + disp->phy->set_orientation(disp, flipped); + } + } + ret = disp->phy->activate(disp); + if (ret) + return dev_err_probe(dev, ret, "PHY activation failed\n"); + /* While the display is still off and the firmware does no AUX. */ + dcp_iboot_update_edid(ib); + ret = ibootep_set_power(dcp, true); + if (ret) + return dev_err_probe(dev, ret, "set_power failed\n"); + ib->powered = true; + dev_info(dev, "%u timing modes, %u color modes\n", ib->nr_tmodes, + ib->nr_cmodes); + +register_kms: + ret = dcp_iboot_kms_init(ib); + if (ret) + return ret; + if (sink) { + ret = dcp_iboot_set_boot_mode(ib); + if (ret) + dev_warn(dev, "boot mode not set again: %d\n", ret); + } + ret = drm_dev_register(&ib->drm, 0); + if (ret) + return ret; + WRITE_ONCE(ib->ready, true); + if (disp->hpd_gpio || disp->pd) + drmm_kms_helper_poll_init(&ib->drm); + if (disp->pd) + queue_delayed_work(system_freezable_wq, &ib->hpd_work, + msecs_to_jiffies(250)); + drm_client_setup(&ib->drm, NULL); + return 0; +} + +/* + * Without a USB-C display the bootloader drives HDMI from the main DCP + * (source 0) and never starts DCPEXT0. hdmi_np: DCPEXT0's node, which + * describes the HDMI PHY. + */ +int dcp_iboot_probe(struct apple_dcp *dcp) +{ + struct device *dev = dcp->dev; + struct device_node *hdmi_np __free(device_node) = t8152_hdmi_node(); + int source = hdmi_np ? t8152_hdmi_source(hdmi_np) : -ENODEV; + struct t8152_display *disp; + struct dcp_iboot *ib; + u32 live; + int ret; + + if (dev->of_node == hdmi_np && source != 1 && + !of_property_present(dev->of_node, "apple,usb-c-ports")) { + dev_info(dev, "%s\n", source == 0 ? "HDMI is on the main DCP" : + "no bootloader HDMI link to adopt"); + return -ENODEV; + } + + /* + * The bootloader's AFK endpoint hand-off: without it there is no + * telling whether an endpoint is live, and an INIT on a live one + * asserts in the firmware. + */ + ret = of_property_read_u32(dev->of_node, "apple,afk-active-endpoints", &live); + if (ret || live & ~GENMASK(15, 0)) + return dev_err_probe(dev, -EINVAL, + "bootloader AFK endpoint handoff required\n"); + + ret = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(64)); + if (ret) + return ret; + + ib = dcp_iboot_alloc(dev); + if (IS_ERR(ib)) + return PTR_ERR(ib); + ib->dcp = dcp; + disp = &ib->disp; + disp->dcp = dcp; + disp->dev = dev; + disp->dp_source = dev->of_node == hdmi_np ? 1 : 0; + of_property_read_u32(dev->of_node, "apple,dp-source", &disp->dp_source); + if (disp->dp_source > 2) + return -EINVAL; + of_property_read_u32(dev->of_node, "apple,die", &disp->die); + dcp->afk_live_endpoints = live; + mutex_init(&ib->lock); + INIT_DELAYED_WORK(&ib->av_work, dcp_iboot_av_work); + + ret = dcp_iboot_scanout_init(ib); + if (ret) + return dev_err_probe(dev, ret, "no scanout DART stream\n"); + + disp->hdmi = source >= 0 && source == disp->dp_source; + if (disp->hdmi && dev->of_node != hdmi_np) + dev_info(dev, "HDMI from the main DCP (no USB-C display at boot)\n"); + /* Read only: the pin belongs to a controller the AOP runs. */ + disp->hpd_gpio = NULL; + if (disp->hdmi) + disp->hpd_gpio = devm_fwnode_gpiod_get(dev, of_fwnode_handle(hdmi_np), + "hpd", GPIOD_ASIS, "hpd"); + if (PTR_ERR(disp->hpd_gpio) == -ENOENT) + disp->hpd_gpio = NULL; + if (IS_ERR(disp->hpd_gpio)) + return dev_err_probe(dev, PTR_ERR(disp->hpd_gpio), "no HPD GPIO\n"); + disp->hpd = 0; + ret = disp->hdmi ? t8152_hdmi_init(disp, hdmi_np) : t8152_atc_init(disp); + if (ret) + return dev_err_probe(dev, ret, "no DisplayPort PHY\n"); + /* Nobody has HDMI from the bootloader: it is one of the shared ports. */ + if (!disp->hdmi && source < 0 && hdmi_np) { + ret = t8152_hdmi_port_init(disp, hdmi_np); + if (ret) + return dev_err_probe(dev, ret, "no shared HDMI port\n"); + } + + WRITE_ONCE(dcp->t8152, disp); + WRITE_ONCE(dcp->iboot, ib); + ret = dcp_iboot_boot(ib); + if (IS_ERR_OR_NULL(dcp->rtk)) { + dcp->rtk = NULL; + return dev_err_probe(dev, ret, "cannot adopt the DCP\n"); + } + /* + * From here on the DCP may DMA into what this driver gives it, and + * there is no safe way to stop it: retain the session until reboot. + */ + if (dcp_retain_live_session(dcp)) { + dev_warn(dev, "live session not recorded; its DMA stays allocated\n"); + WRITE_ONCE(dcp->retain_dma, true); + } + if (ret) + dev_err(dev, "cannot adopt the DCP: %d\n", ret); + else + ret = dcp_iboot_start(ib); + if (!ret) + return 0; + + /* + * Gate RX before draining endpoint work; keep RTKit available until + * every sender has stopped. Do not reclaim firmware-visible storage + * or let the driver core retry a partially started session. + */ + dcp_iboot_detach(ib); + dev_err(dev, "initialization failed (%d); live DMA resources retained until reboot\n", + ret); + return 0; +} + +/* An unbind: the DRM device goes, the firmware session and its DMA stay. */ +void dcp_iboot_remove(struct apple_dcp *dcp) +{ + struct dcp_iboot *ib = dcp->iboot; + bool registered; + + if (!ib) + return; + registered = READ_ONCE(ib->ready); + if (ib->drm.mode_config.poll_enabled) + drm_kms_helper_poll_disable(&ib->drm); + if (!READ_ONCE(dcp->quiescing)) + dcp_iboot_detach(ib); + if (registered) { + drm_dev_unplug(&ib->drm); + /* Drops the committed state's connector reference. */ + drm_atomic_helper_shutdown(&ib->drm); + } +} diff --git a/drivers/gpu/drm/apple/iomfb_iboot.h b/drivers/gpu/drm/apple/iomfb_iboot.h new file mode 100644 index 0000000000000..9c2268604b017 --- /dev/null +++ b/drivers/gpu/drm/apple/iomfb_iboot.h @@ -0,0 +1,139 @@ +/* SPDX-License-Identifier: GPL-2.0-only OR MIT */ +/* + * DCP_FIRMWARE_IBOOT: a T8152 DCP driven through its firmware's iBoot + * interface alone, with a DRM device of its own (iomfb_iboot.c, iboot_drm.c). + */ +#ifndef __APPLE_IOMFB_IBOOT_H__ +#define __APPLE_IOMFB_IBOOT_H__ + +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include + +#include "ibootep.h" +#include "t8152_display.h" + +struct apple_dcp; +struct device; +struct drm_edid; +struct iommu_domain; +struct platform_device; + +struct dcp_iboot_dma { + void *cpu; + dma_addr_t iova; + size_t size; +}; + +/* + * Scanout buffers, each for the largest mode: a frame is copied into one not + * on screen, which SET_SURFACE then shows. Copying into the buffer being + * scanned out tore every frame. Three, so the one written next left the + * screen a frame ago. + */ +#define DCP_IBOOT_SCANOUT_BUFS 3 + +struct dcp_iboot { + struct drm_device drm; + struct apple_dcp *dcp; + struct t8152_display disp; + + /* The DRM device is registered and the session up. */ + bool ready; + bool kms_work_ready; + + /* The scanout DART's own device and domain (dcp_iboot_scanout_init()). */ + struct platform_device *scanout; + struct iommu_domain *scanout_dom; + struct dcp_iboot_dma scan[DCP_IBOOT_SCANOUT_BUFS]; + /* their pages write-combined: the pipe does not snoop the CPU caches */ + void *scan_wc[DCP_IBOOT_SCANOUT_BUFS]; + /* their addresses in the display pipe's DART */ + dma_addr_t scan_dva[DCP_IBOOT_SCANOUT_BUFS]; + /* scan[front] is on screen (or set as the surface at the next power on) */ + unsigned int front; + /* the part of the current picture scan[i] does not hold yet */ + struct drm_rect stale[DCP_IBOOT_SCANOUT_BUFS]; + /* when scan[i] stopped being the requested surface */ + ktime_t retired[DCP_IBOOT_SCANOUT_BUFS]; + /* the last SET_SURFACE of a new buffer */ + ktime_t last_swap; + /* + * A frame written within one refresh of the last swap waits in + * scan[pending] (-1: none) for swap_timer, at most one swap per + * refresh; a newer frame overwrites it. + */ + int pending; + u32 pending_w, pending_h, pending_pitch; + struct hrtimer swap_timer; + struct work_struct swap_work; + u32 boot_w, boot_h, boot_stride; + + /* the sink's EDID, read through the PHY (NULL if unavailable) */ + const struct drm_edid *edid; + + /* iBoot state below, under lock */ + struct mutex lock; + bool enabled; + bool powered; + struct iboot_timing_mode *tmodes; + unsigned int nr_tmodes; + struct iboot_color_mode *cmodes; + unsigned int nr_cmodes; + struct iboot_timing_mode cur_mode; + bool mode_set; + /* SET_MODE of cur_mode failed; the next plug sets it again */ + bool mode_failed; + /* USB-C: re-enter DP alt mode when the sink stopped answering */ + struct work_struct pd_reset_work; + unsigned long pd_reset_time; + /* + * The DPAV video interface started: SET_MODE again, or the + * firmware's idle power off timer takes the link down 30 s later. + */ + struct delayed_work av_work; + struct delayed_work hpd_work; + + struct drm_plane plane; + struct drm_crtc crtc; + struct drm_encoder encoder; + struct drm_connector connector; +}; + +static inline struct dcp_iboot *to_dcp_iboot(struct drm_device *drm) +{ + return container_of(drm, struct dcp_iboot, drm); +} + +#if IS_ENABLED(CONFIG_DRM_APPLE_DCP_T8152) +/* iomfb_iboot.c */ +int dcp_iboot_probe(struct apple_dcp *dcp); +void dcp_iboot_remove(struct apple_dcp *dcp); +int dcp_iboot_wait_hpd(struct dcp_iboot *ib); +int dcp_iboot_get_modes(struct dcp_iboot *ib); + +/* iboot_drm.c */ +struct dcp_iboot *dcp_iboot_alloc(struct device *dev); +const char *dcp_iboot_driver_name(void); +int dcp_iboot_kms_init(struct dcp_iboot *ib); +void dcp_iboot_kms_stop(struct dcp_iboot *ib); +int dcp_iboot_set_boot_mode(struct dcp_iboot *ib); +void dcp_iboot_update_edid(struct dcp_iboot *ib); +void dcp_iboot_reset_mode(struct dcp_iboot *ib); +void dcp_iboot_av_started(struct dcp_iboot *ib); +#else +static inline int dcp_iboot_probe(struct apple_dcp *dcp) { return -ENODEV; } +static inline void dcp_iboot_remove(struct apple_dcp *dcp) { } +#endif + +#endif From 4c54a002da2150db07f783b4d88fc12365f75ddb Mon Sep 17 00:00:00 2001 From: Thomas Glanzmann Date: Fri, 13 Nov 2026 10:07:11 +0100 Subject: [PATCH 23/29] arm64: dts: apple: t8152: describe the display DARTs as Gen3 The DARTs of the main DCP and of DCPEXT0, and those of their display pipes (ADT dart-dcp, dart-disp0, dart-dcpext0 and dart-dispext0), belong to the SPTM, and the bootloader hands them over locked. Their TLB is invalidated through the SoC's TLB invalidation aperture (dart-tlbi-aperture), not through the T8110 TLB command register. Describe them as apple,t8152-dart-gen3 with their windows in the aperture (ADT tlbi-aperture-offset 0x400 and 0x480). The second register window of the display pipe DARTs, their SMMU instance, is not used and is dropped. The other display DARTs keep their description until something uses them. Signed-off-by: Thomas Glanzmann --- .../boot/dts/apple/t8152-peripherals.dtsi | 22 ++++++++++++------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/arch/arm64/boot/dts/apple/t8152-peripherals.dtsi b/arch/arm64/boot/dts/apple/t8152-peripherals.dtsi index 578e7730ef5f1..cab3001ab9086 100644 --- a/arch/arm64/boot/dts/apple/t8152-peripherals.dtsi +++ b/arch/arm64/boot/dts/apple/t8152-peripherals.dtsi @@ -123,17 +123,20 @@ }; dcp_dart: iommu@286340000 { - compatible = "apple,t8152-dart", "apple,t8110-dart"; - reg = <0x2 0x86340000 0x0 0x20000>; + compatible = "apple,t8152-dart-gen3"; + reg = <0x2 0x86340000 0x0 0x20000>, + <0x2 0x00000400 0x0 0x10>; + reg-names = "dart", "tlbi"; interrupts = ; #iommu-cells = <1>; status = "disabled"; }; disp0_dart: iommu@286300000 { - compatible = "apple,t8152-dart", "apple,t8110-dart"; + compatible = "apple,t8152-dart-gen3"; reg = <0x2 0x86300000 0x0 0xc000>, - <0x2 0x86314000 0x0 0x4000>; + <0x2 0x00000400 0x0 0x10>; + reg-names = "dart", "tlbi"; interrupts = ; #iommu-cells = <1>; status = "disabled"; @@ -148,17 +151,20 @@ }; dcpext0_dart: iommu@32a340000 { - compatible = "apple,t8152-dart", "apple,t8110-dart"; - reg = <0x3 0x2a340000 0x0 0x20000>; + compatible = "apple,t8152-dart-gen3"; + reg = <0x3 0x2a340000 0x0 0x20000>, + <0x2 0x00000480 0x0 0x10>; + reg-names = "dart", "tlbi"; interrupts = ; #iommu-cells = <1>; status = "disabled"; }; dispext0_dart: iommu@32a300000 { - compatible = "apple,t8152-dart", "apple,t8110-dart"; + compatible = "apple,t8152-dart-gen3"; reg = <0x3 0x2a300000 0x0 0xc000>, - <0x3 0x2a314000 0x0 0x4000>; + <0x2 0x00000480 0x0 0x10>; + reg-names = "dart", "tlbi"; interrupts = ; #iommu-cells = <1>; status = "disabled"; From 6dce5ece61dfefe5353d229377cf79cdee60ab1e Mon Sep 17 00:00:00 2001 From: Thomas Glanzmann Date: Fri, 13 Nov 2026 10:08:50 +0100 Subject: [PATCH 24/29] arm64: dts: apple: j873: add the USB-C and HDMI displays The bootloader trains the J873g displays through the DCP firmware's iBoot interface and hands both DCPs over with their RTKit sessions running: the main DCP drives a USB-C display on one of the rear ports, or HDMI when no USB-C display is attached at boot, and DCPEXT0 drives the HDMI port next to a USB-C display. Add both DCPs with their post-office mailboxes and enable their Gen3 DARTs, with the DMA window in the upper half of the firmware's 64 GiB window, above the firmware's and the bootloader's mappings. The main DCP lists the three rear USB-C ports with their ATC PHY, PMGR power states, tunables and PD controllers on nub-spmi-a0. DCPEXT0 has the HDMI PHY, the read-only PMGR power states of the PHY and of its CPU, and the HPD line of the DP-to-HDMI converter on the AOP's GPIO controller, which is polled since its interrupts belong to the AOP. Each DCP has the AP DisplayPort AUX controller that reads the sink's EDID. Resources are from the 26A434 ADT. Signed-off-by: Thomas Glanzmann --- arch/arm64/boot/dts/apple/t8152-j873-dcp.dtsi | 471 ++++++++++++++++++ arch/arm64/boot/dts/apple/t8152-j873g.dts | 1 + 2 files changed, 472 insertions(+) create mode 100644 arch/arm64/boot/dts/apple/t8152-j873-dcp.dtsi diff --git a/arch/arm64/boot/dts/apple/t8152-j873-dcp.dtsi b/arch/arm64/boot/dts/apple/t8152-j873-dcp.dtsi new file mode 100644 index 0000000000000..e41c4008c425f --- /dev/null +++ b/arch/arm64/boot/dts/apple/t8152-j873-dcp.dtsi @@ -0,0 +1,471 @@ +// SPDX-License-Identifier: GPL-2.0+ OR MIT +/* + * J873g displays: the rear USB-C ports and the HDMI port, driven through + * the DCP firmware's iBoot interface (apple,t8152-dcp-iboot). The + * bootloader trains the displays and hands both DCPs over with their RTKit + * sessions running. + * + * Resources are from the 26A434 ADT: dcp (iop,ascwrap-v8, post-office + * mailbox 5 with alias: 0x316020000 + 2 * 5 * 0x80), dart-dcp (SID 23), + * dart-disp0 (scanout SID 0), and the atc-phyN PHY windows. The Gen3 + * display DARTs stay locked by iBoot, each with its own live root, so the + * scanout stream is a child device with its own domain. Linux tables are + * published into vacant root slots, so the DMA window is the upper half of + * the 64 GiB vm-base window, above the firmware's and the bootloader's + * mappings. + */ +#include +#include + +&dcp_dart { + apple,dma-range = <0x108 0x0 0x8 0x0>; + status = "okay"; +}; + +&disp0_dart { + apple,dma-range = <0x108 0x0 0x8 0x0>; + status = "okay"; +}; + +&dcpext0_dart { + apple,dma-range = <0x108 0x0 0x8 0x0>; + status = "okay"; +}; + +&dispext0_dart { + apple,dma-range = <0x108 0x0 0x8 0x0>; + status = "okay"; +}; + +/ { + soc { + /* + * ADT /arm-io/nub-spmi-a0 (gen 4): the USB-C PD controllers of + * the three rear ports, each of which can carry the USB-C display. + */ + nub_spmi_a0: spmi@308908000 { + compatible = "apple,t8152-spmi", "apple,t8142-spmi"; + reg = <0x3 0x08908000 0x0 0x4000>; + #address-cells = <2>; + #size-cells = <0>; + + /* back-middle */ + hpm0: usb-pd@c { + compatible = "apple,sn201202x"; + reg = <0xc SPMI_USID>; + }; + + /* back-left */ + hpm1: usb-pd@a { + compatible = "apple,sn201202x"; + reg = <0xa SPMI_USID>; + }; + + /* back-right */ + hpm3: usb-pd@8 { + compatible = "apple,sn201202x"; + reg = <0x8 SPMI_USID>; + }; + }; + + /* + * ADT aop-gpio. Its interrupts are the AOP's, so it has no + * interrupt controller here; the HDMI HPD line is polled. + */ + pinctrl_aop: pinctrl@310824000 { + compatible = "apple,t8152-pinctrl", "apple,t8103-pinctrl"; + reg = <0x3 0x10824000 0x0 0x4000>; + gpio-controller; + #gpio-cells = <2>; + gpio-ranges = <&pinctrl_aop 0 0 77>; + apple,npins = <77>; + }; + + dcp_mbox: mailbox@316020500 { + compatible = "apple,t8152-ascwrap-v8-mailbox"; + reg = <0x3 0x16020500 0x0 0x80>; + interrupts = , + ; + interrupt-names = "send-empty", "recv-not-empty"; + #mbox-cells = <0>; + }; + + dcp_iboot: display@286e00000 { + compatible = "apple,t8152-dcp-iboot"; + reg = <0x2 0x86e00000 0x0 0x74000>, + <0x3 0x0002005c 0x0 0x4>, + <0x3 0x1a760000 0x0 0x4000>, + <0x3 0x00700460 0x0 0x4>; + /* aux: ADT dptx0-aux, aux-ps: PMGR GLOBAL_AUX_CTRL0 */ + reg-names = "coproc", "dp-clock", "aux", "aux-ps"; + #address-cells = <2>; + #size-cells = <2>; + ranges; + mboxes = <&dcp_mbox>; + iommus = <&dcp_dart 23>; + /* + * Cacheable DMA, as iBoot maps DCP buffers: the firmware + * faults on entries with the DART's no-cache bit. + */ + dma-coherent; + + /* + * The display pipe's own stream: a separate device, so + * its domain imports dart-disp0's root, not dart-dcp's. + */ + scanout { + iommus = <&disp0_dart 0>; + dma-coherent; + }; + + /* + * USB-C back-middle: ADT atc-phy0. PS: the ATC's DP_0, DP_1 and + * USB_AON power states (+0, +0x20, +0x100), PSD: its common + * domain. Tunables before and after the lane crossbar setup. + */ + usb-c@403000000 { + reg = <0x4 0x03000000 0x0 0x84000>, + <0x3 0x08290000 0x0 0x104>, + <0x3 0x03a13700 0x0 0xc0>; + reg-names = "atc", "atc-ps", "atc-psd"; + apple,dfp-endpoint = <0>; + apple,usb-pd = <&hpm0>; + apple,tunable-pre = + /* ACIOPHY_CMN_DFLT */ + <0x00800 0x00fc0000 0x00fc0000>, + <0x0084c 0x000ff800 0x000ff800>, + <0x0085c 0x000000ff 0x00000080>, + <0x00860 0x007fffff 0x0052bca4>, + <0x00864 0x02c00000 0x00800000>, + /* CIOPLL_CORE_DFLT */ + <0x01614 0x0003ffc0 0x0001e5c0>, + <0x01640 0x00000007 0x00000003>, + <0x01668 0x00000ffc 0x000005f0>; + apple,tunable-post = + /* ATC_FABRIC */ + <0x6c004 0x003f003f 0x00200020>, + <0x6c00c 0x000000f1 0x000000a1>, + <0x6c010 0x0000801f 0x00000004>, + <0x6c018 0x003f003f 0x00200020>, + <0x6c020 0x000000f1 0x000000a1>, + <0x6c024 0x0000801f 0x00000004>, + <0x6c02c 0x007f007f 0x00410041>, + <0x6c034 0x000000f1 0x00000071>, + <0x6c038 0x0000801f 0x00000004>, + <0x6c040 0x0000001f 0x00000010>, + <0x6c048 0x0000801f 0x00000004>, + <0x6c050 0x007f007f 0x00450045>, + <0x6c058 0x000000f1 0x00000051>, + <0x6c05c 0x0000801f 0x00000004>, + <0x6c064 0x0000003f 0x00000020>, + <0x6c06c 0x0000801f 0x00000004>, + <0x6c074 0x00000001 0x00000001>, + <0x6c078 0x0000801f 0x00000004>, + <0x6c080 0x001f001f 0x00100010>, + <0x6c084 0x000000f1 0x00000011>, + <0x6c088 0x00000001 0x00000001>, + <0x6c08c 0x00000001 0x00000001>, + <0x6c090 0x0000801f 0x00000004>, + <0x6c098 0x001f001f 0x00100010>, + <0x6c09c 0x000000f1 0x00000011>, + <0x6c0a0 0x0000801f 0x00000004>, + <0x6c0a8 0x000000ff 0x000000a0>, + <0x6c0ac 0x0000801f 0x00000004>, + <0x6c0b4 0x0000003f 0x00000020>, + <0x6c0bc 0x0000801f 0x00000004>, + <0x6c0c4 0x0000003f 0x00000020>, + <0x6c0cc 0x0000801f 0x00000004>, + <0x6c0d4 0x0000000f 0x00000008>, + <0x6c0d8 0x0000801f 0x00000004>, + <0x6c0e0 0x001f001f 0x00180018>, + <0x6c0e4 0x0000801f 0x00000004>, + <0x6c0ec 0x001f001f 0x00180018>, + <0x6c0f0 0x0000801f 0x00000004>, + <0x6c0f8 0x001f001f 0x00180018>, + <0x6c0fc 0x0000801f 0x00000004>, + <0x6c104 0x001f001f 0x00180018>, + <0x6c108 0x0000801f 0x00000004>, + <0x6c110 0x00000001 0x00000001>, + <0x6c114 0x0000801f 0x00000004>, + <0x6c11c 0x0000801f 0x00000004>, + <0x6c124 0x0000801f 0x00000004>, + <0x6c12c 0x0000801f 0x00000004>, + <0x6c134 0x0000801f 0x00000004>, + <0x6c13c 0xffffffff 0x01010101>, + <0x6c140 0x0000801f 0x00000004>, + <0x6c148 0x0000801f 0x00000004>, + <0x6c150 0xffffffff 0x01010101>, + <0x6c154 0x0000ffff 0x00000101>, + <0x6c158 0x0000801f 0x00000004>, + <0x6c160 0x0000801f 0x00000004>, + <0x6c168 0x0000801f 0x00000004>, + <0x6c170 0x0000801f 0x00000004>, + <0x6c178 0x0000801f 0x00000004>, + <0x6c180 0xffffffff 0x01010101>, + <0x6c184 0x0000801f 0x00000004>, + <0x6c18c 0x0000801f 0x00000004>, + <0x6c194 0x0000801f 0x00000004>, + /* DPPLL_CORE_DFLT */ + <0x01040 0x0000000c 0x00000008>, + <0x01060 0x0001c000 0x0000c000>, + <0x01098 0x00000f00 0x00000800>, + <0x0109c 0x000fe000 0x000fe000>, + <0x01108 0x07c00000 0x04000000>, + <0x0110c 0x0000007c 0x00000010>, + /* DPPLL_TOP_DFLT */ + <0x00e8c 0x0000007f 0x0000003c>, + <0x00e9c 0x0000007f 0x00000032>, + <0x00eac 0x0000007f 0x00000032>, + <0x00ebc 0x0000007f 0x0000005a>, + /* ASDC_AUX_SHM_DFLT */ + <0x02308 0x000000fc 0x00000084>; + }; + + /* + * USB-C back-left: ADT atc-phy1. PS: the ATC's DP_0, DP_1 and + * USB_AON power states (+0, +0x20, +0x100), PSD: its common + * domain. Tunables before and after the lane crossbar setup. + */ + usb-c@40b000000 { + reg = <0x4 0x0b000000 0x0 0x84000>, + <0x3 0x08290040 0x0 0x104>, + <0x3 0x03a13800 0x0 0xc0>; + reg-names = "atc", "atc-ps", "atc-psd"; + apple,dfp-endpoint = <1>; + apple,usb-pd = <&hpm1>; + apple,tunable-pre = + /* ACIOPHY_CMN_DFLT */ + <0x00800 0x00fc0000 0x00fc0000>, + <0x0084c 0x000ff800 0x000ff800>, + <0x0085c 0x000000ff 0x00000080>, + <0x00860 0x007fffff 0x0052bc26>, + <0x00864 0x02c00000 0x00800000>, + /* CIOPLL_CORE_DFLT */ + <0x01614 0x0003ffc0 0x0001e5c0>, + <0x01640 0x00000007 0x00000003>, + <0x01668 0x00000ffc 0x000005f0>; + apple,tunable-post = + /* ATC_FABRIC */ + <0x6c004 0x003f003f 0x00200020>, + <0x6c00c 0x000000f1 0x000000a1>, + <0x6c010 0x0000801f 0x00000004>, + <0x6c018 0x003f003f 0x00200020>, + <0x6c020 0x000000f1 0x000000a1>, + <0x6c024 0x0000801f 0x00000004>, + <0x6c02c 0x007f007f 0x00410041>, + <0x6c034 0x000000f1 0x00000071>, + <0x6c038 0x0000801f 0x00000004>, + <0x6c040 0x0000001f 0x00000010>, + <0x6c048 0x0000801f 0x00000004>, + <0x6c050 0x007f007f 0x00450045>, + <0x6c058 0x000000f1 0x00000051>, + <0x6c05c 0x0000801f 0x00000004>, + <0x6c064 0x0000003f 0x00000020>, + <0x6c06c 0x0000801f 0x00000004>, + <0x6c074 0x00000001 0x00000001>, + <0x6c078 0x0000801f 0x00000004>, + <0x6c080 0x001f001f 0x00100010>, + <0x6c084 0x000000f1 0x00000011>, + <0x6c088 0x00000001 0x00000001>, + <0x6c08c 0x00000001 0x00000001>, + <0x6c090 0x0000801f 0x00000004>, + <0x6c098 0x001f001f 0x00100010>, + <0x6c09c 0x000000f1 0x00000011>, + <0x6c0a0 0x0000801f 0x00000004>, + <0x6c0a8 0x000000ff 0x000000a0>, + <0x6c0ac 0x0000801f 0x00000004>, + <0x6c0b4 0x0000003f 0x00000020>, + <0x6c0bc 0x0000801f 0x00000004>, + <0x6c0c4 0x0000003f 0x00000020>, + <0x6c0cc 0x0000801f 0x00000004>, + <0x6c0d4 0x0000000f 0x00000008>, + <0x6c0d8 0x0000801f 0x00000004>, + <0x6c0e0 0x001f001f 0x00180018>, + <0x6c0e4 0x0000801f 0x00000004>, + <0x6c0ec 0x001f001f 0x00180018>, + <0x6c0f0 0x0000801f 0x00000004>, + <0x6c0f8 0x001f001f 0x00180018>, + <0x6c0fc 0x0000801f 0x00000004>, + <0x6c104 0x001f001f 0x00180018>, + <0x6c108 0x0000801f 0x00000004>, + <0x6c110 0x00000001 0x00000001>, + <0x6c114 0x0000801f 0x00000004>, + <0x6c11c 0x0000801f 0x00000004>, + <0x6c124 0x0000801f 0x00000004>, + <0x6c12c 0x0000801f 0x00000004>, + <0x6c134 0x0000801f 0x00000004>, + <0x6c13c 0xffffffff 0x01010101>, + <0x6c140 0x0000801f 0x00000004>, + <0x6c148 0x0000801f 0x00000004>, + <0x6c150 0xffffffff 0x01010101>, + <0x6c154 0x0000ffff 0x00000101>, + <0x6c158 0x0000801f 0x00000004>, + <0x6c160 0x0000801f 0x00000004>, + <0x6c168 0x0000801f 0x00000004>, + <0x6c170 0x0000801f 0x00000004>, + <0x6c178 0x0000801f 0x00000004>, + <0x6c180 0xffffffff 0x01010101>, + <0x6c184 0x0000801f 0x00000004>, + <0x6c18c 0x0000801f 0x00000004>, + <0x6c194 0x0000801f 0x00000004>, + /* DPPLL_CORE_DFLT */ + <0x01040 0x0000000c 0x00000008>, + <0x01060 0x0001c000 0x0000c000>, + <0x01098 0x00000f00 0x00000800>, + <0x0109c 0x000fe000 0x000fe000>, + <0x01108 0x07c00000 0x04000000>, + <0x0110c 0x0000007c 0x00000010>, + /* DPPLL_TOP_DFLT */ + <0x00e8c 0x0000007f 0x0000003c>, + <0x00e9c 0x0000007f 0x00000032>, + <0x00eac 0x0000007f 0x00000032>, + <0x00ebc 0x0000007f 0x0000005a>, + /* ASDC_AUX_SHM_DFLT */ + <0x02308 0x000000fc 0x00000084>; + }; + + /* + * USB-C back-right: ADT atc-phy3. PS: the ATC's DP_0, DP_1 and + * USB_AON power states (+0, +0x20, +0x100), PSD: its common + * domain. Tunables before and after the lane crossbar setup. + */ + usb-c@413000000 { + reg = <0x4 0x13000000 0x0 0x84000>, + <0x3 0x082900c0 0x0 0x104>, + <0x3 0x03a13a00 0x0 0xc0>; + reg-names = "atc", "atc-ps", "atc-psd"; + apple,dfp-endpoint = <3>; + apple,usb-pd = <&hpm3>; + apple,tunable-pre = + /* ACIOPHY_CMN_DFLT */ + <0x00800 0x00fc0000 0x00fc0000>, + <0x0084c 0x000ff800 0x000ff800>, + <0x0085c 0x000000ff 0x00000080>, + <0x00860 0x007fffff 0x0052bc26>, + <0x00864 0x02c00000 0x00800000>, + /* CIOPLL_CORE_DFLT */ + <0x01614 0x0003ffc0 0x0001e5c0>, + <0x01640 0x00000007 0x00000003>, + <0x01668 0x00000ffc 0x000005f0>; + apple,tunable-post = + /* ATC_FABRIC */ + <0x6c004 0x003f003f 0x00200020>, + <0x6c00c 0x000000f1 0x000000a1>, + <0x6c010 0x0000801f 0x00000004>, + <0x6c018 0x003f003f 0x00200020>, + <0x6c020 0x000000f1 0x000000a1>, + <0x6c024 0x0000801f 0x00000004>, + <0x6c02c 0x007f007f 0x00410041>, + <0x6c034 0x000000f1 0x00000071>, + <0x6c038 0x0000801f 0x00000004>, + <0x6c040 0x0000001f 0x00000010>, + <0x6c048 0x0000801f 0x00000004>, + <0x6c050 0x007f007f 0x00450045>, + <0x6c058 0x000000f1 0x00000051>, + <0x6c05c 0x0000801f 0x00000004>, + <0x6c064 0x0000003f 0x00000020>, + <0x6c06c 0x0000801f 0x00000004>, + <0x6c074 0x00000001 0x00000001>, + <0x6c078 0x0000801f 0x00000004>, + <0x6c080 0x001f001f 0x00100010>, + <0x6c084 0x000000f1 0x00000011>, + <0x6c088 0x00000001 0x00000001>, + <0x6c08c 0x00000001 0x00000001>, + <0x6c090 0x0000801f 0x00000004>, + <0x6c098 0x001f001f 0x00100010>, + <0x6c09c 0x000000f1 0x00000011>, + <0x6c0a0 0x0000801f 0x00000004>, + <0x6c0a8 0x000000ff 0x000000a0>, + <0x6c0ac 0x0000801f 0x00000004>, + <0x6c0b4 0x0000003f 0x00000020>, + <0x6c0bc 0x0000801f 0x00000004>, + <0x6c0c4 0x0000003f 0x00000020>, + <0x6c0cc 0x0000801f 0x00000004>, + <0x6c0d4 0x0000000f 0x00000008>, + <0x6c0d8 0x0000801f 0x00000004>, + <0x6c0e0 0x001f001f 0x00180018>, + <0x6c0e4 0x0000801f 0x00000004>, + <0x6c0ec 0x001f001f 0x00180018>, + <0x6c0f0 0x0000801f 0x00000004>, + <0x6c0f8 0x001f001f 0x00180018>, + <0x6c0fc 0x0000801f 0x00000004>, + <0x6c104 0x001f001f 0x00180018>, + <0x6c108 0x0000801f 0x00000004>, + <0x6c110 0x00000001 0x00000001>, + <0x6c114 0x0000801f 0x00000004>, + <0x6c11c 0x0000801f 0x00000004>, + <0x6c124 0x0000801f 0x00000004>, + <0x6c12c 0x0000801f 0x00000004>, + <0x6c134 0x0000801f 0x00000004>, + <0x6c13c 0xffffffff 0x01010101>, + <0x6c140 0x0000801f 0x00000004>, + <0x6c148 0x0000801f 0x00000004>, + <0x6c150 0xffffffff 0x01010101>, + <0x6c154 0x0000ffff 0x00000101>, + <0x6c158 0x0000801f 0x00000004>, + <0x6c160 0x0000801f 0x00000004>, + <0x6c168 0x0000801f 0x00000004>, + <0x6c170 0x0000801f 0x00000004>, + <0x6c178 0x0000801f 0x00000004>, + <0x6c180 0xffffffff 0x01010101>, + <0x6c184 0x0000801f 0x00000004>, + <0x6c18c 0x0000801f 0x00000004>, + <0x6c194 0x0000801f 0x00000004>, + /* DPPLL_CORE_DFLT */ + <0x01040 0x0000000c 0x00000008>, + <0x01060 0x0001c000 0x0000c000>, + <0x01098 0x00000f00 0x00000800>, + <0x0109c 0x000fe000 0x000fe000>, + <0x01108 0x07c00000 0x04000000>, + <0x0110c 0x0000007c 0x00000010>, + /* DPPLL_TOP_DFLT */ + <0x00e8c 0x0000007f 0x0000003c>, + <0x00e9c 0x0000007f 0x00000032>, + <0x00eac 0x0000007f 0x00000032>, + <0x00ebc 0x0000007f 0x0000005a>, + /* ASDC_AUX_SHM_DFLT */ + <0x02308 0x000000fc 0x00000084>; + }; + }; + + /* + * HDMI: DCPEXT0 (post-office mailbox 6) drives the dedicated + * dptx-phy through the PS190 converter. ADT dcpext0, + * dart-dcpext0 and dart-dispext0. + */ + dcpext_mbox: mailbox@316020600 { + compatible = "apple,t8152-ascwrap-v8-mailbox"; + reg = <0x3 0x16020600 0x0 0x80>; + interrupts = , + ; + interrupt-names = "send-empty", "recv-not-empty"; + #mbox-cells = <0>; + }; + + dcpext_iboot: display@32ae00000 { + compatible = "apple,t8152-dcp-iboot"; + reg = <0x3 0x2ae00000 0x0 0x74000>, + <0x3 0x1a740000 0x0 0x10000>, + <0x3 0x1a700000 0x0 0x4000>, + <0x3 0x1a764000 0x0 0x4000>, + <0x3 0x00700480 0x0 0x4>, + <0x3 0x007016c0 0x0 0x4>, + <0x3 0x00700e00 0x0 0x4>; + /* aux: ADT dptx1-aux, aux-ps: PMGR GLOBAL_AUX_CTRL1 */ + /* Read-only PMGR DPTX_PHY and DISPEXT0_CPU admission checks. */ + reg-names = "coproc", "phy-core", "phy-apb", "aux", "aux-ps", + "phy-ps", "coproc-ps"; + mboxes = <&dcpext_mbox>; + iommus = <&dcpext0_dart 23>; + dma-coherent; + apple,hdmi; + /* ADT dp2hdmi-gpio0 function-hdmi_hpd */ + hpd-gpios = <&pinctrl_aop 41 GPIO_ACTIVE_HIGH>; + + scanout { + iommus = <&dispext0_dart 0>; + dma-coherent; + }; + }; + }; +}; diff --git a/arch/arm64/boot/dts/apple/t8152-j873g.dts b/arch/arm64/boot/dts/apple/t8152-j873g.dts index 0f98b07748b01..169298a6c9e6c 100644 --- a/arch/arm64/boot/dts/apple/t8152-j873g.dts +++ b/arch/arm64/boot/dts/apple/t8152-j873g.dts @@ -502,3 +502,4 @@ #include "t8152-j873-usb.dtsi" #include "t8152-j873-ethernet.dtsi" #include "t8152-j873-audio.dtsi" +#include "t8152-j873-dcp.dtsi" From e862cf520423cdea62fde3dc70a46d54fdb3ecdd Mon Sep 17 00:00:00 2001 From: Thomas Glanzmann Date: Tue, 17 Nov 2026 22:53:55 +0100 Subject: [PATCH 25/29] arm64: dts: apple: j873: add DCPEXT1 and share the USB-C ports Describe DCPEXT1 (ADT dcpext1: ASC at 0x502e00000, post-office mailbox 7, IRQs 306/266, DARTs dart-dcpext1 SID 23 and dart-dispext1 SID 0) with its PMGR2 PSD admission register. Give each controller its crossbar source, let DCPEXT0 and DCPEXT1 use the main DCP's USB-C port children, and widen dp-clock to the PMGR ATC DP clock dividers of all ATCs. Signed-off-by: Thomas Glanzmann --- arch/arm64/boot/dts/apple/t8152-j873-dcp.dtsi | 58 ++++++++++++++++--- 1 file changed, 51 insertions(+), 7 deletions(-) diff --git a/arch/arm64/boot/dts/apple/t8152-j873-dcp.dtsi b/arch/arm64/boot/dts/apple/t8152-j873-dcp.dtsi index e41c4008c425f..8929f26c8bcc5 100644 --- a/arch/arm64/boot/dts/apple/t8152-j873-dcp.dtsi +++ b/arch/arm64/boot/dts/apple/t8152-j873-dcp.dtsi @@ -1,9 +1,10 @@ // SPDX-License-Identifier: GPL-2.0+ OR MIT /* - * J873g displays: the rear USB-C ports and the HDMI port, driven through + * J873g displays: three controllers for the rear USB-C and HDMI ports, + * driven through * the DCP firmware's iBoot interface (apple,t8152-dcp-iboot). The - * bootloader trains the displays and hands both DCPs over with their RTKit - * sessions running. + * bootloader hands over running or quiesced RTKit sessions. Unused + * controllers can claim another USB-C monitor through the shared crossbar. * * Resources are from the 26A434 ADT: dcp (iop,ascwrap-v8, post-office * mailbox 5 with alias: 0x316020000 + 2 * 5 * 0x80), dart-dcp (SID 23), @@ -37,6 +38,17 @@ status = "okay"; }; +&dcpext1_dart { + apple,dma-range = <0x108 0x0 0x8 0x0>; + status = "okay"; +}; + +&dispext1_dart { + reg-names = "dart", "dapf"; + apple,dma-range = <0x108 0x0 0x8 0x0>; + status = "okay"; +}; + / { soc { /* @@ -93,7 +105,7 @@ dcp_iboot: display@286e00000 { compatible = "apple,t8152-dcp-iboot"; reg = <0x2 0x86e00000 0x0 0x74000>, - <0x3 0x0002005c 0x0 0x4>, + <0x3 0x00020044 0x0 0x1c>, <0x3 0x1a760000 0x0 0x4000>, <0x3 0x00700460 0x0 0x4>; /* aux: ADT dptx0-aux, aux-ps: PMGR GLOBAL_AUX_CTRL0 */ @@ -108,6 +120,7 @@ * faults on entries with the DART's no-cache bit. */ dma-coherent; + apple,dp-source = <0>; /* * The display pipe's own stream: a separate device, so @@ -429,7 +442,7 @@ }; /* - * HDMI: DCPEXT0 (post-office mailbox 6) drives the dedicated + * DCPEXT0 (post-office mailbox 6) can drive USB-C, or the dedicated * dptx-phy through the PS190 converter. ADT dcpext0, * dart-dcpext0 and dart-dispext0. */ @@ -450,14 +463,17 @@ <0x3 0x1a764000 0x0 0x4000>, <0x3 0x00700480 0x0 0x4>, <0x3 0x007016c0 0x0 0x4>, - <0x3 0x00700e00 0x0 0x4>; + <0x3 0x00700e00 0x0 0x4>, + <0x3 0x03a12c00 0x0 0xc0>; /* aux: ADT dptx1-aux, aux-ps: PMGR GLOBAL_AUX_CTRL1 */ /* Read-only PMGR DPTX_PHY and DISPEXT0_CPU admission checks. */ reg-names = "coproc", "phy-core", "phy-apb", "aux", "aux-ps", - "phy-ps", "coproc-ps"; + "phy-ps", "coproc-ps", "coproc-psd"; mboxes = <&dcpext_mbox>; iommus = <&dcpext0_dart 23>; dma-coherent; + apple,dp-source = <1>; + apple,usb-c-ports = <&dcp_iboot>; apple,hdmi; /* ADT dp2hdmi-gpio0 function-hdmi_hpd */ hpd-gpios = <&pinctrl_aop 41 GPIO_ACTIVE_HIGH>; @@ -467,5 +483,33 @@ dma-coherent; }; }; + + dcpext1_mbox: mailbox@316020700 { + compatible = "apple,t8152-ascwrap-v8-mailbox"; + reg = <0x3 0x16020700 0x0 0x80>; + interrupts = , + ; + interrupt-names = "send-empty", "recv-not-empty"; + #mbox-cells = <0>; + }; + + /* ADT dcpext1: crossbar UFP2, firmware SID23, scanout SID0. */ + dcpext1_iboot: display@502e00000 { + compatible = "apple,t8152-dcp-iboot"; + reg = <0x5 0x02e00000 0x0 0x74000>, + <0x3 0x03a12d00 0x0 0xc0>; + reg-names = "coproc", "coproc-psd"; + mboxes = <&dcpext1_mbox>; + iommus = <&dcpext1_dart 23>; + dma-coherent; + apple,dp-source = <2>; + apple,usb-c-ports = <&dcp_iboot>; + + scanout { + iommus = <&dispext1_dart 0>; + dma-coherent; + }; + }; + }; }; From a199849f23e883639c4cab81b00543cbf3660336 Mon Sep 17 00:00:00 2001 From: Thomas Glanzmann Date: Wed, 18 Nov 2026 11:03:18 +0100 Subject: [PATCH 26/29] arm64: dts: apple: j873: describe the HDMI cold start Give the HDMI node the dptx-phy's PSD service (5), the SMC writes that power the PS190 (ADT dp2hdmi-gpio0 function-dp2hdmi_pwr_en and function-hdmi_pwr_en, SMC key pcIO) and the dptx-phy tunables from the ADT, so that the DCP driver can start HDMI without the bootloader. Signed-off-by: Thomas Glanzmann --- arch/arm64/boot/dts/apple/t8152-j873-dcp.dtsi | 39 +++++++++++++++++-- 1 file changed, 36 insertions(+), 3 deletions(-) diff --git a/arch/arm64/boot/dts/apple/t8152-j873-dcp.dtsi b/arch/arm64/boot/dts/apple/t8152-j873-dcp.dtsi index 8929f26c8bcc5..ebfae0dbbd1d0 100644 --- a/arch/arm64/boot/dts/apple/t8152-j873-dcp.dtsi +++ b/arch/arm64/boot/dts/apple/t8152-j873-dcp.dtsi @@ -464,11 +464,12 @@ <0x3 0x00700480 0x0 0x4>, <0x3 0x007016c0 0x0 0x4>, <0x3 0x00700e00 0x0 0x4>, - <0x3 0x03a12c00 0x0 0xc0>; + <0x3 0x03a12c00 0x0 0xc0>, + <0x3 0x03a10500 0x0 0xc0>; /* aux: ADT dptx1-aux, aux-ps: PMGR GLOBAL_AUX_CTRL1 */ - /* Read-only PMGR DPTX_PHY and DISPEXT0_CPU admission checks. */ + /* PMGR DPTX_PHY, read-only DISPEXT0_CPU, PSD service 5 (dptx-phy) */ reg-names = "coproc", "phy-core", "phy-apb", "aux", "aux-ps", - "phy-ps", "coproc-ps", "coproc-psd"; + "phy-ps", "coproc-ps", "coproc-psd", "phy-psd"; mboxes = <&dcpext_mbox>; iommus = <&dcpext0_dart 23>; dma-coherent; @@ -477,6 +478,38 @@ apple,hdmi; /* ADT dp2hdmi-gpio0 function-hdmi_hpd */ hpd-gpios = <&pinctrl_aop 41 GPIO_ACTIVE_HIGH>; + /* function-dp2hdmi_pwr_en, function-hdmi_pwr_en (SMC pcIO) */ + apple,smc = <&smc>; + apple,pcon-power = <0x7063494f 0x00140000 0x00000001>, + <0x7063494f 0x000f0000 0x00800001>; + /* ADT dptx-phy tunables, in AppleT8152DPTXPort::handleSetTunables order */ + apple,tunable-phy = <0x5180 0x1fff000 0x723000>, + <0x5184 0xffffffff 0x2bc>, + <0x7980 0x1fff000 0x723000>, + <0x7984 0xffffffff 0x2bc>, + <0xa180 0x1fff000 0x723000>, + <0xa184 0xffffffff 0x2bc>, + <0xc980 0x1fff000 0x723000>, + <0xc984 0xffffffff 0x2bc>, + <0x4524 0x3800000 0x2800000>, + <0x6d24 0x3800000 0x2800000>, + <0x9524 0x3800000 0x2800000>, + <0xbd24 0x3800000 0x2800000>, + <0x840 0xc 0x8>, + <0x860 0x1c000 0xc000>, + <0x898 0xf00 0x800>, + <0x89c 0xfe000 0xfe000>, + <0x908 0x7c00000 0x4000000>, + <0x90c 0x7c 0x10>, + <0x68c 0x7f 0x3c>, + <0x69c 0x7f 0x32>, + <0x6ac 0x7f 0x32>, + <0x6bc 0x7f 0x5a>, + <0x1408 0xfc 0x84>, + <0x1c4c 0xff800 0xff800>, + <0x1c5c 0xff 0x80>, + <0x1c60 0x7fffff 0x52bba7>, + <0x1c64 0x2c00000 0x800000>; scanout { iommus = <&dispext0_dart 0>; From c2bc858e7a2b4500acd9f92a7d5a4abadbf1e948 Mon Sep 17 00:00:00 2001 From: Thomas Glanzmann Date: Sun, 22 Nov 2026 15:28:08 +0100 Subject: [PATCH 27/29] dt-bindings: display: apple,t8152-dcp-iboot: allow a power domain The display coprocessor has a power state of its own, which its DARTs follow. Allow the node to name it. Signed-off-by: Thomas Glanzmann --- .../devicetree/bindings/display/apple,t8152-dcp-iboot.yaml | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/Documentation/devicetree/bindings/display/apple,t8152-dcp-iboot.yaml b/Documentation/devicetree/bindings/display/apple,t8152-dcp-iboot.yaml index 7c4e2b1db5ab9..6fbdaba2d17a4 100644 --- a/Documentation/devicetree/bindings/display/apple,t8152-dcp-iboot.yaml +++ b/Documentation/devicetree/bindings/display/apple,t8152-dcp-iboot.yaml @@ -188,6 +188,12 @@ properties: maxItems: 1 description: DCP DART stream + power-domains: + maxItems: 1 + description: + The coprocessor's power state, which its firmware runs; the DARTs + follow it. + scanout: type: object additionalProperties: false From 680a085f482f5b45faa5bd6360134ecacfbc487a Mon Sep 17 00:00:00 2001 From: Thomas Glanzmann Date: Sun, 22 Nov 2026 15:28:08 +0100 Subject: [PATCH 28/29] arm64: dts: apple: t8152-j873g: power domains for the display DARTs Every enabled DART must name the power state of the engine it serves (scripts/dtc/apple-dart-pd-check.py). The six J873g display DARTs named none. Their engines are the display coprocessors, whose PMGR states are DISP_CPU (dcp), DISPEXT0_CPU (dcpext0) and DISPEXT1_CPU (dcpext1) in the 26A434 ADT. The AP does not switch those: a coprocessor is started through its PSD service (coproc-psd), and its firmware runs the state; Linux only reads DISPEXT0_CPU (coproc-ps). So they are described as firmware-owned: the OS keeps them on and never reads or writes them, which leaves the display hand-off and the HDMI cold start as they are. The coprocessor nodes name the same state as their DARTs. DISP_CPU sits at PMGR offset 0x14000 (it reads 0x0f0000ff on J873g, as DISPEXT0/1_CPU do), past the 0x14000 the PMGR node described, so the node now spans the ADT's 0x1c000 PMGR window. Signed-off-by: Thomas Glanzmann --- arch/arm64/boot/dts/apple/t8152-j873-dcp.dtsi | 49 +++++++++++++++++++ .../arm64/boot/dts/apple/t8152-j873-qspi.dtsi | 3 +- 2 files changed, 51 insertions(+), 1 deletion(-) diff --git a/arch/arm64/boot/dts/apple/t8152-j873-dcp.dtsi b/arch/arm64/boot/dts/apple/t8152-j873-dcp.dtsi index ebfae0dbbd1d0..8263f1c68634b 100644 --- a/arch/arm64/boot/dts/apple/t8152-j873-dcp.dtsi +++ b/arch/arm64/boot/dts/apple/t8152-j873-dcp.dtsi @@ -18,34 +18,80 @@ #include #include +/* + * The display coprocessors' PMGR power states (ADT pmgr devices DISP_CPU, + * DISPEXT0_CPU and DISPEXT1_CPU), which their DARTs follow. The PMGR does not + * switch them for the AP: the coprocessor is started through its PSD service + * (coproc-psd below) and its firmware runs the state, so Linux only reads + * DISPEXT0_CPU (coproc-ps). They are the firmware's: the OS keeps them on and + * never accesses them. + */ +&pmgr { + ps_dispext1_cpu: power-controller@aa0 { + compatible = "apple,t8152-pmgr-pwrstate", + "apple,t8103-pmgr-pwrstate"; + reg = <0xaa0 4>; + #power-domain-cells = <0>; + #reset-cells = <0>; + label = "dispext1_cpu"; + apple,firmware-owned; + }; + + ps_dispext0_cpu: power-controller@e00 { + compatible = "apple,t8152-pmgr-pwrstate", + "apple,t8103-pmgr-pwrstate"; + reg = <0xe00 4>; + #power-domain-cells = <0>; + #reset-cells = <0>; + label = "dispext0_cpu"; + apple,firmware-owned; + }; + + ps_disp_cpu: power-controller@14000 { + compatible = "apple,t8152-pmgr-pwrstate", + "apple,t8103-pmgr-pwrstate"; + reg = <0x14000 4>; + #power-domain-cells = <0>; + #reset-cells = <0>; + label = "disp_cpu"; + apple,firmware-owned; + }; +}; + &dcp_dart { apple,dma-range = <0x108 0x0 0x8 0x0>; + power-domains = <&ps_disp_cpu>; status = "okay"; }; &disp0_dart { apple,dma-range = <0x108 0x0 0x8 0x0>; + power-domains = <&ps_disp_cpu>; status = "okay"; }; &dcpext0_dart { apple,dma-range = <0x108 0x0 0x8 0x0>; + power-domains = <&ps_dispext0_cpu>; status = "okay"; }; &dispext0_dart { apple,dma-range = <0x108 0x0 0x8 0x0>; + power-domains = <&ps_dispext0_cpu>; status = "okay"; }; &dcpext1_dart { apple,dma-range = <0x108 0x0 0x8 0x0>; + power-domains = <&ps_dispext1_cpu>; status = "okay"; }; &dispext1_dart { reg-names = "dart", "dapf"; apple,dma-range = <0x108 0x0 0x8 0x0>; + power-domains = <&ps_dispext1_cpu>; status = "okay"; }; @@ -115,6 +161,7 @@ ranges; mboxes = <&dcp_mbox>; iommus = <&dcp_dart 23>; + power-domains = <&ps_disp_cpu>; /* * Cacheable DMA, as iBoot maps DCP buffers: the firmware * faults on entries with the DART's no-cache bit. @@ -472,6 +519,7 @@ "phy-ps", "coproc-ps", "coproc-psd", "phy-psd"; mboxes = <&dcpext_mbox>; iommus = <&dcpext0_dart 23>; + power-domains = <&ps_dispext0_cpu>; dma-coherent; apple,dp-source = <1>; apple,usb-c-ports = <&dcp_iboot>; @@ -534,6 +582,7 @@ reg-names = "coproc", "coproc-psd"; mboxes = <&dcpext1_mbox>; iommus = <&dcpext1_dart 23>; + power-domains = <&ps_dispext1_cpu>; dma-coherent; apple,dp-source = <2>; apple,usb-c-ports = <&dcp_iboot>; diff --git a/arch/arm64/boot/dts/apple/t8152-j873-qspi.dtsi b/arch/arm64/boot/dts/apple/t8152-j873-qspi.dtsi index c03fbf04b24e4..538511e3ffc17 100644 --- a/arch/arm64/boot/dts/apple/t8152-j873-qspi.dtsi +++ b/arch/arm64/boot/dts/apple/t8152-j873-qspi.dtsi @@ -25,7 +25,8 @@ "syscon", "simple-mfd"; #address-cells = <1>; #size-cells = <1>; - reg = <0x3 0x700000 0x0 0x14000>; + /* ADT pmgr reg[32]: power states up to DISP_CPU at 0x14000 */ + reg = <0x3 0x700000 0x0 0x1c000>; }; qspi: spi@331118000 { From 0bcbf38c02ce985856cb53e8de2b929e2c86d49e Mon Sep 17 00:00:00 2001 From: Thomas Glanzmann Date: Sat, 21 Nov 2026 22:52:24 +0100 Subject: [PATCH 29/29] drm/apple: iboot: send page-flip events from a vblank timer The iBoot interface has no vblank interrupt, so the CRTC set no_vblank and the atomic helpers completed every page-flip event right after the commit. A compositor then gets its flips back as fast as it can commit and renders, and asks its clients to render, at whatever rate the commits complete instead of once per refresh. The plane update still shows at most one new frame per refresh (swaps are spaced one refresh apart), so the extra frames are drawn and copied for nothing and the animation advances unevenly. Use the DRM vblank timer (drm_vblank_helper): drm_vblank_init(), the timer-backed enable/disable/timestamp functions, and drm_crtc_vblank_atomic_flush() to arm each commit's event for the next tick at the mode's refresh rate. The CRTC switches the timer on at the end of atomic_enable and off at the start of atomic_disable. The timer is not locked to the firmware's swaps. The flip timestamps and the vblank sequence (and so the wp_presentation_feedback a compositor derives from them) are the timer's ticks at the mode's rate, not measured scanout times: they can be up to one refresh away from when the frame actually showed. That is enough for pacing; a reader of presentation feedback on this path should not take them as scanout times. A commit that leaves the CRTC off still sets no_vblank. The commit tail (commit_tail_rpm) does not flush an inactive CRTC, so nothing arms its event; with no_vblank, drm_atomic_helper_commit_hw_done() sends it. Without that, commit_hw_done warns that the event was not consumed. Signed-off-by: Thomas Glanzmann --- drivers/gpu/drm/apple/iboot_drm.c | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/drivers/gpu/drm/apple/iboot_drm.c b/drivers/gpu/drm/apple/iboot_drm.c index c0b3bd2311962..74eddc0fa3ce9 100644 --- a/drivers/gpu/drm/apple/iboot_drm.c +++ b/drivers/gpu/drm/apple/iboot_drm.c @@ -35,6 +35,8 @@ #include #include #include +#include +#include #include "dcp-internal.h" #include "ibootep.h" @@ -689,8 +691,15 @@ static int iboot_crtc_atomic_check(struct drm_crtc *crtc, struct drm_crtc_state *new = drm_atomic_get_new_crtc_state(state, crtc); struct dcp_iboot *ib = to_dcp_iboot(crtc->dev); - /* No vblank interrupt: the helpers complete events after the flush. */ - new->no_vblank = true; + /* + * No vblank interrupt: page-flip events come from a timer at the + * mode's refresh rate (drm_vblank_helper), so a compositor and its + * clients render once per refresh instead of as fast as commits + * complete. A commit that leaves the CRTC off gets no flush + * (commit_tail_rpm flushes active CRTCs only), so its event goes out + * at once, as before. + */ + new->no_vblank = !new->active; if (new->enable && !iboot_find_mode(ib, &new->mode)) return -EINVAL; return 0; @@ -871,6 +880,7 @@ static void iboot_crtc_atomic_enable(struct drm_crtc *crtc, iboot_set_mode_locked(ib, t, c); out: mutex_unlock(&ib->lock); + drm_crtc_vblank_on(crtc); } static void iboot_crtc_atomic_disable(struct drm_crtc *crtc, @@ -880,6 +890,7 @@ static void iboot_crtc_atomic_disable(struct drm_crtc *crtc, struct dcp_iboot *ib = to_dcp_iboot(crtc->dev); int ret, idx; + drm_crtc_vblank_off(crtc); /* A mode change: SET_MODE alone switches, as before. */ if (new->active) return; @@ -915,6 +926,7 @@ static void iboot_crtc_atomic_disable(struct drm_crtc *crtc, static const struct drm_crtc_helper_funcs iboot_crtc_helper = { .atomic_check = iboot_crtc_atomic_check, + .atomic_flush = drm_crtc_vblank_atomic_flush, .atomic_enable = iboot_crtc_atomic_enable, .atomic_disable = iboot_crtc_atomic_disable, }; @@ -926,6 +938,7 @@ static const struct drm_crtc_funcs iboot_crtc_funcs = { .page_flip = drm_atomic_helper_page_flip, .atomic_duplicate_state = drm_atomic_helper_crtc_duplicate_state, .atomic_destroy_state = drm_atomic_helper_crtc_destroy_state, + DRM_CRTC_VBLANK_TIMER_FUNCS, }; static const struct drm_encoder_funcs iboot_encoder_funcs = { @@ -1055,6 +1068,10 @@ int dcp_iboot_kms_init(struct dcp_iboot *ib) return ret; drm_crtc_helper_add(&ib->crtc, &iboot_crtc_helper); + ret = drm_vblank_init(drm, 1); + if (ret) + return ret; + ret = drm_encoder_init(drm, &ib->encoder, &iboot_encoder_funcs, DRM_MODE_ENCODER_TMDS, NULL); if (ret)