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: 1 addition & 2 deletions drivers/cpufreq/apple-soc-cpufreq-test.c
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ static struct cft_ctx *cft_setup(struct kunit *test,
ctx->policy->min = ctx->table[0].frequency;
ctx->policy->max = ctx->table[CFT_NR_STATES - 1].frequency;
ctx->policy->cur = ctx->table[0].frequency;
ctx->policy->fast_switch_enabled = info->lazy_verify;
ctx->policy->fast_switch_enabled = info->verify_transition;
ctx->policy->driver_data = ctx->priv;
ctx->priv->policy = ctx->policy;

Expand Down Expand Up @@ -1418,7 +1418,6 @@ static void cft_test_t8142_lazy_fast_switch(struct kunit *test)
unsigned int freq;

KUNIT_EXPECT_TRUE(test, soc_t8142_info.verify_transition);
KUNIT_EXPECT_TRUE(test, soc_t8142_info.lazy_verify);
KUNIT_EXPECT_EQ(test, soc_t8142_info.transition_timeout_us, 2000U);

cft_set_cmd(ctx->regs, 1);
Expand Down
103 changes: 65 additions & 38 deletions drivers/cpufreq/apple-soc-cpufreq.c
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,13 @@
*/
#define APPLE_DVFS_VERIFY_LAZY_NS (1 * NSEC_PER_MSEC)
#define APPLE_DVFS_VERIFY_SLACK_NS (500 * NSEC_PER_USEC)
#define APPLE_DVFS_TRANSITION_NS (50 * NSEC_PER_USEC)
/*
* Without an OPP clock-latency-ns, a transition is taken to finish within an
* eighth of the SoC's transaction timeout: 50 us of the default 400 us, as
* measured on T8140.
*/
#define APPLE_DVFS_TRANSITION_DIV 8
/* First re-check of a transition still busy; doubles up to the above. */
#define APPLE_DVFS_RETRY_NS (4 * NSEC_PER_USEC)

/*
Expand Down Expand Up @@ -205,8 +211,13 @@ static int apple_fast_die_hp_state;

struct apple_soc_cpufreq_info {
bool has_ps2;
/*
* The command register reads back the accepted state, and each
* request is checked against it. Requests from the scheduler's
* fast-switch path are checked by a timer instead of waited for,
* see "Lazy verification".
*/
bool verify_transition;
bool lazy_verify;
/* Fail closed when the DT describes no fast die-temperature limit. */
bool needs_thermal_policy;
bool needs_thermal_owner;
Expand Down Expand Up @@ -245,6 +256,12 @@ struct apple_cpu_priv {
/* When the last request was written, and when it times out. */
ktime_t write_time;
ktime_t verify_deadline;
/*
* When a transition should have finished after its write, and the
* current re-check interval for one still busy, see "Lazy verification".
*/
u64 transition_ns;
u64 retry_ns;
/* P-state requested while a transition was busy, or -1. */
int pending_pstate;
/*
Expand Down Expand Up @@ -296,15 +313,11 @@ static const struct apple_soc_cpufreq_info soc_t8112_info = {
/*
* T8140 uses 5-bit PS1 state IDs starting at 1. The command register reads
* back the last accepted request, not a measured frequency; each request is
* verified. Requests from the scheduler's fast-switch path are verified by a
* timer rather than waited for, so schedutil can switch from the scheduler,
* see apple_soc_cpufreq_lazy_request(); requests through ->target_index
* (suspend, the static governors) are still waited for. Voltage, PLL and
* firmware throttling configuration are left as the bootloader set them.
* verified. Voltage, PLL and firmware throttling configuration are left as
* the bootloader set them.
*/
static const struct apple_soc_cpufreq_info soc_t8140_info = {
.verify_transition = true,
.lazy_verify = true,
.needs_thermal_policy = true,
.min_pstate = 1,
.max_pstate = 31,
Expand All @@ -320,7 +333,6 @@ static const struct apple_soc_cpufreq_info soc_t8140_info = {
*/
static const struct apple_soc_cpufreq_info soc_t8142_info = {
.verify_transition = true,
.lazy_verify = true,
.transition_timeout_us = 2000,
.min_pstate = 1,
.max_pstate = 31,
Expand Down Expand Up @@ -366,7 +378,6 @@ static const struct apple_soc_cpufreq_info soc_t8132_info = {
/* Command-state readback, not a measurement of the clock frequency. */
static const struct apple_soc_cpufreq_info soc_t6050_info = {
.verify_transition = true,
.lazy_verify = true,
.needs_firmware_thermal_control = true,
.transition_timeout_us = 2000,
.max_pstate = 31,
Expand Down Expand Up @@ -442,7 +453,7 @@ static unsigned int apple_soc_cpufreq_get_rate(unsigned int cpu)

priv = policy->driver_data;

if (priv->info->lazy_verify && !READ_ONCE(policy->fast_switch_enabled)) {
if (priv->info->verify_transition && !READ_ONCE(policy->fast_switch_enabled)) {
unsigned long flags;

raw_spin_lock_irqsave(&priv->lock, flags);
Expand Down Expand Up @@ -558,11 +569,12 @@ static int apple_soc_cpufreq_request(struct cpufreq_policy *policy,
}

/*
* Lazy verification (lazy_verify SoCs)
* Lazy verification (verify_transition SoCs)
*
* A transition takes about 50 us. The scheduler's fast-switch path must not
* wait for one, nor drop a request: schedutil records the frequency it asked
* for and does not ask again for the same one.
* A transition takes tens of microseconds (about 50 us on T8140), longer on
* some SoCs. The scheduler's fast-switch path must not wait for one, nor drop
* a request: schedutil records the frequency it asked for and does not ask
* again for the same one.
*
* A request finding the cluster idle requires the accepted state to be the
* one last written, so verifying the previous request, and is written at
Expand All @@ -573,7 +585,12 @@ static int apple_soc_cpufreq_request(struct cpufreq_policy *policy,
*
* A request finding the cluster busy is kept as pending, replacing an older
* pending one. The timer writes it as soon as the transition has finished,
* after the same check, and then verifies it as above.
* after the same check, and then verifies it as above. It first looks when
* the transition should have finished, transition_ns after the write: the
* OPP table's clock-latency-ns, or an eighth of the SoC's transaction
* timeout. While the transition is still busy it looks again after a delay
* that starts at APPLE_DVFS_RETRY_NS and doubles up to transition_ns, so a
* slow transition costs a few timer runs, not one every few microseconds.
*
* A failure stops all further requests, as for a request that is waited for,
* and the timer reports it.
Expand Down Expand Up @@ -629,6 +646,7 @@ static bool apple_soc_cpufreq_lazy_write(struct apple_cpu_priv *priv, u64 reg,
priv->write_time = ktime_get();
priv->verify_deadline = ktime_add_us(priv->write_time,
apple_soc_cpufreq_timeout(priv->info));
priv->retry_ns = APPLE_DVFS_RETRY_NS;
return true;
}

Expand Down Expand Up @@ -803,9 +821,12 @@ static enum hrtimer_restart apple_soc_cpufreq_verify(struct hrtimer *timer)

if (ktime_after(now, priv->verify_deadline))
apple_soc_cpufreq_fail(priv, reg);
else if (priv->pending_pstate >= 0)
hrtimer_start(timer, ns_to_ktime(APPLE_DVFS_RETRY_NS),
else if (priv->pending_pstate >= 0) {
hrtimer_start(timer, ns_to_ktime(priv->retry_ns),
HRTIMER_MODE_REL_HARD);
priv->retry_ns = min(priv->retry_ns * 2,
priv->transition_ns);
}
else
hrtimer_start(timer, priv->verify_deadline,
HRTIMER_MODE_ABS_HARD);
Expand Down Expand Up @@ -861,7 +882,7 @@ static int apple_soc_cpufreq_lazy_set(struct apple_cpu_priv *priv,
if (reg & APPLE_DVFS_CMD_BUSY) {
/* Retry about when the transition should have finished. */
ktime_t now = ktime_get();
ktime_t at = ktime_add_ns(priv->write_time, APPLE_DVFS_TRANSITION_NS);
ktime_t at = ktime_add_ns(priv->write_time, priv->transition_ns);

if (ktime_before(at, ktime_add_ns(now, APPLE_DVFS_RETRY_NS)))
at = ktime_add_ns(now, APPLE_DVFS_RETRY_NS);
Expand Down Expand Up @@ -929,7 +950,7 @@ static unsigned int apple_soc_cpufreq_lazy_request(struct cpufreq_policy *policy
}

/*
* Reconciles a lazy_verify cluster's accepted or pending P-state toward
* Reconciles a verify_transition cluster's accepted or pending P-state toward
* @target, clamped to current policy bounds and thermal cap, updates
* policy->cur, and records cpufreq_stats when hardware accepts the P-state.
* Called with priv->lock held.
Expand Down Expand Up @@ -959,9 +980,9 @@ static int apple_soc_cpufreq_lazy_reconcile(struct apple_cpu_priv *priv,

/*
* Applies a new fast die-temperature limit, whose highest P-state is @cap at
* @freq, to a lazy_verify cluster at once. schedutil picks up a lower limit
* only at its next frequency update, which a nohz_full CPU running with its
* tick stopped may not have for a long time. The cluster is moved to the
* @freq, to a verify_transition cluster at once. schedutil picks up a lower
* limit only at its next frequency update, which a nohz_full CPU running with
* its tick stopped may not have for a long time. The cluster is moved to the
* P-state the scheduler last asked for, at most @cap, and later requests are
* clamped to @cap. That also restores the request when the limit rises
* again before schedutil has seen it fall: the QoS request is then left
Expand Down Expand Up @@ -1036,7 +1057,7 @@ static int apple_soc_cpufreq_verify_policy(struct cpufreq_policy_data *data)
if (!policy)
return 0;
priv = policy->driver_data;
if (priv && priv->info->lazy_verify) {
if (priv && priv->info->verify_transition) {
unsigned long flags;
unsigned int requested;

Expand All @@ -1063,20 +1084,20 @@ static int apple_soc_cpufreq_set_target(struct cpufreq_policy *policy,
struct apple_cpu_priv *priv = policy->driver_data;

/* A waited-for request replaces a pending one, see "Lazy verification". */
if (priv->info->lazy_verify)
if (priv->info->verify_transition)
apple_soc_cpufreq_drain(priv);

return apple_soc_cpufreq_request(policy, index);
}

/*
* On a lazy_verify SoC policy->cur is the last fast-switch request, which the
* cluster may not hold: it may still be pending, be lost to a failure that
* stopped requests, or have been moved since by a fast die-temperature limit,
* see apple_soc_cpufreq_lazy_cap(). The core skips a suspend frequency equal
* to policy->cur, so settle the cluster first, writing and verifying the
* pending request or reporting the failure, and record the state it settled
* in as policy->cur before the generic suspend.
* On a verify_transition SoC policy->cur is the last fast-switch request,
* which the cluster may not hold: it may still be pending, be lost to a
* failure that stopped requests, or have been moved since by a fast
* die-temperature limit, see apple_soc_cpufreq_lazy_cap(). The core skips a
* suspend frequency equal to policy->cur, so settle the cluster first,
* writing and verifying the pending request or reporting the failure, and
* record the state it settled in as policy->cur before the generic suspend.
*/
static int apple_soc_cpufreq_suspend(struct cpufreq_policy *policy)
{
Expand All @@ -1095,7 +1116,7 @@ static int apple_soc_cpufreq_suspend(struct cpufreq_policy *policy)
spin_unlock_bh(&fd->lock);
}

if (priv->info->lazy_verify) {
if (priv->info->verify_transition) {
int pstate = apple_soc_cpufreq_drain(priv);
unsigned int old_freq, freq;
unsigned long flags;
Expand Down Expand Up @@ -1191,7 +1212,7 @@ static unsigned int apple_soc_cpufreq_fast_switch(struct cpufreq_policy *policy,

if (idx < 0)
return 0;
if (priv->info->lazy_verify)
if (priv->info->verify_transition)
return apple_soc_cpufreq_lazy_request(policy, idx);
if (apple_soc_cpufreq_request(policy, idx))
return 0;
Expand Down Expand Up @@ -1290,8 +1311,8 @@ static void apple_fast_die_arm(struct apple_fast_die *fd)
}

/*
* Applies @limit to a lazy_verify cluster at once, as its fastest state at or
* below the limit, see apple_soc_cpufreq_lazy_cap().
* Applies @limit to a verify_transition cluster at once, as its fastest state
* at or below the limit, see apple_soc_cpufreq_lazy_cap().
*/
static void apple_fast_die_cap(struct apple_fast_die *fd, unsigned int limit)
{
Expand All @@ -1300,7 +1321,7 @@ static void apple_fast_die_cap(struct apple_fast_die *fd, unsigned int limit)
struct cpufreq_frequency_table *pos;
unsigned int cap = 0, freq = 0;

if (!priv->info->lazy_verify)
if (!priv->info->verify_transition)
return;

cpufreq_for_each_valid_entry(pos, policy->freq_table) {
Expand Down Expand Up @@ -1853,8 +1874,14 @@ static int apple_soc_cpufreq_init(struct cpufreq_policy *policy)
}

policy->cpuinfo.transition_latency = transition_latency;
/* Measured OPP latency if there is one, see "Lazy verification". */
priv->transition_ns = dev_pm_opp_get_max_transition_latency(cpu_dev) ?:
div_u64((u64)apple_soc_cpufreq_timeout(info) * NSEC_PER_USEC,
APPLE_DVFS_TRANSITION_DIV);
priv->retry_ns = APPLE_DVFS_RETRY_NS;
policy->dvfs_possible_from_any_cpu = true;
policy->fast_switch_possible = !info->verify_transition || info->lazy_verify;
/* Verified transitions are checked lazily on the fast-switch path. */
policy->fast_switch_possible = true;
policy->suspend_freq = freq_table[0].frequency;

return 0;
Expand Down
Loading