2 #include <linux/acpi.h>
3 #include <linux/slab.h>
4 #include <linux/mxm-wmi.h>
5 #include <linux/vga_switcheroo.h>
6 #include <drm/drm_edid.h>
7 #include <acpi/video.h>
9 #include "nouveau_drm.h"
10 #include "nouveau_acpi.h"
12 #define NOUVEAU_DSM_LED 0x02
13 #define NOUVEAU_DSM_LED_STATE 0x00
14 #define NOUVEAU_DSM_LED_OFF 0x10
15 #define NOUVEAU_DSM_LED_STAMINA 0x11
16 #define NOUVEAU_DSM_LED_SPEED 0x12
18 #define NOUVEAU_DSM_POWER 0x03
19 #define NOUVEAU_DSM_POWER_STATE 0x00
20 #define NOUVEAU_DSM_POWER_SPEED 0x01
21 #define NOUVEAU_DSM_POWER_STAMINA 0x02
23 #define NOUVEAU_DSM_OPTIMUS_CAPS 0x1A
24 #define NOUVEAU_DSM_OPTIMUS_FLAGS 0x1B
26 #define NOUVEAU_DSM_OPTIMUS_POWERDOWN_PS3 (3 << 24)
27 #define NOUVEAU_DSM_OPTIMUS_NO_POWERDOWN_PS3 (2 << 24)
28 #define NOUVEAU_DSM_OPTIMUS_FLAGS_CHANGED (1)
30 #define NOUVEAU_DSM_OPTIMUS_SET_POWERDOWN (NOUVEAU_DSM_OPTIMUS_POWERDOWN_PS3 | NOUVEAU_DSM_OPTIMUS_FLAGS_CHANGED)
32 /* result of the optimus caps function */
33 #define OPTIMUS_ENABLED (1 << 0)
34 #define OPTIMUS_STATUS_MASK (3 << 3)
35 #define OPTIMUS_STATUS_OFF (0 << 3)
36 #define OPTIMUS_STATUS_ON_ENABLED (1 << 3)
37 #define OPTIMUS_STATUS_PWR_STABLE (3 << 3)
38 #define OPTIMUS_DISPLAY_HOTPLUG (1 << 6)
39 #define OPTIMUS_CAPS_MASK (7 << 24)
40 #define OPTIMUS_DYNAMIC_PWR_CAP (1 << 24)
42 #define OPTIMUS_AUDIO_CAPS_MASK (3 << 27)
43 #define OPTIMUS_HDA_CODEC_MASK (2 << 27) /* hda bios control */
45 static struct nouveau_dsm_priv
{
47 bool optimus_detected
;
49 acpi_handle rom_handle
;
52 bool nouveau_is_optimus(void) {
53 return nouveau_dsm_priv
.optimus_detected
;
56 bool nouveau_is_v1_dsm(void) {
57 return nouveau_dsm_priv
.dsm_detected
;
60 #define NOUVEAU_DSM_HAS_MUX 0x1
61 #define NOUVEAU_DSM_HAS_OPT 0x2
63 static const char nouveau_dsm_muid
[] = {
64 0xA0, 0xA0, 0x95, 0x9D, 0x60, 0x00, 0x48, 0x4D,
65 0xB3, 0x4D, 0x7E, 0x5F, 0xEA, 0x12, 0x9F, 0xD4,
68 static const char nouveau_op_dsm_muid
[] = {
69 0xF8, 0xD8, 0x86, 0xA4, 0xDA, 0x0B, 0x1B, 0x47,
70 0xA7, 0x2B, 0x60, 0x42, 0xA6, 0xB5, 0xBE, 0xE0,
73 static int nouveau_optimus_dsm(acpi_handle handle
, int func
, int arg
, uint32_t *result
)
75 struct acpi_buffer output
= { ACPI_ALLOCATE_BUFFER
, NULL
};
76 struct acpi_object_list input
;
77 union acpi_object params
[4];
78 union acpi_object
*obj
;
83 input
.pointer
= params
;
84 params
[0].type
= ACPI_TYPE_BUFFER
;
85 params
[0].buffer
.length
= sizeof(nouveau_op_dsm_muid
);
86 params
[0].buffer
.pointer
= (char *)nouveau_op_dsm_muid
;
87 params
[1].type
= ACPI_TYPE_INTEGER
;
88 params
[1].integer
.value
= 0x00000100;
89 params
[2].type
= ACPI_TYPE_INTEGER
;
90 params
[2].integer
.value
= func
;
91 params
[3].type
= ACPI_TYPE_BUFFER
;
92 params
[3].buffer
.length
= 4;
93 /* ACPI is little endian, AABBCCDD becomes {DD,CC,BB,AA} */
94 for (i
= 0; i
< 4; i
++)
95 args_buff
[i
] = (arg
>> i
* 8) & 0xFF;
96 params
[3].buffer
.pointer
= args_buff
;
98 err
= acpi_evaluate_object(handle
, "_DSM", &input
, &output
);
100 printk(KERN_INFO
"failed to evaluate _DSM: %d\n", err
);
104 obj
= (union acpi_object
*)output
.pointer
;
106 if (obj
->type
== ACPI_TYPE_INTEGER
)
107 if (obj
->integer
.value
== 0x80000002) {
111 if (obj
->type
== ACPI_TYPE_BUFFER
) {
112 if (obj
->buffer
.length
== 4 && result
) {
114 *result
|= obj
->buffer
.pointer
[0];
115 *result
|= (obj
->buffer
.pointer
[1] << 8);
116 *result
|= (obj
->buffer
.pointer
[2] << 16);
117 *result
|= (obj
->buffer
.pointer
[3] << 24);
121 kfree(output
.pointer
);
125 static int nouveau_dsm(acpi_handle handle
, int func
, int arg
, uint32_t *result
)
127 struct acpi_buffer output
= { ACPI_ALLOCATE_BUFFER
, NULL
};
128 struct acpi_object_list input
;
129 union acpi_object params
[4];
130 union acpi_object
*obj
;
134 input
.pointer
= params
;
135 params
[0].type
= ACPI_TYPE_BUFFER
;
136 params
[0].buffer
.length
= sizeof(nouveau_dsm_muid
);
137 params
[0].buffer
.pointer
= (char *)nouveau_dsm_muid
;
138 params
[1].type
= ACPI_TYPE_INTEGER
;
139 params
[1].integer
.value
= 0x00000102;
140 params
[2].type
= ACPI_TYPE_INTEGER
;
141 params
[2].integer
.value
= func
;
142 params
[3].type
= ACPI_TYPE_INTEGER
;
143 params
[3].integer
.value
= arg
;
145 err
= acpi_evaluate_object(handle
, "_DSM", &input
, &output
);
147 printk(KERN_INFO
"failed to evaluate _DSM: %d\n", err
);
151 obj
= (union acpi_object
*)output
.pointer
;
153 if (obj
->type
== ACPI_TYPE_INTEGER
)
154 if (obj
->integer
.value
== 0x80000002)
157 if (obj
->type
== ACPI_TYPE_BUFFER
) {
158 if (obj
->buffer
.length
== 4 && result
) {
160 *result
|= obj
->buffer
.pointer
[0];
161 *result
|= (obj
->buffer
.pointer
[1] << 8);
162 *result
|= (obj
->buffer
.pointer
[2] << 16);
163 *result
|= (obj
->buffer
.pointer
[3] << 24);
167 kfree(output
.pointer
);
171 /* Returns 1 if a DSM function is usable and 0 otherwise */
172 static int nouveau_test_dsm(acpi_handle test_handle
,
173 int (*dsm_func
)(acpi_handle
, int, int, uint32_t *),
178 /* Function 0 returns a Buffer containing available functions. The args
179 * parameter is ignored for function 0, so just put 0 in it */
180 if (dsm_func(test_handle
, 0, 0, &result
))
183 /* ACPI Spec v4 9.14.1: if bit 0 is zero, no function is supported. If
184 * the n-th bit is enabled, function n is supported */
185 return result
& 1 && result
& (1 << sfnc
);
188 static int nouveau_dsm_switch_mux(acpi_handle handle
, int mux_id
)
190 mxm_wmi_call_mxmx(mux_id
== NOUVEAU_DSM_LED_STAMINA
? MXM_MXDS_ADAPTER_IGD
: MXM_MXDS_ADAPTER_0
);
191 mxm_wmi_call_mxds(mux_id
== NOUVEAU_DSM_LED_STAMINA
? MXM_MXDS_ADAPTER_IGD
: MXM_MXDS_ADAPTER_0
);
192 return nouveau_dsm(handle
, NOUVEAU_DSM_LED
, mux_id
, NULL
);
195 static int nouveau_dsm_set_discrete_state(acpi_handle handle
, enum vga_switcheroo_state state
)
198 if (state
== VGA_SWITCHEROO_ON
)
199 arg
= NOUVEAU_DSM_POWER_SPEED
;
201 arg
= NOUVEAU_DSM_POWER_STAMINA
;
202 nouveau_dsm(handle
, NOUVEAU_DSM_POWER
, arg
, NULL
);
206 static int nouveau_dsm_switchto(enum vga_switcheroo_client_id id
)
208 if (!nouveau_dsm_priv
.dsm_detected
)
210 if (id
== VGA_SWITCHEROO_IGD
)
211 return nouveau_dsm_switch_mux(nouveau_dsm_priv
.dhandle
, NOUVEAU_DSM_LED_STAMINA
);
213 return nouveau_dsm_switch_mux(nouveau_dsm_priv
.dhandle
, NOUVEAU_DSM_LED_SPEED
);
216 static int nouveau_dsm_power_state(enum vga_switcheroo_client_id id
,
217 enum vga_switcheroo_state state
)
219 if (id
== VGA_SWITCHEROO_IGD
)
222 /* Optimus laptops have the card already disabled in
223 * nouveau_switcheroo_set_state */
224 if (!nouveau_dsm_priv
.dsm_detected
)
227 return nouveau_dsm_set_discrete_state(nouveau_dsm_priv
.dhandle
, state
);
230 static int nouveau_dsm_get_client_id(struct pci_dev
*pdev
)
232 /* easy option one - intel vendor ID means Integrated */
233 if (pdev
->vendor
== PCI_VENDOR_ID_INTEL
)
234 return VGA_SWITCHEROO_IGD
;
236 /* is this device on Bus 0? - this may need improving */
237 if (pdev
->bus
->number
== 0)
238 return VGA_SWITCHEROO_IGD
;
240 return VGA_SWITCHEROO_DIS
;
243 static struct vga_switcheroo_handler nouveau_dsm_handler
= {
244 .switchto
= nouveau_dsm_switchto
,
245 .power_state
= nouveau_dsm_power_state
,
246 .get_client_id
= nouveau_dsm_get_client_id
,
249 static int nouveau_dsm_pci_probe(struct pci_dev
*pdev
)
254 dhandle
= ACPI_HANDLE(&pdev
->dev
);
258 if (!acpi_has_method(dhandle
, "_DSM"))
261 if (nouveau_test_dsm(dhandle
, nouveau_dsm
, NOUVEAU_DSM_POWER
))
262 retval
|= NOUVEAU_DSM_HAS_MUX
;
264 if (nouveau_test_dsm(dhandle
, nouveau_optimus_dsm
,
265 NOUVEAU_DSM_OPTIMUS_CAPS
))
266 retval
|= NOUVEAU_DSM_HAS_OPT
;
268 if (retval
& NOUVEAU_DSM_HAS_OPT
) {
270 nouveau_optimus_dsm(dhandle
, NOUVEAU_DSM_OPTIMUS_CAPS
, 0,
272 dev_info(&pdev
->dev
, "optimus capabilities: %s, status %s%s\n",
273 (result
& OPTIMUS_ENABLED
) ? "enabled" : "disabled",
274 (result
& OPTIMUS_DYNAMIC_PWR_CAP
) ? "dynamic power, " : "",
275 (result
& OPTIMUS_HDA_CODEC_MASK
) ? "hda bios codec supported" : "");
278 nouveau_dsm_priv
.dhandle
= dhandle
;
283 static bool nouveau_dsm_detect(void)
285 char acpi_method_name
[255] = { 0 };
286 struct acpi_buffer buffer
= {sizeof(acpi_method_name
), acpi_method_name
};
287 struct pci_dev
*pdev
= NULL
;
295 /* lookup the MXM GUID */
296 guid_valid
= mxm_wmi_supported();
299 printk("MXM: GUID detected in BIOS\n");
301 /* now do DSM detection */
302 while ((pdev
= pci_get_class(PCI_CLASS_DISPLAY_VGA
<< 8, pdev
)) != NULL
) {
305 retval
= nouveau_dsm_pci_probe(pdev
);
306 if (retval
& NOUVEAU_DSM_HAS_MUX
)
308 if (retval
& NOUVEAU_DSM_HAS_OPT
)
312 while ((pdev
= pci_get_class(PCI_CLASS_DISPLAY_3D
<< 8, pdev
)) != NULL
) {
315 retval
= nouveau_dsm_pci_probe(pdev
);
316 if (retval
& NOUVEAU_DSM_HAS_MUX
)
318 if (retval
& NOUVEAU_DSM_HAS_OPT
)
322 /* find the optimus DSM or the old v1 DSM */
323 if (has_optimus
== 1) {
324 acpi_get_name(nouveau_dsm_priv
.dhandle
, ACPI_FULL_PATHNAME
,
326 printk(KERN_INFO
"VGA switcheroo: detected Optimus DSM method %s handle\n",
328 nouveau_dsm_priv
.optimus_detected
= true;
330 } else if (vga_count
== 2 && has_dsm
&& guid_valid
) {
331 acpi_get_name(nouveau_dsm_priv
.dhandle
, ACPI_FULL_PATHNAME
,
333 printk(KERN_INFO
"VGA switcheroo: detected DSM switching method %s handle\n",
335 nouveau_dsm_priv
.dsm_detected
= true;
343 void nouveau_register_dsm_handler(void)
347 r
= nouveau_dsm_detect();
351 vga_switcheroo_register_handler(&nouveau_dsm_handler
);
354 /* Must be called for Optimus models before the card can be turned off */
355 void nouveau_switcheroo_optimus_dsm(void)
358 if (!nouveau_dsm_priv
.optimus_detected
)
361 nouveau_optimus_dsm(nouveau_dsm_priv
.dhandle
, NOUVEAU_DSM_OPTIMUS_FLAGS
,
364 nouveau_optimus_dsm(nouveau_dsm_priv
.dhandle
, NOUVEAU_DSM_OPTIMUS_CAPS
,
365 NOUVEAU_DSM_OPTIMUS_SET_POWERDOWN
, &result
);
369 void nouveau_unregister_dsm_handler(void)
371 if (nouveau_dsm_priv
.optimus_detected
|| nouveau_dsm_priv
.dsm_detected
)
372 vga_switcheroo_unregister_handler();
375 /* retrieve the ROM in 4k blocks */
376 static int nouveau_rom_call(acpi_handle rom_handle
, uint8_t *bios
,
380 union acpi_object rom_arg_elements
[2], *obj
;
381 struct acpi_object_list rom_arg
;
382 struct acpi_buffer buffer
= { ACPI_ALLOCATE_BUFFER
, NULL
};
385 rom_arg
.pointer
= &rom_arg_elements
[0];
387 rom_arg_elements
[0].type
= ACPI_TYPE_INTEGER
;
388 rom_arg_elements
[0].integer
.value
= offset
;
390 rom_arg_elements
[1].type
= ACPI_TYPE_INTEGER
;
391 rom_arg_elements
[1].integer
.value
= len
;
393 status
= acpi_evaluate_object(rom_handle
, NULL
, &rom_arg
, &buffer
);
394 if (ACPI_FAILURE(status
)) {
395 printk(KERN_INFO
"failed to evaluate ROM got %s\n", acpi_format_exception(status
));
398 obj
= (union acpi_object
*)buffer
.pointer
;
399 memcpy(bios
+offset
, obj
->buffer
.pointer
, len
);
400 kfree(buffer
.pointer
);
404 bool nouveau_acpi_rom_supported(struct pci_dev
*pdev
)
407 acpi_handle dhandle
, rom_handle
;
409 if (!nouveau_dsm_priv
.dsm_detected
&& !nouveau_dsm_priv
.optimus_detected
)
412 dhandle
= ACPI_HANDLE(&pdev
->dev
);
416 status
= acpi_get_handle(dhandle
, "_ROM", &rom_handle
);
417 if (ACPI_FAILURE(status
))
420 nouveau_dsm_priv
.rom_handle
= rom_handle
;
424 int nouveau_acpi_get_bios_chunk(uint8_t *bios
, int offset
, int len
)
426 return nouveau_rom_call(nouveau_dsm_priv
.rom_handle
, bios
, offset
, len
);
430 nouveau_acpi_edid(struct drm_device
*dev
, struct drm_connector
*connector
)
432 struct acpi_device
*acpidev
;
437 switch (connector
->connector_type
) {
438 case DRM_MODE_CONNECTOR_LVDS
:
439 case DRM_MODE_CONNECTOR_eDP
:
440 type
= ACPI_VIDEO_DISPLAY_LCD
;
446 handle
= ACPI_HANDLE(&dev
->pdev
->dev
);
450 ret
= acpi_bus_get_device(handle
, &acpidev
);
454 ret
= acpi_video_get_edid(acpidev
, type
, -1, &edid
);
458 return kmemdup(edid
, EDID_LENGTH
, GFP_KERNEL
);