Merge tag 'ext4_for_linus_stable' of git://git.kernel.org/pub/scm/linux/kernel/git...
[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 static const char nouveau_dsm_muid[] = {
70         0xA0, 0xA0, 0x95, 0x9D, 0x60, 0x00, 0x48, 0x4D,
71         0xB3, 0x4D, 0x7E, 0x5F, 0xEA, 0x12, 0x9F, 0xD4,
72 };
73
74 static const char nouveau_op_dsm_muid[] = {
75         0xF8, 0xD8, 0x86, 0xA4, 0xDA, 0x0B, 0x1B, 0x47,
76         0xA7, 0x2B, 0x60, 0x42, 0xA6, 0xB5, 0xBE, 0xE0,
77 };
78
79 static int nouveau_optimus_dsm(acpi_handle handle, int func, int arg, uint32_t *result)
80 {
81         struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL };
82         struct acpi_object_list input;
83         union acpi_object params[4];
84         union acpi_object *obj;
85         int i, err;
86         char args_buff[4];
87
88         input.count = 4;
89         input.pointer = params;
90         params[0].type = ACPI_TYPE_BUFFER;
91         params[0].buffer.length = sizeof(nouveau_op_dsm_muid);
92         params[0].buffer.pointer = (char *)nouveau_op_dsm_muid;
93         params[1].type = ACPI_TYPE_INTEGER;
94         params[1].integer.value = 0x00000100;
95         params[2].type = ACPI_TYPE_INTEGER;
96         params[2].integer.value = func;
97         params[3].type = ACPI_TYPE_BUFFER;
98         params[3].buffer.length = 4;
99         /* ACPI is little endian, AABBCCDD becomes {DD,CC,BB,AA} */
100         for (i = 0; i < 4; i++)
101                 args_buff[i] = (arg >> i * 8) & 0xFF;
102         params[3].buffer.pointer = args_buff;
103
104         err = acpi_evaluate_object(handle, "_DSM", &input, &output);
105         if (err) {
106                 printk(KERN_INFO "failed to evaluate _DSM: %d\n", err);
107                 return err;
108         }
109
110         obj = (union acpi_object *)output.pointer;
111
112         if (obj->type == ACPI_TYPE_INTEGER)
113                 if (obj->integer.value == 0x80000002) {
114                         return -ENODEV;
115                 }
116
117         if (obj->type == ACPI_TYPE_BUFFER) {
118                 if (obj->buffer.length == 4 && result) {
119                         *result = 0;
120                         *result |= obj->buffer.pointer[0];
121                         *result |= (obj->buffer.pointer[1] << 8);
122                         *result |= (obj->buffer.pointer[2] << 16);
123                         *result |= (obj->buffer.pointer[3] << 24);
124                 }
125         }
126
127         kfree(output.pointer);
128         return 0;
129 }
130
131 static int nouveau_dsm(acpi_handle handle, int func, int arg, uint32_t *result)
132 {
133         struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL };
134         struct acpi_object_list input;
135         union acpi_object params[4];
136         union acpi_object *obj;
137         int err;
138
139         input.count = 4;
140         input.pointer = params;
141         params[0].type = ACPI_TYPE_BUFFER;
142         params[0].buffer.length = sizeof(nouveau_dsm_muid);
143         params[0].buffer.pointer = (char *)nouveau_dsm_muid;
144         params[1].type = ACPI_TYPE_INTEGER;
145         params[1].integer.value = 0x00000102;
146         params[2].type = ACPI_TYPE_INTEGER;
147         params[2].integer.value = func;
148         params[3].type = ACPI_TYPE_INTEGER;
149         params[3].integer.value = arg;
150
151         err = acpi_evaluate_object(handle, "_DSM", &input, &output);
152         if (err) {
153                 printk(KERN_INFO "failed to evaluate _DSM: %d\n", err);
154                 return err;
155         }
156
157         obj = (union acpi_object *)output.pointer;
158
159         if (obj->type == ACPI_TYPE_INTEGER)
160                 if (obj->integer.value == 0x80000002)
161                         return -ENODEV;
162
163         if (obj->type == ACPI_TYPE_BUFFER) {
164                 if (obj->buffer.length == 4 && result) {
165                         *result = 0;
166                         *result |= obj->buffer.pointer[0];
167                         *result |= (obj->buffer.pointer[1] << 8);
168                         *result |= (obj->buffer.pointer[2] << 16);
169                         *result |= (obj->buffer.pointer[3] << 24);
170                 }
171         }
172
173         kfree(output.pointer);
174         return 0;
175 }
176
177 /* Returns 1 if a DSM function is usable and 0 otherwise */
178 static int nouveau_test_dsm(acpi_handle test_handle,
179         int (*dsm_func)(acpi_handle, int, int, uint32_t *),
180         int sfnc)
181 {
182         u32 result = 0;
183
184         /* Function 0 returns a Buffer containing available functions. The args
185          * parameter is ignored for function 0, so just put 0 in it */
186         if (dsm_func(test_handle, 0, 0, &result))
187                 return 0;
188
189         /* ACPI Spec v4 9.14.1: if bit 0 is zero, no function is supported. If
190          * the n-th bit is enabled, function n is supported */
191         return result & 1 && result & (1 << sfnc);
192 }
193
194 static int nouveau_dsm_switch_mux(acpi_handle handle, int mux_id)
195 {
196         mxm_wmi_call_mxmx(mux_id == NOUVEAU_DSM_LED_STAMINA ? MXM_MXDS_ADAPTER_IGD : MXM_MXDS_ADAPTER_0);
197         mxm_wmi_call_mxds(mux_id == NOUVEAU_DSM_LED_STAMINA ? MXM_MXDS_ADAPTER_IGD : MXM_MXDS_ADAPTER_0);
198         return nouveau_dsm(handle, NOUVEAU_DSM_LED, mux_id, NULL);
199 }
200
201 static int nouveau_dsm_set_discrete_state(acpi_handle handle, enum vga_switcheroo_state state)
202 {
203         int arg;
204         if (state == VGA_SWITCHEROO_ON)
205                 arg = NOUVEAU_DSM_POWER_SPEED;
206         else
207                 arg = NOUVEAU_DSM_POWER_STAMINA;
208         nouveau_dsm(handle, NOUVEAU_DSM_POWER, arg, NULL);
209         return 0;
210 }
211
212 static int nouveau_dsm_switchto(enum vga_switcheroo_client_id id)
213 {
214         if (!nouveau_dsm_priv.dsm_detected)
215                 return 0;
216         if (id == VGA_SWITCHEROO_IGD)
217                 return nouveau_dsm_switch_mux(nouveau_dsm_priv.dhandle, NOUVEAU_DSM_LED_STAMINA);
218         else
219                 return nouveau_dsm_switch_mux(nouveau_dsm_priv.dhandle, NOUVEAU_DSM_LED_SPEED);
220 }
221
222 static int nouveau_dsm_power_state(enum vga_switcheroo_client_id id,
223                                    enum vga_switcheroo_state state)
224 {
225         if (id == VGA_SWITCHEROO_IGD)
226                 return 0;
227
228         /* Optimus laptops have the card already disabled in
229          * nouveau_switcheroo_set_state */
230         if (!nouveau_dsm_priv.dsm_detected)
231                 return 0;
232
233         return nouveau_dsm_set_discrete_state(nouveau_dsm_priv.dhandle, state);
234 }
235
236 static int nouveau_dsm_get_client_id(struct pci_dev *pdev)
237 {
238         /* easy option one - intel vendor ID means Integrated */
239         if (pdev->vendor == PCI_VENDOR_ID_INTEL)
240                 return VGA_SWITCHEROO_IGD;
241
242         /* is this device on Bus 0? - this may need improving */
243         if (pdev->bus->number == 0)
244                 return VGA_SWITCHEROO_IGD;
245
246         return VGA_SWITCHEROO_DIS;
247 }
248
249 static struct vga_switcheroo_handler nouveau_dsm_handler = {
250         .switchto = nouveau_dsm_switchto,
251         .power_state = nouveau_dsm_power_state,
252         .get_client_id = nouveau_dsm_get_client_id,
253 };
254
255 static int nouveau_dsm_pci_probe(struct pci_dev *pdev)
256 {
257         acpi_handle dhandle;
258         int retval = 0;
259
260         dhandle = ACPI_HANDLE(&pdev->dev);
261         if (!dhandle)
262                 return false;
263
264         if (!acpi_has_method(dhandle, "_DSM")) {
265                 nouveau_dsm_priv.other_handle = dhandle;
266                 return false;
267         }
268         if (nouveau_test_dsm(dhandle, nouveau_dsm, NOUVEAU_DSM_POWER))
269                 retval |= NOUVEAU_DSM_HAS_MUX;
270
271         if (nouveau_test_dsm(dhandle, nouveau_optimus_dsm,
272                 NOUVEAU_DSM_OPTIMUS_CAPS))
273                 retval |= NOUVEAU_DSM_HAS_OPT;
274
275         if (retval & NOUVEAU_DSM_HAS_OPT) {
276                 uint32_t result;
277                 nouveau_optimus_dsm(dhandle, NOUVEAU_DSM_OPTIMUS_CAPS, 0,
278                                     &result);
279                 dev_info(&pdev->dev, "optimus capabilities: %s, status %s%s\n",
280                          (result & OPTIMUS_ENABLED) ? "enabled" : "disabled",
281                          (result & OPTIMUS_DYNAMIC_PWR_CAP) ? "dynamic power, " : "",
282                          (result & OPTIMUS_HDA_CODEC_MASK) ? "hda bios codec supported" : "");
283         }
284         if (retval)
285                 nouveau_dsm_priv.dhandle = dhandle;
286
287         return retval;
288 }
289
290 static bool nouveau_dsm_detect(void)
291 {
292         char acpi_method_name[255] = { 0 };
293         struct acpi_buffer buffer = {sizeof(acpi_method_name), acpi_method_name};
294         struct pci_dev *pdev = NULL;
295         int has_dsm = 0;
296         int has_optimus = 0;
297         int vga_count = 0;
298         bool guid_valid;
299         int retval;
300         bool ret = false;
301
302         /* lookup the MXM GUID */
303         guid_valid = mxm_wmi_supported();
304
305         if (guid_valid)
306                 printk("MXM: GUID detected in BIOS\n");
307
308         /* now do DSM detection */
309         while ((pdev = pci_get_class(PCI_CLASS_DISPLAY_VGA << 8, pdev)) != NULL) {
310                 vga_count++;
311
312                 retval = nouveau_dsm_pci_probe(pdev);
313                 if (retval & NOUVEAU_DSM_HAS_MUX)
314                         has_dsm |= 1;
315                 if (retval & NOUVEAU_DSM_HAS_OPT)
316                         has_optimus = 1;
317         }
318
319         while ((pdev = pci_get_class(PCI_CLASS_DISPLAY_3D << 8, pdev)) != NULL) {
320                 vga_count++;
321
322                 retval = nouveau_dsm_pci_probe(pdev);
323                 if (retval & NOUVEAU_DSM_HAS_MUX)
324                         has_dsm |= 1;
325                 if (retval & NOUVEAU_DSM_HAS_OPT)
326                         has_optimus = 1;
327         }
328
329         /* find the optimus DSM or the old v1 DSM */
330         if (has_optimus == 1) {
331                 acpi_get_name(nouveau_dsm_priv.dhandle, ACPI_FULL_PATHNAME,
332                         &buffer);
333                 printk(KERN_INFO "VGA switcheroo: detected Optimus DSM method %s handle\n",
334                         acpi_method_name);
335                 nouveau_dsm_priv.optimus_detected = true;
336                 ret = true;
337         } else if (vga_count == 2 && has_dsm && guid_valid) {
338                 acpi_get_name(nouveau_dsm_priv.dhandle, ACPI_FULL_PATHNAME,
339                         &buffer);
340                 printk(KERN_INFO "VGA switcheroo: detected DSM switching method %s handle\n",
341                         acpi_method_name);
342                 nouveau_dsm_priv.dsm_detected = true;
343                 /*
344                  * On some systems hotplug events are generated for the device
345                  * being switched off when _DSM is executed.  They cause ACPI
346                  * hotplug to trigger and attempt to remove the device from
347                  * the system, which causes it to break down.  Prevent that from
348                  * happening by setting the no_hotplug flag for the involved
349                  * ACPI device objects.
350                  */
351                 acpi_bus_no_hotplug(nouveau_dsm_priv.dhandle);
352                 acpi_bus_no_hotplug(nouveau_dsm_priv.other_handle);
353                 ret = true;
354         }
355
356
357         return ret;
358 }
359
360 void nouveau_register_dsm_handler(void)
361 {
362         bool r;
363
364         r = nouveau_dsm_detect();
365         if (!r)
366                 return;
367
368         vga_switcheroo_register_handler(&nouveau_dsm_handler);
369 }
370
371 /* Must be called for Optimus models before the card can be turned off */
372 void nouveau_switcheroo_optimus_dsm(void)
373 {
374         u32 result = 0;
375         if (!nouveau_dsm_priv.optimus_detected)
376                 return;
377
378         nouveau_optimus_dsm(nouveau_dsm_priv.dhandle, NOUVEAU_DSM_OPTIMUS_FLAGS,
379                             0x3, &result);
380
381         nouveau_optimus_dsm(nouveau_dsm_priv.dhandle, NOUVEAU_DSM_OPTIMUS_CAPS,
382                 NOUVEAU_DSM_OPTIMUS_SET_POWERDOWN, &result);
383
384 }
385
386 void nouveau_unregister_dsm_handler(void)
387 {
388         if (nouveau_dsm_priv.optimus_detected || nouveau_dsm_priv.dsm_detected)
389                 vga_switcheroo_unregister_handler();
390 }
391
392 /* retrieve the ROM in 4k blocks */
393 static int nouveau_rom_call(acpi_handle rom_handle, uint8_t *bios,
394                             int offset, int len)
395 {
396         acpi_status status;
397         union acpi_object rom_arg_elements[2], *obj;
398         struct acpi_object_list rom_arg;
399         struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL};
400
401         rom_arg.count = 2;
402         rom_arg.pointer = &rom_arg_elements[0];
403
404         rom_arg_elements[0].type = ACPI_TYPE_INTEGER;
405         rom_arg_elements[0].integer.value = offset;
406
407         rom_arg_elements[1].type = ACPI_TYPE_INTEGER;
408         rom_arg_elements[1].integer.value = len;
409
410         status = acpi_evaluate_object(rom_handle, NULL, &rom_arg, &buffer);
411         if (ACPI_FAILURE(status)) {
412                 printk(KERN_INFO "failed to evaluate ROM got %s\n", acpi_format_exception(status));
413                 return -ENODEV;
414         }
415         obj = (union acpi_object *)buffer.pointer;
416         memcpy(bios+offset, obj->buffer.pointer, len);
417         kfree(buffer.pointer);
418         return len;
419 }
420
421 bool nouveau_acpi_rom_supported(struct pci_dev *pdev)
422 {
423         acpi_status status;
424         acpi_handle dhandle, rom_handle;
425
426         if (!nouveau_dsm_priv.dsm_detected && !nouveau_dsm_priv.optimus_detected)
427                 return false;
428
429         dhandle = ACPI_HANDLE(&pdev->dev);
430         if (!dhandle)
431                 return false;
432
433         status = acpi_get_handle(dhandle, "_ROM", &rom_handle);
434         if (ACPI_FAILURE(status))
435                 return false;
436
437         nouveau_dsm_priv.rom_handle = rom_handle;
438         return true;
439 }
440
441 int nouveau_acpi_get_bios_chunk(uint8_t *bios, int offset, int len)
442 {
443         return nouveau_rom_call(nouveau_dsm_priv.rom_handle, bios, offset, len);
444 }
445
446 void *
447 nouveau_acpi_edid(struct drm_device *dev, struct drm_connector *connector)
448 {
449         struct acpi_device *acpidev;
450         acpi_handle handle;
451         int type, ret;
452         void *edid;
453
454         switch (connector->connector_type) {
455         case DRM_MODE_CONNECTOR_LVDS:
456         case DRM_MODE_CONNECTOR_eDP:
457                 type = ACPI_VIDEO_DISPLAY_LCD;
458                 break;
459         default:
460                 return NULL;
461         }
462
463         handle = ACPI_HANDLE(&dev->pdev->dev);
464         if (!handle)
465                 return NULL;
466
467         ret = acpi_bus_get_device(handle, &acpidev);
468         if (ret)
469                 return NULL;
470
471         ret = acpi_video_get_edid(acpidev, type, -1, &edid);
472         if (ret < 0)
473                 return NULL;
474
475         return kmemdup(edid, EDID_LENGTH, GFP_KERNEL);
476 }