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