From patchwork Mon Apr 25 22:33:28 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Gustavo Padovan X-Patchwork-Id: 8933101 Return-Path: X-Original-To: patchwork-dri-devel@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork1.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork1.web.kernel.org (Postfix) with ESMTP id 0D3A19F372 for ; Mon, 25 Apr 2016 22:34:25 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id CFCED20165 for ; Mon, 25 Apr 2016 22:34:23 +0000 (UTC) Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) by mail.kernel.org (Postfix) with ESMTP id AA1DF20120 for ; Mon, 25 Apr 2016 22:34:22 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 53E7F6E6FB; Mon, 25 Apr 2016 22:34:15 +0000 (UTC) X-Original-To: dri-devel@lists.freedesktop.org Delivered-To: dri-devel@lists.freedesktop.org Received: from mail-yw0-f193.google.com (mail-yw0-f193.google.com [209.85.161.193]) by gabe.freedesktop.org (Postfix) with ESMTPS id 973946E6CD for ; Mon, 25 Apr 2016 22:34:09 +0000 (UTC) Received: by mail-yw0-f193.google.com with SMTP id u62so1267176ywe.3 for ; Mon, 25 Apr 2016 15:34:09 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=zsJ+T2zQVg+2M7/NQKVfNQpL4Qv8lGewwJH4LuYyCjU=; b=jGHK3n3IuTCOhku11xTu9R6UB8pC9VJ9c1jewEJKSCMy860ZZ4bmcEZm03rIEuUMJX K2Qp463HmE9c9pqlKcEOwi3EX6A1i36X1VssI8E12aPDLASYpcB1lLU9WZtbuctC6Mwr s0FBQ92P/EkAoQtRPJos4NyaG3gnWSBzOUYum/YmYX3lMM5+uWfQDhQ4KeVdBR+S8oTo Bp+cwk4ldX1wCb1tag4DfkQWMY/3sTvgE0oAHppo3C4dFEhpIbZ/eK8FdjL4mlTsB6iD YcsmwQlBVaWGHcrcQ1622XDXAQ4kg5Ixj7mpitzGECrXE/GU3DYS37njeAn7Ic/F1jgY nWuw== X-Gm-Message-State: AOPr4FU8qcjA/NI3Wg0mGE36OTxjIKK3r+P9HB68v9JNwplOpJQgyk0MB4vBi5NhFZCqRA== X-Received: by 10.129.155.73 with SMTP id s70mr22353354ywg.24.1461623648206; Mon, 25 Apr 2016 15:34:08 -0700 (PDT) Received: from jade.localdomain ([201.82.24.203]) by smtp.gmail.com with ESMTPSA id r8sm13840742ywb.20.2016.04.25.15.34.04 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 25 Apr 2016 15:34:07 -0700 (PDT) From: Gustavo Padovan To: Greg Kroah-Hartman Subject: [RFC v2 8/8] drm/fence: add out-fences support Date: Mon, 25 Apr 2016 19:33:28 -0300 Message-Id: <1461623608-29538-9-git-send-email-gustavo@padovan.org> X-Mailer: git-send-email 2.5.5 In-Reply-To: <1461623608-29538-1-git-send-email-gustavo@padovan.org> References: <1461623608-29538-1-git-send-email-gustavo@padovan.org> Cc: devel@driverdev.osuosl.org, Daniel Stone , Daniel Vetter , Riley Andrews , dri-devel@lists.freedesktop.org, linux-kernel@vger.kernel.org, =?UTF-8?q?Arve=20Hj=C3=B8nnev=C3=A5g?= , Gustavo Padovan , John Harrison 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=-3.8 required=5.0 tests=BAYES_00, RCVD_IN_BL_SPAMCOP_NET, RCVD_IN_DNSWL_MED, RP_MATCHES_RCVD, 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: Gustavo Padovan Support DRM out-fences creating a sync_file with a fence for each crtc update with the DRM_MODE_ATOMIC_OUT_FENCE flag. We then send an struct drm_out_fences array with the out-fences fds back in the drm_atomic_ioctl() as an out arg in the out_fences_ptr field. struct drm_out_fences { __u32 crtc_id; __u32 fd; }; v2: Comment by Rob Clark: - Squash commit that adds DRM_MODE_ATOMIC_OUT_FENCE flag here. Comment by Daniel Vetter: - Add clean up code for out_fences Signed-off-by: Gustavo Padovan --- drivers/gpu/drm/drm_atomic.c | 163 +++++++++++++++++++++++++++++++++++++++++-- include/drm/drm_crtc.h | 10 +++ include/uapi/drm/drm_mode.h | 11 ++- 3 files changed, 179 insertions(+), 5 deletions(-) diff --git a/drivers/gpu/drm/drm_atomic.c b/drivers/gpu/drm/drm_atomic.c index 5f9d434..06c6007 100644 --- a/drivers/gpu/drm/drm_atomic.c +++ b/drivers/gpu/drm/drm_atomic.c @@ -1566,6 +1566,133 @@ void drm_atomic_clean_old_fb(struct drm_device *dev, } EXPORT_SYMBOL(drm_atomic_clean_old_fb); +static struct drm_out_fence_state *get_out_fence(struct drm_device *dev, + struct drm_atomic_state *state, + uint32_t __user *out_fences_ptr, + uint64_t count_out_fences, + uint64_t user_data) +{ + struct drm_crtc *crtc; + struct drm_crtc_state *crtc_state; + struct drm_out_fences *out_fences; + struct drm_out_fence_state *fence_state; + int num_fences = 0; + int i, ret; + + if (count_out_fences > dev->mode_config.num_crtc) + return ERR_PTR(-EINVAL); + + out_fences = kcalloc(count_out_fences, sizeof(*out_fences), + GFP_KERNEL); + if (!out_fences) + return ERR_PTR(-ENOMEM); + + fence_state = kcalloc(count_out_fences, sizeof(*fence_state), + GFP_KERNEL); + if (!fence_state) { + kfree(out_fences); + return ERR_PTR(-ENOMEM); + } + + for (i = 0 ; i < count_out_fences ; i++) + fence_state[i].fd = -1; + + for_each_crtc_in_state(state, crtc, crtc_state, i) { + struct drm_pending_vblank_event *e; + struct fence *fence; + char name[32]; + + fence = kzalloc(sizeof(*fence), GFP_KERNEL); + if (!fence) { + ret = -ENOMEM; + goto out; + } + + fence_init(fence, &drm_crtc_fence_ops, &crtc->fence_lock, + crtc->fence_context, crtc->fence_seqno); + + snprintf(name, sizeof(name), "crtc-%d_%lu", + drm_crtc_index(crtc), crtc->fence_seqno++); + + fence_state[i].fd = get_unused_fd_flags(O_CLOEXEC); + if (fence_state[i].fd < 0) { + fence_put(fence); + ret = fence_state[i].fd; + goto out; + } + + fence_state[i].sync_file = sync_file_create(name, fence); + if(!fence_state[i].sync_file) { + fence_put(fence); + ret = -ENOMEM; + goto out; + } + + if (crtc_state->event) { + crtc_state->event->base.fence = fence; + } else { + e = create_vblank_event(dev, NULL, fence, user_data); + if (!e) { + ret = -ENOMEM; + goto out; + } + + crtc_state->event = e; + } + + out_fences[num_fences].crtc_id = crtc->base.id; + out_fences[num_fences].fd = fence_state[i].fd; + num_fences++; + } + + if (copy_to_user(out_fences_ptr, out_fences, + num_fences * sizeof(*out_fences))) { + ret = -EFAULT; + goto out; + } + + kfree(out_fences); + + return fence_state; + +out: + for (i = 0 ; i < count_out_fences ; i++) { + if (fence_state[i].sync_file) + sync_file_put(fence_state[i].sync_file); + if (fence_state[i].fd >= 0) + put_unused_fd(fence_state[i].fd); + } + + kfree(fence_state); + kfree(out_fences); + + return ERR_PTR(ret); +} + +static void install_out_fence(uint64_t count_out_fences, + struct drm_out_fence_state *state) +{ + int i; + + for (i = 0 ; i < count_out_fences ; i++) { + if (state[i].sync_file) + sync_file_install(state[i].sync_file, state[i].fd); + } +} + +static void release_out_fence(uint64_t count_out_fences, + struct drm_out_fence_state *state) +{ + int i; + + for (i = 0 ; i < count_out_fences ; i++) { + if (state->sync_file) + sync_file_put(state->sync_file); + if (state->fd >= 0) + put_unused_fd(state->fd); + } +} + int drm_mode_atomic_ioctl(struct drm_device *dev, void *data, struct drm_file *file_priv) { @@ -1574,12 +1701,14 @@ int drm_mode_atomic_ioctl(struct drm_device *dev, uint32_t __user *count_props_ptr = (uint32_t __user *)(unsigned long)(arg->count_props_ptr); uint32_t __user *props_ptr = (uint32_t __user *)(unsigned long)(arg->props_ptr); uint64_t __user *prop_values_ptr = (uint64_t __user *)(unsigned long)(arg->prop_values_ptr); + uint32_t __user *out_fences_ptr = (uint32_t __user *)(unsigned long)(arg->out_fences_ptr); unsigned int copied_objs, copied_props; struct drm_atomic_state *state; struct drm_modeset_acquire_ctx ctx; struct drm_plane *plane; struct drm_crtc *crtc; struct drm_crtc_state *crtc_state; + struct drm_out_fence_state *fence_state = NULL; unsigned plane_mask; int ret = 0; unsigned int i, j; @@ -1605,9 +1734,13 @@ int drm_mode_atomic_ioctl(struct drm_device *dev, !dev->mode_config.async_page_flip) return -EINVAL; + if ((arg->flags & DRM_MODE_ATOMIC_OUT_FENCE) && !arg->count_out_fences) + return -EINVAL; + /* can't test and expect an event at the same time. */ if ((arg->flags & DRM_MODE_ATOMIC_TEST_ONLY) && - (arg->flags & DRM_MODE_PAGE_FLIP_EVENT)) + (arg->flags & (DRM_MODE_PAGE_FLIP_EVENT + | DRM_MODE_ATOMIC_OUT_FENCE))) return -EINVAL; drm_modeset_acquire_init(&ctx, 0); @@ -1699,16 +1832,30 @@ retry: } } + if (arg->flags & DRM_MODE_ATOMIC_OUT_FENCE) { + fence_state = get_out_fence(dev, state, out_fences_ptr, + arg->count_out_fences, + arg->user_data); + if (IS_ERR(fence_state)) { + ret = PTR_ERR(fence_state); + goto out; + } + } + if (arg->flags & DRM_MODE_ATOMIC_TEST_ONLY) { /* * Unlike commit, check_only does not clean up state. * Below we call drm_atomic_state_free for it. */ ret = drm_atomic_check_only(state); - } else if (arg->flags & DRM_MODE_ATOMIC_NONBLOCK) { - ret = drm_atomic_async_commit(state); } else { - ret = drm_atomic_commit(state); + if (arg->flags & DRM_MODE_ATOMIC_OUT_FENCE) + install_out_fence(arg->count_out_fences, fence_state); + + if (arg->flags & DRM_MODE_ATOMIC_NONBLOCK) + ret = drm_atomic_async_commit(state); + else + ret = drm_atomic_commit(state); } out: @@ -1729,6 +1876,14 @@ out: } } + if ((arg->flags & DRM_MODE_ATOMIC_OUT_FENCE) && + !IS_ERR_OR_NULL(fence_state)) { + if (ret) + release_out_fence(arg->count_out_fences, fence_state); + + kfree(fence_state); + } + if (ret == -EDEADLK) { drm_atomic_state_clear(state); drm_modeset_backoff(&ctx); diff --git a/include/drm/drm_crtc.h b/include/drm/drm_crtc.h index d8c32c8..5f7c272 100644 --- a/include/drm/drm_crtc.h +++ b/include/drm/drm_crtc.h @@ -798,6 +798,16 @@ static inline struct drm_crtc *fence_to_crtc(struct fence *fence) } /** + * struct drm_out_fence_state - store out_fence data for clean up + * @sync_file: sync_file related to the out_fence + * @fd: file descriptor to installed on the sync_file. + */ +struct drm_out_fence_state { + struct sync_file *sync_file; + int fd; +}; + +/** * struct drm_connector_state - mutable connector state * @connector: backpointer to the connector * @crtc: CRTC to connect connector to, NULL if disabled diff --git a/include/uapi/drm/drm_mode.h b/include/uapi/drm/drm_mode.h index 7a7856e..4cdcd22 100644 --- a/include/uapi/drm/drm_mode.h +++ b/include/uapi/drm/drm_mode.h @@ -582,13 +582,20 @@ struct drm_mode_destroy_dumb { #define DRM_MODE_ATOMIC_TEST_ONLY 0x0100 #define DRM_MODE_ATOMIC_NONBLOCK 0x0200 #define DRM_MODE_ATOMIC_ALLOW_MODESET 0x0400 +#define DRM_MODE_ATOMIC_OUT_FENCE 0x0800 #define DRM_MODE_ATOMIC_FLAGS (\ DRM_MODE_PAGE_FLIP_EVENT |\ DRM_MODE_PAGE_FLIP_ASYNC |\ DRM_MODE_ATOMIC_TEST_ONLY |\ DRM_MODE_ATOMIC_NONBLOCK |\ - DRM_MODE_ATOMIC_ALLOW_MODESET) + DRM_MODE_ATOMIC_ALLOW_MODESET |\ + DRM_MODE_ATOMIC_OUT_FENCE) + +struct drm_out_fences { + __u32 crtc_id; + __u32 fd; +}; struct drm_mode_atomic { __u32 flags; @@ -599,6 +606,8 @@ struct drm_mode_atomic { __u64 prop_values_ptr; __u64 reserved; __u64 user_data; + __u64 count_out_fences; + __u64 out_fences_ptr; }; /**