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/accel/ane/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ ane-y := \
ane_drv.o \
ane_tm.o \
ane_dart.o \
ane_perf.o
ane_perf.o \
ane_boost.o

ane_t6021-y := \
ane_fw_platform.o \
Expand Down
25 changes: 25 additions & 0 deletions drivers/accel/ane/ane.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <linux/atomic.h>
#include <linux/list.h>
#include <linux/mutex.h>
#include <linux/pm_qos.h>
#include <linux/workqueue.h>

#include <drm/drm_device.h>
Expand Down Expand Up @@ -126,8 +127,27 @@ struct ane_device {
bool voted;
bool clock_held;
bool pinned;
bool cpu_boost;
} perf;

/*
* Engine-busy CPU cluster boost (ane_boost.c): min-frequency QoS on
* every cpufreq policy while a submit runs and until boost_idle_ms
* after the last one completes. Used only when the DT binds no
* performance controller, so the engine keeps the boost it had
* until it gets a clock level instead.
*/
struct ane_boost {
struct mutex lock; /* protects the fields below */
struct delayed_work off;
struct freq_qos_request *legs;
int nlegs;
int held;
unsigned long last_end;
bool busy;
bool on;
} boost;

/*
* Producer-side stats (sysfs ane_stats, debugfs ane_timeline;
* see ane_stats.h). stats=0 or a failed ring allocation leaves
Expand Down Expand Up @@ -166,6 +186,11 @@ int ane_perf_begin(struct ane_device *ane);
void ane_perf_end(struct ane_device *ane);
int ane_perf_suspend(struct ane_device *ane);

int ane_boost_init(struct ane_device *ane);
void ane_boost_exit(struct ane_device *ane);
void ane_boost_begin(struct ane_device *ane);
void ane_boost_end(struct ane_device *ane);

int ane_dart_init(struct ane_device *ane);
void ane_dart_mask(struct ane_device *ane);
void ane_dart_unmask(struct ane_device *ane);
Expand Down
142 changes: 142 additions & 0 deletions drivers/accel/ane/ane_boost.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
// SPDX-License-Identifier: GPL-2.0-only OR MIT
/*
* Engine-busy CPU cluster boost.
*
* Bandwidth-bound programs run at a speed set by the CPU cluster
* p-states. On T8103, with a mostly-sleeping submitter at normal
* priority, some submits run about twice as long as normal, while the
* submitter's CPU time and run delay stay unchanged; with both clusters
* pinned at their top p-state (performance governor, or SCHED_FIFO for
* the submitter, which schedutil boosts the same way) the slow submits
* disappear. A single long program slows down as well if the QoS
* lapses before it completes, so the hold must last from submit start
* to completion, and the idle delay counts from completion. The likely
* mechanism is the second p-state field apple-soc-cpufreq writes with
* the cluster p-state on T8103 only (DVFS_CMD PS2, has_ps2); the
* memory-controller tuning its author named as unimplemented is still
* absent from the tree. Whatever the field drives, the cure is the
* cluster p-state, which cpufreq lets a driver request.
*
* So hold a min-frequency QoS at the top of every cpufreq policy while a
* submit runs, and drop it boost_idle_ms after the last one completes.
* The QoS goes through cpufreq's own constraint object, so it composes
* with any governor and never touches the DVFS registers.
*/
#include <linux/cpufreq.h>
#include <linux/jiffies.h>
#include <linux/module.h>
#include <linux/pm_qos.h>
#include <linux/slab.h>
#include <linux/workqueue.h>

#include "ane.h"

static unsigned int boost_idle_ms = 100;
module_param(boost_idle_ms, uint, 0644);
MODULE_PARM_DESC(boost_idle_ms,
"hold every CPU cluster at its top p-state while a submit runs and this long after the last one completes (0 = off)");

/* Lock held. */
static void ane_boost_set(struct ane_device *ane, bool on)
{
struct ane_boost *b = &ane->boost;
int cpu, n = 0;

if (on) {
for_each_possible_cpu(cpu) {
struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
int err;

if (!policy)
continue;
if (cpu != cpumask_first(policy->related_cpus) ||
n >= b->nlegs) {
cpufreq_cpu_put(policy);
continue;
}
err = freq_qos_add_request(&policy->constraints,
&b->legs[n], FREQ_QOS_MIN,
policy->cpuinfo.max_freq);
cpufreq_cpu_put(policy);
if (err >= 0)
n++;
}
b->held = n;
} else {
for (n = 0; n < b->held; n++)
freq_qos_remove_request(&b->legs[n]);
b->held = 0;
}
b->on = on;
}

