Skip to content
13 changes: 13 additions & 0 deletions arch/arm64/boot/dts/apple/t8122-j615.dts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,21 @@
/dts-v1/;

#include "t8122.dtsi"
#include "t8122-usb4.dtsi"
#include "t8122-j613-j615.dtsi"

/ {
compatible = "apple,j615", "apple,t8122", "apple,arm-platform";
model = "Apple MacBook Air (15-inch, M3, 2024)";

aliases {
usb4-0-pcie = &usb4_0_pcie;
usb4-0-pcie-port = &usb4_0_pcie_port;
usb4-1-pcie = &usb4_1_pcie;
usb4-1-pcie-port = &usb4_1_pcie_port;
usb4-0-pcie-adapter = &usb4_0_acio;
usb4-1-pcie-adapter = &usb4_1_acio;
};
};

&dcp {
Expand Down Expand Up @@ -152,3 +162,6 @@
/* Per-device factory data from this Mac's own iBoot system container. */
firmware-name = "apple/mesacal-j615.bin";
};

/* Older Stage 2 loaders prune the shared ATC0 suppliers of this unused node. */
/delete-node/ &usb_otg0;
2 changes: 2 additions & 0 deletions arch/arm64/boot/dts/apple/t8122-usb4.dtsi
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
interrupt-parent = <&aic>;
interrupts = <AIC_IRQ 962 IRQ_TYPE_LEVEL_HIGH>;
#iommu-cells = <1>;
power-domains = <&ps_atc0_cio_pcie>;
/* No bootloader fills this one; the tunnel's cold init needs it. */
apple,tunable =
<0x20c 0x800001b7 0x800001b7>,
Expand Down Expand Up @@ -115,6 +116,7 @@
interrupt-parent = <&aic>;
interrupts = <AIC_IRQ 1020 IRQ_TYPE_LEVEL_HIGH>;
#iommu-cells = <1>;
power-domains = <&ps_atc1_cio_pcie>;
/* No bootloader fills this one; the tunnel's cold init needs it. */
apple,tunable =
<0x20c 0x800001b7 0x800001b7>,
Expand Down
10 changes: 8 additions & 2 deletions drivers/thunderbolt/apple.c
Original file line number Diff line number Diff line change
Expand Up @@ -4644,9 +4644,13 @@ static int apple_cio_probe(struct platform_device *pdev)
acio->dev = &pdev->dev;
acio->np = dev->of_node;
acio->hw = of_device_get_match_data(dev);
/* Only J613 has qualified the T8122 router policy and resources. */
/*
* The T8122 router policy starts the router through its MxWrap CPU
* block. A device tree (or an older loader's handoff tree) that does
* not describe that block keeps the T8103 policy.
*/
if (acio->hw == &apple_cio_t8122_hw &&
!of_machine_is_compatible("apple,j613"))
of_property_match_string(acio->np, "reg-names", "cpu") < 0)
acio->hw = &apple_cio_t8103_hw;
if (!acio->hw)
return -EINVAL;
Expand Down Expand Up @@ -4857,6 +4861,8 @@ static int apple_cio_probe(struct platform_device *pdev)
.fwnode = pdev->dev.fwnode,
.set = apple_cio_tbt_switch_set,
.drvdata = acio,
/* A strict power cycle is ordered by the PD driver (sleep and shutdown). */
.pd_ordered_stop = acio->hw->strict_power_cycle,
};
/* A consumer cannot start a host before its failure listener is ready. */
scoped_guard(mutex, &acio->lock) {
Expand Down
19 changes: 19 additions & 0 deletions drivers/usb/typec/mux.c
Original file line number Diff line number Diff line change
Expand Up @@ -891,6 +891,24 @@ bool typec_thunderbolt_switch_is_live(struct typec_thunderbolt_switch *sw)
}
EXPORT_SYMBOL_GPL(typec_thunderbolt_switch_is_live);

/**
* typec_thunderbolt_switch_pd_ordered_stop - Whether the port driver stops the switch
* @sw: USB4/Thunderbolt switch
*
* Returns true if the bound provider asked the Type-C port driver to stop
* it before tasks freeze and at shutdown (see struct
* typec_thunderbolt_switch_desc), false for any other or no provider.
*/
bool typec_thunderbolt_switch_pd_ordered_stop(struct typec_thunderbolt_switch *sw)
{
if (IS_ERR_OR_NULL(sw))
return false;

guard(rwsem_read)(&sw->sw_dev->set_lock);
return sw->sw_dev->set && sw->sw_dev->pd_ordered_stop;
}
EXPORT_SYMBOL_GPL(typec_thunderbolt_switch_pd_ordered_stop);

