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