static void ane_boost_off_work(struct work_struct *work)
{
struct ane_device *ane =
container_of(work, struct ane_device, boost.off.work);
struct ane_boost *b = &ane->boost;

mutex_lock(&b->lock);
/* A submit that began or ended after this expiry was armed keeps
* the boost on; its own completion re-arms the drop.
*/
if (b->on && !b->busy &&
time_after_eq(jiffies, b->last_end + msecs_to_jiffies(boost_idle_ms)))
ane_boost_set(ane, false);
mutex_unlock(&b->lock);
}

/* Engine lock held: submits never overlap, so busy is a flag. */
void ane_boost_begin(struct ane_device *ane)
{
struct ane_boost *b = &ane->boost;

if (!b->legs || !boost_idle_ms)
return;
mutex_lock(&b->lock);
b->busy = true;
if (!b->on)
ane_boost_set(ane, true);
mutex_unlock(&b->lock);
}

void ane_boost_end(struct ane_device *ane)
{
struct ane_boost *b = &ane->boost;

if (!b->legs)
return;
mutex_lock(&b->lock);
b->busy = false;
b->last_end = jiffies;
if (b->on)
mod_delayed_work(system_dfl_wq, &b->off,
msecs_to_jiffies(boost_idle_ms));
mutex_unlock(&b->lock);
}

int ane_boost_init(struct ane_device *ane)
{
struct ane_boost *b = &ane->boost;

mutex_init(&b->lock);
INIT_DELAYED_WORK(&b->off, ane_boost_off_work);
b->nlegs = num_possible_cpus();
b->legs = kcalloc(b->nlegs, sizeof(*b->legs), GFP_KERNEL);
return b->legs ? 0 : -ENOMEM;
}

void ane_boost_exit(struct ane_device *ane)
{
struct ane_boost *b = &ane->boost;

if (!b->legs)
return;
cancel_delayed_work_sync(&b->off);
mutex_lock(&b->lock);
if (b->on)
ane_boost_set(ane, false);
mutex_unlock(&b->lock);
kfree(b->legs);
b->legs = NULL;
}
32 changes: 29 additions & 3 deletions drivers/accel/ane/ane_perf.c
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ int ane_perf_begin(struct ane_device *ane)
struct ane_perf *p = &ane->perf;
int ret = 0;

if (p->cpu_boost)
ane_boost_begin(ane);

/* The submit ioctl already holds a runtime PM reference. */
mutex_lock(&p->lock);
p->busy = true;
Expand Down Expand Up @@ -96,6 +99,9 @@ void ane_perf_end(struct ane_device *ane)
{
struct ane_perf *p = &ane->perf;

if (p->cpu_boost)
ane_boost_end(ane);

mutex_lock(&p->lock);
p->busy = false;
p->release_retries = 0;
Expand Down Expand Up @@ -199,10 +205,26 @@ int ane_perf_init(struct ane_device *ane, unsigned int high_level)
dev_pm_opp_put(opp);
if (!p->peak_bw || p->avg_bw > p->peak_bw)
return -EINVAL;
} else {
dev_warn(ane->dev, "no memory bandwidth provider; CPU frequency boost removed\n");
}
return devm_device_add_group(ane->dev, &ane_perf_group);

ret = devm_device_add_group(ane->dev, &ane_perf_group);
if (ret)
return ret;

/*
* Without a performance controller the engine runs at its boot
* clock, and the CPU cluster boost is what keeps bandwidth-bound
* programs and the submit tail fast. Keep it until the DT binds a
* controller.
*/
if (!p->report) {
ret = ane_boost_init(ane);
if (ret)
return ret;
p->cpu_boost = true;
dev_info(ane->dev, "no performance controller; keeping the CPU frequency boost\n");
}
return 0;
}

int ane_perf_exit(struct ane_device *ane)
Expand All @@ -212,6 +234,10 @@ int ane_perf_exit(struct ane_device *ane)

if (!p->initialized)
return 0;
if (p->cpu_boost) {
ane_boost_exit(ane);
p->cpu_boost = false;
}
cancel_delayed_work_sync(&p->idle);
mutex_lock(&p->lock);
p->error = ane_perf_drop(ane);
Expand Down