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