vga_switcheroo: initial implementation (v15)
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / gpu / drm / nouveau / nouveau_acpi.c
blob0e0730a531371e9e59ec01d1db10b72143c0c67b
1 #include <linux/pci.h>
2 #include <linux/acpi.h>
3 #include <acpi/acpi_drivers.h>
4 #include <acpi/acpi_bus.h>
6 #include "drmP.h"
7 #include "drm.h"
8 #include "drm_sarea.h"
9 #include "drm_crtc_helper.h"
10 #include "nouveau_drv.h"
11 #include "nouveau_drm.h"
12 #include "nv50_display.h"
14 #include <linux/vga_switcheroo.h>
16 #define NOUVEAU_DSM_SUPPORTED 0x00
17 #define NOUVEAU_DSM_SUPPORTED_FUNCTIONS 0x00
19 #define NOUVEAU_DSM_ACTIVE 0x01
20 #define NOUVEAU_DSM_ACTIVE_QUERY 0x00
22 #define NOUVEAU_DSM_LED 0x02
23 #define NOUVEAU_DSM_LED_STATE 0x00
24 #define NOUVEAU_DSM_LED_OFF 0x10
25 #define NOUVEAU_DSM_LED_STAMINA 0x11
26 #define NOUVEAU_DSM_LED_SPEED 0x12
28 #define NOUVEAU_DSM_POWER 0x03
29 #define NOUVEAU_DSM_POWER_STATE 0x00
30 #define NOUVEAU_DSM_POWER_SPEED 0x01
31 #define NOUVEAU_DSM_POWER_STAMINA 0x02
33 static struct nouveau_dsm_priv {
34 bool dsm_detected;
35 acpi_handle dhandle;
36 acpi_handle dsm_handle;
37 } nouveau_dsm_priv;
39 static const char nouveau_dsm_muid[] = {
40 0xA0, 0xA0, 0x95, 0x9D, 0x60, 0x00, 0x48, 0x4D,
41 0xB3, 0x4D, 0x7E, 0x5F, 0xEA, 0x12, 0x9F, 0xD4,
44 static int nouveau_dsm(acpi_handle handle, int func, int arg, int *result)
46 struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL };
47 struct acpi_object_list input;
48 union acpi_object params[4];
49 union acpi_object *obj;
50 int err;
52 input.count = 4;
53 input.pointer = params;
54 params[0].type = ACPI_TYPE_BUFFER;
55 params[0].buffer.length = sizeof(nouveau_dsm_muid);
56 params[0].buffer.pointer = (char *)nouveau_dsm_muid;
57 params[1].type = ACPI_TYPE_INTEGER;
58 params[1].integer.value = 0x00000102;
59 params[2].type = ACPI_TYPE_INTEGER;
60 params[2].integer.value = func;
61 params[3].type = ACPI_TYPE_INTEGER;
62 params[3].integer.value = arg;
64 err = acpi_evaluate_object(handle, "_DSM", &input, &output);
65 if (err) {
66 printk(KERN_INFO "failed to evaluate _DSM: %d\n", err);
67 return err;
70 obj = (union acpi_object *)output.pointer;
72 if (obj->type == ACPI_TYPE_INTEGER)
73 if (obj->integer.value == 0x80000002)
74 return -ENODEV;
76 if (obj->type == ACPI_TYPE_BUFFER) {
77 if (obj->buffer.length == 4 && result) {
78 *result = 0;
79 *result |= obj->buffer.pointer[0];
80 *result |= (obj->buffer.pointer[1] << 8);
81 *result |= (obj->buffer.pointer[2] << 16);
82 *result |= (obj->buffer.pointer[3] << 24);
86 kfree(output.pointer);
87 return 0;
90 static int nouveau_dsm_switch_mux(acpi_handle handle, int mux_id)
92 return nouveau_dsm(handle, NOUVEAU_DSM_LED, mux_id, NULL);
95 static int nouveau_dsm_set_discrete_state(acpi_handle handle, enum vga_switcheroo_state state)
97 int arg;
98 if (state == VGA_SWITCHEROO_ON)
99 arg = NOUVEAU_DSM_POWER_SPEED;
100 else
101 arg = NOUVEAU_DSM_POWER_STAMINA;
102 nouveau_dsm(handle, NOUVEAU_DSM_POWER, arg, NULL);
103 return 0;
106 static int nouveau_dsm_switchto(enum vga_switcheroo_client_id id)
108 if (id == VGA_SWITCHEROO_IGD)
109 return nouveau_dsm_switch_mux(nouveau_dsm_priv.dsm_handle, NOUVEAU_DSM_LED_STAMINA);
110 else
111 return nouveau_dsm_switch_mux(nouveau_dsm_priv.dsm_handle, NOUVEAU_DSM_LED_SPEED);
114 static int nouveau_dsm_power_state(enum vga_switcheroo_client_id id,
115 enum vga_switcheroo_state state)
117 if (id == VGA_SWITCHEROO_IGD)
118 return 0;
120 return nouveau_dsm_set_discrete_state(nouveau_dsm_priv.dsm_handle, state);
123 static int nouveau_dsm_init(void)
125 return 0;
128 static int nouveau_dsm_get_client_id(struct pci_dev *pdev)
130 if (nouveau_dsm_priv.dhandle == DEVICE_ACPI_HANDLE(&pdev->dev))
131 return VGA_SWITCHEROO_IGD;
132 else
133 return VGA_SWITCHEROO_DIS;
136 static struct vga_switcheroo_handler nouveau_dsm_handler = {
137 .switchto = nouveau_dsm_switchto,
138 .power_state = nouveau_dsm_power_state,
139 .init = nouveau_dsm_init,
140 .get_client_id = nouveau_dsm_get_client_id,
143 static bool nouveau_dsm_pci_probe(struct pci_dev *pdev)
145 acpi_handle dhandle, nvidia_handle;
146 acpi_status status;
147 int ret;
148 uint32_t result;
150 dhandle = DEVICE_ACPI_HANDLE(&pdev->dev);
151 if (!dhandle)
152 return false;
153 status = acpi_get_handle(dhandle, "_DSM", &nvidia_handle);
154 if (ACPI_FAILURE(status)) {
155 return false;
158 ret= nouveau_dsm(nvidia_handle, NOUVEAU_DSM_SUPPORTED,
159 NOUVEAU_DSM_SUPPORTED_FUNCTIONS, &result);
160 if (ret < 0)
161 return false;
163 nouveau_dsm_priv.dhandle = dhandle;
164 nouveau_dsm_priv.dsm_handle = nvidia_handle;
165 return true;
168 static bool nouveau_dsm_detect(void)
170 char acpi_method_name[255] = { 0 };
171 struct acpi_buffer buffer = {sizeof(acpi_method_name), acpi_method_name};
172 struct pci_dev *pdev = NULL;
173 int has_dsm = 0;
174 int vga_count = 0;
175 while ((pdev = pci_get_class(PCI_CLASS_DISPLAY_VGA << 8, pdev)) != NULL) {
176 vga_count++;
178 has_dsm |= (nouveau_dsm_pci_probe(pdev) == true);
181 if (vga_count == 2 && has_dsm) {
182 acpi_get_name(nouveau_dsm_priv.dsm_handle, ACPI_FULL_PATHNAME, &buffer);
183 printk(KERN_INFO "VGA switcheroo: detected DSM switching method %s handle\n",
184 acpi_method_name);
185 nouveau_dsm_priv.dsm_detected = true;
186 return true;
188 return false;
191 void nouveau_register_dsm_handler(void)
193 bool r;
195 r = nouveau_dsm_detect();
196 if (!r)
197 return;
199 vga_switcheroo_register_handler(&nouveau_dsm_handler);
202 void nouveau_unregister_dsm_handler(void)
204 vga_switcheroo_unregister_handler();