2 #include <linux/acpi.h>
3 #include <linux/slab.h>
4 #include <acpi/acpi_drivers.h>
5 #include <acpi/acpi_bus.h>
6 #include <acpi/video.h>
8 #include <linux/mxm-wmi.h>
10 #include <linux/vga_switcheroo.h>
12 #include <drm/drm_edid.h>
14 #include "nouveau_drm.h"
15 #include "nouveau_acpi.h"
17 #define NOUVEAU_DSM_LED 0x02
18 #define NOUVEAU_DSM_LED_STATE 0x00
19 #define NOUVEAU_DSM_LED_OFF 0x10
20 #define NOUVEAU_DSM_LED_STAMINA 0x11
21 #define NOUVEAU_DSM_LED_SPEED 0x12
23 #define NOUVEAU_DSM_POWER 0x03
24 #define NOUVEAU_DSM_POWER_STATE 0x00
25 #define NOUVEAU_DSM_POWER_SPEED 0x01
26 #define NOUVEAU_DSM_POWER_STAMINA 0x02
28 #define NOUVEAU_DSM_OPTIMUS_FN 0x1A
29 #define NOUVEAU_DSM_OPTIMUS_ARGS 0x03000001
31 static struct nouveau_dsm_priv
{
33 bool optimus_detected
;
35 acpi_handle rom_handle
;
38 bool nouveau_is_optimus(void) {
39 return nouveau_dsm_priv
.optimus_detected
;
42 bool nouveau_is_v1_dsm(void) {
43 return nouveau_dsm_priv
.dsm_detected
;
46 #define NOUVEAU_DSM_HAS_MUX 0x1
47 #define NOUVEAU_DSM_HAS_OPT 0x2
49 static const char nouveau_dsm_muid
[] = {
50 0xA0, 0xA0, 0x95, 0x9D, 0x60, 0x00, 0x48, 0x4D,
51 0xB3, 0x4D, 0x7E, 0x5F, 0xEA, 0x12, 0x9F, 0xD4,
54 static const char nouveau_op_dsm_muid
[] = {
55 0xF8, 0xD8, 0x86, 0xA4, 0xDA, 0x0B, 0x1B, 0x47,
56 0xA7, 0x2B, 0x60, 0x42, 0xA6, 0xB5, 0xBE, 0xE0,
59 static int nouveau_optimus_dsm(acpi_handle handle
, int func
, int arg
, uint32_t *result
)
61 struct acpi_buffer output
= { ACPI_ALLOCATE_BUFFER
, NULL
};
62 struct acpi_object_list input
;
63 union acpi_object params
[4];
64 union acpi_object
*obj
;
69 input
.pointer
= params
;
70 params
[0].type
= ACPI_TYPE_BUFFER
;
71 params
[0].buffer
.length
= sizeof(nouveau_op_dsm_muid
);
72 params
[0].buffer
.pointer
= (char *)nouveau_op_dsm_muid
;
73 params
[1].type
= ACPI_TYPE_INTEGER
;
74 params
[1].integer
.value
= 0x00000100;
75 params
[2].type
= ACPI_TYPE_INTEGER
;
76 params
[2].integer
.value
= func
;
77 params
[3].type
= ACPI_TYPE_BUFFER
;
78 params
[3].buffer
.length
= 4;
79 /* ACPI is little endian, AABBCCDD becomes {DD,CC,BB,AA} */
80 for (i
= 0; i
< 4; i
++)
81 args_buff
[i
] = (arg
>> i
* 8) & 0xFF;
82 params
[3].buffer
.pointer
= args_buff
;
84 err
= acpi_evaluate_object(handle
, "_DSM", &input
, &output
);
86 printk(KERN_INFO
"failed to evaluate _DSM: %d\n", err
);
90 obj
= (union acpi_object
*)output
.pointer
;
92 if (obj
->type
== ACPI_TYPE_INTEGER
)
93 if (obj
->integer
.value
== 0x80000002) {
97 if (obj
->type
== ACPI_TYPE_BUFFER
) {
98 if (obj
->buffer
.length
== 4 && result
) {
100 *result
|= obj
->buffer
.pointer
[0];
101 *result
|= (obj
->buffer
.pointer
[1] << 8);
102 *result
|= (obj
->buffer
.pointer
[2] << 16);
103 *result
|= (obj
->buffer
.pointer
[3] << 24);
107 kfree(output
.pointer
);
111 static int nouveau_dsm(acpi_handle handle
, int func
, int arg
, uint32_t *result
)
113 struct acpi_buffer output
= { ACPI_ALLOCATE_BUFFER
, NULL
};
114 struct acpi_object_list input
;
115 union acpi_object params
[4];
116 union acpi_object
*obj
;
120 input
.pointer
= params
;
121 params
[0].type
= ACPI_TYPE_BUFFER
;
122 params
[0].buffer
.length
= sizeof(nouveau_dsm_muid
);
123 params
[0].buffer
.pointer
= (char *)nouveau_dsm_muid
;
124 params
[1].type
= ACPI_TYPE_INTEGER
;
125 params
[1].integer
.value
= 0x00000102;
126 params
[2].type
= ACPI_TYPE_INTEGER
;
127 params
[2].integer
.value
= func
;
128 params
[3].type
= ACPI_TYPE_INTEGER
;
129 params
[3].integer
.value
= arg
;
131 err
= acpi_evaluate_object(handle
, "_DSM", &input
, &output
);
133 printk(KERN_INFO
"failed to evaluate _DSM: %d\n", err
);
137 obj
= (union acpi_object
*)output
.pointer
;
139 if (obj
->type
== ACPI_TYPE_INTEGER
)
140 if (obj
->integer
.value
== 0x80000002)
143 if (obj
->type
== ACPI_TYPE_BUFFER
) {
144 if (obj
->buffer
.length
== 4 && result
) {
146 *result
|= obj
->buffer
.pointer
[0];
147 *result
|= (obj
->buffer
.pointer
[1] << 8);
148 *result
|= (obj
->buffer
.pointer
[2] << 16);
149 *result
|= (obj
->buffer
.pointer
[3] << 24);
153 kfree(output
.pointer
);
157 /* Returns 1 if a DSM function is usable and 0 otherwise */
158 static int nouveau_test_dsm(acpi_handle test_handle
,
159 int (*dsm_func
)(acpi_handle
, int, int, uint32_t *),
164 /* Function 0 returns a Buffer containing available functions. The args
165 * parameter is ignored for function 0, so just put 0 in it */
166 if (dsm_func(test_handle
, 0, 0, &result
))
169 /* ACPI Spec v4 9.14.1: if bit 0 is zero, no function is supported. If
170 * the n-th bit is enabled, function n is supported */
171 return result
& 1 && result
& (1 << sfnc
);
174 static int nouveau_dsm_switch_mux(acpi_handle handle
, int mux_id
)
176 mxm_wmi_call_mxmx(mux_id
== NOUVEAU_DSM_LED_STAMINA
? MXM_MXDS_ADAPTER_IGD
: MXM_MXDS_ADAPTER_0
);
177 mxm_wmi_call_mxds(mux_id
== NOUVEAU_DSM_LED_STAMINA
? MXM_MXDS_ADAPTER_IGD
: MXM_MXDS_ADAPTER_0
);
178 return nouveau_dsm(handle
, NOUVEAU_DSM_LED
, mux_id
, NULL
);
181 static int nouveau_dsm_set_discrete_state(acpi_handle handle
, enum vga_switcheroo_state state
)
184 if (state
== VGA_SWITCHEROO_ON
)
185 arg
= NOUVEAU_DSM_POWER_SPEED
;
187 arg
= NOUVEAU_DSM_POWER_STAMINA
;
188 nouveau_dsm(handle
, NOUVEAU_DSM_POWER
, arg
, NULL
);
192 static int nouveau_dsm_switchto(enum vga_switcheroo_client_id id
)
194 if (!nouveau_dsm_priv
.dsm_detected
)
196 if (id
== VGA_SWITCHEROO_IGD
)
197 return nouveau_dsm_switch_mux(nouveau_dsm_priv
.dhandle
, NOUVEAU_DSM_LED_STAMINA
);
199 return nouveau_dsm_switch_mux(nouveau_dsm_priv
.dhandle
, NOUVEAU_DSM_LED_SPEED
);
202 static int nouveau_dsm_power_state(enum vga_switcheroo_client_id id
,
203 enum vga_switcheroo_state state
)
205 if (id
== VGA_SWITCHEROO_IGD
)
208 /* Optimus laptops have the card already disabled in
209 * nouveau_switcheroo_set_state */
210 if (!nouveau_dsm_priv
.dsm_detected
)
213 return nouveau_dsm_set_discrete_state(nouveau_dsm_priv
.dhandle
, state
);
216 static int nouveau_dsm_get_client_id(struct pci_dev
*pdev
)
218 /* easy option one - intel vendor ID means Integrated */
219 if (pdev
->vendor
== PCI_VENDOR_ID_INTEL
)
220 return VGA_SWITCHEROO_IGD
;
222 /* is this device on Bus 0? - this may need improving */
223 if (pdev
->bus
->number
== 0)
224 return VGA_SWITCHEROO_IGD
;
226 return VGA_SWITCHEROO_DIS
;
229 static struct vga_switcheroo_handler nouveau_dsm_handler
= {
230 .switchto
= nouveau_dsm_switchto
,
231 .power_state
= nouveau_dsm_power_state
,
232 .get_client_id
= nouveau_dsm_get_client_id
,
235 static int nouveau_dsm_pci_probe(struct pci_dev
*pdev
)
237 acpi_handle dhandle
, nvidia_handle
;
241 dhandle
= DEVICE_ACPI_HANDLE(&pdev
->dev
);
245 status
= acpi_get_handle(dhandle
, "_DSM", &nvidia_handle
);
246 if (ACPI_FAILURE(status
)) {
250 if (nouveau_test_dsm(dhandle
, nouveau_dsm
, NOUVEAU_DSM_POWER
))
251 retval
|= NOUVEAU_DSM_HAS_MUX
;
253 if (nouveau_test_dsm(dhandle
, nouveau_optimus_dsm
,
254 NOUVEAU_DSM_OPTIMUS_FN
))
255 retval
|= NOUVEAU_DSM_HAS_OPT
;
258 nouveau_dsm_priv
.dhandle
= dhandle
;
263 static bool nouveau_dsm_detect(void)
265 char acpi_method_name
[255] = { 0 };
266 struct acpi_buffer buffer
= {sizeof(acpi_method_name
), acpi_method_name
};
267 struct pci_dev
*pdev
= NULL
;
275 /* lookup the MXM GUID */
276 guid_valid
= mxm_wmi_supported();
279 printk("MXM: GUID detected in BIOS\n");
281 /* now do DSM detection */
282 while ((pdev
= pci_get_class(PCI_CLASS_DISPLAY_VGA
<< 8, pdev
)) != NULL
) {
285 retval
= nouveau_dsm_pci_probe(pdev
);
286 if (retval
& NOUVEAU_DSM_HAS_MUX
)
288 if (retval
& NOUVEAU_DSM_HAS_OPT
)
292 /* find the optimus DSM or the old v1 DSM */
293 if (has_optimus
== 1) {
294 acpi_get_name(nouveau_dsm_priv
.dhandle
, ACPI_FULL_PATHNAME
,
296 printk(KERN_INFO
"VGA switcheroo: detected Optimus DSM method %s handle\n",
298 nouveau_dsm_priv
.optimus_detected
= true;
300 } else if (vga_count
== 2 && has_dsm
&& guid_valid
) {
301 acpi_get_name(nouveau_dsm_priv
.dhandle
, ACPI_FULL_PATHNAME
,
303 printk(KERN_INFO
"VGA switcheroo: detected DSM switching method %s handle\n",
305 nouveau_dsm_priv
.dsm_detected
= true;
313 void nouveau_register_dsm_handler(void)
317 r
= nouveau_dsm_detect();
321 vga_switcheroo_register_handler(&nouveau_dsm_handler
);
324 /* Must be called for Optimus models before the card can be turned off */
325 void nouveau_switcheroo_optimus_dsm(void)
328 if (!nouveau_dsm_priv
.optimus_detected
)
331 nouveau_optimus_dsm(nouveau_dsm_priv
.dhandle
, NOUVEAU_DSM_OPTIMUS_FN
,
332 NOUVEAU_DSM_OPTIMUS_ARGS
, &result
);
335 void nouveau_unregister_dsm_handler(void)
337 if (nouveau_dsm_priv
.optimus_detected
|| nouveau_dsm_priv
.dsm_detected
)
338 vga_switcheroo_unregister_handler();
341 /* retrieve the ROM in 4k blocks */
342 static int nouveau_rom_call(acpi_handle rom_handle
, uint8_t *bios
,
346 union acpi_object rom_arg_elements
[2], *obj
;
347 struct acpi_object_list rom_arg
;
348 struct acpi_buffer buffer
= { ACPI_ALLOCATE_BUFFER
, NULL
};
351 rom_arg
.pointer
= &rom_arg_elements
[0];
353 rom_arg_elements
[0].type
= ACPI_TYPE_INTEGER
;
354 rom_arg_elements
[0].integer
.value
= offset
;
356 rom_arg_elements
[1].type
= ACPI_TYPE_INTEGER
;
357 rom_arg_elements
[1].integer
.value
= len
;
359 status
= acpi_evaluate_object(rom_handle
, NULL
, &rom_arg
, &buffer
);
360 if (ACPI_FAILURE(status
)) {
361 printk(KERN_INFO
"failed to evaluate ROM got %s\n", acpi_format_exception(status
));
364 obj
= (union acpi_object
*)buffer
.pointer
;
365 memcpy(bios
+offset
, obj
->buffer
.pointer
, len
);
366 kfree(buffer
.pointer
);
370 bool nouveau_acpi_rom_supported(struct pci_dev
*pdev
)
373 acpi_handle dhandle
, rom_handle
;
375 if (!nouveau_dsm_priv
.dsm_detected
&& !nouveau_dsm_priv
.optimus_detected
)
378 dhandle
= DEVICE_ACPI_HANDLE(&pdev
->dev
);
382 status
= acpi_get_handle(dhandle
, "_ROM", &rom_handle
);
383 if (ACPI_FAILURE(status
))
386 nouveau_dsm_priv
.rom_handle
= rom_handle
;
390 int nouveau_acpi_get_bios_chunk(uint8_t *bios
, int offset
, int len
)
392 return nouveau_rom_call(nouveau_dsm_priv
.rom_handle
, bios
, offset
, len
);
396 nouveau_acpi_edid(struct drm_device
*dev
, struct drm_connector
*connector
)
398 struct acpi_device
*acpidev
;
403 switch (connector
->connector_type
) {
404 case DRM_MODE_CONNECTOR_LVDS
:
405 case DRM_MODE_CONNECTOR_eDP
:
406 type
= ACPI_VIDEO_DISPLAY_LCD
;
412 handle
= DEVICE_ACPI_HANDLE(&dev
->pdev
->dev
);
416 ret
= acpi_bus_get_device(handle
, &acpidev
);
420 ret
= acpi_video_get_edid(acpidev
, type
, -1, &edid
);
424 return kmemdup(edid
, EDID_LENGTH
, GFP_KERNEL
);