diff mbox

[v2,8/9] acpi: Add support for Apple Gmux _DMS

Message ID 1432795249-4458-8-git-send-email-pierre.morrow@free.fr (mailing list archive)
State New, archived
Headers show

Commit Message

Pierre Moreau May 28, 2015, 6:40 a.m. UTC
Changes since v1:
* Factorise testing of the 3 different _DSMs presence with a single function
* Check for gmux in nouveau_switcheroo_set_state
* Change some comments and messages that were referring explicitly to Optimus
  whereas they were also testing the mux and gmux _DSMs

Signed-off-by: Pierre Moreau <pierre.morrow@free.fr>
---
 drm/nouveau/nouveau_acpi.c | 57 ++++++++++++++++++++++++++++++++++++++++++++--
 drm/nouveau/nouveau_acpi.h |  4 ++++
 drm/nouveau/nouveau_drm.c  | 12 +++++-----
 drm/nouveau/nouveau_vga.c  | 10 ++++----
 4 files changed, 70 insertions(+), 13 deletions(-)

Comments

Pierre Moreau May 28, 2015, 8:52 a.m. UTC | #1
Hi Dave,


----- Mail original -----
> Changes since v1:
[...]
> diff --git a/drm/nouveau/nouveau_vga.c b/drm/nouveau/nouveau_vga.c
> index 9a6328f..7b13804 100644
> --- a/drm/nouveau/nouveau_vga.c
> +++ b/drm/nouveau/nouveau_vga.c
> @@ -36,7 +36,7 @@ nouveau_switcheroo_set_state(struct pci_dev *pdev,
>  {
>  	struct drm_device *dev = pci_get_drvdata(pdev);
>  
> -	if ((nouveau_is_optimus() || nouveau_has_mux()) && state ==
> VGA_SWITCHEROO_OFF)

If I understand it correctly, if the laptop is an Optimus one or has a mux, we are not "allowed" to opt-out of DynPwr/DynOff by powering down the card?
In the same commit adding this conditional (5addcf0a5f0fadceba6bd562d0616a1c5d4c1a4d), you added the possibility to enable/disable dynpm. How is it supposed to work, by simply echo'ing ON or OFF to vga_switcheroo/switch? Then I probably forgot some stuff as it doesn't want to work on my laptop.

Pierre

> +	if (nouveau_has_dsm() && state == VGA_SWITCHEROO_OFF)
>  		return;
>  
>  	if (state == VGA_SWITCHEROO_ON) {
> @@ -96,11 +96,11 @@ nouveau_vga_init(struct nouveau_drm *drm)
>  
>  	if (nouveau_runtime_pm == 1)
>  		runtime = true;
> -	if ((nouveau_runtime_pm == -1) && (nouveau_is_optimus() ||
> nouveau_has_mux()))
> +	if ((nouveau_runtime_pm == -1) && nouveau_has_dsm())
>  		runtime = true;
>  	vga_switcheroo_register_client(dev->pdev, &nouveau_switcheroo_ops,
>  	runtime);
>  
> -	if (runtime && nouveau_has_mux() && !nouveau_is_optimus())
> +	if (runtime && (nouveau_has_mux() || nouveau_has_gmux()) &&
> !nouveau_is_optimus())
>  		vga_switcheroo_init_domain_pm_ops(drm->dev->dev,
>  		&drm->vga_pm_domain);
>  }
>  
> @@ -112,11 +112,11 @@ nouveau_vga_fini(struct nouveau_drm *drm)
>  
>  	if (nouveau_runtime_pm == 1)
>  		runtime = true;
> -	if ((nouveau_runtime_pm == -1) && (nouveau_is_optimus() ||
> nouveau_has_mux()))
> +	if ((nouveau_runtime_pm == -1) && nouveau_has_dsm())
>  		runtime = true;
>  
>  	vga_switcheroo_unregister_client(dev->pdev);
> -	if (runtime && nouveau_has_mux() && !nouveau_is_optimus())
> +	if (runtime && (nouveau_has_mux() || nouveau_has_gmux()) &&
> !nouveau_is_optimus())
>  		vga_switcheroo_fini_domain_pm_ops(drm->dev->dev);
>  	vga_client_register(dev->pdev, NULL, NULL, NULL);
>  }
> --
> 2.4.2
> 
> _______________________________________________
> Nouveau mailing list
> Nouveau@lists.freedesktop.org
> http://lists.freedesktop.org/mailman/listinfo/nouveau
>
Dave Airlie May 29, 2015, 1:20 a.m. UTC | #2
On 28 May 2015 at 18:52, Pierre Moreau <pierre.morrow@free.fr> wrote:
> Hi Dave,
>
>
> ----- Mail original -----
>> Changes since v1:
> [...]
>> diff --git a/drm/nouveau/nouveau_vga.c b/drm/nouveau/nouveau_vga.c
>> index 9a6328f..7b13804 100644
>> --- a/drm/nouveau/nouveau_vga.c
>> +++ b/drm/nouveau/nouveau_vga.c
>> @@ -36,7 +36,7 @@ nouveau_switcheroo_set_state(struct pci_dev *pdev,
>>  {
>>       struct drm_device *dev = pci_get_drvdata(pdev);
>>
>> -     if ((nouveau_is_optimus() || nouveau_has_mux()) && state ==
>> VGA_SWITCHEROO_OFF)
>
> If I understand it correctly, if the laptop is an Optimus one or has a mux, we are not "allowed" to opt-out of DynPwr/DynOff by powering down the card?
> In the same commit adding this conditional (5addcf0a5f0fadceba6bd562d0616a1c5d4c1a4d), you added the possibility to enable/disable dynpm. How is it supposed to work, by simply echo'ing ON or OFF to vga_switcheroo/switch? Then I probably forgot some stuff as it doesn't want to work on my laptop.
>

I can't remember to be honest

I think I wanted to stop the user from changing the state if it was
dynamic, now if you turn off dynpm then you should probably enable
that.

Dave.
Ben Skeggs May 29, 2015, 1:22 a.m. UTC | #3
On 28 May 2015 at 16:40, Pierre Moreau <pierre.morrow@free.fr> wrote:
> Changes since v1:
> * Factorise testing of the 3 different _DSMs presence with a single function
Not overly important, but this part could be split out into an earlier
commit, keeping this commit just for adding the gmux support?

> * Check for gmux in nouveau_switcheroo_set_state
> * Change some comments and messages that were referring explicitly to Optimus
>   whereas they were also testing the mux and gmux _DSMs
As could this part.

Thanks,
Ben.

>
> Signed-off-by: Pierre Moreau <pierre.morrow@free.fr>
> ---
>  drm/nouveau/nouveau_acpi.c | 57 ++++++++++++++++++++++++++++++++++++++++++++--
>  drm/nouveau/nouveau_acpi.h |  4 ++++
>  drm/nouveau/nouveau_drm.c  | 12 +++++-----
>  drm/nouveau/nouveau_vga.c  | 10 ++++----
>  4 files changed, 70 insertions(+), 13 deletions(-)
>
> diff --git a/drm/nouveau/nouveau_acpi.c b/drm/nouveau/nouveau_acpi.c
> index edc4b94..ee5fcfa 100644
> --- a/drm/nouveau/nouveau_acpi.c
> +++ b/drm/nouveau/nouveau_acpi.c
> @@ -29,6 +29,10 @@
>
>  #define NOUVEAU_DSM_OPTIMUS_SET_POWERDOWN (NOUVEAU_DSM_OPTIMUS_POWERDOWN_PS3 | NOUVEAU_DSM_OPTIMUS_FLAGS_CHANGED)
>
> +#define NOUVEAU_DSM_GMUX_V1_CAPS (3 << 6)
> +
> +#define NOUVEAU_DSM_GMUX_V2_CAPS (1 << 1)
> +
>  /* result of the optimus caps function */
>  #define OPTIMUS_ENABLED (1 << 0)
>  #define OPTIMUS_STATUS_MASK (3 << 3)
> @@ -45,6 +49,7 @@
>  static struct nouveau_dsm_priv {
>         bool mux_detected;
>         bool optimus_detected;
> +       bool gmux_detected;
>         acpi_handle dhandle;
>         acpi_handle rom_handle;
>  } nouveau_dsm_priv;
> @@ -57,8 +62,17 @@ bool nouveau_has_mux(void) {
>         return nouveau_dsm_priv.mux_detected;
>  }
>
> +bool nouveau_has_gmux(void) {
> +       return nouveau_dsm_priv.gmux_detected;
> +}
> +
> +bool nouveau_has_dsm(void) {
> +       return nouveau_is_optimus() || nouveau_has_mux() || nouveau_has_gmux();
> +}
> +
>  #define NOUVEAU_DSM_HAS_MUX 0x1
>  #define NOUVEAU_DSM_HAS_OPT 0x2
> +#define NOUVEAU_DSM_HAS_GMUX 0x4
>
>  #ifdef CONFIG_VGA_SWITCHEROO
>  static const uint32_t nouveau_mux_dsm_rid = 0x00000102;
> @@ -73,6 +87,18 @@ static const char nouveau_op_dsm_muid[] = {
>         0xA7, 0x2B, 0x60, 0x42, 0xA6, 0xB5, 0xBE, 0xE0,
>  };
>
> +static const uint32_t nouveau_gmux_v1_dsm_rid = 0x0000101;
> +static const char nouveau_gmux_v1_dsm_muid[] = {
> +       0xA6, 0x69, 0x86, 0x99, 0xE9, 0x8B, 0xFB, 0x49,
> +       0xBD, 0xDB, 0x51, 0xA1, 0xEF, 0xE1, 0x9C, 0x3D,
> +};
> +
> +static const uint32_t nouveau_gmux_v2_dsm_rid = 0x0000001;
> +static const char nouveau_gmux_v2_dsm_muid[] = {
> +       0xC6, 0xB7, 0xB5, 0xA0, 0x18, 0x13, 0x1C, 0x44,
> +       0xB0, 0xC9, 0xFE, 0x69, 0x5E, 0xAF, 0x94, 0x9B,
> +};
> +
>  static int nouveau_evaluate_optimus_dsm(acpi_handle handle, int func, int arg, uint32_t *result)
>  {
>         int i;
> @@ -161,6 +187,17 @@ static int nouveau_evaluate_mux_dsm(acpi_handle handle, int func, int arg)
>         return ret;
>  }
>
> +static int nouveau_check_gmux_dsm(acpi_handle handle)
> +{
> +       bool has_v1 = acpi_check_dsm(handle, nouveau_gmux_v1_dsm_muid,
> +                                    nouveau_gmux_v1_dsm_rid,
> +                                    NOUVEAU_DSM_GMUX_V1_CAPS);
> +       bool has_v2 = acpi_check_dsm(handle, nouveau_gmux_v2_dsm_muid,
> +                                    nouveau_gmux_v2_dsm_rid,
> +                                    NOUVEAU_DSM_GMUX_V2_CAPS);
> +       return has_v1 || has_v2;
> +}
> +
>  static int nouveau_dsm_switch_mux(acpi_handle handle, int mux_id)
>  {
>         mxm_wmi_call_mxmx(mux_id == NOUVEAU_DSM_MUX_LED_STAMINA ? MXM_MXDS_ADAPTER_IGD : MXM_MXDS_ADAPTER_0);
> @@ -238,6 +275,9 @@ static int nouveau_dsm_pci_probe(struct pci_dev *pdev)
>                            1 << NOUVEAU_DSM_MUX_POWER))
>                 retval |= NOUVEAU_DSM_HAS_MUX;
>
> +       if (nouveau_check_gmux_dsm(dhandle))
> +               retval |= NOUVEAU_DSM_HAS_GMUX;
> +
>         if (nouveau_check_optimus_dsm(dhandle))
>                 retval |= NOUVEAU_DSM_HAS_OPT;
>
> @@ -263,6 +303,7 @@ static bool nouveau_dsm_detect(void)
>         struct pci_dev *pdev = NULL;
>         int has_mux = 0;
>         int has_optimus = 0;
> +       bool has_gmux = false;
>         int vga_count = 0;
>         bool guid_valid;
>         int retval;
> @@ -283,6 +324,8 @@ static bool nouveau_dsm_detect(void)
>                         has_mux |= 1;
>                 if (retval & NOUVEAU_DSM_HAS_OPT)
>                         has_optimus = 1;
> +               if (retval & NOUVEAU_DSM_HAS_GMUX)
> +                       has_gmux = true;
>         }
>
>         while ((pdev = pci_get_class(PCI_CLASS_DISPLAY_3D << 8, pdev)) != NULL) {
> @@ -293,9 +336,11 @@ static bool nouveau_dsm_detect(void)
>                         has_mux |= 1;
>                 if (retval & NOUVEAU_DSM_HAS_OPT)
>                         has_optimus = 1;
> +               if (retval & NOUVEAU_DSM_HAS_GMUX)
> +                       has_gmux = true;
>         }
>
> -       /* find the optimus DSM or the mux DSM */
> +       /* find the optimus DSM, the mux DSM or the gmux DSM */
>         if (has_optimus == 1) {
>                 acpi_get_name(nouveau_dsm_priv.dhandle, ACPI_FULL_PATHNAME,
>                         &buffer);
> @@ -310,6 +355,13 @@ static bool nouveau_dsm_detect(void)
>                         acpi_method_name);
>                 nouveau_dsm_priv.mux_detected = true;
>                 ret = true;
> +       } else if (has_gmux) {
> +               acpi_get_name(nouveau_dsm_priv.dhandle, ACPI_FULL_PATHNAME,
> +                       &buffer);
> +               printk(KERN_INFO "VGA switcheroo: detected gmux DSM switching method %s handle\n",
> +                       acpi_method_name);
> +               nouveau_dsm_priv.gmux_detected = true;
> +               ret = true;
>         }
>
>
> @@ -321,7 +373,8 @@ void nouveau_register_dsm_handler(void)
>         bool r;
>
>         r = nouveau_dsm_detect();
> -       if (!r)
> +       /* Apple Gmux will register its own vga switcheroo handler */
> +       if (!r || nouveau_has_gmux())
>                 return;
>
>         vga_switcheroo_register_handler(&nouveau_dsm_handler);
> diff --git a/drm/nouveau/nouveau_acpi.h b/drm/nouveau/nouveau_acpi.h
> index 2bd88a8..e57ca3c 100644
> --- a/drm/nouveau/nouveau_acpi.h
> +++ b/drm/nouveau/nouveau_acpi.h
> @@ -6,6 +6,8 @@
>  #if defined(CONFIG_ACPI) && defined(CONFIG_X86)
>  bool nouveau_is_optimus(void);
>  bool nouveau_has_mux(void);
> +bool nouveau_has_gmux(void);
> +bool nouveau_has_dsm(void);
>  void nouveau_register_dsm_handler(void);
>  void nouveau_unregister_dsm_handler(void);
>  void nouveau_switcheroo_optimus_dsm(void);
> @@ -15,6 +17,8 @@ void *nouveau_acpi_edid(struct drm_device *, struct drm_connector *);
>  #else
>  static inline bool nouveau_is_optimus(void) { return false; };
>  static inline bool nouveau_has_mux(void) { return false; };
> +static inline bool nouveau_has_gmux(void) { return false; };
> +static inline bool nouveau_has_dsm(void) { return false; };
>  static inline void nouveau_register_dsm_handler(void) {}
>  static inline void nouveau_unregister_dsm_handler(void) {}
>  static inline void nouveau_switcheroo_optimus_dsm(void) {}
> diff --git a/drm/nouveau/nouveau_drm.c b/drm/nouveau/nouveau_drm.c
> index c06a7a1..5f85251 100644
> --- a/drm/nouveau/nouveau_drm.c
> +++ b/drm/nouveau/nouveau_drm.c
> @@ -718,9 +718,9 @@ nouveau_pmops_runtime_suspend(struct device *dev)
>                 return -EBUSY;
>         }
>
> -       /* are we optimus enabled? */
> -       if (nouveau_runtime_pm == -1 && !nouveau_is_optimus() && !nouveau_has_mux()) {
> -               DRM_DEBUG_DRIVER("failing to power off - not optimus\n");
> +       /* do we have a _DSM for switching? */
> +       if (nouveau_runtime_pm == -1 && !nouveau_has_dsm()) {
> +               DRM_DEBUG_DRIVER("failing to power off - no valid _DSM found\n");
>                 pm_runtime_forbid(dev);
>                 return -EBUSY;
>         }
> @@ -779,9 +779,9 @@ nouveau_pmops_runtime_idle(struct device *dev)
>                 return -EBUSY;
>         }
>
> -       /* are we optimus enabled? */
> -       if (nouveau_runtime_pm == -1 && !nouveau_is_optimus() && !nouveau_has_mux()) {
> -               DRM_DEBUG_DRIVER("failing to power off - not optimus\n");
> +       /* do we have a _DSM for switching? */
> +       if (nouveau_runtime_pm == -1 && !nouveau_has_dsm()) {
> +               DRM_DEBUG_DRIVER("failing to power off - no valid _DSM found\n");
>                 pm_runtime_forbid(dev);
>                 return -EBUSY;
>         }
> diff --git a/drm/nouveau/nouveau_vga.c b/drm/nouveau/nouveau_vga.c
> index 9a6328f..7b13804 100644
> --- a/drm/nouveau/nouveau_vga.c
> +++ b/drm/nouveau/nouveau_vga.c
> @@ -36,7 +36,7 @@ nouveau_switcheroo_set_state(struct pci_dev *pdev,
>  {
>         struct drm_device *dev = pci_get_drvdata(pdev);
>
> -       if ((nouveau_is_optimus() || nouveau_has_mux()) && state == VGA_SWITCHEROO_OFF)
> +       if (nouveau_has_dsm() && state == VGA_SWITCHEROO_OFF)
>                 return;
>
>         if (state == VGA_SWITCHEROO_ON) {
> @@ -96,11 +96,11 @@ nouveau_vga_init(struct nouveau_drm *drm)
>
>         if (nouveau_runtime_pm == 1)
>                 runtime = true;
> -       if ((nouveau_runtime_pm == -1) && (nouveau_is_optimus() || nouveau_has_mux()))
> +       if ((nouveau_runtime_pm == -1) && nouveau_has_dsm())
>                 runtime = true;
>         vga_switcheroo_register_client(dev->pdev, &nouveau_switcheroo_ops, runtime);
>
> -       if (runtime && nouveau_has_mux() && !nouveau_is_optimus())
> +       if (runtime && (nouveau_has_mux() || nouveau_has_gmux()) && !nouveau_is_optimus())
>                 vga_switcheroo_init_domain_pm_ops(drm->dev->dev, &drm->vga_pm_domain);
>  }
>
> @@ -112,11 +112,11 @@ nouveau_vga_fini(struct nouveau_drm *drm)
>
>         if (nouveau_runtime_pm == 1)
>                 runtime = true;
> -       if ((nouveau_runtime_pm == -1) && (nouveau_is_optimus() || nouveau_has_mux()))
> +       if ((nouveau_runtime_pm == -1) && nouveau_has_dsm())
>                 runtime = true;
>
>         vga_switcheroo_unregister_client(dev->pdev);
> -       if (runtime && nouveau_has_mux() && !nouveau_is_optimus())
> +       if (runtime && (nouveau_has_mux() || nouveau_has_gmux()) && !nouveau_is_optimus())
>                 vga_switcheroo_fini_domain_pm_ops(drm->dev->dev);
>         vga_client_register(dev->pdev, NULL, NULL, NULL);
>  }
> --
> 2.4.2
>
> _______________________________________________
> Nouveau mailing list
> Nouveau@lists.freedesktop.org
> http://lists.freedesktop.org/mailman/listinfo/nouveau
Lukas Wunner May 30, 2015, 6:23 p.m. UTC | #4
Hi Pierre,

On Thu, May 28, 2015 at 10:52:56AM +0200, Pierre Moreau wrote:
> How is it supposed to work, by simply echo'ing ON or OFF to
> vga_switcheroo/switch? Then I probably forgot some stuff as
> it doesn't want to work on my laptop.

What exactly doesn't work and which version of the MacBook Pro did
you try this on? The expected behaviour when switching GPUs on the
MBP is that the GPU that was inactive on bootup will only show a
black screen because it was unable to read the EDID (and DPCD on
retina MBPs) on bootup. In other words, it's broken.

There have been attempts to solve this by multiple folks, the last
one was submitted to dri-devel in April by yours truly:
http://lists.freedesktop.org/archives/dri-devel/2015-April/081515.html

That initial version of my patch set turned out to only work on
pre-retina MBPs, and only if the active GPU on bootup is the
discrete one. I'm currently working on a v2 to solve all that.

We need to get switching working before we can enable runtime PM on
MBPs, otherwise the user will be greeted by a black screen when the
Nvidia GPU goes to sleep and hands over to the integrated GPU.
So your patch is a bit premature I'm afraid. :-(


As to the patch itself, you're checking for existence of the gmux
but we can't make use of it unless its driver is loaded. Hence it
may be easier to simply query for the existence of the driver,
using something along the lines of find_module("apple-gmux").
I imagine this is much less code than checking for the DMI IDs.

Also, Dave Airlie and Matthew Garrett already wrote a patch set to
enable runtime PM for nouveau on MBPs which is more generic: It works
with any vga_switcheroo handler, not just gmux:

http://lists.freedesktop.org/archives/dri-devel/2014-June/060927.html
http://lists.freedesktop.org/archives/dri-devel/2014-June/060928.html
http://lists.freedesktop.org/archives/dri-devel/2014-June/060942.html
http://www.codon.org.uk/~mjg59/tmp/retina_patches/0027-nouveau-enable-runtime-pm-on-apple-gmux-machines.patch
http://www.codon.org.uk/~mjg59/tmp/retina_patches/0028-nouveau-Enable-switcheroo-dynamic-PM-at-switcheroo-e.patch

The fourth one in that list is by Dave Airlie and all the others are
by Matthew Garrett. But, as I said, we can't apply these either until
switching works. I'm working on it. ;-)


As to your questions:

> How is it supposed to work, by simply echo'ing ON or OFF to
> vga_switcheroo/switch?

Echoing ON/OFF to the "switch" file will power the inactive device
up or down. If the currently active gpu is the discrete one (which
is the default when booting a MacBook Pro), this will power up/down
the integrated gpu. You can use the tool "gpu-switch" to force the
MBP to the integrated GPU on next bootup, then you can echo ON/OFF
to change the power state of the Nvidia GPU:
https://github.com/0xbb/gpu-switch

> If I understand it correctly, if the laptop is an Optimus one or
> has a mux, we are not "allowed" to opt-out of DynPwr/DynOff by
> powering down the card?

If we power the GPU down manually by echoing OFF to the "switch"
file, the runtime PM code would probably get confused, I imagine
that's the reason why ON/OFF is ignored if client->driver_power_control
is set to true.

Speaking of which, the external ports of the MBP are soldered to
the discrete GPU and can't be switched. Only the panel can be
switched. So if there are DP or HDMI connectors with status
connected, we can't suspend. That's one of the MBP's numerous
peculiarities that I haven't verified yet they're handled properly
by the code.

Best regards,

Lukas
Pierre Moreau May 30, 2015, 9:21 p.m. UTC | #5
Hi Lukas

----- Mail original -----
> Hi Pierre,
> 
> On Thu, May 28, 2015 at 10:52:56AM +0200, Pierre Moreau wrote:
> > How is it supposed to work, by simply echo'ing ON or OFF to
> > vga_switcheroo/switch? Then I probably forgot some stuff as
> > it doesn't want to work on my laptop.
> 
> What exactly doesn't work and which version of the MacBook Pro did
> you try this on? The expected behaviour when switching GPUs on the
> MBP is that the GPU that was inactive on bootup will only show a
> black screen because it was unable to read the EDID (and DPCD on
> retina MBPs) on bootup. In other words, it's broken.

I'm trying on a mid-2009 MBP, which has a double NVidia setup with a 9400M as IGD and 9600M GT as DIS. Switching between both works (as long as do a PCI-reset of the G96, but that's another story and I should have a patch soon, hopefully).
It was also tested by another user on a retina MBP and we ran into the black screen issue. I pointed him to your patches, but I don't know yet if it helped or not.

> 
> There have been attempts to solve this by multiple folks, the last
> one was submitted to dri-devel in April by yours truly:
> http://lists.freedesktop.org/archives/dri-devel/2015-April/081515.html
> 
> That initial version of my patch set turned out to only work on
> pre-retina MBPs, and only if the active GPU on bootup is the
> discrete one. I'm currently working on a v2 to solve all that.

Cool! Looking forward to it!

> 
> We need to get switching working before we can enable runtime PM on
> MBPs, otherwise the user will be greeted by a black screen when the
> Nvidia GPU goes to sleep and hands over to the integrated GPU.
> So your patch is a bit premature I'm afraid. :-(

It's power management++: we even save energy on the screen by "powering it off" for better battery life! ;)

> 
> 
> As to the patch itself, you're checking for existence of the gmux
> but we can't make use of it unless its driver is loaded. Hence it
> may be easier to simply query for the existence of the driver,
> using something along the lines of find_module("apple-gmux").
> I imagine this is much less code than checking for the DMI IDs.

I didn't know there was such a function! Seems nice!

> 
> Also, Dave Airlie and Matthew Garrett already wrote a patch set to
> enable runtime PM for nouveau on MBPs which is more generic: It works
> with any vga_switcheroo handler, not just gmux:
> 
> http://lists.freedesktop.org/archives/dri-devel/2014-June/060927.html
> http://lists.freedesktop.org/archives/dri-devel/2014-June/060928.html
> http://lists.freedesktop.org/archives/dri-devel/2014-June/060942.html
> http://www.codon.org.uk/~mjg59/tmp/retina_patches/0027-nouveau-enable-runtime-pm-on-apple-gmux-machines.patch
> http://www.codon.org.uk/~mjg59/tmp/retina_patches/0028-nouveau-Enable-switcheroo-dynamic-PM-at-switcheroo-e.patch
> 
> The fourth one in that list is by Dave Airlie and all the others are
> by Matthew Garrett. But, as I said, we can't apply these either until
> switching works. I'm working on it. ;-)

I had no idea these patches existed: I should have looked around before.
I'll drop the patches, apart probably from the *cleaning* ones, but I'll wait for the previously mentioned ones to first land.

> 
> 
> As to your questions:
> 
> > How is it supposed to work, by simply echo'ing ON or OFF to
> > vga_switcheroo/switch?
> 
> Echoing ON/OFF to the "switch" file will power the inactive device
> up or down. If the currently active gpu is the discrete one (which
> is the default when booting a MacBook Pro)

Not on the mid-2009 MBP! ;) Which is great!

