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