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_CAPS 0x1A
29 #define NOUVEAU_DSM_OPTIMUS_FLAGS 0x1B
31 #define NOUVEAU_DSM_OPTIMUS_POWERDOWN_PS3 (3 << 24)
32 #define NOUVEAU_DSM_OPTIMUS_NO_POWERDOWN_PS3 (2 << 24)
33 #define NOUVEAU_DSM_OPTIMUS_FLAGS_CHANGED (1)
35 #define NOUVEAU_DSM_OPTIMUS_SET_POWERDOWN (NOUVEAU_DSM_OPTIMUS_POWERDOWN_PS3 | NOUVEAU_DSM_OPTIMUS_FLAGS_CHANGED)
37 /* result of the optimus caps function */
38 #define OPTIMUS_ENABLED (1 << 0)
39 #define OPTIMUS_STATUS_MASK (3 << 3)
40 #define OPTIMUS_STATUS_OFF (0 << 3)
41 #define OPTIMUS_STATUS_ON_ENABLED (1 << 3)
42 #define OPTIMUS_STATUS_PWR_STABLE (3 << 3)
43 #define OPTIMUS_DISPLAY_HOTPLUG (1 << 6)
44 #define OPTIMUS_CAPS_MASK (7 << 24)
45 #define OPTIMUS_DYNAMIC_PWR_CAP (1 << 24)
47 #define OPTIMUS_AUDIO_CAPS_MASK (3 << 27)
48 #define OPTIMUS_HDA_CODEC_MASK (2 << 27) /* hda bios control */
50 static struct nouveau_dsm_priv
{
52 bool optimus_detected
;
54 acpi_handle other_handle
;
55 acpi_handle rom_handle
;
58 bool nouveau_is_optimus(void) {
59 return nouveau_dsm_priv
.optimus_detected
;
62 bool nouveau_is_v1_dsm(void) {
63 return nouveau_dsm_priv
.dsm_detected
;
66 #define NOUVEAU_DSM_HAS_MUX 0x1
67 #define NOUVEAU_DSM_HAS_OPT 0x2
69 #ifdef CONFIG_VGA_SWITCHEROO
70 static const char nouveau_dsm_muid
[] = {
71 0xA0, 0xA0, 0x95, 0x9D, 0x60, 0x00, 0x48, 0x4D,
72 0xB3, 0x4D, 0x7E, 0x5F, 0xEA, 0x12, 0x9F, 0xD4,
75 static const char nouveau_op_dsm_muid
[] = {
76 0xF8, 0xD8, 0x86, 0xA4, 0xDA, 0x0B, 0x1B, 0x47,
77 0xA7, 0x2B, 0x60, 0x42, 0xA6, 0xB5, 0xBE, 0xE0,
80 static int nouveau_optimus_dsm(acpi_handle handle
, int func
, int arg
, uint32_t *result
)
82 struct acpi_buffer output
= { ACPI_ALLOCATE_BUFFER
, NULL
};
83 struct acpi_object_list input
;
84 union acpi_object params
[4];
85 union acpi_object
*obj
;
90 input
.pointer
= params
;
91 params
[0].type
= ACPI_TYPE_BUFFER
;
92 params
[0].buffer
.length
= sizeof(nouveau_op_dsm_muid
);
93 params
[0].buffer
.pointer
= (char *)nouveau_op_dsm_muid
;
94 params
[1].type
= ACPI_TYPE_INTEGER
;
95 params
[1].integer
.value
= 0x00000100;
96 params
[2].type
= ACPI_TYPE_INTEGER
;
97 params
[2].integer
.value
= func
;
98 params
[3].type
= ACPI_TYPE_BUFFER
;
99 params
[3].buffer
.length
= 4;
100 /* ACPI is little endian, AABBCCDD becomes {DD,CC,BB,AA} */
101 for (i
= 0; i
< 4; i
++)
102 args_buff
[i
] = (arg
>> i
* 8) & 0xFF;
103 params
[3].buffer
.pointer
= args_buff
;
105 err
= acpi_evaluate_object(handle
, "_DSM", &input
, &output
);
107 printk(KERN_INFO
"failed to evaluate _DSM: %d\n", err
);
111 obj
= (union acpi_object
*)output
.pointer
;
113 if (obj
->type
== ACPI_TYPE_INTEGER
)
114 if (obj
->integer
.value
== 0x80000002) {
118 if (obj
->type
== ACPI_TYPE_BUFFER
) {
119 if (obj
->buffer
.length
== 4 && result
) {
121 *result
|= obj
->buffer
.pointer
[0];
122 *result
|= (obj
->buffer
.pointer
[1] << 8);
123 *result
|= (obj
->buffer
.pointer
[2] << 16);
124 *result
|= (obj
->buffer
.pointer
[3] << 24);
128 kfree(output
.pointer
);
132 static int nouveau_dsm(acpi_handle handle
, int func
, int arg
, uint32_t *result
)
134 struct acpi_buffer output
= { ACPI_ALLOCATE_BUFFER
, NULL
};
135 struct acpi_object_list input
;
136 union acpi_object params
[4];
137 union acpi_object
*obj
;
141 input
.pointer
= params
;
142 params
[0].type
= ACPI_TYPE_BUFFER
;
143 params
[0].buffer
.length
= sizeof(nouveau_dsm_muid
);
144 params
[0].buffer
.pointer
= (char *)nouveau_dsm_muid
;
145 params
[1].type
= ACPI_TYPE_INTEGER
;
146 params
[1].integer
.value
= 0x00000102;
147 params
[2].type
= ACPI_TYPE_INTEGER
;
148 params
[2].integer
.value
= func
;
149 params
[3].type
= ACPI_TYPE_INTEGER
;
150 params
[3].integer
.value
= arg
;
152 err
= acpi_evaluate_object(handle
, "_DSM", &input
, &output
);
154 printk(KERN_INFO
"failed to evaluate _DSM: %d\n", err
);
158 obj
= (union acpi_object
*)output
.pointer
;
160 if (obj
->type
== ACPI_TYPE_INTEGER
)
161 if (obj
->integer
.value
== 0x80000002)
164 if (obj
->type
== ACPI_TYPE_BUFFER
) {
165 if (obj
->buffer
.length
== 4 && result
) {
167 *result
|= obj
->buffer
.pointer
[0];
168 *result
|= (obj
->buffer
.pointer
[1] << 8);
169 *result
|= (obj
->buffer
.pointer
[2] << 16);
170 *result
|= (obj
->buffer
.pointer
[3] << 24);
174 kfree(output
.pointer
);
178 /* Returns 1 if a DSM function is usable and 0 otherwise */
179 static int nouveau_test_dsm(acpi_handle test_handle
,
180 int (*dsm_func
)(acpi_handle
, int, int, uint32_t *),
185 /* Function 0 returns a Buffer containing available functions. The args
186 * parameter is ignored for function 0, so just put 0 in it */
187 if (dsm_func(test_handle
, 0, 0, &result
))
190 /* ACPI Spec v4 9.14.1: if bit 0 is zero, no function is supported. If
191 * the n-th bit is enabled, function n is supported */
192 return result
& 1 && result
& (1 << sfnc
);
195 static int nouveau_dsm_switch_mux(acpi_handle handle
, int mux_id
)
197 mxm_wmi_call_mxmx(mux_id
== NOUVEAU_DSM_LED_STAMINA
? MXM_MXDS_ADAPTER_IGD
: MXM_MXDS_ADAPTER_0
);
198 mxm_wmi_call_mxds(mux_id
== NOUVEAU_DSM_LED_STAMINA
? MXM_MXDS_ADAPTER_IGD
: MXM_MXDS_ADAPTER_0
);
199 return nouveau_dsm(handle
, NOUVEAU_DSM_LED
, mux_id
, NULL
);
202 static int nouveau_dsm_set_discrete_state(acpi_handle handle
, enum vga_switcheroo_state state
)
205 if (state
== VGA_SWITCHEROO_ON
)
206 arg
= NOUVEAU_DSM_POWER_SPEED
;
208 arg
= NOUVEAU_DSM_POWER_STAMINA
;
209 nouveau_dsm(handle
, NOUVEAU_DSM_POWER
, arg
, NULL
);
213 static int nouveau_dsm_switchto(enum vga_switcheroo_client_id id
)
215 if (!nouveau_dsm_priv
.dsm_detected
)
217 if (id
== VGA_SWITCHEROO_IGD
)
218 return nouveau_dsm_switch_mux(nouveau_dsm_priv
.dhandle
, NOUVEAU_DSM_LED_STAMINA
);
220 return nouveau_dsm_switch_mux(nouveau_dsm_priv
.dhandle
, NOUVEAU_DSM_LED_SPEED
);
223 static int nouveau_dsm_power_state(enum vga_switcheroo_client_id id
,
224 enum vga_switcheroo_state state
)
226 if (id
== VGA_SWITCHEROO_IGD
)
229 /* Optimus laptops have the card already disabled in
230 * nouveau_switcheroo_set_state */
231 if (!nouveau_dsm_priv
.dsm_detected
)
234 return nouveau_dsm_set_discrete_state(nouveau_dsm_priv
.dhandle
, state
);
237 static int nouveau_dsm_get_client_id(struct pci_dev
*pdev
)
239 /* easy option one - intel vendor ID means Integrated */
240 if (pdev
->vendor
== PCI_VENDOR_ID_INTEL
)
241 return VGA_SWITCHEROO_IGD
;
243 /* is this device on Bus 0? - this may need improving */
244 if (pdev
->bus
->number
== 0)
245 return VGA_SWITCHEROO_IGD
;
247 return VGA_SWITCHEROO_DIS
;
250 static struct vga_switcheroo_handler nouveau_dsm_handler
= {
251 .switchto
= nouveau_dsm_switchto
,
252 .power_state
= nouveau_dsm_power_state
,
253 .get_client_id
= nouveau_dsm_get_client_id
,
256 static int nouveau_dsm_pci_probe(struct pci_dev
*pdev
)
261 dhandle
= DEVICE_ACPI_HANDLE(&pdev
->dev
);
265 if (!acpi_has_method(dhandle
, "_DSM")) {
266 nouveau_dsm_priv
.other_handle
= dhandle
;
269 if (nouveau_test_dsm(dhandle
, nouveau_dsm
, NOUVEAU_DSM_POWER
))
270 retval
|= NOUVEAU_DSM_HAS_MUX
;
272 if (nouveau_test_dsm(dhandle
, nouveau_optimus_dsm
,
273 NOUVEAU_DSM_OPTIMUS_CAPS
))
274 retval
|= NOUVEAU_DSM_HAS_OPT
;
276 if (retval
& NOUVEAU_DSM_HAS_OPT
) {
278 nouveau_optimus_dsm(dhandle
, NOUVEAU_DSM_OPTIMUS_CAPS
, 0,
280 dev_info(&pdev
->dev
, "optimus capabilities: %s, status %s%s\n",
281 (result
& OPTIMUS_ENABLED
) ? "enabled" : "disabled",
282 (result
& OPTIMUS_DYNAMIC_PWR_CAP
) ? "dynamic power, " : "",
283 (result
& OPTIMUS_HDA_CODEC_MASK
) ? "hda bios codec supported" : "");
286 nouveau_dsm_priv
.dhandle
= dhandle
;
291 static bool nouveau_dsm_detect(void)
293 char acpi_method_name
[255] = { 0 };
294 struct acpi_buffer buffer
= {sizeof(acpi_method_name
), acpi_method_name
};
295 struct pci_dev
*pdev
= NULL
;
303 /* lookup the MXM GUID */
304 guid_valid
= mxm_wmi_supported();
307 printk("MXM: GUID detected in BIOS\n");
309 /* now do DSM detection */
310 while ((pdev
= pci_get_class(PCI_CLASS_DISPLAY_VGA
<< 8, pdev
)) != NULL
) {
313 retval
= nouveau_dsm_pci_probe(pdev
);
314 if (retval
& NOUVEAU_DSM_HAS_MUX
)
316 if (retval
& NOUVEAU_DSM_HAS_OPT
)
320 /* find the optimus DSM or the old v1 DSM */
321 if (has_optimus
== 1) {
322 acpi_get_name(nouveau_dsm_priv
.dhandle
, ACPI_FULL_PATHNAME
,
324 printk(KERN_INFO
"VGA switcheroo: detected Optimus DSM method %s handle\n",
326 nouveau_dsm_priv
.optimus_detected
= true;
328 } else if (vga_count
== 2 && has_dsm
&& guid_valid
) {
329 acpi_get_name(nouveau_dsm_priv
.dhandle
, ACPI_FULL_PATHNAME
,
331 printk(KERN_INFO
"VGA switcheroo: detected DSM switching method %s handle\n",
333 nouveau_dsm_priv
.dsm_detected
= true;
335 * On some systems hotplug events are generated for the device
336 * being switched off when _DSM is executed. They cause ACPI
337 * hotplug to trigger and attempt to remove the device from
338 * the system, which causes it to break down. Prevent that from
339 * happening by setting the no_hotplug flag for the involved
340 * ACPI device objects.
342 acpi_bus_no_hotplug(nouveau_dsm_priv
.dhandle
);
343 acpi_bus_no_hotplug(nouveau_dsm_priv
.other_handle
);
351 void nouveau_register_dsm_handler(void)
355 r
= nouveau_dsm_detect();
359 vga_switcheroo_register_handler(&nouveau_dsm_handler
);
362 /* Must be called for Optimus models before the card can be turned off */
363 void nouveau_switcheroo_optimus_dsm(void)
366 if (!nouveau_dsm_priv
.optimus_detected
)
369 nouveau_optimus_dsm(nouveau_dsm_priv
.dhandle
, NOUVEAU_DSM_OPTIMUS_FLAGS
,
372 nouveau_optimus_dsm(nouveau_dsm_priv
.dhandle
, NOUVEAU_DSM_OPTIMUS_CAPS
,
373 NOUVEAU_DSM_OPTIMUS_SET_POWERDOWN
, &result
);
377 void nouveau_unregister_dsm_handler(void)
379 if (nouveau_dsm_priv
.optimus_detected
|| nouveau_dsm_priv
.dsm_detected
)
380 vga_switcheroo_unregister_handler();
383 void nouveau_register_dsm_handler(void) {}
384 void nouveau_unregister_dsm_handler(void) {}
385 void nouveau_switcheroo_optimus_dsm(void) {}
388 /* retrieve the ROM in 4k blocks */
389 static int nouveau_rom_call(acpi_handle rom_handle
, uint8_t *bios
,
393 union acpi_object rom_arg_elements
[2], *obj
;
394 struct acpi_object_list rom_arg
;
395 struct acpi_buffer buffer
= { ACPI_ALLOCATE_BUFFER
, NULL
};
398 rom_arg
.pointer
= &rom_arg_elements
[0];
400 rom_arg_elements
[0].type
= ACPI_TYPE_INTEGER
;
401 rom_arg_elements
[0].integer
.value
= offset
;
403 rom_arg_elements
[1].type
= ACPI_TYPE_INTEGER
;
404 rom_arg_elements
[1].integer
.value
= len
;
406 status
= acpi_evaluate_object(rom_handle
, NULL
, &rom_arg
, &buffer
);
407 if (ACPI_FAILURE(status
)) {
408 printk(KERN_INFO
"failed to evaluate ROM got %s\n", acpi_format_exception(status
));
411 obj
= (union acpi_object
*)buffer
.pointer
;
412 memcpy(bios
+offset
, obj
->buffer
.pointer
, len
);
413 kfree(buffer
.pointer
);
417 bool nouveau_acpi_rom_supported(struct pci_dev
*pdev
)
420 acpi_handle dhandle
, rom_handle
;
422 dhandle
= DEVICE_ACPI_HANDLE(&pdev
->dev
);
426 status
= acpi_get_handle(dhandle
, "_ROM", &rom_handle
);
427 if (ACPI_FAILURE(status
))
430 nouveau_dsm_priv
.rom_handle
= rom_handle
;
434 int nouveau_acpi_get_bios_chunk(uint8_t *bios
, int offset
, int len
)
436 return nouveau_rom_call(nouveau_dsm_priv
.rom_handle
, bios
, offset
, len
);
440 nouveau_acpi_edid(struct drm_device
*dev
, struct drm_connector
*connector
)
442 struct acpi_device
*acpidev
;
447 switch (connector
->connector_type
) {
448 case DRM_MODE_CONNECTOR_LVDS
:
449 case DRM_MODE_CONNECTOR_eDP
:
450 type
= ACPI_VIDEO_DISPLAY_LCD
;
456 handle
= DEVICE_ACPI_HANDLE(&dev
->pdev
->dev
);
460 ret
= acpi_bus_get_device(handle
, &acpidev
);
464 ret
= acpi_video_get_edid(acpidev
, type
, -1, &edid
);
468 return kmemdup(edid
, EDID_LENGTH
, GFP_KERNEL
);