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>
13 #include <linux/delay.h>
15 #include "radeon_acpi.h"
17 struct radeon_atpx_functions {
24 bool disp_connectors_mapping;
25 bool disp_detetion_ports;
30 struct radeon_atpx_functions functions;
33 static struct radeon_atpx_priv {
35 /* handle for device - and atpx */
37 struct radeon_atpx atpx;
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 */
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 */
52 struct atpx_power_control {
62 bool radeon_has_atpx(void) {
63 return radeon_atpx_priv.atpx_detected;
66 bool radeon_has_atpx_dgpu_power_cntl(void) {
67 return radeon_atpx_priv.atpx.functions.power_cntl;
71 * radeon_atpx_call - call an ATPX method
73 * @handle: acpi handle
74 * @function: the ATPX function to execute
75 * @params: ATPX function params
77 * Executes the requested ATPX function (all asics).
78 * Returns a pointer to the acpi output buffer.
80 static union acpi_object *radeon_atpx_call(acpi_handle handle, int function,
81 struct acpi_buffer *params)
84 union acpi_object atpx_arg_elements[2];
85 struct acpi_object_list atpx_arg;
86 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
89 atpx_arg.pointer = &atpx_arg_elements[0];
91 atpx_arg_elements[0].type = ACPI_TYPE_INTEGER;
92 atpx_arg_elements[0].integer.value = function;
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;
99 /* We need a second fake parameter */
100 atpx_arg_elements[1].type = ACPI_TYPE_INTEGER;
101 atpx_arg_elements[1].integer.value = 0;
104 status = acpi_evaluate_object(handle, NULL, &atpx_arg, &buffer);
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);
114 return buffer.pointer;
118 * radeon_atpx_parse_functions - parse supported functions
120 * @f: supported functions struct
121 * @mask: supported functions mask from ATPX
123 * Use the supported functions mask from ATPX function
124 * ATPX_FUNCTION_VERIFY_INTERFACE to determine what functions
125 * are supported (all asics).
127 static void radeon_atpx_parse_functions(struct radeon_atpx_functions *f, u32 mask)
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;
140 * radeon_atpx_validate_functions - validate ATPX functions
142 * @atpx: radeon atpx struct
144 * Validate that required functions are enabled (all asics).
145 * returns 0 on success, error on failure.
147 static int radeon_atpx_validate(struct radeon_atpx *atpx)
151 if (atpx->functions.px_params) {
152 union acpi_object *info;
153 struct atpx_px_params output;
156 info = radeon_atpx_call(atpx->handle, ATPX_FUNCTION_GET_PX_PARAMETERS, NULL);
160 memset(&output, 0, sizeof(output));
162 size = *(u16 *) info->buffer.pointer;
164 printk("ATPX buffer is too small: %zu\n", size);
168 size = min(sizeof(output), size);
170 memcpy(&output, info->buffer.pointer, size);
172 valid_bits = output.flags & output.valid_flags;
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;
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;
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;
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;
207 * radeon_atpx_verify_interface - verify ATPX
209 * @atpx: radeon atpx struct
211 * Execute the ATPX_FUNCTION_VERIFY_INTERFACE ATPX function
212 * to initialize ATPX and determine what features are supported
214 * returns 0 on success, error on failure.
216 static int radeon_atpx_verify_interface(struct radeon_atpx *atpx)
218 union acpi_object *info;
219 struct atpx_verify_interface output;
223 info = radeon_atpx_call(atpx->handle, ATPX_FUNCTION_VERIFY_INTERFACE, NULL);
227 memset(&output, 0, sizeof(output));
229 size = *(u16 *) info->buffer.pointer;
231 printk("ATPX buffer is too small: %zu\n", size);
235 size = min(sizeof(output), size);
237 memcpy(&output, info->buffer.pointer, size);
239 /* TODO: check version? */
240 printk("ATPX version %u, functions 0x%08x\n",
241 output.version, output.function_bits);
243 radeon_atpx_parse_functions(&atpx->functions, output.function_bits);
251 * radeon_atpx_set_discrete_state - power up/down discrete GPU
253 * @atpx: atpx info struct
254 * @state: discrete GPU state (0 = power down, 1 = power up)
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.
260 static int radeon_atpx_set_discrete_state(struct radeon_atpx *atpx, u8 state)
262 struct acpi_buffer params;
263 union acpi_object *info;
264 struct atpx_power_control input;
266 if (atpx->functions.power_cntl) {
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,
278 /* 200ms delay is required after off */
286 * radeon_atpx_switch_disp_mux - switch display mux
288 * @atpx: atpx info struct
289 * @mux_id: mux state (0 = integrated GPU, 1 = discrete GPU)
291 * Execute the ATPX_FUNCTION_DISPLAY_MUX_CONTROL ATPX function to
292 * switch the display mux between the discrete GPU and integrated GPU
294 * Returns 0 on success, error on failure.
296 static int radeon_atpx_switch_disp_mux(struct radeon_atpx *atpx, u16 mux_id)
298 struct acpi_buffer params;
299 union acpi_object *info;
300 struct atpx_mux input;
302 if (atpx->functions.disp_mux_cntl) {
305 params.length = input.size;
306 params.pointer = &input;
307 info = radeon_atpx_call(atpx->handle,
308 ATPX_FUNCTION_DISPLAY_MUX_CONTROL,
318 * radeon_atpx_switch_i2c_mux - switch i2c/hpd mux
320 * @atpx: atpx info struct
321 * @mux_id: mux state (0 = integrated GPU, 1 = discrete GPU)
323 * Execute the ATPX_FUNCTION_I2C_MUX_CONTROL ATPX function to
324 * switch the i2c/hpd mux between the discrete GPU and integrated GPU
326 * Returns 0 on success, error on failure.
328 static int radeon_atpx_switch_i2c_mux(struct radeon_atpx *atpx, u16 mux_id)
330 struct acpi_buffer params;
331 union acpi_object *info;
332 struct atpx_mux input;
334 if (atpx->functions.i2c_mux_cntl) {
337 params.length = input.size;
338 params.pointer = &input;
339 info = radeon_atpx_call(atpx->handle,
340 ATPX_FUNCTION_I2C_MUX_CONTROL,
350 * radeon_atpx_switch_start - notify the sbios of a GPU switch
352 * @atpx: atpx info struct
353 * @mux_id: mux state (0 = integrated GPU, 1 = discrete GPU)
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.
360 static int radeon_atpx_switch_start(struct radeon_atpx *atpx, u16 mux_id)
362 struct acpi_buffer params;
363 union acpi_object *info;
364 struct atpx_mux input;
366 if (atpx->functions.switch_start) {
369 params.length = input.size;
370 params.pointer = &input;
371 info = radeon_atpx_call(atpx->handle,
372 ATPX_FUNCTION_GRAPHICS_DEVICE_SWITCH_START_NOTIFICATION,
382 * radeon_atpx_switch_end - notify the sbios of a GPU switch
384 * @atpx: atpx info struct
385 * @mux_id: mux state (0 = integrated GPU, 1 = discrete GPU)
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.
392 static int radeon_atpx_switch_end(struct radeon_atpx *atpx, u16 mux_id)
394 struct acpi_buffer params;
395 union acpi_object *info;
396 struct atpx_mux input;
398 if (atpx->functions.switch_end) {
401 params.length = input.size;
402 params.pointer = &input;
403 info = radeon_atpx_call(atpx->handle,
404 ATPX_FUNCTION_GRAPHICS_DEVICE_SWITCH_END_NOTIFICATION,
414 * radeon_atpx_switchto - switch to the requested GPU
416 * @id: GPU to switch to
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.
422 static int radeon_atpx_switchto(enum vga_switcheroo_client_id id)
426 if (id == VGA_SWITCHEROO_IGD)
427 gpu_id = ATPX_INTEGRATED_GPU;
429 gpu_id = ATPX_DISCRETE_GPU;
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);
440 * radeon_atpx_power_state - power down/up the requested GPU
442 * @id: GPU to power down/up
443 * @state: requested power state (0 = off, 1 = on)
445 * Execute the necessary ATPX function to power down/up the discrete GPU
447 * Returns 0 on success, error on failure.
449 static int radeon_atpx_power_state(enum vga_switcheroo_client_id id,
450 enum vga_switcheroo_state state)
452 /* on w500 ACPI can't change intel gpu state */
453 if (id == VGA_SWITCHEROO_IGD)
456 radeon_atpx_set_discrete_state(&radeon_atpx_priv.atpx, state);
461 * radeon_atpx_pci_probe_handle - look up the ATPX handle
465 * Look up the ATPX handles (all asics).
466 * Returns true if the handles are found, false if not.
468 static bool radeon_atpx_pci_probe_handle(struct pci_dev *pdev)
470 acpi_handle dhandle, atpx_handle;
473 dhandle = ACPI_HANDLE(&pdev->dev);
477 status = acpi_get_handle(dhandle, "ATPX", &atpx_handle);
478 if (ACPI_FAILURE(status))
481 radeon_atpx_priv.dhandle = dhandle;
482 radeon_atpx_priv.atpx.handle = atpx_handle;
487 * radeon_atpx_init - verify the ATPX interface
489 * Verify the ATPX interface (all asics).
490 * Returns 0 on success, error on failure.
492 static int radeon_atpx_init(void)
496 /* set up the ATPX handle */
497 r = radeon_atpx_verify_interface(&radeon_atpx_priv.atpx);
501 /* validate the atpx setup */
502 r = radeon_atpx_validate(&radeon_atpx_priv.atpx);
510 * radeon_atpx_get_client_id - get the client id
514 * look up whether we are the integrated or discrete GPU (all asics).
515 * Returns the client id.
517 static int radeon_atpx_get_client_id(struct pci_dev *pdev)
519 if (radeon_atpx_priv.dhandle == ACPI_HANDLE(&pdev->dev))
520 return VGA_SWITCHEROO_IGD;
522 return VGA_SWITCHEROO_DIS;
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,
533 * radeon_atpx_detect - detect whether we have PX
535 * Check if we have a PX system (all asics).
536 * Returns true if we have a PX system, false if not.
538 static bool radeon_atpx_detect(void)
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;
546 while ((pdev = pci_get_class(PCI_CLASS_DISPLAY_VGA << 8, pdev)) != NULL) {
549 has_atpx |= (radeon_atpx_pci_probe_handle(pdev) == true);
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) {
556 has_atpx |= (radeon_atpx_pci_probe_handle(pdev) == true);
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",
563 radeon_atpx_priv.atpx_detected = true;
570 * radeon_register_atpx_handler - register with vga_switcheroo
572 * Register the PX callbacks with vga_switcheroo (all asics).
574 void radeon_register_atpx_handler(void)
577 enum vga_switcheroo_handler_flags_t handler_flags = 0;
579 /* detect if we have any ATPX + 2 VGA in the system */
580 r = radeon_atpx_detect();
584 vga_switcheroo_register_handler(&radeon_atpx_handler, handler_flags);
588 * radeon_unregister_atpx_handler - unregister with vga_switcheroo
590 * Unregister the PX callbacks with vga_switcheroo (all asics).
592 void radeon_unregister_atpx_handler(void)
594 vga_switcheroo_unregister_handler();