All of lore.kernel.org
 help / color / mirror / Atom feed
From: Thierry Reding <thierry.reding@gmail.com>
To: Lukas Wunner <lukas@wunner.de>
Cc: Daniel Vetter <daniel.vetter@ffwll.ch>,
	Seth Forshee <seth.forshee@canonical.com>,
	dri-devel@lists.freedesktop.org, Dave Airlie <airlied@redhat.com>
Subject: Re: [PATCH v2.1 1/3] vga_switcheroo: Add support for switching only the DDC
Date: Mon, 17 Aug 2015 12:36:55 +0200	[thread overview]
Message-ID: <20150817103653.GB8453@ulmo.nvidia.com> (raw)
In-Reply-To: <7984d8eacbb0386ca52e3fdf2ad2554dc90ff1fe.1439739853.git.lukas@wunner.de>


[-- Attachment #1.1: Type: text/plain, Size: 7643 bytes --]

On Fri, Aug 14, 2015 at 06:50:15PM +0200, Lukas Wunner wrote:
> Originally by Seth Forshee <seth.forshee@canonical.com>, 2012-10-04:
> During graphics driver initialization it's useful to be able to mux only
> the DDC to the inactive client in order to read the EDID. Add a
> switch_ddc callback to allow capable handlers to provide this
> functionality, and add vga_switcheroo_switch_ddc() to allow DRM to mux
> only the DDC.
> 
> Modified by Dave Airlie <airlied@gmail.com>, 2012-12-22:
> I can't figure out why I didn't like this, but I rewrote this [...] to
> lock/unlock the ddc lines [...]. I think I'd prefer something like that
> otherwise the interface got really ugly.
> 
> Modified by Lukas Wunner <lukas@wunner.de>, 2015-08-14:
> Don't grab vgasr_mutex in lock_ddc/unlock_ddc to avoid the following
> deadlock scenarios: (a) During switch (with vgasr_mutex already held),
> GPU is woken and probes its outputs, tries to re-acquire vgasr_mutex
> to lock DDC lines. (b) Likewise during switch, GPU is suspended and
> calls cancel_delayed_work_sync to stop output polling, if poll task
> is running at this moment we may wait forever for it to finish.
> If we don't grab vgasr_mutex the only bad thing that can happen is
> that the handler suddenly disappears. So block handler deregistration
> until DDC lines are unlocked again.
> 
> WARN_ON_ONCE if unlock_ddc is called without calling lock_ddc first.
> Lock ddc_lock in stage2 to avoid race condition where reading the
> EDID and switching happens simultaneously. Switch DDC lines on
> MIGD / MDIS commands and on runtime suspend. Fix bug in stage2
> where no client had the active attribute set if switching failed.
> Fix erroneous interface documentation.
> 
> If the inactive client registers before the active client then
> old_ddc_owner cannot be determined with find_active_client()
> (null pointer dereference). Therefore change semantics of the
> ->switch_ddc handler callback to return old_ddc_owner.
> 
> v2.1: Overhaul locking, squash commits (requested by Daniel Vetter)
> 
> Bugzilla: https://bugzilla.kernel.org/show_bug.cgi?id=88861
> Bugzilla: https://bugs.freedesktop.org/show_bug.cgi?id=61115
> Tested-by: Lukas Wunner <lukas@wunner.de>
>     [MBP  9,1 2012  intel IVB + nvidia GK107  pre-retina]
> 
> Cc: Seth Forshee <seth.forshee@canonical.com>
> Cc: Dave Airlie <airlied@gmail.com>
> Signed-off-by: Lukas Wunner <lukas@wunner.de>
> ---
>  drivers/gpu/vga/vga_switcheroo.c | 73 +++++++++++++++++++++++++++++++++++++---
>  include/linux/vga_switcheroo.h   |  6 ++++
>  2 files changed, 74 insertions(+), 5 deletions(-)
> 
> diff --git a/drivers/gpu/vga/vga_switcheroo.c b/drivers/gpu/vga/vga_switcheroo.c
> index 37ac7b5..ac4ac12 100644
> --- a/drivers/gpu/vga/vga_switcheroo.c
> +++ b/drivers/gpu/vga/vga_switcheroo.c
> @@ -9,12 +9,13 @@
>  
>   Switcher interface - methods require for ATPX and DCM
>   - switchto - this throws the output MUX switch
> - - discrete_set_power - sets the power state for the discrete card
> + - switch_ddc - switch only DDC lines, return old DDC owner
> + - power_state - sets the power state for either GPU
>  
>   GPU driver interface
>   - set_gpu_state - this should do the equiv of s/r for the card
>  		  - this should *not* set the discrete power state
> - - switch_check  - check if the device is in a position to switch now
> + - can_switch - check if the device is in a position to switch now
>   */
>  
>  #include <linux/module.h>
> @@ -57,6 +58,8 @@ struct vgasr_priv {
>  	struct list_head clients;
>  
>  	struct vga_switcheroo_handler *handler;
> +	struct mutex ddc_lock;
> +	int old_ddc_owner;
>  };
>  
>  #define ID_BIT_AUDIO		0x100
> @@ -70,6 +73,7 @@ static void vga_switcheroo_debugfs_fini(struct vgasr_priv *priv);
>  /* only one switcheroo per system */
>  static struct vgasr_priv vgasr_priv = {
>  	.clients = LIST_HEAD_INIT(vgasr_priv.clients),
> +	.ddc_lock = __MUTEX_INITIALIZER(vgasr_priv.ddc_lock),
>  };
>  
>  static bool vga_switcheroo_ready(void)
> @@ -122,12 +126,14 @@ EXPORT_SYMBOL(vga_switcheroo_register_handler);
>  void vga_switcheroo_unregister_handler(void)
>  {
>  	mutex_lock(&vgasr_mutex);
> +	mutex_lock(&vgasr_priv.ddc_lock);
>  	vgasr_priv.handler = NULL;
>  	if (vgasr_priv.active) {
>  		pr_info("vga_switcheroo: disabled\n");
>  		vga_switcheroo_debugfs_fini(&vgasr_priv);
>  		vgasr_priv.active = false;
>  	}
> +	mutex_unlock(&vgasr_priv.ddc_lock);
>  	mutex_unlock(&vgasr_mutex);
>  }
>  EXPORT_SYMBOL(vga_switcheroo_unregister_handler);
> @@ -256,6 +262,43 @@ void vga_switcheroo_client_fb_set(struct pci_dev *pdev,
>  }
>  EXPORT_SYMBOL(vga_switcheroo_client_fb_set);
>  
> +int vga_switcheroo_lock_ddc(struct pci_dev *pdev)
> +{
> +	int id;
> +
> +	mutex_lock(&vgasr_priv.ddc_lock);
> +
> +	if (!vgasr_priv.handler || !vgasr_priv.handler->switch_ddc)
> +		return vgasr_priv.old_ddc_owner = -ENODEV;

I find this very hard to read. Can this be split across two lines?

> +
> +	id = vgasr_priv.handler->get_client_id(pdev);
> +	return vgasr_priv.old_ddc_owner = vgasr_priv.handler->switch_ddc(id);

This too. I also notice that the only place you call this from doesn't
care about the return value, so why even bother returning one?

> +int vga_switcheroo_unlock_ddc(struct pci_dev *pdev)
> +{
> +	int ret, id;
> +
> +	if (WARN_ON_ONCE(!mutex_is_locked(&vgasr_priv.ddc_lock)))
> +		return -EINVAL;
> +
> +	if (vgasr_priv.old_ddc_owner < 0) {
> +		mutex_unlock(&vgasr_priv.ddc_lock);
> +		return -ENODEV;
> +	}
> +
> +	id = vgasr_priv.handler->get_client_id(pdev);
> +	if (vgasr_priv.old_ddc_owner != id)
> +		ret = vgasr_priv.handler->switch_ddc(vgasr_priv.old_ddc_owner);
> +	else
> +		ret = vgasr_priv.old_ddc_owner;
> +	mutex_unlock(&vgasr_priv.ddc_lock);
> +
> +	return ret;
> +}
> +EXPORT_SYMBOL(vga_switcheroo_unlock_ddc);

Same comment about the return value here.

> diff --git a/include/linux/vga_switcheroo.h b/include/linux/vga_switcheroo.h
> index b483abd..62f303f 100644
> --- a/include/linux/vga_switcheroo.h
> +++ b/include/linux/vga_switcheroo.h
> @@ -29,6 +29,7 @@ enum vga_switcheroo_client_id {
>  };
>  
>  struct vga_switcheroo_handler {
> +	int (*switch_ddc)(enum vga_switcheroo_client_id id);
>  	int (*switchto)(enum vga_switcheroo_client_id id);
>  	int (*power_state)(enum vga_switcheroo_client_id id,
>  			   enum vga_switcheroo_state state);
> @@ -54,6 +55,9 @@ int vga_switcheroo_register_audio_client(struct pci_dev *pdev,
>  void vga_switcheroo_client_fb_set(struct pci_dev *dev,
>  				  struct fb_info *info);
>  
> +int vga_switcheroo_lock_ddc(struct pci_dev *pdev);
> +int vga_switcheroo_unlock_ddc(struct pci_dev *pdev);
> +
>  int vga_switcheroo_register_handler(struct vga_switcheroo_handler *handler);
>  void vga_switcheroo_unregister_handler(void);
>  
> @@ -72,6 +76,8 @@ static inline void vga_switcheroo_unregister_client(struct pci_dev *dev) {}
>  static inline int vga_switcheroo_register_client(struct pci_dev *dev,
>  		const struct vga_switcheroo_client_ops *ops, bool driver_power_control) { return 0; }
>  static inline void vga_switcheroo_client_fb_set(struct pci_dev *dev, struct fb_info *info) {}
> +static inline int vga_switcheroo_lock_ddc(struct pci_dev *pdev) { return -ENODEV; }
> +static inline int vga_switcheroo_unlock_ddc(struct pci_dev *pdev) { return -ENODEV; }

If you care about the return value you'll want to return 0 here to make
sure kernels without VGA switcheroo support will continue to work
properly.

Thierry

[-- Attachment #1.2: signature.asc --]
[-- Type: application/pgp-signature, Size: 819 bytes --]

[-- Attachment #2: Type: text/plain, Size: 159 bytes --]

_______________________________________________
dri-devel mailing list
dri-devel@lists.freedesktop.org
http://lists.freedesktop.org/mailman/listinfo/dri-devel

  parent reply	other threads:[~2015-08-17 10:38 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-08-14 16:50 [PATCH v2.1 1/3] vga_switcheroo: Add support for switching only the DDC Lukas Wunner
2015-08-14 16:18 ` [PATCH v2.1 2/3] apple-gmux: Add switch_ddc support Lukas Wunner
2015-08-14 16:28   ` [PATCH v2.1 3/3] drm/edid: Switch DDC when reading the EDID Lukas Wunner
2015-08-17 10:41     ` Thierry Reding
2015-08-17 20:24       ` Lukas Wunner
2015-08-25  8:00         ` Daniel Vetter
2015-08-18  7:02     ` Jani Nikula
2015-08-18 14:16       ` Lukas Wunner
2015-08-25  8:13   ` [PATCH v2.1 2/3] apple-gmux: Add switch_ddc support Daniel Vetter
2015-08-17 10:36 ` Thierry Reding [this message]
2015-08-17 19:11   ` [PATCH v2.1 1/3] vga_switcheroo: Add support for switching only the DDC Lukas Wunner
2015-08-25  8:12 ` Daniel Vetter
2015-09-17 15:25   ` vga_switcheroo docs (was: Re: [PATCH v2.1 1/3] vga_switcheroo: Add support for switching only the DDC) Lukas Wunner

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20150817103653.GB8453@ulmo.nvidia.com \
    --to=thierry.reding@gmail.com \
    --cc=airlied@redhat.com \
    --cc=daniel.vetter@ffwll.ch \
    --cc=dri-devel@lists.freedesktop.org \
    --cc=lukas@wunner.de \
    --cc=seth.forshee@canonical.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.