> , this will power up/down
> the integrated gpu. You can use the tool "gpu-switch" to force the
> MBP to the integrated GPU on next bootup, then you can echo ON/OFFI'ddd
> to change the power state of the Nvidia GPU:
> https://github.com/0xbb/gpu-switch
> 
> > If I understand it correctly, if the laptop is an Optimus one or
> > has a mux, we are not "allowed" to opt-out of DynPwr/DynOff by
> > powering down the card?
> 
> If we power the GPU down manually by echoing OFF to the "switch"
> file, the runtime PM code would probably get confused, I imagine
> that's the reason why ON/OFF is ignored if
> client->driver_power_control
> is set to true.

I would have guessed that runtime pm would then get disabled.

> 
> Speaking of which, the external ports of the MBP are soldered to
> the discrete GPU and can't be switched. Only the panel can be
> switched. So if there are DP or HDMI connectors with status
> connected, we can't suspend. That's one of the MBP's numerous
> peculiarities that I haven't verified yet they're handled properly
> by the code.

I could check that on my MBP if you want.



Best regards,

Pierre


> 
> Best regards,
> 
> Lukas
>
diff mbox

Patch

diff --git a/drm/nouveau/nouveau_acpi.c b/drm/nouveau/nouveau_acpi.c
index edc4b94..ee5fcfa 100644
--- a/drm/nouveau/nouveau_acpi.c
+++ b/drm/nouveau/nouveau_acpi.c
@@ -29,6 +29,10 @@ 
 
 #define NOUVEAU_DSM_OPTIMUS_SET_POWERDOWN (NOUVEAU_DSM_OPTIMUS_POWERDOWN_PS3 | NOUVEAU_DSM_OPTIMUS_FLAGS_CHANGED)
 
