Merge branch 'vendor/MDOCML'
[dragonfly.git] / sys / dev / 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  * $FreeBSD: head/sys/dev/drm2/radeon/radeon_atpx_handler.c 254885 2013-08-25 19:37:15Z dumbbell $
10  */
11
12 #include <sys/param.h>
13 #include <sys/systm.h>
14 #include <sys/bus.h>
15 #include <sys/linker.h>
16
17 #include <drm/drmP.h>
18 #include <uapi_drm/radeon_drm.h>
19 #include "radeon_acpi.h"
20 #include "radeon_drv.h"
21
22 #ifdef DUMBBELL_WIP
23 struct radeon_atpx_functions {
24         bool px_params;
25         bool power_cntl;
26         bool disp_mux_cntl;
27         bool i2c_mux_cntl;
28         bool switch_start;
29         bool switch_end;
30         bool disp_connectors_mapping;
31         bool disp_detetion_ports;
32 };
33
34 struct radeon_atpx {
35         ACPI_HANDLE handle;
36         struct radeon_atpx_functions functions;
37 };
38
39 static struct radeon_atpx_priv {
40         bool atpx_detected;
41         /* handle for device - and atpx */
42         ACPI_HANDLE dhandle;
43         struct radeon_atpx atpx;
44 } radeon_atpx_priv;
45
46 struct atpx_verify_interface {
47         u16 size;               /* structure size in bytes (includes size field) */
48         u16 version;            /* version */
49         u32 function_bits;      /* supported functions bit vector */
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 /**
63  * radeon_atpx_call - call an ATPX method
64  *
65  * @handle: acpi handle
66  * @function: the ATPX function to execute
67  * @params: ATPX function params
68  *
69  * Executes the requested ATPX function (all asics).
70  * Returns a pointer to the acpi output buffer.
71  */
72 static ACPI_OBJECT *radeon_atpx_call(ACPI_HANDLE handle, int function,
73                                            ACPI_BUFFER *params)
74 {
75         ACPI_STATUS status;
76         ACPI_OBJECT atpx_arg_elements[2];
77         ACPI_OBJECT_LIST atpx_arg;
78         ACPI_BUFFER buffer = { ACPI_ALLOCATE_BUFFER, NULL };
79
80         atpx_arg.Count = 2;
81         atpx_arg.Pointer = &atpx_arg_elements[0];
82
83         atpx_arg_elements[0].Type = ACPI_TYPE_INTEGER;
84         atpx_arg_elements[0].Integer.Value = function;
85
86         if (params) {
87                 atpx_arg_elements[1].Type = ACPI_TYPE_BUFFER;
88                 atpx_arg_elements[1].Buffer.Length = params->Length;
89                 atpx_arg_elements[1].Buffer.Pointer = params->Pointer;
90         } else {
91                 /* We need a second fake parameter */
92                 atpx_arg_elements[1].Type = ACPI_TYPE_INTEGER;
93                 atpx_arg_elements[1].Integer.Value = 0;
94         }
95
96         status = AcpiEvaluateObject(handle, NULL, &atpx_arg, &buffer);
97
98         /* Fail only if calling the method fails and ATPX is supported */
99         if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
100                 DRM_ERROR("failed to evaluate ATPX got %s\n",
101                        AcpiFormatException(status));
102                 AcpiOsFree(buffer.Pointer);
103                 return NULL;
104         }
105
106         return buffer.Pointer;
107 }
108
109 /**
110  * radeon_atpx_parse_functions - parse supported functions
111  *
112  * @f: supported functions struct
113  * @mask: supported functions mask from ATPX
114  *
115  * Use the supported functions mask from ATPX function
116  * ATPX_FUNCTION_VERIFY_INTERFACE to determine what functions
117  * are supported (all asics).
118  */
119 static void radeon_atpx_parse_functions(struct radeon_atpx_functions *f, u32 mask)
120 {
121         f->px_params = mask & ATPX_GET_PX_PARAMETERS_SUPPORTED;
122         f->power_cntl = mask & ATPX_POWER_CONTROL_SUPPORTED;
123         f->disp_mux_cntl = mask & ATPX_DISPLAY_MUX_CONTROL_SUPPORTED;
124         f->i2c_mux_cntl = mask & ATPX_I2C_MUX_CONTROL_SUPPORTED;
125         f->switch_start = mask & ATPX_GRAPHICS_DEVICE_SWITCH_START_NOTIFICATION_SUPPORTED;
126         f->switch_end = mask & ATPX_GRAPHICS_DEVICE_SWITCH_END_NOTIFICATION_SUPPORTED;
127         f->disp_connectors_mapping = mask & ATPX_GET_DISPLAY_CONNECTORS_MAPPING_SUPPORTED;
128         f->disp_detetion_ports = mask & ATPX_GET_DISPLAY_DETECTION_PORTS_SUPPORTED;
129 }
130
131 /**
132  * radeon_atpx_verify_interface - verify ATPX
133  *
134  * @handle: acpi handle
135  * @atpx: radeon atpx struct
136  *
137  * Execute the ATPX_FUNCTION_VERIFY_INTERFACE ATPX function
138  * to initialize ATPX and determine what features are supported
139  * (all asics).
140  * returns 0 on success, error on failure.
141  */
142 static int radeon_atpx_verify_interface(struct radeon_atpx *atpx)
143 {
144         ACPI_OBJECT *info;
145         struct atpx_verify_interface output;
146         size_t size;
147         int err = 0;
148
149         info = radeon_atpx_call(atpx->handle, ATPX_FUNCTION_VERIFY_INTERFACE, NULL);
150         if (!info)
151                 return -EIO;
152
153         memset(&output, 0, sizeof(output));
154
155         size = *(u16 *) info->Buffer.Pointer;
156         if (size < 8) {
157                 DRM_ERROR("ATPX buffer is too small: %zu\n", size);
158                 err = -EINVAL;
159                 goto out;
160         }
161         size = min(sizeof(output), size);
162
163         memcpy(&output, info->Buffer.Pointer, size);
164
165         /* TODO: check version? */
166         DRM_INFO("ATPX version %u\n", output.version);
167
168         radeon_atpx_parse_functions(&atpx->functions, output.function_bits);
169
170 out:
171         AcpiOsFree(info);
172         return err;
173 }
174
175 /**
176  * radeon_atpx_set_discrete_state - power up/down discrete GPU
177  *
178  * @atpx: atpx info struct
179  * @state: discrete GPU state (0 = power down, 1 = power up)
180  *
181  * Execute the ATPX_FUNCTION_POWER_CONTROL ATPX function to
182  * power down/up the discrete GPU (all asics).
183  * Returns 0 on success, error on failure.
184  */
185 static int radeon_atpx_set_discrete_state(struct radeon_atpx *atpx, u8 state)
186 {
187         ACPI_BUFFER params;
188         ACPI_OBJECT *info;
189         struct atpx_power_control input;
190
191         if (atpx->functions.power_cntl) {
192                 input.size = 3;
193                 input.dgpu_state = state;
194                 params.Length = input.size;
195                 params.Pointer = &input;
196                 info = radeon_atpx_call(atpx->handle,
197                                         ATPX_FUNCTION_POWER_CONTROL,
198                                         &params);
199                 if (!info)
200                         return -EIO;
201                 AcpiOsFree(info);
202         }
203         return 0;
204 }
205
206 /**
207  * radeon_atpx_switch_disp_mux - switch display mux
208  *
209  * @atpx: atpx info struct
210  * @mux_id: mux state (0 = integrated GPU, 1 = discrete GPU)
211  *
212  * Execute the ATPX_FUNCTION_DISPLAY_MUX_CONTROL ATPX function to
213  * switch the display mux between the discrete GPU and integrated GPU
214  * (all asics).
215  * Returns 0 on success, error on failure.
216  */
217 static int radeon_atpx_switch_disp_mux(struct radeon_atpx *atpx, u16 mux_id)
218 {
219         ACPI_BUFFER params;
220         ACPI_OBJECT *info;
221         struct atpx_mux input;
222
223         if (atpx->functions.disp_mux_cntl) {
224                 input.size = 4;
225                 input.mux = mux_id;
226                 params.Length = input.size;
227                 params.Pointer = &input;
228                 info = radeon_atpx_call(atpx->handle,
229                                         ATPX_FUNCTION_DISPLAY_MUX_CONTROL,
230                                         &params);
231                 if (!info)
232                         return -EIO;
233                 AcpiOsFree(info);
234         }
235         return 0;
236 }
237
238 /**
239  * radeon_atpx_switch_i2c_mux - switch i2c/hpd mux
240  *
241  * @atpx: atpx info struct
242  * @mux_id: mux state (0 = integrated GPU, 1 = discrete GPU)
243  *
244  * Execute the ATPX_FUNCTION_I2C_MUX_CONTROL ATPX function to
245  * switch the i2c/hpd mux between the discrete GPU and integrated GPU
246  * (all asics).
247  * Returns 0 on success, error on failure.
248  */
249 static int radeon_atpx_switch_i2c_mux(struct radeon_atpx *atpx, u16 mux_id)
250 {
251         ACPI_BUFFER params;
252         ACPI_OBJECT *info;
253         struct atpx_mux input;
254
255         if (atpx->functions.i2c_mux_cntl) {
256                 input.size = 4;
257                 input.mux = mux_id;
258                 params.Length = input.size;
259                 params.Pointer = &input;
260                 info = radeon_atpx_call(atpx->handle,
261                                         ATPX_FUNCTION_I2C_MUX_CONTROL,
262                                         &params);
263                 if (!info)
264                         return -EIO;
265                 AcpiOsFree(info);
266         }
267         return 0;
268 }
269
270 /**
271  * radeon_atpx_switch_start - notify the sbios of a GPU switch
272  *
273  * @atpx: atpx info struct
274  * @mux_id: mux state (0 = integrated GPU, 1 = discrete GPU)
275  *
276  * Execute the ATPX_FUNCTION_GRAPHICS_DEVICE_SWITCH_START_NOTIFICATION ATPX
277  * function to notify the sbios that a switch between the discrete GPU and
278  * integrated GPU has begun (all asics).
279  * Returns 0 on success, error on failure.
280  */
281 static int radeon_atpx_switch_start(struct radeon_atpx *atpx, u16 mux_id)
282 {
283         ACPI_BUFFER params;
284         ACPI_OBJECT *info;
285         struct atpx_mux input;
286
287         if (atpx->functions.switch_start) {
288                 input.size = 4;
289                 input.mux = mux_id;
290                 params.Length = input.size;
291                 params.Pointer = &input;
292                 info = radeon_atpx_call(atpx->handle,
293                                         ATPX_FUNCTION_GRAPHICS_DEVICE_SWITCH_START_NOTIFICATION,
294                                         &params);
295                 if (!info)
296                         return -EIO;
297                 AcpiOsFree(info);
298         }
299         return 0;
300 }
301
302 /**
303  * radeon_atpx_switch_end - notify the sbios of a GPU switch
304  *
305  * @atpx: atpx info struct
306  * @mux_id: mux state (0 = integrated GPU, 1 = discrete GPU)
307  *
308  * Execute the ATPX_FUNCTION_GRAPHICS_DEVICE_SWITCH_END_NOTIFICATION ATPX
309  * function to notify the sbios that a switch between the discrete GPU and
310  * integrated GPU has ended (all asics).
311  * Returns 0 on success, error on failure.
312  */
313 static int radeon_atpx_switch_end(struct radeon_atpx *atpx, u16 mux_id)
314 {
315         ACPI_BUFFER params;
316         ACPI_OBJECT *info;
317         struct atpx_mux input;
318
319         if (atpx->functions.switch_end) {
320                 input.size = 4;
321                 input.mux = mux_id;
322                 params.Length = input.size;
323                 params.Pointer = &input;
324                 info = radeon_atpx_call(atpx->handle,
325                                         ATPX_FUNCTION_GRAPHICS_DEVICE_SWITCH_END_NOTIFICATION,
326                                         &params);
327                 if (!info)
328                         return -EIO;
329                 AcpiOsFree(info);
330         }
331         return 0;
332 }
333
334 /**
335  * radeon_atpx_switchto - switch to the requested GPU
336  *
337  * @id: GPU to switch to
338  *
339  * Execute the necessary ATPX functions to switch between the discrete GPU and
340  * integrated GPU (all asics).
341  * Returns 0 on success, error on failure.
342  */
343 static int radeon_atpx_switchto(enum vga_switcheroo_client_id id)
344 {
345         u16 gpu_id;
346
347         if (id == VGA_SWITCHEROO_IGD)
348                 gpu_id = ATPX_INTEGRATED_GPU;
349         else
350                 gpu_id = ATPX_DISCRETE_GPU;
351
352         radeon_atpx_switch_start(&radeon_atpx_priv.atpx, gpu_id);
353         radeon_atpx_switch_disp_mux(&radeon_atpx_priv.atpx, gpu_id);
354         radeon_atpx_switch_i2c_mux(&radeon_atpx_priv.atpx, gpu_id);
355         radeon_atpx_switch_end(&radeon_atpx_priv.atpx, gpu_id);
356
357         return 0;
358 }
359
360 /**
361  * radeon_atpx_power_state - power down/up the requested GPU
362  *
363  * @id: GPU to power down/up
364  * @state: requested power state (0 = off, 1 = on)
365  *
366  * Execute the necessary ATPX function to power down/up the discrete GPU
367  * (all asics).
368  * Returns 0 on success, error on failure.
369  */
370 static int radeon_atpx_power_state(enum vga_switcheroo_client_id id,
371                                    enum vga_switcheroo_state state)
372 {
373         /* on w500 ACPI can't change intel gpu state */
374         if (id == VGA_SWITCHEROO_IGD)
375                 return 0;
376
377         radeon_atpx_set_discrete_state(&radeon_atpx_priv.atpx, state);
378         return 0;
379 }
380
381 /**
382  * radeon_atpx_pci_probe_handle - look up the ATPX handle
383  *
384  * @pdev: pci device
385  *
386  * Look up the ATPX handles (all asics).
387  * Returns true if the handles are found, false if not.
388  */
389 static bool radeon_atpx_pci_probe_handle(struct pci_dev *pdev)
390 {
391         ACPI_HANDLE dhandle, atpx_handle;
392         ACPI_STATUS status;
393
394         dhandle = DEVICE_ACPI_HANDLE(&pdev->dev);
395         if (!dhandle)
396                 return false;
397
398         status = AcpiGetHandle(dhandle, "ATPX", &atpx_handle);
399         if (ACPI_FAILURE(status))
400                 return false;
401
402         radeon_atpx_priv.dhandle = dhandle;
403         radeon_atpx_priv.atpx.handle = atpx_handle;
404         return true;
405 }
406
407 /**
408  * radeon_atpx_init - verify the ATPX interface
409  *
410  * Verify the ATPX interface (all asics).
411  * Returns 0 on success, error on failure.
412  */
413 static int radeon_atpx_init(void)
414 {
415         /* set up the ATPX handle */
416         return radeon_atpx_verify_interface(&radeon_atpx_priv.atpx);
417 }
418
419 /**
420  * radeon_atpx_get_client_id - get the client id
421  *
422  * @pdev: pci device
423  *
424  * look up whether we are the integrated or discrete GPU (all asics).
425  * Returns the client id.
426  */
427 static int radeon_atpx_get_client_id(struct pci_dev *pdev)
428 {
429         if (radeon_atpx_priv.dhandle == DEVICE_ACPI_HANDLE(&pdev->dev))
430                 return VGA_SWITCHEROO_IGD;
431         else
432                 return VGA_SWITCHEROO_DIS;
433 }
434
435 static struct vga_switcheroo_handler radeon_atpx_handler = {
436         .switchto = radeon_atpx_switchto,
437         .power_state = radeon_atpx_power_state,
438         .init = radeon_atpx_init,
439         .get_client_id = radeon_atpx_get_client_id,
440 };
441
442 /**
443  * radeon_atpx_detect - detect whether we have PX
444  *
445  * Check if we have a PX system (all asics).
446  * Returns true if we have a PX system, false if not.
447  */
448 static bool radeon_atpx_detect(void)
449 {
450         char acpi_method_name[255] = { 0 };
451         ACPI_BUFFER buffer = {sizeof(acpi_method_name), acpi_method_name};
452         struct pci_dev *pdev = NULL;
453         bool has_atpx = false;
454         int vga_count = 0;
455
456         while ((pdev = pci_get_class(PCI_CLASS_DISPLAY_VGA << 8, pdev)) != NULL) {
457                 vga_count++;
458
459                 has_atpx |= (radeon_atpx_pci_probe_handle(pdev) == true);
460         }
461
462         if (has_atpx && vga_count == 2) {
463                 AcpiGetName(radeon_atpx_priv.atpx.handle, ACPI_FULL_PATHNAME, &buffer);
464                 DRM_INFO("VGA switcheroo: detected switching method %s handle\n",
465                        acpi_method_name);
466                 radeon_atpx_priv.atpx_detected = true;
467                 return true;
468         }
469         return false;
470 }
471 #endif /* DUMBBELL_WIP */
472
473 /**
474  * radeon_register_atpx_handler - register with vga_switcheroo
475  *
476  * Register the PX callbacks with vga_switcheroo (all asics).
477  */
478 void radeon_register_atpx_handler(void)
479 {
480 #ifdef DUMBBELL_WIP
481         bool r;
482
483         /* detect if we have any ATPX + 2 VGA in the system */
484         r = radeon_atpx_detect();
485         if (!r)
486                 return;
487
488         vga_switcheroo_register_handler(&radeon_atpx_handler);
489 #endif /* DUMBBELL_WIP */
490 }
491
492 /**
493  * radeon_unregister_atpx_handler - unregister with vga_switcheroo
494  *
495  * Unregister the PX callbacks with vga_switcheroo (all asics).
496  */
497 void radeon_unregister_atpx_handler(void)
498 {
499 #ifdef DUMBBELL_WIP
500         vga_switcheroo_unregister_handler();
501 #endif /* DUMBBELL_WIP */
502 }