x86/xen: resume timer irqs early
[linux/fpc-iii.git] / drivers / gpu / drm / nouveau / nouveau_acpi.c
blobefdb689024e34d53f7b462bdf8d4324e2e97e44c
1 #include <linux/pci.h>
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>
7 #include <acpi/acpi.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 {
51 bool dsm_detected;
52 bool optimus_detected;
53 acpi_handle dhandle;
54 acpi_handle other_handle;
55 acpi_handle rom_handle;
56 } nouveau_dsm_priv;
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;
86 int i, err;
87 char args_buff[4];
89 input.count = 4;
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);
106 if (err) {
107 printk(KERN_INFO "failed to evaluate _DSM: %d\n", err);
108 return err;
111 obj = (union acpi_object *)output.pointer;
113 if (obj->type == ACPI_TYPE_INTEGER)
114 if (obj->integer.value == 0x80000002) {
115 return -ENODEV;
118 if (obj->type == ACPI_TYPE_BUFFER) {
119 if (obj->buffer.length == 4 && result) {
120 *result = 0;
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);
129 return 0;
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;
138 int err;
140 input.count = 4;
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);
153 if (err) {
154 printk(KERN_INFO "failed to evaluate _DSM: %d\n", err);
155 return err;
158 obj = (union acpi_object *)output.pointer;
160 if (obj->type == ACPI_TYPE_INTEGER)
161 if (obj->integer.value == 0x80000002)
162 return -ENODEV;
164 if (obj->type == ACPI_TYPE_BUFFER) {
165 if (obj->buffer.length == 4 && result) {
166 *result = 0;
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);
175 return 0;
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 *),
181 int sfnc)
183 u32 result = 0;
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))
188 return 0;
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)
204 int arg;
205 if (state == VGA_SWITCHEROO_ON)
206 arg = NOUVEAU_DSM_POWER_SPEED;
207 else
208 arg = NOUVEAU_DSM_POWER_STAMINA;
209 nouveau_dsm(handle, NOUVEAU_DSM_POWER, arg, NULL);
210 return 0;
213 static int nouveau_dsm_switchto(enum vga_switcheroo_client_id id)
215 if (!nouveau_dsm_priv.dsm_detected)
216 return 0;
217 if (id == VGA_SWITCHEROO_IGD)
218 return nouveau_dsm_switch_mux(nouveau_dsm_priv.dhandle, NOUVEAU_DSM_LED_STAMINA);
219 else
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)
227 return 0;
229 /* Optimus laptops have the card already disabled in
230 * nouveau_switcheroo_set_state */
231 if (!nouveau_dsm_priv.dsm_detected)
232 return 0;
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)
258 acpi_handle dhandle;
259 int retval = 0;
261 dhandle = DEVICE_ACPI_HANDLE(&pdev->dev);
262 if (!dhandle)
263 return false;
265 if (!acpi_has_method(dhandle, "_DSM")) {
266 nouveau_dsm_priv.other_handle = dhandle;
267 return false;
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) {
277 uint32_t result;
278 nouveau_optimus_dsm(dhandle, NOUVEAU_DSM_OPTIMUS_CAPS, 0,
279 &result);
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" : "");
285 if (retval)
286 nouveau_dsm_priv.dhandle = dhandle;
288 return retval;
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;
296 int has_dsm = 0;
297 int has_optimus = 0;
298 int vga_count = 0;
299 bool guid_valid;
300 int retval;
301 bool ret = false;
303 /* lookup the MXM GUID */
304 guid_valid = mxm_wmi_supported();
306 if (guid_valid)
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) {
311 vga_count++;
313 retval = nouveau_dsm_pci_probe(pdev);
314 if (retval & NOUVEAU_DSM_HAS_MUX)
315 has_dsm |= 1;
316 if (retval & NOUVEAU_DSM_HAS_OPT)
317 has_optimus = 1;
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,
323 &buffer);
324 printk(KERN_INFO "VGA switcheroo: detected Optimus DSM method %s handle\n",
325 acpi_method_name);
326 nouveau_dsm_priv.optimus_detected = true;
327 ret = true;
328 } else if (vga_count == 2 && has_dsm && guid_valid) {
329 acpi_get_name(nouveau_dsm_priv.dhandle, ACPI_FULL_PATHNAME,
330 &buffer);
331 printk(KERN_INFO "VGA switcheroo: detected DSM switching method %s handle\n",
332 acpi_method_name);
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);
344 ret = true;
348 return ret;
351 void nouveau_register_dsm_handler(void)
353 bool r;
355 r = nouveau_dsm_detect();
356 if (!r)
357 return;
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)
365 u32 result = 0;
366 if (!nouveau_dsm_priv.optimus_detected)
367 return;
369 nouveau_optimus_dsm(nouveau_dsm_priv.dhandle, NOUVEAU_DSM_OPTIMUS_FLAGS,
370 0x3, &result);
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();
382 #else
383 void nouveau_register_dsm_handler(void) {}
384 void nouveau_unregister_dsm_handler(void) {}
385 void nouveau_switcheroo_optimus_dsm(void) {}
386 #endif
388 /* retrieve the ROM in 4k blocks */
389 static int nouveau_rom_call(acpi_handle rom_handle, uint8_t *bios,
390 int offset, int len)
392 acpi_status status;
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};
397 rom_arg.count = 2;
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));
409 return -ENODEV;
411 obj = (union acpi_object *)buffer.pointer;
412 memcpy(bios+offset, obj->buffer.pointer, len);
413 kfree(buffer.pointer);
414 return len;
417 bool nouveau_acpi_rom_supported(struct pci_dev *pdev)
419 acpi_status status;
420 acpi_handle dhandle, rom_handle;
422 dhandle = DEVICE_ACPI_HANDLE(&pdev->dev);
423 if (!dhandle)
424 return false;
426 status = acpi_get_handle(dhandle, "_ROM", &rom_handle);
427 if (ACPI_FAILURE(status))
428 return false;
430 nouveau_dsm_priv.rom_handle = rom_handle;
431 return true;
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);
439 void *
440 nouveau_acpi_edid(struct drm_device *dev, struct drm_connector *connector)
442 struct acpi_device *acpidev;
443 acpi_handle handle;
444 int type, ret;
445 void *edid;
447 switch (connector->connector_type) {
448 case DRM_MODE_CONNECTOR_LVDS:
449 case DRM_MODE_CONNECTOR_eDP:
450 type = ACPI_VIDEO_DISPLAY_LCD;
451 break;
452 default:
453 return NULL;
456 handle = DEVICE_ACPI_HANDLE(&dev->pdev->dev);
457 if (!handle)
458 return NULL;
460 ret = acpi_bus_get_device(handle, &acpidev);
461 if (ret)
462 return NULL;
464 ret = acpi_video_get_edid(acpidev, type, -1, &edid);
465 if (ret < 0)
466 return NULL;
468 return kmemdup(edid, EDID_LENGTH, GFP_KERNEL);