+#define NOUVEAU_DSM_GMUX_V1_CAPS (3 << 6)
+
+#define NOUVEAU_DSM_GMUX_V2_CAPS (1 << 1)
+
 /* result of the optimus caps function */
 #define OPTIMUS_ENABLED (1 << 0)
 #define OPTIMUS_STATUS_MASK (3 << 3)
@@ -45,6 +49,7 @@ 
 static struct nouveau_dsm_priv {
 	bool mux_detected;
 	bool optimus_detected;
+	bool gmux_detected;
 	acpi_handle dhandle;
 	acpi_handle rom_handle;
 } nouveau_dsm_priv;
@@ -57,8 +62,17 @@  bool nouveau_has_mux(void) {
 	return nouveau_dsm_priv.mux_detected;
 }
 
+bool nouveau_has_gmux(void) {
+	return nouveau_dsm_priv.gmux_detected;
+}
+
+bool nouveau_has_dsm(void) {
+	return nouveau_is_optimus() || nouveau_has_mux() || nouveau_has_gmux();
+}
+
 #define NOUVEAU_DSM_HAS_MUX 0x1
 #define NOUVEAU_DSM_HAS_OPT 0x2
+#define NOUVEAU_DSM_HAS_GMUX 0x4
 
 #ifdef CONFIG_VGA_SWITCHEROO
 static const uint32_t nouveau_mux_dsm_rid = 0x00000102;
