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 "amdgpu_acpi.h"
17 struct amdgpu_atpx_functions {
24 bool disp_connectors_mapping;
25 bool disp_detetion_ports;
30 struct amdgpu_atpx_functions functions;
33 static struct amdgpu_atpx_priv {
35 /* handle for device - and atpx */
37 acpi_handle other_handle;
38 struct amdgpu_atpx atpx;
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 */
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 */
53 struct atpx_power_control {
63 bool amdgpu_has_atpx(void) {
64 return amdgpu_atpx_priv.atpx_detected;
68 * amdgpu_atpx_call - call an ATPX method
70 * @handle: acpi handle
71 * @function: the ATPX function to execute
72 * @params: ATPX function params
74 * Executes the requested ATPX function (all asics).
75 * Returns a pointer to the acpi output buffer.
77 static union acpi_object *amdgpu_atpx_call(acpi_handle handle, int function,
78 struct acpi_buffer *params)
81 union acpi_object atpx_arg_elements[2];
82 struct acpi_object_list atpx_arg;
83 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
86 atpx_arg.pointer = &atpx_arg_elements[0];
88 atpx_arg_elements[0].type = ACPI_TYPE_INTEGER;
89 atpx_arg_elements[0].integer.value = function;
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;
96 /* We need a second fake parameter */
97 atpx_arg_elements[1].type = ACPI_TYPE_INTEGER;
98 atpx_arg_elements[1].integer.value = 0;
101 status = acpi_evaluate_object(handle, NULL, &atpx_arg, &buffer);
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);
111 return buffer.pointer;
115 * amdgpu_atpx_parse_functions - parse supported functions
117 * @f: supported functions struct
118 * @mask: supported functions mask from ATPX
120 * Use the supported functions mask from ATPX function
121 * ATPX_FUNCTION_VERIFY_INTERFACE to determine what functions
122 * are supported (all asics).
124 static void amdgpu_atpx_parse_functions(struct amdgpu_atpx_functions *f, u32 mask)
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;
137 * amdgpu_atpx_validate_functions - validate ATPX functions
139 * @atpx: amdgpu atpx struct
141 * Validate that required functions are enabled (all asics).
142 * returns 0 on success, error on failure.
144 static int amdgpu_atpx_validate(struct amdgpu_atpx *atpx)
146 /* make sure required functions are enabled */
147 /* dGPU power control is required */
148 atpx->functions.power_cntl = true;
150 if (atpx->functions.px_params) {
151 union acpi_object *info;
152 struct atpx_px_params output;
156 info = amdgpu_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;
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;
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;
190 * amdgpu_atpx_verify_interface - verify ATPX
192 * @atpx: amdgpu atpx struct
194 * Execute the ATPX_FUNCTION_VERIFY_INTERFACE ATPX function
195 * to initialize ATPX and determine what features are supported
197 * returns 0 on success, error on failure.
199 static int amdgpu_atpx_verify_interface(struct amdgpu_atpx *atpx)
201 union acpi_object *info;
202 struct atpx_verify_interface output;
206 info = amdgpu_atpx_call(atpx->handle, ATPX_FUNCTION_VERIFY_INTERFACE, NULL);
210 memset(&output, 0, sizeof(output));
212 size = *(u16 *) info->buffer.pointer;
214 printk("ATPX buffer is too small: %zu\n", size);
218 size = min(sizeof(output), size);
220 memcpy(&output, info->buffer.pointer, size);
222 /* TODO: check version? */
223 printk("ATPX version %u, functions 0x%08x\n",
224 output.version, output.function_bits);
226 amdgpu_atpx_parse_functions(&atpx->functions, output.function_bits);
234 * amdgpu_atpx_set_discrete_state - power up/down discrete GPU
236 * @atpx: atpx info struct
237 * @state: discrete GPU state (0 = power down, 1 = power up)
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.
243 static int amdgpu_atpx_set_discrete_state(struct amdgpu_atpx *atpx, u8 state)
245 struct acpi_buffer params;
246 union acpi_object *info;
247 struct atpx_power_control input;
249 if (atpx->functions.power_cntl) {
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,
261 /* 200ms delay is required after off */
269 * amdgpu_atpx_switch_disp_mux - switch display mux
271 * @atpx: atpx info struct
272 * @mux_id: mux state (0 = integrated GPU, 1 = discrete GPU)
274 * Execute the ATPX_FUNCTION_DISPLAY_MUX_CONTROL ATPX function to
275 * switch the display mux between the discrete GPU and integrated GPU
277 * Returns 0 on success, error on failure.
279 static int amdgpu_atpx_switch_disp_mux(struct amdgpu_atpx *atpx, u16 mux_id)
281 struct acpi_buffer params;
282 union acpi_object *info;
283 struct atpx_mux input;
285 if (atpx->functions.disp_mux_cntl) {
288 params.length = input.size;
289 params.pointer = &input;
290 info = amdgpu_atpx_call(atpx->handle,
291 ATPX_FUNCTION_DISPLAY_MUX_CONTROL,
301 * amdgpu_atpx_switch_i2c_mux - switch i2c/hpd mux
303 * @atpx: atpx info struct
304 * @mux_id: mux state (0 = integrated GPU, 1 = discrete GPU)
306 * Execute the ATPX_FUNCTION_I2C_MUX_CONTROL ATPX function to
307 * switch the i2c/hpd mux between the discrete GPU and integrated GPU
309 * Returns 0 on success, error on failure.
311 static int amdgpu_atpx_switch_i2c_mux(struct amdgpu_atpx *atpx, u16 mux_id)
313 struct acpi_buffer params;
314 union acpi_object *info;
315 struct atpx_mux input;
317 if (atpx->functions.i2c_mux_cntl) {
320 params.length = input.size;
321 params.pointer = &input;
322 info = amdgpu_atpx_call(atpx->handle,
323 ATPX_FUNCTION_I2C_MUX_CONTROL,
333 * amdgpu_atpx_switch_start - notify the sbios of a GPU switch
335 * @atpx: atpx info struct
336 * @mux_id: mux state (0 = integrated GPU, 1 = discrete GPU)
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.
343 static int amdgpu_atpx_switch_start(struct amdgpu_atpx *atpx, u16 mux_id)
345 struct acpi_buffer params;
346 union acpi_object *info;
347 struct atpx_mux input;
349 if (atpx->functions.switch_start) {
352 params.length = input.size;
353 params.pointer = &input;
354 info = amdgpu_atpx_call(atpx->handle,
355 ATPX_FUNCTION_GRAPHICS_DEVICE_SWITCH_START_NOTIFICATION,
365 * amdgpu_atpx_switch_end - notify the sbios of a GPU switch
367 * @atpx: atpx info struct
368 * @mux_id: mux state (0 = integrated GPU, 1 = discrete GPU)
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.
375 static int amdgpu_atpx_switch_end(struct amdgpu_atpx *atpx, u16 mux_id)
377 struct acpi_buffer params;
378 union acpi_object *info;
379 struct atpx_mux input;
381 if (atpx->functions.switch_end) {
384 params.length = input.size;
385 params.pointer = &input;
386 info = amdgpu_atpx_call(atpx->handle,
387 ATPX_FUNCTION_GRAPHICS_DEVICE_SWITCH_END_NOTIFICATION,
397 * amdgpu_atpx_switchto - switch to the requested GPU
399 * @id: GPU to switch to
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.
405 static int amdgpu_atpx_switchto(enum vga_switcheroo_client_id id)
409 if (id == VGA_SWITCHEROO_IGD)
410 gpu_id = ATPX_INTEGRATED_GPU;
412 gpu_id = ATPX_DISCRETE_GPU;
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);
423 * amdgpu_atpx_power_state - power down/up the requested GPU
425 * @id: GPU to power down/up
426 * @state: requested power state (0 = off, 1 = on)
428 * Execute the necessary ATPX function to power down/up the discrete GPU
430 * Returns 0 on success, error on failure.
432 static int amdgpu_atpx_power_state(enum vga_switcheroo_client_id id,
433 enum vga_switcheroo_state state)
435 /* on w500 ACPI can't change intel gpu state */
436 if (id == VGA_SWITCHEROO_IGD)
439 amdgpu_atpx_set_discrete_state(&amdgpu_atpx_priv.atpx, state);
444 * amdgpu_atpx_pci_probe_handle - look up the ATPX handle
448 * Look up the ATPX handles (all asics).
449 * Returns true if the handles are found, false if not.
451 static bool amdgpu_atpx_pci_probe_handle(struct pci_dev *pdev)
453 acpi_handle dhandle, atpx_handle;
456 dhandle = ACPI_HANDLE(&pdev->dev);
460 status = acpi_get_handle(dhandle, "ATPX", &atpx_handle);
461 if (ACPI_FAILURE(status)) {
462 amdgpu_atpx_priv.other_handle = dhandle;
465 amdgpu_atpx_priv.dhandle = dhandle;
466 amdgpu_atpx_priv.atpx.handle = atpx_handle;
471 * amdgpu_atpx_init - verify the ATPX interface
473 * Verify the ATPX interface (all asics).
474 * Returns 0 on success, error on failure.
476 static int amdgpu_atpx_init(void)
480 /* set up the ATPX handle */
481 r = amdgpu_atpx_verify_interface(&amdgpu_atpx_priv.atpx);
485 /* validate the atpx setup */
486 r = amdgpu_atpx_validate(&amdgpu_atpx_priv.atpx);
494 * amdgpu_atpx_get_client_id - get the client id
498 * look up whether we are the integrated or discrete GPU (all asics).
499 * Returns the client id.
501 static int amdgpu_atpx_get_client_id(struct pci_dev *pdev)
503 if (amdgpu_atpx_priv.dhandle == ACPI_HANDLE(&pdev->dev))
504 return VGA_SWITCHEROO_IGD;
506 return VGA_SWITCHEROO_DIS;
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,
517 * amdgpu_atpx_detect - detect whether we have PX
519 * Check if we have a PX system (all asics).
520 * Returns true if we have a PX system, false if not.
522 static bool amdgpu_atpx_detect(void)
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;
530 while ((pdev = pci_get_class(PCI_CLASS_DISPLAY_VGA << 8, pdev)) != NULL) {
533 has_atpx |= (amdgpu_atpx_pci_probe_handle(pdev) == true);
536 while ((pdev = pci_get_class(PCI_CLASS_DISPLAY_OTHER << 8, pdev)) != NULL) {
539 has_atpx |= (amdgpu_atpx_pci_probe_handle(pdev) == true);
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",
546 amdgpu_atpx_priv.atpx_detected = true;
553 * amdgpu_register_atpx_handler - register with vga_switcheroo
555 * Register the PX callbacks with vga_switcheroo (all asics).
557 void amdgpu_register_atpx_handler(void)
561 /* detect if we have any ATPX + 2 VGA in the system */
562 r = amdgpu_atpx_detect();
566 vga_switcheroo_register_handler(&amdgpu_atpx_handler);
570 * amdgpu_unregister_atpx_handler - unregister with vga_switcheroo
572 * Unregister the PX callbacks with vga_switcheroo (all asics).
574 void amdgpu_unregister_atpx_handler(void)
576 vga_switcheroo_unregister_handler();