2 * Copyright (c) 2010 Red Hat Inc.
3 * Author : Dave Airlie <airlied@redhat.com>
7 * ATPX support for both Intel/ATI
9 #include <linux/vga_switcheroo.h>
10 #include <linux/slab.h>
11 #include <linux/acpi.h>
12 #include <linux/pci.h>
14 #include "radeon_acpi.h"
16 struct radeon_atpx_functions
{
23 bool disp_connectors_mapping
;
24 bool disp_detetion_ports
;
29 struct radeon_atpx_functions functions
;
32 static struct radeon_atpx_priv
{
34 /* handle for device - and atpx */
36 struct radeon_atpx atpx
;
39 struct atpx_verify_interface
{
40 u16 size
; /* structure size in bytes (includes size field) */
41 u16 version
; /* version */
42 u32 function_bits
; /* supported functions bit vector */
45 struct atpx_px_params
{
46 u16 size
; /* structure size in bytes (includes size field) */
47 u32 valid_flags
; /* which flags are valid */
48 u32 flags
; /* flags */
51 struct atpx_power_control
{
62 * radeon_atpx_call - call an ATPX method
64 * @handle: acpi handle
65 * @function: the ATPX function to execute
66 * @params: ATPX function params
68 * Executes the requested ATPX function (all asics).
69 * Returns a pointer to the acpi output buffer.
71 static union acpi_object
*radeon_atpx_call(acpi_handle handle
, int function
,
72 struct acpi_buffer
*params
)
75 union acpi_object atpx_arg_elements
[2];
76 struct acpi_object_list atpx_arg
;
77 struct acpi_buffer buffer
= { ACPI_ALLOCATE_BUFFER
, NULL
};
80 atpx_arg
.pointer
= &atpx_arg_elements
[0];
82 atpx_arg_elements
[0].type
= ACPI_TYPE_INTEGER
;
83 atpx_arg_elements
[0].integer
.value
= function
;
86 atpx_arg_elements
[1].type
= ACPI_TYPE_BUFFER
;
87 atpx_arg_elements
[1].buffer
.length
= params
->length
;
88 atpx_arg_elements
[1].buffer
.pointer
= params
->pointer
;
90 /* We need a second fake parameter */
91 atpx_arg_elements
[1].type
= ACPI_TYPE_INTEGER
;
92 atpx_arg_elements
[1].integer
.value
= 0;
95 status
= acpi_evaluate_object(handle
, NULL
, &atpx_arg
, &buffer
);
97 /* Fail only if calling the method fails and ATPX is supported */
98 if (ACPI_FAILURE(status
) && status
!= AE_NOT_FOUND
) {
99 printk("failed to evaluate ATPX got %s\n",
100 acpi_format_exception(status
));
101 kfree(buffer
.pointer
);
105 return buffer
.pointer
;
109 * radeon_atpx_parse_functions - parse supported functions
111 * @f: supported functions struct
112 * @mask: supported functions mask from ATPX
114 * Use the supported functions mask from ATPX function
115 * ATPX_FUNCTION_VERIFY_INTERFACE to determine what functions
116 * are supported (all asics).
118 static void radeon_atpx_parse_functions(struct radeon_atpx_functions
*f
, u32 mask
)
120 f
->px_params
= mask
& ATPX_GET_PX_PARAMETERS_SUPPORTED
;
121 f
->power_cntl
= mask
& ATPX_POWER_CONTROL_SUPPORTED
;
122 f
->disp_mux_cntl
= mask
& ATPX_DISPLAY_MUX_CONTROL_SUPPORTED
;
123 f
->i2c_mux_cntl
= mask
& ATPX_I2C_MUX_CONTROL_SUPPORTED
;
124 f
->switch_start
= mask
& ATPX_GRAPHICS_DEVICE_SWITCH_START_NOTIFICATION_SUPPORTED
;
125 f
->switch_end
= mask
& ATPX_GRAPHICS_DEVICE_SWITCH_END_NOTIFICATION_SUPPORTED
;
126 f
->disp_connectors_mapping
= mask
& ATPX_GET_DISPLAY_CONNECTORS_MAPPING_SUPPORTED
;
127 f
->disp_detetion_ports
= mask
& ATPX_GET_DISPLAY_DETECTION_PORTS_SUPPORTED
;
131 * radeon_atpx_validate_functions - validate ATPX functions
133 * @atpx: radeon atpx struct
135 * Validate that required functions are enabled (all asics).
136 * returns 0 on success, error on failure.
138 static int radeon_atpx_validate(struct radeon_atpx
*atpx
)
140 /* make sure required functions are enabled */
141 /* dGPU power control is required */
142 atpx
->functions
.power_cntl
= true;
144 if (atpx
->functions
.px_params
) {
145 union acpi_object
*info
;
146 struct atpx_px_params output
;
150 info
= radeon_atpx_call(atpx
->handle
, ATPX_FUNCTION_GET_PX_PARAMETERS
, NULL
);
154 memset(&output
, 0, sizeof(output
));
156 size
= *(u16
*) info
->buffer
.pointer
;
158 printk("ATPX buffer is too small: %zu\n", size
);
162 size
= min(sizeof(output
), size
);
164 memcpy(&output
, info
->buffer
.pointer
, size
);
166 valid_bits
= output
.flags
& output
.valid_flags
;
167 /* if separate mux flag is set, mux controls are required */
168 if (valid_bits
& ATPX_SEPARATE_MUX_FOR_I2C
) {
169 atpx
->functions
.i2c_mux_cntl
= true;
170 atpx
->functions
.disp_mux_cntl
= true;
172 /* if any outputs are muxed, mux controls are required */
173 if (valid_bits
& (ATPX_CRT1_RGB_SIGNAL_MUXED
|
174 ATPX_TV_SIGNAL_MUXED
|
175 ATPX_DFP_SIGNAL_MUXED
))
176 atpx
->functions
.disp_mux_cntl
= true;
184 * radeon_atpx_verify_interface - verify ATPX
186 * @atpx: radeon atpx struct
188 * Execute the ATPX_FUNCTION_VERIFY_INTERFACE ATPX function
189 * to initialize ATPX and determine what features are supported
191 * returns 0 on success, error on failure.
193 static int radeon_atpx_verify_interface(struct radeon_atpx
*atpx
)
195 union acpi_object
*info
;
196 struct atpx_verify_interface output
;
200 info
= radeon_atpx_call(atpx
->handle
, ATPX_FUNCTION_VERIFY_INTERFACE
, NULL
);
204 memset(&output
, 0, sizeof(output
));
206 size
= *(u16
*) info
->buffer
.pointer
;
208 printk("ATPX buffer is too small: %zu\n", size
);
212 size
= min(sizeof(output
), size
);
214 memcpy(&output
, info
->buffer
.pointer
, size
);
216 /* TODO: check version? */
217 printk("ATPX version %u\n", output
.version
);
219 radeon_atpx_parse_functions(&atpx
->functions
, output
.function_bits
);
227 * radeon_atpx_set_discrete_state - power up/down discrete GPU
229 * @atpx: atpx info struct
230 * @state: discrete GPU state (0 = power down, 1 = power up)
232 * Execute the ATPX_FUNCTION_POWER_CONTROL ATPX function to
233 * power down/up the discrete GPU (all asics).
234 * Returns 0 on success, error on failure.
236 static int radeon_atpx_set_discrete_state(struct radeon_atpx
*atpx
, u8 state
)
238 struct acpi_buffer params
;
239 union acpi_object
*info
;
240 struct atpx_power_control input
;
242 if (atpx
->functions
.power_cntl
) {
244 input
.dgpu_state
= state
;
245 params
.length
= input
.size
;
246 params
.pointer
= &input
;
247 info
= radeon_atpx_call(atpx
->handle
,
248 ATPX_FUNCTION_POWER_CONTROL
,
258 * radeon_atpx_switch_disp_mux - switch display mux
260 * @atpx: atpx info struct
261 * @mux_id: mux state (0 = integrated GPU, 1 = discrete GPU)
263 * Execute the ATPX_FUNCTION_DISPLAY_MUX_CONTROL ATPX function to
264 * switch the display mux between the discrete GPU and integrated GPU
266 * Returns 0 on success, error on failure.
268 static int radeon_atpx_switch_disp_mux(struct radeon_atpx
*atpx
, u16 mux_id
)
270 struct acpi_buffer params
;
271 union acpi_object
*info
;
272 struct atpx_mux input
;
274 if (atpx
->functions
.disp_mux_cntl
) {
277 params
.length
= input
.size
;
278 params
.pointer
= &input
;
279 info
= radeon_atpx_call(atpx
->handle
,
280 ATPX_FUNCTION_DISPLAY_MUX_CONTROL
,
290 * radeon_atpx_switch_i2c_mux - switch i2c/hpd mux
292 * @atpx: atpx info struct
293 * @mux_id: mux state (0 = integrated GPU, 1 = discrete GPU)
295 * Execute the ATPX_FUNCTION_I2C_MUX_CONTROL ATPX function to
296 * switch the i2c/hpd mux between the discrete GPU and integrated GPU
298 * Returns 0 on success, error on failure.
300 static int radeon_atpx_switch_i2c_mux(struct radeon_atpx
*atpx
, u16 mux_id
)
302 struct acpi_buffer params
;
303 union acpi_object
*info
;
304 struct atpx_mux input
;
306 if (atpx
->functions
.i2c_mux_cntl
) {
309 params
.length
= input
.size
;
310 params
.pointer
= &input
;
311 info
= radeon_atpx_call(atpx
->handle
,
312 ATPX_FUNCTION_I2C_MUX_CONTROL
,
322 * radeon_atpx_switch_start - notify the sbios of a GPU switch
324 * @atpx: atpx info struct
325 * @mux_id: mux state (0 = integrated GPU, 1 = discrete GPU)
327 * Execute the ATPX_FUNCTION_GRAPHICS_DEVICE_SWITCH_START_NOTIFICATION ATPX
328 * function to notify the sbios that a switch between the discrete GPU and
329 * integrated GPU has begun (all asics).
330 * Returns 0 on success, error on failure.
332 static int radeon_atpx_switch_start(struct radeon_atpx
*atpx
, u16 mux_id
)
334 struct acpi_buffer params
;
335 union acpi_object
*info
;
336 struct atpx_mux input
;
338 if (atpx
->functions
.switch_start
) {
341 params
.length
= input
.size
;
342 params
.pointer
= &input
;
343 info
= radeon_atpx_call(atpx
->handle
,
344 ATPX_FUNCTION_GRAPHICS_DEVICE_SWITCH_START_NOTIFICATION
,
354 * radeon_atpx_switch_end - notify the sbios of a GPU switch
356 * @atpx: atpx info struct
357 * @mux_id: mux state (0 = integrated GPU, 1 = discrete GPU)
359 * Execute the ATPX_FUNCTION_GRAPHICS_DEVICE_SWITCH_END_NOTIFICATION ATPX
360 * function to notify the sbios that a switch between the discrete GPU and
361 * integrated GPU has ended (all asics).
362 * Returns 0 on success, error on failure.
364 static int radeon_atpx_switch_end(struct radeon_atpx
*atpx
, u16 mux_id
)
366 struct acpi_buffer params
;
367 union acpi_object
*info
;
368 struct atpx_mux input
;
370 if (atpx
->functions
.switch_end
) {
373 params
.length
= input
.size
;
374 params
.pointer
= &input
;
375 info
= radeon_atpx_call(atpx
->handle
,
376 ATPX_FUNCTION_GRAPHICS_DEVICE_SWITCH_END_NOTIFICATION
,
386 * radeon_atpx_switchto - switch to the requested GPU
388 * @id: GPU to switch to
390 * Execute the necessary ATPX functions to switch between the discrete GPU and
391 * integrated GPU (all asics).
392 * Returns 0 on success, error on failure.
394 static int radeon_atpx_switchto(enum vga_switcheroo_client_id id
)
398 if (id
== VGA_SWITCHEROO_IGD
)
399 gpu_id
= ATPX_INTEGRATED_GPU
;
401 gpu_id
= ATPX_DISCRETE_GPU
;
403 radeon_atpx_switch_start(&radeon_atpx_priv
.atpx
, gpu_id
);
404 radeon_atpx_switch_disp_mux(&radeon_atpx_priv
.atpx
, gpu_id
);
405 radeon_atpx_switch_i2c_mux(&radeon_atpx_priv
.atpx
, gpu_id
);
406 radeon_atpx_switch_end(&radeon_atpx_priv
.atpx
, gpu_id
);
412 * radeon_atpx_power_state - power down/up the requested GPU
414 * @id: GPU to power down/up
415 * @state: requested power state (0 = off, 1 = on)
417 * Execute the necessary ATPX function to power down/up the discrete GPU
419 * Returns 0 on success, error on failure.
421 static int radeon_atpx_power_state(enum vga_switcheroo_client_id id
,
422 enum vga_switcheroo_state state
)
424 /* on w500 ACPI can't change intel gpu state */
425 if (id
== VGA_SWITCHEROO_IGD
)
428 radeon_atpx_set_discrete_state(&radeon_atpx_priv
.atpx
, state
);
433 * radeon_atpx_pci_probe_handle - look up the ATPX handle
437 * Look up the ATPX handles (all asics).
438 * Returns true if the handles are found, false if not.
440 static bool radeon_atpx_pci_probe_handle(struct pci_dev
*pdev
)
442 acpi_handle dhandle
, atpx_handle
;
445 dhandle
= DEVICE_ACPI_HANDLE(&pdev
->dev
);
449 status
= acpi_get_handle(dhandle
, "ATPX", &atpx_handle
);
450 if (ACPI_FAILURE(status
))
453 radeon_atpx_priv
.dhandle
= dhandle
;
454 radeon_atpx_priv
.atpx
.handle
= atpx_handle
;
459 * radeon_atpx_init - verify the ATPX interface
461 * Verify the ATPX interface (all asics).
462 * Returns 0 on success, error on failure.
464 static int radeon_atpx_init(void)
468 /* set up the ATPX handle */
469 r
= radeon_atpx_verify_interface(&radeon_atpx_priv
.atpx
);
473 /* validate the atpx setup */
474 r
= radeon_atpx_validate(&radeon_atpx_priv
.atpx
);
482 * radeon_atpx_get_client_id - get the client id
486 * look up whether we are the integrated or discrete GPU (all asics).
487 * Returns the client id.
489 static int radeon_atpx_get_client_id(struct pci_dev
*pdev
)
491 if (radeon_atpx_priv
.dhandle
== DEVICE_ACPI_HANDLE(&pdev
->dev
))
492 return VGA_SWITCHEROO_IGD
;
494 return VGA_SWITCHEROO_DIS
;
497 static struct vga_switcheroo_handler radeon_atpx_handler
= {
498 .switchto
= radeon_atpx_switchto
,
499 .power_state
= radeon_atpx_power_state
,
500 .init
= radeon_atpx_init
,
501 .get_client_id
= radeon_atpx_get_client_id
,
505 * radeon_atpx_detect - detect whether we have PX
507 * Check if we have a PX system (all asics).
508 * Returns true if we have a PX system, false if not.
510 static bool radeon_atpx_detect(void)
512 char acpi_method_name
[255] = { 0 };
513 struct acpi_buffer buffer
= {sizeof(acpi_method_name
), acpi_method_name
};
514 struct pci_dev
*pdev
= NULL
;
515 bool has_atpx
= false;
518 while ((pdev
= pci_get_class(PCI_CLASS_DISPLAY_VGA
<< 8, pdev
)) != NULL
) {
521 has_atpx
|= (radeon_atpx_pci_probe_handle(pdev
) == true);
524 if (has_atpx
&& vga_count
== 2) {
525 acpi_get_name(radeon_atpx_priv
.atpx
.handle
, ACPI_FULL_PATHNAME
, &buffer
);
526 printk(KERN_INFO
"VGA switcheroo: detected switching method %s handle\n",
528 radeon_atpx_priv
.atpx_detected
= true;
535 * radeon_register_atpx_handler - register with vga_switcheroo
537 * Register the PX callbacks with vga_switcheroo (all asics).
539 void radeon_register_atpx_handler(void)
543 /* detect if we have any ATPX + 2 VGA in the system */
544 r
= radeon_atpx_detect();
548 vga_switcheroo_register_handler(&radeon_atpx_handler
);
552 * radeon_unregister_atpx_handler - unregister with vga_switcheroo
554 * Unregister the PX callbacks with vga_switcheroo (all asics).
556 void radeon_unregister_atpx_handler(void)
558 vga_switcheroo_unregister_handler();