- Update to 3.3-rc2.
[linux-flexiantxendom0-3.2.10.git] / drivers / gpu / drm / nouveau / nouveau_acpi.c
index b600a21..bc8a2d1 100644 (file)
 
 #include <linux/vga_switcheroo.h>
 
-#define NOUVEAU_DSM_SUPPORTED 0x00
-#define NOUVEAU_DSM_SUPPORTED_FUNCTIONS 0x00
-
-#define NOUVEAU_DSM_ACTIVE 0x01
-#define NOUVEAU_DSM_ACTIVE_QUERY 0x00
-
 #define NOUVEAU_DSM_LED 0x02
 #define NOUVEAU_DSM_LED_STATE 0x00
 #define NOUVEAU_DSM_LED_OFF 0x10
@@ -35,6 +29,9 @@
 #define NOUVEAU_DSM_POWER_SPEED 0x01
 #define NOUVEAU_DSM_POWER_STAMINA 0x02
 
+#define NOUVEAU_DSM_OPTIMUS_FN 0x1A
+#define NOUVEAU_DSM_OPTIMUS_ARGS 0x03000001
+
 static struct nouveau_dsm_priv {
        bool dsm_detected;
        bool optimus_detected;
@@ -62,7 +59,8 @@ static int nouveau_optimus_dsm(acpi_handle handle, int func, int arg, uint32_t *
        struct acpi_object_list input;
        union acpi_object params[4];
        union acpi_object *obj;
-       int err;
+       int i, err;
+       char args_buff[4];
 
        input.count = 4;
        input.pointer = params;
@@ -74,7 +72,11 @@ static int nouveau_optimus_dsm(acpi_handle handle, int func, int arg, uint32_t *
        params[2].type = ACPI_TYPE_INTEGER;
        params[2].integer.value = func;
        params[3].type = ACPI_TYPE_BUFFER;
-       params[3].buffer.length = 0;
+       params[3].buffer.length = 4;
+       /* ACPI is little endian, AABBCCDD becomes {DD,CC,BB,AA} */
+       for (i = 0; i < 4; i++)
+               args_buff[i] = (arg >> i * 8) & 0xFF;
+       params[3].buffer.pointer = args_buff;
 
        err = acpi_evaluate_object(handle, "_DSM", &input, &output);
        if (err) {
@@ -149,6 +151,23 @@ static int nouveau_dsm(acpi_handle handle, int func, int arg, uint32_t *result)
        return 0;
 }
 
+/* Returns 1 if a DSM function is usable and 0 otherwise */
+static int nouveau_test_dsm(acpi_handle test_handle,
+       int (*dsm_func)(acpi_handle, int, int, uint32_t *),
+       int sfnc)
+{
+       u32 result = 0;
+
+       /* Function 0 returns a Buffer containing available functions. The args
+        * parameter is ignored for function 0, so just put 0 in it */
+       if (dsm_func(test_handle, 0, 0, &result))
+               return 0;
+
+       /* ACPI Spec v4 9.14.1: if bit 0 is zero, no function is supported. If
+        * the n-th bit is enabled, function n is supported */
+       return result & 1 && result & (1 << sfnc);
+}
+
 static int nouveau_dsm_switch_mux(acpi_handle handle, int mux_id)
 {
        mxm_wmi_call_mxmx(mux_id == NOUVEAU_DSM_LED_STAMINA ? MXM_MXDS_ADAPTER_IGD : MXM_MXDS_ADAPTER_0);
@@ -169,6 +188,10 @@ static int nouveau_dsm_set_discrete_state(acpi_handle handle, enum vga_switchero
 
 static int nouveau_dsm_switchto(enum vga_switcheroo_client_id id)
 {
+       /* perhaps the _DSM functions are mutually exclusive, but prepare for
+        * the future */
+       if (!nouveau_dsm_priv.dsm_detected && nouveau_dsm_priv.optimus_detected)
+               return 0;
        if (id == VGA_SWITCHEROO_IGD)
                return nouveau_dsm_switch_mux(nouveau_dsm_priv.dhandle, NOUVEAU_DSM_LED_STAMINA);
        else
@@ -181,6 +204,11 @@ static int nouveau_dsm_power_state(enum vga_switcheroo_client_id id,
        if (id == VGA_SWITCHEROO_IGD)
                return 0;
 
+       /* Optimus laptops have the card already disabled in
+        * nouveau_switcheroo_set_state */
+       if (!nouveau_dsm_priv.dsm_detected && nouveau_dsm_priv.optimus_detected)
+               return 0;
+
        return nouveau_dsm_set_discrete_state(nouveau_dsm_priv.dhandle, state);
 }
 
@@ -213,8 +241,7 @@ static int nouveau_dsm_pci_probe(struct pci_dev *pdev)
 {
        acpi_handle dhandle, nvidia_handle;
        acpi_status status;
-       int ret, retval = 0;
-       uint32_t result;
+       int retval = 0;
 
        dhandle = DEVICE_ACPI_HANDLE(&pdev->dev);
        if (!dhandle)
@@ -225,13 +252,11 @@ static int nouveau_dsm_pci_probe(struct pci_dev *pdev)
                return false;
        }
 
-       ret = nouveau_dsm(dhandle, NOUVEAU_DSM_SUPPORTED,
-                         NOUVEAU_DSM_SUPPORTED_FUNCTIONS, &result);
-       if (ret == 0)
+       if (nouveau_test_dsm(dhandle, nouveau_dsm, NOUVEAU_DSM_POWER))
                retval |= NOUVEAU_DSM_HAS_MUX;
 
-       ret = nouveau_optimus_dsm(dhandle, 0, 0, &result);
-       if (ret == 0)
+       if (nouveau_test_dsm(dhandle, nouveau_optimus_dsm,
+               NOUVEAU_DSM_OPTIMUS_FN))
                retval |= NOUVEAU_DSM_HAS_OPT;
 
        if (retval)
@@ -270,15 +295,22 @@ static bool nouveau_dsm_detect(void)
        }
 
        if (vga_count == 2 && has_dsm && guid_valid) {
-               acpi_get_name(nouveau_dsm_priv.dhandle, ACPI_FULL_PATHNAME, &buffer);
+               acpi_get_name(nouveau_dsm_priv.dhandle, ACPI_FULL_PATHNAME,
+                       &buffer);
                printk(KERN_INFO "VGA switcheroo: detected DSM switching method %s handle\n",
-                      acpi_method_name);
+                       acpi_method_name);
                nouveau_dsm_priv.dsm_detected = true;
                ret = true;
        }
 
-       if (has_optimus == 1)
+       if (has_optimus == 1) {
+               acpi_get_name(nouveau_dsm_priv.dhandle, ACPI_FULL_PATHNAME,
+                       &buffer);
+               printk(KERN_INFO "VGA switcheroo: detected Optimus DSM method %s handle\n",
+                       acpi_method_name);
                nouveau_dsm_priv.optimus_detected = true;
+               ret = true;
+       }
 
        return ret;
 }
@@ -294,6 +326,17 @@ void nouveau_register_dsm_handler(void)
        vga_switcheroo_register_handler(&nouveau_dsm_handler);
 }
 
+/* Must be called for Optimus models before the card can be turned off */
+void nouveau_switcheroo_optimus_dsm(void)
+{
+       u32 result = 0;
+       if (!nouveau_dsm_priv.optimus_detected)
+               return;
+
+       nouveau_optimus_dsm(nouveau_dsm_priv.dhandle, NOUVEAU_DSM_OPTIMUS_FN,
+               NOUVEAU_DSM_OPTIMUS_ARGS, &result);
+}
+
 void nouveau_unregister_dsm_handler(void)
 {
        vga_switcheroo_unregister_handler();