From patchwork Tue Aug 11 11:37:44 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Thierry Reding X-Patchwork-Id: 6991821 Return-Path: X-Original-To: patchwork-dri-devel@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork2.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork2.web.kernel.org (Postfix) with ESMTP id A5329C05AC for ; Tue, 11 Aug 2015 11:38:45 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 983FD20656 for ; Tue, 11 Aug 2015 11:38:44 +0000 (UTC) Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) by mail.kernel.org (Postfix) with ESMTP id 2921520456 for ; Tue, 11 Aug 2015 11:38:43 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id ED3EC6E7B1; Tue, 11 Aug 2015 04:38:41 -0700 (PDT) X-Original-To: dri-devel@lists.freedesktop.org Delivered-To: dri-devel@lists.freedesktop.org Received: from mail-wi0-f180.google.com (mail-wi0-f180.google.com [209.85.212.180]) by gabe.freedesktop.org (Postfix) with ESMTPS id 01D186E4C1 for ; Tue, 11 Aug 2015 04:38:40 -0700 (PDT) Received: by wicne3 with SMTP id ne3so56926137wic.0 for ; Tue, 11 Aug 2015 04:38:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=Y6M7wZciIz+BzQGLd0fxVAXoCF8Hji6wiXF1onSHpFc=; b=fyiw4rhP7cssmsZhlSiTZcFx6vj8LZs1u03ZB9uhhdDejnasJHBBVXrdQHFIcDZcRY E+idy2KvD+NaVnoipeR5QTAE1c1FoJvpChl67T6ts9F1DujfOg7/Rn47exkbhjfQpua6 rJoPFFpaYoMl0CxArCBvPJxXRMSE4P9iirHNjFhTja895Iat3MFm4Gjt4ajdZmx49Lam EybewZBnAPAEjL+wRP57eTEzE0UUtYozFS1xO/HMJmzc4MwTYS/TQ4jYE2Lg3eOyvYpA CPDXyMOc31O1ZLIujf4lg10ps0gk7gJOPyLCoZjLPEb9h7leb8lkrkO0+S9rno48cI9U 8wXg== X-Received: by 10.180.83.72 with SMTP id o8mr36053489wiy.27.1439293118608; Tue, 11 Aug 2015 04:38:38 -0700 (PDT) Received: from localhost (port-49969.pppoe.wtnet.de. [46.59.195.205]) by smtp.gmail.com with ESMTPSA id lj2sm18431517wic.1.2015.08.11.04.38.37 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 11 Aug 2015 04:38:37 -0700 (PDT) From: Thierry Reding To: dri-devel@lists.freedesktop.org Subject: [PATCH 2/3] drm/atomic-helper: Implement subsystem-level suspend/resume Date: Tue, 11 Aug 2015 13:37:44 +0200 Message-Id: <1439293065-15941-2-git-send-email-thierry.reding@gmail.com> X-Mailer: git-send-email 2.4.5 In-Reply-To: <1439293065-15941-1-git-send-email-thierry.reding@gmail.com> References: <1439293065-15941-1-git-send-email-thierry.reding@gmail.com> X-BeenThere: dri-devel@lists.freedesktop.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: Direct Rendering Infrastructure - Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: dri-devel-bounces@lists.freedesktop.org Sender: "dri-devel" X-Spam-Status: No, score=-4.3 required=5.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED, DKIM_SIGNED, FREEMAIL_FROM, RCVD_IN_DNSWL_MED, RP_MATCHES_RCVD, T_DKIM_INVALID, UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP From: Thierry Reding Provide subsystem-level suspend and resume helpers that can be used to implement suspend/resume on atomic mode-setting enabled drivers. Signed-off-by: Thierry Reding --- drivers/gpu/drm/drm_atomic_helper.c | 191 ++++++++++++++++++++++++++++++++++++ include/drm/drm_atomic_helper.h | 5 + 2 files changed, 196 insertions(+) diff --git a/drivers/gpu/drm/drm_atomic_helper.c b/drivers/gpu/drm/drm_atomic_helper.c index f842499ca79e..aface21b713e 100644 --- a/drivers/gpu/drm/drm_atomic_helper.c +++ b/drivers/gpu/drm/drm_atomic_helper.c @@ -1751,6 +1751,197 @@ backoff: EXPORT_SYMBOL(drm_atomic_helper_set_config); /** + * drm_atomic_helper_disable_all - disable all currently active outputs + * @dev: DRM device + * + * Loops through all connectors, finding those that aren't turned off and then + * turns them off by setting their DPMS mode to OFF and deactivating the CRTC + * that they are connected to. + * + * Returns: 0 on success or a negative error code on failure. + */ +int drm_atomic_helper_disable_all(struct drm_device *dev) +{ + struct drm_mode_config *config = &dev->mode_config; + struct drm_connector_state *conn_state; + struct drm_modeset_acquire_ctx ctx; + struct drm_atomic_state *state; + struct drm_connector *conn; + unsigned int i; + int err; + + state = drm_atomic_state_alloc(dev); + if (!state) + return -ENOMEM; + + drm_modeset_acquire_init(&ctx, 0); + + state->allow_modeset = true; + state->acquire_ctx = &ctx; + +retry: + err = drm_modeset_lock(&config->connection_mutex, &ctx); + if (err < 0) + goto free; + + drm_for_each_connector(conn, dev) { + struct drm_crtc *crtc = conn->state->crtc; + struct drm_crtc_state *crtc_state; + + if (!crtc || conn->dpms != DRM_MODE_DPMS_ON) + continue; + + crtc_state = drm_atomic_get_crtc_state(state, crtc); + if (IS_ERR(crtc_state)) { + err = PTR_ERR(crtc_state); + goto unlock; + } + + err = drm_atomic_add_affected_connectors(state, crtc); + if (err < 0) + goto unlock; + + conn->dpms = DRM_MODE_DPMS_OFF; + crtc_state->active = false; + } + + err = drm_atomic_commit(state); + if (err < 0) { + /* restore DPMS mode */ + for_each_connector_in_state(state, conn, conn_state, i) + conn->dpms = DRM_MODE_DPMS_ON; + + if (err == -EDEADLK) { + drm_atomic_state_clear(state); + drm_modeset_backoff(&ctx); + goto retry; + } + } + +unlock: + drm_modeset_drop_locks(&ctx); +free: + drm_modeset_acquire_fini(&ctx); + + if (err < 0) + drm_atomic_state_free(state); + + return err; +} +EXPORT_SYMBOL(drm_atomic_helper_disable_all); + +/** + * drm_atomic_helper_restore_commit - restores an atomic state object + * @state: atomic state object + * + * Takes an atomic state object for which no locks have been taken (such as + * atomic state objects obtained from drm_atomic_helper_duplicate_state()), + * grabs all the necessary locks, commits the state and releases the locks + * while gracefully handling deadlocks. + * + * Returns: 0 on success or a negative error code on failure. + */ +static int drm_atomic_helper_restore_commit(struct drm_atomic_state *state) +{ + struct drm_mode_config *config = &state->dev->mode_config; + struct drm_plane_state *plane_state; + struct drm_modeset_acquire_ctx ctx; + struct drm_crtc_state *crtc_state; + struct drm_plane *plane; + struct drm_crtc *crtc; + unsigned int i; + int err; + + drm_modeset_acquire_init(&ctx, 0); + state->allow_modeset = true; + state->acquire_ctx = &ctx; + +retry: + err = drm_modeset_lock(&config->connection_mutex, &ctx); + if (err < 0) + goto fini; + + for_each_crtc_in_state(state, crtc, crtc_state, i) { + err = drm_modeset_lock(&crtc->mutex, &ctx); + if (err < 0) + goto unlock; + } + + for_each_plane_in_state(state, plane, plane_state, i) { + err = drm_modeset_lock(&plane->mutex, &ctx); + if (err < 0) + goto unlock; + } + + err = drm_atomic_commit(state); + if (err == -EDEADLK) { + drm_modeset_backoff(&ctx); + goto retry; + } + +unlock: + drm_modeset_drop_locks(&ctx); +fini: + drm_modeset_acquire_fini(&ctx); + + return err; +} + +/** + * drm_atomic_helper_suspend - subsystem-level suspend helper + * @dev: DRM device + * + * Duplicates the current atomic state, disables all active outputs and then + * returns a pointer to the original atomic state to the caller. Drivers can + * pass this pointer to the drm_atomic_helper_resume() helper upon resume to + * restore the output configuration that was active at the time the system + * entered suspend. + * + * Returns: A pointer to a copy of the state before suspend on success or an + * ERR_PTR()-encoded error code on failure. Drivers should store the returned + * atomic state object and pass it to the drm_atomic_helper_resume() helper + * upon resume. + */ +struct drm_atomic_state *drm_atomic_helper_suspend(struct drm_device *dev) +{ + struct drm_atomic_state *state; + int err; + + state = drm_atomic_helper_duplicate_state(dev); + if (IS_ERR(state)) + return state; + + err = drm_atomic_helper_disable_all(dev); + if (err < 0) { + drm_atomic_state_free(state); + return ERR_PTR(err); + } + + return state; +} +EXPORT_SYMBOL(drm_atomic_helper_suspend); + +/** + * drm_atomic_helper_resume - subsystem-level resume helper + * @dev: DRM device + * @state: atomic state to resume to + * + * Takes an atomic state object, grabs all the necessary locks and commits it. + * This can be used in conjunction with the drm_atomic_helper_suspend() helper + * to implement suspend/resume for atomic mode-setting enabled drivers. + * + * Returns: 0 on success or a negative error code on failure. + */ +int drm_atomic_helper_resume(struct drm_device *dev, + struct drm_atomic_state *state) +{ + drm_mode_config_reset(dev); + + return drm_atomic_helper_restore_commit(state); +} +EXPORT_SYMBOL(drm_atomic_helper_resume); + +/** * drm_atomic_helper_crtc_set_property - helper for crtc properties * @crtc: DRM crtc * @property: DRM property diff --git a/include/drm/drm_atomic_helper.h b/include/drm/drm_atomic_helper.h index 5d27612997ce..080f131c9f2e 100644 --- a/include/drm/drm_atomic_helper.h +++ b/include/drm/drm_atomic_helper.h @@ -75,6 +75,11 @@ int drm_atomic_helper_update_plane(struct drm_plane *plane, int drm_atomic_helper_disable_plane(struct drm_plane *plane); int drm_atomic_helper_set_config(struct drm_mode_set *set); +int drm_atomic_helper_disable_all(struct drm_device *dev); +struct drm_atomic_state *drm_atomic_helper_suspend(struct drm_device *dev); +int drm_atomic_helper_resume(struct drm_device *dev, + struct drm_atomic_state *state); + int drm_atomic_helper_crtc_set_property(struct drm_crtc *crtc, struct drm_property *property, uint64_t val);