/**
* typec_thunderbolt_switch_set - Forward cable details to USB4/Thunderbolt switch
* @sw: USB4/Thunderbolt switch
Expand Down Expand Up @@ -1057,6 +1075,7 @@ typec_thunderbolt_switch_register(struct device *parent,
init_rwsem(&sw_dev->set_lock);
sw_dev->owner = parent->driver ? parent->driver->owner : NULL;
sw_dev->set = desc->set;
sw_dev->pd_ordered_stop = desc->pd_ordered_stop;

device_initialize(&sw_dev->dev);
sw_dev->dev.parent = parent;
Expand Down
1 change: 1 addition & 0 deletions drivers/usb/typec/mux.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ struct typec_thunderbolt_switch_dev {
struct module *owner;
typec_thunderbolt_switch_set_fn_t set;
struct atomic_notifier_head notifiers;
bool pd_ordered_stop;
};

#define to_typec_thunderbolt_switch_dev(_dev_) \
Expand Down
76 changes: 72 additions & 4 deletions drivers/usb/typec/tipd/cd321x-tbt-test.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <linux/module.h>
#include <linux/property.h>
#include <linux/reboot.h>
#include <linux/suspend.h>
#include <linux/usb/typec_mux.h>
#include <linux/workqueue.h>

Expand Down Expand Up @@ -285,13 +286,15 @@ static struct cd321x *cd321x_tbt_test_late_port(struct kunit *test)
return cd;
}

static void cd321x_tbt_test_add_router(struct kunit *test,
const struct software_node *node)
static void cd321x_tbt_test_add_router_ordered(struct kunit *test,
const struct software_node *node,
bool pd_ordered_stop)
{
struct typec_thunderbolt_switch_desc desc = {
.fwnode = software_node_fwnode(node),
.set = cd321x_tbt_test_set,
.name = node->name,
.pd_ordered_stop = pd_ordered_stop,
};
struct typec_thunderbolt_switch_dev *sw_dev;
struct device *router;
Expand All @@ -306,6 +309,12 @@ static void cd321x_tbt_test_add_router(struct kunit *test,
KUNIT_ASSERT_EQ(test, ret, 0);
}

static void cd321x_tbt_test_add_router(struct kunit *test,
const struct software_node *node)
{
cd321x_tbt_test_add_router_ordered(test, node, false);
}

/* A router that registers after the port gave up is taken; the cable is reread. */
static void cd321x_tbt_router_binds_late_test(struct kunit *test)
{
Expand Down Expand Up @@ -414,22 +423,27 @@ static void cd321x_tbt_router_rebinds_test(struct kunit *test)
}

/* A port with a live router, as the cable events find it. */
static struct cd321x *cd321x_tbt_test_live_port(struct kunit *test)
static struct cd321x *cd321x_tbt_test_port(struct kunit *test, bool pd_ordered_stop)
{
struct cd321x *cd = cd321x_tbt_test_late_port(test);
int ret;

ret = cd321x_tbt_watch_late(cd, software_node_fwnode(&cd321x_test_connector));
KUNIT_ASSERT_EQ(test, ret, 0);
flush_work(&cd->tbt_attach_work);
cd321x_tbt_test_add_router(test, &cd321x_test_router);
cd321x_tbt_test_add_router_ordered(test, &cd321x_test_router, pd_ordered_stop);
flush_work(&cd->tbt_attach_work);
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, cd->tbt_switch);
scoped_guard(mutex, &cd->tps.lock)
cd->pm.phase = CD321X_PM_RUNNING;
return cd;
}

static struct cd321x *cd321x_tbt_test_live_port(struct kunit *test)
{
return cd321x_tbt_test_port(test, false);
}

/* A reboot closes the router's session at once and keeps cable events away. */
static void cd321x_tbt_reboot_closes_session_test(struct kunit *test)
{
Expand All @@ -454,6 +468,57 @@ static void cd321x_tbt_reboot_other_action_test(struct kunit *test)
KUNIT_EXPECT_TRUE(test, cd321x_pm_can_update(&cd->pm));
}

