]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blob - drivers/gpu/drm/nouveau/nouveau_acpi.c
ACPI: Clean up inclusions of ACPI header files
[mirror_ubuntu-zesty-kernel.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 <linux/mxm-wmi.h>
5 #include <linux/vga_switcheroo.h>
6 #include <drm/drm_edid.h>
7 #include <acpi/video.h>
8
9 #include "nouveau_drm.h"
10 #include "nouveau_acpi.h"
11
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
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 */
44
45 static struct nouveau_dsm_priv {
46 bool dsm_detected;
47 bool optimus_detected;
48 acpi_handle dhandle;
49 acpi_handle rom_handle;
50 } nouveau_dsm_priv;
51
52 bool nouveau_is_optimus(void) {
53 return nouveau_dsm_priv.optimus_detected;
54 }
55
56 bool nouveau_is_v1_dsm(void) {
57 return nouveau_dsm_priv.dsm_detected;
58 }
59
60 #define NOUVEAU_DSM_HAS_MUX 0x1
61 #define NOUVEAU_DSM_HAS_OPT 0x2
62
63 static const char nouveau_dsm_muid[] = {
64 0xA0, 0xA0, 0x95, 0x9D, 0x60, 0x00, 0x48, 0x4D,
65 0xB3, 0x4D, 0x7E, 0x5F, 0xEA, 0x12, 0x9F, 0xD4,
66 };
67
68 static 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
73 static 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;
79 int i, err;
80 char args_buff[4];
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;
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;
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
125 static int nouveau_dsm(acpi_handle handle, int func, int arg, uint32_t *result)
126 {
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
133 input.count = 4;
134 input.pointer = params;
135 params[0].type = ACPI_TYPE_BUFFER;
136 params[0].buffer.length = sizeof(nouveau_dsm_muid);
137 params[0].buffer.pointer = (char *)nouveau_dsm_muid;
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) {
147 printk(KERN_INFO "failed to evaluate _DSM: %d\n", err);
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
171 /* Returns 1 if a DSM function is usable and 0 otherwise */
172 static 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
188 static int nouveau_dsm_switch_mux(acpi_handle handle, int mux_id)
189 {
190 mxm_wmi_call_mxmx(mux_id == NOUVEAU_DSM_LED_STAMINA ? MXM_MXDS_ADAPTER_IGD : MXM_MXDS_ADAPTER_0);
191 mxm_wmi_call_mxds(mux_id == NOUVEAU_DSM_LED_STAMINA ? MXM_MXDS_ADAPTER_IGD : MXM_MXDS_ADAPTER_0);
192 return nouveau_dsm(handle, NOUVEAU_DSM_LED, mux_id, NULL);
193 }
194
195 static 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
206 static int nouveau_dsm_switchto(enum vga_switcheroo_client_id id)
207 {
208 if (!nouveau_dsm_priv.dsm_detected)
209 return 0;
210 if (id == VGA_SWITCHEROO_IGD)
211 return nouveau_dsm_switch_mux(nouveau_dsm_priv.dhandle, NOUVEAU_DSM_LED_STAMINA);
212 else
213 return nouveau_dsm_switch_mux(nouveau_dsm_priv.dhandle, NOUVEAU_DSM_LED_SPEED);
214 }
215
216 static 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
222 /* Optimus laptops have the card already disabled in
223 * nouveau_switcheroo_set_state */
224 if (!nouveau_dsm_priv.dsm_detected)
225 return 0;
226
227 return nouveau_dsm_set_discrete_state(nouveau_dsm_priv.dhandle, state);
228 }
229
230 static int nouveau_dsm_get_client_id(struct pci_dev *pdev)
231 {
232 /* easy option one - intel vendor ID means Integrated */
233 if (pdev->vendor == PCI_VENDOR_ID_INTEL)
234 return VGA_SWITCHEROO_IGD;
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;
241 }
242
243 static struct vga_switcheroo_handler nouveau_dsm_handler = {
244 .switchto = nouveau_dsm_switchto,
245 .power_state = nouveau_dsm_power_state,
246 .get_client_id = nouveau_dsm_get_client_id,
247 };
248
249 static int nouveau_dsm_pci_probe(struct pci_dev *pdev)
250 {
251 acpi_handle dhandle;
252 int retval = 0;
253
254 dhandle = ACPI_HANDLE(&pdev->dev);
255 if (!dhandle)
256 return false;
257
258 if (!acpi_has_method(dhandle, "_DSM"))
259 return false;
260
261 if (nouveau_test_dsm(dhandle, nouveau_dsm, NOUVEAU_DSM_POWER))
262 retval |= NOUVEAU_DSM_HAS_MUX;
263
264 if (nouveau_test_dsm(dhandle, nouveau_optimus_dsm,
265 NOUVEAU_DSM_OPTIMUS_CAPS))
266 retval |= NOUVEAU_DSM_HAS_OPT;
267
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 }
277 if (retval)
278 nouveau_dsm_priv.dhandle = dhandle;
279
280 return retval;
281 }
282
283 static 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;
289 int has_optimus = 0;
290 int vga_count = 0;
291 bool guid_valid;
292 int retval;
293 bool ret = false;
294
295 /* lookup the MXM GUID */
296 guid_valid = mxm_wmi_supported();
297
298 if (guid_valid)
299 printk("MXM: GUID detected in BIOS\n");
300
301 /* now do DSM detection */
302 while ((pdev = pci_get_class(PCI_CLASS_DISPLAY_VGA << 8, pdev)) != NULL) {
303 vga_count++;
304
305 retval = nouveau_dsm_pci_probe(pdev);
306 if (retval & NOUVEAU_DSM_HAS_MUX)
307 has_dsm |= 1;
308 if (retval & NOUVEAU_DSM_HAS_OPT)
309 has_optimus = 1;
310 }
311
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
322 /* find the optimus DSM or the old v1 DSM */
323 if (has_optimus == 1) {
324 acpi_get_name(nouveau_dsm_priv.dhandle, ACPI_FULL_PATHNAME,
325 &buffer);
326 printk(KERN_INFO "VGA switcheroo: detected Optimus DSM method %s handle\n",
327 acpi_method_name);
328 nouveau_dsm_priv.optimus_detected = true;
329 ret = true;
330 } else if (vga_count == 2 && has_dsm && guid_valid) {
331 acpi_get_name(nouveau_dsm_priv.dhandle, ACPI_FULL_PATHNAME,
332 &buffer);
333 printk(KERN_INFO "VGA switcheroo: detected DSM switching method %s handle\n",
334 acpi_method_name);
335 nouveau_dsm_priv.dsm_detected = true;
336 ret = true;
337 }
338
339
340 return ret;
341 }
342
343 void 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
354 /* Must be called for Optimus models before the card can be turned off */
355 void nouveau_switcheroo_optimus_dsm(void)
356 {
357 u32 result = 0;
358 if (!nouveau_dsm_priv.optimus_detected)
359 return;
360
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
367 }
368
369 void nouveau_unregister_dsm_handler(void)
370 {
371 if (nouveau_dsm_priv.optimus_detected || nouveau_dsm_priv.dsm_detected)
372 vga_switcheroo_unregister_handler();
373 }
374
375 /* retrieve the ROM in 4k blocks */
376 static 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
404 bool nouveau_acpi_rom_supported(struct pci_dev *pdev)
405 {
406 acpi_status status;
407 acpi_handle dhandle, rom_handle;
408
409 if (!nouveau_dsm_priv.dsm_detected && !nouveau_dsm_priv.optimus_detected)
410 return false;
411
412 dhandle = ACPI_HANDLE(&pdev->dev);
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
424 int 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 }
428
429 void *
430 nouveau_acpi_edid(struct drm_device *dev, struct drm_connector *connector)
431 {
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:
443 return NULL;
444 }
445
446 handle = ACPI_HANDLE(&dev->pdev->dev);
447 if (!handle)
448 return NULL;
449
450 ret = acpi_bus_get_device(handle, &acpidev);
451 if (ret)
452 return NULL;
453
454 ret = acpi_video_get_edid(acpidev, type, -1, &edid);
455 if (ret < 0)
456 return NULL;
457
458 return kmemdup(edid, EDID_LENGTH, GFP_KERNEL);
459 }