]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - drivers/gpu/drm/nouveau/nouveau_acpi.c
ACPI: Clean up inclusions of ACPI header files
[mirror_ubuntu-artful-kernel.git] / drivers / gpu / drm / nouveau / nouveau_acpi.c
CommitLineData
6ee73861
BS
1#include <linux/pci.h>
2#include <linux/acpi.h>
5a0e3ad6 3#include <linux/slab.h>
8116188f 4#include <linux/mxm-wmi.h>
6a9ee8af 5#include <linux/vga_switcheroo.h>
612a9aab 6#include <drm/drm_edid.h>
8b48463f 7#include <acpi/video.h>
c0077061
BS
8
9#include "nouveau_drm.h"
10#include "nouveau_acpi.h"
11
6ee73861
BS
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
17
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
22
5addcf0a
DA
23#define NOUVEAU_DSM_OPTIMUS_CAPS 0x1A
24#define NOUVEAU_DSM_OPTIMUS_FLAGS 0x1B
25
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)
29
30#define NOUVEAU_DSM_OPTIMUS_SET_POWERDOWN (NOUVEAU_DSM_OPTIMUS_POWERDOWN_PS3 | NOUVEAU_DSM_OPTIMUS_FLAGS_CHANGED)
31
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)
41
42#define OPTIMUS_AUDIO_CAPS_MASK (3 << 27)
43#define OPTIMUS_HDA_CODEC_MASK (2 << 27) /* hda bios control */
d099230c 44
6a9ee8af
DA
45static struct nouveau_dsm_priv {
46 bool dsm_detected;
f19467c5 47 bool optimus_detected;
6a9ee8af 48 acpi_handle dhandle;
afeb3e11 49 acpi_handle rom_handle;
6a9ee8af
DA
50} nouveau_dsm_priv;
51
c839d748
DA
52bool nouveau_is_optimus(void) {
53 return nouveau_dsm_priv.optimus_detected;
54}
55
56bool nouveau_is_v1_dsm(void) {
57 return nouveau_dsm_priv.dsm_detected;
58}
59
f19467c5
DA
60#define NOUVEAU_DSM_HAS_MUX 0x1
61#define NOUVEAU_DSM_HAS_OPT 0x2
62
6a9ee8af
DA
63static const char nouveau_dsm_muid[] = {
64 0xA0, 0xA0, 0x95, 0x9D, 0x60, 0x00, 0x48, 0x4D,
65 0xB3, 0x4D, 0x7E, 0x5F, 0xEA, 0x12, 0x9F, 0xD4,
66};
6ee73861 67
f19467c5
DA
68static const char nouveau_op_dsm_muid[] = {
69 0xF8, 0xD8, 0x86, 0xA4, 0xDA, 0x0B, 0x1B, 0x47,
70 0xA7, 0x2B, 0x60, 0x42, 0xA6, 0xB5, 0xBE, 0xE0,
71};
72
73static int nouveau_optimus_dsm(acpi_handle handle, int func, int arg, uint32_t *result)
74{
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;
d099230c
PL
79 int i, err;
80 char args_buff[4];
f19467c5
DA
81
82 input.count = 4;
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;
d099230c
PL
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;
f19467c5
DA
97
98 err = acpi_evaluate_object(handle, "_DSM", &input, &output);
99 if (err) {
100 printk(KERN_INFO "failed to evaluate _DSM: %d\n", err);
101 return err;
102 }
103
104 obj = (union acpi_object *)output.pointer;
105
106 if (obj->type == ACPI_TYPE_INTEGER)
107 if (obj->integer.value == 0x80000002) {
108 return -ENODEV;
109 }
110
111 if (obj->type == ACPI_TYPE_BUFFER) {
112 if (obj->buffer.length == 4 && result) {
113 *result = 0;
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);
118 }
119 }
120
121 kfree(output.pointer);
122 return 0;
123}
124
6e86e041 125static int nouveau_dsm(acpi_handle handle, int func, int arg, uint32_t *result)
6a9ee8af 126{
6ee73861
BS
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;
131 int err;
132
6ee73861
BS
133 input.count = 4;
134 input.pointer = params;
135 params[0].type = ACPI_TYPE_BUFFER;
6a9ee8af
DA
136 params[0].buffer.length = sizeof(nouveau_dsm_muid);
137 params[0].buffer.pointer = (char *)nouveau_dsm_muid;
6ee73861
BS
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;
144
145 err = acpi_evaluate_object(handle, "_DSM", &input, &output);
146 if (err) {
6a9ee8af 147 printk(KERN_INFO "failed to evaluate _DSM: %d\n", err);
6ee73861
BS
148 return err;
149 }
150
151 obj = (union acpi_object *)output.pointer;
152
153 if (obj->type == ACPI_TYPE_INTEGER)
154 if (obj->integer.value == 0x80000002)
155 return -ENODEV;
156
157 if (obj->type == ACPI_TYPE_BUFFER) {
158 if (obj->buffer.length == 4 && result) {
159 *result = 0;
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);
164 }
165 }
166
167 kfree(output.pointer);
168 return 0;
169}
170
9075e85f
PL
171/* Returns 1 if a DSM function is usable and 0 otherwise */
172static int nouveau_test_dsm(acpi_handle test_handle,
173 int (*dsm_func)(acpi_handle, int, int, uint32_t *),
174 int sfnc)
175{
176 u32 result = 0;
177
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))
181 return 0;
182
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);
186}
187
6a9ee8af 188static int nouveau_dsm_switch_mux(acpi_handle handle, int mux_id)
6ee73861 189{
000703f4 190 mxm_wmi_call_mxmx(mux_id == NOUVEAU_DSM_LED_STAMINA ? MXM_MXDS_ADAPTER_IGD : MXM_MXDS_ADAPTER_0);
8116188f 191 mxm_wmi_call_mxds(mux_id == NOUVEAU_DSM_LED_STAMINA ? MXM_MXDS_ADAPTER_IGD : MXM_MXDS_ADAPTER_0);
6a9ee8af
DA
192 return nouveau_dsm(handle, NOUVEAU_DSM_LED, mux_id, NULL);
193}
194
195static int nouveau_dsm_set_discrete_state(acpi_handle handle, enum vga_switcheroo_state state)
196{
197 int arg;
198 if (state == VGA_SWITCHEROO_ON)
199 arg = NOUVEAU_DSM_POWER_SPEED;
200 else
201 arg = NOUVEAU_DSM_POWER_STAMINA;
202 nouveau_dsm(handle, NOUVEAU_DSM_POWER, arg, NULL);
203 return 0;
204}
205
206static int nouveau_dsm_switchto(enum vga_switcheroo_client_id id)
207{
c839d748 208 if (!nouveau_dsm_priv.dsm_detected)
d099230c 209 return 0;
6a9ee8af 210 if (id == VGA_SWITCHEROO_IGD)
fc5ea29d 211 return nouveau_dsm_switch_mux(nouveau_dsm_priv.dhandle, NOUVEAU_DSM_LED_STAMINA);
6a9ee8af 212 else
fc5ea29d 213 return nouveau_dsm_switch_mux(nouveau_dsm_priv.dhandle, NOUVEAU_DSM_LED_SPEED);
6a9ee8af 214}
6ee73861 215
6a9ee8af
DA
216static int nouveau_dsm_power_state(enum vga_switcheroo_client_id id,
217 enum vga_switcheroo_state state)
218{
219 if (id == VGA_SWITCHEROO_IGD)
220 return 0;
221
d099230c
PL
222 /* Optimus laptops have the card already disabled in
223 * nouveau_switcheroo_set_state */
c839d748 224 if (!nouveau_dsm_priv.dsm_detected)
d099230c
PL
225 return 0;
226
fc5ea29d 227 return nouveau_dsm_set_discrete_state(nouveau_dsm_priv.dhandle, state);
6a9ee8af
DA
228}
229
6a9ee8af 230static int nouveau_dsm_get_client_id(struct pci_dev *pdev)
6ee73861 231{
d1fbd923
DA
232 /* easy option one - intel vendor ID means Integrated */
233 if (pdev->vendor == PCI_VENDOR_ID_INTEL)
6a9ee8af 234 return VGA_SWITCHEROO_IGD;
d1fbd923
DA
235
236 /* is this device on Bus 0? - this may need improving */
237 if (pdev->bus->number == 0)
238 return VGA_SWITCHEROO_IGD;
239
240 return VGA_SWITCHEROO_DIS;
6a9ee8af
DA
241}
242
243static struct vga_switcheroo_handler nouveau_dsm_handler = {
244 .switchto = nouveau_dsm_switchto,
245 .power_state = nouveau_dsm_power_state,
6a9ee8af
DA
246 .get_client_id = nouveau_dsm_get_client_id,
247};
6ee73861 248
f19467c5 249static int nouveau_dsm_pci_probe(struct pci_dev *pdev)
6a9ee8af 250{
187b5b5d 251 acpi_handle dhandle;
9075e85f 252 int retval = 0;
6a9ee8af 253
3a83f992 254 dhandle = ACPI_HANDLE(&pdev->dev);
6a9ee8af
DA
255 if (!dhandle)
256 return false;
afeb3e11 257
187b5b5d 258 if (!acpi_has_method(dhandle, "_DSM"))
6ee73861
BS
259 return false;
260
9075e85f 261 if (nouveau_test_dsm(dhandle, nouveau_dsm, NOUVEAU_DSM_POWER))
f19467c5
DA
262 retval |= NOUVEAU_DSM_HAS_MUX;
263
9075e85f 264 if (nouveau_test_dsm(dhandle, nouveau_optimus_dsm,
5addcf0a 265 NOUVEAU_DSM_OPTIMUS_CAPS))
f19467c5
DA
266 retval |= NOUVEAU_DSM_HAS_OPT;
267
5addcf0a
DA
268 if (retval & NOUVEAU_DSM_HAS_OPT) {
269 uint32_t result;
270 nouveau_optimus_dsm(dhandle, NOUVEAU_DSM_OPTIMUS_CAPS, 0,
271 &result);
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" : "");
276 }
f19467c5
DA
277 if (retval)
278 nouveau_dsm_priv.dhandle = dhandle;
6ee73861 279
f19467c5 280 return retval;
6ee73861 281}
6a9ee8af
DA
282
283static bool nouveau_dsm_detect(void)
284{
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;
288 int has_dsm = 0;
addde4ec 289 int has_optimus = 0;
6a9ee8af 290 int vga_count = 0;
8116188f 291 bool guid_valid;
f19467c5
DA
292 int retval;
293 bool ret = false;
8116188f 294
f19467c5 295 /* lookup the MXM GUID */
8116188f 296 guid_valid = mxm_wmi_supported();
8116188f 297
f19467c5
DA
298 if (guid_valid)
299 printk("MXM: GUID detected in BIOS\n");
afeb3e11 300
f19467c5 301 /* now do DSM detection */
6a9ee8af
DA
302 while ((pdev = pci_get_class(PCI_CLASS_DISPLAY_VGA << 8, pdev)) != NULL) {
303 vga_count++;
304
f19467c5 305 retval = nouveau_dsm_pci_probe(pdev);
f19467c5
DA
306 if (retval & NOUVEAU_DSM_HAS_MUX)
307 has_dsm |= 1;
308 if (retval & NOUVEAU_DSM_HAS_OPT)
309 has_optimus = 1;
6a9ee8af
DA
310 }
311
4c60fac1
EV
312 while ((pdev = pci_get_class(PCI_CLASS_DISPLAY_3D << 8, pdev)) != NULL) {
313 vga_count++;
314
315 retval = nouveau_dsm_pci_probe(pdev);
316 if (retval & NOUVEAU_DSM_HAS_MUX)
317 has_dsm |= 1;
318 if (retval & NOUVEAU_DSM_HAS_OPT)
319 has_optimus = 1;
320 }
321
c839d748
DA
322 /* find the optimus DSM or the old v1 DSM */
323 if (has_optimus == 1) {
d099230c
PL
324 acpi_get_name(nouveau_dsm_priv.dhandle, ACPI_FULL_PATHNAME,
325 &buffer);
c839d748 326 printk(KERN_INFO "VGA switcheroo: detected Optimus DSM method %s handle\n",
d099230c 327 acpi_method_name);
c839d748 328 nouveau_dsm_priv.optimus_detected = true;
f19467c5 329 ret = true;
c839d748 330 } else if (vga_count == 2 && has_dsm && guid_valid) {
d099230c
PL
331 acpi_get_name(nouveau_dsm_priv.dhandle, ACPI_FULL_PATHNAME,
332 &buffer);
c839d748 333 printk(KERN_INFO "VGA switcheroo: detected DSM switching method %s handle\n",
d099230c 334 acpi_method_name);
c839d748 335 nouveau_dsm_priv.dsm_detected = true;
d099230c
PL
336 ret = true;
337 }
f19467c5 338
c839d748 339
f19467c5 340 return ret;
6a9ee8af
DA
341}
342
343void nouveau_register_dsm_handler(void)
344{
345 bool r;
346
347 r = nouveau_dsm_detect();
348 if (!r)
349 return;
350
351 vga_switcheroo_register_handler(&nouveau_dsm_handler);
352}
353
d099230c
PL
354/* Must be called for Optimus models before the card can be turned off */
355void nouveau_switcheroo_optimus_dsm(void)
356{
357 u32 result = 0;
358 if (!nouveau_dsm_priv.optimus_detected)
359 return;
360
5addcf0a
DA
361 nouveau_optimus_dsm(nouveau_dsm_priv.dhandle, NOUVEAU_DSM_OPTIMUS_FLAGS,
362 0x3, &result);
363
364 nouveau_optimus_dsm(nouveau_dsm_priv.dhandle, NOUVEAU_DSM_OPTIMUS_CAPS,
365 NOUVEAU_DSM_OPTIMUS_SET_POWERDOWN, &result);
366
d099230c
PL
367}
368
6a9ee8af
DA
369void nouveau_unregister_dsm_handler(void)
370{
2f3787aa
AH
371 if (nouveau_dsm_priv.optimus_detected || nouveau_dsm_priv.dsm_detected)
372 vga_switcheroo_unregister_handler();
6a9ee8af 373}
afeb3e11
DA
374
375/* retrieve the ROM in 4k blocks */
376static int nouveau_rom_call(acpi_handle rom_handle, uint8_t *bios,
377 int offset, int len)
378{
379 acpi_status status;
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};
383
384 rom_arg.count = 2;
385 rom_arg.pointer = &rom_arg_elements[0];
386
387 rom_arg_elements[0].type = ACPI_TYPE_INTEGER;
388 rom_arg_elements[0].integer.value = offset;
389
390 rom_arg_elements[1].type = ACPI_TYPE_INTEGER;
391 rom_arg_elements[1].integer.value = len;
392
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));
396 return -ENODEV;
397 }
398 obj = (union acpi_object *)buffer.pointer;
399 memcpy(bios+offset, obj->buffer.pointer, len);
400 kfree(buffer.pointer);
401 return len;
402}
403
404bool nouveau_acpi_rom_supported(struct pci_dev *pdev)
405{
406 acpi_status status;
407 acpi_handle dhandle, rom_handle;
408
f19467c5 409 if (!nouveau_dsm_priv.dsm_detected && !nouveau_dsm_priv.optimus_detected)
afeb3e11
DA
410 return false;
411
3a83f992 412 dhandle = ACPI_HANDLE(&pdev->dev);
afeb3e11
DA
413 if (!dhandle)
414 return false;
415
416 status = acpi_get_handle(dhandle, "_ROM", &rom_handle);
417 if (ACPI_FAILURE(status))
418 return false;
419
420 nouveau_dsm_priv.rom_handle = rom_handle;
421 return true;
422}
423
424int nouveau_acpi_get_bios_chunk(uint8_t *bios, int offset, int len)
425{
426 return nouveau_rom_call(nouveau_dsm_priv.rom_handle, bios, offset, len);
427}
a6ed76d7 428
c0077061 429void *
a6ed76d7
BS
430nouveau_acpi_edid(struct drm_device *dev, struct drm_connector *connector)
431{
a6ed76d7
BS
432 struct acpi_device *acpidev;
433 acpi_handle handle;
434 int type, ret;
435 void *edid;
436
437 switch (connector->connector_type) {
438 case DRM_MODE_CONNECTOR_LVDS:
439 case DRM_MODE_CONNECTOR_eDP:
440 type = ACPI_VIDEO_DISPLAY_LCD;
441 break;
442 default:
c0077061 443 return NULL;
a6ed76d7
BS
444 }
445
3a83f992 446 handle = ACPI_HANDLE(&dev->pdev->dev);
a6ed76d7 447 if (!handle)
c0077061 448 return NULL;
a6ed76d7
BS
449
450 ret = acpi_bus_get_device(handle, &acpidev);
451 if (ret)
c0077061 452 return NULL;
a6ed76d7
BS
453
454 ret = acpi_video_get_edid(acpidev, type, -1, &edid);
455 if (ret < 0)
c0077061 456 return NULL;
a6ed76d7 457
c0077061 458 return kmemdup(edid, EDID_LENGTH, GFP_KERNEL);
a6ed76d7 459}