/* A router that stops on its own is not stopped by the port before sleep. */
static void cd321x_tbt_sleep_router_stops_itself_test(struct kunit *test)
{
struct cd321x_tbt_test *ctx = test->priv;
struct cd321x *cd = cd321x_tbt_test_live_port(test);

KUNIT_EXPECT_EQ(test, cd321x_session_pm(&cd->session_pm, PM_SUSPEND_PREPARE, NULL),
NOTIFY_DONE);
KUNIT_EXPECT_EQ(test, ctx->set_calls, 0);
KUNIT_EXPECT_EQ(test, cd321x_session_pm(&cd->session_pm, PM_POST_SUSPEND, NULL),
NOTIFY_OK);
KUNIT_EXPECT_EQ(test, ctx->set_calls, 0);
}

/* A router that asks for it is put to sleep by the port and woken after. */
static void cd321x_tbt_sleep_router_ordered_test(struct kunit *test)
{
struct cd321x_tbt_test *ctx = test->priv;
struct cd321x *cd = cd321x_tbt_test_port(test, true);

KUNIT_EXPECT_EQ(test, cd321x_session_pm(&cd->session_pm, PM_SUSPEND_PREPARE, NULL),
NOTIFY_OK);
KUNIT_EXPECT_EQ(test, ctx->set_calls, 1);
KUNIT_EXPECT_EQ(test, ctx->last_state, TYPEC_THUNDERBOLT_SWITCH_SLEEP);
/* The port is gated for sleep; there is no controller to reread after it. */
scoped_guard(mutex, &cd->tps.lock) {
KUNIT_EXPECT_EQ(test, cd->pm.phase, CD321X_PM_PREPARED);
cd->pm.phase = CD321X_PM_INITIALIZING;
}
KUNIT_EXPECT_EQ(test, cd321x_session_pm(&cd->session_pm, PM_POST_SUSPEND, NULL),
NOTIFY_OK);
KUNIT_EXPECT_EQ(test, ctx->set_calls, 2);
KUNIT_EXPECT_EQ(test, ctx->last_state, TYPEC_THUNDERBOLT_SWITCH_WAKE);
}

/* A plain TPS6598x has no struct cd321x around it: its shutdown stops nothing. */
static void cd321x_tbt_shutdown_plain_port_test(struct kunit *test)
{
static const struct tipd_data plain = {
.tps_struct_size = sizeof(struct tps6598x),
};
struct cd321x_tbt_test *ctx = test->priv;
struct tps6598x *tps = kunit_kzalloc(test, sizeof(*tps), GFP_KERNEL);

KUNIT_ASSERT_NOT_NULL(test, tps);
tps->data = &plain;
mutex_init(&tps->lock);
tipd_shutdown(tps);
KUNIT_EXPECT_EQ(test, ctx->set_calls, 0);
}

static struct kunit_case cd321x_tbt_cases[] = {
KUNIT_CASE(cd321x_tbt_no_router_test),
KUNIT_CASE(cd321x_tbt_router_bound_test),
Expand All @@ -466,6 +531,9 @@ static struct kunit_case cd321x_tbt_cases[] = {
KUNIT_CASE(cd321x_tbt_router_rebinds_test),
KUNIT_CASE(cd321x_tbt_reboot_closes_session_test),
KUNIT_CASE(cd321x_tbt_reboot_other_action_test),
KUNIT_CASE(cd321x_tbt_sleep_router_stops_itself_test),
KUNIT_CASE(cd321x_tbt_sleep_router_ordered_test),
KUNIT_CASE(cd321x_tbt_shutdown_plain_port_test),
{}
};

Expand Down
42 changes: 31 additions & 11 deletions drivers/usb/typec/tipd/core.c
Original file line number Diff line number Diff line change
Expand Up @@ -2619,34 +2619,45 @@ static void cd321x_session_restore(struct cd321x *cd)
cd321x_complete(&cd->tps);
}

