From patchwork Tue Sep 17 20:04:33 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Paul X-Patchwork-Id: 11149367 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 1DBAD13BD for ; Tue, 17 Sep 2019 20:04:54 +0000 (UTC) Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 0609A214AF for ; Tue, 17 Sep 2019 20:04:54 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 0609A214AF Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=poorly.run Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=dri-devel-bounces@lists.freedesktop.org Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 14A396ED3D; Tue, 17 Sep 2019 20:04:53 +0000 (UTC) X-Original-To: dri-devel@lists.freedesktop.org Delivered-To: dri-devel@lists.freedesktop.org Received: from mail-yw1-xc42.google.com (mail-yw1-xc42.google.com [IPv6:2607:f8b0:4864:20::c42]) by gabe.freedesktop.org (Postfix) with ESMTPS id EED7F6ED3D for ; Tue, 17 Sep 2019 20:04:50 +0000 (UTC) Received: by mail-yw1-xc42.google.com with SMTP id 201so1605658ywn.13 for ; Tue, 17 Sep 2019 13:04:50 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=wkqE9utRolz42+Kkf6uy7tQVtznTQJwx51DLaVFkD8s=; b=HVHgVRI+Dfc90atQHZYYdRaH/r1r389z5U2Wo3TzWaHgghpNDANH2D7row4JtCSIRS 7ytwNzJV3WhvZcH21RnCJA3TILbx8GEwbDiYa8KbRsqriS8/kOa+s4mnjZJaAu1oDXHS sr4o8uX+rI1zWx+/Ocm7/kePmtwnA3jTuutoreuehoDyOBfaZaIgphjqUn6MGIdty/Xz v0+ufebv2cviTbS+ibA81UL1/uU/bRjbGVT8DCiSlMZR0vhg6VIQPhwfeL7OUuzSMaVw b8Z3lL/KO2wGIqyHcOOeetYCPaXSJO8EmlSE5C+2bN06BW0wCRXv4U+/IkLzOqr9PSkw JaIw== X-Gm-Message-State: APjAAAW4+16IJjGMzDCn2NWRxxuSYZ5vGyRPAyIFvOXdNcfI84wYWEoI K17I5a4fpUjhuARWanLXqoZFxFmvLDgoJQ== X-Google-Smtp-Source: APXvYqw/X4iooG1+kY/6j/ft28vKNqk/hvJe0EcXnSU5e0gkJiQebL+/eMVtLWPKP2YnCfWB/DbM6g== X-Received: by 2002:a81:98cb:: with SMTP id p194mr347606ywg.0.1568750689894; Tue, 17 Sep 2019 13:04:49 -0700 (PDT) Received: from rosewood.cam.corp.google.com ([2620:0:1013:11:89c6:2139:5435:371d]) by smtp.gmail.com with ESMTPSA id g128sm662175ywb.13.2019.09.17.13.04.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 17 Sep 2019 13:04:49 -0700 (PDT) From: Sean Paul To: dri-devel@lists.freedesktop.org Subject: [PATCH 2/2] drm: Measure Self Refresh Entry/Exit times to avoid thrashing Date: Tue, 17 Sep 2019 16:04:33 -0400 Message-Id: <20190917200443.64481-2-sean@poorly.run> X-Mailer: git-send-email 2.23.0.237.gc6a4ce50a0-goog In-Reply-To: <20190917200443.64481-1-sean@poorly.run> References: <20190917200443.64481-1-sean@poorly.run> MIME-Version: 1.0 X-Mailman-Original-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=poorly.run; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=wkqE9utRolz42+Kkf6uy7tQVtznTQJwx51DLaVFkD8s=; b=FqCh2jMzJpjxjY2DX73H9fuioBZym3EBg3tQDrGlIaKQQ8MADR2/GVZcMk1xt9Wh3t wNaqjS1jlj1LQKOf2QzzLdtXry2kDx1eo7LsQZEJMWxTfthInm98AVx6vLyr3JR8/2ec YRT4ew6L6rQE2YjAR8jV096ZtSGXpmDrOzpuqGMwoPypw5M0wGXsryRZ3f1BeelvmrH2 8Y6/V80OFd3OOSea0UgJcEzlct6oWBH7odL44VZ0CAzzDw2/FMvepoC39jDnkOflAHI7 JXwLg8CTOJBk8ccQyDRtFX7RAhfk+RFARL1OBsCjzY+3xfFASxbIFYYGEmjEn5e8i3v1 9L8Q== X-BeenThere: dri-devel@lists.freedesktop.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: Direct Rendering Infrastructure - Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Maxime Ripard , David Airlie , Sean Paul , jekarl@iki.fi, Sean Paul Errors-To: dri-devel-bounces@lists.freedesktop.org Sender: "dri-devel" From: Sean Paul Currently the self refresh idle timer is a const set by the crtc. This is fine if the self refresh entry/exit times are well-known for all panels used on that crtc. However panels and workloads can vary quite a bit, and a timeout which works well for one doesn't work well for another. In the extreme, if the timeout is too short we could get in a situation where the self refresh exits are taking so long we queue up a self refresh entry before the exit commit is even finished. This patch changes the idle timeout to a moving average of the entry times + a moving average of exit times + the crtc constant. This patch was tested on rockchip, with a "kevin" CrOS panel the idle delay averages out to about ~235ms (35 entry + 100 exit + 100 const). On the same board, the "bob" panel idle delay lands around ~340ms (90 entry + 150 exit + 100 const). Signed-off-by: Sean Paul Reviewed-by: Daniel Vetter --- drivers/gpu/drm/drm_atomic_helper.c | 20 +++++++ drivers/gpu/drm/drm_self_refresh_helper.c | 71 ++++++++++++++++++++++- include/drm/drm_self_refresh_helper.h | 2 + 3 files changed, 92 insertions(+), 1 deletion(-) diff --git a/drivers/gpu/drm/drm_atomic_helper.c b/drivers/gpu/drm/drm_atomic_helper.c index 9d7e4da6c292..3f13fa9a9e24 100644 --- a/drivers/gpu/drm/drm_atomic_helper.c +++ b/drivers/gpu/drm/drm_atomic_helper.c @@ -26,6 +26,7 @@ */ #include +#include #include #include @@ -1570,9 +1571,23 @@ static void commit_tail(struct drm_atomic_state *old_state) { struct drm_device *dev = old_state->dev; const struct drm_mode_config_helper_funcs *funcs; + ktime_t start; + s64 commit_time_ms; funcs = dev->mode_config.helper_private; + /* + * We're measuring the _entire_ commit, so the time will vary depending + * on how many fences and objects are involved. For the purposes of self + * refresh, this is desirable since it'll give us an idea of how + * congested things are. This will inform our decision on how often we + * should enter self refresh after idle. + * + * These times will be averaged out in the self refresh helpers to avoid + * overreacting over one outlier frame + */ + start = ktime_get(); + drm_atomic_helper_wait_for_fences(dev, old_state, false); drm_atomic_helper_wait_for_dependencies(old_state); @@ -1582,6 +1597,11 @@ static void commit_tail(struct drm_atomic_state *old_state) else drm_atomic_helper_commit_tail(old_state); + commit_time_ms = ktime_ms_delta(ktime_get(), start); + if (commit_time_ms > 0) + drm_self_refresh_helper_update_avg_times(old_state, + (unsigned long)commit_time_ms); + drm_atomic_helper_commit_cleanup_done(old_state); drm_atomic_state_put(old_state); diff --git a/drivers/gpu/drm/drm_self_refresh_helper.c b/drivers/gpu/drm/drm_self_refresh_helper.c index 9095cebf2147..522430f8eef1 100644 --- a/drivers/gpu/drm/drm_self_refresh_helper.c +++ b/drivers/gpu/drm/drm_self_refresh_helper.c @@ -5,6 +5,7 @@ * Authors: * Sean Paul */ +#include #include #include #include @@ -50,10 +51,16 @@ * atomic_check when &drm_crtc_state.self_refresh_active is true. */ +DECLARE_EWMA(psr_time, 4, 4) + struct drm_self_refresh_data { struct drm_crtc *crtc; struct delayed_work entry_work; unsigned int entry_delay_ms; + + struct mutex avg_mutex; + struct ewma_psr_time entry_avg_ms; + struct ewma_psr_time exit_avg_ms; }; static void drm_self_refresh_helper_entry_work(struct work_struct *work) @@ -121,6 +128,59 @@ static void drm_self_refresh_helper_entry_work(struct work_struct *work) drm_modeset_acquire_fini(&ctx); } +/** + * drm_self_refresh_helper_update_avg_times - Updates a crtc's SR time averages + * @state: the state which has just been applied to hardware + * @commit_time_ms: the amount of time in ms that this commit took to complete + * + * Called after &drm_mode_config_funcs.atomic_commit_tail, this function will + * update the average entry/exit self refresh times on self refresh transitions. + * These averages will be used when calculating how long to delay before + * entering self refresh mode after activity. + */ +void drm_self_refresh_helper_update_avg_times(struct drm_atomic_state *state, + unsigned int commit_time_ms) +{ + struct drm_crtc *crtc; + struct drm_crtc_state *old_crtc_state, *new_crtc_state; + int i; + + for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, + new_crtc_state, i) { + struct drm_self_refresh_data *sr_data = crtc->self_refresh_data; + struct ewma_psr_time *time; + + if (old_crtc_state->self_refresh_active == + new_crtc_state->self_refresh_active) + continue; + + if (new_crtc_state->self_refresh_active) + time = &sr_data->entry_avg_ms; + else + time = &sr_data->exit_avg_ms; + + /* + * It might be nice if we could rely on &drm_crtc.mutex to + * protect &drm_self_refresh_data.exit_avg_ms, as we do with + * &drm_self_refresh_data.entry_avg_ms, but there are a few + * reasons why a separate lock is a better choice: + * - We can't rely on &drm_crtc.mutex being held here if we're + * doing a nonblocking commit + * - We can't grab &drm_crtc.mutex here since drm_modeset_lock() + * doesn't tell us whether the lock was already held in the + * acquire context (it eats -EALREADY), so we can't tell if we + * should drop it or not + * - We don't need such a heavy-handed lock for what we're + * trying to do here, commit ordering doesn't matter, so a + * point-of-use lock will be less contentious + */ + mutex_lock(&sr_data->avg_mutex); + ewma_psr_time_add(time, commit_time_ms); + mutex_unlock(&sr_data->avg_mutex); + } +} +EXPORT_SYMBOL(drm_self_refresh_helper_update_avg_times); + /** * drm_self_refresh_helper_alter_state - Alters the atomic state for SR exit * @state: the state currently being checked @@ -152,6 +212,7 @@ void drm_self_refresh_helper_alter_state(struct drm_atomic_state *state) for_each_new_crtc_in_state(state, crtc, crtc_state, i) { struct drm_self_refresh_data *sr_data; + unsigned int delay; /* Don't trigger the entry timer when we're already in SR */ if (crtc_state->self_refresh_active) @@ -161,8 +222,13 @@ void drm_self_refresh_helper_alter_state(struct drm_atomic_state *state) if (!sr_data) continue; + mutex_lock(&sr_data->avg_mutex); + delay = ewma_psr_time_read(&sr_data->entry_avg_ms) + + ewma_psr_time_read(&sr_data->exit_avg_ms) + + sr_data->entry_delay_ms; + mutex_unlock(&sr_data->avg_mutex); mod_delayed_work(system_wq, &sr_data->entry_work, - msecs_to_jiffies(sr_data->entry_delay_ms)); + msecs_to_jiffies(delay)); } } EXPORT_SYMBOL(drm_self_refresh_helper_alter_state); @@ -191,6 +257,9 @@ int drm_self_refresh_helper_init(struct drm_crtc *crtc, drm_self_refresh_helper_entry_work); sr_data->entry_delay_ms = entry_delay_ms; sr_data->crtc = crtc; + mutex_init(&sr_data->avg_mutex); + ewma_psr_time_init(&sr_data->entry_avg_ms); + ewma_psr_time_init(&sr_data->exit_avg_ms); crtc->self_refresh_data = sr_data; return 0; diff --git a/include/drm/drm_self_refresh_helper.h b/include/drm/drm_self_refresh_helper.h index 397a583ccca7..ff777690c564 100644 --- a/include/drm/drm_self_refresh_helper.h +++ b/include/drm/drm_self_refresh_helper.h @@ -12,6 +12,8 @@ struct drm_atomic_state; struct drm_crtc; void drm_self_refresh_helper_alter_state(struct drm_atomic_state *state); +void drm_self_refresh_helper_update_avg_times(struct drm_atomic_state *state, + unsigned int commit_time_ms); int drm_self_refresh_helper_init(struct drm_crtc *crtc, unsigned int entry_delay_ms);