@@ -73,6 +87,18 @@  static const char nouveau_op_dsm_muid[] = {
 	0xA7, 0x2B, 0x60, 0x42, 0xA6, 0xB5, 0xBE, 0xE0,
 };
 
+static const uint32_t nouveau_gmux_v1_dsm_rid = 0x0000101;
+static const char nouveau_gmux_v1_dsm_muid[] = {
+	0xA6, 0x69, 0x86, 0x99, 0xE9, 0x8B, 0xFB, 0x49,
+	0xBD, 0xDB, 0x51, 0xA1, 0xEF, 0xE1, 0x9C, 0x3D,
+};
+
+static const uint32_t nouveau_gmux_v2_dsm_rid = 0x0000001;
+static const char nouveau_gmux_v2_dsm_muid[] = {
+	0xC6, 0xB7, 0xB5, 0xA0, 0x18, 0x13, 0x1C, 0x44,
+	0xB0, 0xC9, 0xFE, 0x69, 0x5E, 0xAF, 0x94, 0x9B,
+};
+
 static int nouveau_evaluate_optimus_dsm(acpi_handle handle, int func, int arg, uint32_t *result)
 {
 	int i;
@@ -161,6 +187,17 @@  static int nouveau_evaluate_mux_dsm(acpi_handle handle, int func, int arg)
 	return ret;
 }
 
