Alex Deucher | efd4e41 | 2012-07-31 17:18:17 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2012 Advanced Micro Devices, Inc. |
| 3 | * |
| 4 | * Permission is hereby granted, free of charge, to any person obtaining a |
| 5 | * copy of this software and associated documentation files (the "Software"), |
| 6 | * to deal in the Software without restriction, including without limitation |
| 7 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 8 | * and/or sell copies of the Software, and to permit persons to whom the |
| 9 | * Software is furnished to do so, subject to the following conditions: |
| 10 | * |
| 11 | * The above copyright notice and this permission notice shall be included in |
| 12 | * all copies or substantial portions of the Software. |
| 13 | * |
| 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 17 | * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR |
| 18 | * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, |
| 19 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
| 20 | * OTHER DEALINGS IN THE SOFTWARE. |
| 21 | * |
| 22 | */ |
| 23 | |
Alberto Milone | d7a2952 | 2010-07-06 11:40:24 -0400 | [diff] [blame] | 24 | #include <linux/pci.h> |
| 25 | #include <linux/acpi.h> |
| 26 | #include <linux/slab.h> |
| 27 | #include <acpi/acpi_drivers.h> |
| 28 | #include <acpi/acpi_bus.h> |
Luca Tettamanti | fda4b25 | 2012-07-30 21:20:35 +0200 | [diff] [blame^] | 29 | #include <acpi/video.h> |
Alberto Milone | d7a2952 | 2010-07-06 11:40:24 -0400 | [diff] [blame] | 30 | |
| 31 | #include "drmP.h" |
| 32 | #include "drm.h" |
| 33 | #include "drm_sarea.h" |
| 34 | #include "drm_crtc_helper.h" |
| 35 | #include "radeon.h" |
Alex Deucher | 9e05b2f | 2012-07-18 13:28:52 -0400 | [diff] [blame] | 36 | #include "radeon_acpi.h" |
Luca Tettamanti | fda4b25 | 2012-07-30 21:20:35 +0200 | [diff] [blame^] | 37 | #include "atom.h" |
Alberto Milone | d7a2952 | 2010-07-06 11:40:24 -0400 | [diff] [blame] | 38 | |
| 39 | #include <linux/vga_switcheroo.h> |
| 40 | |
Luca Tettamanti | fd64ca8 | 2012-08-16 11:11:18 -0400 | [diff] [blame] | 41 | struct atif_verify_interface { |
| 42 | u16 size; /* structure size in bytes (includes size field) */ |
| 43 | u16 version; /* version */ |
| 44 | u32 notification_mask; /* supported notifications mask */ |
| 45 | u32 function_bits; /* supported functions bit vector */ |
| 46 | } __packed; |
| 47 | |
Luca Tettamanti | ce3cf82 | 2012-07-30 21:16:06 +0200 | [diff] [blame] | 48 | struct atif_system_params { |
Luca Tettamanti | fda4b25 | 2012-07-30 21:20:35 +0200 | [diff] [blame^] | 49 | u16 size; /* structure size in bytes (includes size field) */ |
| 50 | u32 valid_mask; /* valid flags mask */ |
| 51 | u32 flags; /* flags */ |
| 52 | u8 command_code; /* notify command code */ |
| 53 | } __packed; |
| 54 | |
| 55 | struct atif_sbios_requests { |
| 56 | u16 size; /* structure size in bytes (includes size field) */ |
| 57 | u32 pending; /* pending sbios requests */ |
| 58 | u8 panel_exp_mode; /* panel expansion mode */ |
| 59 | u8 thermal_gfx; /* thermal state: target gfx controller */ |
| 60 | u8 thermal_state; /* thermal state: state id (0: exit state, non-0: state) */ |
| 61 | u8 forced_power_gfx; /* forced power state: target gfx controller */ |
| 62 | u8 forced_power_state; /* forced power state: state id */ |
| 63 | u8 system_power_src; /* system power source */ |
| 64 | u8 backlight_level; /* panel backlight level (0-255) */ |
Luca Tettamanti | ce3cf82 | 2012-07-30 21:16:06 +0200 | [diff] [blame] | 65 | } __packed; |
| 66 | |
| 67 | #define ATIF_NOTIFY_MASK 0x3 |
| 68 | #define ATIF_NOTIFY_NONE 0 |
| 69 | #define ATIF_NOTIFY_81 1 |
| 70 | #define ATIF_NOTIFY_N 2 |
| 71 | |
Alberto Milone | d7a2952 | 2010-07-06 11:40:24 -0400 | [diff] [blame] | 72 | /* Call the ATIF method |
Alberto Milone | d7a2952 | 2010-07-06 11:40:24 -0400 | [diff] [blame] | 73 | */ |
Luca Tettamanti | 8650467 | 2012-07-29 17:04:43 +0200 | [diff] [blame] | 74 | static union acpi_object *radeon_atif_call(acpi_handle handle, int function, |
| 75 | struct acpi_buffer *params) |
Alberto Milone | d7a2952 | 2010-07-06 11:40:24 -0400 | [diff] [blame] | 76 | { |
| 77 | acpi_status status; |
| 78 | union acpi_object atif_arg_elements[2]; |
| 79 | struct acpi_object_list atif_arg; |
Luca Tettamanti | 8650467 | 2012-07-29 17:04:43 +0200 | [diff] [blame] | 80 | struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL }; |
Alberto Milone | d7a2952 | 2010-07-06 11:40:24 -0400 | [diff] [blame] | 81 | |
| 82 | atif_arg.count = 2; |
| 83 | atif_arg.pointer = &atif_arg_elements[0]; |
| 84 | |
| 85 | atif_arg_elements[0].type = ACPI_TYPE_INTEGER; |
Luca Tettamanti | 8650467 | 2012-07-29 17:04:43 +0200 | [diff] [blame] | 86 | atif_arg_elements[0].integer.value = function; |
| 87 | |
| 88 | if (params) { |
| 89 | atif_arg_elements[1].type = ACPI_TYPE_BUFFER; |
| 90 | atif_arg_elements[1].buffer.length = params->length; |
| 91 | atif_arg_elements[1].buffer.pointer = params->pointer; |
| 92 | } else { |
| 93 | /* We need a second fake parameter */ |
| 94 | atif_arg_elements[1].type = ACPI_TYPE_INTEGER; |
| 95 | atif_arg_elements[1].integer.value = 0; |
| 96 | } |
Alberto Milone | d7a2952 | 2010-07-06 11:40:24 -0400 | [diff] [blame] | 97 | |
| 98 | status = acpi_evaluate_object(handle, "ATIF", &atif_arg, &buffer); |
| 99 | |
| 100 | /* Fail only if calling the method fails and ATIF is supported */ |
| 101 | if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) { |
Jean Delvare | bc96f94 | 2011-11-30 17:26:36 +0100 | [diff] [blame] | 102 | DRM_DEBUG_DRIVER("failed to evaluate ATIF got %s\n", |
| 103 | acpi_format_exception(status)); |
Alberto Milone | d7a2952 | 2010-07-06 11:40:24 -0400 | [diff] [blame] | 104 | kfree(buffer.pointer); |
Luca Tettamanti | 8650467 | 2012-07-29 17:04:43 +0200 | [diff] [blame] | 105 | return NULL; |
Alberto Milone | d7a2952 | 2010-07-06 11:40:24 -0400 | [diff] [blame] | 106 | } |
| 107 | |
Luca Tettamanti | 8650467 | 2012-07-29 17:04:43 +0200 | [diff] [blame] | 108 | return buffer.pointer; |
Alberto Milone | d7a2952 | 2010-07-06 11:40:24 -0400 | [diff] [blame] | 109 | } |
| 110 | |
Luca Tettamanti | fd64ca8 | 2012-08-16 11:11:18 -0400 | [diff] [blame] | 111 | static void radeon_atif_parse_notification(struct radeon_atif_notifications *n, u32 mask) |
| 112 | { |
| 113 | n->display_switch = mask & ATIF_DISPLAY_SWITCH_REQUEST_SUPPORTED; |
| 114 | n->expansion_mode_change = mask & ATIF_EXPANSION_MODE_CHANGE_REQUEST_SUPPORTED; |
| 115 | n->thermal_state = mask & ATIF_THERMAL_STATE_CHANGE_REQUEST_SUPPORTED; |
| 116 | n->forced_power_state = mask & ATIF_FORCED_POWER_STATE_CHANGE_REQUEST_SUPPORTED; |
| 117 | n->system_power_state = mask & ATIF_SYSTEM_POWER_SOURCE_CHANGE_REQUEST_SUPPORTED; |
| 118 | n->display_conf_change = mask & ATIF_DISPLAY_CONF_CHANGE_REQUEST_SUPPORTED; |
| 119 | n->px_gfx_switch = mask & ATIF_PX_GFX_SWITCH_REQUEST_SUPPORTED; |
| 120 | n->brightness_change = mask & ATIF_PANEL_BRIGHTNESS_CHANGE_REQUEST_SUPPORTED; |
| 121 | n->dgpu_display_event = mask & ATIF_DGPU_DISPLAY_EVENT_SUPPORTED; |
| 122 | } |
| 123 | |
| 124 | static void radeon_atif_parse_functions(struct radeon_atif_functions *f, u32 mask) |
| 125 | { |
| 126 | f->system_params = mask & ATIF_GET_SYSTEM_PARAMETERS_SUPPORTED; |
| 127 | f->sbios_requests = mask & ATIF_GET_SYSTEM_BIOS_REQUESTS_SUPPORTED; |
| 128 | f->select_active_disp = mask & ATIF_SELECT_ACTIVE_DISPLAYS_SUPPORTED; |
| 129 | f->lid_state = mask & ATIF_GET_LID_STATE_SUPPORTED; |
| 130 | f->get_tv_standard = mask & ATIF_GET_TV_STANDARD_FROM_CMOS_SUPPORTED; |
| 131 | f->set_tv_standard = mask & ATIF_SET_TV_STANDARD_IN_CMOS_SUPPORTED; |
| 132 | f->get_panel_expansion_mode = mask & ATIF_GET_PANEL_EXPANSION_MODE_FROM_CMOS_SUPPORTED; |
| 133 | f->set_panel_expansion_mode = mask & ATIF_SET_PANEL_EXPANSION_MODE_IN_CMOS_SUPPORTED; |
| 134 | f->temperature_change = mask & ATIF_TEMPERATURE_CHANGE_NOTIFICATION_SUPPORTED; |
| 135 | f->graphics_device_types = mask & ATIF_GET_GRAPHICS_DEVICE_TYPES_SUPPORTED; |
| 136 | } |
| 137 | |
| 138 | static int radeon_atif_verify_interface(acpi_handle handle, |
| 139 | struct radeon_atif *atif) |
| 140 | { |
| 141 | union acpi_object *info; |
| 142 | struct atif_verify_interface output; |
| 143 | size_t size; |
| 144 | int err = 0; |
| 145 | |
| 146 | info = radeon_atif_call(handle, ATIF_FUNCTION_VERIFY_INTERFACE, NULL); |
| 147 | if (!info) |
| 148 | return -EIO; |
| 149 | |
| 150 | memset(&output, 0, sizeof(output)); |
| 151 | |
| 152 | size = *(u16 *) info->buffer.pointer; |
| 153 | if (size < 12) { |
| 154 | DRM_INFO("ATIF buffer is too small: %lu\n", size); |
| 155 | err = -EINVAL; |
| 156 | goto out; |
| 157 | } |
| 158 | size = min(sizeof(output), size); |
| 159 | |
| 160 | memcpy(&output, info->buffer.pointer, size); |
| 161 | |
| 162 | /* TODO: check version? */ |
| 163 | DRM_DEBUG_DRIVER("ATIF version %u\n", output.version); |
| 164 | |
| 165 | radeon_atif_parse_notification(&atif->notifications, output.notification_mask); |
| 166 | radeon_atif_parse_functions(&atif->functions, output.function_bits); |
| 167 | |
| 168 | out: |
| 169 | kfree(info); |
| 170 | return err; |
| 171 | } |
| 172 | |
Luca Tettamanti | ce3cf82 | 2012-07-30 21:16:06 +0200 | [diff] [blame] | 173 | static int radeon_atif_get_notification_params(acpi_handle handle, |
| 174 | struct radeon_atif_notification_cfg *n) |
| 175 | { |
| 176 | union acpi_object *info; |
| 177 | struct atif_system_params params; |
| 178 | size_t size; |
| 179 | int err = 0; |
| 180 | |
| 181 | info = radeon_atif_call(handle, ATIF_FUNCTION_GET_SYSTEM_PARAMETERS, NULL); |
| 182 | if (!info) { |
| 183 | err = -EIO; |
| 184 | goto out; |
| 185 | } |
| 186 | |
| 187 | size = *(u16 *) info->buffer.pointer; |
| 188 | if (size < 10) { |
| 189 | err = -EINVAL; |
| 190 | goto out; |
| 191 | } |
| 192 | |
| 193 | memset(¶ms, 0, sizeof(params)); |
| 194 | size = min(sizeof(params), size); |
| 195 | memcpy(¶ms, info->buffer.pointer, size); |
| 196 | |
Luca Tettamanti | fda4b25 | 2012-07-30 21:20:35 +0200 | [diff] [blame^] | 197 | DRM_DEBUG_DRIVER("SYSTEM_PARAMS: mask = %#x, flags = %#x\n", |
| 198 | params.flags, params.valid_mask); |
Luca Tettamanti | ce3cf82 | 2012-07-30 21:16:06 +0200 | [diff] [blame] | 199 | params.flags = params.flags & params.valid_mask; |
| 200 | |
| 201 | if ((params.flags & ATIF_NOTIFY_MASK) == ATIF_NOTIFY_NONE) { |
| 202 | n->enabled = false; |
| 203 | n->command_code = 0; |
| 204 | } else if ((params.flags & ATIF_NOTIFY_MASK) == ATIF_NOTIFY_81) { |
| 205 | n->enabled = true; |
| 206 | n->command_code = 0x81; |
| 207 | } else { |
| 208 | if (size < 11) { |
| 209 | err = -EINVAL; |
| 210 | goto out; |
| 211 | } |
| 212 | n->enabled = true; |
| 213 | n->command_code = params.command_code; |
| 214 | } |
| 215 | |
| 216 | out: |
Luca Tettamanti | fda4b25 | 2012-07-30 21:20:35 +0200 | [diff] [blame^] | 217 | DRM_DEBUG_DRIVER("Notification %s, command code = %#x\n", |
| 218 | (n->enabled ? "enabled" : "disabled"), |
| 219 | n->command_code); |
Luca Tettamanti | ce3cf82 | 2012-07-30 21:16:06 +0200 | [diff] [blame] | 220 | kfree(info); |
| 221 | return err; |
| 222 | } |
| 223 | |
Luca Tettamanti | fda4b25 | 2012-07-30 21:20:35 +0200 | [diff] [blame^] | 224 | static int radeon_atif_get_sbios_requests(acpi_handle handle, |
| 225 | struct atif_sbios_requests *req) |
| 226 | { |
| 227 | union acpi_object *info; |
| 228 | size_t size; |
| 229 | int count = 0; |
| 230 | |
| 231 | info = radeon_atif_call(handle, ATIF_FUNCTION_GET_SYSTEM_BIOS_REQUESTS, NULL); |
| 232 | if (!info) |
| 233 | return -EIO; |
| 234 | |
| 235 | size = *(u16 *)info->buffer.pointer; |
| 236 | if (size < 0xd) { |
| 237 | count = -EINVAL; |
| 238 | goto out; |
| 239 | } |
| 240 | memset(req, 0, sizeof(*req)); |
| 241 | |
| 242 | size = min(sizeof(*req), size); |
| 243 | memcpy(req, info->buffer.pointer, size); |
| 244 | DRM_DEBUG_DRIVER("SBIOS pending requests: %#x\n", req->pending); |
| 245 | |
| 246 | count = hweight32(req->pending); |
| 247 | |
| 248 | out: |
| 249 | kfree(info); |
| 250 | return count; |
| 251 | } |
| 252 | |
| 253 | int radeon_atif_handler(struct radeon_device *rdev, |
| 254 | struct acpi_bus_event *event) |
| 255 | { |
| 256 | struct radeon_atif *atif = &rdev->atif; |
| 257 | struct atif_sbios_requests req; |
| 258 | acpi_handle handle; |
| 259 | int count; |
| 260 | |
| 261 | DRM_DEBUG_DRIVER("event, device_class = %s, type = %#x\n", |
| 262 | event->device_class, event->type); |
| 263 | |
| 264 | if (strcmp(event->device_class, ACPI_VIDEO_CLASS) != 0) |
| 265 | return NOTIFY_DONE; |
| 266 | |
| 267 | if (!atif->notification_cfg.enabled || |
| 268 | event->type != atif->notification_cfg.command_code) |
| 269 | /* Not our event */ |
| 270 | return NOTIFY_DONE; |
| 271 | |
| 272 | /* Check pending SBIOS requests */ |
| 273 | handle = DEVICE_ACPI_HANDLE(&rdev->pdev->dev); |
| 274 | count = radeon_atif_get_sbios_requests(handle, &req); |
| 275 | |
| 276 | if (count <= 0) |
| 277 | return NOTIFY_DONE; |
| 278 | |
| 279 | DRM_DEBUG_DRIVER("ATIF: %d pending SBIOS requests\n", count); |
| 280 | |
| 281 | if (req.pending & ATIF_PANEL_BRIGHTNESS_CHANGE_REQUEST) { |
| 282 | struct radeon_encoder *enc = atif->backlight_ctl; |
| 283 | |
| 284 | if (enc) { |
| 285 | struct radeon_encoder_atom_dig *dig = enc->enc_priv; |
| 286 | dig->backlight_level = req.backlight_level; |
| 287 | |
| 288 | DRM_DEBUG_DRIVER("Changing brightness to %d\n", |
| 289 | req.backlight_level); |
| 290 | |
| 291 | atombios_set_panel_brightness(enc); |
| 292 | |
| 293 | backlight_force_update(dig->bl_dev, |
| 294 | BACKLIGHT_UPDATE_HOTKEY); |
| 295 | } |
| 296 | } |
| 297 | /* TODO: check other events */ |
| 298 | |
| 299 | return NOTIFY_OK; |
| 300 | } |
| 301 | |
Alberto Milone | d7a2952 | 2010-07-06 11:40:24 -0400 | [diff] [blame] | 302 | /* Call all ACPI methods here */ |
| 303 | int radeon_acpi_init(struct radeon_device *rdev) |
| 304 | { |
| 305 | acpi_handle handle; |
Luca Tettamanti | ce3cf82 | 2012-07-30 21:16:06 +0200 | [diff] [blame] | 306 | struct radeon_atif *atif = &rdev->atif; |
Luca Tettamanti | fd64ca8 | 2012-08-16 11:11:18 -0400 | [diff] [blame] | 307 | int ret; |
Alberto Milone | d7a2952 | 2010-07-06 11:40:24 -0400 | [diff] [blame] | 308 | |
Alberto Milone | d7a2952 | 2010-07-06 11:40:24 -0400 | [diff] [blame] | 309 | /* Get the device handle */ |
| 310 | handle = DEVICE_ACPI_HANDLE(&rdev->pdev->dev); |
| 311 | |
Jean Delvare | 48cc9b2 | 2011-11-30 17:36:39 +0100 | [diff] [blame] | 312 | /* No need to proceed if we're sure that ATIF is not supported */ |
| 313 | if (!ASIC_IS_AVIVO(rdev) || !rdev->bios || !handle) |
| 314 | return 0; |
| 315 | |
Alberto Milone | d7a2952 | 2010-07-06 11:40:24 -0400 | [diff] [blame] | 316 | /* Call the ATIF method */ |
Luca Tettamanti | ce3cf82 | 2012-07-30 21:16:06 +0200 | [diff] [blame] | 317 | ret = radeon_atif_verify_interface(handle, atif); |
| 318 | if (ret) { |
Luca Tettamanti | fd64ca8 | 2012-08-16 11:11:18 -0400 | [diff] [blame] | 319 | DRM_DEBUG_DRIVER("Call to verify_interface failed: %d\n", ret); |
Luca Tettamanti | ce3cf82 | 2012-07-30 21:16:06 +0200 | [diff] [blame] | 320 | goto out; |
| 321 | } |
Alberto Milone | d7a2952 | 2010-07-06 11:40:24 -0400 | [diff] [blame] | 322 | |
Luca Tettamanti | fda4b25 | 2012-07-30 21:20:35 +0200 | [diff] [blame^] | 323 | if (atif->notifications.brightness_change) { |
| 324 | struct drm_encoder *tmp; |
| 325 | struct radeon_encoder *target = NULL; |
| 326 | |
| 327 | /* Find the encoder controlling the brightness */ |
| 328 | list_for_each_entry(tmp, &rdev->ddev->mode_config.encoder_list, |
| 329 | head) { |
| 330 | struct radeon_encoder *enc = to_radeon_encoder(tmp); |
| 331 | struct radeon_encoder_atom_dig *dig = enc->enc_priv; |
| 332 | |
| 333 | if ((enc->devices & (ATOM_DEVICE_LCD_SUPPORT)) && |
| 334 | dig->bl_dev != NULL) { |
| 335 | target = enc; |
| 336 | break; |
| 337 | } |
| 338 | } |
| 339 | |
| 340 | atif->backlight_ctl = target; |
| 341 | if (!target) { |
| 342 | /* Brightness change notification is enabled, but we |
| 343 | * didn't find a backlight controller, this should |
| 344 | * never happen. |
| 345 | */ |
| 346 | DRM_ERROR("Cannot find a backlight controller\n"); |
| 347 | } |
| 348 | } |
| 349 | |
Luca Tettamanti | ce3cf82 | 2012-07-30 21:16:06 +0200 | [diff] [blame] | 350 | if (atif->functions.sbios_requests && !atif->functions.system_params) { |
| 351 | /* XXX check this workraround, if sbios request function is |
| 352 | * present we have to see how it's configured in the system |
| 353 | * params |
| 354 | */ |
| 355 | atif->functions.system_params = true; |
| 356 | } |
| 357 | |
| 358 | if (atif->functions.system_params) { |
| 359 | ret = radeon_atif_get_notification_params(handle, |
| 360 | &atif->notification_cfg); |
| 361 | if (ret) { |
| 362 | DRM_DEBUG_DRIVER("Call to GET_SYSTEM_PARAMS failed: %d\n", |
| 363 | ret); |
| 364 | /* Disable notification */ |
| 365 | atif->notification_cfg.enabled = false; |
| 366 | } |
| 367 | } |
| 368 | |
| 369 | out: |
Luca Tettamanti | 8650467 | 2012-07-29 17:04:43 +0200 | [diff] [blame] | 370 | return ret; |
Alberto Milone | d7a2952 | 2010-07-06 11:40:24 -0400 | [diff] [blame] | 371 | } |
| 372 | |