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 | 492b49a | 2012-08-16 14:07:37 -0400 | [diff] [blame^] | 46 | struct atpx_power_control { |
| 47 | u16 size; |
| 48 | u8 dgpu_state; |
| 49 | } __packed; |
| 50 | |
| 51 | struct atpx_mux { |
| 52 | u16 size; |
| 53 | u16 mux; |
| 54 | } __packed; |
| 55 | |
Alex Deucher | 48fa412 | 2012-08-16 14:01:57 -0400 | [diff] [blame] | 56 | /** |
| 57 | * radeon_atpx_call - call an ATPX method |
| 58 | * |
| 59 | * @handle: acpi handle |
| 60 | * @function: the ATPX function to execute |
| 61 | * @params: ATPX function params |
| 62 | * |
| 63 | * Executes the requested ATPX function (all asics). |
| 64 | * Returns a pointer to the acpi output buffer. |
| 65 | */ |
| 66 | static union acpi_object *radeon_atpx_call(acpi_handle handle, int function, |
| 67 | struct acpi_buffer *params) |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 68 | { |
| 69 | acpi_status status; |
Alex Deucher | 48fa412 | 2012-08-16 14:01:57 -0400 | [diff] [blame] | 70 | union acpi_object atpx_arg_elements[2]; |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 71 | struct acpi_object_list atpx_arg; |
| 72 | struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL }; |
| 73 | |
| 74 | atpx_arg.count = 2; |
| 75 | atpx_arg.pointer = &atpx_arg_elements[0]; |
| 76 | |
| 77 | atpx_arg_elements[0].type = ACPI_TYPE_INTEGER; |
Alex Deucher | 48fa412 | 2012-08-16 14:01:57 -0400 | [diff] [blame] | 78 | atpx_arg_elements[0].integer.value = function; |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 79 | |
Alex Deucher | 48fa412 | 2012-08-16 14:01:57 -0400 | [diff] [blame] | 80 | if (params) { |
| 81 | atpx_arg_elements[1].type = ACPI_TYPE_BUFFER; |
| 82 | atpx_arg_elements[1].buffer.length = params->length; |
| 83 | atpx_arg_elements[1].buffer.pointer = params->pointer; |
| 84 | } else { |
| 85 | /* We need a second fake parameter */ |
| 86 | atpx_arg_elements[1].type = ACPI_TYPE_INTEGER; |
| 87 | atpx_arg_elements[1].integer.value = 0; |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 88 | } |
Alex Deucher | 48fa412 | 2012-08-16 14:01:57 -0400 | [diff] [blame] | 89 | |
| 90 | status = acpi_evaluate_object(handle, "ATPX", &atpx_arg, &buffer); |
| 91 | |
| 92 | /* Fail only if calling the method fails and ATPX is supported */ |
| 93 | if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) { |
| 94 | printk("failed to evaluate ATPX got %s\n", |
| 95 | acpi_format_exception(status)); |
| 96 | kfree(buffer.pointer); |
| 97 | return NULL; |
| 98 | } |
| 99 | |
| 100 | return buffer.pointer; |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * radeon_atpx_parse_functions - parse supported functions |
| 105 | * |
| 106 | * @f: supported functions struct |
| 107 | * @mask: supported functions mask from ATPX |
| 108 | * |
| 109 | * Use the supported functions mask from ATPX function |
| 110 | * ATPX_FUNCTION_VERIFY_INTERFACE to determine what functions |
| 111 | * are supported (all asics). |
| 112 | */ |
| 113 | static void radeon_atpx_parse_functions(struct radeon_atpx_functions *f, u32 mask) |
| 114 | { |
| 115 | f->px_params = mask & ATPX_GET_PX_PARAMETERS_SUPPORTED; |
| 116 | f->power_cntl = mask & ATPX_POWER_CONTROL_SUPPORTED; |
| 117 | f->disp_mux_cntl = mask & ATPX_DISPLAY_MUX_CONTROL_SUPPORTED; |
| 118 | f->i2c_mux_cntl = mask & ATPX_I2C_MUX_CONTROL_SUPPORTED; |
| 119 | f->switch_start = mask & ATPX_GRAPHICS_DEVICE_SWITCH_START_NOTIFICATION_SUPPORTED; |
| 120 | f->switch_end = mask & ATPX_GRAPHICS_DEVICE_SWITCH_END_NOTIFICATION_SUPPORTED; |
| 121 | f->disp_connectors_mapping = mask & ATPX_GET_DISPLAY_CONNECTORS_MAPPING_SUPPORTED; |
| 122 | f->disp_detetion_ports = mask & ATPX_GET_DISPLAY_DETECTION_PORTS_SUPPORTED; |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * radeon_atpx_verify_interface - verify ATPX |
| 127 | * |
| 128 | * @handle: acpi handle |
| 129 | * @atpx: radeon atpx struct |
| 130 | * |
| 131 | * Execute the ATPX_FUNCTION_VERIFY_INTERFACE ATPX function |
| 132 | * to initialize ATPX and determine what features are supported |
| 133 | * (all asics). |
| 134 | * returns 0 on success, error on failure. |
| 135 | */ |
Alex Deucher | 492b49a | 2012-08-16 14:07:37 -0400 | [diff] [blame^] | 136 | static int radeon_atpx_verify_interface(struct radeon_atpx *atpx) |
Alex Deucher | 48fa412 | 2012-08-16 14:01:57 -0400 | [diff] [blame] | 137 | { |
| 138 | union acpi_object *info; |
| 139 | struct atpx_verify_interface output; |
| 140 | size_t size; |
| 141 | int err = 0; |
| 142 | |
Alex Deucher | 492b49a | 2012-08-16 14:07:37 -0400 | [diff] [blame^] | 143 | info = radeon_atpx_call(atpx->handle, ATPX_FUNCTION_VERIFY_INTERFACE, NULL); |
Alex Deucher | 48fa412 | 2012-08-16 14:01:57 -0400 | [diff] [blame] | 144 | if (!info) |
| 145 | return -EIO; |
| 146 | |
| 147 | memset(&output, 0, sizeof(output)); |
| 148 | |
| 149 | size = *(u16 *) info->buffer.pointer; |
| 150 | if (size < 8) { |
| 151 | printk("ATPX buffer is too small: %lu\n", size); |
| 152 | err = -EINVAL; |
| 153 | goto out; |
| 154 | } |
| 155 | size = min(sizeof(output), size); |
| 156 | |
| 157 | memcpy(&output, info->buffer.pointer, size); |
| 158 | |
| 159 | /* TODO: check version? */ |
| 160 | printk("ATPX version %u\n", output.version); |
| 161 | |
| 162 | radeon_atpx_parse_functions(&atpx->functions, output.function_bits); |
| 163 | |
| 164 | out: |
| 165 | kfree(info); |
| 166 | return err; |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 167 | } |
| 168 | |
Alex Deucher | 492b49a | 2012-08-16 14:07:37 -0400 | [diff] [blame^] | 169 | 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] | 170 | { |
Alex Deucher | 492b49a | 2012-08-16 14:07:37 -0400 | [diff] [blame^] | 171 | struct acpi_buffer params; |
| 172 | union acpi_object *info; |
| 173 | struct atpx_power_control input; |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 174 | |
Alex Deucher | 492b49a | 2012-08-16 14:07:37 -0400 | [diff] [blame^] | 175 | if (atpx->functions.power_cntl) { |
| 176 | input.size = 3; |
| 177 | input.dgpu_state = state; |
| 178 | params.length = input.size; |
| 179 | params.pointer = &input; |
| 180 | info = radeon_atpx_call(atpx->handle, |
| 181 | ATPX_FUNCTION_POWER_CONTROL, |
| 182 | ¶ms); |
| 183 | if (!info) |
| 184 | return -EIO; |
| 185 | kfree(info); |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 186 | } |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 187 | return 0; |
| 188 | } |
| 189 | |
Alex Deucher | 492b49a | 2012-08-16 14:07:37 -0400 | [diff] [blame^] | 190 | 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] | 191 | { |
Alex Deucher | 492b49a | 2012-08-16 14:07:37 -0400 | [diff] [blame^] | 192 | struct acpi_buffer params; |
| 193 | union acpi_object *info; |
| 194 | struct atpx_mux input; |
| 195 | |
| 196 | if (atpx->functions.disp_mux_cntl) { |
| 197 | input.size = 4; |
| 198 | input.mux = mux_id; |
| 199 | params.length = input.size; |
| 200 | params.pointer = &input; |
| 201 | info = radeon_atpx_call(atpx->handle, |
| 202 | ATPX_FUNCTION_DISPLAY_MUX_CONTROL, |
| 203 | ¶ms); |
| 204 | if (!info) |
| 205 | return -EIO; |
| 206 | kfree(info); |
| 207 | } |
| 208 | return 0; |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 209 | } |
| 210 | |
Alex Deucher | 492b49a | 2012-08-16 14:07:37 -0400 | [diff] [blame^] | 211 | 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] | 212 | { |
Alex Deucher | 492b49a | 2012-08-16 14:07:37 -0400 | [diff] [blame^] | 213 | struct acpi_buffer params; |
| 214 | union acpi_object *info; |
| 215 | struct atpx_mux input; |
| 216 | |
| 217 | if (atpx->functions.i2c_mux_cntl) { |
| 218 | input.size = 4; |
| 219 | input.mux = mux_id; |
| 220 | params.length = input.size; |
| 221 | params.pointer = &input; |
| 222 | info = radeon_atpx_call(atpx->handle, |
| 223 | ATPX_FUNCTION_I2C_MUX_CONTROL, |
| 224 | ¶ms); |
| 225 | if (!info) |
| 226 | return -EIO; |
| 227 | kfree(info); |
| 228 | } |
| 229 | return 0; |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 230 | } |
| 231 | |
Alex Deucher | 492b49a | 2012-08-16 14:07:37 -0400 | [diff] [blame^] | 232 | 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] | 233 | { |
Alex Deucher | 492b49a | 2012-08-16 14:07:37 -0400 | [diff] [blame^] | 234 | struct acpi_buffer params; |
| 235 | union acpi_object *info; |
| 236 | struct atpx_mux input; |
| 237 | |
| 238 | if (atpx->functions.switch_start) { |
| 239 | input.size = 4; |
| 240 | input.mux = mux_id; |
| 241 | params.length = input.size; |
| 242 | params.pointer = &input; |
| 243 | info = radeon_atpx_call(atpx->handle, |
| 244 | ATPX_FUNCTION_GRAPHICS_DEVICE_SWITCH_START_NOTIFICATION, |
| 245 | ¶ms); |
| 246 | if (!info) |
| 247 | return -EIO; |
| 248 | kfree(info); |
| 249 | } |
| 250 | return 0; |
Alex Deucher | 58e7381 | 2011-05-06 01:42:49 -0400 | [diff] [blame] | 251 | } |
| 252 | |
Alex Deucher | 492b49a | 2012-08-16 14:07:37 -0400 | [diff] [blame^] | 253 | 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] | 254 | { |
Alex Deucher | 492b49a | 2012-08-16 14:07:37 -0400 | [diff] [blame^] | 255 | struct acpi_buffer params; |
| 256 | union acpi_object *info; |
| 257 | struct atpx_mux input; |
Alex Deucher | 58e7381 | 2011-05-06 01:42:49 -0400 | [diff] [blame] | 258 | |
Alex Deucher | 492b49a | 2012-08-16 14:07:37 -0400 | [diff] [blame^] | 259 | if (atpx->functions.switch_end) { |
| 260 | input.size = 4; |
| 261 | input.mux = mux_id; |
| 262 | params.length = input.size; |
| 263 | params.pointer = &input; |
| 264 | info = radeon_atpx_call(atpx->handle, |
| 265 | ATPX_FUNCTION_GRAPHICS_DEVICE_SWITCH_END_NOTIFICATION, |
| 266 | ¶ms); |
| 267 | if (!info) |
| 268 | return -EIO; |
| 269 | kfree(info); |
| 270 | } |
| 271 | return 0; |
Alex Deucher | 58e7381 | 2011-05-06 01:42:49 -0400 | [diff] [blame] | 272 | } |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 273 | |
| 274 | static int radeon_atpx_switchto(enum vga_switcheroo_client_id id) |
| 275 | { |
Alex Deucher | 492b49a | 2012-08-16 14:07:37 -0400 | [diff] [blame^] | 276 | u16 gpu_id; |
Alex Deucher | 58e7381 | 2011-05-06 01:42:49 -0400 | [diff] [blame] | 277 | |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 278 | if (id == VGA_SWITCHEROO_IGD) |
Alex Deucher | 9e05b2f | 2012-07-18 13:28:52 -0400 | [diff] [blame] | 279 | gpu_id = ATPX_INTEGRATED_GPU; |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 280 | else |
Alex Deucher | 9e05b2f | 2012-07-18 13:28:52 -0400 | [diff] [blame] | 281 | gpu_id = ATPX_DISCRETE_GPU; |
Alex Deucher | 58e7381 | 2011-05-06 01:42:49 -0400 | [diff] [blame] | 282 | |
Alex Deucher | 492b49a | 2012-08-16 14:07:37 -0400 | [diff] [blame^] | 283 | radeon_atpx_switch_start(&radeon_atpx_priv.atpx, gpu_id); |
| 284 | radeon_atpx_switch_disp_mux(&radeon_atpx_priv.atpx, gpu_id); |
| 285 | radeon_atpx_switch_i2c_mux(&radeon_atpx_priv.atpx, gpu_id); |
| 286 | radeon_atpx_switch_end(&radeon_atpx_priv.atpx, gpu_id); |
Alex Deucher | 58e7381 | 2011-05-06 01:42:49 -0400 | [diff] [blame] | 287 | |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 288 | return 0; |
| 289 | } |
| 290 | |
| 291 | static int radeon_atpx_power_state(enum vga_switcheroo_client_id id, |
| 292 | enum vga_switcheroo_state state) |
| 293 | { |
| 294 | /* on w500 ACPI can't change intel gpu state */ |
| 295 | if (id == VGA_SWITCHEROO_IGD) |
| 296 | return 0; |
| 297 | |
Alex Deucher | 492b49a | 2012-08-16 14:07:37 -0400 | [diff] [blame^] | 298 | radeon_atpx_set_discrete_state(&radeon_atpx_priv.atpx, state); |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 299 | return 0; |
| 300 | } |
| 301 | |
| 302 | static bool radeon_atpx_pci_probe_handle(struct pci_dev *pdev) |
| 303 | { |
Alex Deucher | c61e277 | 2012-08-16 15:39:09 -0400 | [diff] [blame] | 304 | acpi_handle dhandle, atpx_handle; |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 305 | acpi_status status; |
| 306 | |
| 307 | dhandle = DEVICE_ACPI_HANDLE(&pdev->dev); |
| 308 | if (!dhandle) |
| 309 | return false; |
| 310 | |
| 311 | status = acpi_get_handle(dhandle, "ATPX", &atpx_handle); |
| 312 | if (ACPI_FAILURE(status)) |
| 313 | return false; |
| 314 | |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 315 | radeon_atpx_priv.dhandle = dhandle; |
Alex Deucher | 492b49a | 2012-08-16 14:07:37 -0400 | [diff] [blame^] | 316 | radeon_atpx_priv.atpx.handle = atpx_handle; |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 317 | return true; |
| 318 | } |
| 319 | |
| 320 | static int radeon_atpx_init(void) |
| 321 | { |
| 322 | /* set up the ATPX handle */ |
Alex Deucher | 492b49a | 2012-08-16 14:07:37 -0400 | [diff] [blame^] | 323 | return radeon_atpx_verify_interface(&radeon_atpx_priv.atpx); |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 324 | } |
| 325 | |
| 326 | static int radeon_atpx_get_client_id(struct pci_dev *pdev) |
| 327 | { |
| 328 | if (radeon_atpx_priv.dhandle == DEVICE_ACPI_HANDLE(&pdev->dev)) |
| 329 | return VGA_SWITCHEROO_IGD; |
| 330 | else |
| 331 | return VGA_SWITCHEROO_DIS; |
| 332 | } |
| 333 | |
| 334 | static struct vga_switcheroo_handler radeon_atpx_handler = { |
| 335 | .switchto = radeon_atpx_switchto, |
| 336 | .power_state = radeon_atpx_power_state, |
| 337 | .init = radeon_atpx_init, |
| 338 | .get_client_id = radeon_atpx_get_client_id, |
| 339 | }; |
| 340 | |
| 341 | static bool radeon_atpx_detect(void) |
| 342 | { |
| 343 | char acpi_method_name[255] = { 0 }; |
| 344 | struct acpi_buffer buffer = {sizeof(acpi_method_name), acpi_method_name}; |
| 345 | struct pci_dev *pdev = NULL; |
| 346 | bool has_atpx = false; |
| 347 | int vga_count = 0; |
| 348 | |
| 349 | while ((pdev = pci_get_class(PCI_CLASS_DISPLAY_VGA << 8, pdev)) != NULL) { |
| 350 | vga_count++; |
| 351 | |
| 352 | has_atpx |= (radeon_atpx_pci_probe_handle(pdev) == true); |
| 353 | } |
| 354 | |
| 355 | if (has_atpx && vga_count == 2) { |
Alex Deucher | 492b49a | 2012-08-16 14:07:37 -0400 | [diff] [blame^] | 356 | acpi_get_name(radeon_atpx_priv.atpx.handle, ACPI_FULL_PATHNAME, &buffer); |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 357 | printk(KERN_INFO "VGA switcheroo: detected switching method %s handle\n", |
| 358 | acpi_method_name); |
| 359 | radeon_atpx_priv.atpx_detected = true; |
| 360 | return true; |
| 361 | } |
| 362 | return false; |
| 363 | } |
| 364 | |
| 365 | void radeon_register_atpx_handler(void) |
| 366 | { |
| 367 | bool r; |
| 368 | |
| 369 | /* detect if we have any ATPX + 2 VGA in the system */ |
| 370 | r = radeon_atpx_detect(); |
| 371 | if (!r) |
| 372 | return; |
| 373 | |
| 374 | vga_switcheroo_register_handler(&radeon_atpx_handler); |
| 375 | } |
| 376 | |
| 377 | void radeon_unregister_atpx_handler(void) |
| 378 | { |
| 379 | vga_switcheroo_unregister_handler(); |
| 380 | } |