+static int nouveau_check_gmux_dsm(acpi_handle handle)
+{
+	bool has_v1 = acpi_check_dsm(handle, nouveau_gmux_v1_dsm_muid,
+			             nouveau_gmux_v1_dsm_rid,
+			             NOUVEAU_DSM_GMUX_V1_CAPS);
+	bool has_v2 = acpi_check_dsm(handle, nouveau_gmux_v2_dsm_muid,
+			             nouveau_gmux_v2_dsm_rid,
+			             NOUVEAU_DSM_GMUX_V2_CAPS);
+	return has_v1 || has_v2;
+}
+
 static int nouveau_dsm_switch_mux(acpi_handle handle, int mux_id)
 {
 	mxm_wmi_call_mxmx(mux_id == NOUVEAU_DSM_MUX_LED_STAMINA ? MXM_MXDS_ADAPTER_IGD : MXM_MXDS_ADAPTER_0);
@@ -238,6 +275,9 @@  static int nouveau_dsm_pci_probe(struct pci_dev *pdev)
 			   1 << NOUVEAU_DSM_MUX_POWER))
 		retval |= NOUVEAU_DSM_HAS_MUX;
 
+	if (nouveau_check_gmux_dsm(dhandle))
+		retval |= NOUVEAU_DSM_HAS_GMUX;
+
 	if (nouveau_check_optimus_dsm(dhandle))
 		retval |= NOUVEAU_DSM_HAS_OPT;
 
