2 * Copyright (c) 2010 Red Hat Inc.
3 * Author : Dave Airlie <airlied@redhat.com>
7 * ATPX support for both Intel/ATI
9 * $FreeBSD: head/sys/dev/drm2/radeon/radeon_atpx_handler.c 254885 2013-08-25 19:37:15Z dumbbell $
12 #include <sys/param.h>
13 #include <sys/systm.h>
15 #include <sys/linker.h>
18 #include <uapi_drm/radeon_drm.h>
19 #include "radeon_acpi.h"
20 #include "radeon_drv.h"
23 struct radeon_atpx_functions {
30 bool disp_connectors_mapping;
31 bool disp_detetion_ports;
36 struct radeon_atpx_functions functions;
39 static struct radeon_atpx_priv {
41 /* handle for device - and atpx */
43 struct radeon_atpx atpx;
46 struct atpx_verify_interface {
47 u16 size; /* structure size in bytes (includes size field) */
48 u16 version; /* version */
49 u32 function_bits; /* supported functions bit vector */
52 struct atpx_power_control {
63 * radeon_atpx_call - call an ATPX method
65 * @handle: acpi handle
66 * @function: the ATPX function to execute
67 * @params: ATPX function params
69 * Executes the requested ATPX function (all asics).
70 * Returns a pointer to the acpi output buffer.
72 static ACPI_OBJECT *radeon_atpx_call(ACPI_HANDLE handle, int function,
76 ACPI_OBJECT atpx_arg_elements[2];
77 ACPI_OBJECT_LIST atpx_arg;
78 ACPI_BUFFER buffer = { ACPI_ALLOCATE_BUFFER, NULL };
81 atpx_arg.Pointer = &atpx_arg_elements[0];
83 atpx_arg_elements[0].Type = ACPI_TYPE_INTEGER;
84 atpx_arg_elements[0].Integer.Value = function;
87 atpx_arg_elements[1].Type = ACPI_TYPE_BUFFER;
88 atpx_arg_elements[1].Buffer.Length = params->Length;
89 atpx_arg_elements[1].Buffer.Pointer = params->Pointer;
91 /* We need a second fake parameter */
92 atpx_arg_elements[1].Type = ACPI_TYPE_INTEGER;
93 atpx_arg_elements[1].Integer.Value = 0;
96 status = AcpiEvaluateObject(handle, NULL, &atpx_arg, &buffer);
98 /* Fail only if calling the method fails and ATPX is supported */
99 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
100 DRM_ERROR("failed to evaluate ATPX got %s\n",
101 AcpiFormatException(status));
102 AcpiOsFree(buffer.Pointer);
106 return buffer.Pointer;
110 * radeon_atpx_parse_functions - parse supported functions
112 * @f: supported functions struct
113 * @mask: supported functions mask from ATPX
115 * Use the supported functions mask from ATPX function
116 * ATPX_FUNCTION_VERIFY_INTERFACE to determine what functions
117 * are supported (all asics).
119 static void radeon_atpx_parse_functions(struct radeon_atpx_functions *f, u32 mask)
121 f->px_params = mask & ATPX_GET_PX_PARAMETERS_SUPPORTED;
122 f->power_cntl = mask & ATPX_POWER_CONTROL_SUPPORTED;
123 f->disp_mux_cntl = mask & ATPX_DISPLAY_MUX_CONTROL_SUPPORTED;
124 f->i2c_mux_cntl = mask & ATPX_I2C_MUX_CONTROL_SUPPORTED;
125 f->switch_start = mask & ATPX_GRAPHICS_DEVICE_SWITCH_START_NOTIFICATION_SUPPORTED;
126 f->switch_end = mask & ATPX_GRAPHICS_DEVICE_SWITCH_END_NOTIFICATION_SUPPORTED;
127 f->disp_connectors_mapping = mask & ATPX_GET_DISPLAY_CONNECTORS_MAPPING_SUPPORTED;
128 f->disp_detetion_ports = mask & ATPX_GET_DISPLAY_DETECTION_PORTS_SUPPORTED;
132 * radeon_atpx_verify_interface - verify ATPX
134 * @handle: acpi handle
135 * @atpx: radeon atpx struct
137 * Execute the ATPX_FUNCTION_VERIFY_INTERFACE ATPX function
138 * to initialize ATPX and determine what features are supported
140 * returns 0 on success, error on failure.
142 static int radeon_atpx_verify_interface(struct radeon_atpx *atpx)
145 struct atpx_verify_interface output;
149 info = radeon_atpx_call(atpx->handle, ATPX_FUNCTION_VERIFY_INTERFACE, NULL);
153 memset(&output, 0, sizeof(output));
155 size = *(u16 *) info->Buffer.Pointer;
157 DRM_ERROR("ATPX buffer is too small: %zu\n", size);
161 size = min(sizeof(output), size);
163 memcpy(&output, info->Buffer.Pointer, size);
165 /* TODO: check version? */
166 DRM_INFO("ATPX version %u\n", output.version);
168 radeon_atpx_parse_functions(&atpx->functions, output.function_bits);
176 * radeon_atpx_set_discrete_state - power up/down discrete GPU
178 * @atpx: atpx info struct
179 * @state: discrete GPU state (0 = power down, 1 = power up)
181 * Execute the ATPX_FUNCTION_POWER_CONTROL ATPX function to
182 * power down/up the discrete GPU (all asics).
183 * Returns 0 on success, error on failure.
185 static int radeon_atpx_set_discrete_state(struct radeon_atpx *atpx, u8 state)
189 struct atpx_power_control input;
191 if (atpx->functions.power_cntl) {
193 input.dgpu_state = state;
194 params.Length = input.size;
195 params.Pointer = &input;
196 info = radeon_atpx_call(atpx->handle,
197 ATPX_FUNCTION_POWER_CONTROL,
207 * radeon_atpx_switch_disp_mux - switch display mux
209 * @atpx: atpx info struct
210 * @mux_id: mux state (0 = integrated GPU, 1 = discrete GPU)
212 * Execute the ATPX_FUNCTION_DISPLAY_MUX_CONTROL ATPX function to
213 * switch the display mux between the discrete GPU and integrated GPU
215 * Returns 0 on success, error on failure.
217 static int radeon_atpx_switch_disp_mux(struct radeon_atpx *atpx, u16 mux_id)
221 struct atpx_mux input;
223 if (atpx->functions.disp_mux_cntl) {
226 params.Length = input.size;
227 params.Pointer = &input;
228 info = radeon_atpx_call(atpx->handle,
229 ATPX_FUNCTION_DISPLAY_MUX_CONTROL,
239 * radeon_atpx_switch_i2c_mux - switch i2c/hpd mux
241 * @atpx: atpx info struct
242 * @mux_id: mux state (0 = integrated GPU, 1 = discrete GPU)
244 * Execute the ATPX_FUNCTION_I2C_MUX_CONTROL ATPX function to
245 * switch the i2c/hpd mux between the discrete GPU and integrated GPU
247 * Returns 0 on success, error on failure.
249 static int radeon_atpx_switch_i2c_mux(struct radeon_atpx *atpx, u16 mux_id)
253 struct atpx_mux input;
255 if (atpx->functions.i2c_mux_cntl) {
258 params.Length = input.size;
259 params.Pointer = &input;
260 info = radeon_atpx_call(atpx->handle,
261 ATPX_FUNCTION_I2C_MUX_CONTROL,
271 * radeon_atpx_switch_start - notify the sbios of a GPU switch
273 * @atpx: atpx info struct
274 * @mux_id: mux state (0 = integrated GPU, 1 = discrete GPU)
276 * Execute the ATPX_FUNCTION_GRAPHICS_DEVICE_SWITCH_START_NOTIFICATION ATPX
277 * function to notify the sbios that a switch between the discrete GPU and
278 * integrated GPU has begun (all asics).
279 * Returns 0 on success, error on failure.
281 static int radeon_atpx_switch_start(struct radeon_atpx *atpx, u16 mux_id)
285 struct atpx_mux input;
287 if (atpx->functions.switch_start) {
290 params.Length = input.size;
291 params.Pointer = &input;
292 info = radeon_atpx_call(atpx->handle,
293 ATPX_FUNCTION_GRAPHICS_DEVICE_SWITCH_START_NOTIFICATION,
303 * radeon_atpx_switch_end - notify the sbios of a GPU switch
305 * @atpx: atpx info struct
306 * @mux_id: mux state (0 = integrated GPU, 1 = discrete GPU)
308 * Execute the ATPX_FUNCTION_GRAPHICS_DEVICE_SWITCH_END_NOTIFICATION ATPX
309 * function to notify the sbios that a switch between the discrete GPU and
310 * integrated GPU has ended (all asics).
311 * Returns 0 on success, error on failure.
313 static int radeon_atpx_switch_end(struct radeon_atpx *atpx, u16 mux_id)
317 struct atpx_mux input;
319 if (atpx->functions.switch_end) {
322 params.Length = input.size;
323 params.Pointer = &input;
324 info = radeon_atpx_call(atpx->handle,
325 ATPX_FUNCTION_GRAPHICS_DEVICE_SWITCH_END_NOTIFICATION,
335 * radeon_atpx_switchto - switch to the requested GPU
337 * @id: GPU to switch to
339 * Execute the necessary ATPX functions to switch between the discrete GPU and
340 * integrated GPU (all asics).
341 * Returns 0 on success, error on failure.
343 static int radeon_atpx_switchto(enum vga_switcheroo_client_id id)
347 if (id == VGA_SWITCHEROO_IGD)
348 gpu_id = ATPX_INTEGRATED_GPU;
350 gpu_id = ATPX_DISCRETE_GPU;
352 radeon_atpx_switch_start(&radeon_atpx_priv.atpx, gpu_id);
353 radeon_atpx_switch_disp_mux(&radeon_atpx_priv.atpx, gpu_id);
354 radeon_atpx_switch_i2c_mux(&radeon_atpx_priv.atpx, gpu_id);
355 radeon_atpx_switch_end(&radeon_atpx_priv.atpx, gpu_id);
361 * radeon_atpx_power_state - power down/up the requested GPU
363 * @id: GPU to power down/up
364 * @state: requested power state (0 = off, 1 = on)
366 * Execute the necessary ATPX function to power down/up the discrete GPU
368 * Returns 0 on success, error on failure.
370 static int radeon_atpx_power_state(enum vga_switcheroo_client_id id,
371 enum vga_switcheroo_state state)
373 /* on w500 ACPI can't change intel gpu state */
374 if (id == VGA_SWITCHEROO_IGD)
377 radeon_atpx_set_discrete_state(&radeon_atpx_priv.atpx, state);
382 * radeon_atpx_pci_probe_handle - look up the ATPX handle
386 * Look up the ATPX handles (all asics).
387 * Returns true if the handles are found, false if not.
389 static bool radeon_atpx_pci_probe_handle(struct pci_dev *pdev)
391 ACPI_HANDLE dhandle, atpx_handle;
394 dhandle = DEVICE_ACPI_HANDLE(&pdev->dev);
398 status = AcpiGetHandle(dhandle, "ATPX", &atpx_handle);
399 if (ACPI_FAILURE(status))
402 radeon_atpx_priv.dhandle = dhandle;
403 radeon_atpx_priv.atpx.handle = atpx_handle;
408 * radeon_atpx_init - verify the ATPX interface
410 * Verify the ATPX interface (all asics).
411 * Returns 0 on success, error on failure.
413 static int radeon_atpx_init(void)
415 /* set up the ATPX handle */
416 return radeon_atpx_verify_interface(&radeon_atpx_priv.atpx);
420 * radeon_atpx_get_client_id - get the client id
424 * look up whether we are the integrated or discrete GPU (all asics).
425 * Returns the client id.
427 static int radeon_atpx_get_client_id(struct pci_dev *pdev)
429 if (radeon_atpx_priv.dhandle == DEVICE_ACPI_HANDLE(&pdev->dev))
430 return VGA_SWITCHEROO_IGD;
432 return VGA_SWITCHEROO_DIS;
435 static struct vga_switcheroo_handler radeon_atpx_handler = {
436 .switchto = radeon_atpx_switchto,
437 .power_state = radeon_atpx_power_state,
438 .init = radeon_atpx_init,
439 .get_client_id = radeon_atpx_get_client_id,
443 * radeon_atpx_detect - detect whether we have PX
445 * Check if we have a PX system (all asics).
446 * Returns true if we have a PX system, false if not.
448 static bool radeon_atpx_detect(void)
450 char acpi_method_name[255] = { 0 };
451 ACPI_BUFFER buffer = {sizeof(acpi_method_name), acpi_method_name};
452 struct pci_dev *pdev = NULL;
453 bool has_atpx = false;
456 while ((pdev = pci_get_class(PCI_CLASS_DISPLAY_VGA << 8, pdev)) != NULL) {
459 has_atpx |= (radeon_atpx_pci_probe_handle(pdev) == true);
462 if (has_atpx && vga_count == 2) {
463 AcpiGetName(radeon_atpx_priv.atpx.handle, ACPI_FULL_PATHNAME, &buffer);
464 DRM_INFO("VGA switcheroo: detected switching method %s handle\n",
466 radeon_atpx_priv.atpx_detected = true;
471 #endif /* DUMBBELL_WIP */
474 * radeon_register_atpx_handler - register with vga_switcheroo
476 * Register the PX callbacks with vga_switcheroo (all asics).
478 void radeon_register_atpx_handler(void)
483 /* detect if we have any ATPX + 2 VGA in the system */
484 r = radeon_atpx_detect();
488 vga_switcheroo_register_handler(&radeon_atpx_handler);
489 #endif /* DUMBBELL_WIP */
493 * radeon_unregister_atpx_handler - unregister with vga_switcheroo
495 * Unregister the PX callbacks with vga_switcheroo (all asics).
497 void radeon_unregister_atpx_handler(void)
500 vga_switcheroo_unregister_handler();
501 #endif /* DUMBBELL_WIP */