blob: 0e0730a531371e9e59ec01d1db10b72143c0c67b [file] [log] [blame]
Ben Skeggs6ee73862009-12-11 19:24:15 +10001#include <linux/pci.h>
2#include <linux/acpi.h>
3#include <acpi/acpi_drivers.h>
4#include <acpi/acpi_bus.h>
5
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"
13
Dave Airlie6a9ee8a2010-02-01 15:38:10 +100014#include <linux/vga_switcheroo.h>
15
Ben Skeggs6ee73862009-12-11 19:24:15 +100016#define NOUVEAU_DSM_SUPPORTED 0x00
17#define NOUVEAU_DSM_SUPPORTED_FUNCTIONS 0x00
18
19#define NOUVEAU_DSM_ACTIVE 0x01
20#define NOUVEAU_DSM_ACTIVE_QUERY 0x00
21
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
27
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
32
Dave Airlie6a9ee8a2010-02-01 15:38:10 +100033static struct nouveau_dsm_priv {
34 bool dsm_detected;
35 acpi_handle dhandle;
36 acpi_handle dsm_handle;
37} nouveau_dsm_priv;
Ben Skeggs6ee73862009-12-11 19:24:15 +100038
Dave Airlie6a9ee8a2010-02-01 15:38:10 +100039static const char nouveau_dsm_muid[] = {
40 0xA0, 0xA0, 0x95, 0x9D, 0x60, 0x00, 0x48, 0x4D,
41 0xB3, 0x4D, 0x7E, 0x5F, 0xEA, 0x12, 0x9F, 0xD4,
42};
43
44static int nouveau_dsm(acpi_handle handle, int func, int arg, int *result)
45{
Ben Skeggs6ee73862009-12-11 19:24:15 +100046 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;
51
Ben Skeggs6ee73862009-12-11 19:24:15 +100052 input.count = 4;
53 input.pointer = params;
54 params[0].type = ACPI_TYPE_BUFFER;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +100055 params[0].buffer.length = sizeof(nouveau_dsm_muid);
56 params[0].buffer.pointer = (char *)nouveau_dsm_muid;
Ben Skeggs6ee73862009-12-11 19:24:15 +100057 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;
63
64 err = acpi_evaluate_object(handle, "_DSM", &input, &output);
65 if (err) {
Dave Airlie6a9ee8a2010-02-01 15:38:10 +100066 printk(KERN_INFO "failed to evaluate _DSM: %d\n", err);
Ben Skeggs6ee73862009-12-11 19:24:15 +100067 return err;
68 }
69
70 obj = (union acpi_object *)output.pointer;
71
72 if (obj->type == ACPI_TYPE_INTEGER)
73 if (obj->integer.value == 0x80000002)
74 return -ENODEV;
75
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);
83 }
84 }
85
86 kfree(output.pointer);
87 return 0;
88}
89
Dave Airlie6a9ee8a2010-02-01 15:38:10 +100090static int nouveau_dsm_switch_mux(acpi_handle handle, int mux_id)
Ben Skeggs6ee73862009-12-11 19:24:15 +100091{
Dave Airlie6a9ee8a2010-02-01 15:38:10 +100092 return nouveau_dsm(handle, NOUVEAU_DSM_LED, mux_id, NULL);
93}
Ben Skeggs6ee73862009-12-11 19:24:15 +100094
Dave Airlie6a9ee8a2010-02-01 15:38:10 +100095static int nouveau_dsm_set_discrete_state(acpi_handle handle, enum vga_switcheroo_state state)
96{
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);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000103 return 0;
104}
105
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000106static int nouveau_dsm_switchto(enum vga_switcheroo_client_id id)
Ben Skeggs6ee73862009-12-11 19:24:15 +1000107{
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000108 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);
112}
Ben Skeggs6ee73862009-12-11 19:24:15 +1000113
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000114static int nouveau_dsm_power_state(enum vga_switcheroo_client_id id,
115 enum vga_switcheroo_state state)
116{
117 if (id == VGA_SWITCHEROO_IGD)
118 return 0;
119
120 return nouveau_dsm_set_discrete_state(nouveau_dsm_priv.dsm_handle, state);
121}
122
123static int nouveau_dsm_init(void)
124{
125 return 0;
126}
127
128static int nouveau_dsm_get_client_id(struct pci_dev *pdev)
129{
130 if (nouveau_dsm_priv.dhandle == DEVICE_ACPI_HANDLE(&pdev->dev))
131 return VGA_SWITCHEROO_IGD;
132 else
133 return VGA_SWITCHEROO_DIS;
134}
135
136static 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,
141};
142
143static bool nouveau_dsm_pci_probe(struct pci_dev *pdev)
144{
145 acpi_handle dhandle, nvidia_handle;
146 acpi_status status;
147 int ret;
148 uint32_t result;
149
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;
156 }
157
158 ret= nouveau_dsm(nvidia_handle, NOUVEAU_DSM_SUPPORTED,
159 NOUVEAU_DSM_SUPPORTED_FUNCTIONS, &result);
160 if (ret < 0)
Ben Skeggs6ee73862009-12-11 19:24:15 +1000161 return false;
162
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000163 nouveau_dsm_priv.dhandle = dhandle;
164 nouveau_dsm_priv.dsm_handle = nvidia_handle;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000165 return true;
166}
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000167
168static bool nouveau_dsm_detect(void)
169{
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++;
177
178 has_dsm |= (nouveau_dsm_pci_probe(pdev) == true);
179 }
180
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;
187 }
188 return false;
189}
190
191void nouveau_register_dsm_handler(void)
192{
193 bool r;
194
195 r = nouveau_dsm_detect();
196 if (!r)
197 return;
198
199 vga_switcheroo_register_handler(&nouveau_dsm_handler);
200}
201
202void nouveau_unregister_dsm_handler(void)
203{
204 vga_switcheroo_unregister_handler();
205}