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> |
Alex Deucher | c491707 | 2012-07-31 17:14:35 -0400 | [diff] [blame] | 27 | #include <linux/power_supply.h> |
Alberto Milone | d7a2952 | 2010-07-06 11:40:24 -0400 | [diff] [blame] | 28 | #include <acpi/acpi_drivers.h> |
| 29 | #include <acpi/acpi_bus.h> |
Luca Tettamanti | fda4b25 | 2012-07-30 21:20:35 +0200 | [diff] [blame] | 30 | #include <acpi/video.h> |
Alberto Milone | d7a2952 | 2010-07-06 11:40:24 -0400 | [diff] [blame] | 31 | |
| 32 | #include "drmP.h" |
| 33 | #include "drm.h" |
| 34 | #include "drm_sarea.h" |
| 35 | #include "drm_crtc_helper.h" |
| 36 | #include "radeon.h" |
Alex Deucher | 9e05b2f | 2012-07-18 13:28:52 -0400 | [diff] [blame] | 37 | #include "radeon_acpi.h" |
Luca Tettamanti | fda4b25 | 2012-07-30 21:20:35 +0200 | [diff] [blame] | 38 | #include "atom.h" |
Alberto Milone | d7a2952 | 2010-07-06 11:40:24 -0400 | [diff] [blame] | 39 | |
| 40 | #include <linux/vga_switcheroo.h> |
| 41 | |
Alex Deucher | c491707 | 2012-07-31 17:14:35 -0400 | [diff] [blame] | 42 | #define ACPI_AC_CLASS "ac_adapter" |
| 43 | |
| 44 | extern void radeon_pm_acpi_event_handler(struct radeon_device *rdev); |
| 45 | |
Luca Tettamanti | fd64ca8 | 2012-08-16 11:11:18 -0400 | [diff] [blame] | 46 | struct atif_verify_interface { |
| 47 | u16 size; /* structure size in bytes (includes size field) */ |
| 48 | u16 version; /* version */ |
| 49 | u32 notification_mask; /* supported notifications mask */ |
| 50 | u32 function_bits; /* supported functions bit vector */ |
| 51 | } __packed; |
| 52 | |
Luca Tettamanti | ce3cf82 | 2012-07-30 21:16:06 +0200 | [diff] [blame] | 53 | struct atif_system_params { |
Luca Tettamanti | fda4b25 | 2012-07-30 21:20:35 +0200 | [diff] [blame] | 54 | u16 size; /* structure size in bytes (includes size field) */ |
| 55 | u32 valid_mask; /* valid flags mask */ |
| 56 | u32 flags; /* flags */ |
| 57 | u8 command_code; /* notify command code */ |
| 58 | } __packed; |
| 59 | |
| 60 | struct atif_sbios_requests { |
| 61 | u16 size; /* structure size in bytes (includes size field) */ |
| 62 | u32 pending; /* pending sbios requests */ |
| 63 | u8 panel_exp_mode; /* panel expansion mode */ |
| 64 | u8 thermal_gfx; /* thermal state: target gfx controller */ |
| 65 | u8 thermal_state; /* thermal state: state id (0: exit state, non-0: state) */ |
| 66 | u8 forced_power_gfx; /* forced power state: target gfx controller */ |
| 67 | u8 forced_power_state; /* forced power state: state id */ |
| 68 | u8 system_power_src; /* system power source */ |
| 69 | u8 backlight_level; /* panel backlight level (0-255) */ |
Luca Tettamanti | ce3cf82 | 2012-07-30 21:16:06 +0200 | [diff] [blame] | 70 | } __packed; |
| 71 | |
| 72 | #define ATIF_NOTIFY_MASK 0x3 |
| 73 | #define ATIF_NOTIFY_NONE 0 |
| 74 | #define ATIF_NOTIFY_81 1 |
| 75 | #define ATIF_NOTIFY_N 2 |
| 76 | |
Alex Deucher | e3a1592 | 2012-08-16 11:13:43 -0400 | [diff] [blame^] | 77 | struct atcs_verify_interface { |
| 78 | u16 size; /* structure size in bytes (includes size field) */ |
| 79 | u16 version; /* version */ |
| 80 | u32 function_bits; /* supported functions bit vector */ |
| 81 | } __packed; |
| 82 | |
Alberto Milone | d7a2952 | 2010-07-06 11:40:24 -0400 | [diff] [blame] | 83 | /* Call the ATIF method |
Alberto Milone | d7a2952 | 2010-07-06 11:40:24 -0400 | [diff] [blame] | 84 | */ |
Alex Deucher | c3c6516 | 2012-08-02 12:30:49 -0400 | [diff] [blame] | 85 | /** |
| 86 | * radeon_atif_call - call an ATIF method |
| 87 | * |
| 88 | * @handle: acpi handle |
| 89 | * @function: the ATIF function to execute |
| 90 | * @params: ATIF function params |
| 91 | * |
| 92 | * Executes the requested ATIF function (all asics). |
| 93 | * Returns a pointer to the acpi output buffer. |
| 94 | */ |
Luca Tettamanti | 8650467 | 2012-07-29 17:04:43 +0200 | [diff] [blame] | 95 | static union acpi_object *radeon_atif_call(acpi_handle handle, int function, |
| 96 | struct acpi_buffer *params) |
Alberto Milone | d7a2952 | 2010-07-06 11:40:24 -0400 | [diff] [blame] | 97 | { |
| 98 | acpi_status status; |
| 99 | union acpi_object atif_arg_elements[2]; |
| 100 | struct acpi_object_list atif_arg; |
Luca Tettamanti | 8650467 | 2012-07-29 17:04:43 +0200 | [diff] [blame] | 101 | struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL }; |
Alberto Milone | d7a2952 | 2010-07-06 11:40:24 -0400 | [diff] [blame] | 102 | |
| 103 | atif_arg.count = 2; |
| 104 | atif_arg.pointer = &atif_arg_elements[0]; |
| 105 | |
| 106 | atif_arg_elements[0].type = ACPI_TYPE_INTEGER; |
Luca Tettamanti | 8650467 | 2012-07-29 17:04:43 +0200 | [diff] [blame] | 107 | atif_arg_elements[0].integer.value = function; |
| 108 | |
| 109 | if (params) { |
| 110 | atif_arg_elements[1].type = ACPI_TYPE_BUFFER; |
| 111 | atif_arg_elements[1].buffer.length = params->length; |
| 112 | atif_arg_elements[1].buffer.pointer = params->pointer; |
| 113 | } else { |
| 114 | /* We need a second fake parameter */ |
| 115 | atif_arg_elements[1].type = ACPI_TYPE_INTEGER; |
| 116 | atif_arg_elements[1].integer.value = 0; |
| 117 | } |
Alberto Milone | d7a2952 | 2010-07-06 11:40:24 -0400 | [diff] [blame] | 118 | |
| 119 | status = acpi_evaluate_object(handle, "ATIF", &atif_arg, &buffer); |
| 120 | |
| 121 | /* Fail only if calling the method fails and ATIF is supported */ |
| 122 | if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) { |
Jean Delvare | bc96f94 | 2011-11-30 17:26:36 +0100 | [diff] [blame] | 123 | DRM_DEBUG_DRIVER("failed to evaluate ATIF got %s\n", |
| 124 | acpi_format_exception(status)); |
Alberto Milone | d7a2952 | 2010-07-06 11:40:24 -0400 | [diff] [blame] | 125 | kfree(buffer.pointer); |
Luca Tettamanti | 8650467 | 2012-07-29 17:04:43 +0200 | [diff] [blame] | 126 | return NULL; |
Alberto Milone | d7a2952 | 2010-07-06 11:40:24 -0400 | [diff] [blame] | 127 | } |
| 128 | |
Luca Tettamanti | 8650467 | 2012-07-29 17:04:43 +0200 | [diff] [blame] | 129 | return buffer.pointer; |
Alberto Milone | d7a2952 | 2010-07-06 11:40:24 -0400 | [diff] [blame] | 130 | } |
| 131 | |
Alex Deucher | c3c6516 | 2012-08-02 12:30:49 -0400 | [diff] [blame] | 132 | /** |
| 133 | * radeon_atif_parse_notification - parse supported notifications |
| 134 | * |
| 135 | * @n: supported notifications struct |
| 136 | * @mask: supported notifications mask from ATIF |
| 137 | * |
| 138 | * Use the supported notifications mask from ATIF function |
| 139 | * ATIF_FUNCTION_VERIFY_INTERFACE to determine what notifications |
| 140 | * are supported (all asics). |
| 141 | */ |
Luca Tettamanti | fd64ca8 | 2012-08-16 11:11:18 -0400 | [diff] [blame] | 142 | static void radeon_atif_parse_notification(struct radeon_atif_notifications *n, u32 mask) |
| 143 | { |
| 144 | n->display_switch = mask & ATIF_DISPLAY_SWITCH_REQUEST_SUPPORTED; |
| 145 | n->expansion_mode_change = mask & ATIF_EXPANSION_MODE_CHANGE_REQUEST_SUPPORTED; |
| 146 | n->thermal_state = mask & ATIF_THERMAL_STATE_CHANGE_REQUEST_SUPPORTED; |
| 147 | n->forced_power_state = mask & ATIF_FORCED_POWER_STATE_CHANGE_REQUEST_SUPPORTED; |
| 148 | n->system_power_state = mask & ATIF_SYSTEM_POWER_SOURCE_CHANGE_REQUEST_SUPPORTED; |
| 149 | n->display_conf_change = mask & ATIF_DISPLAY_CONF_CHANGE_REQUEST_SUPPORTED; |
| 150 | n->px_gfx_switch = mask & ATIF_PX_GFX_SWITCH_REQUEST_SUPPORTED; |
| 151 | n->brightness_change = mask & ATIF_PANEL_BRIGHTNESS_CHANGE_REQUEST_SUPPORTED; |
| 152 | n->dgpu_display_event = mask & ATIF_DGPU_DISPLAY_EVENT_SUPPORTED; |
| 153 | } |
| 154 | |
Alex Deucher | c3c6516 | 2012-08-02 12:30:49 -0400 | [diff] [blame] | 155 | /** |
| 156 | * radeon_atif_parse_functions - parse supported functions |
| 157 | * |
| 158 | * @f: supported functions struct |
| 159 | * @mask: supported functions mask from ATIF |
| 160 | * |
| 161 | * Use the supported functions mask from ATIF function |
| 162 | * ATIF_FUNCTION_VERIFY_INTERFACE to determine what functions |
| 163 | * are supported (all asics). |
| 164 | */ |
Luca Tettamanti | fd64ca8 | 2012-08-16 11:11:18 -0400 | [diff] [blame] | 165 | static void radeon_atif_parse_functions(struct radeon_atif_functions *f, u32 mask) |
| 166 | { |
| 167 | f->system_params = mask & ATIF_GET_SYSTEM_PARAMETERS_SUPPORTED; |
| 168 | f->sbios_requests = mask & ATIF_GET_SYSTEM_BIOS_REQUESTS_SUPPORTED; |
| 169 | f->select_active_disp = mask & ATIF_SELECT_ACTIVE_DISPLAYS_SUPPORTED; |
| 170 | f->lid_state = mask & ATIF_GET_LID_STATE_SUPPORTED; |
| 171 | f->get_tv_standard = mask & ATIF_GET_TV_STANDARD_FROM_CMOS_SUPPORTED; |
| 172 | f->set_tv_standard = mask & ATIF_SET_TV_STANDARD_IN_CMOS_SUPPORTED; |
| 173 | f->get_panel_expansion_mode = mask & ATIF_GET_PANEL_EXPANSION_MODE_FROM_CMOS_SUPPORTED; |
| 174 | f->set_panel_expansion_mode = mask & ATIF_SET_PANEL_EXPANSION_MODE_IN_CMOS_SUPPORTED; |
| 175 | f->temperature_change = mask & ATIF_TEMPERATURE_CHANGE_NOTIFICATION_SUPPORTED; |
| 176 | f->graphics_device_types = mask & ATIF_GET_GRAPHICS_DEVICE_TYPES_SUPPORTED; |
| 177 | } |
| 178 | |
Alex Deucher | c3c6516 | 2012-08-02 12:30:49 -0400 | [diff] [blame] | 179 | /** |
| 180 | * radeon_atif_verify_interface - verify ATIF |
| 181 | * |
| 182 | * @handle: acpi handle |
| 183 | * @atif: radeon atif struct |
| 184 | * |
| 185 | * Execute the ATIF_FUNCTION_VERIFY_INTERFACE ATIF function |
| 186 | * to initialize ATIF and determine what features are supported |
| 187 | * (all asics). |
| 188 | * returns 0 on success, error on failure. |
| 189 | */ |
Luca Tettamanti | fd64ca8 | 2012-08-16 11:11:18 -0400 | [diff] [blame] | 190 | static int radeon_atif_verify_interface(acpi_handle handle, |
| 191 | struct radeon_atif *atif) |
| 192 | { |
| 193 | union acpi_object *info; |
| 194 | struct atif_verify_interface output; |
| 195 | size_t size; |
| 196 | int err = 0; |
| 197 | |
| 198 | info = radeon_atif_call(handle, ATIF_FUNCTION_VERIFY_INTERFACE, NULL); |
| 199 | if (!info) |
| 200 | return -EIO; |
| 201 | |
| 202 | memset(&output, 0, sizeof(output)); |
| 203 | |
| 204 | size = *(u16 *) info->buffer.pointer; |
| 205 | if (size < 12) { |
| 206 | DRM_INFO("ATIF buffer is too small: %lu\n", size); |
| 207 | err = -EINVAL; |
| 208 | goto out; |
| 209 | } |
| 210 | size = min(sizeof(output), size); |
| 211 | |
| 212 | memcpy(&output, info->buffer.pointer, size); |
| 213 | |
| 214 | /* TODO: check version? */ |
| 215 | DRM_DEBUG_DRIVER("ATIF version %u\n", output.version); |
| 216 | |
| 217 | radeon_atif_parse_notification(&atif->notifications, output.notification_mask); |
| 218 | radeon_atif_parse_functions(&atif->functions, output.function_bits); |
| 219 | |
| 220 | out: |
| 221 | kfree(info); |
| 222 | return err; |
| 223 | } |
| 224 | |
Alex Deucher | c3c6516 | 2012-08-02 12:30:49 -0400 | [diff] [blame] | 225 | /** |
| 226 | * radeon_atif_get_notification_params - determine notify configuration |
| 227 | * |
| 228 | * @handle: acpi handle |
| 229 | * @n: atif notification configuration struct |
| 230 | * |
| 231 | * Execute the ATIF_FUNCTION_GET_SYSTEM_PARAMETERS ATIF function |
| 232 | * to determine if a notifier is used and if so which one |
| 233 | * (all asics). This is either Notify(VGA, 0x81) or Notify(VGA, n) |
| 234 | * where n is specified in the result if a notifier is used. |
| 235 | * Returns 0 on success, error on failure. |
| 236 | */ |
Luca Tettamanti | ce3cf82 | 2012-07-30 21:16:06 +0200 | [diff] [blame] | 237 | static int radeon_atif_get_notification_params(acpi_handle handle, |
| 238 | struct radeon_atif_notification_cfg *n) |
| 239 | { |
| 240 | union acpi_object *info; |
| 241 | struct atif_system_params params; |
| 242 | size_t size; |
| 243 | int err = 0; |
| 244 | |
| 245 | info = radeon_atif_call(handle, ATIF_FUNCTION_GET_SYSTEM_PARAMETERS, NULL); |
| 246 | if (!info) { |
| 247 | err = -EIO; |
| 248 | goto out; |
| 249 | } |
| 250 | |
| 251 | size = *(u16 *) info->buffer.pointer; |
| 252 | if (size < 10) { |
| 253 | err = -EINVAL; |
| 254 | goto out; |
| 255 | } |
| 256 | |
| 257 | memset(¶ms, 0, sizeof(params)); |
| 258 | size = min(sizeof(params), size); |
| 259 | memcpy(¶ms, info->buffer.pointer, size); |
| 260 | |
Luca Tettamanti | fda4b25 | 2012-07-30 21:20:35 +0200 | [diff] [blame] | 261 | DRM_DEBUG_DRIVER("SYSTEM_PARAMS: mask = %#x, flags = %#x\n", |
| 262 | params.flags, params.valid_mask); |
Luca Tettamanti | ce3cf82 | 2012-07-30 21:16:06 +0200 | [diff] [blame] | 263 | params.flags = params.flags & params.valid_mask; |
| 264 | |
| 265 | if ((params.flags & ATIF_NOTIFY_MASK) == ATIF_NOTIFY_NONE) { |
| 266 | n->enabled = false; |
| 267 | n->command_code = 0; |
| 268 | } else if ((params.flags & ATIF_NOTIFY_MASK) == ATIF_NOTIFY_81) { |
| 269 | n->enabled = true; |
| 270 | n->command_code = 0x81; |
| 271 | } else { |
| 272 | if (size < 11) { |
| 273 | err = -EINVAL; |
| 274 | goto out; |
| 275 | } |
| 276 | n->enabled = true; |
| 277 | n->command_code = params.command_code; |
| 278 | } |
| 279 | |
| 280 | out: |
Luca Tettamanti | fda4b25 | 2012-07-30 21:20:35 +0200 | [diff] [blame] | 281 | DRM_DEBUG_DRIVER("Notification %s, command code = %#x\n", |
| 282 | (n->enabled ? "enabled" : "disabled"), |
| 283 | n->command_code); |
Luca Tettamanti | ce3cf82 | 2012-07-30 21:16:06 +0200 | [diff] [blame] | 284 | kfree(info); |
| 285 | return err; |
| 286 | } |
| 287 | |
Alex Deucher | c3c6516 | 2012-08-02 12:30:49 -0400 | [diff] [blame] | 288 | /** |
| 289 | * radeon_atif_get_sbios_requests - get requested sbios event |
| 290 | * |
| 291 | * @handle: acpi handle |
| 292 | * @req: atif sbios request struct |
| 293 | * |
| 294 | * Execute the ATIF_FUNCTION_GET_SYSTEM_BIOS_REQUESTS ATIF function |
| 295 | * to determine what requests the sbios is making to the driver |
| 296 | * (all asics). |
| 297 | * Returns 0 on success, error on failure. |
| 298 | */ |
Luca Tettamanti | fda4b25 | 2012-07-30 21:20:35 +0200 | [diff] [blame] | 299 | static int radeon_atif_get_sbios_requests(acpi_handle handle, |
| 300 | struct atif_sbios_requests *req) |
| 301 | { |
| 302 | union acpi_object *info; |
| 303 | size_t size; |
| 304 | int count = 0; |
| 305 | |
| 306 | info = radeon_atif_call(handle, ATIF_FUNCTION_GET_SYSTEM_BIOS_REQUESTS, NULL); |
| 307 | if (!info) |
| 308 | return -EIO; |
| 309 | |
| 310 | size = *(u16 *)info->buffer.pointer; |
| 311 | if (size < 0xd) { |
| 312 | count = -EINVAL; |
| 313 | goto out; |
| 314 | } |
| 315 | memset(req, 0, sizeof(*req)); |
| 316 | |
| 317 | size = min(sizeof(*req), size); |
| 318 | memcpy(req, info->buffer.pointer, size); |
| 319 | DRM_DEBUG_DRIVER("SBIOS pending requests: %#x\n", req->pending); |
| 320 | |
| 321 | count = hweight32(req->pending); |
| 322 | |
| 323 | out: |
| 324 | kfree(info); |
| 325 | return count; |
| 326 | } |
| 327 | |
Alex Deucher | c3c6516 | 2012-08-02 12:30:49 -0400 | [diff] [blame] | 328 | /** |
| 329 | * radeon_atif_handler - handle ATIF notify requests |
| 330 | * |
| 331 | * @rdev: radeon_device pointer |
| 332 | * @event: atif sbios request struct |
| 333 | * |
| 334 | * Checks the acpi event and if it matches an atif event, |
| 335 | * handles it. |
| 336 | * Returns NOTIFY code |
| 337 | */ |
Luca Tettamanti | fda4b25 | 2012-07-30 21:20:35 +0200 | [diff] [blame] | 338 | int radeon_atif_handler(struct radeon_device *rdev, |
| 339 | struct acpi_bus_event *event) |
| 340 | { |
| 341 | struct radeon_atif *atif = &rdev->atif; |
| 342 | struct atif_sbios_requests req; |
| 343 | acpi_handle handle; |
| 344 | int count; |
| 345 | |
| 346 | DRM_DEBUG_DRIVER("event, device_class = %s, type = %#x\n", |
| 347 | event->device_class, event->type); |
| 348 | |
| 349 | if (strcmp(event->device_class, ACPI_VIDEO_CLASS) != 0) |
| 350 | return NOTIFY_DONE; |
| 351 | |
| 352 | if (!atif->notification_cfg.enabled || |
| 353 | event->type != atif->notification_cfg.command_code) |
| 354 | /* Not our event */ |
| 355 | return NOTIFY_DONE; |
| 356 | |
| 357 | /* Check pending SBIOS requests */ |
| 358 | handle = DEVICE_ACPI_HANDLE(&rdev->pdev->dev); |
| 359 | count = radeon_atif_get_sbios_requests(handle, &req); |
| 360 | |
| 361 | if (count <= 0) |
| 362 | return NOTIFY_DONE; |
| 363 | |
| 364 | DRM_DEBUG_DRIVER("ATIF: %d pending SBIOS requests\n", count); |
| 365 | |
| 366 | if (req.pending & ATIF_PANEL_BRIGHTNESS_CHANGE_REQUEST) { |
| 367 | struct radeon_encoder *enc = atif->backlight_ctl; |
| 368 | |
| 369 | if (enc) { |
| 370 | struct radeon_encoder_atom_dig *dig = enc->enc_priv; |
| 371 | dig->backlight_level = req.backlight_level; |
| 372 | |
| 373 | DRM_DEBUG_DRIVER("Changing brightness to %d\n", |
| 374 | req.backlight_level); |
| 375 | |
| 376 | atombios_set_panel_brightness(enc); |
| 377 | |
| 378 | backlight_force_update(dig->bl_dev, |
| 379 | BACKLIGHT_UPDATE_HOTKEY); |
| 380 | } |
| 381 | } |
| 382 | /* TODO: check other events */ |
| 383 | |
Luca Tettamanti | 92fdf89 | 2012-08-02 11:39:38 -0400 | [diff] [blame] | 384 | /* We've handled the event, stop the notifier chain. The ACPI interface |
| 385 | * overloads ACPI_VIDEO_NOTIFY_PROBE, we don't want to send that to |
| 386 | * userspace if the event was generated only to signal a SBIOS |
| 387 | * request. |
| 388 | */ |
| 389 | return NOTIFY_BAD; |
Luca Tettamanti | fda4b25 | 2012-07-30 21:20:35 +0200 | [diff] [blame] | 390 | } |
| 391 | |
Alex Deucher | e3a1592 | 2012-08-16 11:13:43 -0400 | [diff] [blame^] | 392 | /* Call the ATCS method |
| 393 | */ |
| 394 | /** |
| 395 | * radeon_atcs_call - call an ATCS method |
| 396 | * |
| 397 | * @handle: acpi handle |
| 398 | * @function: the ATCS function to execute |
| 399 | * @params: ATCS function params |
| 400 | * |
| 401 | * Executes the requested ATCS function (all asics). |
| 402 | * Returns a pointer to the acpi output buffer. |
| 403 | */ |
| 404 | static union acpi_object *radeon_atcs_call(acpi_handle handle, int function, |
| 405 | struct acpi_buffer *params) |
| 406 | { |
| 407 | acpi_status status; |
| 408 | union acpi_object atcs_arg_elements[2]; |
| 409 | struct acpi_object_list atcs_arg; |
| 410 | struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL }; |
| 411 | |
| 412 | atcs_arg.count = 2; |
| 413 | atcs_arg.pointer = &atcs_arg_elements[0]; |
| 414 | |
| 415 | atcs_arg_elements[0].type = ACPI_TYPE_INTEGER; |
| 416 | atcs_arg_elements[0].integer.value = function; |
| 417 | |
| 418 | if (params) { |
| 419 | atcs_arg_elements[1].type = ACPI_TYPE_BUFFER; |
| 420 | atcs_arg_elements[1].buffer.length = params->length; |
| 421 | atcs_arg_elements[1].buffer.pointer = params->pointer; |
| 422 | } else { |
| 423 | /* We need a second fake parameter */ |
| 424 | atcs_arg_elements[1].type = ACPI_TYPE_INTEGER; |
| 425 | atcs_arg_elements[1].integer.value = 0; |
| 426 | } |
| 427 | |
| 428 | status = acpi_evaluate_object(handle, "ATCS", &atcs_arg, &buffer); |
| 429 | |
| 430 | /* Fail only if calling the method fails and ATIF is supported */ |
| 431 | if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) { |
| 432 | DRM_DEBUG_DRIVER("failed to evaluate ATCS got %s\n", |
| 433 | acpi_format_exception(status)); |
| 434 | kfree(buffer.pointer); |
| 435 | return NULL; |
| 436 | } |
| 437 | |
| 438 | return buffer.pointer; |
| 439 | } |
| 440 | |
| 441 | /** |
| 442 | * radeon_atcs_parse_functions - parse supported functions |
| 443 | * |
| 444 | * @f: supported functions struct |
| 445 | * @mask: supported functions mask from ATCS |
| 446 | * |
| 447 | * Use the supported functions mask from ATCS function |
| 448 | * ATCS_FUNCTION_VERIFY_INTERFACE to determine what functions |
| 449 | * are supported (all asics). |
| 450 | */ |
| 451 | static void radeon_atcs_parse_functions(struct radeon_atcs_functions *f, u32 mask) |
| 452 | { |
| 453 | f->get_ext_state = mask & ATCS_GET_EXTERNAL_STATE_SUPPORTED; |
| 454 | f->pcie_perf_req = mask & ATCS_PCIE_PERFORMANCE_REQUEST_SUPPORTED; |
| 455 | f->pcie_dev_rdy = mask & ATCS_PCIE_DEVICE_READY_NOTIFICATION_SUPPORTED; |
| 456 | f->pcie_bus_width = mask & ATCS_SET_PCIE_BUS_WIDTH_SUPPORTED; |
| 457 | } |
| 458 | |
| 459 | /** |
| 460 | * radeon_atcs_verify_interface - verify ATCS |
| 461 | * |
| 462 | * @handle: acpi handle |
| 463 | * @atcs: radeon atcs struct |
| 464 | * |
| 465 | * Execute the ATCS_FUNCTION_VERIFY_INTERFACE ATCS function |
| 466 | * to initialize ATCS and determine what features are supported |
| 467 | * (all asics). |
| 468 | * returns 0 on success, error on failure. |
| 469 | */ |
| 470 | static int radeon_atcs_verify_interface(acpi_handle handle, |
| 471 | struct radeon_atcs *atcs) |
| 472 | { |
| 473 | union acpi_object *info; |
| 474 | struct atcs_verify_interface output; |
| 475 | size_t size; |
| 476 | int err = 0; |
| 477 | |
| 478 | info = radeon_atcs_call(handle, ATCS_FUNCTION_VERIFY_INTERFACE, NULL); |
| 479 | if (!info) |
| 480 | return -EIO; |
| 481 | |
| 482 | memset(&output, 0, sizeof(output)); |
| 483 | |
| 484 | size = *(u16 *) info->buffer.pointer; |
| 485 | if (size < 8) { |
| 486 | DRM_INFO("ATCS buffer is too small: %lu\n", size); |
| 487 | err = -EINVAL; |
| 488 | goto out; |
| 489 | } |
| 490 | size = min(sizeof(output), size); |
| 491 | |
| 492 | memcpy(&output, info->buffer.pointer, size); |
| 493 | |
| 494 | /* TODO: check version? */ |
| 495 | DRM_DEBUG_DRIVER("ATCS version %u\n", output.version); |
| 496 | |
| 497 | radeon_atcs_parse_functions(&atcs->functions, output.function_bits); |
| 498 | |
| 499 | out: |
| 500 | kfree(info); |
| 501 | return err; |
| 502 | } |
| 503 | |
Alex Deucher | c3c6516 | 2012-08-02 12:30:49 -0400 | [diff] [blame] | 504 | /** |
| 505 | * radeon_acpi_event - handle notify events |
| 506 | * |
| 507 | * @nb: notifier block |
| 508 | * @val: val |
| 509 | * @data: acpi event |
| 510 | * |
| 511 | * Calls relevant radeon functions in response to various |
| 512 | * acpi events. |
| 513 | * Returns NOTIFY code |
| 514 | */ |
Alex Deucher | c491707 | 2012-07-31 17:14:35 -0400 | [diff] [blame] | 515 | static int radeon_acpi_event(struct notifier_block *nb, |
| 516 | unsigned long val, |
| 517 | void *data) |
| 518 | { |
| 519 | struct radeon_device *rdev = container_of(nb, struct radeon_device, acpi_nb); |
| 520 | struct acpi_bus_event *entry = (struct acpi_bus_event *)data; |
| 521 | |
| 522 | if (strcmp(entry->device_class, ACPI_AC_CLASS) == 0) { |
| 523 | if (power_supply_is_system_supplied() > 0) |
| 524 | DRM_DEBUG_DRIVER("pm: AC\n"); |
| 525 | else |
| 526 | DRM_DEBUG_DRIVER("pm: DC\n"); |
| 527 | |
| 528 | radeon_pm_acpi_event_handler(rdev); |
| 529 | } |
| 530 | |
| 531 | /* Check for pending SBIOS requests */ |
| 532 | return radeon_atif_handler(rdev, entry); |
| 533 | } |
| 534 | |
Alberto Milone | d7a2952 | 2010-07-06 11:40:24 -0400 | [diff] [blame] | 535 | /* Call all ACPI methods here */ |
Alex Deucher | c3c6516 | 2012-08-02 12:30:49 -0400 | [diff] [blame] | 536 | /** |
| 537 | * radeon_acpi_init - init driver acpi support |
| 538 | * |
| 539 | * @rdev: radeon_device pointer |
| 540 | * |
| 541 | * Verifies the AMD ACPI interfaces and registers with the acpi |
| 542 | * notifier chain (all asics). |
| 543 | * Returns 0 on success, error on failure. |
| 544 | */ |
Alberto Milone | d7a2952 | 2010-07-06 11:40:24 -0400 | [diff] [blame] | 545 | int radeon_acpi_init(struct radeon_device *rdev) |
| 546 | { |
| 547 | acpi_handle handle; |
Luca Tettamanti | ce3cf82 | 2012-07-30 21:16:06 +0200 | [diff] [blame] | 548 | struct radeon_atif *atif = &rdev->atif; |
Alex Deucher | e3a1592 | 2012-08-16 11:13:43 -0400 | [diff] [blame^] | 549 | struct radeon_atcs *atcs = &rdev->atcs; |
Luca Tettamanti | fd64ca8 | 2012-08-16 11:11:18 -0400 | [diff] [blame] | 550 | int ret; |
Alberto Milone | d7a2952 | 2010-07-06 11:40:24 -0400 | [diff] [blame] | 551 | |
Alberto Milone | d7a2952 | 2010-07-06 11:40:24 -0400 | [diff] [blame] | 552 | /* Get the device handle */ |
| 553 | handle = DEVICE_ACPI_HANDLE(&rdev->pdev->dev); |
| 554 | |
Jean Delvare | 48cc9b2 | 2011-11-30 17:36:39 +0100 | [diff] [blame] | 555 | /* No need to proceed if we're sure that ATIF is not supported */ |
| 556 | if (!ASIC_IS_AVIVO(rdev) || !rdev->bios || !handle) |
| 557 | return 0; |
| 558 | |
Alex Deucher | e3a1592 | 2012-08-16 11:13:43 -0400 | [diff] [blame^] | 559 | /* Call the ATCS method */ |
| 560 | ret = radeon_atcs_verify_interface(handle, atcs); |
| 561 | if (ret) { |
| 562 | DRM_DEBUG_DRIVER("Call to ATCS verify_interface failed: %d\n", ret); |
| 563 | } |
| 564 | |
Alberto Milone | d7a2952 | 2010-07-06 11:40:24 -0400 | [diff] [blame] | 565 | /* Call the ATIF method */ |
Luca Tettamanti | ce3cf82 | 2012-07-30 21:16:06 +0200 | [diff] [blame] | 566 | ret = radeon_atif_verify_interface(handle, atif); |
| 567 | if (ret) { |
Alex Deucher | e3a1592 | 2012-08-16 11:13:43 -0400 | [diff] [blame^] | 568 | DRM_DEBUG_DRIVER("Call to ATIF verify_interface failed: %d\n", ret); |
Luca Tettamanti | ce3cf82 | 2012-07-30 21:16:06 +0200 | [diff] [blame] | 569 | goto out; |
| 570 | } |
Alberto Milone | d7a2952 | 2010-07-06 11:40:24 -0400 | [diff] [blame] | 571 | |
Luca Tettamanti | fda4b25 | 2012-07-30 21:20:35 +0200 | [diff] [blame] | 572 | if (atif->notifications.brightness_change) { |
| 573 | struct drm_encoder *tmp; |
| 574 | struct radeon_encoder *target = NULL; |
| 575 | |
| 576 | /* Find the encoder controlling the brightness */ |
| 577 | list_for_each_entry(tmp, &rdev->ddev->mode_config.encoder_list, |
| 578 | head) { |
| 579 | struct radeon_encoder *enc = to_radeon_encoder(tmp); |
| 580 | struct radeon_encoder_atom_dig *dig = enc->enc_priv; |
| 581 | |
| 582 | if ((enc->devices & (ATOM_DEVICE_LCD_SUPPORT)) && |
| 583 | dig->bl_dev != NULL) { |
| 584 | target = enc; |
| 585 | break; |
| 586 | } |
| 587 | } |
| 588 | |
| 589 | atif->backlight_ctl = target; |
| 590 | if (!target) { |
| 591 | /* Brightness change notification is enabled, but we |
| 592 | * didn't find a backlight controller, this should |
| 593 | * never happen. |
| 594 | */ |
| 595 | DRM_ERROR("Cannot find a backlight controller\n"); |
| 596 | } |
| 597 | } |
| 598 | |
Luca Tettamanti | ce3cf82 | 2012-07-30 21:16:06 +0200 | [diff] [blame] | 599 | if (atif->functions.sbios_requests && !atif->functions.system_params) { |
| 600 | /* XXX check this workraround, if sbios request function is |
| 601 | * present we have to see how it's configured in the system |
| 602 | * params |
| 603 | */ |
| 604 | atif->functions.system_params = true; |
| 605 | } |
| 606 | |
| 607 | if (atif->functions.system_params) { |
| 608 | ret = radeon_atif_get_notification_params(handle, |
| 609 | &atif->notification_cfg); |
| 610 | if (ret) { |
| 611 | DRM_DEBUG_DRIVER("Call to GET_SYSTEM_PARAMS failed: %d\n", |
| 612 | ret); |
| 613 | /* Disable notification */ |
| 614 | atif->notification_cfg.enabled = false; |
| 615 | } |
| 616 | } |
| 617 | |
| 618 | out: |
Alex Deucher | c491707 | 2012-07-31 17:14:35 -0400 | [diff] [blame] | 619 | rdev->acpi_nb.notifier_call = radeon_acpi_event; |
| 620 | register_acpi_notifier(&rdev->acpi_nb); |
| 621 | |
Luca Tettamanti | 8650467 | 2012-07-29 17:04:43 +0200 | [diff] [blame] | 622 | return ret; |
Alberto Milone | d7a2952 | 2010-07-06 11:40:24 -0400 | [diff] [blame] | 623 | } |
| 624 | |
Alex Deucher | c3c6516 | 2012-08-02 12:30:49 -0400 | [diff] [blame] | 625 | /** |
| 626 | * radeon_acpi_fini - tear down driver acpi support |
| 627 | * |
| 628 | * @rdev: radeon_device pointer |
| 629 | * |
| 630 | * Unregisters with the acpi notifier chain (all asics). |
| 631 | */ |
Alex Deucher | c491707 | 2012-07-31 17:14:35 -0400 | [diff] [blame] | 632 | void radeon_acpi_fini(struct radeon_device *rdev) |
| 633 | { |
| 634 | unregister_acpi_notifier(&rdev->acpi_nb); |
| 635 | } |