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
50 changes: 50 additions & 0 deletions drivers/usb/typec/tipd/cd321x-tbt-test.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <kunit/test-bug.h>
#include <linux/module.h>
#include <linux/property.h>
#include <linux/reboot.h>
#include <linux/usb/typec_mux.h>
#include <linux/workqueue.h>

Expand Down Expand Up @@ -101,6 +102,8 @@ struct cd321x_tbt_test {
struct device *dev;
bool keep_waiting;
unsigned int keep_waiting_calls;
unsigned int set_calls;
int last_state;
};

/* Stands in for the driver core's answer on whether to keep waiting. */
Expand Down Expand Up @@ -143,6 +146,10 @@ static int cd321x_tbt_test_init(struct kunit *test)
static int cd321x_tbt_test_set(struct typec_thunderbolt_switch_dev *sw,
const struct typec_thunderbolt_switch_data *data)
{
struct cd321x_tbt_test *ctx = kunit_get_current_test()->priv;

ctx->set_calls++;
ctx->last_state = data->state;
return 0;
}

Expand Down Expand Up @@ -406,6 +413,47 @@ static void cd321x_tbt_router_rebinds_test(struct kunit *test)
KUNIT_EXPECT_FALSE(test, cd->tbt_notifier_registered);
}

/* A port with a live router, as the cable events find it. */
static struct cd321x *cd321x_tbt_test_live_port(struct kunit *test)
{
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);
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;
}

/* 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)
{
struct cd321x_tbt_test *ctx = test->priv;
struct cd321x *cd = cd321x_tbt_test_live_port(test);

KUNIT_EXPECT_EQ(test, cd321x_reboot_notify(&cd->reboot_nb, SYS_RESTART, NULL),
NOTIFY_OK);
KUNIT_EXPECT_EQ(test, ctx->set_calls, 1);
KUNIT_EXPECT_EQ(test, ctx->last_state, TYPEC_THUNDERBOLT_SWITCH_OFF);
KUNIT_EXPECT_FALSE(test, cd321x_pm_can_update(&cd->pm));
}

/* Only a reboot, halt or power off closes the session. */
static void cd321x_tbt_reboot_other_action_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_reboot_notify(&cd->reboot_nb, 0, NULL), NOTIFY_DONE);
KUNIT_EXPECT_EQ(test, ctx->set_calls, 0);
KUNIT_EXPECT_TRUE(test, cd321x_pm_can_update(&cd->pm));
}

static struct kunit_case cd321x_tbt_cases[] = {
KUNIT_CASE(cd321x_tbt_no_router_test),
KUNIT_CASE(cd321x_tbt_router_bound_test),
Expand All @@ -416,6 +464,8 @@ static struct kunit_case cd321x_tbt_cases[] = {
KUNIT_CASE(cd321x_tbt_other_router_binds_test),
KUNIT_CASE(cd321x_tbt_router_binds_after_removal_test),
KUNIT_CASE(cd321x_tbt_router_rebinds_test),
KUNIT_CASE(cd321x_tbt_reboot_closes_session_test),
KUNIT_CASE(cd321x_tbt_reboot_other_action_test),
{}
};

Expand Down
52 changes: 52 additions & 0 deletions drivers/usb/typec/tipd/core.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include <linux/soc/apple/j613-display.h>
#include <linux/power_supply.h>
#include <linux/regmap.h>
#include <linux/reboot.h>
#include <linux/unaligned.h>
#include <linux/usb/pd.h>
#include <linux/soc/apple/sleep.h>
Expand Down Expand Up @@ -2607,6 +2608,45 @@ VISIBLE_IF_KUNIT void cd321x_stop_work(struct cd321x *cd321x)
}
EXPORT_SYMBOL_IF_KUNIT(cd321x_stop_work);

/*
* Close the port's USB4/Thunderbolt session while the system still runs. A
* cable event that closes it inside device_shutdown() makes pid 1 wait for the
* PCIe-C reset handshake of an ACIO that is already going down. Stopping the
* work first keeps every later event away from the router.
*/
VISIBLE_IF_KUNIT int cd321x_reboot_close(struct cd321x *cd321x)
{
struct typec_thunderbolt_switch_data off = {
.state = TYPEC_THUNDERBOLT_SWITCH_OFF,
};

cd321x_stop_work(cd321x);
return typec_thunderbolt_switch_set(cd321x->tbt_switch, &off);
}
EXPORT_SYMBOL_IF_KUNIT(cd321x_reboot_close);

VISIBLE_IF_KUNIT int cd321x_reboot_notify(struct notifier_block *nb,
unsigned long action, void *data)
{
struct cd321x *cd321x = container_of(nb, struct cd321x, reboot_nb);
struct tps6598x *tps = &cd321x->tps;
int ret;

if (action != SYS_RESTART && action != SYS_HALT && action != SYS_POWER_OFF)
return NOTIFY_DONE;

if (tps->irq)
disable_irq(tps->irq);
if (cd321x->tbt_switch)
dev_info(tps->dev, "closing the USB4/Thunderbolt session before shutdown\n");
ret = cd321x_reboot_close(cd321x);
if (cd321x->tbt_switch)
dev_info(tps->dev, "closed the USB4/Thunderbolt session before shutdown: %d\n",
ret);
return NOTIFY_OK;
}
EXPORT_SYMBOL_IF_KUNIT(cd321x_reboot_notify);

