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
59 changes: 48 additions & 11 deletions drivers/gpu/drm/apple/dcp-180hz.c
Original file line number Diff line number Diff line change
Expand Up @@ -266,26 +266,49 @@ bool dcp_180hz_pending(struct apple_dcp *dcp, const struct dcp_display_mode *mod
return raw && raw->extensions == 1 && dcp_180hz_edid((const u8 *)raw, 256, &mode->mode);
}

/*
* Probes again while an exception mode waits for the rest of its proof.
* Each input already has an event that probes again (the link facts and
* the announcement through dcp_180hz_notify(), the AV service through its
* publication, a new catalog through dcp_180hz_catalog_changed()); this
* bounded re-probe is kept until every one of them is shown to cover all
* orders of arrival, and says so when it runs out. The budget counts the
* re-probes that ran: a request while one is pending only moves its timer.
* See
* https://github.com/aurora-silicon/linux/issues/2346
*/
#define DCP_180HZ_PROBE_RETRIES 8
#define DCP_180HZ_PROBE_DELAY_MS 300

static void dcp_180hz_retry_work(struct work_struct *work)
{
struct apple_dcp *dcp = container_of(to_delayed_work(work), struct apple_dcp, rate_retry_wq);
unsigned long flags;
bool current_generation;
bool owner;

guard(mutex)(&dcp->hpd_mutex);
owner = dcp->connector &&
READ_ONCE(dcp->connector->dcp) == to_platform_device(dcp->dev);
/*
* One section for the generation check, the re-probe and its count, so
* a proof that starts over in between is never charged for this timer.
* The budget counts re-probes this queued: none when the hotplug work
* was already pending, and none at all once it is spent.
*/
spin_lock_irqsave(&dcp->metadata.lock, flags);
current_generation = dcp->typec_cable_connected && !dcp->quiescing &&
dcp->rate_proof.retry_generation == dcp->typec_generation;
if (owner && dcp->typec_cable_connected && !dcp->quiescing &&
dcp->rate_proof.retry_generation == dcp->typec_generation &&
dcp->rate_proof.retries < DCP_180HZ_PROBE_RETRIES &&
schedule_work(&dcp->connector->hotplug_wq))
dcp->rate_proof.retries++;
spin_unlock_irqrestore(&dcp->metadata.lock, flags);
if (current_generation && dcp->connector &&
READ_ONCE(dcp->connector->dcp) == to_platform_device(dcp->dev))
schedule_work(&dcp->connector->hotplug_wq);
}


void dcp_180hz_retry(struct apple_dcp *dcp)
{
bool retry = false, gave_up = false, rate = false, lanes = false;
unsigned long flags;
bool retry = false;

if (!dcp->rate_retry_initialized || READ_ONCE(dcp->quiescing))
return;
Expand All @@ -294,13 +317,27 @@ void dcp_180hz_retry(struct apple_dcp *dcp)
dcp->rate_proof.retry_generation = READ_ONCE(dcp->typec_generation);
dcp->rate_proof.retries = 0;
}
if (READ_ONCE(dcp->typec_cable_connected) && dcp->rate_proof.retries < 8) {
dcp->rate_proof.retries++;
retry = true;
if (READ_ONCE(dcp->typec_cable_connected) &&
dcp->rate_proof.retries <= DCP_180HZ_PROBE_RETRIES) {
/*
* Every re-probe of the budget ran and the mode is still not
* offered: said once, until the proof starts over.
*/
gave_up = dcp->rate_proof.retries == DCP_180HZ_PROBE_RETRIES;
if (gave_up)
dcp->rate_proof.retries++;
retry = !gave_up;
rate = dcp_180hz_fact_current(dcp, &dcp->rate_proof.rate);
lanes = dcp_180hz_fact_current(dcp, &dcp->rate_proof.lanes);
}
spin_unlock_irqrestore(&dcp->metadata.lock, flags);
if (retry)
mod_delayed_work(system_freezable_wq, &dcp->rate_retry_wq, msecs_to_jiffies(300));
mod_delayed_work(system_freezable_wq, &dcp->rate_retry_wq,
msecs_to_jiffies(DCP_180HZ_PROBE_DELAY_MS));
else if (gave_up)
dev_warn(dcp->dev, "180 Hz exception mode still not offered after %u probes (link rate %s, lane count %s)\n",
DCP_180HZ_PROBE_RETRIES, rate ? "proven" : "missing",
lanes ? "proven" : "missing");
}

