From patchwork Thu Jul 19 00:22:16 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Haneen Mohammed X-Patchwork-Id: 10533729 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id CD5F6601D2 for ; Thu, 19 Jul 2018 07:07:45 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id B6AF828CDF for ; Thu, 19 Jul 2018 07:07:45 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id AA389293B0; Thu, 19 Jul 2018 07:07:45 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-5.2 required=2.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED, FREEMAIL_FROM, MAILING_LIST_MULTI, RCVD_IN_DNSWL_MED autolearn=ham version=3.3.1 Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.2 with cipher DHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 0DBBE28CDF for ; Thu, 19 Jul 2018 07:07:45 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 8895F6ED0C; Thu, 19 Jul 2018 07:04:50 +0000 (UTC) X-Original-To: dri-devel@lists.freedesktop.org Delivered-To: dri-devel@lists.freedesktop.org Received: from mail-wm0-x241.google.com (mail-wm0-x241.google.com [IPv6:2a00:1450:400c:c09::241]) by gabe.freedesktop.org (Postfix) with ESMTPS id 82BA36E319 for ; Thu, 19 Jul 2018 00:22:21 +0000 (UTC) Received: by mail-wm0-x241.google.com with SMTP id h3-v6so3953864wmb.1 for ; Wed, 18 Jul 2018 17:22:21 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to:user-agent; bh=Ne3nIYYO2JpdQaMfxIVnxyWP2OSofayF3ktYbzb8mvc=; b=Kd22E620xzfO9gRUOG0exn+RdHKkwNGoleA0w9+JpNIUL4guLUTFF2oEw3rX3OIega p05Ku72PyxUMWAc8BrrkxDjArlMkPctTqgVGFORk67TgpAIAlvLYaTi9lBaLKV3y2F0E wJx6mOapLK2kxdEsfa68aLV/t5lZZsNBZOIKhWDxhNz66q8LKxCFTWZldcNftDbn9o7V jHlo78m5fX1fAI2kXwLg2SGB5oyGs+WS/9HM7aWAeIKEeaAmQfn3O7BvY8WHZ1JN+8iZ UUhrgzYZLst0xTSuY0Q1wbDclQjv9NydYKEjgTPJQ+Or9ozc76otRVgfn20s9aosqzY3 mLww== X-Gm-Message-State: AOUpUlH2DLYakHc4LGxGrDxOzvzC6e2jJmLlw7RcvU5HYUm33epzlIMH u4Vfof8wukPXdfkiDCHfStBLfcF8 X-Google-Smtp-Source: AAOMgpcqUmHErVrv+H3BRRP3dVXRwrU87hkeAduRbnraFNGeSgT/yBdnr7Wq5FVGY6aeJ9jJyydtDw== X-Received: by 2002:a1c:99c2:: with SMTP id b185-v6mr2594432wme.15.1531959739851; Wed, 18 Jul 2018 17:22:19 -0700 (PDT) Received: from haneenDRM ([188.50.2.36]) by smtp.gmail.com with ESMTPSA id h8-v6sm2513126wmb.32.2018.07.18.17.22.18 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Wed, 18 Jul 2018 17:22:19 -0700 (PDT) Date: Thu, 19 Jul 2018 03:22:16 +0300 From: Haneen Mohammed To: dri-devel@lists.freedesktop.org Subject: [PATCH v3 5/5] drm/vkms: Implement CRC debugfs API Message-ID: References: MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.9.4 (2018-02-28) X-Mailman-Approved-At: Thu, 19 Jul 2018 07:03:43 +0000 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: hamohammed.sa@gmail.com, rodrigosiqueiramelo@gmail.com Errors-To: dri-devel-bounces@lists.freedesktop.org Sender: "dri-devel" X-Virus-Scanned: ClamAV using ClamSMTP Implement the set_crc_source() callback. Compute CRC using crc32 on the visible part of the framebuffer. Use ordered workqueue to compute and add CRC at the end of a vblank. Use appropriate synchronization methods since the CRC computation must be atomic wrt the generated vblank event for a given atomic update, by using spinlock across atomic_begin and atomic_flush to wrap the event handling code completely and match the flip event with the CRC. Signed-off-by: Haneen Mohammed --- Changes in v3: - flush work item instead of workqueue - include missing vkms_crc.c file drivers/gpu/drm/vkms/Makefile | 2 +- drivers/gpu/drm/vkms/vkms_crc.c | 66 +++++++++++++++++++++++++++++++ drivers/gpu/drm/vkms/vkms_crtc.c | 58 ++++++++++++++++++++++----- drivers/gpu/drm/vkms/vkms_drv.c | 1 + drivers/gpu/drm/vkms/vkms_drv.h | 21 ++++++++++ drivers/gpu/drm/vkms/vkms_plane.c | 10 +++++ 6 files changed, 148 insertions(+), 10 deletions(-) create mode 100644 drivers/gpu/drm/vkms/vkms_crc.c diff --git a/drivers/gpu/drm/vkms/Makefile b/drivers/gpu/drm/vkms/Makefile index 986297da51bf..37966914f70b 100644 --- a/drivers/gpu/drm/vkms/Makefile +++ b/drivers/gpu/drm/vkms/Makefile @@ -1,3 +1,3 @@ -vkms-y := vkms_drv.o vkms_plane.o vkms_output.o vkms_crtc.o vkms_gem.o +vkms-y := vkms_drv.o vkms_plane.o vkms_output.o vkms_crtc.o vkms_gem.o vkms_crc.o obj-$(CONFIG_DRM_VKMS) += vkms.o diff --git a/drivers/gpu/drm/vkms/vkms_crc.c b/drivers/gpu/drm/vkms/vkms_crc.c new file mode 100644 index 000000000000..9e8bb5c7fc80 --- /dev/null +++ b/drivers/gpu/drm/vkms/vkms_crc.c @@ -0,0 +1,66 @@ +// SPDX-License-Identifier: GPL-2.0 +#include "vkms_drv.h" +#include +#include + +static uint32_t _vkms_get_crc(struct vkms_crc_data *crc_data) +{ + struct drm_framebuffer *fb = &crc_data->fb; + struct drm_gem_object *gem_obj = drm_gem_fb_get_obj(fb, 0); + struct vkms_gem_object *vkms_obj = drm_gem_to_vkms_gem(gem_obj); + u32 crc = 0; + int i = 0; + unsigned int x = crc_data->src.x1 >> 16; + unsigned int y = crc_data->src.y1 >> 16; + unsigned int height = drm_rect_height(&crc_data->src) >> 16; + unsigned int width = drm_rect_width(&crc_data->src) >> 16; + unsigned int cpp = fb->format->cpp[0]; + unsigned int src_offset; + unsigned int size_byte = width * cpp; + void *vaddr = vkms_obj->vaddr; + + if (WARN_ON(!vaddr)) + return crc; + + for (i = y; i < y + height; i++) { + src_offset = fb->offsets[0] + (i * fb->pitches[0]) + (x * cpp); + crc = crc32_le(crc, vaddr + src_offset, size_byte); + } + + return crc; +} + +void vkms_crc_work_handle(struct work_struct *work) +{ + struct vkms_crtc_state *crtc_state = container_of(work, + struct vkms_crtc_state, + crc_work); + struct drm_crtc *crtc = crtc_state->base.crtc; + struct vkms_output *out = drm_crtc_to_vkms_output(crtc); + + if (crtc_state && out->crc_enabled) { + u32 crc32 = _vkms_get_crc(&crtc_state->data); + unsigned int n_frame = crtc_state->n_frame; + + drm_crtc_add_crc_entry(crtc, true, n_frame, &crc32); + } +} + +int vkms_set_crc_source(struct drm_crtc *crtc, const char *src_name, + size_t *values_cnt) +{ + struct vkms_output *out = drm_crtc_to_vkms_output(crtc); + + if (!src_name) { + out->crc_enabled = false; + } else if (strcmp(src_name, "auto") == 0) { + out->crc_enabled = true; + } else { + out->crc_enabled = false; + return -EINVAL; + } + + *values_cnt = 1; + + return 0; +} diff --git a/drivers/gpu/drm/vkms/vkms_crtc.c b/drivers/gpu/drm/vkms/vkms_crtc.c index 26babb85ca77..a7b39627b581 100644 --- a/drivers/gpu/drm/vkms/vkms_crtc.c +++ b/drivers/gpu/drm/vkms/vkms_crtc.c @@ -10,17 +10,32 @@ #include #include -static enum hrtimer_restart vkms_vblank_simulate(struct hrtimer *timer) +static void _vblank_handle(struct vkms_output *output) { - struct vkms_output *output = container_of(timer, struct vkms_output, - vblank_hrtimer); struct drm_crtc *crtc = &output->crtc; - int ret_overrun; + struct vkms_crtc_state *state = to_vkms_crtc_state(crtc->state); bool ret; + spin_lock(&output->lock); ret = drm_crtc_handle_vblank(crtc); if (!ret) - DRM_ERROR("vkms failure on handling vblank"); + DRM_ERROR("vkms failure on handling vblank\n"); + + if (state && output->crc_enabled) { + state->n_frame = drm_crtc_accurate_vblank_count(crtc); + queue_work(output->crc_workq, &state->crc_work); + } + + spin_unlock(&output->lock); +} + +static enum hrtimer_restart vkms_vblank_simulate(struct hrtimer *timer) +{ + struct vkms_output *output = container_of(timer, struct vkms_output, + vblank_hrtimer); + int ret_overrun; + + _vblank_handle(output); ret_overrun = hrtimer_forward_now(&output->vblank_hrtimer, output->period_ns); @@ -97,6 +112,8 @@ vkms_atomic_crtc_duplicate_state(struct drm_crtc *crtc) __drm_atomic_helper_crtc_duplicate_state(crtc, &vkms_state->base); + INIT_WORK(&vkms_state->crc_work, vkms_crc_work_handle); + return &vkms_state->base; } @@ -108,7 +125,13 @@ static void vkms_atomic_crtc_destroy_state(struct drm_crtc *crtc, vkms_state = to_vkms_crtc_state(state); __drm_atomic_helper_crtc_destroy_state(state); - kfree(vkms_state); + + if (vkms_state) { + flush_work(&vkms_state->crc_work); + drm_framebuffer_put(&vkms_state->data.fb); + memset(&vkms_state->data, 0, sizeof(struct vkms_crc_data)); + kfree(vkms_state); + } } static const struct drm_crtc_funcs vkms_crtc_funcs = { @@ -120,6 +143,7 @@ static const struct drm_crtc_funcs vkms_crtc_funcs = { .atomic_destroy_state = vkms_atomic_crtc_destroy_state, .enable_vblank = vkms_enable_vblank, .disable_vblank = vkms_disable_vblank, + .set_crc_source = vkms_set_crc_source, }; static void vkms_crtc_atomic_enable(struct drm_crtc *crtc, @@ -134,26 +158,37 @@ static void vkms_crtc_atomic_disable(struct drm_crtc *crtc, drm_crtc_vblank_off(crtc); } +static void vkms_crtc_atomic_begin(struct drm_crtc *crtc, + struct drm_crtc_state *old_crtc_state) +{ + struct vkms_output *vkms_output = drm_crtc_to_vkms_output(crtc); + + spin_lock_irq(&vkms_output->lock); +} + static void vkms_crtc_atomic_flush(struct drm_crtc *crtc, struct drm_crtc_state *old_crtc_state) { - unsigned long flags; + struct vkms_output *vkms_output = drm_crtc_to_vkms_output(crtc); if (crtc->state->event) { - spin_lock_irqsave(&crtc->dev->event_lock, flags); + spin_lock(&crtc->dev->event_lock); if (drm_crtc_vblank_get(crtc) != 0) drm_crtc_send_vblank_event(crtc, crtc->state->event); else drm_crtc_arm_vblank_event(crtc, crtc->state->event); - spin_unlock_irqrestore(&crtc->dev->event_lock, flags); + spin_unlock(&crtc->dev->event_lock); crtc->state->event = NULL; } + + spin_unlock_irq(&vkms_output->lock); } static const struct drm_crtc_helper_funcs vkms_crtc_helper_funcs = { + .atomic_begin = vkms_crtc_atomic_begin, .atomic_flush = vkms_crtc_atomic_flush, .atomic_enable = vkms_crtc_atomic_enable, .atomic_disable = vkms_crtc_atomic_disable, @@ -162,6 +197,7 @@ static const struct drm_crtc_helper_funcs vkms_crtc_helper_funcs = { int vkms_crtc_init(struct drm_device *dev, struct drm_crtc *crtc, struct drm_plane *primary, struct drm_plane *cursor) { + struct vkms_output *vkms_out = drm_crtc_to_vkms_output(crtc); int ret; ret = drm_crtc_init_with_planes(dev, crtc, primary, cursor, @@ -173,5 +209,9 @@ int vkms_crtc_init(struct drm_device *dev, struct drm_crtc *crtc, drm_crtc_helper_add(crtc, &vkms_crtc_helper_funcs); + spin_lock_init(&vkms_out->lock); + + vkms_out->crc_workq = alloc_ordered_workqueue("vkms_crc_workq", 0); + return ret; } diff --git a/drivers/gpu/drm/vkms/vkms_drv.c b/drivers/gpu/drm/vkms/vkms_drv.c index 37aa2ef33b21..5d78bd97e69c 100644 --- a/drivers/gpu/drm/vkms/vkms_drv.c +++ b/drivers/gpu/drm/vkms/vkms_drv.c @@ -46,6 +46,7 @@ static void vkms_release(struct drm_device *dev) platform_device_unregister(vkms->platform); drm_mode_config_cleanup(&vkms->drm); drm_dev_fini(&vkms->drm); + destroy_workqueue(vkms->output.crc_workq); } static struct drm_driver vkms_driver = { diff --git a/drivers/gpu/drm/vkms/vkms_drv.h b/drivers/gpu/drm/vkms/vkms_drv.h index 75e52d61e65d..a80281a12010 100644 --- a/drivers/gpu/drm/vkms/vkms_drv.h +++ b/drivers/gpu/drm/vkms/vkms_drv.h @@ -20,12 +20,23 @@ static const u32 vkms_formats[] = { DRM_FORMAT_XRGB8888, }; +struct vkms_crc_data { + struct drm_rect src; + struct drm_framebuffer fb; +}; + /** * vkms_crtc_state - Driver specific CRTC state * @base: base CRTC state + * @crc_work: work struct to compute and add CRC entries + * @data: data required for CRC computation + * @n_frame: frame number for computed CRC */ struct vkms_crtc_state { struct drm_crtc_state base; + struct work_struct crc_work; + struct vkms_crc_data data; + unsigned int n_frame; }; struct vkms_output { @@ -35,6 +46,11 @@ struct vkms_output { struct hrtimer vblank_hrtimer; ktime_t period_ns; struct drm_pending_vblank_event *event; + bool crc_enabled; + /* ordered wq for crc_work */ + struct workqueue_struct *crc_workq; + /* protects concurrent access to crc_data */ + spinlock_t lock; }; struct vkms_device { @@ -95,4 +111,9 @@ int vkms_gem_vmap(struct drm_gem_object *obj); void vkms_gem_vunmap(struct drm_gem_object *obj); +/* CRC Support */ +int vkms_set_crc_source(struct drm_crtc *crtc, const char *src_name, + size_t *values_cnt); +void vkms_crc_work_handle(struct work_struct *work); + #endif /* _VKMS_DRV_H_ */ diff --git a/drivers/gpu/drm/vkms/vkms_plane.c b/drivers/gpu/drm/vkms/vkms_plane.c index 5b7c5d59f103..b316b2af88ee 100644 --- a/drivers/gpu/drm/vkms/vkms_plane.c +++ b/drivers/gpu/drm/vkms/vkms_plane.c @@ -24,6 +24,16 @@ static const struct drm_plane_funcs vkms_plane_funcs = { static void vkms_primary_plane_update(struct drm_plane *plane, struct drm_plane_state *old_state) { + struct vkms_crtc_state *state; + + if (!plane->state->crtc || !plane->state->fb) + return; + + state = to_vkms_crtc_state(plane->state->crtc->state); + memcpy(&state->data.src, &plane->state->src, sizeof(struct drm_rect)); + memcpy(&state->data.fb, plane->state->fb, + sizeof(struct drm_framebuffer)); + drm_framebuffer_get(&state->data.fb); } static int vkms_plane_atomic_check(struct drm_plane *plane,