]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blame - drivers/gpu/drm/amd/amdgpu/amdgpu_atpx_handler.c
Merge remote-tracking branches 'asoc/topic/sgtl5000', 'asoc/topic/simple', 'asoc...
[mirror_ubuntu-zesty-kernel.git] / drivers / gpu / drm / amd / amdgpu / amdgpu_atpx_handler.c
CommitLineData
d38ceaf9
AD
1/*
2 * Copyright (c) 2010 Red Hat Inc.
3 * Author : Dave Airlie <airlied@redhat.com>
4 *
5 * Licensed under GPLv2
6 *
7 * ATPX support for both Intel/ATI
8 */
9#include <linux/vga_switcheroo.h>
10#include <linux/slab.h>
11#include <linux/acpi.h>
12#include <linux/pci.h>
f81eb1a3 13#include <linux/delay.h>
d38ceaf9 14
66dc0ddd 15#include "amd_acpi.h"
d38ceaf9
AD
16
17struct amdgpu_atpx_functions {
18 bool px_params;
19 bool power_cntl;
20 bool disp_mux_cntl;
21 bool i2c_mux_cntl;
22 bool switch_start;
23 bool switch_end;
24 bool disp_connectors_mapping;
25 bool disp_detetion_ports;
26};
27
28struct amdgpu_atpx {
29 acpi_handle handle;
30 struct amdgpu_atpx_functions functions;
2f5af82e 31 bool is_hybrid;
d38ceaf9
AD
32};
33
34static struct amdgpu_atpx_priv {
35 bool atpx_detected;
36 /* handle for device - and atpx */
37 acpi_handle dhandle;
38 acpi_handle other_handle;
39 struct amdgpu_atpx atpx;
40} amdgpu_atpx_priv;
41
42struct atpx_verify_interface {
43 u16 size; /* structure size in bytes (includes size field) */
44 u16 version; /* version */
45 u32 function_bits; /* supported functions bit vector */
46} __packed;
47
48struct atpx_px_params {
49 u16 size; /* structure size in bytes (includes size field) */
50 u32 valid_flags; /* which flags are valid */
51 u32 flags; /* flags */
52} __packed;
53
54struct atpx_power_control {
55 u16 size;
56 u8 dgpu_state;
57} __packed;
58
59struct atpx_mux {
60 u16 size;
61 u16 mux;
62} __packed;
63
64bool amdgpu_has_atpx(void) {
65 return amdgpu_atpx_priv.atpx_detected;
66}
67
a78fe133
AD
68bool amdgpu_has_atpx_dgpu_power_cntl(void) {
69 return amdgpu_atpx_priv.atpx.functions.power_cntl;
70}
71
2f5af82e
AD
72bool amdgpu_is_atpx_hybrid(void) {
73 return amdgpu_atpx_priv.atpx.is_hybrid;
74}
75
d38ceaf9
AD
76/**
77 * amdgpu_atpx_call - call an ATPX method
78 *
79 * @handle: acpi handle
80 * @function: the ATPX function to execute
81 * @params: ATPX function params
82 *
83 * Executes the requested ATPX function (all asics).
84 * Returns a pointer to the acpi output buffer.
85 */
86static union acpi_object *amdgpu_atpx_call(acpi_handle handle, int function,
87 struct acpi_buffer *params)
88{
89 acpi_status status;
90 union acpi_object atpx_arg_elements[2];
91 struct acpi_object_list atpx_arg;
92 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
93
94 atpx_arg.count = 2;
95 atpx_arg.pointer = &atpx_arg_elements[0];
96
97 atpx_arg_elements[0].type = ACPI_TYPE_INTEGER;
98 atpx_arg_elements[0].integer.value = function;
99
100 if (params) {
101 atpx_arg_elements[1].type = ACPI_TYPE_BUFFER;
102 atpx_arg_elements[1].buffer.length = params->length;
103 atpx_arg_elements[1].buffer.pointer = params->pointer;
104 } else {
105 /* We need a second fake parameter */
106 atpx_arg_elements[1].type = ACPI_TYPE_INTEGER;
107 atpx_arg_elements[1].integer.value = 0;
108 }
109
110 status = acpi_evaluate_object(handle, NULL, &atpx_arg, &buffer);
111
112 /* Fail only if calling the method fails and ATPX is supported */
113 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
114 printk("failed to evaluate ATPX got %s\n",
115 acpi_format_exception(status));
116 kfree(buffer.pointer);
117 return NULL;
118 }
119
120 return buffer.pointer;
121}
122
123/**
124 * amdgpu_atpx_parse_functions - parse supported functions
125 *
126 * @f: supported functions struct
127 * @mask: supported functions mask from ATPX
128 *
129 * Use the supported functions mask from ATPX function
130 * ATPX_FUNCTION_VERIFY_INTERFACE to determine what functions
131 * are supported (all asics).
132 */
133static void amdgpu_atpx_parse_functions(struct amdgpu_atpx_functions *f, u32 mask)
134{
135 f->px_params = mask & ATPX_GET_PX_PARAMETERS_SUPPORTED;
136 f->power_cntl = mask & ATPX_POWER_CONTROL_SUPPORTED;
137 f->disp_mux_cntl = mask & ATPX_DISPLAY_MUX_CONTROL_SUPPORTED;
138 f->i2c_mux_cntl = mask & ATPX_I2C_MUX_CONTROL_SUPPORTED;
139 f->switch_start = mask & ATPX_GRAPHICS_DEVICE_SWITCH_START_NOTIFICATION_SUPPORTED;
140 f->switch_end = mask & ATPX_GRAPHICS_DEVICE_SWITCH_END_NOTIFICATION_SUPPORTED;
141 f->disp_connectors_mapping = mask & ATPX_GET_DISPLAY_CONNECTORS_MAPPING_SUPPORTED;
142 f->disp_detetion_ports = mask & ATPX_GET_DISPLAY_DETECTION_PORTS_SUPPORTED;
143}
144
145/**
146 * amdgpu_atpx_validate_functions - validate ATPX functions
147 *
148 * @atpx: amdgpu atpx struct
149 *
150 * Validate that required functions are enabled (all asics).
151 * returns 0 on success, error on failure.
152 */
153static int amdgpu_atpx_validate(struct amdgpu_atpx *atpx)
154{
8d45f80e 155 u32 valid_bits = 0;
e9bef455 156
d38ceaf9
AD
157 if (atpx->functions.px_params) {
158 union acpi_object *info;
159 struct atpx_px_params output;
160 size_t size;
d38ceaf9
AD
161
162 info = amdgpu_atpx_call(atpx->handle, ATPX_FUNCTION_GET_PX_PARAMETERS, NULL);
163 if (!info)
164 return -EIO;
165
166 memset(&output, 0, sizeof(output));
167
168 size = *(u16 *) info->buffer.pointer;
169 if (size < 10) {
170 printk("ATPX buffer is too small: %zu\n", size);
171 kfree(info);
172 return -EINVAL;
173 }
174 size = min(sizeof(output), size);
175
176 memcpy(&output, info->buffer.pointer, size);
177
178 valid_bits = output.flags & output.valid_flags;
5c614792 179
d38ceaf9
AD
180 kfree(info);
181 }
8d45f80e
AD
182
183 /* if separate mux flag is set, mux controls are required */
184 if (valid_bits & ATPX_SEPARATE_MUX_FOR_I2C) {
185 atpx->functions.i2c_mux_cntl = true;
186 atpx->functions.disp_mux_cntl = true;
187 }
188 /* if any outputs are muxed, mux controls are required */
189 if (valid_bits & (ATPX_CRT1_RGB_SIGNAL_MUXED |
190 ATPX_TV_SIGNAL_MUXED |
191 ATPX_DFP_SIGNAL_MUXED))
192 atpx->functions.disp_mux_cntl = true;
193
194
195 /* some bioses set these bits rather than flagging power_cntl as supported */
196 if (valid_bits & (ATPX_DYNAMIC_PX_SUPPORTED |
197 ATPX_DYNAMIC_DGPU_POWER_OFF_SUPPORTED))
198 atpx->functions.power_cntl = true;
199
2f5af82e 200 atpx->is_hybrid = false;
8d45f80e 201 if (valid_bits & ATPX_MS_HYBRID_GFX_SUPPORTED) {
2f5af82e 202 printk("ATPX Hybrid Graphics\n");
8d45f80e 203 atpx->functions.power_cntl = false;
2f5af82e 204 atpx->is_hybrid = true;
8d45f80e
AD
205 }
206
d38ceaf9
AD
207 return 0;
208}
209
210/**
211 * amdgpu_atpx_verify_interface - verify ATPX
212 *
213 * @atpx: amdgpu atpx struct
214 *
215 * Execute the ATPX_FUNCTION_VERIFY_INTERFACE ATPX function
216 * to initialize ATPX and determine what features are supported
217 * (all asics).
218 * returns 0 on success, error on failure.
219 */
220static int amdgpu_atpx_verify_interface(struct amdgpu_atpx *atpx)
221{
222 union acpi_object *info;
223 struct atpx_verify_interface output;
224 size_t size;
225 int err = 0;
226
227 info = amdgpu_atpx_call(atpx->handle, ATPX_FUNCTION_VERIFY_INTERFACE, NULL);
228 if (!info)
229 return -EIO;
230
231 memset(&output, 0, sizeof(output));
232
233 size = *(u16 *) info->buffer.pointer;
234 if (size < 8) {
235 printk("ATPX buffer is too small: %zu\n", size);
236 err = -EINVAL;
237 goto out;
238 }
239 size = min(sizeof(output), size);
240
241 memcpy(&output, info->buffer.pointer, size);
242
243 /* TODO: check version? */
244 printk("ATPX version %u, functions 0x%08x\n",
245 output.version, output.function_bits);
246
247 amdgpu_atpx_parse_functions(&atpx->functions, output.function_bits);
248
249out:
250 kfree(info);
251 return err;
252}
253
254/**
255 * amdgpu_atpx_set_discrete_state - power up/down discrete GPU
256 *
257 * @atpx: atpx info struct
258 * @state: discrete GPU state (0 = power down, 1 = power up)
259 *
260 * Execute the ATPX_FUNCTION_POWER_CONTROL ATPX function to
261 * power down/up the discrete GPU (all asics).
262 * Returns 0 on success, error on failure.
263 */
264static int amdgpu_atpx_set_discrete_state(struct amdgpu_atpx *atpx, u8 state)
265{
266 struct acpi_buffer params;
267 union acpi_object *info;
268 struct atpx_power_control input;
269
270 if (atpx->functions.power_cntl) {
271 input.size = 3;
272 input.dgpu_state = state;
273 params.length = input.size;
274 params.pointer = &input;
275 info = amdgpu_atpx_call(atpx->handle,
276 ATPX_FUNCTION_POWER_CONTROL,
277 &params);
278 if (!info)
279 return -EIO;
280 kfree(info);
f81eb1a3
AD
281
282 /* 200ms delay is required after off */
283 if (state == 0)
284 msleep(200);
d38ceaf9
AD
285 }
286 return 0;
287}
288
289/**
290 * amdgpu_atpx_switch_disp_mux - switch display mux
291 *
292 * @atpx: atpx info struct
293 * @mux_id: mux state (0 = integrated GPU, 1 = discrete GPU)
294 *
295 * Execute the ATPX_FUNCTION_DISPLAY_MUX_CONTROL ATPX function to
296 * switch the display mux between the discrete GPU and integrated GPU
297 * (all asics).
298 * Returns 0 on success, error on failure.
299 */
300static int amdgpu_atpx_switch_disp_mux(struct amdgpu_atpx *atpx, u16 mux_id)
301{
302 struct acpi_buffer params;
303 union acpi_object *info;
304 struct atpx_mux input;
305
306 if (atpx->functions.disp_mux_cntl) {
307 input.size = 4;
308 input.mux = mux_id;
309 params.length = input.size;
310 params.pointer = &input;
311 info = amdgpu_atpx_call(atpx->handle,
312 ATPX_FUNCTION_DISPLAY_MUX_CONTROL,
313 &params);
314 if (!info)
315 return -EIO;
316 kfree(info);
317 }
318 return 0;
319}
320
321/**
322 * amdgpu_atpx_switch_i2c_mux - switch i2c/hpd mux
323 *
324 * @atpx: atpx info struct
325 * @mux_id: mux state (0 = integrated GPU, 1 = discrete GPU)
326 *
327 * Execute the ATPX_FUNCTION_I2C_MUX_CONTROL ATPX function to
328 * switch the i2c/hpd mux between the discrete GPU and integrated GPU
329 * (all asics).
330 * Returns 0 on success, error on failure.
331 */
332static int amdgpu_atpx_switch_i2c_mux(struct amdgpu_atpx *atpx, u16 mux_id)
333{
334 struct acpi_buffer params;
335 union acpi_object *info;
336 struct atpx_mux input;
337
338 if (atpx->functions.i2c_mux_cntl) {
339 input.size = 4;
340 input.mux = mux_id;
341 params.length = input.size;
342 params.pointer = &input;
343 info = amdgpu_atpx_call(atpx->handle,
344 ATPX_FUNCTION_I2C_MUX_CONTROL,
345 &params);
346 if (!info)
347 return -EIO;
348 kfree(info);
349 }
350 return 0;
351}
352
353/**
354 * amdgpu_atpx_switch_start - notify the sbios of a GPU switch
355 *
356 * @atpx: atpx info struct
357 * @mux_id: mux state (0 = integrated GPU, 1 = discrete GPU)
358 *
359 * Execute the ATPX_FUNCTION_GRAPHICS_DEVICE_SWITCH_START_NOTIFICATION ATPX
360 * function to notify the sbios that a switch between the discrete GPU and
361 * integrated GPU has begun (all asics).
362 * Returns 0 on success, error on failure.
363 */
364static int amdgpu_atpx_switch_start(struct amdgpu_atpx *atpx, u16 mux_id)
365{
366 struct acpi_buffer params;
367 union acpi_object *info;
368 struct atpx_mux input;
369
370 if (atpx->functions.switch_start) {
371 input.size = 4;
372 input.mux = mux_id;
373 params.length = input.size;
374 params.pointer = &input;
375 info = amdgpu_atpx_call(atpx->handle,
376 ATPX_FUNCTION_GRAPHICS_DEVICE_SWITCH_START_NOTIFICATION,
377 &params);
378 if (!info)
379 return -EIO;
380 kfree(info);
381 }
382 return 0;
383}
384
385/**
386 * amdgpu_atpx_switch_end - notify the sbios of a GPU switch
387 *
388 * @atpx: atpx info struct
389 * @mux_id: mux state (0 = integrated GPU, 1 = discrete GPU)
390 *
391 * Execute the ATPX_FUNCTION_GRAPHICS_DEVICE_SWITCH_END_NOTIFICATION ATPX
392 * function to notify the sbios that a switch between the discrete GPU and
393 * integrated GPU has ended (all asics).
394 * Returns 0 on success, error on failure.
395 */
396static int amdgpu_atpx_switch_end(struct amdgpu_atpx *atpx, u16 mux_id)
397{
398 struct acpi_buffer params;
399 union acpi_object *info;
400 struct atpx_mux input;
401
402 if (atpx->functions.switch_end) {
403 input.size = 4;
404 input.mux = mux_id;
405 params.length = input.size;
406 params.pointer = &input;
407 info = amdgpu_atpx_call(atpx->handle,
408 ATPX_FUNCTION_GRAPHICS_DEVICE_SWITCH_END_NOTIFICATION,
409 &params);
410 if (!info)
411 return -EIO;
412 kfree(info);
413 }
414 return 0;
415}
416
417/**
418 * amdgpu_atpx_switchto - switch to the requested GPU
419 *
420 * @id: GPU to switch to
421 *
422 * Execute the necessary ATPX functions to switch between the discrete GPU and
423 * integrated GPU (all asics).
424 * Returns 0 on success, error on failure.
425 */
426static int amdgpu_atpx_switchto(enum vga_switcheroo_client_id id)
427{
428 u16 gpu_id;
429
430 if (id == VGA_SWITCHEROO_IGD)
431 gpu_id = ATPX_INTEGRATED_GPU;
432 else
433 gpu_id = ATPX_DISCRETE_GPU;
434
435 amdgpu_atpx_switch_start(&amdgpu_atpx_priv.atpx, gpu_id);
436 amdgpu_atpx_switch_disp_mux(&amdgpu_atpx_priv.atpx, gpu_id);
437 amdgpu_atpx_switch_i2c_mux(&amdgpu_atpx_priv.atpx, gpu_id);
438 amdgpu_atpx_switch_end(&amdgpu_atpx_priv.atpx, gpu_id);
439
440 return 0;
441}
442
443/**
444 * amdgpu_atpx_power_state - power down/up the requested GPU
445 *
446 * @id: GPU to power down/up
447 * @state: requested power state (0 = off, 1 = on)
448 *
449 * Execute the necessary ATPX function to power down/up the discrete GPU
450 * (all asics).
451 * Returns 0 on success, error on failure.
452 */
453static int amdgpu_atpx_power_state(enum vga_switcheroo_client_id id,
454 enum vga_switcheroo_state state)
455{
456 /* on w500 ACPI can't change intel gpu state */
457 if (id == VGA_SWITCHEROO_IGD)
458 return 0;
459
460 amdgpu_atpx_set_discrete_state(&amdgpu_atpx_priv.atpx, state);
461 return 0;
462}
463
464/**
465 * amdgpu_atpx_pci_probe_handle - look up the ATPX handle
466 *
467 * @pdev: pci device
468 *
469 * Look up the ATPX handles (all asics).
470 * Returns true if the handles are found, false if not.
471 */
472static bool amdgpu_atpx_pci_probe_handle(struct pci_dev *pdev)
473{
474 acpi_handle dhandle, atpx_handle;
475 acpi_status status;
476
477 dhandle = ACPI_HANDLE(&pdev->dev);
478 if (!dhandle)
479 return false;
480
481 status = acpi_get_handle(dhandle, "ATPX", &atpx_handle);
482 if (ACPI_FAILURE(status)) {
483 amdgpu_atpx_priv.other_handle = dhandle;
484 return false;
485 }
486 amdgpu_atpx_priv.dhandle = dhandle;
487 amdgpu_atpx_priv.atpx.handle = atpx_handle;
488 return true;
489}
490
491/**
492 * amdgpu_atpx_init - verify the ATPX interface
493 *
494 * Verify the ATPX interface (all asics).
495 * Returns 0 on success, error on failure.
496 */
497static int amdgpu_atpx_init(void)
498{
499 int r;
500
501 /* set up the ATPX handle */
502 r = amdgpu_atpx_verify_interface(&amdgpu_atpx_priv.atpx);
503 if (r)
504 return r;
505
506 /* validate the atpx setup */
507 r = amdgpu_atpx_validate(&amdgpu_atpx_priv.atpx);
508 if (r)
509 return r;
510
511 return 0;
512}
513
514/**
515 * amdgpu_atpx_get_client_id - get the client id
516 *
517 * @pdev: pci device
518 *
519 * look up whether we are the integrated or discrete GPU (all asics).
520 * Returns the client id.
521 */
522static int amdgpu_atpx_get_client_id(struct pci_dev *pdev)
523{
524 if (amdgpu_atpx_priv.dhandle == ACPI_HANDLE(&pdev->dev))
525 return VGA_SWITCHEROO_IGD;
526 else
527 return VGA_SWITCHEROO_DIS;
528}
529
5d170139 530static const struct vga_switcheroo_handler amdgpu_atpx_handler = {
d38ceaf9
AD
531 .switchto = amdgpu_atpx_switchto,
532 .power_state = amdgpu_atpx_power_state,
d38ceaf9
AD
533 .get_client_id = amdgpu_atpx_get_client_id,
534};
535
536/**
537 * amdgpu_atpx_detect - detect whether we have PX
538 *
539 * Check if we have a PX system (all asics).
540 * Returns true if we have a PX system, false if not.
541 */
542static bool amdgpu_atpx_detect(void)
543{
544 char acpi_method_name[255] = { 0 };
545 struct acpi_buffer buffer = {sizeof(acpi_method_name), acpi_method_name};
546 struct pci_dev *pdev = NULL;
547 bool has_atpx = false;
548 int vga_count = 0;
549
550 while ((pdev = pci_get_class(PCI_CLASS_DISPLAY_VGA << 8, pdev)) != NULL) {
551 vga_count++;
552
553 has_atpx |= (amdgpu_atpx_pci_probe_handle(pdev) == true);
554 }
555
556 while ((pdev = pci_get_class(PCI_CLASS_DISPLAY_OTHER << 8, pdev)) != NULL) {
557 vga_count++;
558
559 has_atpx |= (amdgpu_atpx_pci_probe_handle(pdev) == true);
560 }
561
562 if (has_atpx && vga_count == 2) {
563 acpi_get_name(amdgpu_atpx_priv.atpx.handle, ACPI_FULL_PATHNAME, &buffer);
1694467b 564 printk(KERN_INFO "vga_switcheroo: detected switching method %s handle\n",
d38ceaf9
AD
565 acpi_method_name);
566 amdgpu_atpx_priv.atpx_detected = true;
6b1095ee 567 amdgpu_atpx_init();
d38ceaf9
AD
568 return true;
569 }
570 return false;
571}
572
573/**
574 * amdgpu_register_atpx_handler - register with vga_switcheroo
575 *
576 * Register the PX callbacks with vga_switcheroo (all asics).
577 */
578void amdgpu_register_atpx_handler(void)
579{
580 bool r;
156d7d41 581 enum vga_switcheroo_handler_flags_t handler_flags = 0;
d38ceaf9
AD
582
583 /* detect if we have any ATPX + 2 VGA in the system */
584 r = amdgpu_atpx_detect();
585 if (!r)
586 return;
587
156d7d41 588 vga_switcheroo_register_handler(&amdgpu_atpx_handler, handler_flags);
d38ceaf9
AD
589}
590
591/**
592 * amdgpu_unregister_atpx_handler - unregister with vga_switcheroo
593 *
594 * Unregister the PX callbacks with vga_switcheroo (all asics).
595 */
596void amdgpu_unregister_atpx_handler(void)
597{
598 vga_switcheroo_unregister_handler();
599}