diff --git a/arch/arm64/boot/dts/apple/t6040-j614s-pciec.dtsi b/arch/arm64/boot/dts/apple/t6040-j614s-pciec.dtsi new file mode 100644 index 0000000000000..9e131830b8663 --- /dev/null +++ b/arch/arm64/boot/dts/apple/t6040-j614s-pciec.dtsi @@ -0,0 +1,94 @@ +// SPDX-License-Identifier: GPL-2.0+ OR MIT +/* + * J614s: PCIe-C (tunneled PCIe) on the three Thunderbolt ports, e.g. a + * Studio Display's xHCI. No boot loader hands the ports over: the kernel + * cold-initializes them. + * + * From the j614s ADT (macOS 26 IODeviceTree), identical for apciec0-2 and + * dart-apciec0-2: + * - the DART's DMA window: vm-base 1 TiB + 2 GiB, vm-size 0x7ff00000, its + * first 16 KiB a dead mapping; + * - bypass-253 and bypass-255: the port's own writes (MSIs turned into AIC + * writes) use those reserved streams, which must bypass, as on T6050; + * - the DART tunables, the controller's debug, rc and fabric tunables and + * the port's config tunables. + * Full error reporting, CTC flush and the ATC PCIe power domain as on + * T6050's PCIe-C DART. + */ + +#define J614S_PCIEC(n) \ +&usb4_##n##_pcie_tunnel { \ + status = "okay"; \ +}; \ + \ +&apciec##n##_dart { \ + apple,dma-range = <0x100 0x80004000 0x0 0x7fefc000>; \ + power-domains = <&ps_atc##n##_pcie>; \ + apple,bypass-sids = <253 255>; \ + apple,full-error-reporting; \ + apple,ctc-flush; \ + apple,tunable = <0x20c 0x80000121 0x80000121>, \ + <0x220 0xf0f0f 0xf0f0f>, \ + <0x224 0xffffff 0x80808>, \ + <0x300 0x1f31 0x1>, \ + <0x308 0x3ffffffc 0x10000000>, \ + <0x310 0x3ffffffc 0x3ffffffc>; \ + status = "okay"; \ +}; \ + \ +&usb4_##n##_pcie { \ + apple,pciec-kernel-init; \ + apple,tunable-debug = <0x1000 0x1 0x0>; \ + apple,tunable-rc = <0x78 0x7000 0x0>, \ + <0x718 0x7c000 0x0>, \ + <0x814 0xffffffff 0x2000f060>, \ + <0x8bc 0x1 0x0>; \ + apple,tunable-fabric = <0x4 0x1f001f 0x100010>, \ + <0x8 0x1 0x1>, \ + <0xc 0x1 0x1>, \ + <0x10 0x801f 0x4>, \ + <0x18 0xff 0xe0>, \ + <0x1c 0x1 0x1>, \ + <0x20 0x801f 0x4>, \ + <0x28 0x1f 0x10>, \ + <0x2c 0x801f 0x4>, \ + <0x34 0x3f 0x20>, \ + <0x38 0x801f 0x4>, \ + <0x40 0x3f 0x20>, \ + <0x44 0x801f 0x4>, \ + <0x4c 0x10001 0x10001>, \ + <0x50 0x1 0x1>, \ + <0x54 0x1 0x1>, \ + <0x58 0x801f 0x4>, \ + <0x60 0x7f007f 0x450045>, \ + <0x64 0x801f 0x4>, \ + <0x6c 0x1f001f 0x100010>, \ + <0x70 0xf1 0x1>, \ + <0x74 0x801f 0x4>, \ + <0x7c 0x801f 0x4>, \ + <0x84 0x1 0x1>, \ + <0x88 0x1 0x1>, \ + <0x8c 0x801f 0x4>, \ + <0x94 0x1 0x1>, \ + <0x98 0x801f 0x4>, \ + <0xa0 0x1 0x1>, \ + <0xa4 0x801f 0x4>, \ + <0xac 0x1 0x1>, \ + <0xb0 0x1 0x1>, \ + <0xb4 0x801f 0x4>, \ + <0xbc 0x801f 0x4>, \ + <0xc4 0x801f 0x4>; \ + status = "okay"; \ +}; \ + \ +&usb4_##n##_pcie_port { \ + apple,tunable = <0x140 0x1 0x1>, \ + <0x2a0 0x7e 0xa>, \ + <0x1090 0x1 0x0>; \ +}; + +J614S_PCIEC(0) +J614S_PCIEC(1) +J614S_PCIEC(2) + +#undef J614S_PCIEC diff --git a/arch/arm64/boot/dts/apple/t6040-j614s.dts b/arch/arm64/boot/dts/apple/t6040-j614s.dts index 2d4bd56b1ba99..73b76db059b18 100644 --- a/arch/arm64/boot/dts/apple/t6040-j614s.dts +++ b/arch/arm64/boot/dts/apple/t6040-j614s.dts @@ -131,7 +131,6 @@ }; &usb4_0_acio { - /delete-property/ apple,pcie-tunnel; status = "okay"; }; @@ -166,7 +165,6 @@ }; &usb4_1_acio { - /delete-property/ apple,pcie-tunnel; status = "okay"; }; @@ -201,7 +199,6 @@ }; &usb4_2_acio { - /delete-property/ apple,pcie-tunnel; status = "okay"; }; @@ -216,3 +213,4 @@ }; #include "t6040-j614s-extdisp-hdmi.dtsi" +#include "t6040-j614s-pciec.dtsi" diff --git a/drivers/gpu/drm/apple/dcp-fabric.c b/drivers/gpu/drm/apple/dcp-fabric.c index 25fadf4739c1f..5cbad80ca42a7 100644 --- a/drivers/gpu/drm/apple/dcp-fabric.c +++ b/drivers/gpu/drm/apple/dcp-fabric.c @@ -456,6 +456,12 @@ bool dcp_is_typec_only(struct platform_device *pdev) return !dcp->fixed_phy; } +static bool dcp_fabric_t6040_tunnel_clock(void) +{ + return of_machine_is_compatible("apple,t6040") || + of_machine_is_compatible("apple,t6041"); +} + static int dcp_route_clock_rate(struct apple_dcp_typec_route *route, struct phy *phy, unsigned int dpin, u8 rate) { @@ -479,6 +485,20 @@ static int dcp_route_clock_rate(struct apple_dcp_typec_route *route, return ret; } + /* T6040: the PHY's own AUSPLL feeds the pixel clock of DP IN 0 and 1. */ + if (dcp_fabric_t6040_tunnel_clock()) { + typeof(apple_t6040_dp_tunnel_rate) *fn = + symbol_get(apple_t6040_dp_tunnel_rate); + + if (!fn) + return rate ? -EOPNOTSUPP : 0; + ret = fn(phy, dpin, rate); + symbol_put(apple_t6040_dp_tunnel_rate); + if (!ret) + route->tunnel_rate = rate; + return ret; + } + /* A T8132 dcpext names its source (2 or 4), as T6030 does. */ if (of_machine_is_compatible("apple,t6030") || t8132) { typeof(apple_atc_dp_tunnel_rate_source) *fn; @@ -2266,6 +2286,9 @@ static void dcp_tunnel_prepare(struct apple_dcp_typec_route *route, if (ret && ret != -EOPNOTSUPP) dev_warn(dcp->dev, "DP tunnel crossbar preselect failed: %d\n", ret); + /* The clock call starts everything on T6040. */ + if (dcp_fabric_t6040_tunnel_clock()) + return; open = symbol_get(apple_atc_dp_tunnel_open); if (!open) return; @@ -2451,8 +2474,16 @@ int dcp_tunnel_crossbar_up(struct apple_dcp *dcp, u32 unit) ret = mux_control_try_select(route->active_xbar, route->mux_index); if (!ret) route->xbar_up = true; - /* T602X DP IN selection reserves and routes the mux only. */ - if (!ret && dcp_t602x_tunnel_route(route)) { + /* + * T602X DP IN selection reserves and routes the mux only, so the + * first link up also enables the connection. The T6041 crossbar + * (M4 Pro/Max) takes the same T6030 flow on every firmware: without + * this its clocks stay off until a retrain, and a mode set that + * keeps the trained link (5K on 26.6) never gets any video. + */ + if (!ret && (dcp_t602x_tunnel_route(route) || + of_device_is_compatible(route->active_xbar->chip->dev.parent->of_node, + "apple,t6041-display-crossbar"))) { ret = dcp_tunnel_xbar_configure(route); if (!ret) ret = dcp_dpxbar_link(route->active_xbar, true); @@ -3066,12 +3097,22 @@ static int dcp_tb_split_teardown_locked(struct apple_dcp_typec_port *port, /* Retire the callback before dropping its clock reference. */ dcp->split.active = false; if (dcp->phy) { - typeof(apple_atc_dp_tunnel_rate) *stop = - symbol_get(apple_atc_dp_tunnel_rate); + if (dcp_fabric_t6040_tunnel_clock()) { + typeof(apple_t6040_dp_tunnel_rate) *stop = + symbol_get(apple_t6040_dp_tunnel_rate); + + if (stop) { + stop(dcp->phy, 1, 0); + symbol_put(apple_t6040_dp_tunnel_rate); + } + } else { + typeof(apple_atc_dp_tunnel_rate) *stop = + symbol_get(apple_atc_dp_tunnel_rate); - if (stop) { - stop(dcp->phy, 1, 0); - symbol_put(apple_atc_dp_tunnel_rate); + if (stop) { + stop(dcp->phy, 1, 0); + symbol_put(apple_atc_dp_tunnel_rate); + } } } if (dcp->split.xbar) { diff --git a/drivers/mux/apple-display-crossbar.c b/drivers/mux/apple-display-crossbar.c index f45d296eebef4..12e1f8a1cf789 100644 --- a/drivers/mux/apple-display-crossbar.c +++ b/drivers/mux/apple-display-crossbar.c @@ -106,6 +106,8 @@ struct apple_dpxbar_hw { bool t6030_flow; /* no UNK_TUNABLE write at probe */ bool no_tunable; + /* DP IN tunnels enable the ATC at 0x38; 0x34 selects UHBR (T604X) */ + bool t604x_dpin_atc; /* power domain held only while an output is selected */ bool runtime_pm; /* T8142 layout: a connection status still busy after its poll is only logged */ @@ -236,6 +238,14 @@ static void t602x_dump(struct apple_dpxbar *xbar, const char *tag) #define T602X_DISPEXT_ENABLE 0x02c #define T602X_MUX_SELECT 0x030 #define T602X_ATC_ENABLE 0x034 +/* + * T604X, when a connection comes up: 0x34 holds a per-clock UHBR select + * (bit n set when the link rate is UHBR, cleared when it is not) and the ATC + * is enabled at 0x38 (its bit ORed in); neither is cleared at teardown. Used + * for DP IN tunnels only: the DP alt mode output keeps the T6030 write. + */ +#define T604X_ATC_UHBR_SELECT 0x034 +#define T604X_ATC_ENABLE 0x038 /* * Field of a T6030 output in the gate, select and enable registers. DP IN 0, @@ -285,7 +295,13 @@ static void apple_dpxbar_t6030_link(struct apple_dpxbar *xbar, dpxbar_set32(xbar, T602X_FIFO_WR_DPTX_CLK_EN, u); dpxbar_set32(xbar, rd, u); dpxbar_set32(xbar, out, d); - dpxbar_set32(xbar, T602X_ATC_ENABLE, d); + if (xbar->hw->t604x_dpin_atc && index != MUX_DPPHY) { + /* DP IN tunnels carry DP 1.x rates only: no UHBR select. */ + dpxbar_clear32(xbar, T604X_ATC_UHBR_SELECT, d); + dpxbar_set32(xbar, T604X_ATC_ENABLE, d); + } else { + dpxbar_set32(xbar, T602X_ATC_ENABLE, d); + } dpxbar_set32(xbar, T602X_DISPEXT_ENABLE, u); } else { dpxbar_clear32(xbar, T602X_DISPEXT_ENABLE, u); @@ -1380,6 +1396,9 @@ static int apple_dpxbar_resume(struct device *dev) u32 shift = apple_dpxbar_t6030_shift(i); u32 mask = (0xfU << shift) | (0xfU << (shift + 12)); u32 route = (state << shift) | (state << (shift + 12)); + /* Where apple_dpxbar_t6030_link() set the ATC enable of this output. */ + u32 atc_enable_reg = xbar->hw->t604x_dpin_atc && i != MUX_DPPHY ? + T604X_ATC_ENABLE : T602X_ATC_ENABLE; if (state < 0) continue; @@ -1388,7 +1407,7 @@ static int apple_dpxbar_resume(struct device *dev) restored |= BIT(i); } if (xbar->tunnel_link_up[i] && - (!(readl(xbar->regs + T602X_ATC_ENABLE) & BIT(shift)) || + (!(readl(xbar->regs + atc_enable_reg) & BIT(shift)) || !(readl(xbar->regs + T602X_DISPEXT_ENABLE) & BIT(state)))) { xbar->tunnel_link_up[i] = false; restored |= BIT(i); @@ -1445,6 +1464,7 @@ static const struct apple_dpxbar_hw apple_dpxbar_hw_t6041 = { .ops = &apple_dpxbar_t602x_ops, .t6030_flow = true, .no_tunable = true, + .t604x_dpin_atc = true, }; /* diff --git a/drivers/pci/controller/pcie-apple.c b/drivers/pci/controller/pcie-apple.c index a3671bf39d196..b2224f9fe6930 100644 --- a/drivers/pci/controller/pcie-apple.c +++ b/drivers/pci/controller/pcie-apple.c @@ -432,6 +432,16 @@ static const struct hw_info t8132_pciec_hw = { .s2idle_hold_domain = true, }; +/* + * T6040 PCIe-C is an Intr2AXI controller like T6050: the ADT + * (apciec,t6040) has no oe-fabric region, and its fabric/rc/debug/config and + * DART tunables are T6050's with small value differences. Cold init therefore + * takes the T6050 path (no port reset sequence, RID2SID cleared only). + * RID2SID slots are allocated as on T6050, not indexed by SID: with + * iommu-map-mask 0xff a device behind the tunnel (e.g. 03:00.0, the + * Studio Display's xHCI) shares SID 0 with 01:00.0, and an indexed slot + * then fails its enable with -EBUSY. + */ static const struct hw_info t6040_pciec_hw = { .port_msiaddr = PORT_MSIADDR, .port_msiaddr_hi = PORT_MSIADDR + 4, @@ -440,9 +450,9 @@ static const struct hw_info t6040_pciec_hw = { .port_msimap = PORT_T602X_MSIMAP, .port_tunctrl = 0x1088, .max_rid2sid = 256, - .sid_indexed_rid2sid = true, .tunneled = true, - .t8132_reset = true, + .tunnel_intr2axi = true, + .no_port_reset = true, .msimap_by_vector = true, .s2idle_hold_domain = true, }; diff --git a/drivers/phy/apple/t6040-usb2.c b/drivers/phy/apple/t6040-usb2.c index 50d82116aac2d..ba933da64a02a 100644 --- a/drivers/phy/apple/t6040-usb2.c +++ b/drivers/phy/apple/t6040-usb2.c @@ -293,6 +293,9 @@ struct t6040_usb2_phy { bool usb3_lent; /* DP mode took the lanes of this Type-C port */ bool pipe_switched; /* the controller's PIPE runs on this PHY */ bool cio_up; /* the lanes run USB4/Thunderbolt */ + u8 tun_rate[2]; /* DP tunnel link rate per DP IN adapter, 0 = off */ + bool tun_pre; /* switch 1 is held for DP IN 1 alone */ + bool tun_pll; /* AUSPLL programmed for DP tunnels */ /* DisplayPort; dp_ok is false when the DT lacks a DP route */ struct phy *dp; @@ -1769,11 +1772,14 @@ static int t6040_cio_up_locked(struct t6040_usb2_phy *tphy) return ret; } +static void t6040_tun_stop_all_locked(struct t6040_usb2_phy *tphy); + static void t6040_cio_down_locked(struct t6040_usb2_phy *tphy) { lockdep_assert_held(&tphy->lock); if (!tphy->cio_up) return; + t6040_tun_stop_all_locked(tphy); t6040_phy_tx_cmn_down(tphy); t6040_cio_rx_down(tphy); fsleep(200); @@ -1949,6 +1955,307 @@ static const struct phy_ops t6040_dp_phy_ops = { .set_mode = t6040_dp_set_mode, }; +/* ---- DP tunnels: the pixel clocks of the router's DP IN adapters (PHY in CIO) ---- + * + * The DP 1.4 tunnel's init, update and shutdown and the DP clock + * configuration: one fixed AUSPLL frequency feeds the DPTX pixel clocks, + * whose select field divides it per link rate. DP IN adapter 0 is taken to + * use PCLK1 and clock switch 1, adapter 1 PCLK2 and switch 2 (the PHY does + * not choose: the index is the display controller's). Driven only by + * appledrm through apple_t6040_dp_tunnel_rate(), and only while the PHY is in + * CIO. + */ +#define T6040_CORE_RCAL1 0x804 /* AUS40CMN_DIG RCAL1 */ +#define T6040_CORE_RCAL1_DONE BIT(0) +#define T6040_CORE_AUSPLL_CMD 0x3000 /* AUSPLL_TOP_APB_CMD_CMDOVERRIDE */ +#define T6040_CORE_AUSPLL_CMD_REQ BIT(0) +#define T6040_CORE_AUSPLL_CMD_ACK BIT(1) +#define T6040_CORE_AUSPLL_FREQDESC_A 0x3080 +#define T6040_CORE_AUSPLL_FREQDESC_B 0x3084 +#define T6040_CORE_AUSPLL_FREQDESC_C 0x3088 +#define T6040_CORE_AUSPLL_CLKOUT_MASTER 0x3200 +#define T6040_CORE_AUSPLL_CLKOUT_DIV 0x3208 +#define T6040_CORE_AUSPLL_BGR 0x3218 +#define T6040_CORE_AUSPLL_FREQINIT_1 0x3234 +#define T6040_CORE_TX_DP_CTRL0 0x7000 +#define T6040_CORE_TX_DP_CTRL0_ENABLES GENMASK(18, 16) +#define T6040_CORE_DP_PCLK_STATUS 0x7034 +#define T6040_CORE_DP_PCLK_STATUS_LOCK BIT(3) /* AUSPLL_LOCK */ + +static const struct t6040_tun_pclk { + u8 sel_shift; /* TX_DP_CTRL0 select field */ + u32 en; /* TX_DP_CTRL0 enable */ + u32 sw_mask, sw_gate, sw_on; /* clock switch: cleared, set, status when on */ + u32 req[2], req_stat[2], sw_off; /* PMGR requests, their status, cleared at off */ +} t6040_tun_pclk[2] = { + { 4, BIT(16), 0x473, 0x10, BIT(0), { 0x20, 0x40 }, { BIT(1), BIT(2) }, 0x403 }, + { 8, BIT(17), 0xb8c, 0x80, BIT(3), { 0x100, 0x200 }, { BIT(4), BIT(5) }, 0x80c }, +}; + +/* The pixel clock select: RBR, HBR, HBR2, HBR3; UHBR is not tunnelled on this path. */ +static int t6040_tun_select(u8 rate) +{ + switch (rate) { + case 0x06: + return 4; + case 0x0a: + return 3; + case 0x14: + return 1; + case 0x1e: + return 0; + default: + return -EINVAL; + } +} + +/* DP IN 1 runs on PCLK2 and needs PCLK1 as well: without it its video stops. */ +static u32 t6040_tun_enables(struct t6040_usb2_phy *tphy) +{ + return ((tphy->tun_rate[0] || tphy->tun_rate[1]) ? t6040_tun_pclk[0].en : 0) | + (tphy->tun_rate[1] ? t6040_tun_pclk[1].en : 0); +} + +/* AUSPLL_TOP command override: request @req with the toggle protocol, then wait for @ack. */ +static int t6040_tun_pll_cmd(struct t6040_usb2_phy *tphy, u32 val, bool ack_set) +{ + void __iomem *cmd = t6040_core(tphy) + T6040_CORE_AUSPLL_CMD; + u32 v; + + writel((readl(cmd) & 0xe0000006) | val, cmd); + return readl_poll_timeout(cmd, v, !!(v & T6040_CORE_AUSPLL_CMD_ACK) == ack_set, + 10, 500 * USEC_PER_MSEC); +} + +/* The DP 1.4 tunnel shutdown: pixel clocks off, AUSPLL released. */ +static void t6040_tun_pll_stop(struct t6040_usb2_phy *tphy) +{ + void __iomem *core = t6040_core(tphy); + bool ack = readl(core + T6040_CORE_AUSPLL_CMD) & T6040_CORE_AUSPLL_CMD_ACK; + u32 v; + + t6040_usb2_clear32(core + T6040_CORE_TX_DP_CTRL0, T6040_CORE_TX_DP_CTRL0_ENABLES); + t6040_usb2_clear32(core + T6040_CORE_TX_DP_CTRL0, 0xc); + if (t6040_tun_pll_cmd(tphy, (ack ? 0 : T6040_CORE_AUSPLL_CMD_REQ) | 0x10000000, !ack) || + t6040_tun_pll_cmd(tphy, (ack ? T6040_CORE_AUSPLL_CMD_REQ : 0) | 0x10000018, ack) || + readl_poll_timeout(core + T6040_CORE_DP_PCLK_STATUS, v, + !(v & T6040_CORE_DP_PCLK_STATUS_LOCK), 10, 500 * USEC_PER_MSEC)) + dev_warn(tphy->dev, "DP tunnel: AUSPLL release timed out (cmd %#x, pclk status %#x)\n", + readl(core + T6040_CORE_AUSPLL_CMD), + readl(core + T6040_CORE_DP_PCLK_STATUS)); + /* Both outputs the start set: a stale 0x800 reads as "in use" next time. */ + t6040_usb2_clear32(core + T6040_CORE_AUSPLL_CLKOUT_MASTER, 0x808); + tphy->tun_pll = false; +} + +/* The common block's init, then the fixed AUSPLL program of the DP 1.4 tunnel init. */ +static int t6040_tun_pll_start(struct t6040_usb2_phy *tphy) +{ + void __iomem *core = t6040_core(tphy); + bool ack; + u32 v; + + if ((readl(core + T6040_CORE_AUSPLL_CLKOUT_MASTER) & 0x808) || + (readl(core + T6040_CORE_AUSPLL_CMD) & T6040_CORE_AUSPLL_CMD_REQ)) { + dev_warn(tphy->dev, "DP tunnel: AUSPLL already in use (master %#x, cmd %#x)\n", + readl(core + T6040_CORE_AUSPLL_CLKOUT_MASTER), + readl(core + T6040_CORE_AUSPLL_CMD)); + return -EBUSY; + } + t6040_usb2_clear32(core + T6040_CORE_TX_DP_CTRL0, T6040_CORE_TX_DP_CTRL0_ENABLES); + + t6040_usb2_set32(core + T6040_CORE_CMN_OVRD, 0x4); + t6040_usb2_set32(core + T6040_CORE_CMN_OVRD, 0x8); + udelay(1); + t6040_usb2_set32(core + T6040_CORE_CMN_OVRD, 0x1); + t6040_usb2_set32(core + T6040_CORE_CMN_OVRD, 0x2); + udelay(1); + t6040_usb2_clear32(core + T6040_CORE_CMN_OVRD, T6040_CORE_CMN_OVRD_CLAMP); + t6040_usb2_set32(core + T6040_CORE_CMN_OVRD, 0x20); + udelay(1); + t6040_usb2_set32(core + T6040_CORE_CMN_BIAS, T6040_CORE_CMN_BIAS_BGBIAS_EN_OV); + udelay(15); + if (readl_poll_timeout(core + T6040_CORE_RCAL1, v, v & T6040_CORE_RCAL1_DONE, 1, 100)) + dev_dbg(tphy->dev, "DP tunnel: RCAL not done (%#x)\n", v); + + t6040_usb2_clear32(core + T6040_CORE_AUSPLL_FREQINIT_1, 0x3); + writel(0x1e0e021c, core + T6040_CORE_AUSPLL_FREQDESC_A); + t6040_usb2_mask32(core + T6040_CORE_AUSPLL_FREQDESC_B, 0x0fffffff, 0); + t6040_usb2_mask32(core + T6040_CORE_AUSPLL_FREQDESC_C, 0x007fffff, 0x644a00); + t6040_usb2_mask32(core + T6040_CORE_AUSPLL_CLKOUT_DIV, 0x001f0000, 0x10000); + t6040_usb2_set32(core + T6040_CORE_AUSPLL_BGR, 0x1); + t6040_usb2_set32(core + T6040_CORE_AUSPLL_CLKOUT_MASTER, 0x808); + tphy->tun_pll = true; + + ack = readl(core + T6040_CORE_AUSPLL_CMD) & T6040_CORE_AUSPLL_CMD_ACK; + if (t6040_tun_pll_cmd(tphy, (ack ? 0 : T6040_CORE_AUSPLL_CMD_REQ) | 0x10000000, true) || + readl_poll_timeout(core + T6040_CORE_DP_PCLK_STATUS, v, + !!(v & T6040_CORE_DP_PCLK_STATUS_LOCK) == !ack, + 10, 500 * USEC_PER_MSEC) || + t6040_tun_pll_cmd(tphy, (ack ? T6040_CORE_AUSPLL_CMD_REQ : 0) | 0x10010000, ack)) { + dev_err(tphy->dev, "DP tunnel: AUSPLL did not lock (cmd %#x, pclk status %#x)\n", + readl(core + T6040_CORE_AUSPLL_CMD), + readl(core + T6040_CORE_DP_PCLK_STATUS)); + t6040_tun_pll_stop(tphy); + return -ETIMEDOUT; + } + t6040_usb2_set32(core + T6040_CORE_TX_DP_CTRL0, 0xc); + return 0; +} + +static void t6040_tun_select_field(u32 *v, const struct t6040_tun_pclk *p, int sel, bool en) +{ + if (!!(*v & p->en) != en || ((*v >> p->sel_shift) & 0xf) != sel) + *v = (*v & ~(p->en | (0xf << p->sel_shift))) | (sel << p->sel_shift); +} + +/* The DP 1.4 tunnel update for one pclk: select with its enable off, then the enables. */ +static void t6040_tun_select_write(struct t6040_usb2_phy *tphy, unsigned int dpin, int sel) +{ + void __iomem *ctrl0 = t6040_core(tphy) + T6040_CORE_TX_DP_CTRL0; + u32 v = readl(ctrl0); + + t6040_tun_select_field(&v, &t6040_tun_pclk[dpin], sel, !!tphy->tun_rate[dpin]); + /* PCLK1 is DP IN 1's prerequisite at its own rate, unless DP IN 0 runs on it. */ + if (dpin == 1 && !tphy->tun_rate[0]) + t6040_tun_select_field(&v, &t6040_tun_pclk[0], sel, true); + writel(v, ctrl0); + writel((v & ~T6040_CORE_TX_DP_CTRL0_ENABLES) | t6040_tun_enables(tphy), ctrl0); +} + +/* The DP clock configuration for one DP 1.x switch: pixel-clock gate on, or off. */ +static int t6040_tun_switch(struct t6040_usb2_phy *tphy, unsigned int dpin, bool on) +{ + const struct t6040_tun_pclk *p = &t6040_tun_pclk[dpin]; + void __iomem *cfg = t6040_cfg(tphy); + unsigned int i; + u32 s; + + if (on) { + t6040_usb2_mask32(cfg + T6040_CFG_DP_CLKSWITCH_CTRL, p->sw_mask, p->sw_gate); + return readl_poll_timeout(cfg + T6040_CFG_DP_CLKSWITCH_STAT, s, s & p->sw_on, + 10, 5 * USEC_PER_MSEC); + } + t6040_usb2_set32(cfg + T6040_CFG_DP_CLKSWITCH_CTRL, p->sw_gate); + for (i = 0; i < 2; i++) { + if (!(readl(cfg + T6040_CFG_DP_CLKSWITCH_CTRL) & p->req[i])) + continue; + t6040_usb2_clear32(cfg + T6040_CFG_DP_CLKSWITCH_CTRL, p->req[i]); + if (readl_poll_timeout(cfg + T6040_CFG_DP_CLKSWITCH_STAT, s, !(s & p->req_stat[i]), + 10, 5 * USEC_PER_MSEC)) + dev_dbg(tphy->dev, "DP tunnel %u: request %#x not released (%#x)\n", + dpin, p->req[i], s); + } + t6040_usb2_clear32(cfg + T6040_CFG_DP_CLKSWITCH_CTRL, p->sw_off); + return 0; +} + +/* All tunnel clocks down, which the PHY's shutdown of mode 2 starts with. */ +static void t6040_tun_stop_all_locked(struct t6040_usb2_phy *tphy) +{ + unsigned int dpin; + + lockdep_assert_held(&tphy->lock); + if (!tphy->tun_pll) + return; + for (dpin = 0; dpin < 2; dpin++) { + if (!tphy->tun_rate[dpin]) + continue; + tphy->tun_rate[dpin] = 0; + t6040_tun_switch(tphy, dpin, false); + } + if (tphy->tun_pre) { + tphy->tun_pre = false; + t6040_tun_switch(tphy, 0, false); + } + t6040_tun_pll_stop(tphy); + dev_info(tphy->dev, "DP tunnel clocks off\n"); +} + +static int t6040_tun_rate_locked(struct t6040_usb2_phy *tphy, unsigned int dpin, u8 rate) +{ + int sel, ret; + u8 old = tphy->tun_rate[dpin]; + + lockdep_assert_held(&tphy->lock); + if (!rate) { + if (!old) + return 0; + tphy->tun_rate[dpin] = 0; + if (dpin == 0 && tphy->tun_rate[1]) { + /* DP IN 1 keeps PCLK1 and its switch, now at its own rate. */ + tphy->tun_pre = true; + t6040_tun_select_write(tphy, 1, t6040_tun_select(tphy->tun_rate[1])); + } else { + t6040_tun_switch(tphy, dpin, false); + if (dpin == 1 && tphy->tun_pre) { + tphy->tun_pre = false; + t6040_tun_switch(tphy, 0, false); + } + } + t6040_usb2_mask32(t6040_core(tphy) + T6040_CORE_TX_DP_CTRL0, + T6040_CORE_TX_DP_CTRL0_ENABLES, t6040_tun_enables(tphy)); + if (!tphy->tun_rate[!dpin]) + t6040_tun_pll_stop(tphy); + dev_dbg(tphy->dev, "DP tunnel %u off\n", dpin); + return 0; + } + sel = t6040_tun_select(rate); + if (sel < 0) + return sel; + if (!tphy->cio_up) + return -EBUSY; + if (old == rate) + return 0; + if (!tphy->tun_pll) { + ret = t6040_tun_pll_start(tphy); + if (ret) + return ret; + } + tphy->tun_rate[dpin] = rate; + t6040_tun_select_write(tphy, dpin, sel); + /* DP IN 1 alone: switch 1 passes PCLK1, its prerequisite. */ + if (dpin == 1 && !tphy->tun_rate[0] && !tphy->tun_pre) { + ret = t6040_tun_switch(tphy, 0, true); + if (ret) + goto switch_failed; + tphy->tun_pre = true; + } else if (dpin == 0) { + tphy->tun_pre = false; /* DP IN 0 owns switch 1 again */ + } + ret = t6040_tun_switch(tphy, dpin, true); +switch_failed: + if (ret) { + dev_err(tphy->dev, "DP tunnel %u: clock switch timed out (ctrl %#x status %#x)\n", + dpin, readl(t6040_cfg(tphy) + T6040_CFG_DP_CLKSWITCH_CTRL), + readl(t6040_cfg(tphy) + T6040_CFG_DP_CLKSWITCH_STAT)); + t6040_tun_rate_locked(tphy, dpin, 0); + return ret; + } + dev_info(tphy->dev, "DP tunnel %u: rate %#x, select %d (ctrl0 %#x, pclk status %#x)\n", + dpin, rate, sel, readl(t6040_core(tphy) + T6040_CORE_TX_DP_CTRL0), + readl(t6040_core(tphy) + T6040_CORE_DP_PCLK_STATUS)); + return 0; +} + +/* + * The pixel clock of a DP tunnel for appledrm, which looks it up with symbol_get(): start the + * clock of DP IN adapter @dpin (0 or 1) at DP link rate code @rate, or stop it with 0. The + * PHY must be in CIO. A symbol of its own, as for T6050: phy-apple-atc exports + * apple_atc_dp_tunnel_rate() for the older SoCs, and both can be built into one kernel. + */ +int apple_t6040_dp_tunnel_rate(struct phy *phy, unsigned int dpin, u8 rate) +{ + struct t6040_usb2_phy *tphy; + + if (!phy || phy->ops != &t6040_dp_phy_ops || dpin > 1) + return -EINVAL; + tphy = phy_get_drvdata(phy); + guard(mutex)(&tphy->lock); + return t6040_tun_rate_locked(tphy, dpin, rate); +} +EXPORT_SYMBOL_GPL(apple_t6040_dp_tunnel_rate); + /* ---- Type-C mode switch: the Type-C controller asks for DP on a USB-C port ---- */ static int t6040_dp_mux_set(struct typec_mux_dev *mux, struct typec_mux_state *state) diff --git a/drivers/thunderbolt/apple.c b/drivers/thunderbolt/apple.c index be0c647b30de4..12b85380c8c91 100644 --- a/drivers/thunderbolt/apple.c +++ b/drivers/thunderbolt/apple.c @@ -171,6 +171,8 @@ struct apple_cio_hw { bool gen4_link_recovery; /* Leave the host router's TMU to its firmware; host TMU constants. */ bool host_tmu; + /* Every router's TMU in bi-directional HiFi, as macOS runs it. */ + bool tmu_hifi_bi; const struct apple_dpin_policy *dp; }; @@ -245,7 +247,14 @@ static const struct apple_cio_hw apple_cio_t6040_hw = { .lstx_after_boot = true, .strict_fw_ready = true, .no_host_clx = true, - .dp = &apple_dpin_disabled, + /* + * macOS runs the host's TMU in bi-directional HiFi with a Studio + * Display attached, whose DP OUT adapter requires it (Min Required TMU + * Mode 3); without it the display drops HPD a second into 5K. + */ + .tmu_hifi_bi = true, + /* The T8132 (M4) DP IN flow; the PHY's AUSPLL is the pixel clock. */ + .dp = &apple_dpin_m4, }; #define APPLE_CIO_NHI_HOP_COUNT 0x0 @@ -630,6 +639,9 @@ static const struct of_device_id apple_dpin_qualified_soc[] = { { .compatible = "apple,t6021" }, { .compatible = "apple,t6030" }, { .compatible = "apple,t8132" }, + /* T6040/T6041 (M4 Pro/Max): M4 DP IN flow, PHY tunnel pixel clock */ + { .compatible = "apple,t6040" }, + { .compatible = "apple,t6041" }, /* T6050: t8103 DP IN handshake, M3 flow, four DP IN adapters */ { .compatible = "apple,t6050" }, {}, @@ -2706,6 +2718,8 @@ static int apple_nhi_probe(struct platform_device *pdev) anhi->nhi.quirks |= QUIRK_GEN4_LINK_RECOVERY; if (acio->hw->host_tmu) anhi->nhi.quirks |= QUIRK_HOST_TMU; + if (acio->hw->tmu_hifi_bi) + anhi->nhi.quirks |= QUIRK_TMU_HIFI_BI; /* * TEST: T6050 takes the vendor sleep and the state transfer as the J713 does (macOS, diff --git a/drivers/thunderbolt/nhi.h b/drivers/thunderbolt/nhi.h index 8770e398acb03..d4b9374c8d1e3 100644 --- a/drivers/thunderbolt/nhi.h +++ b/drivers/thunderbolt/nhi.h @@ -256,6 +256,12 @@ struct tb_nhi_ops { * and routers are enumerated with a 10 ms notification timeout. */ #define QUIRK_CIO_MACOS_FRAMES BIT(14) +/* + * The host router and every device router run their TMU in bi-directional + * HiFi mode, as macOS does: a DP OUT adapter may require it to recover its + * stream clock. + */ +#define QUIRK_TMU_HIFI_BI BIT(15) /* Host display policies are independent of which optional hooks are installed. */ #define TB_HOST_DP_NOTIFY BIT(0) diff --git a/drivers/thunderbolt/tb.c b/drivers/thunderbolt/tb.c index 074ce00efdf1c..d04c86e07a270 100644 --- a/drivers/thunderbolt/tb.c +++ b/drivers/thunderbolt/tb.c @@ -363,6 +363,24 @@ static int tb_enable_tmu(struct tb_switch *sw) { int ret; + /* + * Bi-directional HiFi everywhere (host router set in tb_start()). A + * device router may already read as bi-directional HiFi without the + * host's time: enable it from scratch with the time posted. + */ + if ((sw->tb->nhi->quirks & QUIRK_TMU_HIFI_BI) && tb_route(sw)) { + ret = tb_switch_tmu_configure(sw, TB_SWITCH_TMU_MODE_HIFI_BI); + if (ret) + return ret; + ret = tb_switch_tmu_disable(sw); + if (ret) + return ret; + ret = tb_switch_tmu_post_time(sw); + if (ret) + return ret; + return tb_switch_tmu_enable(sw); + } + /* The host router's firmware runs its TMU on these hosts. */ if (!tb_route(sw) && tb_switch_tmu_host_managed(sw)) return 0; @@ -3985,10 +4003,13 @@ static int tb_start(struct tb *tb, bool reset) /* * To support highest CLx state, we set host router's TMU to - * Normal mode. + * Normal mode, unless the host runs bi-directional HiFi. */ if (!tb_switch_tmu_host_managed(tb->root_switch)) { - tb_switch_tmu_configure(tb->root_switch, TB_SWITCH_TMU_MODE_LOWRES); + tb_switch_tmu_configure(tb->root_switch, + (tb->nhi->quirks & QUIRK_TMU_HIFI_BI) ? + TB_SWITCH_TMU_MODE_HIFI_BI : + TB_SWITCH_TMU_MODE_LOWRES); /* Enable TMU if it is off */ tb_switch_tmu_enable(tb->root_switch); } diff --git a/include/linux/soc/apple/dp-tunnel.h b/include/linux/soc/apple/dp-tunnel.h index 09afceeda7000..03399c9da3f2b 100644 --- a/include/linux/soc/apple/dp-tunnel.h +++ b/include/linux/soc/apple/dp-tunnel.h @@ -67,6 +67,12 @@ int apple_dcp_tb_dp_event(struct device_node *connector_np, unsigned int dpin, */ int apple_atc_dp_tunnel_rate(struct phy *phy, unsigned int dpin, u8 rate); +/* + * T6040 ATC PHY: the same for DP IN adapter @dpin (0 or 1), a symbol of its own because + * phy-apple-atc and phy-apple-t6040-usb2 can both be built. + */ +int apple_t6040_dp_tunnel_rate(struct phy *phy, unsigned int dpin, u8 rate); + /* T6030's external DPTX port 0 sources feed DP IN0/1 through PCLK1/2. */ static inline int apple_dp_tunnel_t6030_pclk(unsigned int source, unsigned int dpin) { diff --git a/tools/asahi/t6030-clock-tests.py b/tools/asahi/t6030-clock-tests.py index 1c26aee6b8718..77bea106500df 100644 --- a/tools/asahi/t6030-clock-tests.py +++ b/tools/asahi/t6030-clock-tests.py @@ -21,14 +21,14 @@ 'atc_t8122_tunnel_stop', 'atc_t8122_tunnel_pclk', 'atc_t8122_tunnel_start', 'apple_atc_dp_tunnel_rate_source'], 'drivers/gpu/drm/apple/dcp-fabric.c': [ - 'dcp_route_clock_rate', 'dcp_tunnel_set_rate'], + 'dcp_fabric_t6040_tunnel_clock', 'dcp_route_clock_rate', 'dcp_tunnel_set_rate'], } CASES = ['dual-start', 'reverse-start', 'stop-second', 'same-rate', 'stale-owner', 'same-source', 'retune-one', 'busy-mode', 'busy-pll', 'invalid-source', 'invalid-rate', 'wrong-generation', 'failed-cleanup', 'failed-final-stop', 'stale-ack', 'unflagged-ack'] CASES += ['fail-start-' + str(i) for i in range(4)] -CASES += ['t6050-dpin-clocks'] +CASES += ['t6050-dpin-clocks', 't6040-tunnel-clock'] def function(source, name): match = re.search(r'^(?:static\s+)?(?:inline\s+)?(?:unsigned\s+)?(?:int|void|bool)\s+' + name + diff --git a/tools/asahi/t6030-clock/fixture.c b/tools/asahi/t6030-clock/fixture.c index 3efdc838fe8d1..ce276bd92c831 100644 --- a/tools/asahi/t6030-clock/fixture.c +++ b/tools/asahi/t6030-clock/fixture.c @@ -138,6 +138,18 @@ static int apple_t6050_dp_tunnel_clock(struct phy *p, unsigned clock, u8 rate) t6050_rate = rate; return t6050_error; } +/* T6040 ATC PHY (t6040-usb2.c): one pixel clock per DP IN adapter (0 or 1), recorded. */ +static unsigned t6040_calls, t6040_dpin; +static u8 t6040_rate; +static int t6040_error; +static int apple_t6040_dp_tunnel_rate(struct phy *p, unsigned dpin, u8 rate) +{ + assert(!strcmp(machine, "apple,t6040") || !strcmp(machine, "apple,t6041")); + t6040_calls++; + t6040_dpin = dpin; + t6040_rate = rate; + return t6040_error; +} /* * Production: of_device_is_compatible(dcp->dev->of_node, "apple,t8132-dcpext"). * These are T6030 DCPs: the T8132 dcpext paths must never be taken. @@ -275,6 +287,30 @@ int main(int argc, char **argv) printf("PASS %s: DP IN n on tunnel clock n + 1, rate kept only when set\n", name); return 0; } + if (!strcmp(name, "t6040-tunnel-clock")) { + /* T6040 and T6041 use the PHY's own symbol, not the ATC one nor the T6050 one. */ + const char *const machines[] = { "apple,t6040", "apple,t6041" }; + u8 route_rate; + for (unsigned m = 0; m < 2; m++) { + machine = machines[m]; + t6040_calls = 0; + t6040_error = 0; + for (unsigned dpin = 0; dpin < 2; dpin++) { + assert(!t6050_clock_call(&phy, dpin, 0x1e, &route_rate)); + assert(t6040_dpin == dpin && t6040_rate == 0x1e && route_rate == 0x1e); + assert(!t6050_clock_call(&phy, dpin, 0, &route_rate)); + assert(t6040_dpin == dpin && !t6040_rate && !route_rate); + } + assert(t6040_calls == 4); + /* A clock that failed to start leaves the route's rate alone. */ + t6040_error = -ETIMEDOUT; + assert(t6050_clock_call(&phy, 1, 0x14, &route_rate) == -ETIMEDOUT && + route_rate == 0x55); + } + assert(!t6050_calls && !writes && !polls && !crossbar_stops); + printf("PASS %s: T6040/T6041 tunnel clock through apple_t6040_dp_tunnel_rate\n", name); + return 0; + } if (!strcmp(name, "wrong-generation")) { hw.gen = ATCPHY_GENERATION_T8103; assert(clock_call(&phy, 0, 0, 0x14) == -EOPNOTSUPP && !writes);