Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion drivers/gpu/drm/apple/dcp-internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,8 @@ struct apple_dcp_hw_data {
* The endpoint the DPTX port services are announced on, where it is not
* the firmware's default (DPTX_ENDPOINT_V2 on firmware 27): the T8142
* dcpext announces them on DPTX_ENDPOINT. 0: the default, or on a
* firmware-27 dcpext what its firmware advertises (dcp_v27_dptx_layout()).
* firmware-27 dcpext or DCP with Type-C routes what its firmware
* advertises (dcp_v27_dptx_layout()).
*/
u8 dptx_endpoint;
/*
Expand Down
7 changes: 5 additions & 2 deletions drivers/gpu/drm/apple/dcp.c
Original file line number Diff line number Diff line change
Expand Up @@ -4657,15 +4657,18 @@ static int dcp_ext26_start_services(struct apple_dcp *dcp)
* whose firmware advertises both of those gets the controller and its ports
* there.
*
* A primary DCP can also serve external ports when the device tree connects
* Type-C display routes to it.
*
* Decided here, not at probe: RTKit is woken at bind, and its endpoint map is
* known only from then on. Hardware data that names an endpoint keeps it:
* T8142's dcpext (the same layout), and the T6050 and firmware-27 T6041
* dcpexts, whose ports have been opened on DPTX_ENDPOINT_V2 so far.
*/
static void dcp_v27_dptx_layout(struct apple_dcp *dcp)
{
if (dcp->fw_compat != DCP_FIRMWARE_V_27_0 || !dcp->dcpext ||
dcp->hw.dptx_endpoint)
if (dcp->fw_compat != DCP_FIRMWARE_V_27_0 ||
(!dcp->dcpext && !dcp->nr_typec_routes) || dcp->hw.dptx_endpoint)
return;
if (!apple_rtkit_has_endpoint(dcp->rtk, DPTX_CONTROLLER_ENDPOINT) ||
!apple_rtkit_has_endpoint(dcp->rtk, DPTX_ENDPOINT))
Expand Down
12 changes: 7 additions & 5 deletions drivers/gpu/drm/apple/dptxep.c
Original file line number Diff line number Diff line change
Expand Up @@ -1667,13 +1667,15 @@ int dptxep_init(struct apple_dcp *dcp)
* up so a later announce can still bind.
*/
/*
* A firmware-27 external processor whose ports are not
* on this endpoint announces nothing here either (a 27.2
* dcpext on DPTX_ENDPOINT_V2 where its firmware does not
* advertise DPTX_ENDPOINT, see dcp_v27_dptx_layout()).
* A firmware-27 external processor, or a firmware-27
* DCP with Type-C routes, whose ports are not on this
* endpoint announces nothing here either (a 27.2 DCP on
* DPTX_ENDPOINT_V2 where its firmware does not advertise
* DPTX_ENDPOINT, see dcp_v27_dptx_layout()).
*/
if (dcp_fw_is_14_7(dcp) || dcp_is_ext26(dcp) ||
(dcp->fw_compat == DCP_FIRMWARE_V_27_0 && dcp->dcpext)) {
(dcp->fw_compat == DCP_FIRMWARE_V_27_0 &&
(dcp->dcpext || dcp->nr_typec_routes))) {
dev_info(dcp->dev,
"DPTX port %u not announced yet\n", port);
return 0;
Expand Down
15 changes: 12 additions & 3 deletions drivers/phy/apple/atc.c
Original file line number Diff line number Diff line change
Expand Up @@ -4949,9 +4949,18 @@ static int atcphy_dpphy_configure(struct phy *phy, union phy_configure_opts *opt
if (atcphy->hw->dp_t8122)
return atcphy_dpphy_configure_t8122(atcphy, opts);

if (opts->set_voltages)
return -EINVAL;
if (opts->set_lanes)
/*
* These PHYs have no drive level control: the lanes keep the levels
* their tunables set up, as the T8132 branch above does. A drive-only
* request succeeds, so DCP's link training goes on with those levels;
* drive levels together with a rate change are not a request DCP makes.
*/
if (opts->set_voltages && !opts->set_rate && !opts->set_lanes) {
dev_dbg(atcphy->dev, "DP drive request ignored (lane 0 swing %u pre %u)\n",
opts->voltage[0], opts->pre[0]);
return 0;
}
if (opts->set_voltages || opts->set_lanes)
return -EINVAL;

if (opts->set_rate) {
Expand Down
155 changes: 155 additions & 0 deletions tools/asahi/atc-dp-drive-tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
#!/usr/bin/env python3
# SPDX-License-Identifier: GPL-2.0-only OR MIT
"""How the ATC PHY answers a DP drive level request, and what the firmware-27 drive call does with it.

Builds the production atcphy_dpphy_configure() against stubs and runs it over
PHY generation, DP state and the request's set_voltages/set_rate/set_lanes. A
PHY without drive level control (the T8103 generation: T8103, T8112, T600x,
T602x) accepts a drive-only request and keeps the levels its tunables set, as
the T8132 branch does; a drive request together with a rate or lane change is
refused. The firmware-27 SetDriveSettings handler passes every PHY error back
to DCP (no errno is special), so a real failure still fails the call.

--mutation names a change to the production source that the matrix must kill.
"""
import argparse
import re
import subprocess
import sys
import tempfile
from pathlib import Path

MUTATIONS = {
# The PR's first version: a drive-only request answered -EOPNOTSUPP.
'eopnotsupp': ('\t\treturn 0;\n\t}\n\tif (opts->set_voltages || opts->set_lanes)',
'\t\treturn -EOPNOTSUPP;\n\t}\n\tif (opts->set_voltages || opts->set_lanes)'),
# Accept drive levels that come with a rate change.
'accept-with-rate': ('opts->set_voltages && !opts->set_rate && !opts->set_lanes',
'opts->set_voltages && !opts->set_lanes'),
# The pre-PR refusal of every drive request.
'refuse-drive': ('opts->set_voltages && !opts->set_rate && !opts->set_lanes', '0'),
}

parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument('--mutation', choices=sorted(MUTATIONS))
args = parser.parse_args()

root = Path(__file__).resolve().parents[2]
atc_c = (root / 'drivers/phy/apple/atc.c').read_text()
drive_inc = (root / 'drivers/gpu/drm/apple/dptx-drive-v27.inc').read_text()


def function(source, marker):
start = source.index(marker)
opening = source.index('{', start)
end, depth = opening + 1, 1
while depth:
depth += (source[end] == '{') - (source[end] == '}')
end += 1
return source[start:end]


configure = function(atc_c, 'static int atcphy_dpphy_configure(struct phy *phy,')
# Selected by the PHY's generation and hardware flags, never by a compatible.
assert 'compatible' not in configure and 'of_machine' not in configure

# The firmware-27 drive call returns every PHY error to DCP unchanged.
drive = function(drive_inc, 'static int dptxport_call_drive_v27(')
call = drive[drive.index('ret = phy_configure(dptx->atcphy, &opts);'):]
assert re.match(r'ret = phy_configure\(dptx->atcphy, &opts\);\s*if \(ret\)\s*return ret;', call), \
'dptxport_call_drive_v27() must pass every phy_configure() error back'
assert 'EOPNOTSUPP' not in drive

if args.mutation:
old, new = MUTATIONS[args.mutation]
assert configure.count(old) == 1, args.mutation
configure = configure.replace(old, new)

program = r'''
#include <assert.h>
#include <errno.h>
#include <stdbool.h>
#include <stdio.h>
typedef unsigned int u32;
enum atcphy_dp_link_rate { ATCPHY_DP_LINK_RATE_RBR, ATCPHY_DP_LINK_RATE_HBR, ATCPHY_DP_LINK_RATE_HBR2,
ATCPHY_DP_LINK_RATE_HBR3 };
enum { ATCPHY_GENERATION_T8103, ATCPHY_GENERATION_T8122, ATCPHY_GENERATION_T8132 };
enum { APPLE_ATCPHY_MODE_OFF, APPLE_ATCPHY_MODE_DP, APPLE_ATCPHY_MODE_USB3_DP };
struct phy_configure_opts_dp { u32 link_rate, lanes, voltage[4], pre[4]; bool set_rate, set_lanes, set_voltages; };
union phy_configure_opts { struct phy_configure_opts_dp dp; };
struct hw { int gen; bool has_t8140_dp, dp_t8122; };
struct apple_atcphy { const struct hw *hw; void *dev; int mode; int lock; };
struct phy { struct apple_atcphy *a; };
static struct apple_atcphy *phy_get_drvdata(struct phy *p) { return p->a; }
static bool dp_on;
static int calls_rate, calls_other, last_rate = -1;
static bool atcphy_t8132_dp_on(struct apple_atcphy *a) { return dp_on; }
static int j700_dp_configure(struct apple_atcphy *a, struct phy_configure_opts_dp *o) { calls_other++; return 0; }
static int atcphy_dpphy_configure_t8122(struct apple_atcphy *a, struct phy_configure_opts_dp *o) { calls_other++; return 0; }
static void atcphy_restore_after_pd_off(struct apple_atcphy *a) { }
static int atcphy_dp_set_rate_t8122(struct apple_atcphy *a, u32 r) { calls_other++; return 0; }
static int atcphy_dp_configure(struct apple_atcphy *a, enum atcphy_dp_link_rate lr) { calls_rate++; last_rate = lr; return 0; }
#define guard(x) (void)
#define dev_dbg(...) do { } while (0)
#define dev_err(...) do { } while (0)
''' + configure + r'''
int main(void)
{
static const struct hw t8103 = { ATCPHY_GENERATION_T8103 };
struct apple_atcphy a = { .hw = &t8103, .mode = APPLE_ATCPHY_MODE_DP };
struct phy p = { &a };
unsigned cases = 0;

for (unsigned v = 0; v < 2; v++)
for (unsigned r = 0; r < 2; r++)
for (unsigned l = 0; l < 2; l++) {
union phy_configure_opts o = { .dp = { .set_voltages = v, .set_rate = r, .set_lanes = l,
.link_rate = 5400, .voltage = { 2 }, .pre = { 1 } } };
int ret;

calls_rate = calls_other = 0;
ret = atcphy_dpphy_configure(&p, &o);
if (v && !r && !l)
/* A drive-only request: accepted, the PHY is not touched. */
assert(ret == 0 && !calls_rate && !calls_other);
else if (v || l)
/* Drive levels with a rate or lanes, or lanes at all: refused, nothing done. */
assert(ret == -EINVAL && !calls_rate && !calls_other);
else if (r)
assert(ret == 0 && calls_rate == 1 && last_rate == ATCPHY_DP_LINK_RATE_HBR2);
else
assert(ret == 0 && !calls_rate && !calls_other);
cases++;
}
/* An unknown rate is still refused. */
union phy_configure_opts bad = { .dp = { .set_rate = true, .link_rate = 4321 } };
assert(atcphy_dpphy_configure(&p, &bad) == -EINVAL);

/* T8132 keeps its own answers: DP off is -EOPNOTSUPP, a drive-only request with DP on is 0. */
static const struct hw t8132 = { ATCPHY_GENERATION_T8132 };
a.hw = &t8132;
union phy_configure_opts drv = { .dp = { .set_voltages = true } };
dp_on = false;
assert(atcphy_dpphy_configure(&p, &drv) == -EOPNOTSUPP);
dp_on = true;
assert(atcphy_dpphy_configure(&p, &drv) == 0);
/* T8122-class PHYs go to their own handler. */
static const struct hw t8122 = { ATCPHY_GENERATION_T8122, .dp_t8122 = true };
a.hw = &t8122;
calls_other = 0;
assert(atcphy_dpphy_configure(&p, &drv) == 0 && calls_other == 1);
printf("%u cases\n", cases);
return 0;
}
'''

with tempfile.TemporaryDirectory() as tmp:
src = Path(tmp) / 'test.c'
src.write_text(program)
exe = src.with_suffix('')
subprocess.run(['cc', '-Werror', '-Wall', '-Wno-unused-function', '-Wno-unused-value',
'-fsanitize=address,undefined', str(src), '-o', str(exe)], check=True)
run = subprocess.run([str(exe)])
if run.returncode:
sys.exit(f'FAIL: the drive matrix exited {run.returncode}')
print('PASS ATC PHY without drive control accepts a drive-only DP request; firmware-27 drive call passes PHY errors back')
28 changes: 16 additions & 12 deletions tools/asahi/dcp-v27-dptx-layout-tests.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
#!/usr/bin/env python3
# SPDX-License-Identifier: GPL-2.0-only OR MIT
"""Where a firmware-27 dcpext opens its DPTX controller and ports.
"""Where a firmware-27 dcpext, or a DCP with Type-C routes, opens its DPTX controller and ports.

Builds the production dcp_v27_dptx_layout() and dptxep_init()'s endpoint
choice, and runs them over firmware, processor role, advertised endpoints and
hardware data. A firmware-27 dcpext whose firmware advertises both
DPTX_CONTROLLER_ENDPOINT (0x24) and DPTX_ENDPOINT (0x2a) gets the controller
and its ports there (J413 and J615 on 27.2); hardware data that names an
hardware data. A firmware-27 dcpext, or a firmware-27 DCP that the device tree
connects to Type-C display routes (the M2 Mac mini's primary), whose firmware
advertises both DPTX_CONTROLLER_ENDPOINT (0x24) and DPTX_ENDPOINT (0x2a) gets
the controller and its ports there (J413 and J615 on 27.2); hardware data that names an
endpoint keeps it (T8142, T6050 and firmware-27 T6041 dcpexts); every other
processor is unchanged. The selector must not test a compatible.

Expand All @@ -24,9 +25,11 @@
'ignore-advertisement': ('\tif (!apple_rtkit_has_endpoint(dcp->rtk, DPTX_CONTROLLER_ENDPOINT) ||\n'
'\t !apple_rtkit_has_endpoint(dcp->rtk, DPTX_ENDPOINT))\n\t\treturn;\n', ''),
# Override hardware data that already names an endpoint.
'ignore-pinned': (' ||\n\t dcp->hw.dptx_endpoint)\n', ')\n'),
# Apply to internal processors too.
'ignore-dcpext': (' || !dcp->dcpext', ''),
'ignore-pinned': (' || dcp->hw.dptx_endpoint)\n', ')\n'),
# Apply to every processor, with or without Type-C routes.
'ignore-dcpext': ('(!dcp->dcpext && !dcp->nr_typec_routes) || ', ''),
# Leave a DCP with Type-C routes on the default endpoints.
'ignore-typec-routes': ('(!dcp->dcpext && !dcp->nr_typec_routes)', '!dcp->dcpext'),
}

parser = argparse.ArgumentParser(description=__doc__)
Expand Down Expand Up @@ -93,7 +96,7 @@ def table(name):
#define DPTX_CONTROLLER_ENDPOINT 0x24
#define DPTX_ENDPOINT 0x2a
#define DPTX_ENDPOINT_V2 0x2c
struct apple_dcp { int fw_compat; bool dcpext, ext26; void *rtk, *dev;
struct apple_dcp { int fw_compat; bool dcpext, ext26; u32 nr_typec_routes; void *rtk, *dev;
struct { u8 dptx_endpoint; bool dptx_controller; int iomfb_method_profile; } hw; };
static u32 advertised;
static bool apple_rtkit_has_endpoint(void *rtk, int ep) { return advertised & (1u << (ep - 0x20)); }
Expand Down Expand Up @@ -122,12 +125,13 @@ def table(name):
for (unsigned ext = 0; ext < 2; ext++)
for (unsigned m = 0; m < 6; m++)
for (unsigned p = 0; p < 3; p++)
for (unsigned c = 0; c < 2; c++) {
struct apple_dcp d = { .fw_compat = fws[f], .dcpext = ext,
for (unsigned c = 0; c < 2; c++)
for (unsigned r = 0; r < 2; r++) {
struct apple_dcp d = { .fw_compat = fws[f], .dcpext = ext, .nr_typec_routes = 2 * r,
.hw.dptx_endpoint = pins[p], .hw.dptx_controller = c };
struct apple_dcp before = d;
bool both = (maps[m] & BIT(0x24)) && (maps[m] & BIT(0x2a));
bool chosen = fws[f] == DCP_FIRMWARE_V_27_0 && ext && !pins[p] && both;
bool chosen = fws[f] == DCP_FIRMWARE_V_27_0 && (ext || r) && !pins[p] && both;

advertised = maps[m];
info_lines = 0;
Expand Down Expand Up @@ -175,4 +179,4 @@ def table(name):
run = subprocess.run([str(exe)])
if run.returncode:
sys.exit(f'FAIL: the layout matrix exited {run.returncode}')
print('PASS firmware-27 dcpext DPTX layout from its advertised endpoints; pinned and other processors unchanged')
print('PASS firmware-27 dcpext / Type-C-routed DCP DPTX layout from its advertised endpoints; pinned and other processors unchanged')
5 changes: 4 additions & 1 deletion tools/asahi/run-host-tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,11 @@ def mutations(flag, names, base, expect=KILL):

'dcpext26-startup-tests.py': [([], PASS)],
'iomfb-startup-tests.py': [([], PASS)],
'atc-dp-drive-tests.py': [([], PASS)] + mutations(
'--mutation', ['eopnotsupp', 'accept-with-rate', 'refuse-drive'], []),
'dcp-v27-dptx-layout-tests.py': [([], PASS)] + mutations(
'--mutation', ['ignore-advertisement', 'ignore-pinned', 'ignore-dcpext'], []),
'--mutation', ['ignore-advertisement', 'ignore-pinned', 'ignore-dcpext',
'ignore-typec-routes'], []),
'dcpext26-wire-tests.py': [([], PASS)],
'pmp-native26-tests.py': [([], PASS)],

Expand Down
Loading