static int cd321x_session_pm(struct notifier_block *nb, unsigned long event, void *unused)
VISIBLE_IF_KUNIT int cd321x_session_pm(struct notifier_block *nb, unsigned long event, void *unused)
{
struct cd321x *cd = container_of(nb, struct cd321x, session_pm);
int ret;

switch (event) {
case PM_SUSPEND_PREPARE:
/* Only a switch that asked for it is stopped from here. */
scoped_guard(mutex, &cd->tps.lock)
cd->session_pm_armed =
typec_thunderbolt_switch_pd_ordered_stop(cd->tbt_switch);
if (!cd->session_pm_armed)
return NOTIFY_DONE;
ret = cd321x_session_stop(cd, true);
if (ret)
if (ret) {
cd321x_session_restore(cd);
/* The PM core skips our POST_SUSPEND after a failed PREPARE. */
cd->session_pm_armed = false;
}
return notifier_from_errno(ret);
case PM_POST_SUSPEND:
cd321x_session_restore(cd);
if (cd->session_pm_armed)
cd321x_session_restore(cd);
cd->session_pm_armed = false;
break;
}
return NOTIFY_OK;
}
EXPORT_SYMBOL_IF_KUNIT(cd321x_session_pm);

static void cd321x_ready(struct tps6598x *tps)
{
struct cd321x *cd321x = container_of(tps, struct cd321x, tps);

if (of_machine_is_compatible("apple,j613")) {
cd321x->session_pm.notifier_call = cd321x_session_pm;
cd321x->session_pm.priority = 100;
if (!register_pm_notifier(&cd321x->session_pm))
cd321x->session_pm_registered = true;
}
/* The switch may bind later; each suspend asks it again. */
cd321x->session_pm.notifier_call = cd321x_session_pm;
cd321x->session_pm.priority = 100;
if (!register_pm_notifier(&cd321x->session_pm))
cd321x->session_pm_registered = true;

guard(mutex)(&tps->lock);
/* Include failures reported while the port was still being initialized. */
Expand Down Expand Up @@ -3939,11 +3950,20 @@ EXPORT_SYMBOL_GPL(tipd_init);

void tipd_shutdown(struct tps6598x *tps)
{
struct cd321x *cd;
bool ordered;
int ret;

if (!of_machine_is_compatible("apple,j613"))
/* Only a port embedded in a struct cd321x has a USB4 session to stop. */
if (tps->data->tps_struct_size < sizeof(struct cd321x))
return;
cd = container_of(tps, struct cd321x, tps);

scoped_guard(mutex, &tps->lock)
ordered = typec_thunderbolt_switch_pd_ordered_stop(cd->tbt_switch);
if (!ordered)
return;
ret = cd321x_session_stop(container_of(tps, struct cd321x, tps), false);
ret = cd321x_session_stop(cd, false);
if (ret)
dev_err(tps->dev, "USB4 shutdown incomplete: %d\n", ret);
}
Expand Down
7 changes: 7 additions & 0 deletions drivers/usb/typec/tipd/i2c.c
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,12 @@ static int __maybe_unused tps6598x_resume(struct device *dev)
return ret;
}

/* Stops a USB4 session first when its switch asks for a PD-ordered stop. */
static void tps6598x_shutdown_i2c(struct i2c_client *client)
{
tipd_shutdown(i2c_get_clientdata(client));
}

static const struct dev_pm_ops tps6598x_pm_ops = {
.prepare = pm_sleep_ptr(tipd_prepare),
.complete = pm_sleep_ptr(tipd_complete),
Expand Down Expand Up @@ -122,6 +128,7 @@ static struct i2c_driver tps6598x_i2c_driver = {
},
.probe = tps6598x_probe_i2c,
.remove = tps6598x_remove_i2c,
.shutdown = tps6598x_shutdown_i2c,
.id_table = tps6598x_id,
};
module_i2c_driver(tps6598x_i2c_driver);
Expand Down
2 changes: 2 additions & 0 deletions drivers/usb/typec/tipd/tps6598x.h
Original file line number Diff line number Diff line change
Expand Up @@ -448,6 +448,7 @@ struct cd321x_status {
struct cd321x {
struct notifier_block session_pm;
bool session_pm_registered;
bool session_pm_armed;
bool session_closed;
struct tps6598x tps;

Expand Down Expand Up @@ -588,6 +589,7 @@ void cd321x_stop_work(struct cd321x *cd321x);
int cd321x_reboot_close(struct cd321x *cd321x);
int cd321x_reboot_notify(struct notifier_block *nb, unsigned long action,
void *data);
int cd321x_session_pm(struct notifier_block *nb, unsigned long event, void *unused);
bool cd321x_tbt_switch_keep_waiting(struct device *dev);
struct typec_thunderbolt_switch *
cd321x_tbt_switch_get(struct device *dev, struct fwnode_handle *fwnode,
Expand Down
Loading