2 * Copyright (c) 2010 Red Hat Inc.
3 * Author : Dave Airlie <airlied@redhat.com>
7 * ATPX support for both Intel/ATI
10 #include <sys/cdefs.h>
11 __FBSDID("$FreeBSD$");
13 #include <sys/param.h>
14 #include <sys/systm.h>
16 #include <sys/linker.h>
18 #include <contrib/dev/acpica/include/acpi.h>
19 #include <dev/acpica/acpivar.h>
21 #include <dev/drm2/drmP.h>
22 #include <dev/drm2/radeon/radeon_drm.h>
23 #include "radeon_acpi.h"
24 #include "radeon_drv.h"
27 struct radeon_atpx_functions {
34 bool disp_connectors_mapping;
35 bool disp_detetion_ports;
40 struct radeon_atpx_functions functions;
43 static struct radeon_atpx_priv {
45 /* handle for device - and atpx */
47 struct radeon_atpx atpx;
50 struct atpx_verify_interface {
51 u16 size; /* structure size in bytes (includes size field) */
52 u16 version; /* version */
53 u32 function_bits; /* supported functions bit vector */
56 struct atpx_power_control {
67 * radeon_atpx_call - call an ATPX method
69 * @handle: acpi handle
70 * @function: the ATPX function to execute
71 * @params: ATPX function params
73 * Executes the requested ATPX function (all asics).
74 * Returns a pointer to the acpi output buffer.
76 static ACPI_OBJECT *radeon_atpx_call(ACPI_HANDLE handle, int function,
80 ACPI_OBJECT atpx_arg_elements[2];
81 ACPI_OBJECT_LIST atpx_arg;
82 ACPI_BUFFER buffer = { ACPI_ALLOCATE_BUFFER, NULL };
85 atpx_arg.Pointer = &atpx_arg_elements[0];
87 atpx_arg_elements[0].Type = ACPI_TYPE_INTEGER;
88 atpx_arg_elements[0].Integer.Value = function;
91 atpx_arg_elements[1].Type = ACPI_TYPE_BUFFER;
92 atpx_arg_elements[1].Buffer.Length = params->Length;
93 atpx_arg_elements[1].Buffer.Pointer = params->Pointer;
95 /* We need a second fake parameter */
96 atpx_arg_elements[1].Type = ACPI_TYPE_INTEGER;
97 atpx_arg_elements[1].Integer.Value = 0;
100 status = AcpiEvaluateObject(handle, NULL, &atpx_arg, &buffer);
102 /* Fail only if calling the method fails and ATPX is supported */
103 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
104 DRM_ERROR("failed to evaluate ATPX got %s\n",
105 AcpiFormatException(status));
106 AcpiOsFree(buffer.Pointer);
110 return buffer.Pointer;
114 * radeon_atpx_parse_functions - parse supported functions
116 * @f: supported functions struct
117 * @mask: supported functions mask from ATPX
119 * Use the supported functions mask from ATPX function
120 * ATPX_FUNCTION_VERIFY_INTERFACE to determine what functions
121 * are supported (all asics).
123 static void radeon_atpx_parse_functions(struct radeon_atpx_functions *f, u32 mask)
125 f->px_params = mask & ATPX_GET_PX_PARAMETERS_SUPPORTED;
126 f->power_cntl = mask & ATPX_POWER_CONTROL_SUPPORTED;
127 f->disp_mux_cntl = mask & ATPX_DISPLAY_MUX_CONTROL_SUPPORTED;
128 f->i2c_mux_cntl = mask & ATPX_I2C_MUX_CONTROL_SUPPORTED;
129 f->switch_start = mask & ATPX_GRAPHICS_DEVICE_SWITCH_START_NOTIFICATION_SUPPORTED;
130 f->switch_end = mask & ATPX_GRAPHICS_DEVICE_SWITCH_END_NOTIFICATION_SUPPORTED;
131 f->disp_connectors_mapping = mask & ATPX_GET_DISPLAY_CONNECTORS_MAPPING_SUPPORTED;
132 f->disp_detetion_ports = mask & ATPX_GET_DISPLAY_DETECTION_PORTS_SUPPORTED;
136 * radeon_atpx_verify_interface - verify ATPX
138 * @handle: acpi handle
139 * @atpx: radeon atpx struct
141 * Execute the ATPX_FUNCTION_VERIFY_INTERFACE ATPX function
142 * to initialize ATPX and determine what features are supported
144 * returns 0 on success, error on failure.
146 static int radeon_atpx_verify_interface(struct radeon_atpx *atpx)
149 struct atpx_verify_interface output;
153 info = radeon_atpx_call(atpx->handle, ATPX_FUNCTION_VERIFY_INTERFACE, NULL);
157 memset(&output, 0, sizeof(output));
159 size = *(u16 *) info->Buffer.Pointer;
161 DRM_ERROR("ATPX buffer is too small: %zu\n", size);
165 size = min(sizeof(output), size);
167 memcpy(&output, info->Buffer.Pointer, size);
169 /* TODO: check version? */
170 DRM_INFO("ATPX version %u\n", output.version);
172 radeon_atpx_parse_functions(&atpx->functions, output.function_bits);
180 * radeon_atpx_set_discrete_state - power up/down discrete GPU
182 * @atpx: atpx info struct
183 * @state: discrete GPU state (0 = power down, 1 = power up)
185 * Execute the ATPX_FUNCTION_POWER_CONTROL ATPX function to
186 * power down/up the discrete GPU (all asics).
187 * Returns 0 on success, error on failure.
189 static int radeon_atpx_set_discrete_state(struct radeon_atpx *atpx, u8 state)
193 struct atpx_power_control input;
195 if (atpx->functions.power_cntl) {
197 input.dgpu_state = state;
198 params.Length = input.size;
199 params.Pointer = &input;
200 info = radeon_atpx_call(atpx->handle,
201 ATPX_FUNCTION_POWER_CONTROL,
211 * radeon_atpx_switch_disp_mux - switch display mux
213 * @atpx: atpx info struct
214 * @mux_id: mux state (0 = integrated GPU, 1 = discrete GPU)
216 * Execute the ATPX_FUNCTION_DISPLAY_MUX_CONTROL ATPX function to
217 * switch the display mux between the discrete GPU and integrated GPU
219 * Returns 0 on success, error on failure.
221 static int radeon_atpx_switch_disp_mux(struct radeon_atpx *atpx, u16 mux_id)
225 struct atpx_mux input;
227 if (atpx->functions.disp_mux_cntl) {
230 params.Length = input.size;
231 params.Pointer = &input;
232 info = radeon_atpx_call(atpx->handle,
233 ATPX_FUNCTION_DISPLAY_MUX_CONTROL,
243 * radeon_atpx_switch_i2c_mux - switch i2c/hpd mux
245 * @atpx: atpx info struct
246 * @mux_id: mux state (0 = integrated GPU, 1 = discrete GPU)
248 * Execute the ATPX_FUNCTION_I2C_MUX_CONTROL ATPX function to
249 * switch the i2c/hpd mux between the discrete GPU and integrated GPU
251 * Returns 0 on success, error on failure.
253 static int radeon_atpx_switch_i2c_mux(struct radeon_atpx *atpx, u16 mux_id)
257 struct atpx_mux input;
259 if (atpx->functions.i2c_mux_cntl) {
262 params.Length = input.size;
263 params.Pointer = &input;
264 info = radeon_atpx_call(atpx->handle,
265 ATPX_FUNCTION_I2C_MUX_CONTROL,
275 * radeon_atpx_switch_start - notify the sbios of a GPU switch
277 * @atpx: atpx info struct
278 * @mux_id: mux state (0 = integrated GPU, 1 = discrete GPU)
280 * Execute the ATPX_FUNCTION_GRAPHICS_DEVICE_SWITCH_START_NOTIFICATION ATPX
281 * function to notify the sbios that a switch between the discrete GPU and
282 * integrated GPU has begun (all asics).
283 * Returns 0 on success, error on failure.
285 static int radeon_atpx_switch_start(struct radeon_atpx *atpx, u16 mux_id)
289 struct atpx_mux input;
291 if (atpx->functions.switch_start) {
294 params.Length = input.size;
295 params.Pointer = &input;
296 info = radeon_atpx_call(atpx->handle,
297 ATPX_FUNCTION_GRAPHICS_DEVICE_SWITCH_START_NOTIFICATION,
307 * radeon_atpx_switch_end - notify the sbios of a GPU switch
309 * @atpx: atpx info struct
310 * @mux_id: mux state (0 = integrated GPU, 1 = discrete GPU)
312 * Execute the ATPX_FUNCTION_GRAPHICS_DEVICE_SWITCH_END_NOTIFICATION ATPX
313 * function to notify the sbios that a switch between the discrete GPU and
314 * integrated GPU has ended (all asics).
315 * Returns 0 on success, error on failure.
317 static int radeon_atpx_switch_end(struct radeon_atpx *atpx, u16 mux_id)
321 struct atpx_mux input;
323 if (atpx->functions.switch_end) {
326 params.Length = input.size;
327 params.Pointer = &input;
328 info = radeon_atpx_call(atpx->handle,
329 ATPX_FUNCTION_GRAPHICS_DEVICE_SWITCH_END_NOTIFICATION,
339 * radeon_atpx_switchto - switch to the requested GPU
341 * @id: GPU to switch to
343 * Execute the necessary ATPX functions to switch between the discrete GPU and
344 * integrated GPU (all asics).
345 * Returns 0 on success, error on failure.
347 static int radeon_atpx_switchto(enum vga_switcheroo_client_id id)
351 if (id == VGA_SWITCHEROO_IGD)
352 gpu_id = ATPX_INTEGRATED_GPU;
354 gpu_id = ATPX_DISCRETE_GPU;
356 radeon_atpx_switch_start(&radeon_atpx_priv.atpx, gpu_id);
357 radeon_atpx_switch_disp_mux(&radeon_atpx_priv.atpx, gpu_id);
358 radeon_atpx_switch_i2c_mux(&radeon_atpx_priv.atpx, gpu_id);
359 radeon_atpx_switch_end(&radeon_atpx_priv.atpx, gpu_id);
365 * radeon_atpx_power_state - power down/up the requested GPU
367 * @id: GPU to power down/up
368 * @state: requested power state (0 = off, 1 = on)
370 * Execute the necessary ATPX function to power down/up the discrete GPU
372 * Returns 0 on success, error on failure.
374 static int radeon_atpx_power_state(enum vga_switcheroo_client_id id,
375 enum vga_switcheroo_state state)
377 /* on w500 ACPI can't change intel gpu state */
378 if (id == VGA_SWITCHEROO_IGD)
381 radeon_atpx_set_discrete_state(&radeon_atpx_priv.atpx, state);
386 * radeon_atpx_pci_probe_handle - look up the ATPX handle
390 * Look up the ATPX handles (all asics).
391 * Returns true if the handles are found, false if not.
393 static bool radeon_atpx_pci_probe_handle(struct pci_dev *pdev)
395 ACPI_HANDLE dhandle, atpx_handle;
398 dhandle = DEVICE_ACPI_HANDLE(&pdev->dev);
402 status = AcpiGetHandle(dhandle, "ATPX", &atpx_handle);
403 if (ACPI_FAILURE(status))
406 radeon_atpx_priv.dhandle = dhandle;
407 radeon_atpx_priv.atpx.handle = atpx_handle;
412 * radeon_atpx_init - verify the ATPX interface
414 * Verify the ATPX interface (all asics).
415 * Returns 0 on success, error on failure.
417 static int radeon_atpx_init(void)
419 /* set up the ATPX handle */
420 return radeon_atpx_verify_interface(&radeon_atpx_priv.atpx);
424 * radeon_atpx_get_client_id - get the client id
428 * look up whether we are the integrated or discrete GPU (all asics).
429 * Returns the client id.
431 static int radeon_atpx_get_client_id(struct pci_dev *pdev)
433 if (radeon_atpx_priv.dhandle == DEVICE_ACPI_HANDLE(&pdev->dev))
434 return VGA_SWITCHEROO_IGD;
436 return VGA_SWITCHEROO_DIS;
439 static struct vga_switcheroo_handler radeon_atpx_handler = {
440 .switchto = radeon_atpx_switchto,
441 .power_state = radeon_atpx_power_state,
442 .init = radeon_atpx_init,
443 .get_client_id = radeon_atpx_get_client_id,
447 * radeon_atpx_detect - detect whether we have PX
449 * Check if we have a PX system (all asics).
450 * Returns true if we have a PX system, false if not.
452 static bool radeon_atpx_detect(void)
454 char acpi_method_name[255] = { 0 };
455 ACPI_BUFFER buffer = {sizeof(acpi_method_name), acpi_method_name};
456 struct pci_dev *pdev = NULL;
457 bool has_atpx = false;
460 while ((pdev = pci_get_class(PCI_CLASS_DISPLAY_VGA << 8, pdev)) != NULL) {
463 has_atpx |= (radeon_atpx_pci_probe_handle(pdev) == true);
466 if (has_atpx && vga_count == 2) {
467 AcpiGetName(radeon_atpx_priv.atpx.handle, ACPI_FULL_PATHNAME, &buffer);
468 DRM_INFO("VGA switcheroo: detected switching method %s handle\n",
470 radeon_atpx_priv.atpx_detected = true;
475 #endif /* DUMBBELL_WIP */
478 * radeon_register_atpx_handler - register with vga_switcheroo
480 * Register the PX callbacks with vga_switcheroo (all asics).
482 void radeon_register_atpx_handler(void)
487 /* detect if we have any ATPX + 2 VGA in the system */
488 r = radeon_atpx_detect();
492 vga_switcheroo_register_handler(&radeon_atpx_handler);
493 #endif /* DUMBBELL_WIP */
497 * radeon_unregister_atpx_handler - unregister with vga_switcheroo
499 * Unregister the PX callbacks with vga_switcheroo (all asics).
501 void radeon_unregister_atpx_handler(void)
504 vga_switcheroo_unregister_handler();
505 #endif /* DUMBBELL_WIP */