@@ -263,6 +303,7 @@  static bool nouveau_dsm_detect(void)
 	struct pci_dev *pdev = NULL;
 	int has_mux = 0;
 	int has_optimus = 0;
+	bool has_gmux = false;
 	int vga_count = 0;
 	bool guid_valid;
 	int retval;
@@ -283,6 +324,8 @@  static bool nouveau_dsm_detect(void)
 			has_mux |= 1;
 		if (retval & NOUVEAU_DSM_HAS_OPT)
 			has_optimus = 1;
+		if (retval & NOUVEAU_DSM_HAS_GMUX)
+			has_gmux = true;
 	}
 
 	while ((pdev = pci_get_class(PCI_CLASS_DISPLAY_3D << 8, pdev)) != NULL) {
@@ -293,9 +336,11 @@  static bool nouveau_dsm_detect(void)
 			has_mux |= 1;
 		if (retval & NOUVEAU_DSM_HAS_OPT)
 			has_optimus = 1;
+		if (retval & NOUVEAU_DSM_HAS_GMUX)
+			has_gmux = true;
 	}
 
-	/* find the optimus DSM or the mux DSM */
+	/* find the optimus DSM, the mux DSM or the gmux DSM */
 	if (has_optimus == 1) {
 		acpi_get_name(nouveau_dsm_priv.dhandle, ACPI_FULL_PATHNAME,
 			&buffer);
@@ -310,6 +355,13 @@  static bool nouveau_dsm_detect(void)
 			acpi_method_name);
 		nouveau_dsm_priv.mux_detected = true;
 		ret = true;
