2 * Copyright (c) 2010 Red Hat Inc.
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>
17 struct amdgpu_atpx_functions {
24 bool disp_connectors_mapping;
25 bool disp_detetion_ports;
30 struct amdgpu_atpx_functions functions;
34 static struct amdgpu_atpx_priv {
36 /* handle for device - and atpx */
38 acpi_handle other_handle;
39 struct amdgpu_atpx atpx;
42 struct atpx_verify_interface {
43 u16 size; /* structure size in bytes (includes size field) */
44 u16 version; /* version */
45 u32 function_bits; /* supported functions bit vector */
48 struct atpx_px_params {
49 u16 size; /* structure size in bytes (includes size field) */
50 u32 valid_flags; /* which flags are valid */
51 u32 flags; /* flags */
54 struct atpx_power_control {
64 bool amdgpu_has_atpx(void) {
65 return amdgpu_atpx_priv.atpx_detected;
68 bool amdgpu_has_atpx_dgpu_power_cntl(void) {
69 return amdgpu_atpx_priv.atpx.functions.power_cntl;
72 bool amdgpu_is_atpx_hybrid(void) {
73 return amdgpu_atpx_priv.atpx.is_hybrid;
77 * amdgpu_atpx_call - call an ATPX method
79 * @handle: acpi handle
80 * @function: the ATPX function to execute
81 * @params: ATPX function params
83 * Executes the requested ATPX function (all asics).
84 * Returns a pointer to the acpi output buffer.
86 static union acpi_object *amdgpu_atpx_call(acpi_handle handle, int function,
87 struct acpi_buffer *params)
90 union acpi_object atpx_arg_elements[2];
91 struct acpi_object_list atpx_arg;
92 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
95 atpx_arg.pointer = &atpx_arg_elements[0];
97 atpx_arg_elements[0].type = ACPI_TYPE_INTEGER;
98 atpx_arg_elements[0].integer.value = function;
101 atpx_arg_elements[1].type = ACPI_TYPE_BUFFER;
102 atpx_arg_elements[1].buffer.length = params->length;
103 atpx_arg_elements[1].buffer.pointer = params->pointer;
105 /* We need a second fake parameter */
106 atpx_arg_elements[1].type = ACPI_TYPE_INTEGER;
107 atpx_arg_elements[1].integer.value = 0;
110 status = acpi_evaluate_object(handle, NULL, &atpx_arg, &buffer);
112 /* Fail only if calling the method fails and ATPX is supported */
113 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
114 printk("failed to evaluate ATPX got %s\n",
115 acpi_format_exception(status));
116 kfree(buffer.pointer);
120 return buffer.pointer;
124 * amdgpu_atpx_parse_functions - parse supported functions
126 * @f: supported functions struct
127 * @mask: supported functions mask from ATPX
129 * Use the supported functions mask from ATPX function
130 * ATPX_FUNCTION_VERIFY_INTERFACE to determine what functions
131 * are supported (all asics).
133 static void amdgpu_atpx_parse_functions(struct amdgpu_atpx_functions *f, u32 mask)
135 f->px_params = mask & ATPX_GET_PX_PARAMETERS_SUPPORTED;
136 f->power_cntl = mask & ATPX_POWER_CONTROL_SUPPORTED;
137 f->disp_mux_cntl = mask & ATPX_DISPLAY_MUX_CONTROL_SUPPORTED;
138 f->i2c_mux_cntl = mask & ATPX_I2C_MUX_CONTROL_SUPPORTED;
139 f->switch_start = mask & ATPX_GRAPHICS_DEVICE_SWITCH_START_NOTIFICATION_SUPPORTED;
140 f->switch_end = mask & ATPX_GRAPHICS_DEVICE_SWITCH_END_NOTIFICATION_SUPPORTED;
141 f->disp_connectors_mapping = mask & ATPX_GET_DISPLAY_CONNECTORS_MAPPING_SUPPORTED;
142 f->disp_detetion_ports = mask & ATPX_GET_DISPLAY_DETECTION_PORTS_SUPPORTED;
146 * amdgpu_atpx_validate_functions - validate ATPX functions
148 * @atpx: amdgpu atpx struct
150 * Validate that required functions are enabled (all asics).
151 * returns 0 on success, error on failure.
153 static int amdgpu_atpx_validate(struct amdgpu_atpx *atpx)
157 if (atpx->functions.px_params) {
158 union acpi_object *info;
159 struct atpx_px_params output;
162 info = amdgpu_atpx_call(atpx->handle, ATPX_FUNCTION_GET_PX_PARAMETERS, NULL);
166 memset(&output, 0, sizeof(output));
168 size = *(u16 *) info->buffer.pointer;
170 printk("ATPX buffer is too small: %zu\n", size);
174 size = min(sizeof(output), size);
176 memcpy(&output, info->buffer.pointer, size);
178 valid_bits = output.flags & output.valid_flags;
183 /* if separate mux flag is set, mux controls are required */
184 if (valid_bits & ATPX_SEPARATE_MUX_FOR_I2C) {
185 atpx->functions.i2c_mux_cntl = true;
186 atpx->functions.disp_mux_cntl = true;
188 /* if any outputs are muxed, mux controls are required */
189 if (valid_bits & (ATPX_CRT1_RGB_SIGNAL_MUXED |
190 ATPX_TV_SIGNAL_MUXED |
191 ATPX_DFP_SIGNAL_MUXED))
192 atpx->functions.disp_mux_cntl = true;
195 /* some bioses set these bits rather than flagging power_cntl as supported */
196 if (valid_bits & (ATPX_DYNAMIC_PX_SUPPORTED |
197 ATPX_DYNAMIC_DGPU_POWER_OFF_SUPPORTED))
198 atpx->functions.power_cntl = true;
200 atpx->is_hybrid = false;
201 if (valid_bits & ATPX_MS_HYBRID_GFX_SUPPORTED) {
202 printk("ATPX Hybrid Graphics\n");
203 atpx->functions.power_cntl = false;
204 atpx->is_hybrid = true;
211 * amdgpu_atpx_verify_interface - verify ATPX
213 * @atpx: amdgpu atpx struct
215 * Execute the ATPX_FUNCTION_VERIFY_INTERFACE ATPX function
216 * to initialize ATPX and determine what features are supported
218 * returns 0 on success, error on failure.
220 static int amdgpu_atpx_verify_interface(struct amdgpu_atpx *atpx)
222 union acpi_object *info;
223 struct atpx_verify_interface output;
227 info = amdgpu_atpx_call(atpx->handle, ATPX_FUNCTION_VERIFY_INTERFACE, NULL);
231 memset(&output, 0, sizeof(output));
233 size = *(u16 *) info->buffer.pointer;
235 printk("ATPX buffer is too small: %zu\n", size);
239 size = min(sizeof(output), size);
241 memcpy(&output, info->buffer.pointer, size);
243 /* TODO: check version? */
244 printk("ATPX version %u, functions 0x%08x\n",
245 output.version, output.function_bits);
247 amdgpu_atpx_parse_functions(&atpx->functions, output.function_bits);
255 * amdgpu_atpx_set_discrete_state - power up/down discrete GPU
257 * @atpx: atpx info struct
258 * @state: discrete GPU state (0 = power down, 1 = power up)
260 * Execute the ATPX_FUNCTION_POWER_CONTROL ATPX function to
261 * power down/up the discrete GPU (all asics).
262 * Returns 0 on success, error on failure.
264 static int amdgpu_atpx_set_discrete_state(struct amdgpu_atpx *atpx, u8 state)
266 struct acpi_buffer params;
267 union acpi_object *info;
268 struct atpx_power_control input;
270 if (atpx->functions.power_cntl) {
272 input.dgpu_state = state;
273 params.length = input.size;
274 params.pointer = &input;
275 info = amdgpu_atpx_call(atpx->handle,
276 ATPX_FUNCTION_POWER_CONTROL,
282 /* 200ms delay is required after off */
290 * amdgpu_atpx_switch_disp_mux - switch display mux
292 * @atpx: atpx info struct
293 * @mux_id: mux state (0 = integrated GPU, 1 = discrete GPU)
295 * Execute the ATPX_FUNCTION_DISPLAY_MUX_CONTROL ATPX function to
296 * switch the display mux between the discrete GPU and integrated GPU
298 * Returns 0 on success, error on failure.
300 static int amdgpu_atpx_switch_disp_mux(struct amdgpu_atpx *atpx, u16 mux_id)
302 struct acpi_buffer params;
303 union acpi_object *info;
304 struct atpx_mux input;
306 if (atpx->functions.disp_mux_cntl) {
309 params.length = input.size;
310 params.pointer = &input;
311 info = amdgpu_atpx_call(atpx->handle,
312 ATPX_FUNCTION_DISPLAY_MUX_CONTROL,
322 * amdgpu_atpx_switch_i2c_mux - switch i2c/hpd mux
324 * @atpx: atpx info struct
325 * @mux_id: mux state (0 = integrated GPU, 1 = discrete GPU)
327 * Execute the ATPX_FUNCTION_I2C_MUX_CONTROL ATPX function to
328 * switch the i2c/hpd mux between the discrete GPU and integrated GPU
330 * Returns 0 on success, error on failure.
332 static int amdgpu_atpx_switch_i2c_mux(struct amdgpu_atpx *atpx, u16 mux_id)
334 struct acpi_buffer params;
335 union acpi_object *info;
336 struct atpx_mux input;
338 if (atpx->functions.i2c_mux_cntl) {
341 params.length = input.size;
342 params.pointer = &input;
343 info = amdgpu_atpx_call(atpx->handle,
344 ATPX_FUNCTION_I2C_MUX_CONTROL,
354 * amdgpu_atpx_switch_start - 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_START_NOTIFICATION ATPX
360 * function to notify the sbios that a switch between the discrete GPU and
361 * integrated GPU has begun (all asics).
362 * Returns 0 on success, error on failure.
364 static int amdgpu_atpx_switch_start(struct amdgpu_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_start) {
373 params.length = input.size;
374 params.pointer = &input;
375 info = amdgpu_atpx_call(atpx->handle,
376 ATPX_FUNCTION_GRAPHICS_DEVICE_SWITCH_START_NOTIFICATION,
386 * amdgpu_atpx_switch_end - notify the sbios of a GPU switch
388 * @atpx: atpx info struct
389 * @mux_id: mux state (0 = integrated GPU, 1 = discrete GPU)
391 * Execute the ATPX_FUNCTION_GRAPHICS_DEVICE_SWITCH_END_NOTIFICATION ATPX
392 * function to notify the sbios that a switch between the discrete GPU and
393 * integrated GPU has ended (all asics).
394 * Returns 0 on success, error on failure.
396 static int amdgpu_atpx_switch_end(struct amdgpu_atpx *atpx, u16 mux_id)
398 struct acpi_buffer params;
399 union acpi_object *info;
400 struct atpx_mux input;
402 if (atpx->functions.switch_end) {
405 params.length = input.size;
406 params.pointer = &input;
407 info = amdgpu_atpx_call(atpx->handle,
408 ATPX_FUNCTION_GRAPHICS_DEVICE_SWITCH_END_NOTIFICATION,
418 * amdgpu_atpx_switchto - switch to the requested GPU
420 * @id: GPU to switch to
422 * Execute the necessary ATPX functions to switch between the discrete GPU and
423 * integrated GPU (all asics).
424 * Returns 0 on success, error on failure.
426 static int amdgpu_atpx_switchto(enum vga_switcheroo_client_id id)
430 if (id == VGA_SWITCHEROO_IGD)
431 gpu_id = ATPX_INTEGRATED_GPU;
433 gpu_id = ATPX_DISCRETE_GPU;
435 amdgpu_atpx_switch_start(&amdgpu_atpx_priv.atpx, gpu_id);
436 amdgpu_atpx_switch_disp_mux(&amdgpu_atpx_priv.atpx, gpu_id);
437 amdgpu_atpx_switch_i2c_mux(&amdgpu_atpx_priv.atpx, gpu_id);
438 amdgpu_atpx_switch_end(&amdgpu_atpx_priv.atpx, gpu_id);
444 * amdgpu_atpx_power_state - power down/up the requested GPU
446 * @id: GPU to power down/up
447 * @state: requested power state (0 = off, 1 = on)
449 * Execute the necessary ATPX function to power down/up the discrete GPU
451 * Returns 0 on success, error on failure.
453 static int amdgpu_atpx_power_state(enum vga_switcheroo_client_id id,
454 enum vga_switcheroo_state state)
456 /* on w500 ACPI can't change intel gpu state */
457 if (id == VGA_SWITCHEROO_IGD)
460 amdgpu_atpx_set_discrete_state(&amdgpu_atpx_priv.atpx, state);
465 * amdgpu_atpx_pci_probe_handle - look up the ATPX handle
469 * Look up the ATPX handles (all asics).
470 * Returns true if the handles are found, false if not.
472 static bool amdgpu_atpx_pci_probe_handle(struct pci_dev *pdev)
474 acpi_handle dhandle, atpx_handle;
477 dhandle = ACPI_HANDLE(&pdev->dev);
481 status = acpi_get_handle(dhandle, "ATPX", &atpx_handle);
482 if (ACPI_FAILURE(status)) {
483 amdgpu_atpx_priv.other_handle = dhandle;
486 amdgpu_atpx_priv.dhandle = dhandle;
487 amdgpu_atpx_priv.atpx.handle = atpx_handle;
492 * amdgpu_atpx_init - verify the ATPX interface
494 * Verify the ATPX interface (all asics).
495 * Returns 0 on success, error on failure.
497 static int amdgpu_atpx_init(void)
501 /* set up the ATPX handle */
502 r = amdgpu_atpx_verify_interface(&amdgpu_atpx_priv.atpx);
506 /* validate the atpx setup */
507 r = amdgpu_atpx_validate(&amdgpu_atpx_priv.atpx);
515 * amdgpu_atpx_get_client_id - get the client id
519 * look up whether we are the integrated or discrete GPU (all asics).
520 * Returns the client id.
522 static int amdgpu_atpx_get_client_id(struct pci_dev *pdev)
524 if (amdgpu_atpx_priv.dhandle == ACPI_HANDLE(&pdev->dev))
525 return VGA_SWITCHEROO_IGD;
527 return VGA_SWITCHEROO_DIS;
530 static const struct vga_switcheroo_handler amdgpu_atpx_handler = {
531 .switchto = amdgpu_atpx_switchto,
532 .power_state = amdgpu_atpx_power_state,
533 .get_client_id = amdgpu_atpx_get_client_id,
537 * amdgpu_atpx_detect - detect whether we have PX
539 * Check if we have a PX system (all asics).
540 * Returns true if we have a PX system, false if not.
542 static bool amdgpu_atpx_detect(void)
544 char acpi_method_name[255] = { 0 };
545 struct acpi_buffer buffer = {sizeof(acpi_method_name), acpi_method_name};
546 struct pci_dev *pdev = NULL;
547 bool has_atpx = false;
550 while ((pdev = pci_get_class(PCI_CLASS_DISPLAY_VGA << 8, pdev)) != NULL) {
553 has_atpx |= (amdgpu_atpx_pci_probe_handle(pdev) == true);
556 while ((pdev = pci_get_class(PCI_CLASS_DISPLAY_OTHER << 8, pdev)) != NULL) {
559 has_atpx |= (amdgpu_atpx_pci_probe_handle(pdev) == true);
562 if (has_atpx && vga_count == 2) {
563 acpi_get_name(amdgpu_atpx_priv.atpx.handle, ACPI_FULL_PATHNAME, &buffer);
564 printk(KERN_INFO "vga_switcheroo: detected switching method %s handle\n",
566 amdgpu_atpx_priv.atpx_detected = true;
574 * amdgpu_register_atpx_handler - register with vga_switcheroo
576 * Register the PX callbacks with vga_switcheroo (all asics).
578 void amdgpu_register_atpx_handler(void)
581 enum vga_switcheroo_handler_flags_t handler_flags = 0;
583 /* detect if we have any ATPX + 2 VGA in the system */
584 r = amdgpu_atpx_detect();
588 vga_switcheroo_register_handler(&amdgpu_atpx_handler, handler_flags);
592 * amdgpu_unregister_atpx_handler - unregister with vga_switcheroo
594 * Unregister the PX callbacks with vga_switcheroo (all asics).
596 void amdgpu_unregister_atpx_handler(void)
598 vga_switcheroo_unregister_handler();