Merge tag 'drm-intel-fixes-2014-01-28' of git://people.freedesktop.org/~danvet/drm...
[cascardo/linux.git] / drivers / gpu / drm / nouveau / nouveau_acpi.c
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>
9
10 #include <linux/vga_switcheroo.h>
11
12 #include <drm/drm_edid.h>
13
14 #include "nouveau_drm.h"
15 #include "nouveau_acpi.h"
16
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
22
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
27
28 #define NOUVEAU_DSM_OPTIMUS_CAPS 0x1A
29 #define NOUVEAU_DSM_OPTIMUS_FLAGS 0x1B
30
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)
34
35 #define NOUVEAU_DSM_OPTIMUS_SET_POWERDOWN (NOUVEAU_DSM_OPTIMUS_POWERDOWN_PS3 | NOUVEAU_DSM_OPTIMUS_FLAGS_CHANGED)
36
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)
46
47 #define OPTIMUS_AUDIO_CAPS_MASK (3 << 27)
48 #define OPTIMUS_HDA_CODEC_MASK (2 << 27) /* hda bios control */
49
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;
57
58 bool nouveau_is_optimus(void) {
59         return nouveau_dsm_priv.optimus_detected;
60 }
61
62 bool nouveau_is_v1_dsm(void) {
63         return nouveau_dsm_priv.dsm_detected;
64 }
65
66 #define NOUVEAU_DSM_HAS_MUX 0x1
67 #define NOUVEAU_DSM_HAS_OPT 0x2
68
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,
73 };
74
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,
78 };
79
80 static int nouveau_optimus_dsm(acpi_handle handle, int func, int arg, uint32_t *result)
81 {
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];
88
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;
104
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;
109         }
110
111         obj = (union acpi_object *)output.pointer;
112
113         if (obj->type == ACPI_TYPE_INTEGER)
114                 if (obj->integer.value == 0x80000002) {
115                         return -ENODEV;
116                 }
117
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);
125                 }
126         }
127
128         kfree(output.pointer);
129         return 0;
130 }
131
132 static int nouveau_dsm(acpi_handle handle, int func, int arg, uint32_t *result)
133 {
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;
139
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;
151
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;
156         }
157
158         obj = (union acpi_object *)output.pointer;
159
160         if (obj->type == ACPI_TYPE_INTEGER)
161                 if (obj->integer.value == 0x80000002)
162                         return -ENODEV;
163
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);
171                 }
172         }
173
174         kfree(output.pointer);
175         return 0;
176 }
177
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)
182 {
183         u32 result = 0;
184
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;
189
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);
193 }
194
195 static int nouveau_dsm_switch_mux(acpi_handle handle, int mux_id)
196 {
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);
200 }
201
202 static int nouveau_dsm_set_discrete_state(acpi_handle handle, enum vga_switcheroo_state state)
203 {
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;
211 }
212
213 static int nouveau_dsm_switchto(enum vga_switcheroo_client_id id)
214 {
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);
221 }
222
223 static int nouveau_dsm_power_state(enum vga_switcheroo_client_id id,
224                                    enum vga_switcheroo_state state)
225 {
226         if (id == VGA_SWITCHEROO_IGD)
227                 return 0;
228
229         /* Optimus laptops have the card already disabled in
230          * nouveau_switcheroo_set_state */
231         if (!nouveau_dsm_priv.dsm_detected)
232                 return 0;
233
234         return nouveau_dsm_set_discrete_state(nouveau_dsm_priv.dhandle, state);
235 }
236
237 static int nouveau_dsm_get_client_id(struct pci_dev *pdev)
238 {
239         /* easy option one - intel vendor ID means Integrated */
240         if (pdev->vendor == PCI_VENDOR_ID_INTEL)
241                 return VGA_SWITCHEROO_IGD;
242
243         /* is this device on Bus 0? - this may need improving */
244         if (pdev->bus->number == 0)
245                 return VGA_SWITCHEROO_IGD;
246
247         return VGA_SWITCHEROO_DIS;
248 }
249
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,
254 };
255
256 static int nouveau_dsm_pci_probe(struct pci_dev *pdev)
257 {
258         acpi_handle dhandle;
259         int retval = 0;
260
261         dhandle = ACPI_HANDLE(&pdev->dev);
262         if (!dhandle)
263                 return false;
264
265         if (!acpi_has_method(dhandle, "_DSM")) {
266                 nouveau_dsm_priv.other_handle = dhandle;
267                 return false;
268         }
269         if (nouveau_test_dsm(dhandle, nouveau_dsm, NOUVEAU_DSM_POWER))
270                 retval |= NOUVEAU_DSM_HAS_MUX;
271
272         if (nouveau_test_dsm(dhandle, nouveau_optimus_dsm,
273                 NOUVEAU_DSM_OPTIMUS_CAPS))
274                 retval |= NOUVEAU_DSM_HAS_OPT;
275
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" : "");
284         }
285         if (retval)
286                 nouveau_dsm_priv.dhandle = dhandle;
287
288         return retval;
289 }
290
291 static bool nouveau_dsm_detect(void)
292 {
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;
302
303         /* lookup the MXM GUID */
304         guid_valid = mxm_wmi_supported();
305
306         if (guid_valid)
307                 printk("MXM: GUID detected in BIOS\n");
308
309         /* now do DSM detection */
310         while ((pdev = pci_get_class(PCI_CLASS_DISPLAY_VGA << 8, pdev)) != NULL) {
311                 vga_count++;
312
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;
318         }
319
320         while ((pdev = pci_get_class(PCI_CLASS_DISPLAY_3D << 8, pdev)) != NULL) {
321                 vga_count++;
322
323                 retval = nouveau_dsm_pci_probe(pdev);
324                 if (retval & NOUVEAU_DSM_HAS_MUX)
325                         has_dsm |= 1;
326                 if (retval & NOUVEAU_DSM_HAS_OPT)
327                         has_optimus = 1;
328         }
329
330         /* find the optimus DSM or the old v1 DSM */
331         if (has_optimus == 1) {
332                 acpi_get_name(nouveau_dsm_priv.dhandle, ACPI_FULL_PATHNAME,
333                         &buffer);
334                 printk(KERN_INFO "VGA switcheroo: detected Optimus DSM method %s handle\n",
335                         acpi_method_name);
336                 nouveau_dsm_priv.optimus_detected = true;
337                 ret = true;
338         } else if (vga_count == 2 && has_dsm && guid_valid) {
339                 acpi_get_name(nouveau_dsm_priv.dhandle, ACPI_FULL_PATHNAME,
340                         &buffer);
341                 printk(KERN_INFO "VGA switcheroo: detected DSM switching method %s handle\n",
342                         acpi_method_name);
343                 nouveau_dsm_priv.dsm_detected = true;
344                 /*
345                  * On some systems hotplug events are generated for the device
346                  * being switched off when _DSM is executed.  They cause ACPI
347                  * hotplug to trigger and attempt to remove the device from
348                  * the system, which causes it to break down.  Prevent that from
349                  * happening by setting the no_hotplug flag for the involved
350                  * ACPI device objects.
351                  */
352                 acpi_bus_no_hotplug(nouveau_dsm_priv.dhandle);
353                 acpi_bus_no_hotplug(nouveau_dsm_priv.other_handle);
354                 ret = true;
355         }
356
357
358         return ret;
359 }
360
361 void nouveau_register_dsm_handler(void)
362 {
363         bool r;
364
365         r = nouveau_dsm_detect();
366         if (!r)
367                 return;
368
369         vga_switcheroo_register_handler(&nouveau_dsm_handler);
370 }
371
372 /* Must be called for Optimus models before the card can be turned off */
373 void nouveau_switcheroo_optimus_dsm(void)
374 {
375         u32 result = 0;
376         if (!nouveau_dsm_priv.optimus_detected)
377                 return;
378
379         nouveau_optimus_dsm(nouveau_dsm_priv.dhandle, NOUVEAU_DSM_OPTIMUS_FLAGS,
380                             0x3, &result);
381
382         nouveau_optimus_dsm(nouveau_dsm_priv.dhandle, NOUVEAU_DSM_OPTIMUS_CAPS,
383                 NOUVEAU_DSM_OPTIMUS_SET_POWERDOWN, &result);
384
385 }
386
387 void nouveau_unregister_dsm_handler(void)
388 {
389         if (nouveau_dsm_priv.optimus_detected || nouveau_dsm_priv.dsm_detected)
390                 vga_switcheroo_unregister_handler();
391 }
392 #else
393 void nouveau_register_dsm_handler(void) {}
394 void nouveau_unregister_dsm_handler(void) {}
395 void nouveau_switcheroo_optimus_dsm(void) {}
396 #endif
397
398 /* retrieve the ROM in 4k blocks */
399 static int nouveau_rom_call(acpi_handle rom_handle, uint8_t *bios,
400                             int offset, int len)
401 {
402         acpi_status status;
403         union acpi_object rom_arg_elements[2], *obj;
404         struct acpi_object_list rom_arg;
405         struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL};
406
407         rom_arg.count = 2;
408         rom_arg.pointer = &rom_arg_elements[0];
409
410         rom_arg_elements[0].type = ACPI_TYPE_INTEGER;
411         rom_arg_elements[0].integer.value = offset;
412
413         rom_arg_elements[1].type = ACPI_TYPE_INTEGER;
414         rom_arg_elements[1].integer.value = len;
415
416         status = acpi_evaluate_object(rom_handle, NULL, &rom_arg, &buffer);
417         if (ACPI_FAILURE(status)) {
418                 printk(KERN_INFO "failed to evaluate ROM got %s\n", acpi_format_exception(status));
419                 return -ENODEV;
420         }
421         obj = (union acpi_object *)buffer.pointer;
422         memcpy(bios+offset, obj->buffer.pointer, len);
423         kfree(buffer.pointer);
424         return len;
425 }
426
427 bool nouveau_acpi_rom_supported(struct pci_dev *pdev)
428 {
429         acpi_status status;
430         acpi_handle dhandle, rom_handle;
431
432         if (!nouveau_dsm_priv.dsm_detected && !nouveau_dsm_priv.optimus_detected)
433                 return false;
434
435         dhandle = ACPI_HANDLE(&pdev->dev);
436         if (!dhandle)
437                 return false;
438
439         status = acpi_get_handle(dhandle, "_ROM", &rom_handle);
440         if (ACPI_FAILURE(status))
441                 return false;
442
443         nouveau_dsm_priv.rom_handle = rom_handle;
444         return true;
445 }
446
447 int nouveau_acpi_get_bios_chunk(uint8_t *bios, int offset, int len)
448 {
449         return nouveau_rom_call(nouveau_dsm_priv.rom_handle, bios, offset, len);
450 }
451
452 void *
453 nouveau_acpi_edid(struct drm_device *dev, struct drm_connector *connector)
454 {
455         struct acpi_device *acpidev;
456         acpi_handle handle;
457         int type, ret;
458         void *edid;
459
460         switch (connector->connector_type) {
461         case DRM_MODE_CONNECTOR_LVDS:
462         case DRM_MODE_CONNECTOR_eDP:
463                 type = ACPI_VIDEO_DISPLAY_LCD;
464                 break;
465         default:
466                 return NULL;
467         }
468
469         handle = ACPI_HANDLE(&dev->pdev->dev);
470         if (!handle)
471                 return NULL;
472
473         ret = acpi_bus_get_device(handle, &acpidev);
474         if (ret)
475                 return NULL;
476
477         ret = acpi_video_get_edid(acpidev, type, -1, &edid);
478         if (ret < 0)
479                 return NULL;
480
481         return kmemdup(edid, EDID_LENGTH, GFP_KERNEL);
482 }