static void dcp_180hz_cancel(void *data)
Expand Down
7 changes: 6 additions & 1 deletion drivers/gpu/drm/apple/dcp.c
Original file line number Diff line number Diff line change
Expand Up @@ -9927,7 +9927,12 @@ static int dcp_s2r_quiesce(struct apple_dcp *dcp, u32 *status)
idle_sent = true;
ret = apple_rtkit_idle_with(dcp->rtk, dcp_secure_port_idle, dcp);
if (ret) {
/* Answer what it still has queued, its log counters among them, and ask again. */
/*
* Answer what it still has queued, its log counters among them, and ask again.
* That queued messages are the cause is not shown: the one recorded refusal
* (-62 after 1137 ms on J700) was refused again. Kept until counted; see
* https://github.com/aurora-silicon/linux/issues/2347
*/
dev_warn(dcp->dev, "firmware refused idle after %lld ms: %d, control %#x status %#x: retrying once\n",
ktime_ms_delta(ktime_get(), start), ret,
readl(dcp->coproc_reg + APPLE_DCP_COPROC_CPU_CONTROL),
Expand Down
21 changes: 19 additions & 2 deletions drivers/gpu/drm/apple/dcp_backlight.c
Original file line number Diff line number Diff line change
Expand Up @@ -588,13 +588,30 @@ int dcp_backlight_wake_lit(struct apple_dcp *dcp)
return ret;
}

/*
* Called with the lock held; warns once when a refused level gets no more
* deferred presents.
*/
static unsigned int dcp_backlight_retry_delay_locked(struct apple_dcp *dcp)
{
struct dcp_backlight_state *state = &dcp->backlight.state;

lockdep_assert_held(&dcp->backlight.lock);
if (dcp_bl_retry_exhausted_report(state))
dev_warn_ratelimited(dcp->dev,
"backlight: %u nits refused %u times, %u presents refused in total; waiting for the next present\n",
dcp_bl_effective(state), DCP_BL_RETRY_MAX + 1,
state->refused);
return dcp_bl_retry_delay(state);
}

unsigned int dcp_backlight_retry_delay(struct apple_dcp *dcp)
{
unsigned long flags;
unsigned int delay;

spin_lock_irqsave(&dcp->backlight.lock, flags);
delay = dcp_bl_retry_delay(&dcp->backlight.state);
delay = dcp_backlight_retry_delay_locked(dcp);
spin_unlock_irqrestore(&dcp->backlight.lock, flags);
return delay;
}
Expand Down Expand Up @@ -662,7 +679,7 @@ void dcp_backlight_desktop_finish(struct apple_dcp *dcp, u32 id,
finished = dcp_bl_swap_finish(&dcp->backlight.state,
&dcp->desktop->backlight, id, reply, accepted);
changed = previous != dcp_bl_actual(&dcp->backlight.state);
delay = dcp_bl_retry_delay(&dcp->backlight.state);
delay = finished ? dcp_backlight_retry_delay_locked(dcp) : 0;
pending = dcp->backlight.state.dirty;
spin_unlock_irqrestore(&dcp->backlight.lock, flags);
if (changed)
Expand Down
52 changes: 48 additions & 4 deletions drivers/gpu/drm/apple/dcp_backlight.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,12 @@ struct dcp_backlight_state {
/* Firmware observations confirm output; they never become user requests. */
u32 reported;
u64 sequence;
/* Deferred presents of the current request after a refusal. */
u8 retries;
/* Presents carrying a level that the firmware refused, in total. */
u32 refused;
/* The refusal that used up the deferred presents is not reported yet. */
bool retry_report;
bool ready;
bool controlled;
/* An unchanged registration default cannot take over an unknown level. */
Expand Down Expand Up @@ -53,6 +58,23 @@ struct dcp_backlight_present {
u32 nits;
};

/*
* The level rides on a present; there is no separate brightness write. It
* is taken once the firmware has accepted the swap and completed it, and
* dcp_bl_complete() is called on exactly those events. A present can still
* be refused: swap_submit answers a non-zero status, or the swap watchdog
* retires a present the firmware never completed. Both are logged where
* they happen. The firmware gives no reason for a refusal and sends nothing
* that says when a present would be taken, so on a static display (no other
* present coming) the refused level is presented again after
* DCP_BL_RETRY_BASE_MS ms, doubling each time, at most DCP_BL_RETRY_MAX
* times per request. After that it stays pending for the next present or
* request, and dcp_backlight_retry_delay() warns once. The cause of refusals
* is not known yet: https://github.com/aurora-silicon/linux/issues/2332
*/
#define DCP_BL_RETRY_MAX 3U
#define DCP_BL_RETRY_BASE_MS 100U

/*
* Public powerlog reports use millinits. A report slightly above the panel
* ceiling describes the brightest level the panel can show, so clamp it; one
Expand Down Expand Up @@ -303,8 +325,11 @@ static inline bool dcp_bl_complete(struct dcp_backlight_state *state,
state->level_known = true;
} else {
state->dirty = true;
if (state->retries < 4)
state->refused++;
if (state->retries <= DCP_BL_RETRY_MAX) {
state->retries++;
state->retry_report = state->retries > DCP_BL_RETRY_MAX;
}
}
return true;
}
Expand All @@ -316,13 +341,32 @@ static inline int dcp_bl_reported_actual(const struct dcp_backlight_state *state
return state->actual;
}

/* Three deferred retries per user request; a permanent rejection stays dirty. */
/* A refused level that has used up its deferred presents. */
static inline bool dcp_bl_retry_exhausted(const struct dcp_backlight_state *state)
{
return state->ready && state->dirty && !state->in_flight &&
state->retries > DCP_BL_RETRY_MAX;
}

/*
* True once for each exhausted budget, for the refusal that used it up.
* Later refusals of the same request are counted but not reported again.
*/
static inline bool dcp_bl_retry_exhausted_report(struct dcp_backlight_state *state)
{
if (!state->retry_report || !dcp_bl_retry_exhausted(state))
return false;
state->retry_report = false;
return true;
}

/* Delay before the next deferred present of a refused level, 0 for none. */
static inline unsigned int dcp_bl_retry_delay(const struct dcp_backlight_state *state)
{
if (!state->ready || !state->dirty || state->in_flight ||
!state->retries || state->retries > 3)
!state->retries || state->retries > DCP_BL_RETRY_MAX)
return 0;
return 100U << (state->retries - 1);
return DCP_BL_RETRY_BASE_MS << (state->retries - 1);
}

/* A desktop present can complete before its submit reply arrives. */
Expand Down
3 changes: 0 additions & 3 deletions drivers/gpu/drm/apple/iomfb.c
Original file line number Diff line number Diff line change
Expand Up @@ -484,9 +484,6 @@ static void iomfb_queue_advance(struct apple_dcp *dcp)
if (delay)
mod_delayed_work(system_dfl_wq, &dcp->iomfb.backlight_retry,
msecs_to_jiffies(delay));
else
dev_warn_ratelimited(dcp->dev,
"backlight retry limit reached\n");
}
}
transaction->release(transaction);
Expand Down
35 changes: 27 additions & 8 deletions tools/testing/apple/check-backlight-retry.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ def function(file, name):
functions = '\n'.join(function(f, n) for f, n in (
('dcp_backlight.c', 'dcp_backlight_complete'),
('dcp_backlight.c', 'dcp_backlight_pending'),
('dcp_backlight.c', 'dcp_backlight_retry_delay_locked'),
('dcp_backlight.c', 'dcp_backlight_retry_delay'),
('iomfb.c', 'iomfb_present_failed_h17p'),
('iomfb.c', 'iomfb_present_complete_h17p'),
Expand All @@ -55,6 +56,10 @@ def function(file, name):
#include <string.h>
typedef uint8_t u8;typedef uint32_t u32;typedef uint64_t u64;
#define READ_ONCE(x) (x)
#define min(a,b) ((a)<(b)?(a):(b))
#define max(a,b) ((a)>(b)?(a):(b))
static inline u64 div_u64(u64 n,u32 d) {return n/d;}
static inline u64 mul_u32_u32(u32 a,u32 b) {return (u64)a*b;}
#define WRITE_ONCE(x,v) ((x)=(v))
#define lockdep_assert_held(x) ((void)(x))
#define spin_lock_irqsave(x,f) do { (void)(x);(f)=0; } while(0)
Expand All @@ -64,6 +69,11 @@ def function(file, name):
#define msecs_to_jiffies(x) (x)
#define IOMFB_OPAQUE_X_WAITING 0
#define IOMFB_OPAQUE_X_NEEDED 1
#define DCP_FIRMWARE_V_27_0 27
#define list_for_each_entry_safe(p,n,h,m) for((void)(n),(p)=NULL;(p)&&(h)->empty;)
#define list_del(x) ((void)(x))
struct iomfb_scanout_h17p {int link;};
static void iomfb_scanout_release_h17p(struct iomfb_scanout_h17p *s) {(void)s;}
struct work_struct {int id;};struct delayed_work {struct work_struct work;};
struct list_head {int empty;};
struct iomfb_transaction {
Expand All @@ -72,16 +82,16 @@ def function(file, name):
void (*release)(struct iomfb_transaction *);
};
struct apple_dcp {
bool crashed;void *dev;struct work_struct bl_update_wq,vblank_wq;
bool crashed,ext26_control_pending;int fw_compat;void *dev;struct work_struct bl_update_wq,vblank_wq;
struct {int lock;struct dcp_backlight_state state;} backlight;
struct {bool pending;} present_state_h17p;
struct {int lock;bool stopped,backlight_queued;struct iomfb_transaction *active;
struct delayed_work timeout,backlight_retry;struct work_struct work;
struct list_head pending;int opaque_x_state;
struct list_head pending,dropped;int opaque_x_state,callback_count;
} iomfb;
};
static unsigned delay,scheduled,released,discarded;static bool idle=true;
static void *system_wq;
static unsigned delay,scheduled,released,discarded,warned;static bool idle=true;
static void *system_dfl_wq;
static bool iomfb_channels_idle(struct apple_dcp *d) {(void)d;return idle;}
static int iomfb_enqueue_opaque_x(struct apple_dcp *d) {(void)d;return 0;}
static void iomfb_discard_pending(struct apple_dcp *d) {(void)d;discarded++;}
Expand All @@ -91,7 +101,7 @@ def function(file, name):
static void mod_delayed_work(void *q,struct delayed_work *w,unsigned ms) {
(void)q;assert(w->work.id==2);delay=ms;
}
static void dev_warn_ratelimited(void *d,const char *msg) {(void)d;(void)msg;}
#define dev_warn_ratelimited(d,...) do { (void)(d);warned++; } while(0)
static void iomfb_scanout_complete_h17p(struct apple_dcp *d) {(void)d;}
static void atomic_cmpxchg(int *p,int old,int value) {if(*p==old)*p=value;}
static void release(struct iomfb_transaction *t) {(void)t;released++;}
Expand All @@ -106,7 +116,7 @@ def function(file, name):
d->iomfb.pending.empty=1;assert(!dcp_bl_init(&d->backlight.state,509,true,inherited,false,0));
assert(dcp_bl_seed(&d->backlight.state,36));assert(!dcp_bl_dpms(&d->backlight.state,true));
assert(!dcp_bl_request(&d->backlight.state,140,false,false));
delay=scheduled=released=discarded=0;idle=true;
delay=scheduled=released=discarded=warned=0;idle=true;
}
static void reserve(struct apple_dcp *d,struct iomfb_transaction *t) {
memset(t,0,sizeof(*t));t->brightness_only=true;t->release=release;
Expand All @@ -129,11 +139,20 @@ def function(file, name):
init(&d);
for(unsigned i=0;i<4;i++) {
reserve(&d,&t);delay=0;reject(&d);assert(delay==(i<3?(100U<<i):0));
/* Only the refusal that ends the budget is reported as exhausted. */
assert(warned==(i==3));
}
assert(d.backlight.state.dirty&&d.backlight.state.retries==4&&!d.iomfb.active);
assert(d.backlight.state.refused==4);
/* Later refusals of the same request are counted, not reported again. */
for(unsigned i=0;i<2;i++) {reserve(&d,&t);delay=0;reject(&d);assert(!delay&&warned==1);}
assert(d.backlight.state.retries==4&&d.backlight.state.refused==6);
/* A new user request re-arms the budget and can complete. */
assert(!dcp_bl_request(&d.backlight.state,36,false,false));assert(!d.backlight.state.retries);
reserve(&d,&t);reject(&d);assert(delay==100);
reserve(&d,&t);reject(&d);assert(delay==100&&warned==1);
/* Its budget ends with its own report. */
for(unsigned i=0;i<3;i++) {reserve(&d,&t);reject(&d);}
assert(warned==2);
for(unsigned stopped=0;stopped<2;stopped++) {
init(&d);reserve(&d,&t);iomfb_present_failed_h17p(&d);
d.crashed=!stopped;d.iomfb.stopped=stopped;delay=scheduled=0;
Expand All @@ -151,7 +170,7 @@ def function(file, name):
code = a.output / 'backlight-retry.c'
code.write_text(prefix + functions + main)
command = [a.cc, '-std=gnu11', '-O1', '-g', '-Wall', '-Wextra', '-Werror',
'-Wno-unused-parameter', '-fsanitize=address,undefined', str(code),
'-Wno-unused-parameter', '-Wno-unused-function', '-fsanitize=address,undefined', str(code),
'-o', str(a.output / 'backlight-retry')]
subprocess.run(command, check=True)
result = subprocess.check_output([str(a.output / 'backlight-retry')], text=True)
Expand Down
Loading
Loading