+	} else if (has_gmux) {
+		acpi_get_name(nouveau_dsm_priv.dhandle, ACPI_FULL_PATHNAME,
+			&buffer);
+		printk(KERN_INFO "VGA switcheroo: detected gmux DSM switching method %s handle\n",
+			acpi_method_name);
+		nouveau_dsm_priv.gmux_detected = true;
+		ret = true;
 	}
 
 
@@ -321,7 +373,8 @@  void nouveau_register_dsm_handler(void)
 	bool r;
 
 	r = nouveau_dsm_detect();
-	if (!r)
+	/* Apple Gmux will register its own vga switcheroo handler */
+	if (!r || nouveau_has_gmux())
 		return;
 
 	vga_switcheroo_register_handler(&nouveau_dsm_handler);
diff --git a/drm/nouveau/nouveau_acpi.h b/drm/nouveau/nouveau_acpi.h
index 2bd88a8..e57ca3c 100644
--- a/drm/nouveau/nouveau_acpi.h
+++ b/drm/nouveau/nouveau_acpi.h
@@ -6,6 +6,8 @@ 
 #if defined(CONFIG_ACPI) && defined(CONFIG_X86)
 bool nouveau_is_optimus(void);
 bool nouveau_has_mux(void);
+bool nouveau_has_gmux(void);
+bool nouveau_has_dsm(void);
 void nouveau_register_dsm_handler(void);
 void nouveau_unregister_dsm_handler(void);
 void nouveau_switcheroo_optimus_dsm(void);
