From patchwork Wed Aug 3 21:33:34 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Rodrigo Vivi X-Patchwork-Id: 9262235 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 DC0C36048B for ; Wed, 3 Aug 2016 21:35:02 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id CF37C2808C for ; Wed, 3 Aug 2016 21:35:02 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id C000E1FE95; Wed, 3 Aug 2016 21:35:02 +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=-4.2 required=2.0 tests=BAYES_00, RCVD_IN_DNSWL_MED autolearn=unavailable version=3.3.1 Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 4655F1FE95 for ; Wed, 3 Aug 2016 21:35:02 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 96B566E8F7; Wed, 3 Aug 2016 21:34:21 +0000 (UTC) X-Original-To: dri-devel@lists.freedesktop.org Delivered-To: dri-devel@lists.freedesktop.org Received: from mga01.intel.com (mga01.intel.com [192.55.52.88]) by gabe.freedesktop.org (Postfix) with ESMTP id 520B36E8E6; Wed, 3 Aug 2016 21:34:09 +0000 (UTC) Received: from orsmga003.jf.intel.com ([10.7.209.27]) by fmsmga101.fm.intel.com with ESMTP; 03 Aug 2016 14:34:08 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.28,467,1464678000"; d="scan'208";a="859262338" Received: from rdvivi-dublin.jf.intel.com ([10.7.196.152]) by orsmga003.jf.intel.com with ESMTP; 03 Aug 2016 14:34:08 -0700 From: Rodrigo Vivi To: intel-gfx@lists.freedesktop.org Subject: [PATCH 1/6] drm: Add vblank prepare and unprepare hooks. Date: Wed, 3 Aug 2016 14:33:34 -0700 Message-Id: <1470260019-6173-2-git-send-email-rodrigo.vivi@intel.com> X-Mailer: git-send-email 2.4.3 In-Reply-To: <1470260019-6173-1-git-send-email-rodrigo.vivi@intel.com> References: <1470260019-6173-1-git-send-email-rodrigo.vivi@intel.com> Cc: Dhinakaran Pandiyan , dri-devel@lists.freedesktop.org, Rodrigo Vivi 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-Virus-Scanned: ClamAV using ClamSMTP This will allow drivers to control specific power saving feature and power domains when dealing with vblanks. Vblanks code are protected by spin_locks where we can't have anything that can sleep. While power saving features and power domain code have mutexes to control the states. Mutex can sleep so they cannot be used inside spin lock areas. So the easiest way to deal with them currently is to add these prepare hook for pre enabling vblanks and unprepare one for post disabling them. Let's introduce this optional prepare and unprepare hooks so drivers can deal with cases like this and any other case that should require sleeping codes interacting with vblanks. v2: - Rebase after a split on drm_irq.h. - Use refcount to minimize the prepare/unprepare calls to only when enabling or disabling the vblank irq. (DK's) - Improved doc. - Fix the negative unprepare.count case. Cc: Dhinakaran Pandiyan Signed-off-by: Rodrigo Vivi --- drivers/gpu/drm/drm_irq.c | 37 ++++++++++++++++++++++++++++++++++++- include/drm/drmP.h | 43 +++++++++++++++++++++++++++++++++++++++++++ include/drm/drm_irq.h | 20 ++++++++++++++++++++ 3 files changed, 99 insertions(+), 1 deletion(-) diff --git a/drivers/gpu/drm/drm_irq.c b/drivers/gpu/drm/drm_irq.c index 77f357b..d0d1dde 100644 --- a/drivers/gpu/drm/drm_irq.c +++ b/drivers/gpu/drm/drm_irq.c @@ -339,6 +339,8 @@ void drm_vblank_cleanup(struct drm_device *dev) drm_core_check_feature(dev, DRIVER_MODESET)); del_timer_sync(&vblank->disable_timer); + + flush_work(&vblank->unprepare.work); } kfree(dev->vblank); @@ -347,6 +349,24 @@ void drm_vblank_cleanup(struct drm_device *dev) } EXPORT_SYMBOL(drm_vblank_cleanup); +static void drm_vblank_unprepare_work_fn(struct work_struct *work) +{ + struct drm_vblank_crtc *vblank; + struct drm_device *dev; + + vblank = container_of(work, typeof(*vblank), unprepare.work); + dev = vblank->dev; + + if (atomic_read(&vblank->unprepare.counter) == 0) + return; + + do { + if (dev->driver->unprepare_vblank && + atomic_read(&vblank->refcount) == 0) + dev->driver->unprepare_vblank(dev, vblank->pipe); + } while (!atomic_dec_and_test(&vblank->unprepare.counter)); +} + /** * drm_vblank_init - initialize vblank support * @dev: DRM device @@ -380,6 +400,9 @@ int drm_vblank_init(struct drm_device *dev, unsigned int num_crtcs) setup_timer(&vblank->disable_timer, vblank_disable_fn, (unsigned long)vblank); seqlock_init(&vblank->seqlock); + + INIT_WORK(&vblank->unprepare.work, + drm_vblank_unprepare_work_fn); } DRM_INFO("Supports vblank timestamp caching Rev 2 (21.10.2013).\n"); @@ -1117,6 +1140,10 @@ static int drm_vblank_get(struct drm_device *dev, unsigned int pipe) if (WARN_ON(pipe >= dev->num_crtcs)) return -EINVAL; + if (dev->driver->prepare_vblank && + atomic_read(&vblank->refcount) == 0) + dev->driver->prepare_vblank(dev, pipe); + spin_lock_irqsave(&dev->vbl_lock, irqflags); /* Going from 0->1 means we have to enable interrupts again */ if (atomic_add_return(1, &vblank->refcount) == 1) { @@ -1129,6 +1156,10 @@ static int drm_vblank_get(struct drm_device *dev, unsigned int pipe) } spin_unlock_irqrestore(&dev->vbl_lock, irqflags); + if (dev->driver->unprepare_vblank && + atomic_read(&vblank->refcount) == 0) + dev->driver->unprepare_vblank(dev, pipe); + return ret; } @@ -1178,6 +1209,11 @@ static void drm_vblank_put(struct drm_device *dev, unsigned int pipe) mod_timer(&vblank->disable_timer, jiffies + ((drm_vblank_offdelay * HZ)/1000)); } + + if (atomic_read(&vblank->refcount) == 0) { + atomic_inc(&vblank->unprepare.counter); + schedule_work(&vblank->unprepare.work); + } } /** @@ -1603,7 +1639,6 @@ static int drm_queue_vblank_event(struct drm_device *dev, unsigned int pipe, } spin_unlock_irqrestore(&dev->event_lock, flags); - return 0; err_unlock: diff --git a/include/drm/drmP.h b/include/drm/drmP.h index d377865..c6ca061 100644 --- a/include/drm/drmP.h +++ b/include/drm/drmP.h @@ -422,6 +422,49 @@ struct drm_driver { u32 (*get_vblank_counter) (struct drm_device *dev, unsigned int pipe); /** + * prepare_vblank - Optional prepare vblank hook. + * @dev: DRM device + * @pipe: counter to fetch + * + * Drivers that need to handle any kind of mutex or any other sleeping + * code in combination with vblanks need to implement this hook + * that will be called before drm_vblank_get spin_lock gets. + * + * Prepare/Unprepare was designed to be able to sleep so they are not + * free from concurrence. There might be few simultaneous calls. + * This concurrence needs to be handled on the driver side that is + * implementing these hooks. Probably with protected get/put counters. + * + * If this is used with sleeping code you need to make sure you are + * not calling drm_crtc_vblank_get inside spinlock areas or with local + * irq disabled. + * + */ + void (*prepare_vblank) (struct drm_device *dev, unsigned int pipe); + + /** + * unprepare_vblank - Optional unprepare vblank hook. + * @dev: DRM device + * @pipe: counter to fetch + * + * Drivers that need to handle any kind of mutex or any other sleeping + * code in combination with vblanks need to implement this hook + * that will be called in a work queue to be executed after spin lock + * areas of drm_vblank_put. + * + * Prepare/Unprepare was designed to be able to sleep so they are not + * free from concurrence. There might be few simultaneous calls. + * This concurrence needs to be handled on the driver side that is + * implementing these hooks. Probably with protected get/put counters. + * + * Unprepare is called from a workqueue because asynchronous + * drm_crtc_vblank_put will be in spinlock areas. So it is safe to + * keep drm_crtc_vblank_put in areas that cannot spleep. + */ + void (*unprepare_vblank) (struct drm_device *dev, unsigned int pipe); + + + /** * enable_vblank - enable vblank interrupt events * @dev: DRM device * @pipe: which irq to enable diff --git a/include/drm/drm_irq.h b/include/drm/drm_irq.h index 2401b14..93a9e9d 100644 --- a/include/drm/drm_irq.h +++ b/include/drm/drm_irq.h @@ -45,6 +45,20 @@ struct drm_pending_vblank_event { }; /** + * struct drm_vblank_unprepare - unprepare vblank + */ +struct drm_vblank_unprepare { + /** + * @work: Delayed work to handle unprepare out of spinlock areas + */ + struct work_struct work; + /** + * @counter: Number of vblanks handled + */ + atomic_t counter; +}; + +/** * struct drm_vblank_crtc - vblank tracking for a CRTC * * This structure tracks the vblank state for one CRTC. @@ -128,6 +142,12 @@ struct drm_vblank_crtc { * disabling functions multiple times. */ bool enabled; + /** + * @unprepare: Tracks the amount of drm_vblank_put handled that needs + * a delayed unprepare call so the unprepare can run out of protected + * areas where code can sleep. + */ + struct drm_vblank_unprepare unprepare; }; extern int drm_irq_install(struct drm_device *dev, int irq);