Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2010 Red Hat Inc. |
| 3 | * Author : Dave Airlie <airlied@redhat.com> |
| 4 | * |
| 5 | * Licensed under GPLv2 |
| 6 | * |
| 7 | * ATPX support for both Intel/ATI |
| 8 | */ |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 9 | #include <linux/vga_switcheroo.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 10 | #include <linux/slab.h> |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 11 | #include <acpi/acpi.h> |
| 12 | #include <acpi/acpi_bus.h> |
| 13 | #include <linux/pci.h> |
| 14 | |
Alex Deucher | 9e05b2f | 2012-07-18 13:28:52 -0400 | [diff] [blame] | 15 | #include "radeon_acpi.h" |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 16 | |
Alex Deucher | 48fa412 | 2012-08-16 14:01:57 -0400 | [diff] [blame] | 17 | struct radeon_atpx_functions { |
| 18 | bool px_params; |
| 19 | bool power_cntl; |
| 20 | bool disp_mux_cntl; |
| 21 | bool i2c_mux_cntl; |
| 22 | bool switch_start; |
| 23 | bool switch_end; |
| 24 | bool disp_connectors_mapping; |
| 25 | bool disp_detetion_ports; |
| 26 | }; |
| 27 | |
| 28 | struct radeon_atpx { |
Alex Deucher | 492b49a | 2012-08-16 14:07:37 -0400 | [diff] [blame] | 29 | acpi_handle handle; |
Alex Deucher | 48fa412 | 2012-08-16 14:01:57 -0400 | [diff] [blame] | 30 | struct radeon_atpx_functions functions; |
| 31 | }; |
| 32 | |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 33 | static struct radeon_atpx_priv { |
| 34 | bool atpx_detected; |
| 35 | /* handle for device - and atpx */ |
| 36 | acpi_handle dhandle; |
Alex Deucher | 48fa412 | 2012-08-16 14:01:57 -0400 | [diff] [blame] | 37 | struct radeon_atpx atpx; |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 38 | } radeon_atpx_priv; |
| 39 | |
Alex Deucher | 48fa412 | 2012-08-16 14:01:57 -0400 | [diff] [blame] | 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 */ |
| 44 | } __packed; |
| 45 | |
Alex Deucher | 43a23aa | 2013-02-19 12:55:52 -0500 | [diff] [blame] | 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 */ |
| 50 | } __packed; |
| 51 | |
Alex Deucher | 492b49a | 2012-08-16 14:07:37 -0400 | [diff] [blame] | 52 | struct atpx_power_control { |
| 53 | u16 size; |
| 54 | u8 dgpu_state; |
| 55 | } __packed; |
| 56 | |
| 57 | struct atpx_mux { |
| 58 | u16 size; |
| 59 | u16 mux; |
| 60 | } __packed; |
| 61 | |
Alex Deucher | 48fa412 | 2012-08-16 14:01:57 -0400 | [diff] [blame] | 62 | /** |
| 63 | * radeon_atpx_call - call an ATPX method |
| 64 | * |
| 65 | * @handle: acpi handle |
| 66 | * @function: the ATPX function to execute |
| 67 | * @params: ATPX function params |
| 68 | * |
| 69 | * Executes the requested ATPX function (all asics). |
| 70 | * Returns a pointer to the acpi output buffer. |
| 71 | */ |
| 72 | static union acpi_object *radeon_atpx_call(acpi_handle handle, int function, |
| 73 | struct acpi_buffer *params) |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 74 | { |
| 75 | acpi_status status; |
Alex Deucher | 48fa412 | 2012-08-16 14:01:57 -0400 | [diff] [blame] | 76 | union acpi_object atpx_arg_elements[2]; |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 77 | struct acpi_object_list atpx_arg; |
| 78 | struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL }; |
| 79 | |
| 80 | atpx_arg.count = 2; |
| 81 | atpx_arg.pointer = &atpx_arg_elements[0]; |
| 82 | |
| 83 | atpx_arg_elements[0].type = ACPI_TYPE_INTEGER; |
Alex Deucher | 48fa412 | 2012-08-16 14:01:57 -0400 | [diff] [blame] | 84 | atpx_arg_elements[0].integer.value = function; |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 85 | |
Alex Deucher | 48fa412 | 2012-08-16 14:01:57 -0400 | [diff] [blame] | 86 | if (params) { |
| 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; |
| 90 | } else { |
| 91 | /* We need a second fake parameter */ |
| 92 | atpx_arg_elements[1].type = ACPI_TYPE_INTEGER; |
| 93 | atpx_arg_elements[1].integer.value = 0; |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 94 | } |
Alex Deucher | 48fa412 | 2012-08-16 14:01:57 -0400 | [diff] [blame] | 95 | |
Alex Deucher | 0b90365 | 2012-10-23 17:57:54 -0400 | [diff] [blame] | 96 | status = acpi_evaluate_object(handle, NULL, &atpx_arg, &buffer); |
Alex Deucher | 48fa412 | 2012-08-16 14:01:57 -0400 | [diff] [blame] | 97 | |
| 98 | /* Fail only if calling the method fails and ATPX is supported */ |
| 99 | if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) { |
| 100 | printk("failed to evaluate ATPX got %s\n", |
| 101 | acpi_format_exception(status)); |
| 102 | kfree(buffer.pointer); |
| 103 | return NULL; |
| 104 | } |
| 105 | |
| 106 | return buffer.pointer; |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * radeon_atpx_parse_functions - parse supported functions |
| 111 | * |
| 112 | * @f: supported functions struct |
| 113 | * @mask: supported functions mask from ATPX |
| 114 | * |
| 115 | * Use the supported functions mask from ATPX function |
| 116 | * ATPX_FUNCTION_VERIFY_INTERFACE to determine what functions |
| 117 | * are supported (all asics). |
| 118 | */ |
| 119 | static void radeon_atpx_parse_functions(struct radeon_atpx_functions *f, u32 mask) |
| 120 | { |
| 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; |
| 129 | } |
| 130 | |
| 131 | /** |
Alex Deucher | 43a23aa | 2013-02-19 12:55:52 -0500 | [diff] [blame] | 132 | * radeon_atpx_validate_functions - validate ATPX functions |
| 133 | * |
| 134 | * @atpx: radeon atpx struct |
| 135 | * |
| 136 | * Validate that required functions are enabled (all asics). |
| 137 | * returns 0 on success, error on failure. |
| 138 | */ |
| 139 | static int radeon_atpx_validate(struct radeon_atpx *atpx) |
| 140 | { |
| 141 | /* make sure required functions are enabled */ |
| 142 | /* dGPU power control is required */ |
| 143 | atpx->functions.power_cntl = true; |
| 144 | |
| 145 | if (atpx->functions.px_params) { |
| 146 | union acpi_object *info; |
| 147 | struct atpx_px_params output; |
| 148 | size_t size; |
| 149 | u32 valid_bits; |
| 150 | |
| 151 | info = radeon_atpx_call(atpx->handle, ATPX_FUNCTION_GET_PX_PARAMETERS, NULL); |
| 152 | if (!info) |
| 153 | return -EIO; |
| 154 | |
| 155 | memset(&output, 0, sizeof(output)); |
| 156 | |
| 157 | size = *(u16 *) info->buffer.pointer; |
| 158 | if (size < 10) { |
| 159 | printk("ATPX buffer is too small: %zu\n", size); |
| 160 | kfree(info); |
| 161 | return -EINVAL; |
| 162 | } |
| 163 | size = min(sizeof(output), size); |
| 164 | |
| 165 | memcpy(&output, info->buffer.pointer, size); |
| 166 | |
| 167 | valid_bits = output.flags & output.valid_flags; |
| 168 | /* if separate mux flag is set, mux controls are required */ |
| 169 | if (valid_bits & ATPX_SEPARATE_MUX_FOR_I2C) { |
| 170 | atpx->functions.i2c_mux_cntl = true; |
| 171 | atpx->functions.disp_mux_cntl = true; |
| 172 | } |
| 173 | /* if any outputs are muxed, mux controls are required */ |
| 174 | if (valid_bits & (ATPX_CRT1_RGB_SIGNAL_MUXED | |
| 175 | ATPX_TV_SIGNAL_MUXED | |
| 176 | ATPX_DFP_SIGNAL_MUXED)) |
| 177 | atpx->functions.disp_mux_cntl = true; |
| 178 | |
| 179 | kfree(info); |
| 180 | } |
| 181 | return 0; |
| 182 | } |
| 183 | |
| 184 | /** |
Alex Deucher | 48fa412 | 2012-08-16 14:01:57 -0400 | [diff] [blame] | 185 | * radeon_atpx_verify_interface - verify ATPX |
| 186 | * |
Alex Deucher | 48fa412 | 2012-08-16 14:01:57 -0400 | [diff] [blame] | 187 | * @atpx: radeon atpx struct |
| 188 | * |
| 189 | * Execute the ATPX_FUNCTION_VERIFY_INTERFACE ATPX function |
| 190 | * to initialize ATPX and determine what features are supported |
| 191 | * (all asics). |
| 192 | * returns 0 on success, error on failure. |
| 193 | */ |
Alex Deucher | 492b49a | 2012-08-16 14:07:37 -0400 | [diff] [blame] | 194 | static int radeon_atpx_verify_interface(struct radeon_atpx *atpx) |
Alex Deucher | 48fa412 | 2012-08-16 14:01:57 -0400 | [diff] [blame] | 195 | { |
| 196 | union acpi_object *info; |
| 197 | struct atpx_verify_interface output; |
| 198 | size_t size; |
| 199 | int err = 0; |
| 200 | |
Alex Deucher | 492b49a | 2012-08-16 14:07:37 -0400 | [diff] [blame] | 201 | info = radeon_atpx_call(atpx->handle, ATPX_FUNCTION_VERIFY_INTERFACE, NULL); |
Alex Deucher | 48fa412 | 2012-08-16 14:01:57 -0400 | [diff] [blame] | 202 | if (!info) |
| 203 | return -EIO; |
| 204 | |
| 205 | memset(&output, 0, sizeof(output)); |
| 206 | |
| 207 | size = *(u16 *) info->buffer.pointer; |
| 208 | if (size < 8) { |
Randy Dunlap | bd6126b | 2012-10-16 10:15:45 +1000 | [diff] [blame] | 209 | printk("ATPX buffer is too small: %zu\n", size); |
Alex Deucher | 48fa412 | 2012-08-16 14:01:57 -0400 | [diff] [blame] | 210 | err = -EINVAL; |
| 211 | goto out; |
| 212 | } |
| 213 | size = min(sizeof(output), size); |
| 214 | |
| 215 | memcpy(&output, info->buffer.pointer, size); |
| 216 | |
| 217 | /* TODO: check version? */ |
Alex Deucher | 6a57734 | 2014-02-20 09:16:01 -0500 | [diff] [blame] | 218 | printk("ATPX version %u, functions 0x%08x\n", |
| 219 | output.version, output.function_bits); |
Alex Deucher | 48fa412 | 2012-08-16 14:01:57 -0400 | [diff] [blame] | 220 | |
| 221 | radeon_atpx_parse_functions(&atpx->functions, output.function_bits); |
| 222 | |
| 223 | out: |
| 224 | kfree(info); |
| 225 | return err; |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 226 | } |
| 227 | |
Alex Deucher | 82e0293 | 2012-08-16 14:09:21 -0400 | [diff] [blame] | 228 | /** |
| 229 | * radeon_atpx_set_discrete_state - power up/down discrete GPU |
| 230 | * |
| 231 | * @atpx: atpx info struct |
| 232 | * @state: discrete GPU state (0 = power down, 1 = power up) |
| 233 | * |
| 234 | * Execute the ATPX_FUNCTION_POWER_CONTROL ATPX function to |
| 235 | * power down/up the discrete GPU (all asics). |
| 236 | * Returns 0 on success, error on failure. |
| 237 | */ |
Alex Deucher | 492b49a | 2012-08-16 14:07:37 -0400 | [diff] [blame] | 238 | static int radeon_atpx_set_discrete_state(struct radeon_atpx *atpx, u8 state) |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 239 | { |
Alex Deucher | 492b49a | 2012-08-16 14:07:37 -0400 | [diff] [blame] | 240 | struct acpi_buffer params; |
| 241 | union acpi_object *info; |
| 242 | struct atpx_power_control input; |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 243 | |
Alex Deucher | 492b49a | 2012-08-16 14:07:37 -0400 | [diff] [blame] | 244 | if (atpx->functions.power_cntl) { |
| 245 | input.size = 3; |
| 246 | input.dgpu_state = state; |
| 247 | params.length = input.size; |
| 248 | params.pointer = &input; |
| 249 | info = radeon_atpx_call(atpx->handle, |
| 250 | ATPX_FUNCTION_POWER_CONTROL, |
| 251 | ¶ms); |
| 252 | if (!info) |
| 253 | return -EIO; |
| 254 | kfree(info); |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 255 | } |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 256 | return 0; |
| 257 | } |
| 258 | |
Alex Deucher | 82e0293 | 2012-08-16 14:09:21 -0400 | [diff] [blame] | 259 | /** |
| 260 | * radeon_atpx_switch_disp_mux - switch display mux |
| 261 | * |
| 262 | * @atpx: atpx info struct |
| 263 | * @mux_id: mux state (0 = integrated GPU, 1 = discrete GPU) |
| 264 | * |
| 265 | * Execute the ATPX_FUNCTION_DISPLAY_MUX_CONTROL ATPX function to |
| 266 | * switch the display mux between the discrete GPU and integrated GPU |
| 267 | * (all asics). |
| 268 | * Returns 0 on success, error on failure. |
| 269 | */ |
Alex Deucher | 492b49a | 2012-08-16 14:07:37 -0400 | [diff] [blame] | 270 | static int radeon_atpx_switch_disp_mux(struct radeon_atpx *atpx, u16 mux_id) |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 271 | { |
Alex Deucher | 492b49a | 2012-08-16 14:07:37 -0400 | [diff] [blame] | 272 | struct acpi_buffer params; |
| 273 | union acpi_object *info; |
| 274 | struct atpx_mux input; |
| 275 | |
| 276 | if (atpx->functions.disp_mux_cntl) { |
| 277 | input.size = 4; |
| 278 | input.mux = mux_id; |
| 279 | params.length = input.size; |
| 280 | params.pointer = &input; |
| 281 | info = radeon_atpx_call(atpx->handle, |
| 282 | ATPX_FUNCTION_DISPLAY_MUX_CONTROL, |
| 283 | ¶ms); |
| 284 | if (!info) |
| 285 | return -EIO; |
| 286 | kfree(info); |
| 287 | } |
| 288 | return 0; |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 289 | } |
| 290 | |
Alex Deucher | 82e0293 | 2012-08-16 14:09:21 -0400 | [diff] [blame] | 291 | /** |
| 292 | * radeon_atpx_switch_i2c_mux - switch i2c/hpd mux |
| 293 | * |
| 294 | * @atpx: atpx info struct |
| 295 | * @mux_id: mux state (0 = integrated GPU, 1 = discrete GPU) |
| 296 | * |
| 297 | * Execute the ATPX_FUNCTION_I2C_MUX_CONTROL ATPX function to |
| 298 | * switch the i2c/hpd mux between the discrete GPU and integrated GPU |
| 299 | * (all asics). |
| 300 | * Returns 0 on success, error on failure. |
| 301 | */ |
Alex Deucher | 492b49a | 2012-08-16 14:07:37 -0400 | [diff] [blame] | 302 | static int radeon_atpx_switch_i2c_mux(struct radeon_atpx *atpx, u16 mux_id) |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 303 | { |
Alex Deucher | 492b49a | 2012-08-16 14:07:37 -0400 | [diff] [blame] | 304 | struct acpi_buffer params; |
| 305 | union acpi_object *info; |
| 306 | struct atpx_mux input; |
| 307 | |
| 308 | if (atpx->functions.i2c_mux_cntl) { |
| 309 | input.size = 4; |
| 310 | input.mux = mux_id; |
| 311 | params.length = input.size; |
| 312 | params.pointer = &input; |
| 313 | info = radeon_atpx_call(atpx->handle, |
| 314 | ATPX_FUNCTION_I2C_MUX_CONTROL, |
| 315 | ¶ms); |
| 316 | if (!info) |
| 317 | return -EIO; |
| 318 | kfree(info); |
| 319 | } |
| 320 | return 0; |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 321 | } |
| 322 | |
Alex Deucher | 82e0293 | 2012-08-16 14:09:21 -0400 | [diff] [blame] | 323 | /** |
| 324 | * radeon_atpx_switch_start - notify the sbios of a GPU switch |
| 325 | * |
| 326 | * @atpx: atpx info struct |
| 327 | * @mux_id: mux state (0 = integrated GPU, 1 = discrete GPU) |
| 328 | * |
| 329 | * Execute the ATPX_FUNCTION_GRAPHICS_DEVICE_SWITCH_START_NOTIFICATION ATPX |
| 330 | * function to notify the sbios that a switch between the discrete GPU and |
| 331 | * integrated GPU has begun (all asics). |
| 332 | * Returns 0 on success, error on failure. |
| 333 | */ |
Alex Deucher | 492b49a | 2012-08-16 14:07:37 -0400 | [diff] [blame] | 334 | static int radeon_atpx_switch_start(struct radeon_atpx *atpx, u16 mux_id) |
Alex Deucher | 58e7381 | 2011-05-06 01:42:49 -0400 | [diff] [blame] | 335 | { |
Alex Deucher | 492b49a | 2012-08-16 14:07:37 -0400 | [diff] [blame] | 336 | struct acpi_buffer params; |
| 337 | union acpi_object *info; |
| 338 | struct atpx_mux input; |
| 339 | |
| 340 | if (atpx->functions.switch_start) { |
| 341 | input.size = 4; |
| 342 | input.mux = mux_id; |
| 343 | params.length = input.size; |
| 344 | params.pointer = &input; |
| 345 | info = radeon_atpx_call(atpx->handle, |
| 346 | ATPX_FUNCTION_GRAPHICS_DEVICE_SWITCH_START_NOTIFICATION, |
| 347 | ¶ms); |
| 348 | if (!info) |
| 349 | return -EIO; |
| 350 | kfree(info); |
| 351 | } |
| 352 | return 0; |
Alex Deucher | 58e7381 | 2011-05-06 01:42:49 -0400 | [diff] [blame] | 353 | } |
| 354 | |
Alex Deucher | 82e0293 | 2012-08-16 14:09:21 -0400 | [diff] [blame] | 355 | /** |
| 356 | * radeon_atpx_switch_end - notify the sbios of a GPU switch |
| 357 | * |
| 358 | * @atpx: atpx info struct |
| 359 | * @mux_id: mux state (0 = integrated GPU, 1 = discrete GPU) |
| 360 | * |
| 361 | * Execute the ATPX_FUNCTION_GRAPHICS_DEVICE_SWITCH_END_NOTIFICATION ATPX |
| 362 | * function to notify the sbios that a switch between the discrete GPU and |
| 363 | * integrated GPU has ended (all asics). |
| 364 | * Returns 0 on success, error on failure. |
| 365 | */ |
Alex Deucher | 492b49a | 2012-08-16 14:07:37 -0400 | [diff] [blame] | 366 | static int radeon_atpx_switch_end(struct radeon_atpx *atpx, u16 mux_id) |
Alex Deucher | 58e7381 | 2011-05-06 01:42:49 -0400 | [diff] [blame] | 367 | { |
Alex Deucher | 492b49a | 2012-08-16 14:07:37 -0400 | [diff] [blame] | 368 | struct acpi_buffer params; |
| 369 | union acpi_object *info; |
| 370 | struct atpx_mux input; |
Alex Deucher | 58e7381 | 2011-05-06 01:42:49 -0400 | [diff] [blame] | 371 | |
Alex Deucher | 492b49a | 2012-08-16 14:07:37 -0400 | [diff] [blame] | 372 | if (atpx->functions.switch_end) { |
| 373 | input.size = 4; |
| 374 | input.mux = mux_id; |
| 375 | params.length = input.size; |
| 376 | params.pointer = &input; |
| 377 | info = radeon_atpx_call(atpx->handle, |
| 378 | ATPX_FUNCTION_GRAPHICS_DEVICE_SWITCH_END_NOTIFICATION, |
| 379 | ¶ms); |
| 380 | if (!info) |
| 381 | return -EIO; |
| 382 | kfree(info); |
| 383 | } |
| 384 | return 0; |
Alex Deucher | 58e7381 | 2011-05-06 01:42:49 -0400 | [diff] [blame] | 385 | } |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 386 | |
Alex Deucher | 82e0293 | 2012-08-16 14:09:21 -0400 | [diff] [blame] | 387 | /** |
| 388 | * radeon_atpx_switchto - switch to the requested GPU |
| 389 | * |
| 390 | * @id: GPU to switch to |
| 391 | * |
| 392 | * Execute the necessary ATPX functions to switch between the discrete GPU and |
| 393 | * integrated GPU (all asics). |
| 394 | * Returns 0 on success, error on failure. |
| 395 | */ |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 396 | static int radeon_atpx_switchto(enum vga_switcheroo_client_id id) |
| 397 | { |
Alex Deucher | 492b49a | 2012-08-16 14:07:37 -0400 | [diff] [blame] | 398 | u16 gpu_id; |
Alex Deucher | 58e7381 | 2011-05-06 01:42:49 -0400 | [diff] [blame] | 399 | |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 400 | if (id == VGA_SWITCHEROO_IGD) |
Alex Deucher | 9e05b2f | 2012-07-18 13:28:52 -0400 | [diff] [blame] | 401 | gpu_id = ATPX_INTEGRATED_GPU; |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 402 | else |
Alex Deucher | 9e05b2f | 2012-07-18 13:28:52 -0400 | [diff] [blame] | 403 | gpu_id = ATPX_DISCRETE_GPU; |
Alex Deucher | 58e7381 | 2011-05-06 01:42:49 -0400 | [diff] [blame] | 404 | |
Alex Deucher | 492b49a | 2012-08-16 14:07:37 -0400 | [diff] [blame] | 405 | radeon_atpx_switch_start(&radeon_atpx_priv.atpx, gpu_id); |
| 406 | radeon_atpx_switch_disp_mux(&radeon_atpx_priv.atpx, gpu_id); |
| 407 | radeon_atpx_switch_i2c_mux(&radeon_atpx_priv.atpx, gpu_id); |
| 408 | radeon_atpx_switch_end(&radeon_atpx_priv.atpx, gpu_id); |
Alex Deucher | 58e7381 | 2011-05-06 01:42:49 -0400 | [diff] [blame] | 409 | |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 410 | return 0; |
| 411 | } |
| 412 | |
Alex Deucher | 82e0293 | 2012-08-16 14:09:21 -0400 | [diff] [blame] | 413 | /** |
Igor Murzov | dfdcbeb | 2012-10-25 17:09:00 +0400 | [diff] [blame] | 414 | * radeon_atpx_power_state - power down/up the requested GPU |
Alex Deucher | 82e0293 | 2012-08-16 14:09:21 -0400 | [diff] [blame] | 415 | * |
Igor Murzov | dfdcbeb | 2012-10-25 17:09:00 +0400 | [diff] [blame] | 416 | * @id: GPU to power down/up |
Alex Deucher | 82e0293 | 2012-08-16 14:09:21 -0400 | [diff] [blame] | 417 | * @state: requested power state (0 = off, 1 = on) |
| 418 | * |
| 419 | * Execute the necessary ATPX function to power down/up the discrete GPU |
| 420 | * (all asics). |
| 421 | * Returns 0 on success, error on failure. |
| 422 | */ |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 423 | static int radeon_atpx_power_state(enum vga_switcheroo_client_id id, |
| 424 | enum vga_switcheroo_state state) |
| 425 | { |
| 426 | /* on w500 ACPI can't change intel gpu state */ |
| 427 | if (id == VGA_SWITCHEROO_IGD) |
| 428 | return 0; |
| 429 | |
Alex Deucher | 492b49a | 2012-08-16 14:07:37 -0400 | [diff] [blame] | 430 | radeon_atpx_set_discrete_state(&radeon_atpx_priv.atpx, state); |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 431 | return 0; |
| 432 | } |
| 433 | |
Alex Deucher | 82e0293 | 2012-08-16 14:09:21 -0400 | [diff] [blame] | 434 | /** |
Alex Deucher | c9bd773 | 2012-10-23 12:42:42 -0400 | [diff] [blame] | 435 | * radeon_atpx_pci_probe_handle - look up the ATPX handle |
Alex Deucher | 82e0293 | 2012-08-16 14:09:21 -0400 | [diff] [blame] | 436 | * |
| 437 | * @pdev: pci device |
| 438 | * |
Alex Deucher | c9bd773 | 2012-10-23 12:42:42 -0400 | [diff] [blame] | 439 | * Look up the ATPX handles (all asics). |
Alex Deucher | 82e0293 | 2012-08-16 14:09:21 -0400 | [diff] [blame] | 440 | * Returns true if the handles are found, false if not. |
| 441 | */ |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 442 | static bool radeon_atpx_pci_probe_handle(struct pci_dev *pdev) |
| 443 | { |
Alex Deucher | c61e277 | 2012-08-16 15:39:09 -0400 | [diff] [blame] | 444 | acpi_handle dhandle, atpx_handle; |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 445 | acpi_status status; |
| 446 | |
| 447 | dhandle = DEVICE_ACPI_HANDLE(&pdev->dev); |
| 448 | if (!dhandle) |
| 449 | return false; |
| 450 | |
| 451 | status = acpi_get_handle(dhandle, "ATPX", &atpx_handle); |
| 452 | if (ACPI_FAILURE(status)) |
| 453 | return false; |
| 454 | |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 455 | radeon_atpx_priv.dhandle = dhandle; |
Alex Deucher | 492b49a | 2012-08-16 14:07:37 -0400 | [diff] [blame] | 456 | radeon_atpx_priv.atpx.handle = atpx_handle; |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 457 | return true; |
| 458 | } |
| 459 | |
Alex Deucher | 82e0293 | 2012-08-16 14:09:21 -0400 | [diff] [blame] | 460 | /** |
| 461 | * radeon_atpx_init - verify the ATPX interface |
| 462 | * |
| 463 | * Verify the ATPX interface (all asics). |
| 464 | * Returns 0 on success, error on failure. |
| 465 | */ |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 466 | static int radeon_atpx_init(void) |
| 467 | { |
Alex Deucher | 43a23aa | 2013-02-19 12:55:52 -0500 | [diff] [blame] | 468 | int r; |
| 469 | |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 470 | /* set up the ATPX handle */ |
Alex Deucher | 43a23aa | 2013-02-19 12:55:52 -0500 | [diff] [blame] | 471 | r = radeon_atpx_verify_interface(&radeon_atpx_priv.atpx); |
| 472 | if (r) |
| 473 | return r; |
| 474 | |
| 475 | /* validate the atpx setup */ |
| 476 | r = radeon_atpx_validate(&radeon_atpx_priv.atpx); |
| 477 | if (r) |
| 478 | return r; |
| 479 | |
| 480 | return 0; |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 481 | } |
| 482 | |
Alex Deucher | 82e0293 | 2012-08-16 14:09:21 -0400 | [diff] [blame] | 483 | /** |
| 484 | * radeon_atpx_get_client_id - get the client id |
| 485 | * |
| 486 | * @pdev: pci device |
| 487 | * |
| 488 | * look up whether we are the integrated or discrete GPU (all asics). |
| 489 | * Returns the client id. |
| 490 | */ |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 491 | static int radeon_atpx_get_client_id(struct pci_dev *pdev) |
| 492 | { |
| 493 | if (radeon_atpx_priv.dhandle == DEVICE_ACPI_HANDLE(&pdev->dev)) |
| 494 | return VGA_SWITCHEROO_IGD; |
| 495 | else |
| 496 | return VGA_SWITCHEROO_DIS; |
| 497 | } |
| 498 | |
| 499 | static struct vga_switcheroo_handler radeon_atpx_handler = { |
| 500 | .switchto = radeon_atpx_switchto, |
| 501 | .power_state = radeon_atpx_power_state, |
| 502 | .init = radeon_atpx_init, |
| 503 | .get_client_id = radeon_atpx_get_client_id, |
| 504 | }; |
| 505 | |
Alex Deucher | 82e0293 | 2012-08-16 14:09:21 -0400 | [diff] [blame] | 506 | /** |
| 507 | * radeon_atpx_detect - detect whether we have PX |
| 508 | * |
| 509 | * Check if we have a PX system (all asics). |
| 510 | * Returns true if we have a PX system, false if not. |
| 511 | */ |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 512 | static bool radeon_atpx_detect(void) |
| 513 | { |
| 514 | char acpi_method_name[255] = { 0 }; |
| 515 | struct acpi_buffer buffer = {sizeof(acpi_method_name), acpi_method_name}; |
| 516 | struct pci_dev *pdev = NULL; |
| 517 | bool has_atpx = false; |
| 518 | int vga_count = 0; |
| 519 | |
| 520 | while ((pdev = pci_get_class(PCI_CLASS_DISPLAY_VGA << 8, pdev)) != NULL) { |
| 521 | vga_count++; |
| 522 | |
| 523 | has_atpx |= (radeon_atpx_pci_probe_handle(pdev) == true); |
| 524 | } |
| 525 | |
Alex Deucher | 7f69542 | 2014-04-15 12:44:34 -0400 | [diff] [blame] | 526 | /* some newer PX laptops mark the dGPU as a non-VGA display device */ |
| 527 | while ((pdev = pci_get_class(PCI_CLASS_DISPLAY_OTHER << 8, pdev)) != NULL) { |
| 528 | vga_count++; |
| 529 | |
| 530 | has_atpx |= (radeon_atpx_pci_probe_handle(pdev) == true); |
| 531 | } |
| 532 | |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 533 | if (has_atpx && vga_count == 2) { |
Alex Deucher | 492b49a | 2012-08-16 14:07:37 -0400 | [diff] [blame] | 534 | acpi_get_name(radeon_atpx_priv.atpx.handle, ACPI_FULL_PATHNAME, &buffer); |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 535 | printk(KERN_INFO "VGA switcheroo: detected switching method %s handle\n", |
| 536 | acpi_method_name); |
| 537 | radeon_atpx_priv.atpx_detected = true; |
| 538 | return true; |
| 539 | } |
| 540 | return false; |
| 541 | } |
| 542 | |
Alex Deucher | 82e0293 | 2012-08-16 14:09:21 -0400 | [diff] [blame] | 543 | /** |
| 544 | * radeon_register_atpx_handler - register with vga_switcheroo |
| 545 | * |
| 546 | * Register the PX callbacks with vga_switcheroo (all asics). |
| 547 | */ |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 548 | void radeon_register_atpx_handler(void) |
| 549 | { |
| 550 | bool r; |
| 551 | |
| 552 | /* detect if we have any ATPX + 2 VGA in the system */ |
| 553 | r = radeon_atpx_detect(); |
| 554 | if (!r) |
| 555 | return; |
| 556 | |
| 557 | vga_switcheroo_register_handler(&radeon_atpx_handler); |
| 558 | } |
| 559 | |
Alex Deucher | 82e0293 | 2012-08-16 14:09:21 -0400 | [diff] [blame] | 560 | /** |
| 561 | * radeon_unregister_atpx_handler - unregister with vga_switcheroo |
| 562 | * |
| 563 | * Unregister the PX callbacks with vga_switcheroo (all asics). |
| 564 | */ |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 565 | void radeon_unregister_atpx_handler(void) |
| 566 | { |
| 567 | vga_switcheroo_unregister_handler(); |
| 568 | } |