/* Separate so that the KUnit tests can choose the driver core's answer. */
VISIBLE_IF_KUNIT bool cd321x_tbt_switch_keep_waiting(struct device *dev)
{
Expand Down Expand Up @@ -2832,6 +2872,13 @@ cd321x_register_port(struct tps6598x *tps, struct fwnode_handle *fwnode)
cd321x_tbt_watch_late(cd321x, fwnode))
dev_warn(tps->dev, "cannot watch for the USB4/Thunderbolt router\n");

cd321x->reboot_nb.notifier_call = cd321x_reboot_notify;
cd321x->reboot_nb.priority = INT_MAX;
if (register_reboot_notifier(&cd321x->reboot_nb))
dev_warn(tps->dev, "cannot close the USB4/Thunderbolt session at shutdown\n");
else
cd321x->reboot_nb_registered = true;

return 0;

err_put_switch:
Expand Down Expand Up @@ -2863,6 +2910,11 @@ cd321x_unregister_port(struct tps6598x *tps)
{
struct cd321x *cd321x = container_of(tps, struct cd321x, tps);

if (cd321x->reboot_nb_registered) {
unregister_reboot_notifier(&cd321x->reboot_nb);
cd321x->reboot_nb_registered = false;
}

cd321x_stop_work(cd321x);
fwnode_handle_put(cd321x->connector_fwnode);
cd321x->connector_fwnode = NULL;
Expand Down
6 changes: 6 additions & 0 deletions drivers/usb/typec/tipd/tps6598x.h
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,9 @@ struct cd321x {
struct notifier_block tbt_watch;
struct work_struct tbt_attach_work;
bool tbt_watch_registered;
/* Closes the USB4/Thunderbolt session before device_shutdown() */
struct notifier_block reboot_nb;
bool reboot_nb_registered;
atomic_t link_event;
atomic_t ready_event;
atomic_t restore_event;
Expand Down Expand Up @@ -578,6 +581,9 @@ void cd321x_mux_gave_up(struct cd321x *cd321x, unsigned int retries, unsigned in
void cd321x_init_work(struct cd321x *cd321x);
int cd321x_init_mux(struct cd321x *cd321x);
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);
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
23 changes: 22 additions & 1 deletion tools/testing/selftests/drivers/gpu/apple-dcp/pm.py
Original file line number Diff line number Diff line change
Expand Up @@ -493,7 +493,7 @@ def function(text, name):
struct typec_mux_state { void *alt,*data; unsigned long mode; };
struct device;
struct tps6598x { int lock; void *port; struct device *dev; };
struct notifier_block { int (*notifier_call)(struct notifier_block *,unsigned long,void *); };
struct notifier_block { int (*notifier_call)(struct notifier_block *,unsigned long,void *); int priority; };
'''
pm = tipd_pm_header(root)
mocks = r'''
Expand Down Expand Up @@ -762,6 +762,27 @@ def function(text, name):
cd321x_unregister_port(&c.tps);balanced();
''', 1)

# A port that closes its USB4 session before a reboot registers a reboot notifier at probe.
if 'register_reboot_notifier(' in core:
member = ' bool state_valid,tbt_notifier_registered;\n'
assert mocks.count(member) == 1
mocks = mocks.replace(member, member + ' struct notifier_block reboot_nb;\n bool reboot_nb_registered;\n', 1)
mocks += r'''
#ifndef INT_MAX
#define INT_MAX 0x7fffffff
#endif
static int reboots;
static int cd321x_reboot_notify(struct notifier_block *n,unsigned long a,void *d) { (void)n;(void)a;(void)d;return 0; }
static int register_reboot_notifier(struct notifier_block *n) { (void)n;reboots++;return 0; }
static void unregister_reboot_notifier(struct notifier_block *n) { (void)n;assert(reboots>0);reboots--; }
'''
idle = ' assert(!ports&&!altmodes&&!muxes&&!switches&&!notifiers);\n}'
assert controls.count(idle) == 1
controls = controls.replace(idle, idle[:-2] + '\n assert(!reboots);\n}', 1)
probed = ' reset(&c);assert(cd321x_register_port(&c.tps,&connector)==0&&c.state_valid);\n'
assert controls.count(probed) == 1
controls = controls.replace(probed, probed + ' assert(reboots==1&&c.reboot_nb_registered&&c.reboot_nb.notifier_call==cd321x_reboot_notify);\n', 1)

return prefix + pm + mocks + production + '\n#define TEST_DEFER 1\n#define TEST_FWNODE 1\n' + controls


Expand Down
Loading