@@ -15,6 +17,8 @@  void *nouveau_acpi_edid(struct drm_device *, struct drm_connector *);
 #else
 static inline bool nouveau_is_optimus(void) { return false; };
 static inline bool nouveau_has_mux(void) { return false; };
+static inline bool nouveau_has_gmux(void) { return false; };
+static inline bool nouveau_has_dsm(void) { return false; };
 static inline void nouveau_register_dsm_handler(void) {}
 static inline void nouveau_unregister_dsm_handler(void) {}
 static inline void nouveau_switcheroo_optimus_dsm(void) {}
diff --git a/drm/nouveau/nouveau_drm.c b/drm/nouveau/nouveau_drm.c
index c06a7a1..5f85251 100644
--- a/drm/nouveau/nouveau_drm.c
+++ b/drm/nouveau/nouveau_drm.c
@@ -718,9 +718,9 @@  nouveau_pmops_runtime_suspend(struct device *dev)
 		return -EBUSY;
 	}
 
-	/* are we optimus enabled? */
-	if (nouveau_runtime_pm == -1 && !nouveau_is_optimus() && !nouveau_has_mux()) {
-		DRM_DEBUG_DRIVER("failing to power off - not optimus\n");
+	/* do we have a _DSM for switching? */
+	if (nouveau_runtime_pm == -1 && !nouveau_has_dsm()) {
+		DRM_DEBUG_DRIVER("failing to power off - no valid _DSM found\n");
 		pm_runtime_forbid(dev);
 		return -EBUSY;
 	}
@@ -779,9 +779,9 @@  nouveau_pmops_runtime_idle(struct device *dev)
 		return -EBUSY;
 	}
 
-	/* are we optimus enabled? */
-	if (nouveau_runtime_pm == -1 && !nouveau_is_optimus() && !nouveau_has_mux()) {
-		DRM_DEBUG_DRIVER("failing to power off - not optimus\n");
+	/* do we have a _DSM for switching? */
+	if (nouveau_runtime_pm == -1 && !nouveau_has_dsm()) {
+		DRM_DEBUG_DRIVER("failing to power off - no valid _DSM found\n");
 		pm_runtime_forbid(dev);
 		return -EBUSY;
 	}
diff --git a/drm/nouveau/nouveau_vga.c b/drm/nouveau/nouveau_vga.c
index 9a6328f..7b13804 100644
--- a/drm/nouveau/nouveau_vga.c
+++ b/drm/nouveau/nouveau_vga.c
@@ -36,7 +36,7 @@  nouveau_switcheroo_set_state(struct pci_dev *pdev,
 {
 	struct drm_device *dev = pci_get_drvdata(pdev);
 
-	if ((nouveau_is_optimus() || nouveau_has_mux()) && state == VGA_SWITCHEROO_OFF)
+	if (nouveau_has_dsm() && state == VGA_SWITCHEROO_OFF)
 		return;
 
 	if (state == VGA_SWITCHEROO_ON) {
@@ -96,11 +96,11 @@  nouveau_vga_init(struct nouveau_drm *drm)
 
 	if (nouveau_runtime_pm == 1)
 		runtime = true;
-	if ((nouveau_runtime_pm == -1) && (nouveau_is_optimus() || nouveau_has_mux()))
+	if ((nouveau_runtime_pm == -1) && nouveau_has_dsm())
 		runtime = true;
 	vga_switcheroo_register_client(dev->pdev, &nouveau_switcheroo_ops, runtime);
 
-	if (runtime && nouveau_has_mux() && !nouveau_is_optimus())
+	if (runtime && (nouveau_has_mux() || nouveau_has_gmux()) && !nouveau_is_optimus())
 		vga_switcheroo_init_domain_pm_ops(drm->dev->dev, &drm->vga_pm_domain);
 }
 
@@ -112,11 +112,11 @@  nouveau_vga_fini(struct nouveau_drm *drm)
 
 	if (nouveau_runtime_pm == 1)
 		runtime = true;
-	if ((nouveau_runtime_pm == -1) && (nouveau_is_optimus() || nouveau_has_mux()))
+	if ((nouveau_runtime_pm == -1) && nouveau_has_dsm())
 		runtime = true;
 
 	vga_switcheroo_unregister_client(dev->pdev);
-	if (runtime && nouveau_has_mux() && !nouveau_is_optimus())
+	if (runtime && (nouveau_has_mux() || nouveau_has_gmux()) && !nouveau_is_optimus())
 		vga_switcheroo_fini_domain_pm_ops(drm->dev->dev);
 	vga_client_register(dev->pdev, NULL, NULL, NULL);
 }